From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796360469; b=ABSfZ1vFg8Hh1/ZMl2emtWmj0e4dVQUO1AYh9WQEa3KUYinXOcyzYmT+LU/B+kay8Lz2 XE3muV53Qgw1R3zTTISIvYFc5M4xeDMEVOzMy4rgSbvkwCjhNLppr71M1jHLTK/r9OTbe u9MHKFlkp6p/0NbVrIsYCM7WHgw2afc4sK1rrAzcmjZkKqVLiKqduTeYSS41u2c6gcko4 hf6Snk4Qd8+L+FdnqOdsvXZH7PBY2FiEsulqxsXh4jriDEjFSJa91JkvVNC5OW5gaHYrh HH4eMHjM5FmZ+S0dMVQRIA/+jp5mWFAYLcxhPvX4h5LHdBItjiB1twgUzgb/Earlzgg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796360469; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=lmdF2aCycrZfGVJdxNbIC0xd7MqRIjvaxPzVafw/HQE=; b=bxsIdLz/jaWxFne5k3A8RMZUR14aZLe2fpNvU3FnDHBNejcH7q5Ky0LkmZP8ruN9f77v nEHo1RV+W301K972EuWtv02hcyEhhGYCjpG668rovC59StrNbH0MmoVPgNYtoYxXQFFol 6MDnwqFst7qiPIBJVSnT2VNh0Vdo4CC1hP1NhRe87B4aY+fqUN3jshue9VpE0j/4UZ0gS WZer5PwHT6egvd0sGBCR1BWbYqbBe2ew7BJNFryqK7CToi0ZwoJvGccrk78pspcC3eOH+ LzeSypXoNlg5V1OPsWDGu4PXi1caR28iT9wOOEfTHoPhh+XhJlTfJ0IpTcDYFQTlRYQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=e7y6rth0; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EC471606A322 for ; Fri, 4 Dec 2026 05:01:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=e7y6rth0 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3B9561F000C2; Fri, 4 Dec 2026 05:01:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796360468; bh=lmdF2aCycrZfGVJdxNbIC0xd7MqRIjvaxPzVafw/HQE=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=e7y6rth0cuWy+USUgHO7WJH02exJyMp3zpzaVk4XX5+QScBAMqwJK6gAZy7yW6n3J OrDX1svU8QyvX0xKBeageDyuDaJWfrdMuVBkuYrm3vmrK8+uHG6tyxsyzVlQSe/T09 +iLolcrAeD9NY3s5nyhGWhJ77eynYWxuzhV2+31/sKcsbvsArVKlfxoH+vuGt+aTrH KYILdo19tq4FFVo2iGdrI4pxJ8PWp4BSBfXiU/bnQJAo1ucwLTADbjjgjPt0mfaCJ5 RnsQLzSEXYN9v/TMiAxMuEMeLWs37ceuYyIPbT42iV5F24PPrItXJs2+ilCRwb+lqV +PWNyln5VdN0A== From: Sasha Levin To: stable@vger.kernel.org Cc: Umang Pokhriyal , Andrew Morton , "Mike Rapoport (Microsoft)" , Peter Xu , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Suren Baghdasaryan , Michal Hocko , Sasha Levin Subject: [PATCH 6.12.y] userfaultfd: validate the whole range before changing write protection Date: Fri, 4 Dec 2026 00:01:05 -0500 Message-ID: <20261204050049.2704060-1-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <2026120259-emotion-pastrami-2bd8@gregkh> References: <2026120259-emotion-pastrami-2bd8@gregkh> X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Umang Pokhriyal [ Upstream commit 28ae0385b638f095109d282b927ac1a51f75c530 ] mwriteprotect_range() checks each VMA as it walks the range. When a later VMA is not registered for write protection, or is a hugetlb VMA the range is not aligned to, the call fails after the earlier VMAs were already changed. When unprotecting, userfaultfd_writeprotect() then skips wake_userfault(), so threads blocked on those pages are not woken although the pages are writable again. An unprivileged process can trigger this with a range that spans an unregistered VMA. It hasn't been seen in practice; it was found while reviewing the userfaultfd range ioctls with an LLM. Validate the whole range first, as mrwprotect_range() already does. Link: https://lore.kernel.org/20261104093457.152245-1-umangpokhriyall@gmail.com Fixes: a1b92a3f1498 ("mm/userfaultfd: support WP on multiple VMAs") Signed-off-by: Umang Pokhriyal Signed-off-by: Andrew Morton Reviewed-by: Mike Rapoport (Microsoft) Cc: Peter Xu Cc: David Hildenbrand Cc: Lorenzo Stoakes Cc: Liam R. Howlett Cc: Vlastimil Babka Cc: Suren Baghdasaryan Cc: Michal Hocko Cc: [ sashal: adapted the hugetlb VMA check to use is_vm_hugetlb_page() instead of vma_is_hugetlb(), and adjusted hunk context for mwriteprotect_range() not being static. ] Signed-off-by: Sasha Levin --- mm/userfaultfd.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index f592f92c74079..f8cfb03dfed0d 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -965,21 +965,26 @@ int mwriteprotect_range(struct userfaultfd_ctx *ctx, unsigned long start, if (atomic_read(&ctx->mmap_changing)) goto out_unlock; + /* + * Validate every spanned VMA before changing any of them, so an + * invalid range is rejected without changing part of it. + */ err = -ENOENT; for_each_vma_range(vmi, dst_vma, end) { - - if (!userfaultfd_wp(dst_vma)) { - err = -ENOENT; - break; - } + if (!userfaultfd_wp(dst_vma)) + goto out_unlock; if (is_vm_hugetlb_page(dst_vma)) { - err = -EINVAL; page_mask = vma_kernel_pagesize(dst_vma) - 1; - if ((start & page_mask) || (len & page_mask)) - break; + if ((start & page_mask) || (len & page_mask)) { + err = -EINVAL; + goto out_unlock; + } } + } + vma_iter_set(&vmi, start); + for_each_vma_range(vmi, dst_vma, end) { _start = max(dst_vma->vm_start, start); _end = min(dst_vma->vm_end, end); -- 2.53.0