[PATCH v2 0/8] fork, exit: fix use-after-frees on task pointers
From: Chris Mason
Date: Sat Dec 05 2026 - 08:11:31 EST
Hi everyone,
This is v2 of the AI generated fixes for stale pointers in task_struct.
The kernel only updates real_parent, parent and group_leader while a task is in
the process tree. BPF programs can find it before that, during the fork, and
after that, once it's been released. These pointers can lead to freed memory
then.
In v1, a released thread held a reference on its group leader, which I didn't
love. v2 takes no references. Instead, a task outside of the process tree only
points at tasks that can't go away: the forking task during the fork, and
init_task or itself after release_task().
- Patch 1: taskstats has the same bug without BPF, as Alexei spotted. It checks
pid_alive() now.
- Patch 2: a prep patch for patch 3.
- Patch 3: release_task() resets the pointers.
- Patch 4: the same for tasks that are still being forked, and for idle tasks.
- Patch 5: the BPF thread iterator doesn't start outside of the process tree.
- Patch 6: amdkfd can use a freed group leader too, without any BPF.
- Patches 7 and 8: an optional RFC for Oleg's idea to move group_leader into
signal_struct. BPF programs then see the right leader of a released thread,
but have to read it from the new place.
Patch 7/8 aren't required for my series but I had the bots look at Oleg's
suggestion.
The downside of patches 1-5 is that BPF programs see PID 0 as the parent of a
released task. Without patch 8, they also see a released thread as its own
leader.
AI generated tests reproduce the BPF bugs with KASAN on v7.4-rc5, and none of
them shows up with patches 1-5. Patch 6 isn't tested on hardware.
Changes since v1:
- Patches 1, 2 and 6-8: new.
- Patch 3: no reference on the leader any more, because it leaked (sashiko,
bpf-ci).
- Patch 4: pid_alive() now fails during a fork (Oleg), so patch 5 also catches
tasks that are still being forked (bpf-ci). The pointers are reset after a
failed fork too (sashiko). New changelog (Oleg).
- Patches 3 and 4: no Fixes: lines, because BPF programs could hit these bugs
before the commits that v1 named.
- The title doesn't blame BPF any more (Alexei).
v1: https://lore.kernel.org/all/20261203200612.26376-1-mason@xxxxxxxxxx/
This is on top of v7.4-rc5.
Chris Mason (8):
tsacct: don't dereference a stale group_leader
exec, cgroup, prlimit: prepare for group_leader resets
exit: reset a released task's parent and leader
fork: don't leave stale parent pointers in a new task
bpf: Don't start a thread walk at a released task
drm/amdkfd: fix crashes if a debugged process exits
signal: add task_group_leader()
signal: move group_leader into signal_struct
Documentation/bpf/kfuncs.rst | 2 +-
arch/x86/kernel/fpu/core.c | 4 +-
arch/x86/kernel/fpu/xstate.c | 6 +-
arch/x86/kernel/fpu/xstate.h | 4 +-
arch/x86/kernel/signal.c | 4 +-
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 2 +-
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 2 +-
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 6 +-
drivers/gpu/drm/amd/amdkfd/kfd_priv.h | 3 +-
drivers/gpu/drm/amd/amdkfd/kfd_process.c | 33 ++++---
drivers/gpu/drm/panfrost/panfrost_gem.c | 2 +-
drivers/gpu/drm/panthor/panthor_gem.c | 2 +-
drivers/gpu/drm/panthor/panthor_sched.c | 8 +-
drivers/gpu/drm/scheduler/sched_entity.c | 9 +-
drivers/iommu/iommufd/ioas.c | 4 +-
drivers/iommu/iommufd/pages.c | 4 +-
drivers/vfio/vfio_iommu_type1.c | 6 +-
fs/binfmt_elf.c | 2 +-
fs/binfmt_elf_fdpic.c | 2 +-
fs/exec.c | 17 ++--
include/linux/cgroup.h | 2 +-
include/linux/pid.h | 1 +
include/linux/sched.h | 1 -
include/linux/sched/signal.h | 28 +++++-
include/linux/sched/task.h | 43 +++------
init/init_task.c | 3 +-
kernel/acct.c | 2 +-
kernel/bpf/task_iter.c | 16 +++-
kernel/bpf/verifier.c | 5 +
kernel/cgroup/cgroup.c | 26 +++---
kernel/cgroup/cpuset.c | 4 +-
kernel/debug/kdb/kdb_main.c | 2 +-
kernel/exit.c | 5 +-
kernel/fork.c | 93 +++++++++++++------
kernel/signal.c | 6 +-
kernel/sys.c | 22 ++---
kernel/tsacct.c | 6 +-
lib/is_single_threaded.c | 2 +-
rust/helpers/task.c | 7 ++
rust/kernel/task.rs | 9 +-
security/apparmor/resource.c | 2 +-
security/yama/yama_lsm.c | 10 +-
tools/bpf/bpftool/skeleton/pid_iter.bpf.c | 25 ++++-
.../selftests/bpf/progs/rcu_read_lock.c | 4 +-
.../selftests/bpf/progs/task_kfunc_success.c | 4 +-
.../bpf/progs/test_core_reloc_kernel.c | 47 +++++-----
.../test.d/dynevent/btf_typecast_accepted.tc | 4 +-
47 files changed, 301 insertions(+), 200 deletions(-)
base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244