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=1796381525; b=lhG4g8uJmyyQNj/xXOyHgJpZfqkz20qX39dfLAfFRazF3eobu50W09zQtTKBXsLWFmbR 1WqejwTTzCS4GuEWNGcQ0uouiIcqbP6erJhUieMoyRmH0/Tm0yTDRJFzVtmWWSFlFjkAA MeibJATPEcvEDWJ8+BrKArDMrisRsjl6v/ydM8v2qbhyLNd1U2DMlBymkO5DytSZVwXz4 DW6dnZ0edzoesPc9xu1q+o+FSHktwRz4b1EItWltYJXJWapih/NKoigiO86YzJhr51W6h 0otlFahYpocjMtfan4vAqW12gPJFt6GVNIjLTcfgXLalNBlmOTy/6U6Trt2n9XAhwsA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796381525; 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=RxSnpZjHvJYnbnxOnXIMYo6GX8w0d3Arg8Io1fm5xcs=; b=ZQLolOiWCTfng9k3Oq+fEE1wo1VfXkDYtr/1ws6sbchjnSIUuvGFiqypVWGRAL1VfbqP WYmFUosqJQzNzz5o5MkL+s6podc7x9QGycpbLBVQ5pvWgdyUnK63O3GH+ukaiUK0v+5UA BNxI2NClKmm/LUjfPTMj+aSPq1r1o4B+wY/i2SYttBqMxxoUlONSutOzb1yZv4745Mrs9 vq9Xjh4FvdvKieWJz+EPUD9fJaKLSsCF+lDhj8e6seXPRwwdYShP4o+Lcxj7IypH+mKyS hfGdbBh8qigXQM4jBpMaWA3SPA2+qeSUPoGIZIfVjSz4Hg3GWgElu+zfcjs+gDbKOcQ== 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=W8ULHlUZ; 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 6CBBC606A33D; Fri, 4 Dec 2026 10:52:05 +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=W8ULHlUZ Received: by smtp.kernel.org (Postfix) with ESMTPSA id E1C9B1F000C2; Fri, 4 Dec 2026 10:52:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796381525; bh=RxSnpZjHvJYnbnxOnXIMYo6GX8w0d3Arg8Io1fm5xcs=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=W8ULHlUZMTAuZI0jDdaa1EcMUdzdjzJ4/AFQ1fcUGrNU/1TJH8bhpjpGgzSQrBtvf RXFgSV3N/IMEGB11zNEEDDcu5dE7BlF563753VtzLqkrsucoEJLW+d6Ok3mFu4IGuE KMEg95YvqhNWDdqtjCb/bikpN1wGiVia0Q2xAgFo= 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 7.3 740/742] userfaultfd: validate the whole range before changing write protection Date: Fri, 4 Dec 2026 19:10:04 +0900 Message-ID: <20261204100654.772223165@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100635.034361255@linuxfoundation.org> References: <20261204100635.034361255@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 7.3-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: Replaced vma_is_hugetlb() with is_vm_hugetlb_page() in the new validation loop of mwriteprotect_range(), since the former helper does not exist in this tree. ] 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 @@ -1136,21 +1136,26 @@ static int mwriteprotect_range(struct us 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);