* [PATCH 0/3] fork, exit: fix BPF use-after-frees on task pointers
@ 2026-12-03 20:06 Chris Mason
2026-12-03 20:06 ` [PATCH 1/3] fork: make the creator the parent of an unlinked task Chris Mason
` (2 more replies)
0 siblings, 3 replies; 8+ messages in thread
From: Chris Mason @ 2026-12-03 20:06 UTC (permalink / raw)
To: brauner, oleg, ast, daniel, andrii, bpf, linux-kernel
Cc: akpm, peterz, mingo, kees, tj, eddyz87, memxor, yonghong.song,
martin.lau, song, jolsa, emil, ihor.solodrai, sched-ext, mason
Hi everyone,
These are AI generated fixes for a few ways that BPF programs can follow
task_struct pointers into freed memory.
Patch 1 fixes the parent pointers of new tasks and idle tasks, patch 2
fixes the parent and leader pointers of released tasks, and patch 3 stops
the BPF thread iterator from starting at a released task.
Patch 2 makes a released thread hold a reference on its group leader,
which I don't love. If there are better ideas, I'm happy to get
the bots cranking on them.
Chris Mason (3):
fork: make the creator the parent of an unlinked task
exit: don't leave stale parent and leader pointers
bpf: Don't start a thread walk at a released task
kernel/bpf/task_iter.c | 3 +++
kernel/exit.c | 22 +++++++++++++++++++++-
kernel/fork.c | 40 ++++++++++++++++++++++++++--------------
3 files changed, 50 insertions(+), 15 deletions(-)
base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
--
2.55.0
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH 1/3] fork: make the creator the parent of an unlinked task
2026-12-03 20:06 [PATCH 0/3] fork, exit: fix BPF use-after-frees on task pointers Chris Mason
@ 2026-12-03 20:06 ` Chris Mason
2026-12-03 20:31 ` sashiko-bot
2026-12-03 20:06 ` [PATCH 2/3] exit: don't leave stale parent and leader pointers Chris Mason
2026-12-03 20:06 ` [PATCH 3/3] bpf: Don't start a thread walk at a released task Chris Mason
2 siblings, 1 reply; 8+ messages in thread
From: Chris Mason @ 2026-12-03 20:06 UTC (permalink / raw)
To: brauner, oleg, ast, daniel, andrii, bpf, linux-kernel
Cc: akpm, peterz, mingo, kees, tj, eddyz87, memxor, yonghong.song,
martin.lau, song, jolsa, emil, ihor.solodrai, sched-ext, mason
copy_process() lets BPF hit a use-after-free through the new task's
->real_parent if the forking task's parent is reaped during the fork.
The verifier lets BPF programs follow ->real_parent and ->parent under
RCU, but forget_original_parent() only updates linked tasks. The new task
isn't linked until the end of copy_process(), and the task_alloc LSM hook
and sched_ext's ops.init_task() and ops.exit_task() see it before that.
dup_task_struct() just copies both pointers, without taking a reference:
B, child of A A and its parent
============= ================
copy_process()
dup_task_struct()
p->real_parent = A
exit_notify()
forget_original_parent()
B->real_parent = reaper
release_task(A)
put_task_struct_rcu_user()
delayed_put_task_struct()
security_task_alloc(p)
bpf: p->real_parent->pid
The idle tasks have the same problem. Init forks the ones for the
secondary CPUs, so their ->real_parent is init's thread group leader.
They are never linked, so nothing updates the pointer. If another thread
of init calls execve(), de_thread() releases the old leader, but
sched_show_task() and BPF programs at sched_switch can still follow
->real_parent to it.
The fix used here is to point both pointers at the forking task until the
fork can no longer fail, and at init_task for the idle tasks.
Fixes: 9bb00b2895cb ("bpf: Add kfunc bpf_rcu_read_lock/unlock()")
Fixes: 6fcd486b3a0a ("bpf: Refactor RCU enforcement in the verifier.")
Signed-off-by: Chris Mason <mason@kernel.org>
---
kernel/fork.c | 37 +++++++++++++++++++++++--------------
1 file changed, 23 insertions(+), 14 deletions(-)
diff --git a/kernel/fork.c b/kernel/fork.c
index d55a7d21a8fb2..3734aaa717130 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -977,6 +977,12 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node)
refcount_set(&tsk->rcu_users, 0);
/* One for the rcu users */
refcount_set(&tsk->usage, 1);
+ /*
+ * Nobody updates the parents of a task that is on no list, and orig
+ * stays until copy_process() is done.
+ */
+ RCU_INIT_POINTER(tsk->real_parent, orig);
+ RCU_INIT_POINTER(tsk->parent, orig);
#ifdef CONFIG_BLK_DEV_IO_TRACE
tsk->btrace_seq = 0;
#endif
@@ -2478,20 +2484,6 @@ __latent_entropy struct task_struct *copy_process(
*/
write_lock_irq(&tasklist_lock);
- /* CLONE_PARENT re-uses the old parent */
- if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) {
- p->real_parent = current->real_parent;
- p->parent_exec_id = current->parent_exec_id;
- if (clone_flags & CLONE_THREAD)
- p->exit_signal = -1;
- else
- p->exit_signal = current->group_leader->exit_signal;
- } else {
- p->real_parent = current;
- p->parent_exec_id = current->self_exec_id;
- p->exit_signal = args->exit_signal;
- }
-
klp_copy_process(p);
sched_core_fork(p);
@@ -2526,6 +2518,20 @@ __latent_entropy struct task_struct *copy_process(
*/
refcount_set(&p->rcu_users, 2);
+ /* CLONE_PARENT re-uses the old parent */
+ if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) {
+ p->real_parent = current->real_parent;
+ p->parent_exec_id = current->parent_exec_id;
+ if (clone_flags & CLONE_THREAD)
+ p->exit_signal = -1;
+ else
+ p->exit_signal = current->group_leader->exit_signal;
+ } else {
+ p->real_parent = current;
+ p->parent_exec_id = current->self_exec_id;
+ p->exit_signal = args->exit_signal;
+ }
+
/*
* Copy seccomp details explicitly here, in case they were changed
* before holding sighand lock.
@@ -2718,6 +2724,9 @@ struct task_struct * __init fork_idle(int cpu)
task = copy_process(&init_struct_pid, 0, cpu_to_node(cpu), &args);
if (!IS_ERR(task)) {
+ /* An idle task is never linked, and current can go away. */
+ RCU_INIT_POINTER(task->real_parent, &init_task);
+ RCU_INIT_POINTER(task->parent, &init_task);
init_idle_pids(task);
init_idle(task, cpu);
}
--
2.55.0
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH 2/3] exit: don't leave stale parent and leader pointers
2026-12-03 20:06 [PATCH 0/3] fork, exit: fix BPF use-after-frees on task pointers Chris Mason
2026-12-03 20:06 ` [PATCH 1/3] fork: make the creator the parent of an unlinked task Chris Mason
@ 2026-12-03 20:06 ` Chris Mason
2026-12-03 20:31 ` sashiko-bot
2026-12-03 20:55 ` bot+bpf-ci
2026-12-03 20:06 ` [PATCH 3/3] bpf: Don't start a thread walk at a released task Chris Mason
2 siblings, 2 replies; 8+ messages in thread
From: Chris Mason @ 2026-12-03 20:06 UTC (permalink / raw)
To: brauner, oleg, ast, daniel, andrii, bpf, linux-kernel
Cc: akpm, peterz, mingo, kees, tj, eddyz87, memxor, yonghong.song,
martin.lau, song, jolsa, emil, ihor.solodrai, sched-ext, mason
release_task() leaves real_parent, parent and group_leader dangling, so a
BPF program that follows them on a released task hits a use-after-free.
These pointers hold no reference. The verifier doesn't require a
pid_alive() check. Under RCU, BTF_TYPE_SAFE_RCU marks every trusted task's
real_parent and group_leader as valid and non-NULL, and the __rcu type tag
makes parent MEM_RCU | PTR_MAYBE_NULL. A program can hold a trusted pointer
to a released task:
bpf_kptr_xchg(&v->task, T) /* or the task iterator's reference */
release_task(T) /* real_parent, group_leader stay */
release_task(parent)
release_task(leader)
<grace period> /* parent and leader are freed */
T->real_parent->pid /* use-after-free */
The fix used here is to point a released task's real_parent and parent at
init_task, and to have a released thread hold a reference on its leader.
Fixes: 9bb00b2895cb ("bpf: Add kfunc bpf_rcu_read_lock/unlock()")
Fixes: 6fcd486b3a0a ("bpf: Refactor RCU enforcement in the verifier.")
Signed-off-by: Chris Mason <mason@kernel.org>
---
kernel/exit.c | 22 +++++++++++++++++++++-
kernel/fork.c | 3 +++
2 files changed, 24 insertions(+), 1 deletion(-)
diff --git a/kernel/exit.c b/kernel/exit.c
index d5e7bc2af804a..74d7a93355cbc 100644
--- a/kernel/exit.c
+++ b/kernel/exit.c
@@ -245,6 +245,7 @@ void release_task(struct task_struct *p)
struct release_task_post post;
struct task_struct *leader;
struct pid *thread_pid;
+ bool pin_leader;
int zap_leader;
repeat:
memset(&post, 0, sizeof(post));
@@ -262,6 +263,17 @@ void release_task(struct task_struct *p)
*/
thread_pid = get_pid(task_pid(p));
+ /*
+ * A released thread pins its leader, which lockless next_thread()
+ * needs. Until @p is released its leader can neither change nor go
+ * away. notify_count < 0 means mt-exec: @p is the old leader, and
+ * must not pin the new one, or repeated execs could build a chain.
+ */
+ leader = p->group_leader;
+ pin_leader = leader != p && READ_ONCE(p->signal->notify_count) >= 0;
+ if (pin_leader)
+ get_task_struct(leader);
+
write_lock_irq(&tasklist_lock);
ptrace_release_task(p);
__exit_signal(&post, p);
@@ -272,7 +284,6 @@ void release_task(struct task_struct *p)
* group leader's parent process. (if it wants notification.)
*/
zap_leader = 0;
- leader = p->group_leader;
if (leader != p && thread_group_empty(leader)
&& leader->exit_state == EXIT_ZOMBIE) {
/* for pidfs_exit() and do_notify_parent() */
@@ -288,6 +299,15 @@ void release_task(struct task_struct *p)
leader->exit_state = EXIT_DEAD;
}
+ /*
+ * @p is off its lists, so nobody can update these pointers any more,
+ * but they have to stay valid for whoever holds a reference to @p.
+ */
+ RCU_INIT_POINTER(p->real_parent, &init_task);
+ RCU_INIT_POINTER(p->parent, &init_task);
+ if (!pin_leader)
+ p->group_leader = p;
+
write_unlock_irq(&tasklist_lock);
proc_flush_pid(thread_pid);
put_pid(thread_pid);
diff --git a/kernel/fork.c b/kernel/fork.c
index 3734aaa717130..fbad2637d89d0 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -793,6 +793,9 @@ void __put_task_struct(struct task_struct *tsk)
exit_creds(tsk);
delayacct_tsk_free(tsk);
put_signal_struct(tsk->signal);
+ /* release_task() has pinned the leader of a thread */
+ if (tsk->group_leader != tsk)
+ put_task_struct(tsk->group_leader);
sched_core_free(tsk);
free_task(tsk);
}
--
2.55.0
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH 3/3] bpf: Don't start a thread walk at a released task
2026-12-03 20:06 [PATCH 0/3] fork, exit: fix BPF use-after-frees on task pointers Chris Mason
2026-12-03 20:06 ` [PATCH 1/3] fork: make the creator the parent of an unlinked task Chris Mason
2026-12-03 20:06 ` [PATCH 2/3] exit: don't leave stale parent and leader pointers Chris Mason
@ 2026-12-03 20:06 ` Chris Mason
2026-12-03 20:42 ` bot+bpf-ci
2 siblings, 1 reply; 8+ messages in thread
From: Chris Mason @ 2026-12-03 20:06 UTC (permalink / raw)
To: brauner, oleg, ast, daniel, andrii, bpf, linux-kernel
Cc: akpm, peterz, mingo, kees, tj, eddyz87, memxor, yonghong.song,
martin.lau, song, jolsa, emil, ihor.solodrai, sched-ext, mason
bpf_iter_task_next() hits a use-after-free on a task_struct if a
BPF_TASK_ITER_PROC_THREADS walk starts at a released task.
list_del_rcu() leaves thread_node.next of a released task in place, so
it dangles once the next thread is released and freed. But
bpf_iter_task_new() only checks the start task for NULL, and a trusted
task, e.g. a kptr taken from a map, may have been released long before
the RCU section that KF_RCU_PROTECTED requires:
T and the next thread N are released, N is freed
bpf_iter_task_new(&it, T, BPF_TASK_ITER_PROC_THREADS)
bpf_iter_task_next() returns T
kit->pos = __next_thread(T) T->thread_node.next is still N
bpf_iter_task_next() returns N
kit->pos = __next_thread(N) reads N->signal
The fix used here is to reject a start task that fails pid_alive(), as
next_tid() does.
Fixes: c68a78ffe2cb ("bpf: Introduce task open coded iterator kfuncs")
Signed-off-by: Chris Mason <mason@kernel.org>
---
kernel/bpf/task_iter.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/kernel/bpf/task_iter.c b/kernel/bpf/task_iter.c
index 2ee7b3b3c10be..a5f0849500715 100644
--- a/kernel/bpf/task_iter.c
+++ b/kernel/bpf/task_iter.c
@@ -1145,6 +1145,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;
default:
return -EINVAL;
--
2.55.0
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH 1/3] fork: make the creator the parent of an unlinked task
2026-12-03 20:06 ` [PATCH 1/3] fork: make the creator the parent of an unlinked task Chris Mason
@ 2026-12-03 20:31 ` sashiko-bot
0 siblings, 0 replies; 8+ messages in thread
From: sashiko-bot @ 2026-12-03 20:31 UTC (permalink / raw)
To: Chris Mason, bpf; +Cc: sched-ext
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] fork: stale parent and group_leader pointers across
delayed_free_task()
--
commit baa0d889662d0754e858f27233d7dd23f9b6844d
Author: Chris Mason <mason@kernel.org>
fork: make the creator the parent of an unlinked task
This commit points a new task's real_parent and parent at the forking task
in dup_task_struct() until copy_process() reaches the point of no return,
and points idle tasks' parent pointers at init_task.
> diff --git a/kernel/fork.c b/kernel/fork.c
> index d55a7d21a8fb2..3734aaa717130 100644
> --- a/kernel/fork.c
> +++ b/kernel/fork.c
> @@ -977,6 +977,12 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node)
> refcount_set(&tsk->rcu_users, 0);
> /* One for the rcu users */
> refcount_set(&tsk->usage, 1);
> + /*
> + * Nobody updates the parents of a task that is on no list, and orig
> + * stays until copy_process() is done.
> + */
> + RCU_INIT_POINTER(tsk->real_parent, orig);
> + RCU_INIT_POINTER(tsk->parent, orig);
> #ifdef CONFIG_BLK_DEV_IO_TRACE
> tsk->btrace_seq = 0;
> #endif
[Severity: Medium]
Can tsk->real_parent and tsk->parent dangle past copy_process() when a fork
fails under CONFIG_MEMCG?
On bad_fork_free, delayed_free_task(p) defers free_task(p) via call_rcu()
when CONFIG_MEMCG is enabled. Once copy_process() returns an error, orig can
exit, be reaped, and be freed before __delayed_free_task() runs on another
CPU:
CPU0 CPU1
copy_process()
dup_task_struct()
p->real_parent = orig
... (fails -> bad_fork_free)
delayed_free_task(p)
call_rcu(&p->rcu, ...)
orig exits and is reaped
delayed_put_task_struct(orig)
free_task(orig)
__delayed_free_task(p)
free_task(p)
fentry/free_task:
bpf_find_vma(p->real_parent, ...)
<-- use-after-free
In an fentry/free_task BPF program, walking tsk->real_parent or tsk->parent
produces a plain PTR_TO_BTF_ID accepted by legacy helpers such as
bpf_find_vma(). bpf_find_vma() directly locks task->alloc_lock without
probe-fault protection, resulting in a slab use-after-free.
bad_fork_free already clears p->mm for this window from commit dab101c9414d
("fork: clear p->mm when copy_process() fails"), and p->group_leader has the
same lifetime issue. Resetting p->real_parent and p->parent to &init_task
and p->group_leader to p before delayed_free_task(p) would close this gap.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203200612.26376-1-mason@kernel.org?part=1
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 2/3] exit: don't leave stale parent and leader pointers
2026-12-03 20:06 ` [PATCH 2/3] exit: don't leave stale parent and leader pointers Chris Mason
@ 2026-12-03 20:31 ` sashiko-bot
2026-12-03 20:55 ` bot+bpf-ci
1 sibling, 0 replies; 8+ messages in thread
From: sashiko-bot @ 2026-12-03 20:31 UTC (permalink / raw)
To: Chris Mason, bpf; +Cc: sched-ext
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] exit: plain store to p->group_leader in release_task() races
with RCU readers
- [Medium] exit: leader pinning creates refcount cycle with BPF task
storage kptr
- [Medium] exit: release_task() bounces leader cacheline 0 via usage
refcount
- [Low] exit: unlocked notify_count read in release_task() races with
unmarked writers
--
commit 6d90216e0968878272ede88e228cf18f77903929
Author: Chris Mason <mason@kernel.org>
exit: don't leave stale parent and leader pointers
This commit resets real_parent and parent to init_task in release_task()
and pins a released sub-thread's group_leader until __put_task_struct() so
BPF programs holding a reference to a released task cannot follow dangling
pointers.
> diff --git a/kernel/exit.c b/kernel/exit.c
> index d5e7bc2af804a..74d7a93355cbc 100644
> --- a/kernel/exit.c
> +++ b/kernel/exit.c
[ ... ]
> @@ -262,6 +263,17 @@ void release_task(struct task_struct *p)
> */
> thread_pid = get_pid(task_pid(p));
>
> + /*
> + * A released thread pins its leader, which lockless next_thread()
> + * needs. Until @p is released its leader can neither change nor go
> + * away. notify_count < 0 means mt-exec: @p is the old leader, and
> + * must not pin the new one, or repeated execs could build a chain.
> + */
> + leader = p->group_leader;
> + pin_leader = leader != p && READ_ONCE(p->signal->notify_count) >= 0;
> + if (pin_leader)
> + get_task_struct(leader);
> +
[Severity: Low]
Does this unlocked READ_ONCE(p->signal->notify_count) race with unmarked
writers to sig->notify_count?
Because release_task() reads notify_count before taking tasklist_lock and
without sighand->siglock, it can run concurrently with plain stores in
de_thread() (under siglock or read_lock(&tasklist_lock)) and unmarked
decrements in __exit_signal() (under tasklist_lock and siglock). That
triggers KCSAN data-race warnings during concurrent multi-threaded execve()
and sub-thread exits.
Moving the pin_leader check into __exit_signal() under sighand->siglock, or
annotating the writers with WRITE_ONCE(), would avoid the data race.
[Severity: Medium]
Does calling get_task_struct(leader) on every non-leader thread exit
introduce unnecessary cacheline 0 false sharing on the thread group leader?
Together with put_task_struct(tsk->group_leader) in __put_task_struct(),
every exiting sub-thread now performs two atomic updates to leader->usage,
even when no BPF programs are loaded. In struct task_struct, usage sits in
cacheline 0 alongside thread_info, __state, stack, and flags, so frequent
short-lived worker exits (such as thread pools or io-wq workers) bounce the
active leader's hottest cacheline across CPUs and contend on leader->usage
during mass thread teardown.
Only check_unsafe_exec() and cgroup_migrate()/cgroup_attach_task() walk
threads with next_thread() locklessly; converting those two sites to
for_each_thread() would allow release_task() to set p->group_leader = p
unconditionally without touching leader->usage.
[ ... ]
> @@ -288,6 +299,15 @@ void release_task(struct task_struct *p)
> leader->exit_state = EXIT_DEAD;
> }
>
> + /*
> + * @p is off its lists, so nobody can update these pointers any more,
> + * but they have to stay valid for whoever holds a reference to @p.
> + */
> + RCU_INIT_POINTER(p->real_parent, &init_task);
> + RCU_INIT_POINTER(p->parent, &init_task);
> + if (!pin_leader)
> + p->group_leader = p;
> +
> write_unlock_irq(&tasklist_lock);
[Severity: Medium]
Can this unmarked store to p->group_leader race with lockless RCU readers?
BPF programs (via BTF_TYPE_SAFE_RCU), task_is_descendant(), and
next_thread() read group_leader under RCU without holding tasklist_lock.
During multi-threaded execve(), de_thread() sets leader->group_leader = tsk
and then calls release_task(leader), where pin_leader is false, so this
plain assignment mutates p->group_leader concurrently with RCU readers,
triggering KCSAN warnings and permitting store tearing. On every normal
leader exit, leader == p also leaves pin_leader false and performs a
redundant plain write even though p->group_leader is already p.
Guarding the assignment with if (!pin_leader && leader != p) and using
WRITE_ONCE(p->group_leader, p) would eliminate both the data race and the
redundant store.
> diff --git a/kernel/fork.c b/kernel/fork.c
> index 3734aaa717130..fbad2637d89d0 100644
> --- a/kernel/fork.c
> +++ b/kernel/fork.c
> @@ -793,6 +793,9 @@ void __put_task_struct(struct task_struct *tsk)
> exit_creds(tsk);
> delayacct_tsk_free(tsk);
> put_signal_struct(tsk->signal);
> + /* release_task() has pinned the leader of a thread */
> + if (tsk->group_leader != tsk)
> + put_task_struct(tsk->group_leader);
> sched_core_free(tsk);
> free_task(tsk);
> }
[Severity: Medium]
Can pinning the leader until __put_task_struct() form a reference-count
cycle with BPF_MAP_TYPE_TASK_STORAGE?
BPF task local storage allows BPF_KPTR_REF fields, so a BPF program can
store a bpf_task_acquire() reference to a sub-thread T in its thread group
leader L's task storage. That kptr keeps T->rcu_users >= 1 until
free_task(L) calls bpf_task_storage_free(L). With this change, once both T
and L exit, T pins L->usage until __put_task_struct(T) runs, while L's task
storage prevents __put_task_struct(T) from ever running, leaking both
task_structs and their shared signal_struct until the BPF map is destroyed.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203200612.26376-1-mason@kernel.org?part=2
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 3/3] bpf: Don't start a thread walk at a released task
2026-12-03 20:06 ` [PATCH 3/3] bpf: Don't start a thread walk at a released task Chris Mason
@ 2026-12-03 20:42 ` bot+bpf-ci
0 siblings, 0 replies; 8+ messages in thread
From: bot+bpf-ci @ 2026-12-03 20:42 UTC (permalink / raw)
To: mason, brauner, oleg, ast, daniel, andrii, bpf, linux-kernel
Cc: akpm, peterz, mingo, kees, tj, eddyz87, memxor, yonghong.song,
martin.lau, song, jolsa, emil, ihor.solodrai, sched-ext, mason,
ast, andrii, daniel, martin.lau, eddyz87, yonghong.song, mason,
ihor.solodrai
[-- Attachment #1: Type: text/plain, Size: 3155 bytes --]
> 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
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 2/3] exit: don't leave stale parent and leader pointers
2026-12-03 20:06 ` [PATCH 2/3] exit: don't leave stale parent and leader pointers Chris Mason
2026-12-03 20:31 ` sashiko-bot
@ 2026-12-03 20:55 ` bot+bpf-ci
1 sibling, 0 replies; 8+ messages in thread
From: bot+bpf-ci @ 2026-12-03 20:55 UTC (permalink / raw)
To: mason, brauner, oleg, ast, daniel, andrii, bpf, linux-kernel
Cc: akpm, peterz, mingo, kees, tj, eddyz87, memxor, yonghong.song,
martin.lau, song, jolsa, emil, ihor.solodrai, sched-ext, mason,
ast, andrii, daniel, martin.lau, eddyz87, yonghong.song, mason,
ihor.solodrai
[-- Attachment #1: Type: text/plain, Size: 3050 bytes --]
> exit: don't leave stale parent and leader pointers
TL;DR: Released threads now pin their group leader, which may create a
reference cycle with BPF task-storage kptrs, leaking both task_structs until
the map is destroyed. The commit message omits this lifetime change.
> diff --git a/kernel/exit.c b/kernel/exit.c
> --- a/kernel/exit.c
> +++ b/kernel/exit.c
[ ... ]
> @@ -262,6 +263,17 @@ void release_task(struct task_struct *p)
> */
> thread_pid = get_pid(task_pid(p));
>
> + /*
> + * A released thread pins its leader, which lockless next_thread()
> + * needs. Until @p is released its leader can neither change nor go
> + * away. notify_count < 0 means mt-exec: @p is the old leader, and
> + * must not pin the new one, or repeated execs could build a chain.
> + */
> + leader = p->group_leader;
> + pin_leader = leader != p && READ_ONCE(p->signal->notify_count) >= 0;
> + if (pin_leader)
> + get_task_struct(leader);
> +
Can this pin create a reference cycle between a thread and its leader?
Previously the reference to the leader L held on behalf of a released
thread T was not counted, so L's task_struct was freed once L itself was
reaped. Now T holds a usage reference on L until T's own
__put_task_struct():
kernel/fork.c:__put_task_struct()
/* release_task() has pinned the leader of a thread */
if (tsk->group_leader != tsk)
put_task_struct(tsk->group_leader);
Anything whose release of T is tied to L's final free now forms a cycle.
BPF task-local storage looks like one such case, since
bpf_task_storage_free() is only called from free_task(), and
map_check_btf() allows BPF_KPTR_REF fields in BPF_MAP_TYPE_TASK_STORAGE
values. A program that stores a referenced kptr to thread T in L's task
storage:
v = bpf_task_storage_get(&map, L, NULL, BPF_LOCAL_STORAGE_GET_F_CREATE);
old = bpf_kptr_xchg(&v->t, bpf_task_from_pid(tid_of_T));
used to be fine, because L's free released the storage and dropped T. With
this patch:
T exits -> exit_notify() -> release_task(T) -> get_task_struct(L)
L exits and is reaped -> put_task_struct_rcu_user(L) -> delayed_put_task_struct(L)
-> put_task_struct(L): usage stays >= 1 (T's pin), L is not freed
free_task(L) never runs, so L's storage and its kptr to T are never released
T's usage stays >= 1, so __put_task_struct(T) never drops the pin on L
Both task_structs then stay allocated until the map itself is destroyed,
along with what __put_task_struct() would have released: creds,
signal_struct, css_set and LSM blobs. A cleanup hook on lsm/task_free for L
also never runs. A long-running BPF LSM or sched_ext program could leak
this way once per process.
The commit message also does not mention this new lifetime dependency:
holding any thread's task_struct now keeps its leader's task_struct alive.
Is that intended?
---
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
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-12-03 20:55 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 20:06 [PATCH 0/3] fork, exit: fix BPF use-after-frees on task pointers Chris Mason
2026-12-03 20:06 ` [PATCH 1/3] fork: make the creator the parent of an unlinked task Chris Mason
2026-12-03 20:31 ` sashiko-bot
2026-12-03 20:06 ` [PATCH 2/3] exit: don't leave stale parent and leader pointers Chris Mason
2026-12-03 20:31 ` sashiko-bot
2026-12-03 20:55 ` bot+bpf-ci
2026-12-03 20:06 ` [PATCH 3/3] bpf: Don't start a thread walk at a released task Chris Mason
2026-12-03 20:42 ` bot+bpf-ci
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox