Re: [PATCH v2 19/35] hugetlbfs: recheck the vma range after the lock wait

From: Jose A. Perez de Azpillaga

Date: Fri Dec 04 2026 - 06:13:20 EST


On 12/3/26 12:37 PM, David Hildenbrand (Arm) wrote:
> From: Chris Mason <mason@xxxxxxxxxx>
>
> A hole punch or a truncation of a hugetlb file that races with a split
> of a shared vma can lead to a use-after-free read of a page table, and
> to an MMU notifier range with end <= start, which trips a WARN_ON_ONCE()
> in KVM.
>
> hugetlb_unmap_file_folio() computes the address of the folio in a vma
> and walks the page tables to unmap the folio. As free_pgtables() unlinks
> a vma from the rmap tree before it frees its page tables, i_mmap_rwsem
> does not protect page tables outside of the vma, so walking them can
> result in a use-after-free read.
>
> The rmap tree walk guarantees that a found vma spans our folio. However,
> when we have to fall back to locking the vma lock first and drop the
> i_mmap_rwsem, we don't check whether that is still the case after we
> acquired the vma lock and the i_mmap_rwsem. The following is then
> possible:
>
> hugetlb_unmap_file_folio() __split_vma(), new_below == 0
> ========================== =============================
> hugetlb_vma_trylock_write() fails
> i_mmap_unlock_write()
> down_write(&vma_lock->rw_sema)
> vma_prepare()
> i_mmap_lock_write()
> vma->vm_end = addr
> vma_complete()
> i_mmap_unlock_write()
> i_mmap_lock_write()
> vma_lock->vma is not NULL
> vma_offset_start() >= vma->vm_end
> vma_offset_end() == vma->vm_end
> hugetlb_vma_maps_pfn(vma, v_start)
> unmap_hugepage_range(vma, v_start, v_end)
>
> Similarly, a split with new_below == 1, where the vma keeps the upper
> part and vm_start and vm_pgoff move up, or shrinking the vma with
> mremap() can leave the vma not covering our folio.
>
> Let's fix it by checking whether the possibly split vma still spans our
> folio. Add a VM_WARN_ON_ONCE() to highlight the problem that was
> previously missed.
>
> This has medium urgency, as this is triggerable by unprivileged users.
>
> Fixes: 40549ba8f8e0 ("hugetlb: use new vma_lock for pmd sharing synchronization")
> Cc: <stable@xxxxxxxxxxxxxxx>
> Signed-off-by: Chris Mason <mason@xxxxxxxxxx>
> [ david: adjust comment and description ]
> Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>

LGTM:

Reviewed-by: Jose A. Perez de Azpillaga <azpijr@xxxxxxxxx>

--
cheers,
jose a. p-a