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 2/3] exit: don't leave stale parent and leader pointers
Date: Thu, 3 Dec 2026 15:06:11 -0500 [thread overview]
Message-ID: <20261203200612.26376-3-mason@kernel.org> (raw)
In-Reply-To: <20261203200612.26376-1-mason@kernel.org>
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
next prev parent reply other threads:[~2026-12-03 20:06 UTC|newest]
Thread overview: 5+ 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 ` [PATCH 1/3] fork: make the creator the parent of an unlinked task Chris Mason
2026-12-03 20:06 ` Chris Mason [this message]
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
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-3-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