Re: [PATCH v2 3/8] exit: reset a released task's parent and leader

From: bot+bpf-ci

Date: Sat Dec 05 2026 - 08:54:18 EST


> exit: reset a released task's parent and leader

TL;DR: Resetting group_leader to the released thread may let a BPF thread walk
reach a freed task (use-after-free) until a later patch in the series fixes
it; the commit message also omits this.

> diff --git a/include/linux/sched/task.h b/include/linux/sched/task.h
> --- a/include/linux/sched/task.h
> +++ b/include/linux/sched/task.h
> @@ -80,6 +80,17 @@ extern void fork_init(void);

[ ... ]

> +static inline void reset_parent_and_leader(struct task_struct *p)
> +{
> + RCU_INIT_POINTER(p->real_parent, &init_task);
> + RCU_INIT_POINTER(p->parent, &init_task);
> + WRITE_ONCE(p->group_leader, p);
> +}

Can resetting group_leader to the released thread itself turn a safe BPF
idiom into a use-after-free?

The verifier treats task->group_leader as RCU-trusted, so a BPF program
can start a thread walk at the leader:

leader = bpf_task_acquire(task->group_leader);
bpf_iter_task_new(&it, leader, BPF_TASK_ITER_PROC_THREADS);
while ((t = bpf_iter_task_next(&it)))
...

Before this patch, a released task T whose process was still alive had
T->group_leader pointing at a leader still linked on signal->thread_head,
so the walk only visited list members.

After this patch T->group_leader == T, so the walk starts at T.
__unhash_process() has already done list_del_rcu(&T->thread_node), which
leaves T->thread_node.next pointing at T's old successor S:

release_task(T)
list_del_rcu(&T->thread_node) /* T still points to S */
reset_parent_and_leader(T) /* T->group_leader = T */
release_task(S) /* S freed after a grace period */

BPF prog holding T (e.g. tp_btf/sched_switch with prev == T in the
do_exit() tail after exit_notify() autoreaped it, or a stashed kptr)
leader = bpf_task_acquire(T->group_leader) /* returns T */
bpf_iter_task_next() -> returns T
bpf_iter_task_next() -> __next_thread(T) returns freed S

This looks reachable for any untraced sub-thread, because exit_notify()
autoreaps those with release_task(current) while the thread is still
running the preemptible tail of do_exit().

The commit message only covers lockless parent walks ending early. Does
it need to mention thread walks started at group_leader, and that
group_leader->pid, ->comm and ->start_time, as well as the test
task->group_leader == task, now describe the thread rather than the
process?

This is addressed later in the series: 438ba8469d28 ("bpf: Don't start a
thread walk at a released task") makes bpf_iter_task_new() refuse a task
that fails pid_alive(), and d4b728aefcfd ("signal: move group_leader into
signal_struct") removes the WRITE_ONCE(p->group_leader, p) reset. Until
438ba8469d28 is applied, this commit widens the BPF use-after-free window.


---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/47525108594