From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796328379; b=RY10mK9yocX3fOe8Hc8tn6qLw0Nbd+heMLpZej9AGdWDY/Fs7eWlhSGs695MKflwLE/4 Ees3xWquhYMEXPDN2+4vwJY4pLUr021cZTQvYf1L34P9IfusKb+dHzRUAnhZ2G0q1l+ua A0AZJ/OdlXAjCzqn6WJE5i/4dGQnDay0Sh0CNGFifOzkxFU6bv0BbEyBIQX3ObeEH1RK+ lMUlWKdAD4UBT4Qt4aQejOrS4JYseuSVbaUldzZuGoA9sFWRkT2wmPPadhliZ9DhITFpQ sUGmhwhpRCybbkx52RewvrQ5GxOZiTRdorsY7npEuFZdG/qFF8Lpb+smtldwRPakN8Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796328379; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=E6DWNjWs1nB5QNK99IsA7CJBw89zW2MKrFDLnqulj84=; b=zK0G2O3dELrL/RogdwsAfoDVZs0Vh73zdz3h2BKe5J+uu1hXDPxXfjorAW8OrDi8MGvY QZ+rwlBdXVZeL5/PfcalvFaePoZfsNpOnqOOwXIEkKs4CAPLCS1E3ainithNNAb+DNH9W 6I2Jer50K7NsBjIzOmtqS3CykuQVni7n6+hmpP5fIzxE3SoH9+riSV2g/6L8TctMSyNw3 SokK/o0A5sNBFDO4C+tQYWgORDwVvMKG/lwMhfvZIzburRcjPdMBwYCxQxyNiJRGy+jU/ PwYyP2Q+fd8ImE8K+SK/0hQ+8TcDBZLkp7byrdDjonXD0a7BaCgZ3lkH47Sl2tlCEDg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=CwN03p1A; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4BE13606A322; Thu, 3 Dec 2026 20:06:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=CwN03p1A Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8F2EB1F000CE; Thu, 3 Dec 2026 20:06:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796328378; bh=E6DWNjWs1nB5QNK99IsA7CJBw89zW2MKrFDLnqulj84=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=CwN03p1ALHZHXYBM0wdXhsZ0G4XLczF4QdfIjAPl4ErC7eEBTb90jWx4HkcQqGzKQ X3Kx9E7iDuP/pwFxQEQqwW+IZw7rQM5ZqalDVlip06wPtId+aFr967rW1DvnUHMHGJ tGLwAD3Uz3hr+At3xJua2OmyovBq/0vq9Y7c2h4sDn5f/yMo5PhbmjsC09NaI9sUX5 cr3Anogf6J5prmInjtNeCC6Vtay7h0FzdmUllU5j0yHxE3pzgozkRB/Nd3RtOX4OBA pnC6DXT7H8QGam/bx8c50/2dScybPMJxcx9SB4DQmXf8d4a2PFX8neaDvqrq//fBFb 05AjTlta+8HKg== From: Chris Mason 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 Message-ID: <20261203200612.26376-2-mason@kernel.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261203200612.26376-1-mason@kernel.org> References: <20261203200612.26376-1-mason@kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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