Skip to content

Commit 23ec90e

Browse files
tzussmanakpm00
authored andcommitted
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/[email protected] Fixes: 86039bd ("userfaultfd: add new syscall to provide memory externalization") Signed-off-by: Tal Zussman <[email protected]> Acked-by: David Hildenbrand <[email protected]> Cc: Al Viro <[email protected]> Cc: Andrea Arcangeli <[email protected]> Cc: Christian Brauner <[email protected]> Cc: Jan Kara <[email protected]> Cc: Jason A. Donenfeld <[email protected]> Cc: Peter Xu <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 7208cc6 commit 23ec90e

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

fs/userfaultfd.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,6 +1467,14 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
14671467
BUG_ON(!!cur->vm_userfaultfd_ctx.ctx ^
14681468
!!(cur->vm_flags & __VM_UFFD_FLAGS));
14691469

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+
14701478
/*
14711479
* Check not compatible vmas, not strictly required
14721480
* here as not compatible vmas cannot have an
@@ -1490,15 +1498,12 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
14901498
for_each_vma_range(vmi, vma, end) {
14911499
cond_resched();
14921500

1493-
BUG_ON(!vma_can_userfault(vma, vma->vm_flags, wp_async));
1494-
1495-
/*
1496-
* Nothing to do: this vma is already registered into this
1497-
* userfaultfd and with the right tracking mode too.
1498-
*/
1501+
/* VMA not registered with userfaultfd. */
14991502
if (!vma->vm_userfaultfd_ctx.ctx)
15001503
goto skip;
15011504

1505+
VM_WARN_ON_ONCE(vma->vm_userfaultfd_ctx.ctx != ctx);
1506+
VM_WARN_ON_ONCE(!vma_can_userfault(vma, vma->vm_flags, wp_async));
15021507
WARN_ON(!(vma->vm_flags & VM_MAYWRITE));
15031508

15041509
if (vma->vm_start > start)

0 commit comments

Comments
 (0)