From: "Lorenzo Stoakes (ARM)" <ljs@kernel.org>
To: xu.xin@linux.dev
Cc: zhaogongyi@bytedance.com, aarcange@redhat.com,
akpm@linux-foundation.org, chengming.zhou@linux.dev,
david@kernel.org, linux-kernel@vger.kernel.org,
linux-mm@kvack.org, riel@redhat.com
Subject: Re: [PATCH] mm/ksm: avoid anon_vma UAF during page merging
Date: Mon, 2 Nov 2026 12:14:11 +0000 [thread overview]
Message-ID: <auh9cfHS7DYJAzgP@gremlin> (raw)
In-Reply-To: <20261102101831.218090-1-xu.xin@linux.dev>
On Mon, Nov 02, 2026 at 06:18:31PM +0800, xu.xin@linux.dev wrote:
> > 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.
> >
> > 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@bytedance.com>
> > ---
> > 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.
>
> Thanks for the report.
>
> I’d like to ask whether this issue is easy to reproduce on 6.18 since
> I suspect the accuracy of the “fixes” tag in the commit description.
>
> Besides, adding an extra folio_test_ksm() check is unneeded, see reasons below.
>
> >
> > 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;
>
> The check folio_test_ksm(folio)) in try_to_merge_one_page is unnecessary, because before
> try_to_merge_one_page() is called, cmp_and_merge_page() invokes folio_stable_node(), which already checks
> whether this is a KSM page. Only KSM fork pages are allowed to pass through; even if they do pass, they
> will ultimately be intercepted by the if (page == kpage) check in try_to_merge_one_page.
This doesn't quite cover it, as cmp_and_merge_page() doesn't bail if
!folio_test_ksm(), it can then fall through to stable_tree_search() which can
return NULL legitimately there, so the page ends up in th eunstable tree.
Actually right now what's saving us is page_address_in_vma() implicitly
rejecting KSM folios (since folio_anon_vma() returns NULL for them).
So if you remove that, you have to re-add the check there.
This is very fiddly, and KSM is very worrying :)
(I'm sending a fix separately.)
--
Cheers, Lorenzo
prev parent reply other threads:[~2026-11-02 12:14 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-31 18:22 Zhao Gongyi
2026-11-01 18:11 ` David Hildenbrand (Arm)
2026-11-02 3:32 ` Zhao Gongyi
2026-11-02 11:23 ` Lorenzo Stoakes (ARM)
2026-11-02 10:18 ` xu.xin
2026-11-02 11:14 ` Zhao Gongyi
2026-11-02 12:14 ` Lorenzo Stoakes (ARM) [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=auh9cfHS7DYJAzgP@gremlin \
--to=ljs@kernel.org \
--cc=aarcange@redhat.com \
--cc=akpm@linux-foundation.org \
--cc=chengming.zhou@linux.dev \
--cc=david@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=riel@redhat.com \
--cc=xu.xin@linux.dev \
--cc=zhaogongyi@bytedance.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®