[PATCH 1/3] fork: make the creator the parent of an unlinked task

From: Chris Mason

Date: Thu Dec 03 2026 - 15:06:25 EST


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@xxxxxxxxxx>
---
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