Re: [PATCH 3/3] bpf: Don't start a thread walk at a released task
From: bot+bpf-ci
Date: Thu Dec 03 2026 - 15:42:46 EST
> bpf: Don't start a thread walk at a released task
TL;DR: bpf_iter_task_new()'s pid_alive() check may miss a task still inside
copy_process(). A BPF program could then walk a stale thread_node into a freed
sibling thread, a possible use-after-free.
> diff --git a/kernel/bpf/task_iter.c b/kernel/bpf/task_iter.c
> --- a/kernel/bpf/task_iter.c
> +++ b/kernel/bpf/task_iter.c
> @@ -1161,6 +1161,9 @@ __bpf_kfunc int bpf_iter_task_new(struct bpf_iter_task *it,
> case BPF_TASK_ITER_PROC_THREADS:
> if (!task__nullable)
> return -EINVAL;
> + /* ->thread_node of a released task is stale, see next_tid() */
> + if (!pid_alive(task__nullable))
> + return -ESRCH;
> break;
This isn't a bug in the patch itself, but does the pid_alive() check leave
a similar case uncovered for a task that is still inside copy_process()?
dup_task_struct() copies the parent with arch_dup_task_struct(), and
nothing in copy_process() touches ->thread_pid until:
init_task_pid(p, PIDTYPE_PID, pid)
so pid_alive(p) is true for the whole fork. In that window
p->thread_node is a byte copy of current->thread_node, and its ->next
points at &X->thread_node for a sibling thread X. That pointer holds no
reference and no RCU section covers it, so X can exit, go through
release_task() and delayed_put_task_struct(), and be freed while
copy_process() sleeps in copy_creds(), alloc_pid(), cgroup_can_fork() and
so on.
BPF programs get a trusted pointer to p in this window, from the LSM
task_alloc hook (security_task_alloc(), bpf_lsm_task_alloc is not in
untrusted_lsm_hooks), from sched_ext ops.init_task() via
sched_cgroup_fork()->scx_fork(), and from ops.exit_task() via
scx_cancel_fork(). bpf_iter_task_new() is in common_kfunc_set, so these
programs can call it:
copy_process() sibling X of current
dup_task_struct()
p->thread_node.next = &X->thread_node
do_exit() -> release_task(X)
<grace period>, X freed
security_task_alloc(p) / scx_fork(p)
bpf_iter_task_new(&it, p, BPF_TASK_ITER_PROC_THREADS)
pid_alive(p) == 1 thread_pid copied from current
bpf_iter_task_next() returns p
kit->pos = __next_thread(p) reads stale p->thread_node.next == X
bpf_iter_task_next() returns freed X to the program
__next_thread(X) reads X->signal
next_tid() in fs/proc/base.c can rely on pid_alive() because procfs only
reaches hashed tasks, but that doesn't hold for the start tasks BPF can
pass here.
Is the PROC_THREADS walk intended to be safe for a task that is still being
forked? If so, could the start-task check also reject unlinked tasks, for
example a task whose ->thread_pid is not yet attached, or one with
rcu_users == 0 which bpf_task_acquire() already treats as unusable?
---
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/47195022171