All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] uprobes: clear extra_consumers before pooling return instances
@ 2026-04-08 10:02 Keenan Dong
  2026-04-08 10:38 ` sashiko-bot
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Keenan Dong @ 2026-04-08 10:02 UTC (permalink / raw)
  To: peterz, mingo, acme, namhyung, mhiramat, oleg
  Cc: mark.rutland, alexander.shishkin, jolsa, irogers, adrian.hunter,
	james.clark, andrii, linux-perf-users, linux-kernel,
	linux-trace-kernel, Keenan Dong

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.

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.

Free and clear extra_consumers before putting the instance back into
the pool so every reused entry starts from a clean state.

Fixes: 8622e45b5da1 ("uprobes: Reuse return_instances between multiple uretprobes within task")
Reported-by: Keenan Dong <keenanat2000@gmail.com>
Signed-off-by: Keenan Dong <keenanat2000@gmail.com>
---
 kernel/events/uprobes.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c
index 923b24b321cc..24b9884a2667 100644
--- 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


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] uprobes: clear extra_consumers before pooling return instances
  2026-04-08 10:02 [PATCH] uprobes: clear extra_consumers before pooling return instances Keenan Dong
@ 2026-04-08 10:38 ` sashiko-bot
  2026-11-20 13:06 ` Reza Ghanbari
  2026-11-23 14:45 ` Oleg Nesterov
  2 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-04-08 10:38 UTC (permalink / raw)
  To: Keenan Dong; +Cc: linux-perf-users

Sashiko AI review found 1 potential issue(s):
- [Critical] Missing full memory barrier in `ri_timer` allows Store-Load reordering, causing a race condition with `free_ret_instance` that can lead to Use-After-Free or uprobe corruption.
--

commit 892d89a5acfb2bb4da3888666af1d58594ed9e7d
Author: Keenan Dong <keenanat2000@gmail.com>

uprobes: clear extra_consumers before pooling return instances

This commit clears the extra_consumers array and frees its memory before
pushing a return_instance back to the per-task pool. This prevents a
potential double free if a recycled object retains a stale pointer from
its previous lifetime.

> diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c
> index 4084e926e2844..4695cb812fe26 100644
> --- 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;

While looking at how return instances are recycled into the pool here, I
noticed a potential race condition in the pre-existing code involving
ri_timer() and free_ret_instance().

In uprobes.c, ri_seqcount is used in a two-way synchronization pattern
between free_ret_instance() and ri_timer().

In free_ret_instance(), the task unlinks ri, executes hprobe_consume()
(which contains a full memory barrier via xchg()), and then reads
ri_seqcount via raw_seqcount_try_begin().

In ri_timer(), the timer asserts its presence by incrementing the seqcount
via raw_write_seqcount_begin(), and then iterates over
utask->return_instances.

For this mutual exclusion to work, both sides require a full memory barrier.
However, raw_write_seqcount_begin() only provides a write barrier
(smp_wmb()), which does not prevent Store-Load reordering.

Could this allow the timer CPU to execute the load of
utask->return_instances before the store to ri_seqcount.sequence becomes
globally visible?

If this reordering occurs, the following sequence seems possible:

1. ri_timer() reads a pointer to ri and stalls.
2. The task unlinks ri, executes its full barrier, reads the even seqcount,
   and pushes ri to ri_pool.
3. The task then quickly reuses ri for a new uretprobe, setting
   hprobe->state = HPROBE_LEASED.
4. ri_timer() resumes and executes try_cmpxchg() on hprobe->state.

Because the state is HPROBE_LEASED again, the cmpxchg succeeds. It
transitions the new uprobe's state to STABLE, dropping the SRCU lock.
When the task later consumes the new uprobe, it will call put_uprobe()
without a matching get_uprobe().

Can this sequence lead to a premature refcount drop and use-after-free
of the new uprobe?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260408100247.2065245-1-keenanat2000@gmail.com?part=1

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] uprobes: clear extra_consumers before pooling return instances
  2026-04-08 10:02 [PATCH] uprobes: clear extra_consumers before pooling return instances Keenan Dong
  2026-04-08 10:38 ` sashiko-bot
@ 2026-11-20 13:06 ` Reza Ghanbari
  2026-11-23 14:45 ` Oleg Nesterov
  2 siblings, 0 replies; 7+ messages in thread
From: Reza Ghanbari @ 2026-11-20 13:06 UTC (permalink / raw)
  To: Keenan Dong
  Cc: peterz, mingo, acme, namhyung, mhiramat, oleg, mark.rutland,
	alexander.shishkin, jolsa, irogers, adrian.hunter, james.clark,
	andrii, linux-perf-users, linux-kernel, linux-trace-kernel,
	ming.jvle

Hi Keenan, all,

I ran into this crash on my own and can confirm your patch fixes it, so
here is a Tested-by.

I was seeing it on an unpatched 6.18.63 (kernel.org source, built with a
Debian 13 config). With libfakechroot in /etc/ld.so.preload and a couple
of "setarch -R ls" loops running, I attach entry and return uprobes with
bpftrace to libfakechroot and to the same functions in libc, detaching
and reattaching every few seconds. The kernel oopses in handler_chain()
under krealloc() within a minute or so, which is the extra_consumers
path your patch is about. I haven't pinned down exactly which probes end
up sharing an address in this setup.

With only your patch on top of 6.18.63, the same probes ran 20
attach/detach cycles with no oops, and a fork-heavy variant ran 40 more,
also clean. 6.12.117 doesn't crash, which fits the Fixes: tag.

One more thing: Keke Ming's v2 for dup_return_instance() has a hunk that
clears extra_consumers when cons_cnt <= 1. With only your patch applied
the fork-heavy runs were clean, so I haven't seen a need for that hunk
for this crash.

I can send the reproducer and a full splat if that would help.

Tested-by: Reza Ghanbari <ghanbari.reza94@gmail.com>

Thanks,
Reza

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] uprobes: clear extra_consumers before pooling return instances
  2026-04-08 10:02 [PATCH] uprobes: clear extra_consumers before pooling return instances Keenan Dong
  2026-04-08 10:38 ` sashiko-bot
  2026-11-20 13:06 ` Reza Ghanbari
@ 2026-11-23 14:45 ` Oleg Nesterov
  2026-11-23 17:32   ` Oleg Nesterov
  2 siblings, 1 reply; 7+ messages in thread
From: Oleg Nesterov @ 2026-11-23 14:45 UTC (permalink / raw)
  To: Keenan Dong, Andrii Nakryiko
  Cc: peterz, mingo, acme, namhyung, mhiramat, mark.rutland,
	alexander.shishkin, jolsa, irogers, adrian.hunter, james.clark,
	linux-perf-users, linux-kernel, linux-trace-kernel

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.

Oleg.

> Free and clear extra_consumers before putting the instance back into
> the pool so every reused entry starts from a clean state.
> 
> Fixes: 8622e45b5da1 ("uprobes: Reuse return_instances between multiple uretprobes within task")
> Reported-by: Keenan Dong <keenanat2000@gmail.com>
> Signed-off-by: Keenan Dong <keenanat2000@gmail.com>
> ---
>  kernel/events/uprobes.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c
> index 923b24b321cc..24b9884a2667 100644
> --- 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
> 


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] uprobes: clear extra_consumers before pooling return instances
  2026-11-23 14:45 ` Oleg Nesterov
@ 2026-11-23 17:32   ` Oleg Nesterov
  2026-11-24 20:14     ` Andrii Nakryiko
  0 siblings, 1 reply; 7+ messages in thread
From: Oleg Nesterov @ 2026-11-23 17:32 UTC (permalink / raw)
  To: Keenan Dong, Andrii Nakryiko
  Cc: peterz, mingo, acme, namhyung, mhiramat, mark.rutland,
	alexander.shishkin, jolsa, irogers, adrian.hunter, james.clark,
	linux-perf-users, linux-kernel, linux-trace-kernel

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?

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
> > 


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] uprobes: clear extra_consumers before pooling return instances
  2026-11-23 17:32   ` Oleg Nesterov
@ 2026-11-24 20:14     ` Andrii Nakryiko
  2026-12-01  1:11       ` Masami Hiramatsu (Google)
  0 siblings, 1 reply; 7+ messages in thread
From: Andrii Nakryiko @ 2026-11-24 20:14 UTC (permalink / raw)
  To: Oleg Nesterov
  Cc: Keenan Dong, Andrii Nakryiko, peterz, mingo, acme, namhyung,
	mhiramat, mark.rutland, alexander.shishkin, jolsa, irogers,
	adrian.hunter, james.clark, linux-perf-users, linux-kernel,
	linux-trace-kernel, Keke Ming

On Mon, Nov 23, 2026 at 9:32 AM Oleg Nesterov <oleg@redhat.com> 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
> > >
>

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] uprobes: clear extra_consumers before pooling return instances
  2026-11-24 20:14     ` Andrii Nakryiko
@ 2026-12-01  1:11       ` Masami Hiramatsu (Google)
  0 siblings, 0 replies; 7+ messages in thread
From: Masami Hiramatsu (Google) @ 2026-12-01  1:11 UTC (permalink / raw)
  To: Andrii Nakryiko, Oleg Nesterov, Keenan Dong, Keke Ming
  Cc: Andrii Nakryiko, Reza Ghanbari, peterz, mingo, acme, namhyung,
	mark.rutland, alexander.shishkin, jolsa, irogers, adrian.hunter,
	james.clark, linux-perf-users, linux-kernel, linux-trace-kernel

On Tue, 24 Nov 2026 12:14:53 -0800
Andrii Nakryiko <andrii.nakryiko@gmail.com> wrote:

> On Mon, Nov 23, 2026 at 9:32 AM Oleg Nesterov <oleg@redhat.com> wrote:
> >
> > 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.

+1. UAF/double-free happens when fork() shallow-copies a reused
return_instance with cons_cnt <= 1 in this function.

Clearing ri->extra_consumers = NULL in the else branch directly fixes
that aliasing without losing buffer reuse on the hot path.

Note that I also just replied to Keke's v2 thread [1] asking to split
this dup_return_instance() fix out into a standalone patch with
Fixes: 8622e45b5da1 and Cc: stable@vger.kernel.org. When sending that
standalone patch, please also credit Keenan (Reported-by:) and Reza
(Tested-by:) from this thread.

[1] https://lore.kernel.org/all/20260822150906.528208-1-ming.jvle@gmail.com/

Thank you,

-- 
Masami Hiramatsu (Google) <mhiramat@kernel.org>


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-12-01  1:12 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-08 10:02 [PATCH] uprobes: clear extra_consumers before pooling return instances Keenan Dong
2026-04-08 10:38 ` sashiko-bot
2026-11-20 13:06 ` Reza Ghanbari
2026-11-23 14:45 ` Oleg Nesterov
2026-11-23 17:32   ` Oleg Nesterov
2026-11-24 20:14     ` Andrii Nakryiko
2026-12-01  1:11       ` Masami Hiramatsu (Google)

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.