UFFDIO_REGISTER and UFFDIO_UNREGISTER both scan the VMAs of the range before changing anything. For a hugetlb VMA, userfaultfd_register() rejects a range whose start or end is not huge page aligned there. However, userfaultfd_unregister() only checks the start. An unaligned end only fails later, when vma_modify_flags_uffd() cannot split the VMA there. At that point, userfaultfd_clear_vma() already cleared the WP or RWP protection. The ioctl returns -EINVAL and the VMA stays registered, but accesses to its pages are no longer reported. This requires a caller passing a misaligned range. To fix it, check the end of a registered VMA in the pre-scan as well. The ioctl still returns -EINVAL, but no longer unregisters the VMAs before the misaligned one first. Fixes: cab350afcbc9 ("userfaultfd: hugetlbfs: allow registration of ranges containing huge pages") Fixes: f369b07c8614 ("mm/uffd: reset write protection when unregister with wp-mode") Signed-off-by: Tal Zussman --- mm/userfaultfd.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index bf42fabb5afd..d8abc24353e1 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -3960,6 +3960,8 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx, found = false; cur = vma; do { + const struct userfaultfd_ctx *vma_ctx = cur->vm_userfaultfd_ctx.ctx; + cond_resched(); VM_WARN_ON_ONCE(!!cur->vm_userfaultfd_ctx.ctx ^ @@ -3983,6 +3985,15 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx, if (!vma_can_userfault(cur, cur->vm_flags, wp_async)) goto out_unlock; + /* See the check in userfaultfd_register(). */ + if (vma_ctx && vma_is_hugetlb(cur) && + end <= cur->vm_end && end > cur->vm_start) { + const unsigned long vma_hpagesize = vma_kernel_pagesize(cur); + + if (!IS_ALIGNED(end, vma_hpagesize)) + goto out_unlock; + } + found = true; } for_each_vma_range(vmi, cur, end); VM_WARN_ON_ONCE(!found); -- 2.39.5