BPF List
 help / color / mirror / Atom feed
* [PATCH bpf-next v4 0/6] bpf: Fix async callback use-after-free on map deletion
@ 2026-11-19  3:30 Yun Lu
  2026-11-19  3:30 ` [PATCH bpf-next v4 1/6] bpf: Mark task work FREED before detaching it Yun Lu
                   ` (5 more replies)
  0 siblings, 6 replies; 10+ messages in thread
From: Yun Lu @ 2026-11-19  3:30 UTC (permalink / raw)
  To: ast, daniel, andrii, eddyz87, memxor, martin.lau, song,
	yonghong.song, jolsa, emil, ihor.solodrai
  Cc: yatsenko, bpf

From: Yun Lu <luyun@kylinos.cn>

This series fixes a use-after-free when a BPF program schedules task
work, workqueue work or a timer through a value pointer retained across
map deletion. Review of the earlier task-work series [1] pointed out
that cancellation can leave the deleted value open to reinitialization.

The original reader keeps the element alive while it queues new work,
but the new callback can start outside the grace period that protects
the element and access the freed key or value. Patch 2 describes the
race and includes a KASAN report from an unmodified kernel.

Mark task work FREED or clear cb->map before detaching the context.
Following Alexei's suggestions [2, 3], check task-work membership after
claiming PENDING and check wq/timer membership in the shared setter.
The setter also rejects zero usercnt. An idle callback can survive in
an unlinked cached element past the last user reference and be reused
when the element is inserted again.

Patches 1-3 fix task work and add its regression test. Patches 4-6 fix
the shared wq/timer paths and cover both in the wq and rhash_timer
selftests.

Testing on x86-64 QEMU/KVM with KASAN, PROVE_LOCKING, PROVE_RCU and
DEBUG_ATOMIC_SLEEP:

- task_work_stress, wq, rhash_timer, timer and timer_mim pass.
- With the same test binary on v3, the new wq test fails as expected:
  the setter returns 0 instead of -EPERM.
- A reduced LKMM model rules out the stale usercnt result with the full
  barrier after the membership check.

Changes since v3:
- Reject zero usercnt in the shared setter (Sashiko), order the check
  after lookup with a full barrier, and add a wq regression for reuse.
- Consolidate the task-work return value documentation (bpf-ci).

v3: https://lore.kernel.org/all/20261111024333.228633-1-luyun_611@163.com/

Changes since v2:
- Use the existing map lookup and move the wq/timer check to the shared
  setter (Alexei).
- Give slot detachment to the first invalidator and reuse the existing
  task-work, wq and timer selftests.

v2: https://lore.kernel.org/all/20261106095747.471718-1-luyun_611@163.com/

Changes since v1:
- Replace slot poisoning and allocator hooks with lookup checks
  and split out cancellation ordering (Alexei).
- Extend the fix and regression coverage to wq and timers (Alexei,
  Mykyta).

v1: https://lore.kernel.org/all/20261028073525.1035605-1-luyun_611@163.com/

[1] https://lore.kernel.org/all/20261012061915.231254-1-luyun_611@163.com/
[2] https://lore.kernel.org/all/DMGN8VM2BAOR.28NRNN51MCI8I@gmail.com/
[3] https://lore.kernel.org/all/DMOAQI1S0JBS.3N3WETHD2L6KQ@gmail.com/

Yun Lu (6):
  bpf: Mark task work FREED before detaching it
  bpf: Check task work value membership before scheduling
  selftests/bpf: Test task work scheduling on a deleted value
  bpf: Invalidate async callbacks before detaching them
  bpf: Check async callback membership and user references
  selftests/bpf: Test wq callback setup after deletion and user release

 include/uapi/linux/bpf.h                      |   2 +
 kernel/bpf/helpers.c                          |  89 +++++++++++++--
 tools/include/uapi/linux/bpf.h                |   2 +
 .../selftests/bpf/prog_tests/rhash_timer.c    |  24 ++--
 .../bpf/prog_tests/task_work_stress.c         |  18 +++
 tools/testing/selftests/bpf/prog_tests/wq.c   | 103 +++++++++++++++++-
 .../testing/selftests/bpf/progs/rhash_timer.c |  26 ++---
 .../selftests/bpf/progs/task_work_stress.c    |  17 +++
 tools/testing/selftests/bpf/progs/wq.c        |  62 +++++++++++
 9 files changed, 299 insertions(+), 44 deletions(-)


base-commit: bd75869299f1d4ff6e9f45117cce28f8badee1cd
-- 
2.43.0


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

end of thread, other threads:[~2026-11-19  4:20 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19  3:30 [PATCH bpf-next v4 0/6] bpf: Fix async callback use-after-free on map deletion Yun Lu
2026-11-19  3:30 ` [PATCH bpf-next v4 1/6] bpf: Mark task work FREED before detaching it Yun Lu
2026-11-19  3:30 ` [PATCH bpf-next v4 2/6] bpf: Check task work value membership before scheduling Yun Lu
2026-11-19  3:30 ` [PATCH bpf-next v4 3/6] selftests/bpf: Test task work scheduling on a deleted value Yun Lu
2026-11-19  3:30 ` [PATCH bpf-next v4 4/6] bpf: Invalidate async callbacks before detaching them Yun Lu
2026-11-19  3:30 ` [PATCH bpf-next v4 5/6] bpf: Check async callback membership and user references Yun Lu
2026-11-19  4:20   ` bot+bpf-ci
2026-11-19  3:30 ` [PATCH bpf-next v4 6/6] selftests/bpf: Test wq callback setup after deletion and user release Yun Lu
2026-11-19  3:43   ` sashiko-bot
2026-11-19  4:05   ` bot+bpf-ci

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