[PATCH v2 4/8] fork: don't leave stale parent pointers in a new task
From: Chris Mason
Date: Sat Dec 05 2026 - 08:13:02 EST
copy_process() causes a use-after-free in BPF programs if the forking
task's parent is freed during the fork.
forget_original_parent() doesn't find a new task until copy_process() has
added it to the process tree. But BPF programs can dereference its
real_parent before that, in the task_alloc LSM hook and in sched_ext's
ops.init_task():
copy_process() parent exits
============== ============
p = dup_task_struct(current, node)
/* copies real_parent */
forget_original_parent()
/* doesn't find p */
release_task(parent)
/* freed after a grace period */
security_task_alloc(p)
BPF program:
p->real_parent->pid
/* use-after-free */
If the fork fails, the same can happen in ops.exit_task(), and later in a
BPF program attached to free_task(), when the forking task can be gone too.
pid_alive(p) catches none of this, as thread_pid is copied too.
The idle threads from fork_idle() never get into the process tree, but BPF
programs see them in sched_switch. Their real_parent is init's first
thread, which is freed if another thread of init calls execve().
The fix used here is to use the forking task as the parent during the
fork, and init_task if the new task never gets into the process tree.
Signed-off-by: Chris Mason <mason@xxxxxxxxxx>
---
include/linux/pid.h | 1 +
kernel/fork.c | 17 +++++++++++++++--
2 files changed, 16 insertions(+), 2 deletions(-)
diff --git a/include/linux/pid.h b/include/linux/pid.h
index 185bf0cde2d14..26eee52fb1e30 100644
--- a/include/linux/pid.h
+++ b/include/linux/pid.h
@@ -319,6 +319,7 @@ static inline pid_t task_tgid_nr(struct task_struct *tsk)
* @p: Task structure to be checked.
*
* Test if a process is not yet dead (at most zombie state)
+ * and no longer being forked.
* If pid_alive fails, then pointers within the task structure
* can be stale and must not be dereferenced.
*
diff --git a/kernel/fork.c b/kernel/fork.c
index d55a7d21a8fb2..c99d7147f1f1d 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -928,6 +928,14 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node)
if (err)
goto free_tsk;
+ /*
+ * pid_alive() has to fail until copy_process() links the task. @orig is
+ * a safe parent until then, as it can't exit during its own fork.
+ */
+ tsk->thread_pid = NULL;
+ RCU_INIT_POINTER(tsk->real_parent, orig);
+ RCU_INIT_POINTER(tsk->parent, orig);
+
err = alloc_thread_stack_node(tsk, node);
if (err)
goto free_tsk;
@@ -2480,14 +2488,12 @@ __latent_entropy struct task_struct *copy_process(
/* 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;
}
@@ -2520,6 +2526,10 @@ __latent_entropy struct task_struct *copy_process(
/* No more failure paths after this point. */
+ /* Not earlier, as nobody updates it if the fork fails. */
+ if (clone_flags & (CLONE_PARENT | CLONE_THREAD))
+ p->real_parent = current->real_parent;
+
/*
* One for the user space visible state that goes away when reaped.
* One for the scheduler.
@@ -2678,6 +2688,8 @@ __latent_entropy struct task_struct *copy_process(
* can access it concurrently anymore, but prevent later access.
*/
p->mm = NULL;
+ /* current can exit before p is freed. */
+ reset_parent_and_leader(p);
WRITE_ONCE(p->__state, TASK_DEAD);
exit_task_stack_account(p);
put_task_stack(p);
@@ -2718,6 +2730,7 @@ struct task_struct * __init fork_idle(int cpu)
task = copy_process(&init_struct_pid, 0, cpu_to_node(cpu), &args);
if (!IS_ERR(task)) {
+ reset_parent_and_leader(task);
init_idle_pids(task);
init_idle(task, cpu);
}