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=1796382951; b=ShSsdBkca9cQqEAZ0A1gxsaJTwl7kFzGMMhrLuaHBtiRHdETFU5u2o1AAlYqCa4XRTbT e/TqPSllj0prglIhhkfVhluBJswTQrAPXeuIZFBHZ9ENfQKra/r1vv+jymdGGrXwDd2Q5 hDdGIYmArC2bkx+TmfBq4TqJjAbrM3nDmnVT9YPn40cxRryB1/G40r2LqOgC+rWKqxt0d EFNLmxPuHBH/by6en9C1WK5oHZeU7z/kXMKhUx+bipNlLS5ZJoObwasjNa/iQe+n7InSO /vGIL0BNE7GA9beFVCsvhxgJQOnY9FhxUdNbnKGom/nxxx8GQm0zTkJxiSJbCdCUgIw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796382951; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=r6vGOJ1fE5nMmaUG4O09J23py/ZffoulP2J0l7N4F/I=; b=tqN1sPs+jRPXWyviPd6I4kZj1VJzoXcKFjlNL8WdyEuED9pJO78/9zXtcQTXc/TqIXic F3qGG76aXS0z3x6R+W609A25acnbzjDcJKej3fqsWew2J1rsYnpT17ucUGxMP/cV0cUdI 3hIWt3AVIPQNw524OsNg9r8SrrvvpeKUk4uB2AG/WNStCnjm7aKEfH3bL0as1fjJdN4qv AoBa+0eMELrWWGVX2JeVgEBiQpUFVurW/ZLAWVjri/kUzEdYxYlpLX6/6A0IdOdRuvVXL PRBk6os5Bd8n1ifNq8VzENUkn7Xdc6EbX9vlFqgn9RXFYWgoWa37OJvYKV3JsxMg28Q== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=iE5cOuxh; 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 C1BB7606A339; Fri, 4 Dec 2026 11:15:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=iE5cOuxh Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1DD661F000C2; Fri, 4 Dec 2026 11:15:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796382951; bh=r6vGOJ1fE5nMmaUG4O09J23py/ZffoulP2J0l7N4F/I=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=iE5cOuxhZRh/oejJmYnmUe2Yr8Pd3chOQf/LdamZe6XFPr6PSBXms0Xefiqzi4s5N bmOTehC4KK12W5AzHsV3lJijMhTObv2NI8Kz1BKxUSnToiKqAzaQQkASE4Rpypf/sm A6n0vvqXIJB0pIZA3/JRvcVk4YsupO7Y8paURx7A= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, 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 662/703] userfaultfd: validate the whole range before changing write protection Date: Fri, 4 Dec 2026 19:09:21 +0900 Message-ID: <20261204100645.395877383@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100627.522266145@linuxfoundation.org> References: <20261204100627.522266145@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore 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 6.18-stable review patch. If anyone has any objections, please let me know. ------------------ 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 Signed-off-by: Greg Kroah-Hartman --- mm/userfaultfd.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -961,21 +961,26 @@ int mwriteprotect_range(struct userfault 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);