The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [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; 5+ 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] 5+ 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: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, 0 replies; 5+ 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] 5+ 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:06 ` [PATCH 3/3] bpf: Don't start a thread walk at a released task Chris Mason
  2 siblings, 0 replies; 5+ 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] 5+ 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; 5+ 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] 5+ 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; 5+ 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] 5+ messages in thread

end of thread, other threads:[~2026-12-03 20:42 UTC | newest]

Thread overview: 5+ 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: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
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