linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Harry Yoo <harry.yoo@oracle.com>
To: syzbot <syzbot+5272541ccbbb14e2ec30@syzkaller.appspotmail.com>
Cc: Liam.Howlett@oracle.com, akpm@linux-foundation.org,
	david@kernel.org, jannh@google.com, linux-kernel@vger.kernel.org,
	linux-mm@kvack.org, lorenzo.stoakes@oracle.com, riel@surriel.com,
	syzkaller-bugs@googlegroups.com, vbabka@suse.cz
Subject: Re: [syzbot] [mm?] KASAN: slab-use-after-free Read in folio_remove_rmap_ptes
Date: Wed, 31 Dec 2025 17:50:06 +0900	[thread overview]
Message-ID: <aVTjvi6FCp1IzQME@hyeyoo> (raw)
In-Reply-To: <aU-IVkmsD1IN2joN@hyeyoo>

On Sat, Dec 27, 2025 at 04:18:46PM +0900, Harry Yoo wrote:
> On Fri, Dec 26, 2025 at 07:01:39PM +0900, Harry Yoo wrote:
> > On Thu, Dec 25, 2025 at 11:48:22PM -0800, syzbot wrote:
> > > Last potentially related work creation:
> > >  kasan_save_stack+0x33/0x60 mm/kasan/common.c:56
> > >  kasan_record_aux_stack+0xa7/0xc0 mm/kasan/generic.c:556
> > >  slab_free_hook mm/slub.c:2501 [inline]
> > >  slab_free mm/slub.c:6670 [inline]
> > >  kmem_cache_free+0x15e/0x770 mm/slub.c:6781
> > >  anon_vma_free mm/rmap.c:136 [inline]
> > >  __put_anon_vma+0x114/0x3a0 mm/rmap.c:2780
> > >  put_anon_vma include/linux/rmap.h:117 [inline]
> > >  unlink_anon_vmas+0x58a/0x820 mm/rmap.c:443
> > >  dontunmap_complete mm/mremap.c:1265 [inline]
> > 
> > And then (potentially) it was freed due to MREMAP_DONTUNMAP.
> > If it's correct, now we know when the refcount has been dropped to zero!
> > 
> > In dontunmap_complete():
> > > if (new_vma != vrm_vma && start == old_start && end == old_end)
> > >        unlink_anon_vmas(vrm->vma)
> > 
> > It calls unlink_anon_vmas() on the old VMA if the new range is not
> > merged into the old VMA.
> > 
> > Hmm I'm having difficult time understanding how the commit 1583aa278f5
> > ("mm: mremap: unlink anon_vmas when mremap with MREMAP_DONTUNMAP success")
> > is supposed to work when the new range is merged into an existing
> > VMA (that is not the old VMA itself).
> > 
> > The merge will succeed only if the other VMA doesn't have anon_vma
> > or it has the same anon_vma... which means we're reusing anon_vma
> > of the old vma, but we're calling put_anon_vma() on it?
> 
> Hmm, no. I tried to write a repro but it didn't work because we free
> an anon_vma only when its root rb node is empty.
> 
> Looks like I'm still missing something; How can it be empty
> when it's actually in use?

What prevents an anon_vma from being reused after the kernel releases
the rmap lock and before it calls put_anon_vma() in unlink_anon_vmas()?

Something like:

T1                                  T2

mremap(MREMAP_DONTUNMAP)
-> dontunmap_complete()
  -> unlink_anon_vmas()                 
     -> lock_anon_vma_root()
     -> remove anon_vmas from
        the chain their rbtrees
	are not empty
     -> unlock_anon_vma_root()
                                  mremap()
				    -> the new range is merged to
				       a vma with an anon_vma
				    -> reuses the anon_vma
     -> call put_anon_vma() on
        anon_vmas in the chain
	(with empty rbtree)

	Oops! it's still in use
	(due to reuse in T2)
	but the kernel called
	put_anon_vma() on it

                                  exit()
				    -> unmaps the all VMAs and sees
				       anon_vma with refcount == 0

-- 
Cheers,
Harry / Hyeonggon


      reply	other threads:[~2025-12-31  8:50 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-12-26  7:48 syzbot
2025-12-26 10:01 ` Harry Yoo
2025-12-27  7:18   ` Harry Yoo
2025-12-31  8:50     ` Harry Yoo [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=aVTjvi6FCp1IzQME@hyeyoo \
    --to=harry.yoo@oracle.com \
    --cc=Liam.Howlett@oracle.com \
    --cc=akpm@linux-foundation.org \
    --cc=david@kernel.org \
    --cc=jannh@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=lorenzo.stoakes@oracle.com \
    --cc=riel@surriel.com \
    --cc=syzbot+5272541ccbbb14e2ec30@syzkaller.appspotmail.com \
    --cc=syzkaller-bugs@googlegroups.com \
    --cc=vbabka@suse.cz \
    /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