From patchwork Sun May 14 17:27:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13240537 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id A5E55C7EE22 for ; Sun, 14 May 2023 17:27:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9C110900003; Sun, 14 May 2023 13:27:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 96FCE900002; Sun, 14 May 2023 13:27:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 83813900003; Sun, 14 May 2023 13:27:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 74DA2900002 for ; Sun, 14 May 2023 13:27:39 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 39199141213 for ; Sun, 14 May 2023 17:27:39 +0000 (UTC) X-FDA: 80789542638.02.69B90FB Received: from mail-wm1-f42.google.com (mail-wm1-f42.google.com [209.85.128.42]) by imf21.hostedemail.com (Postfix) with ESMTP id 672C01C0006 for ; Sun, 14 May 2023 17:27:37 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=GCG2lzdB; spf=pass (imf21.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.42 as permitted sender) smtp.mailfrom=lstoakes@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1684085257; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=uUF48M/gKd1iv/2X/7XjPdwpczkBf5R4Jm0U4/h4Sxg=; b=SrgpXFT70zrnPdUF0tjC7MCRHLs2/IpgljpqoTQOZMR6o+UIJP70qqeF5LeTAfvwQobnd3 sY4GFe9G3854esa8nNBBxxfhiUdZZpGKwkto47MXEsNjtRjH/QuWUKWITAVviUc63g4M/f 5rXCQlodkoD8CBN16Bt4rSxP8Cc1YNE= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1684085257; a=rsa-sha256; cv=none; b=pUXaQwFMo4u7cPWl9pfVasuzJDXdrpkumu98WoOvSWXmIXsmBtSc8JBqP/F6SRnA+3yNR5 Qx3I2KGvonsE3RLm/f6YaMFiGO1DpsqH17PpL3LTfIJgecWw+P6/eQ5cpLrJncvZ6ilg48 FBCZ/SYBMObTCMJWf/P7oF9KeBrRpOI= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=GCG2lzdB; spf=pass (imf21.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.42 as permitted sender) smtp.mailfrom=lstoakes@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-wm1-f42.google.com with SMTP id 5b1f17b1804b1-3f450815d02so34362005e9.0 for ; Sun, 14 May 2023 10:27:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684085255; x=1686677255; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=uUF48M/gKd1iv/2X/7XjPdwpczkBf5R4Jm0U4/h4Sxg=; b=GCG2lzdBfPYkNSmWKn7eWpRh09Mf6rxqbCPxdzx5D9hYB7d5ZScC6OnPRHsMyM3foV LPrzeoR65Fcdv1xH3KjIPOOrxAktbQZMmHEmN+UX2Lu9avbXXnHehn9t3UCR6bd8uMw0 4oIr+H8DAOS0o9r6Wnbx5dB7F9LBYgnKDFBmnfgP6UtttdtVpwZJ35d2nwKhMP6zAJuD KaEY+lF8LK7px+niWvhmtQbNID5IEVT8egODaO9ZOxEKdBKkv3GQVfVtSI/XksfO3iDm J5bCfXdoxN6U22Xo4X4aEvOXwH13ky32GZIGrgjgG0v9WWSjGqPVmPAfF7130IpzTyhK UY5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684085255; x=1686677255; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=uUF48M/gKd1iv/2X/7XjPdwpczkBf5R4Jm0U4/h4Sxg=; b=RBu+RzHzONY/GP6u/w3E6oNLmgSsWvQkxVxQgcAOzLe2ae3EjilvDPVqG4Nptroty4 vhjpADx6A5kgyppQYWhsAuHQ0zSdqf4yYORKpN5Nn2SjwSwECmdiI8trUNpTzeALWblM WIDVmWednrglG/ZCs+eoyiIbY//cjrjoPNeghIkf75VnGOCOiJ8L238/3/M13A0wsQ/c SayI5EMem68FAQ0LOEOz0ManbEULBkYW2GgsmXeTifuPyJOPrJDeZBBv1U9yQCCsAxdC e8QCPCuWQl80Gk0Ip3/MaexCurRFvSNU3bvtg92d9k+XA0rezOyVVQ2UjF/H9YqKcQ26 a2Bg== X-Gm-Message-State: AC+VfDygWRG25sCoKAqigfmsOgD5jKfxy2be/4y5GsQvZNuQUnZKxvyr GbEFoD01RUQh5WGfgvJtYNTJqB2IK3A= X-Google-Smtp-Source: ACHHUZ7cEvZO1PaTOCiVtVDIInd+jEL6foY7nJ0y26zuxv4a/EjIZpfrIjyS8FPrtoYx4hkk5isM+Q== X-Received: by 2002:a05:6000:18cd:b0:306:2841:7abb with SMTP id w13-20020a05600018cd00b0030628417abbmr19510223wrq.20.1684085254814; Sun, 14 May 2023 10:27:34 -0700 (PDT) Received: from lucifer.home ([2a00:23c5:dc8c:8701:1663:9a35:5a7b:1d76]) by smtp.googlemail.com with ESMTPSA id q10-20020adfcd8a000000b00307c8d6b4a0sm11203667wrj.26.2023.05.14.10.27.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 May 2023 10:27:33 -0700 (PDT) From: Lorenzo Stoakes To: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton Cc: Mike Rapoport , Peter Xu , Mark Rutland , "Liam R . Howlett" , Alexander Viro , Christian Brauner , Lorenzo Stoakes Subject: [PATCH for 6.4-rcX] mm: userfaultfd: avoid passing an invalid range to vma_merge() Date: Sun, 14 May 2023 18:27:31 +0100 Message-Id: <20230514172731.134188-1-lstoakes@gmail.com> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 X-Rspamd-Queue-Id: 672C01C0006 X-Rspam-User: X-Rspamd-Server: rspam06 X-Stat-Signature: 1i1up8y11unpwwtmi55shxu8ep76mjmz X-HE-Tag: 1684085257-252818 X-HE-Meta: U2FsdGVkX1+K/iqLhMryZWXtixixFn6/VCl4apcU2xGlJGiUWHA+AxX+b4Ex77wdkW+FsdUxF1aXRi0hCTe+27ZkieuyjFIEbrzKCprq7lXqm1vK1TAkMHmyBwKISnlR2VxMULKTor+zvo5vaDSh8owY1E8UdqiSS4Tu+rDBQ6ktdh5fSJUPze0UVN8NO4fPQUuqPnk1v+h9HNHnE5Clr/UhEzWhpt1funCrYeUb1PfSI/ETzJFFeb7H9YjSXfLxMwWXgmNpJ7aLzDtR+M1awG6KRKWsBd+QDK97+mEd3uuUe60X18tDq1IkW3QRbWrZUcx9DoFEi/6yqksy7vs/sLufnhsljXmgIIirHPit/gGbSC201ivf57zqaOX2AJQ81D1INcjSnA4QR/YEzkdtwGCE08NsPp+b5KEcn3OswkAmyfbV1I5FQFFfUBg3fU2ADpXESSPNYqpPzvQ4U7oAeGU8A7+FpCaQvG6aJy4gS3r3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ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: The userfaultfd_[un]register() functions will knowingly pass an invalid address range to vma_merge(), then rely on it failing to merge to indicate that the VMA should be split into a valid one. This is not something that should be relied upon, as vma_merge() implicitly assumes in cases 5-8 that curr->vm_start == addr. This is now enforced since commit b0729ae0ae67 ("mm/mmap/vma_merge: explicitly assign res, vma, extend invariants") with an explicit VM_WARN_ON() check. Since commit 29417d292bd0 ("mm/mmap/vma_merge: always check invariants") this check is performed unconditionally, which caused this assert to arise in tests performed by Mark [1]. This patch fixes the issue by performing the split operations before attempting to merge VMAs in both instances. The problematic operation is splitting the start of the VMA since we were clamping to the end of the VMA in any case, however it is useful to group both of the split operations together to avoid egregious goto's and to abstract the code between the functions. As well as fixing the repro described in [1] this also continues to pass uffd unit tests. [1]:https://lore.kernel.org/all/ZFunF7DmMdK05MoF@FVFF77S0Q05N.cambridge.arm.com Reported-by: Mark Rutland Closes: https://lore.kernel.org/all/ZFunF7DmMdK05MoF@FVFF77S0Q05N.cambridge.arm.com/ Signed-off-by: Lorenzo Stoakes --- fs/userfaultfd.c | 111 +++++++++++++++++++++++++++-------------------- 1 file changed, 63 insertions(+), 48 deletions(-) -- 2.40.1 diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index 0fd96d6e39ce..4453e7040157 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -1319,6 +1319,35 @@ static __always_inline int validate_range(struct mm_struct *mm, return 0; } +static int split_range(struct vma_iterator *vmi, + struct vm_area_struct *vma, + unsigned long start, + unsigned long end, + bool *can_merge) +{ + int ret; + bool merge = true; + + /* The range must always be clamped to the start of a VMA. */ + if (vma->vm_start < start) { + ret = split_vma(vmi, vma, start, 1); + if (ret) + return ret; + + merge = false; + } + + /* It must also be clamped to the end of a VMA. */ + if (vma->vm_end > end) { + ret = split_vma(vmi, vma, end, 0); + if (ret) + return ret; + } + + *can_merge = merge; + return 0; +} + static int userfaultfd_register(struct userfaultfd_ctx *ctx, unsigned long arg) { @@ -1330,7 +1359,7 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx, unsigned long vm_flags, new_flags; bool found; bool basic_ioctls; - unsigned long start, end, vma_end; + unsigned long start, end; struct vma_iterator vmi; user_uffdio_register = (struct uffdio_register __user *) arg; @@ -1462,6 +1491,8 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx, ret = 0; for_each_vma_range(vmi, vma, end) { + bool can_merge; + cond_resched(); BUG_ON(!vma_can_userfault(vma, vm_flags)); @@ -1477,32 +1508,22 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx, (vma->vm_flags & vm_flags) == vm_flags) goto skip; - if (vma->vm_start > start) - start = vma->vm_start; - vma_end = min(end, vma->vm_end); + ret = split_range(&vmi, vma, start, end, &can_merge); + if (ret) + break; new_flags = (vma->vm_flags & ~__VM_UFFD_FLAGS) | vm_flags; - prev = vma_merge(&vmi, mm, prev, start, vma_end, new_flags, - vma->anon_vma, vma->vm_file, vma->vm_pgoff, - vma_policy(vma), - ((struct vm_userfaultfd_ctx){ ctx }), - anon_vma_name(vma)); - if (prev) { + if (can_merge) { + prev = vma_merge(&vmi, mm, prev, vma->vm_start, vma->vm_end, new_flags, + vma->anon_vma, vma->vm_file, vma->vm_pgoff, + vma_policy(vma), + ((struct vm_userfaultfd_ctx){ ctx }), + anon_vma_name(vma)); + /* vma_merge() invalidated the mas */ - vma = prev; - goto next; - } - if (vma->vm_start < start) { - ret = split_vma(&vmi, vma, start, 1); - if (ret) - break; - } - if (vma->vm_end > end) { - ret = split_vma(&vmi, vma, end, 0); - if (ret) - break; + if (prev) + vma = prev; } - next: /* * In the vma_merge() successful mprotect-like case 8: * the next vma was merged into the current one and @@ -1560,7 +1581,7 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx, struct uffdio_range uffdio_unregister; unsigned long new_flags; bool found; - unsigned long start, end, vma_end; + unsigned long start, end; const void __user *buf = (void __user *)arg; struct vma_iterator vmi; @@ -1627,6 +1648,8 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx, prev = vma_prev(&vmi); ret = 0; for_each_vma_range(vmi, vma, end) { + bool can_merge; + cond_resched(); BUG_ON(!vma_can_userfault(vma, vma->vm_flags)); @@ -1640,9 +1663,9 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx, WARN_ON(!(vma->vm_flags & VM_MAYWRITE)); - if (vma->vm_start > start) - start = vma->vm_start; - vma_end = min(end, vma->vm_end); + ret = split_range(&vmi, vma, start, end, &can_merge); + if (ret) + break; if (userfaultfd_missing(vma)) { /* @@ -1652,35 +1675,27 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx, * UFFDIO_WAKE explicitly. */ struct userfaultfd_wake_range range; - range.start = start; - range.len = vma_end - start; + range.start = vma->vm_start; + range.len = vma->vm_end - vma->vm_start; wake_userfault(vma->vm_userfaultfd_ctx.ctx, &range); } /* Reset ptes for the whole vma range if wr-protected */ if (userfaultfd_wp(vma)) - uffd_wp_range(vma, start, vma_end - start, false); + uffd_wp_range(vma, vma->vm_start, + vma->vm_end - vma->vm_start, false); new_flags = vma->vm_flags & ~__VM_UFFD_FLAGS; - prev = vma_merge(&vmi, mm, prev, start, vma_end, new_flags, - vma->anon_vma, vma->vm_file, vma->vm_pgoff, - vma_policy(vma), - NULL_VM_UFFD_CTX, anon_vma_name(vma)); - if (prev) { - vma = prev; - goto next; - } - if (vma->vm_start < start) { - ret = split_vma(&vmi, vma, start, 1); - if (ret) - break; - } - if (vma->vm_end > end) { - ret = split_vma(&vmi, vma, end, 0); - if (ret) - break; + if (can_merge) { + prev = vma_merge(&vmi, mm, prev, vma->vm_start, + vma->vm_end, new_flags, vma->anon_vma, + vma->vm_file, vma->vm_pgoff, + vma_policy(vma), + NULL_VM_UFFD_CTX, anon_vma_name(vma)); + /* vma_merge() invalidated the mas */ + if (prev) + vma = prev; } - next: /* * In the vma_merge() successful mprotect-like case 8: * the next vma was merged into the current one and