Re: [PATCH v2 1/4] userfaultfd: write-lock the VMA before clearing its uffd state
From: Jose A. Perez de Azpillaga
Date: Fri Dec 04 2026 - 04:27:42 EST
On 12/3/26 6:15 PM, Mike Rapoport (Microsoft) wrote:
> UFFDIO_COPY and other uffd operations run under the per-VMA lock. They
> check that the VMA is registered with a userfaultfd, but not that it is
> registered with the userfaultfd that issued the operation.
>
> When a userfaultfd is closed, userfaultfd_release_all() clears the uffd
> state of its VMAs without write-locking them. Another userfaultfd of the
> same mm can then install a write-protected PTE in a part of the VMA that
> userfaultfd_clear_ptes() has already walked. The PTE keeps its uffd bit
> after the VMA is unregistered, and a later write fault makes it writable
> with the uffd bit still set.
>
> close(uffd1) UFFDIO_COPY(uffd2, MODE_WP)
>
> userfaultfd_release_all()
> mmap_write_lock()
> userfaultfd_clear_vma()
> userfaultfd_clear_ptes()
> change_protection()
> // walks past addr
> lock_vma_under_rcu()
> validate_dst_vma()
> // installs PTE with the uffd bit
> // at addr
> vma_end_read()
> userfaultfd_reset_ctx()
> vma_start_write()
> mmap_write_unlock()
>
> write fault at addr
> do_wp_page()
> // the PTE becomes writable with the uffd bit set
>
> UFFDIO_UNREGISTER is not affected since
> commit 35f7ecd55ce7 ("mm/userfaultfd: write-lock the VMA before waking
> waiters when unregistering").
>
> Write-lock the VMA in userfaultfd_clear_ptes() before walking it.
>
> Reported-by: Sashiko <sashiko-bot@xxxxxxxxxx>
> Closes: https://sashiko.dev/bug/linux-5cddfd13-146a-47fc-97f8-feca4b2339b4
> Fixes: c88033efe9a3 ("mm/userfaultfd: reset ptes when close() for wr-protected ones")
> Reviewed-by: Suren Baghdasaryan <surenb@xxxxxxxxxx>
> Signed-off-by: Mike Rapoport (Microsoft) <rppt@xxxxxxxxxx>
LGTM:
Reviewed-by: Jose A. Perez de Azpillaga <azpijr@xxxxxxxxx>
--
cheers,
jose a. p-a