Received: by 2002:a05:7412:6592:b0:d7:7d3a:4fe2 with SMTP id m18csp606853rdg; Thu, 10 Aug 2023 13:11:31 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGiUjeUEQlTZuq56eGvHxDM/Eo7hdeiYngkO4jzh1wiw0BXUpgvU6KSkJPaASSYtfJxE6he X-Received: by 2002:a05:6a20:12c3:b0:140:3cc7:b3a6 with SMTP id v3-20020a056a2012c300b001403cc7b3a6mr4197513pzg.4.1691698290762; Thu, 10 Aug 2023 13:11:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691698290; cv=none; d=google.com; s=arc-20160816; b=RSS1q1X7Qb4Zq0kJb65XOOpK147hxmO1GlmSRS6GBEYiEuLaR9FiVWiQtYKcFwFfnR WMhJEtW8jwkAMCqAGpYs8YtvZPb9ZVIJCinkQtaPB+zzc3PSWh+Q4DKbgfj06lfH66w4 s6QmbakNdO4dqTk/JJqxX4/BYy5GQaaSp4l4AA7z5Eoj9hII4jgn7VGFmLZJxWAuFY26 lPsP4YyvUcOJnrJCIuEvMhFhTVcNpqre51Gx8bUiTzv+5wCjdewC2LMGO4lvHEBdHT1Z ASZJsp1E38XTpVyvZK6Po90M+UPNAsl050yfcruruZ3V8fQQmMFz5zr1a1sW8rXy9o9c xYDA== 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:mime-version:date :dkim-signature; bh=JVwAsL8GyIj4mx5rACaLqR7121szF8L7kVa/VdSYhKk=; fh=vgwy83u71T67ZVqecCxaI/mFlQEtXT0uJbK1d1LCg8M=; b=I1dZ8hVc3cmBA0qXkLaWtIXKmhCG7lQZAZ77AssgQ/dz3SqQZjz+XsemXkqU/TlfJ2 p8xhAiMeV2n3NmfCsLGegBPNlALVXXwaJfHAw5XFSHZUMTjfNEfs8+9abziqLAH6ClCB RwSy2bodDkZJ1WgyXhLGCfdpDmegqQoexnkX+Sj61mj8H/hIoITxlAXfzRZggDlcoBHw VxgbPWSVeMKo3EBG6Ww5CdmY76j47r0og6DQQtyBafa6tY2BDCBOlM8Q2/XIFxk1Azz6 oRAjPT6PcA1m8lIh3Sc4MyEhV+fFCf3rF7L9+DWjlG5+8vqoU2OGaF1Sxxmty97wSvxB Skew== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b="W/I0jMUU"; 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 132-20020a63028a000000b005599cbfa257si2052056pgc.275.2023.08.10.13.11.16; Thu, 10 Aug 2023 13:11:30 -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="W/I0jMUU"; 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 S234124AbjHJTVt (ORCPT + 99 others); Thu, 10 Aug 2023 15:21:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36276 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230197AbjHJTVr (ORCPT ); Thu, 10 Aug 2023 15:21:47 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 72A0B8E for ; Thu, 10 Aug 2023 12:21:47 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5840614b13cso23921677b3.0 for ; Thu, 10 Aug 2023 12:21:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1691695306; x=1692300106; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=JVwAsL8GyIj4mx5rACaLqR7121szF8L7kVa/VdSYhKk=; b=W/I0jMUUR76P+6pY9PNw6/9lrs82do/vNTSTR7MQbf0T5sxD5ee07o/AopBu6jRp2E WZnCIRWERMlxTbBmgviUTWUKyaRGjZ2Xtt1BFf1MsbHT42k3S+u6IuCKQHRfDmTvVkCE Cgwa+5SM2CMJrzCWkOBVh5eYlaF0Vb0S9olgCPGKYX3KqTvn3i8MswXT95LHLMmNMoew bn/cMAlDdhvhTpkNRRFfiCYCF4vOpk6ZCG73MqBGRK/Sb7T78Wp3u8WtYw8fSZ2yrpmE q8mlC5QOTvl4xRYwIKw3SSeZQe5wrTD/reHyzKv2bJRFQMN2wpSoIKnnaNwfM+A/yBCh BIMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691695306; x=1692300106; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=JVwAsL8GyIj4mx5rACaLqR7121szF8L7kVa/VdSYhKk=; b=JO0a1gh3oogdc/D4wLArtCD0epHSOdWaVzyQ/MkzRDnIOplZEJnm4cqhAacswuuF7n Bz4w4lO5fkMMa+x0Gwv6K+nhT+KPRFzF+crUqakJRnxkian5iXIqmGLiWuZ8WjDbimCN LDSCgWFdt/FgyvUQCfGBgh+llVbZtYnmPBYVAKD5CBiQ23s39aPChe58oVkbpPeto3Cy wwFsXU78Qv4H/VIXPIGKshHrYiI+uVFx1B/DGuBeoXlgj8fStDWPoCqAgx0gn0EXlE1R +u0B/z+qM/2uynaBaQMp1+Vixa2Nx7qQES4c7NAEUzMSTrU9qNj28k8O7+ER1JZWVNip kUeg== X-Gm-Message-State: AOJu0Yy11Y9Y8dmvngeC5EWrHnPy3BzT4z1DEdwuanP/DvugrMNLLYMt Ps9/5ZOb3Qdt+dBGHzAm+Bu5gapRJoolfRzlYdah X-Received: from axel.svl.corp.google.com ([2620:15c:2a3:200:cc07:13ef:656b:e8de]) (user=axelrasmussen job=sendgmr) by 2002:a81:a94a:0:b0:56c:ed45:442c with SMTP id g71-20020a81a94a000000b0056ced45442cmr65776ywh.5.1691695306687; Thu, 10 Aug 2023 12:21:46 -0700 (PDT) Date: Thu, 10 Aug 2023 12:21:28 -0700 Mime-Version: 1.0 X-Mailer: git-send-email 2.41.0.640.ga95def55d0-goog Message-ID: <20230810192128.1855570-1-axelrasmussen@google.com> Subject: [PATCH mm-unstable fix] mm: userfaultfd: check for start + len overflow in validate_range: fix From: Axel Rasmussen To: Alexander Viro , Andrew Morton , Brian Geffon , Christian Brauner , David Hildenbrand , Gaosheng Cui , Huang Ying , Hugh Dickins , James Houghton , 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 , Steven Barrett , 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,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 A previous fixup to this commit fixed one issue, but introduced another: we're now overly strict when validating the src address for UFFDIO_COPY. Most of the validation in validate_range is useful to apply to src as well as dst, but page alignment is only a requirement for dst, not src. So, split the function up so src can use an "unaligned" variant, while still allowing us to share the majority of the code between the different cases. Reported-by: Ryan Roberts Closes: https://lore.kernel.org/linux-mm/8fbb5965-28f7-4e9a-ac04-1406ed8fc2d4@arm.com/T/#t Signed-off-by: Axel Rasmussen --- fs/userfaultfd.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index bb5c474a0a77..1091cb461747 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -1287,13 +1287,11 @@ static __always_inline void wake_userfault(struct userfaultfd_ctx *ctx, __wake_userfault(ctx, range); } -static __always_inline int validate_range(struct mm_struct *mm, - __u64 start, __u64 len) +static __always_inline int validate_unaligned_range( + struct mm_struct *mm, __u64 start, __u64 len) { __u64 task_size = mm->task_size; - if (start & ~PAGE_MASK) - return -EINVAL; if (len & ~PAGE_MASK) return -EINVAL; if (!len) @@ -1309,6 +1307,15 @@ static __always_inline int validate_range(struct mm_struct *mm, return 0; } +static __always_inline int validate_range(struct mm_struct *mm, + __u64 start, __u64 len) +{ + if (start & ~PAGE_MASK) + return -EINVAL; + + return validate_unaligned_range(mm, start, len); +} + static int userfaultfd_register(struct userfaultfd_ctx *ctx, unsigned long arg) { @@ -1759,7 +1766,8 @@ static int userfaultfd_copy(struct userfaultfd_ctx *ctx, sizeof(uffdio_copy)-sizeof(__s64))) goto out; - ret = validate_range(ctx->mm, uffdio_copy.src, uffdio_copy.len); + ret = validate_unaligned_range(ctx->mm, uffdio_copy.src, + uffdio_copy.len); if (ret) goto out; ret = validate_range(ctx->mm, uffdio_copy.dst, uffdio_copy.len); -- 2.41.0.640.ga95def55d0-goog