Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

userfaultfd: prevent unregistering VMAs through a different userfaultfd

Currently, a VMA registered with a uffd can be unregistered through a
different uffd associated with the same mm_struct.

The existing behavior is slightly broken and may incorrectly reject
unregistering some VMAs due to the following check:

if (!vma_can_userfault(cur, cur->vm_flags, wp_async))
goto out_unlock;

where wp_async is derived from ctx, not from cur. For example, a
file-backed VMA registered with wp_async enabled and UFFD_WP mode cannot
be unregistered through a uffd that does not have wp_async enabled.

Rather than fix this and maintain this odd behavior, make unregistration
stricter by requiring VMAs to be unregistered through the same uffd they
were registered with. Additionally, reorder the BUG() checks to avoid the
aforementioned wp_async issue in them. Convert the existing check to
VM_WARN_ON_ONCE() as BUG_ON() is deprecated.

This change slightly modifies the ABI. It should not be backported to
-stable. It is expected that no one depends on this behavior, and no such
cases are known.

While at it, correct the comment for the no userfaultfd case. This seems
to be a copy-paste artifact from the analogous userfaultfd_register()
check.

Link: https://lkml.kernel.org/r/20250619-uffd-fixes-v3-2-a7274d3bd5e4@columbia.edu
Fixes: 86039bd3b4e6 ("userfaultfd: add new syscall to provide memory externalization")
Signed-off-by: Tal Zussman <tz2294@columbia.edu>
Acked-by: David Hildenbrand <david@redhat.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Andrea Arcangeli <aarcange@redhat.com>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Jan Kara <jack@suse.cz>
Cc: Jason A. Donenfeld <Jason@zx2c4.com>
Cc: Peter Xu <peterx@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Tal Zussman and committed by
Andrew Morton
23ec90eb 7208cc64

+11 -6
+11 -6
fs/userfaultfd.c
··· 1468 1468 !!(cur->vm_flags & __VM_UFFD_FLAGS)); 1469 1469 1470 1470 /* 1471 + * Prevent unregistering through a different userfaultfd than 1472 + * the one used for registration. 1473 + */ 1474 + if (cur->vm_userfaultfd_ctx.ctx && 1475 + cur->vm_userfaultfd_ctx.ctx != ctx) 1476 + goto out_unlock; 1477 + 1478 + /* 1471 1479 * Check not compatible vmas, not strictly required 1472 1480 * here as not compatible vmas cannot have an 1473 1481 * userfaultfd_ctx registered on them, but this ··· 1498 1490 for_each_vma_range(vmi, vma, end) { 1499 1491 cond_resched(); 1500 1492 1501 - BUG_ON(!vma_can_userfault(vma, vma->vm_flags, wp_async)); 1502 - 1503 - /* 1504 - * Nothing to do: this vma is already registered into this 1505 - * userfaultfd and with the right tracking mode too. 1506 - */ 1493 + /* VMA not registered with userfaultfd. */ 1507 1494 if (!vma->vm_userfaultfd_ctx.ctx) 1508 1495 goto skip; 1509 1496 1497 + VM_WARN_ON_ONCE(vma->vm_userfaultfd_ctx.ctx != ctx); 1498 + VM_WARN_ON_ONCE(!vma_can_userfault(vma, vma->vm_flags, wp_async)); 1510 1499 WARN_ON(!(vma->vm_flags & VM_MAYWRITE)); 1511 1500 1512 1501 if (vma->vm_start > start)