[PATCH 08/21] hugetlbfs: recheck the vma range after the lock wait
From: David Hildenbrand (Arm)
Date: Wed Nov 25 2026 - 17:35:45 EST
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>
---
fs/hugetlbfs/inode.c | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c
index 5437a55d14f1c..0b96fdc3ad9d8 100644
--- a/fs/hugetlbfs/inode.c
+++ b/fs/hugetlbfs/inode.c
@@ -442,10 +442,11 @@ static void hugetlb_unmap_file_folio(struct hstate *h,
i_mmap_lock_write(mapping);
vma = vma_lock->vma;
- if (!vma) {
+ if (!vma || vma_start_pgoff(vma) >= end || vma_end_pgoff(vma) <= start) {
/*
- * If lock is no longer attached to vma, then just
- * unlock, drop our reference and retry looking for
+ * If the lock is no longer attached to this vma, or if
+ * the vma changed and no longer covers the folio, then
+ * just unlock, drop our reference and retry looking for
* other vmas.
*/
up_write(&vma_lock->rw_sema);
@@ -459,6 +460,7 @@ static void hugetlb_unmap_file_folio(struct hstate *h,
*/
v_start = vma_offset_start(vma, start);
v_end = vma_offset_end(vma, end);
+ VM_WARN_ON_ONCE(v_start >= v_end);
if (hugetlb_vma_maps_pfn(vma, v_start, pfn))
unmap_hugepage_range(vma, v_start, v_end, NULL,
ZAP_FLAG_DROP_MARKER);
--
2.56.1