Re: [PATCH] mm/ksm: avoid anon_vma UAF during page merging
From: David Hildenbrand (Arm)
Date: Sun Nov 01 2026 - 13:11:37 EST
On 10/31/26 19:22, Zhao Gongyi wrote:
Hi,
thanks for the report! CCing Lorenzo.
> KASAN reports a use-after-free in page_address_in_vma() while ksmd
> merges pages. ksmd keeps a folio reference across an mmap_lock drop,
> during which mremap() can remove its last mapping and free its
> anon_vma. page_address_in_vma() then dereferences the stale pointer
> from folio->mapping.
Do you have a link to the report or the actual KASAN report?
>
> KSM needs only a candidate address. write_protect_page() checks that
> the PTE maps the folio, and replace_page() verifies the saved PTE
> before replacing it. Compute the candidate from the folio index and
> VMA instead of dereferencing the folio-side anon_vma. Reject KSM
> folios because their page index is not meaningful.
>
> Fixes: 21d0d443cdc1 ("rmap: resurrect page_address_in_vma anon_vma check")
> Signed-off-by: Zhao Gongyi <zhaogongyi@xxxxxxxxxxxxx>
> ---
> Testing:
> Unpatched mm-new: fixed-pool KSM/mremap reproduced the KASAN UAF.
> Patched mm-new: the same workload ran for 12 minutes without KASAN,
> BUG or WARNING; fork/COW KSM smoke passed.
>
> mm/ksm.c | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/mm/ksm.c b/mm/ksm.c
> index d6f736a..f8af5ee 100644
> --- a/mm/ksm.c
> +++ b/mm/ksm.c
> @@ -1307,7 +1307,7 @@ static int write_protect_page(struct vm_area_struct *vma, struct folio *folio,
> if (WARN_ON_ONCE(folio_test_large(folio)))
> return err;
>
> - pvmw.address = page_address_in_vma(folio, folio_page(folio, 0), vma);
> + pvmw.address = vma_anon_address(vma, folio_pgoff(folio), 1);
> if (pvmw.address == -EFAULT)
> goto out;
>
> @@ -1408,7 +1408,7 @@ static int replace_page(struct vm_area_struct *vma, struct page *page,
> int err = -EFAULT;
> struct mmu_notifier_range range;
>
> - addr = page_address_in_vma(folio, page, vma);
> + addr = vma_anon_address(vma, page_pgoff(folio, page), 1);
> if (addr == -EFAULT)
> goto out;
>
> @@ -1515,7 +1515,7 @@ static int try_to_merge_one_page(struct vm_area_struct *vma,
> if (page == kpage) /* ksm page forked */
> return 0;
>
> - if (!folio_test_anon(folio))
> + if (!folio_test_anon(folio) || folio_test_ksm(folio))
> goto out;
>
> /*
> base-commit: 243e8cdab64129d6207c398f8cf5fbb1962ff7e1
--
Cheers,
David