* [PATCH] mm/userfaultfd: validate context for range operations
@ 2026-12-02 13:16 Shawn Shao
2026-12-03 13:26 ` sashiko-bot
2026-12-03 13:45 ` Mike Rapoport
0 siblings, 2 replies; 3+ messages in thread
From: Shawn Shao @ 2026-12-02 13:16 UTC (permalink / raw)
To: akpm, rppt, peterx; +Cc: linux-mm
Hello,
userfaultfd range operations locate destination VMAs through ctx->mm, but
they do not consistently verify that the VMA is registered through the
context issuing the operation. A fill or protection operation can therefore
cross the VMA's context boundary and can route a fault wakeup through a
different context.
Require an exact context match in the shared fill validation and in the
write-protect and read-write-protect range walks. Keep the same validation
on the hugetlb fill retry path.
HEAD commit: 693c2e04e3f1d4c59af236e1211b7b8cb165a790
git tree: c2ddff65ad678839e4004d5e4f7f5d28aadd0e80
source version: Linux 7.4.0-rc5
architecture: x86-64
compiler: gcc 11.4.0, GNU ld 2.38
The issue reproduces on Linux 7.4.0-rc4 at commit
7cd8495c0f16275c4f9abe1cef09a7b5fcd6d046, with git tree
194227446ae61b4971eea1b7bed06856fe3cc477. The affected code is present in
the HEAD revision above. The patch was built and tested on that revision.
A source reproducer and positive/control results are available for private
review.
Fixes: 86039bd3b4e6 ("userfaultfd: add new syscall to provide memory externalization")
Fixes: ffd05793963a ("userfaultfd: wp: support write protection for userfault vma range")
Fixes: 6eab8f2cc646 ("userfaultfd: add UFFDIO_REGISTER_MODE_RWP and UFFDIO_RWPROTECT plumbing")
Signed-off-by: Shawn Shao <shawnq1@163.com>
---
mm/userfaultfd.c | 29 +++++++++++++++++------------
1 file changed, 17 insertions(+), 12 deletions(-)
diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c
index 65f7399e79b65..aeaa88d559e36 100644
--- a/mm/userfaultfd.c
+++ b/mm/userfaultfd.c
@@ -88,18 +88,20 @@ static const struct vm_uffd_ops *vma_uffd_ops(struct vm_area_struct *vma)
}
static __always_inline
-bool validate_dst_vma(struct vm_area_struct *dst_vma, unsigned long dst_end)
+bool validate_dst_vma(struct userfaultfd_ctx *ctx,
+ struct vm_area_struct *dst_vma, unsigned long dst_end)
{
/* Make sure that the dst range is fully within dst_vma. */
if (dst_end > dst_vma->vm_end)
return false;
/*
- * Check the vma is registered in uffd, this is required to
- * enforce the VM_MAYWRITE check done at uffd registration
- * time.
+ * The vma must be registered with this userfaultfd. Besides
+ * enforcing the VM_MAYWRITE check done at registration time,
+ * this ensures that faults are delivered to the context that
+ * resolves them.
*/
- if (!dst_vma->vm_userfaultfd_ctx.ctx)
+ if (dst_vma->vm_userfaultfd_ctx.ctx != ctx)
return false;
return true;
@@ -163,14 +165,15 @@ static struct vm_area_struct *uffd_lock_vma(struct mm_struct *mm,
return vma;
}
-static struct vm_area_struct *uffd_mfill_lock(struct mm_struct *dst_mm,
+static struct vm_area_struct *uffd_mfill_lock(struct userfaultfd_ctx *ctx,
unsigned long dst_start,
unsigned long len)
{
struct vm_area_struct *dst_vma;
- dst_vma = uffd_lock_vma(dst_mm, dst_start);
- if (IS_ERR(dst_vma) || validate_dst_vma(dst_vma, dst_start + len))
+ dst_vma = uffd_lock_vma(ctx->mm, dst_start);
+ if (IS_ERR(dst_vma) ||
+ validate_dst_vma(ctx, dst_vma, dst_start + len))
return dst_vma;
vma_end_read(dst_vma);
@@ -204,7 +207,7 @@ static int mfill_get_vma(struct mfill_state *state)
* Make sure the vma is not shared, that the dst range is
* both valid and fully within a single existing vma.
*/
- dst_vma = uffd_mfill_lock(ctx->mm, state->dst_start, state->len);
+ dst_vma = uffd_mfill_lock(ctx, state->dst_start, state->len);
if (IS_ERR(dst_vma))
return PTR_ERR(dst_vma);
@@ -800,7 +803,7 @@ static __always_inline ssize_t mfill_atomic_hugetlb(
* retry, dst_vma will be set to NULL and we must lookup again.
*/
if (!dst_vma) {
- dst_vma = uffd_mfill_lock(dst_mm, dst_start, len);
+ dst_vma = uffd_mfill_lock(ctx, dst_start, len);
if (IS_ERR(dst_vma)) {
err = PTR_ERR(dst_vma);
goto out;
@@ -1115,7 +1118,8 @@ static int mwriteprotect_range(struct userfaultfd_ctx *ctx, unsigned long start,
*/
err = -ENOENT;
for_each_vma_range(vmi, dst_vma, end) {
- if (!userfaultfd_wp(dst_vma))
+ if (!userfaultfd_wp(dst_vma) ||
+ dst_vma->vm_userfaultfd_ctx.ctx != ctx)
goto out_unlock;
if (vma_is_hugetlb(dst_vma)) {
@@ -1177,7 +1181,8 @@ int mrwprotect_range(struct userfaultfd_ctx *ctx, unsigned long start,
* process with only a prefix of the range re-protected.
*/
for_each_vma_range(vmi, dst_vma, end) {
- if (!userfaultfd_rwp(dst_vma))
+ if (!userfaultfd_rwp(dst_vma) ||
+ dst_vma->vm_userfaultfd_ctx.ctx != ctx)
return -ENOENT;
if (vma_is_hugetlb(dst_vma)) {
--
2.34.1
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH] mm/userfaultfd: validate context for range operations
2026-12-02 13:16 [PATCH] mm/userfaultfd: validate context for range operations Shawn Shao
@ 2026-12-03 13:26 ` sashiko-bot
2026-12-03 13:45 ` Mike Rapoport
1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-12-03 13:26 UTC (permalink / raw)
To: Shawn Shao, akpm, rppt, peterx; +Cc: linux-mm
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] mm/userfaultfd: inaccurate Fixes trailer for validate_dst_vma()
fill check
--
commit 43228c9ac2ba9eef695cbebd4847d45be3edba16
Author: Shawn Shao <shawnq1@163.com>
> mm/userfaultfd: validate context for range operations
>
> Hello,
>
> userfaultfd range operations locate destination VMAs through ctx->mm, but
> they do not consistently verify that the VMA is registered through the
> context issuing the operation. A fill or protection operation can therefore
> cross the VMA's context boundary and can route a fault wakeup through a
> different context.
>
> Require an exact context match in the shared fill validation and in the
> write-protect and read-write-protect range walks. Keep the same validation
> on the hugetlb fill retry path.
>
> HEAD commit: 693c2e04e3f1d4c59af236e1211b7b8cb165a790
> git tree: c2ddff65ad678839e4004d5e4f7f5d28aadd0e80
> source version: Linux 7.4.0-rc5
> architecture: x86-64
> compiler: gcc 11.4.0, GNU ld 2.38
>
> The issue reproduces on Linux 7.4.0-rc4 at commit
> 7cd8495c0f16275c4f9abe1cef09a7b5fcd6d046, with git tree
> 194227446ae61b4971eea1b7bed06856fe3cc477. The affected code is present in
> the HEAD revision above. The patch was built and tested on that revision.
>
> A source reproducer and positive/control results are available for private
> review.
>
> Fixes: 86039bd3b4e6 ("userfaultfd: add new syscall to provide memory externalization")
> Fixes: ffd05793963a ("userfaultfd: wp: support write protection for userfault vma range")
> Fixes: 6eab8f2cc646 ("userfaultfd: add UFFDIO_REGISTER_MODE_RWP and UFFDIO_RWPROTECT plumbing")
> Signed-off-by: Shawn Shao <shawnq1@163.com>
[Severity: Low]
86039bd3b4e6 ("userfaultfd: add new syscall to provide memory
externalization") only introduced fs/userfaultfd.c without mm/userfaultfd.c
or any fill operations. The !dst_vma->vm_userfaultfd_ctx.ctx fill check in
mm/userfaultfd.c was introduced in c1a4de99fada ("userfaultfd:
mcopy_atomic|mfill_zeropage: UFFDIO_COPY|UFFDIO_ZEROPAGE preparation") and
exposed to userspace in ad465cae96b4 ("userfaultfd: UFFDIO_COPY and
UFFDIO_ZEROPAGE").
Should the first Fixes: tag reference c1a4de99fada ("userfaultfd:
mcopy_atomic|mfill_zeropage: UFFDIO_COPY|UFFDIO_ZEROPAGE preparation") (or
ad465cae96b4 ("userfaultfd: UFFDIO_COPY and UFFDIO_ZEROPAGE")) instead?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261202131600.3164107-1-shawnq1@163.com?part=1
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH] mm/userfaultfd: validate context for range operations
2026-12-02 13:16 [PATCH] mm/userfaultfd: validate context for range operations Shawn Shao
2026-12-03 13:26 ` sashiko-bot
@ 2026-12-03 13:45 ` Mike Rapoport
1 sibling, 0 replies; 3+ messages in thread
From: Mike Rapoport @ 2026-12-03 13:45 UTC (permalink / raw)
To: Shawn Shao; +Cc: Andrew Morton, Peter Xu, linux-mm
Hi Shawn,
On Wed, Dec 02, 2026 at 01:16:00PM +0000, Shawn Shao wrote:
> Hello,
>
> userfaultfd range operations locate destination VMAs through ctx->mm, but
> they do not consistently verify that the VMA is registered through the
> context issuing the operation. A fill or protection operation can therefore
> cross the VMA's context boundary and can route a fault wakeup through a
> different context.
What is the actual problem? Operations on a range registered with another
userfaultfd of the same mm were allowed from the beginning, the check that
the range is registered was there to catch userspace errors (see the
comment c1a4de99fada added to __mcopy_atomic()).
The only real issue with that I'm aware of is a race between close() of a
userfaultfd and UFFDIO_COPY on another userfaultfd, and it's fixed by
write-locking the VMA before clearing the uffd state:
https://lore.kernel.org/all/20261202-fixes-uffd-sashiko-v1-0-a6f5a874334a@kernel.org
If you see something else that breaks, please describe in the changelog
what happens, how it can be triggered and what is the effect.
And making such operations fail with -ENOENT is a user visible change,
the changelog should say so and explain why it's safe.
> Require an exact context match in the shared fill validation and in the
> write-protect and read-write-protect range walks. Keep the same validation
> on the hugetlb fill retry path.
>
> HEAD commit: 693c2e04e3f1d4c59af236e1211b7b8cb165a790
> git tree: c2ddff65ad678839e4004d5e4f7f5d28aadd0e80
> source version: Linux 7.4.0-rc5
> architecture: x86-64
> compiler: gcc 11.4.0, GNU ld 2.38
>
> The issue reproduces on Linux 7.4.0-rc4 at commit
> 7cd8495c0f16275c4f9abe1cef09a7b5fcd6d046, with git tree
> 194227446ae61b4971eea1b7bed06856fe3cc477. The affected code is present in
> the HEAD revision above. The patch was built and tested on that revision.
>
> A source reproducer and positive/control results are available for private
> review.
None of this belongs in a changelog. If the reproducer triggers a bug,
describe what it does and what it shows.
> Fixes: 86039bd3b4e6 ("userfaultfd: add new syscall to provide memory externalization")
> Fixes: ffd05793963a ("userfaultfd: wp: support write protection for userfault vma range")
> Fixes: 6eab8f2cc646 ("userfaultfd: add UFFDIO_REGISTER_MODE_RWP and UFFDIO_RWPROTECT plumbing")
I doubt these are accurate.
--
Sincerely yours,
Mike.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-12-03 13:45 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 13:16 [PATCH] mm/userfaultfd: validate context for range operations Shawn Shao
2026-12-03 13:26 ` sashiko-bot
2026-12-03 13:45 ` Mike Rapoport
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox