uprobes: clear extra_consumers before pooling return instances
Subsystems (3)
PATCH 1/1 uprobes: clear extra_consumers before pooling return instances
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.
--- kernel/events/uprobes.c | 2 ++ 1 file changed, 2 insertions(+)
kernel/events/uprobes.c +2 -0
@@ -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;
Signature · 1 line
2.43.0
[email protected]
AI reviewSashiko 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…
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 <[email protected]>
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.
Quoted 4 lines
kernel/events/uprobes.c +0
-0
[ ... ]
kernel/events/uprobes.c unsigned long uprobe_get_trap_addr(struct pt_regs *regs) · line 1952
Quoted 9 lines
kernel/events/uprobes.c +2
-0@@ -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?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
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.
Thanks,
Reza