* [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
` (3 more replies)
0 siblings, 4 replies; 11+ 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] 11+ 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-04 15:27 ` Oleg Nesterov
2026-12-03 20:06 ` [PATCH 2/3] exit: don't leave stale parent and leader pointers Chris Mason
` (2 subsequent siblings)
3 siblings, 1 reply; 11+ 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] 11+ 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: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-04 2:36 ` [PATCH 0/3] fork, exit: fix BPF use-after-frees on task pointers Alexei Starovoitov
3 siblings, 1 reply; 11+ 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] 11+ 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
2026-12-04 2:36 ` [PATCH 0/3] fork, exit: fix BPF use-after-frees on task pointers Alexei Starovoitov
3 siblings, 1 reply; 11+ 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] 11+ 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; 11+ 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] 11+ 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:55 ` bot+bpf-ci
0 siblings, 0 replies; 11+ 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] 11+ messages in thread
* Re: [PATCH 0/3] fork, exit: fix BPF use-after-frees on task pointers
2026-12-03 20:06 [PATCH 0/3] fork, exit: fix BPF use-after-frees on task pointers Chris Mason
` (2 preceding siblings ...)
2026-12-03 20:06 ` [PATCH 3/3] bpf: Don't start a thread walk at a released task Chris Mason
@ 2026-12-04 2:36 ` Alexei Starovoitov
2026-12-04 12:53 ` Oleg Nesterov
2026-12-04 15:36 ` Chris Mason
3 siblings, 2 replies; 11+ messages in thread
From: Alexei Starovoitov @ 2026-12-04 2:36 UTC (permalink / raw)
To: Chris Mason, brauner, oleg, daniel, andrii, bpf, linux-kernel
Cc: akpm, peterz, mingo, kees, tj, eddyz87, memxor, yonghong.song,
martin.lau, song, jolsa, emil, ihor.solodrai, sched-ext
On Thu, Dec 03, 2026 at 12:06 PM Chris Mason <mason@kernel.org> wrote:
> These are AI generated fixes for a few ways that BPF programs can follow
> task_struct pointers into freed memory.
The bugs look real. fork and exit are the right place to fix them imo.
The verifier can't tell a released task from a live one.
BUT the title is misleading.
The kernel has the same bug without bpf. fill_stats_for_pid() holds
a reference on tsk and nothing else. Then bacct_add_tsk() reads
tsk->group_leader->start_time.
> 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.
I don't see a different option.
Oleg,
pls review.
Christian,
if it looks good to folks, could you take patches 1-2 via your tree?
I'll take patch 3.
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH 0/3] fork, exit: fix BPF use-after-frees on task pointers
2026-12-04 2:36 ` [PATCH 0/3] fork, exit: fix BPF use-after-frees on task pointers Alexei Starovoitov
@ 2026-12-04 12:53 ` Oleg Nesterov
2026-12-04 15:36 ` Chris Mason
1 sibling, 0 replies; 11+ messages in thread
From: Oleg Nesterov @ 2026-12-04 12:53 UTC (permalink / raw)
To: Alexei Starovoitov
Cc: Chris Mason, brauner, daniel, andrii, bpf, linux-kernel, akpm,
peterz, mingo, kees, tj, eddyz87, memxor, yonghong.song,
martin.lau, song, jolsa, emil, ihor.solodrai, sched-ext
On 12/04, Alexei Starovoitov wrote:
>
> BUT the title is misleading.
> The kernel has the same bug without bpf. fill_stats_for_pid() holds
> a reference on tsk and nothing else. Then bacct_add_tsk() reads
> tsk->group_leader->start_time.
Oh yes, and IIRC there are more buggy users of ->group_leader
> > 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.
I think we should move ->group_leader from task_struct to ->signal_struct.
I was going to do this a long ago, but was distructed. See the preparations
I sent https://lore.kernel.org/all/aXY_h8i78n6yD9JY@redhat.com/
I'll try to take a look this weekend, I need to recall the implications.
But ->real_parent has to stay per task, and I don't really understand 1/3,
even the changelog.
Oleg.
^ permalink raw reply [flat|nested] 11+ 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-04 15:27 ` Oleg Nesterov
2026-12-04 16:27 ` Chris Mason
0 siblings, 1 reply; 11+ messages in thread
From: Oleg Nesterov @ 2026-12-04 15:27 UTC (permalink / raw)
To: Chris Mason
Cc: brauner, ast, daniel, andrii, bpf, linux-kernel, akpm, peterz,
mingo, kees, tj, eddyz87, memxor, yonghong.song, martin.lau, song,
jolsa, emil, ihor.solodrai, sched-ext
On 12/03, Chris Mason wrote:
>
> 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
I know nothing about bpf, could you add more details please ?
OK, B, child of A, does copy_process().
A exits right after dup_task_struct(). p->real_parent points to nowhere.
Who exactly can dererence p->real_parent at this point?
With the change in dup_task_struct(), why is it correct to pretend that
p->real_parent == B ?
> 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.
Yes... perhaps fork_idle() should use CLONE_PARENT... but your change
looks fine.
> The fix used here is to point both pointers at the forking task until the
> fork can no longer fail,
OK, this change suggests that we have another problem: copy_process() with
CLONE_PARENT or CLONE_THREAD can fail and drop tasklist_lock after it sets
p->real_parent = A. A can exit.
Who can derefence ->real_parent of the soon-to-be-freed and not-yet-linked
task p?
I am just trying to understand the problem. Perhaps we should change
dup_task_struct() to nullify p->thread_pid to make pid_alive(p) false?
Oleg.
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH 0/3] fork, exit: fix BPF use-after-frees on task pointers
2026-12-04 2:36 ` [PATCH 0/3] fork, exit: fix BPF use-after-frees on task pointers Alexei Starovoitov
2026-12-04 12:53 ` Oleg Nesterov
@ 2026-12-04 15:36 ` Chris Mason
1 sibling, 0 replies; 11+ messages in thread
From: Chris Mason @ 2026-12-04 15:36 UTC (permalink / raw)
To: Alexei Starovoitov
Cc: brauner, oleg, daniel, andrii, bpf, linux-kernel, akpm, peterz,
mingo, kees, tj, eddyz87, memxor, yonghong.song, martin.lau, song,
jolsa, emil, ihor.solodrai, sched-ext
On Fri, 04 Dec 2026 02:36:25 +0000, Alexei Starovoitov wrote:
> On Thu, Dec 03, 2026 at 12:06 PM Chris Mason <mason@kernel.org> wrote:
>> These are AI generated fixes for a few ways that BPF programs can follow
>> task_struct pointers into freed memory.
>
> The bugs look real. fork and exit are the right place to fix them imo.
> The verifier can't tell a released task from a live one.
>
> BUT the title is misleading.
> The kernel has the same bug without bpf. fill_stats_for_pid() holds
> a reference on tsk and nothing else. Then bacct_add_tsk() reads
> tsk->group_leader->start_time.
>
>> 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.
>
> I don't see a different option.
>
> Oleg,
> pls review.
>
> Christian,
> if it looks good to folks, could you take patches 1-2 via your tree?
> I'll take patch 3.
Please hold off on patches 1-2, it looks like patch 2 leaks. A BPF program can
keep a thread in its leader's task storage, and then neither of them is freed.
The bots are working on v2, and I'll have them try Oleg's suggestion as well.
You're right about bacct_add_tsk(), claude reproduced that one with KASAN.
-chris
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH 1/3] fork: make the creator the parent of an unlinked task
2026-12-04 15:27 ` Oleg Nesterov
@ 2026-12-04 16:27 ` Chris Mason
0 siblings, 0 replies; 11+ messages in thread
From: Chris Mason @ 2026-12-04 16:27 UTC (permalink / raw)
To: Oleg Nesterov
Cc: brauner, ast, daniel, andrii, bpf, linux-kernel, akpm, peterz,
mingo, kees, tj, eddyz87, memxor, yonghong.song, martin.lau, song,
jolsa, emil, ihor.solodrai, sched-ext
On Fri, 4 Dec 2026 16:27:58 +0100, Oleg Nesterov wrote:
> I know nothing about bpf, could you add more details please ?
>
> OK, B, child of A, does copy_process().
>
> A exits right after dup_task_struct(). p->real_parent points to nowhere.
>
> Who exactly can dererence p->real_parent at this point?
BPF programs...security_task_alloc() and sched_cgroup_fork() call them with
p before it's linked, and they're allowed to read p->real_parent->pid, for
example. The scx_lavd scheduler does this now.
> With the change in dup_task_struct(), why is it correct to pretend that
> p->real_parent == B ?
It's only correct for a plain fork, where B will be the parent anyway? For a
new thread, the parent will be A, and copy_process() sets that at the end.
CLONE_PARENT is the same.
Until then, bpf programs need p->real_parent pointing to a task that can't
be freed. A can exit at any time, but B can't, because it's busy with the fork.
The downside is a window where a bpf program sees B as the parent of a
new thread.
(I don't love any of these fixes, just explaining the crash itself)
[ ... ]
> OK, this change suggests that we have another problem: copy_process() with
> CLONE_PARENT or CLONE_THREAD can fail and drop tasklist_lock after it sets
> p->real_parent = A. A can exit.
>
> Who can derefence ->real_parent of the soon-to-be-freed and not-yet-linked
> task p?
sched_cancel_fork() calls sched_ext's ops.exit_task() after tasklist_lock is
dropped. This patch missed that one, thanks for flagging.
> I am just trying to understand the problem. Perhaps we should change
> dup_task_struct() to nullify p->thread_pid to make pid_alive(p) false?
It looks like that isn't enough on its own. BPF programs follow ->real_parent
without checking pid_alive().
-chris
^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2026-12-04 16:28 UTC | newest]
Thread overview: 11+ 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-04 15:27 ` Oleg Nesterov
2026-12-04 16:27 ` 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: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
2026-12-04 2:36 ` [PATCH 0/3] fork, exit: fix BPF use-after-frees on task pointers Alexei Starovoitov
2026-12-04 12:53 ` Oleg Nesterov
2026-12-04 15:36 ` Chris Mason
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox