Re: [PATCH] uprobes: clear extra_consumers before pooling return instances

From: Andrii Nakryiko

Date: Tue Nov 24 2026 - 15:15:16 EST


On Mon, Nov 23, 2026 at 9:32 AM Oleg Nesterov <oleg@xxxxxxxxxx> wrote:
>
> On 11/23, Oleg Nesterov wrote:
> >
> > Andrii, can you take a look? I never understood this code in details,
> > and today I forgot everything...
> >
> > On 04/08, Keenan Dong wrote:
> > >
> > > ri_pool_push() returns a return_instance to the per-task pool for
> > > later reuse. The pool reset clears cons_cnt, but it leaves
> > > extra_consumers behind.
> >
> > So at first glance this doesn't look right no matter what...
> >
> > > A reused return_instance can later grow a fresh extra_consumers
> > > array and then reach the cleanup path with a stale pointer from its
> > > previous lifetime, leading to a double free of the recycled object.
> >
> > Can you add more details to explain the possible double free?
> > This is not immediately obvious, at least not to me.
>
> May be this has something to do with dup_utask() -> dup_return_instance() ?
>
> If cons_cnt = 0 but ri->extra_consumers != NULL, then
> dup_return_instance() -> kmemdup() will copy old->extra_consumers, but
> but kmemdup(old->extra_consumers) won't be called?
>

I think all we need this just this:

diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c
index c47b1ee266d5..f265527e6897 100644
--- a/kernel/events/uprobes.c
+++ b/kernel/events/uprobes.c
@@ -2118,6 +2118,9 @@ static struct return_instance
*dup_return_instance(struct return_instance *old)
kfree(ri);
return NULL;
}
+ } else {
+ /* old->extra_consumers can be a leftover buffer from
ri reuse */
+ ri->extra_consumers = NULL;
}

return ri;

cc'ing Keke Ming who was going to send this as a stand-alone fix
earlier this summer.


> Damn this code is too complex for me ;) Anyway, I think the changelog
> should say more.
>
> Oleg.
>
> > > --- a/kernel/events/uprobes.c
> > > +++ b/kernel/events/uprobes.c
> > > @@ -1945,6 +1945,8 @@ unsigned long uprobe_get_trap_addr(struct pt_regs *regs)
> > >
> > > static void ri_pool_push(struct uprobe_task *utask, struct return_instance *ri)
> > > {
> > > + kfree(ri->extra_consumers);
> > > + ri->extra_consumers = NULL;
> > > ri->cons_cnt = 0;
> > > ri->next = utask->ri_pool;
> > > utask->ri_pool = ri;
> > > --
> > > 2.43.0
> > >
>