Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/8] fork, exit: fix use-after-frees on task pointers
@ 2026-12-05 13:11 Chris Mason
  2026-12-05 13:11 ` [PATCH v2 1/8] tsacct: don't dereference a stale group_leader Chris Mason
                   ` (8 more replies)
  0 siblings, 9 replies; 19+ messages in thread
From: Chris Mason @ 2026-12-05 13:11 UTC (permalink / raw)
  To: brauner, oleg, ast, daniel, andrii, bpf, linux-kernel
  Cc: Felix Kuehling, amd-gfx, akpm, peterz, mingo, kees, viro, jack,
	tj, hannes, mkoutny, bsingharora, eddyz87, memxor, yonghong.song,
	martin.lau, song, jolsa, emil, ihor.solodrai, sched-ext, cgroups,
	linux-fsdevel, linux-mm, mason

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@kernel.org/

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


^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2026-12-05 16:19 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 15:35   ` Oleg Nesterov
2026-12-05 13:12 ` [PATCH v2 3/8] exit: reset a released task's parent and leader Chris Mason
2026-12-05 13:36   ` sashiko-bot
2026-12-05 13:54   ` bot+bpf-ci
2026-12-05 13:12 ` [PATCH v2 4/8] fork: don't leave stale parent pointers in a new task Chris Mason
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:40   ` sashiko-bot
2026-12-05 13:13 ` [RFC PATCH v2 7/8] signal: add task_group_leader() Chris Mason
2026-12-05 15:29   ` Oleg Nesterov
2026-12-05 13:13 ` [RFC PATCH v2 8/8] signal: move group_leader into signal_struct Chris Mason
2026-12-05 13:54   ` bot+bpf-ci
2026-12-05 15:24   ` Oleg Nesterov
2026-12-05 15:59 ` [PATCH] change next_thread() to use list_next_entry_circular() Oleg Nesterov
2026-12-05 16:11   ` sashiko-bot
2026-12-05 16:19   ` Oleg Nesterov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox