[PATCH v2 3/8] exit: reset a released task's parent and leader

From: Chris Mason

Date: Sat Dec 05 2026 - 08:12:48 EST


release_task() causes a use-after-free in BPF programs if a released task
outlives its parent or group leader.

real_parent, parent and group_leader are never updated after
release_task(), but BPF programs can dereference them without a
pid_alive() check:

release_task(p)
release_task(p->group_leader) /* freed after a grace period */

BPF program with a reference on p:
p->group_leader->pid /* use-after-free */

The fix used here is to let release_task() reset the parents to init_task
and group_leader to the task itself.

A lockless walk up the parents can now end early, at a task that is being
released. A BPF LSM program that denies something to all descendants of a
process can then miss one, and Yama can wrongly refuse a ptrace attach.

Signed-off-by: Chris Mason <mason@xxxxxxxxxx>
---
include/linux/sched/task.h | 11 +++++++++++
kernel/exit.c | 1 +
2 files changed, 12 insertions(+)

diff --git a/include/linux/sched/task.h b/include/linux/sched/task.h
index 90ed5bc3c7afe..b30a66748af2c 100644
--- a/include/linux/sched/task.h
+++ b/include/linux/sched/task.h
@@ -80,6 +80,17 @@ extern void fork_init(void);

extern void release_task(struct task_struct * p);

+/*
+ * Nobody updates these pointers if @p isn't in the process tree, but BPF
+ * programs can still follow them. So don't leave any that can go stale.
+ */
+static inline void reset_parent_and_leader(struct task_struct *p)
+{
+ RCU_INIT_POINTER(p->real_parent, &init_task);
+ RCU_INIT_POINTER(p->parent, &init_task);
+ WRITE_ONCE(p->group_leader, p);
+}
+
extern int copy_thread(struct task_struct *, const struct kernel_clone_args *);

extern void flush_thread(void);
diff --git a/kernel/exit.c b/kernel/exit.c
index d5e7bc2af804a..8a5985821e17d 100644
--- a/kernel/exit.c
+++ b/kernel/exit.c
@@ -288,6 +288,7 @@ void release_task(struct task_struct *p)
leader->exit_state = EXIT_DEAD;
}

+ reset_parent_and_leader(p);
write_unlock_irq(&tasklist_lock);
proc_flush_pid(thread_pid);
put_pid(thread_pid);