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=1796360254; b=hv8mizCPzH47f4y1VnfZnB0+hHjLXOgOqaAo+E+CJnZig3l4B2ardhPy3Q80iUEKM8hA vfqr8y0iUPvNV8vO7PUGbMOVwf3s6YyGgj24yuqaufzpDBRtk1ROSR/CIHoyxBeRLZs71 4yb0cDk6VZgpsbpgj3s/56zuJiGML1m4EK5fS5jMq0apCylnMSzbljWDnQ2yvK+xNjd/h eR5m6QPGJagGSngzXrEwFLJIkUeHL7otmcabyVOLxsJx7/OzLFKs5lmHMP+6m2MSEOXCe FO2Ut5vIYFT37kisYvwNfW0B9g+5Qb9tDwsX3Me0b3yEfsMDNW7le6k9XcrSgd/JxFw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796360254; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=px2es6FkeT6l5zlhkA20/ObmXeEIg37Y16Td1rzE1aI=; b=wBraeuCD9eG/0H1zfa63IsczTxEzCAbOVWZ6NHnJEeOr01w84R3AemEO/mPkMPqBTpA3 N/RoNNh3iO/ID9BBhzWoz3YEsf4kN3R3kxzKS2YDx89QDjKMuW3xHS6VE1EPX8m27+/ZF qbEF2KK4e5RbRBKsJSyKza4PbYj//04Ya5d3Djr+QDG1EpBG+E7KDOpkCN2DJ+pufh2xZ iNW4/tltFhqqtJ2C0e01gJs8I4YIZ50yZG9uxvREPQ5+RyAmprX5AWCORamCu1jzZqMps 61J8Y7wz4uwZmY1JrzwYhzpEYKh8b7VE+RIbWWWCZ85Gw+JBhPVVUeQ5yIJaUYgWhsw== 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=fAtp0WEy; 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 CC797606A322 for ; Fri, 4 Dec 2026 04:57:34 +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=fAtp0WEy Received: by smtp.kernel.org (Postfix) with ESMTPSA id 15DF91F000C2; Fri, 4 Dec 2026 04:57:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796360254; bh=px2es6FkeT6l5zlhkA20/ObmXeEIg37Y16Td1rzE1aI=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=fAtp0WEyUg6wk4R9x52max8BPr3X7jpCn/w8SB9zMvKyWIBcs/20CGtpYHdO6rhUM bh8E1SV5zseTyFshbnFhAcZChFvwZZtOH/Pu0bo6sFR8AIbANZt1bc6FR8icXRnXua Eaeeg6T3rCeQJpvf83jRTanfZwtENPQsVGXU80QHIWLx6EQrPQ/OyiMQS1yQ1RARIc eSYvvTfspSItW2K4Tv1egVz0sXQkfGQMRWfLGKDeYOn40AmvU+zxYZNmofbUCsRHcx ZSy/8Ws9B4AaMqgEtQjZpxTad3wppANT4VgsksVdcY25Kiy2HIzbed7f60MtzmI57F 30c3Uwvha+eiw== 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.18.y] userfaultfd: validate the whole range before changing write protection Date: Thu, 3 Dec 2026 23:57:31 -0500 Message-ID: <20261204045711.2695442-1-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <2026120258-corrosive-quail-beba@gregkh> References: <2026120258-corrosive-quail-beba@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 to the older tree by using is_vm_hugetlb_page() instead of vma_is_hugetlb() and dropping the static qualifier from the mwriteprotect_range() hunk context. ] 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 58157b60874e8..6e65f19576d2e 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -961,21 +961,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