Linux cgroups development
 help / color / mirror / Atom feed
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, viro@zeniv.linux.org.uk,
	jack@suse.cz, tj@kernel.org, hannes@cmpxchg.org,
	mkoutny@suse.com, bsingharora@gmail.com, 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,
	cgroups@vger.kernel.org, linux-fsdevel@vger.kernel.org,
	linux-mm@kvack.org, mason@kernel.org
Subject: [PATCH v2 4/8] fork: don't leave stale parent pointers in a new task
Date: Sat, 05 Dec 2026 08:12:31 -0500	[thread overview]
Message-ID: <20261204233428.14861-5-mason@kernel.org> (raw)
In-Reply-To: <20261204233428.14861-1-mason@kernel.org>

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@kernel.org>
---
 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);
 	}


  parent reply	other threads:[~2026-12-05 13:12 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05 13:11 [PATCH v2 0/8] fork, exit: fix use-after-frees on task pointers Chris Mason
2026-12-05 13:11 ` [PATCH v2 1/8] tsacct: don't dereference a stale group_leader Chris Mason
2026-12-05 13:12 ` [PATCH v2 2/8] exec, cgroup, prlimit: prepare for group_leader resets Chris Mason
2026-12-05 13:12 ` [PATCH v2 3/8] exit: reset a released task's parent and leader Chris Mason
2026-12-05 13:12 ` Chris Mason [this message]
2026-12-05 13:12 ` [PATCH v2 5/8] bpf: Don't start a thread walk at a released task Chris Mason
2026-12-05 13:12 ` [PATCH v2 6/8] drm/amdkfd: fix crashes if a debugged process exits Chris Mason
2026-12-05 13:13 ` [RFC PATCH v2 7/8] signal: add task_group_leader() Chris Mason
2026-12-05 13:13 ` [RFC PATCH v2 8/8] signal: move group_leader into signal_struct 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=20261204233428.14861-5-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=bsingharora@gmail.com \
    --cc=cgroups@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=hannes@cmpxchg.org \
    --cc=ihor.solodrai@linux.dev \
    --cc=jack@suse.cz \
    --cc=jolsa@kernel.org \
    --cc=kees@kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=martin.lau@linux.dev \
    --cc=memxor@gmail.com \
    --cc=mingo@redhat.com \
    --cc=mkoutny@suse.com \
    --cc=oleg@redhat.com \
    --cc=peterz@infradead.org \
    --cc=sched-ext@lists.linux.dev \
    --cc=song@kernel.org \
    --cc=tj@kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    --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