Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp31120020rwd; Thu, 6 Jul 2023 16:18:19 -0700 (PDT) X-Google-Smtp-Source: APBJJlFjv9myozn1UBuuaNv71apAgmtY4og+eUN+hnvfJDmi8VgHApBawNaR40QPY64yV84Hm+1U X-Received: by 2002:a9d:5902:0:b0:6b8:51a9:e2a3 with SMTP id t2-20020a9d5902000000b006b851a9e2a3mr3149498oth.27.1688685498802; Thu, 06 Jul 2023 16:18:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688685498; cv=none; d=google.com; s=arc-20160816; b=s6uYnqnwG7hQzcTYn9e4ZBZ5H7Yf86jiPAk4kfVQd7SY/Da/RjvKdgLvop7HqeOrJe +tH6y/Nwmh9mko7/G+/GCac0wrGDhqIbeuD3+jtUWc5/IyZwB4SPy/363a8Hme6RN9+W fgodnVEer9TrAzpjDmbEjjDgLwPz87vGbqgvwr7iHxBpAUcNRgoLX4y94qZqQMTIGzYr RJL8QVdU7OtyCTMXff2DebtwI/a300p2bSUZWbpU0NvKXzFjnLOLmHrcQgFyJQLcfuWE gAx3soGY2MWk+ogSnHQ9Mz1WuKnNg1laYBD6NMq3D+2I5jKYVKkgog8mGZu0QBHAm1bu /ymA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=dr4h9VGDfsn/VMT+xHDPiOp9dXBZSKUD38K7YaReMD4=; fh=Opjbx899L+35XA6SNQJRoMm3wFpvelEj3IooMxkZLjI=; b=MUEicoE0M0n7+SQx/CcyOqt4RzahoMg2kv2CuP8vNl7PD+HdAaCOTIXy90h8YnFRw+ JE3KM3/Q5ZaofJQ0RpFXOJputCNjj/UYoZ2J/tF5dXPiQcr+1ekIw/DZWVRbi8L51s6Z ybfnxoqXk8Bww2CTvnOHAylbz+aNK2O6pM2IsVaihhSim/bP42Z+fKrwSkhczTvhhFxx UwvNfsSipJUb53o/+tv9SOBwnDjiVpoywwjmmB5Fen/W3v6QcMO/vMHoFyYesZ5vkCmx LbRLn+plgX7XwlF9GrSUvA9Fs2em//USAF16eVPHFZwsgyPcVAENR5e5M7JvcgcLN7uH NPtQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=oG6cUbXq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s186-20020a6377c3000000b0054ff40830b5si2308081pgc.384.2023.07.06.16.17.58; Thu, 06 Jul 2023 16:18:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=oG6cUbXq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231781AbjGFWvG (ORCPT + 99 others); Thu, 6 Jul 2023 18:51:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56338 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231450AbjGFWu4 (ORCPT ); Thu, 6 Jul 2023 18:50:56 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1690E1BC2 for ; Thu, 6 Jul 2023 15:50:54 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-c5e3d2c339aso1226519276.3 for ; Thu, 06 Jul 2023 15:50:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1688683853; x=1691275853; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=dr4h9VGDfsn/VMT+xHDPiOp9dXBZSKUD38K7YaReMD4=; b=oG6cUbXqueLNKCE01QfHG46TYxjrnSczrRs1z4fAd0XehnlwpZ6QzxEJt4W2Ue3oTg jmIlcVMPOO3wBc5Jmpx0+P7PdoVG9zHOG4l9JNotEr1VVRKn8UBqflwPIUBZiM7amwz3 tHjgfEOpZHvakwUYyo/kFliQBAwyxEYaH9p9Krdz4uCEX03XtWL0k4huwPf4JMBUWiuJ vH0FF8WK3+d9dnq2JbKxfTWxmC9bR4z1x++0EIg8VN51Y6zcrk9sFkWFs+n0Ee8WaTrI T/JjfeQ68QMFZsZ0wuD8e82BwZ+qeWxNN+55lMptu2IruYWfXF6lh4uEunTtWoQohPkt /enw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688683853; x=1691275853; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=dr4h9VGDfsn/VMT+xHDPiOp9dXBZSKUD38K7YaReMD4=; b=Ody9o9YN03XQFVmqKdHC0dsLFOqopHqmrz5REKN/qr2Q0MWDVnBusTBHmZkh9ijBxp YV3dZDNsWFu7RUv13rLDhyWDE+vx2zgngfBnkAtM+avjb/tmrszahvFYFxjieQSlwUxB nLLdzGzk1o/ndRzZtZw55oCXAaQo4Pt+X1AbBW5H3ysWbJ/DolQy//neDUzYdGknplcD Dnxu/gf9fRLuJNhUrflldK2fxSo2/sWd70PMgeS6NPAFESNWPsFOiT4orJsx+cyBSQUS nDEtHl1uZrZrJuFZ9JrCIHDe8WjdnuuDHU8fgHlpG2kEz/eJOwV457vtJ3SYwxbn3KCY 8VJQ== X-Gm-Message-State: ABy/qLa672gaxpMqeZFisQrRxm3XtK/m6VIdUrovZeWTKeGadKXzqJbW XJsHONQwplevPRKZx7cCpkakZZUzVumFo2pGqxaZ X-Received: from axel.svl.corp.google.com ([2620:15c:2a3:200:bec3:2b1c:87a:fca2]) (user=axelrasmussen job=sendgmr) by 2002:a25:c70e:0:b0:c67:e177:100a with SMTP id w14-20020a25c70e000000b00c67e177100amr19507ybe.4.1688683853333; Thu, 06 Jul 2023 15:50:53 -0700 (PDT) Date: Thu, 6 Jul 2023 15:50:30 -0700 In-Reply-To: <20230706225037.1164380-1-axelrasmussen@google.com> Mime-Version: 1.0 References: <20230706225037.1164380-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230706225037.1164380-3-axelrasmussen@google.com> Subject: [PATCH v3 2/8] mm: userfaultfd: check for start + len overflow in validate_range From: Axel Rasmussen To: Alexander Viro , Andrew Morton , Brian Geffon , Christian Brauner , David Hildenbrand , Gaosheng Cui , Huang Ying , Hugh Dickins , James Houghton , "Jan Alexander Steffens (heftig)" , Jiaqi Yan , Jonathan Corbet , Kefeng Wang , "Liam R. Howlett" , Miaohe Lin , Mike Kravetz , "Mike Rapoport (IBM)" , Muchun Song , Nadav Amit , Naoya Horiguchi , Peter Xu , Ryan Roberts , Shuah Khan , Suleiman Souhlal , Suren Baghdasaryan , "T.J. Alumbaugh" , Yu Zhao , ZhangPeng Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, Axel Rasmussen Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Most userfaultfd ioctls take a `start + len` range as an argument. We have the validate_range helper to check that such ranges are valid. However, some (but not all!) ioctls *also* check that `start + len` doesn't wrap around (overflow). Just check for this in validate_range. This saves some repetitive code, and adds the check to some ioctls which weren't bothering to check for it before. Signed-off-by: Axel Rasmussen --- fs/userfaultfd.c | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index 7cecd49e078b..2e84684c46f0 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -1306,6 +1306,8 @@ static __always_inline int validate_range(struct mm_struct *mm, return -EINVAL; if (len > task_size - start) return -EINVAL; + if (start + len <= start) + return -EINVAL; return 0; } @@ -1760,14 +1762,8 @@ static int userfaultfd_copy(struct userfaultfd_ctx *ctx, ret = validate_range(ctx->mm, uffdio_copy.dst, uffdio_copy.len); if (ret) goto out; - /* - * double check for wraparound just in case. copy_from_user() - * will later check uffdio_copy.src + uffdio_copy.len to fit - * in the userland range. - */ + ret = -EINVAL; - if (uffdio_copy.src + uffdio_copy.len <= uffdio_copy.src) - goto out; if (uffdio_copy.mode & ~(UFFDIO_COPY_MODE_DONTWAKE|UFFDIO_COPY_MODE_WP)) goto out; if (uffdio_copy.mode & UFFDIO_COPY_MODE_WP) @@ -1927,11 +1923,6 @@ static int userfaultfd_continue(struct userfaultfd_ctx *ctx, unsigned long arg) goto out; ret = -EINVAL; - /* double check for wraparound just in case. */ - if (uffdio_continue.range.start + uffdio_continue.range.len <= - uffdio_continue.range.start) { - goto out; - } if (uffdio_continue.mode & ~(UFFDIO_CONTINUE_MODE_DONTWAKE | UFFDIO_CONTINUE_MODE_WP)) goto out; -- 2.41.0.255.g8b1d071c50-goog