From: Chris Mason <mason@kernel.org>
To: brauner@kernel.org, oleg@redhat.com, ast@kernel.org,
daniel@iogearbox.net, andrii@kernel.org, bpf@vger.kernel.org,
linux-kernel@vger.kernel.org
Cc: akpm@linux-foundation.org, peterz@infradead.org,
mingo@redhat.com, kees@kernel.org, tj@kernel.org,
eddyz87@gmail.com, memxor@gmail.com, yonghong.song@linux.dev,
martin.lau@linux.dev, song@kernel.org, jolsa@kernel.org,
emil@etsalapatis.com, ihor.solodrai@linux.dev,
sched-ext@lists.linux.dev, mason@kernel.org
Subject: [PATCH 1/3] fork: make the creator the parent of an unlinked task
Date: Thu, 3 Dec 2026 15:06:10 -0500 [thread overview]
Message-ID: <20261203200612.26376-2-mason@kernel.org> (raw)
In-Reply-To: <20261203200612.26376-1-mason@kernel.org>
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
next prev parent reply other threads:[~2026-12-03 20:06 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261203200612.26376-2-mason@kernel.org \
--to=mason@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=brauner@kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=emil@etsalapatis.com \
--cc=ihor.solodrai@linux.dev \
--cc=jolsa@kernel.org \
--cc=kees@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=mingo@redhat.com \
--cc=oleg@redhat.com \
--cc=peterz@infradead.org \
--cc=sched-ext@lists.linux.dev \
--cc=song@kernel.org \
--cc=tj@kernel.org \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox