Re: [PATCH 3/3] mm/slub: copy only the requested size in kvrealloc()
From: Harry Yoo
Date: Wed Dec 02 2026 - 11:26:04 EST
On Thu, Dec 03, 2026 at 01:04:56AM +0900, Sang-Heon Jeon wrote:
> 2026년 12월 2일 (수) 오후 11:51, Harry Yoo <harry@xxxxxxxxxx>님이 작성:
> > > diff --git a/mm/slub.c b/mm/slub.c
> > > index 8e6e900fc39b..9742c2825a06 100644
> > > --- a/mm/slub.c
> > > +++ b/mm/slub.c
> > > @@ -7272,10 +7272,18 @@ void *kvrealloc_node_align_noprof(const void *p, DECL_TOKEN_PARAMS(size, token),
> > > if (!n)
> > > return NULL;
> > >
> > > - if (p) {
> > > + if (!ZERO_OR_NULL_PTR(p)) {
> > > + struct slab *slab = virt_to_slab(p);
> > > + size_t old_size;
> > > +
> > > + if (!slab)
> > > + old_size = ksize(p);
> > > + else
> > > + old_size = get_orig_size(slab->slab_cache, (void *)p);
> > > +
> > > /* We already know that `p` is not a vmalloc address. */
> > > kasan_disable_current();
> > > - memcpy(n, kasan_reset_tag(p), min(size, ksize(p)));
> > > + memcpy(n, kasan_reset_tag(p), min(size, old_size));
> >
> > Do we really need this when kvrealloc() frees a slab object and
> > allocates a vmalloc object(), why?
>
> The case that i identified is below, SLAB_STORE_USER enabled:
>
> 1. process A allocates with kzalloc(80). write something to it and free.
> 2. process B allocates with kzalloc(70). get the same object from A
> allocated. still 70-80 bytes have data from process A
> 3. process B calls kvrealloc() with __GFP_ZERO and allocates a vmalloc object.
> 4. then copy ksize(p) = 96 bytes which might include non-zero and did
> not originate from B.
Oh, I see. I overlooked that.
> So, if you feel the existing logic is not harmful to production, I'm
> okay with that too. In that case, feel free to drop this patch.
A bug is a bug, it should be fixed anyway...
1. Could you please try to extract this to a helper and deduplicate
this? __do_krealloc() has the same logic and this patch
doesn't handle kfence objects.
2. Please make sure that this issue could be reproduced before this
patch and this patch resolves the issue.
Thanks!
--
Cheers,
Harry / Hyeonggon