* [PATCH 1/2] userfaultfd: validate the whole range before changing write protection [not found] <20261102103621.156928-1-umangpokhriyall@gmail.com> @ 2026-11-02 10:36 ` Umang Pokhriyal 2026-11-03 18:54 ` Mike Rapoport 0 siblings, 1 reply; 3+ messages in thread From: Umang Pokhriyal @ 2026-11-02 10:36 UTC (permalink / raw) To: Andrew Morton Cc: Mike Rapoport, Peter Xu, David Hildenbrand, Lorenzo Stoakes, Liam R . Howlett, Vlastimil Babka, Suren Baghdasaryan, Michal Hocko, Shuah Khan, linux-mm, linux-kselftest, linux-kernel, Umang Pokhriyal, stable 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. Fixes: a1b92a3f1498 ("mm/userfaultfd: support WP on multiple VMAs") Cc: stable@vger.kernel.org Signed-off-by: Umang Pokhriyal <umangpokhriyall@gmail.com> --- mm/userfaultfd.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index 87da92526..b8a9d9574 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -1105,21 +1105,31 @@ static 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; + goto out_unlock; } if (vma_is_hugetlb(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; + } } + err = 0; + } + if (err) + 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 ^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH 1/2] userfaultfd: validate the whole range before changing write protection 2026-11-02 10:36 ` [PATCH 1/2] userfaultfd: validate the whole range before changing write protection Umang Pokhriyal @ 2026-11-03 18:54 ` Mike Rapoport 2026-11-04 9:21 ` Umang Pokhriyal 0 siblings, 1 reply; 3+ messages in thread From: Mike Rapoport @ 2026-11-03 18:54 UTC (permalink / raw) To: Umang Pokhriyal Cc: Andrew Morton, Peter Xu, David Hildenbrand, Lorenzo Stoakes, Liam R . Howlett, Vlastimil Babka, Suren Baghdasaryan, Michal Hocko, Shuah Khan, linux-mm, linux-kselftest, linux-kernel, stable On Mon, Nov 02, 2026 at 04:06:20PM +0530, Umang Pokhriyal wrote: > 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. > > Fixes: a1b92a3f1498 ("mm/userfaultfd: support WP on multiple VMAs") > Cc: stable@vger.kernel.org > Signed-off-by: Umang Pokhriyal <umangpokhriyall@gmail.com> > --- > mm/userfaultfd.c | 20 +++++++++++++++----- > 1 file changed, 15 insertions(+), 5 deletions(-) > > diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c > index 87da92526..b8a9d9574 100644 > --- a/mm/userfaultfd.c > +++ b/mm/userfaultfd.c > @@ -1105,21 +1105,31 @@ static 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; Nit: this assignment is now redundant, isn't it? > for_each_vma_range(vmi, dst_vma, end) { > - > if (!userfaultfd_wp(dst_vma)) { > err = -ENOENT; > - break; > + goto out_unlock; > } > > if (vma_is_hugetlb(dst_vma)) { > - err = -EINVAL; Same here. > 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; > + } > } > + err = 0; > + } > + if (err) > + goto out_unlock; If we a breaking on any error to out_unlock, why do we need err = 0 and this check? > > + 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 > -- Sincerely yours, Mike. ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH 1/2] userfaultfd: validate the whole range before changing write protection 2026-11-03 18:54 ` Mike Rapoport @ 2026-11-04 9:21 ` Umang Pokhriyal 0 siblings, 0 replies; 3+ messages in thread From: Umang Pokhriyal @ 2026-11-04 9:21 UTC (permalink / raw) To: Mike Rapoport Cc: Andrew Morton, Peter Xu, David Hildenbrand, Lorenzo Stoakes, Liam R . Howlett, Vlastimil Babka, Suren Baghdasaryan, Michal Hocko, Shuah Khan, linux-mm, linux-kselftest, linux-kernel, stable On Wed, Nov 4, 2026 at 12:24 AM Mike Rapoport <rppt@kernel.org> wrote: > If we a breaking on any error to out_unlock, why do we need err = 0 and > this check? We don't, thanks. v2 drops them and the -ENOENT inside the loop. I kept the one before the loop, because for a range with no VMA both loops are skipped, and without it the call would return the -EAGAIN left over from the mmap_changing check instead of -ENOENT. I'll also drop the selftest. ^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-04 9:21 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <20261102103621.156928-1-umangpokhriyall@gmail.com>
2026-11-02 10:36 ` [PATCH 1/2] userfaultfd: validate the whole range before changing write protection Umang Pokhriyal
2026-11-03 18:54 ` Mike Rapoport
2026-11-04 9:21 ` Umang Pokhriyal
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox