Re: [PATCH 1/3] fork: make the creator the parent of an unlinked task

From: Oleg Nesterov

Date: Fri Dec 04 2026 - 10:28:23 EST


On 12/03, Chris Mason wrote:
>
> 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

I know nothing about bpf, could you add more details please ?

OK, B, child of A, does copy_process().

A exits right after dup_task_struct(). p->real_parent points to nowhere.

Who exactly can dererence p->real_parent at this point?

With the change in dup_task_struct(), why is it correct to pretend that
p->real_parent == B ?

> 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.

Yes... perhaps fork_idle() should use CLONE_PARENT... but your change
looks fine.

> The fix used here is to point both pointers at the forking task until the
> fork can no longer fail,

OK, this change suggests that we have another problem: copy_process() with
CLONE_PARENT or CLONE_THREAD can fail and drop tasklist_lock after it sets
p->real_parent = A. A can exit.

Who can derefence ->real_parent of the soon-to-be-freed and not-yet-linked
task p?

I am just trying to understand the problem. Perhaps we should change
dup_task_struct() to nullify p->thread_pid to make pid_alive(p) false?

Oleg.