perf/bpf: Fix BPF program inheritance and overflow races
Get series with b4
b4 am [email protected]Versions newest first
- v1 Latest2 patches 0 repliesNew
Patches in v1 No applies recorded yet
| # | Subject and files | Size | Replies | Applied |
|---|---|---|---|---|
| 1/2 | perf/bpf: Fix BPF program inheritance races, cookie, and leak in perf_event_alloc()kernel/ | +49 | 0 replies | |
| 2/2 | perf/bpf: Fix racy event->prog dereference in __perf_event_overflow()kernel/ | +16 | 0 replies |
Reviews trailers per patch
No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.
Cover letter [PATCH v1 0/2] perf/bpf: Fix BPF program inheritance and overflow races
This two-patch series fixes pre-existing races, cookie handling, and a reference leak in how perf_event handles BPF programs attached to non-tracing and tracing events:
1. perf_event_alloc() / perf_event_set_bpf_handler() / perf_event_free_bpf_handler():
- Skip inheriting parent_event->prog on tracing events (tracepoints,
kprobes, uprobes), where copying event->prog without attaching to
tp_event->prog_array caused child teardown to detach the parent's
program. - Protect lockless inheritance of parent_event->prog with rcu_tasks_trace + rcu read locks, smp_load_acquire()/ smp_store_release(), xchg(), synchronize_rcu() for sleepable programs, and bpf_prog_inc_not_zero().
- Preserve parent_event->bpf_cookie across event inheritance.
- Move perf_event_free_bpf_prog() into __free_event() so perf_event_alloc() error paths cannot leak the inherited bpf_prog reference.
2. __perf_event_overflow() / bpf_overflow_handler():
- Avoid dereferencing event->prog->type outside an RCU read-side critical section in __perf_event_overflow().
- Check prog->type == BPF_PROG_TYPE_PERF_EVENT under rcu_read_lock() using smp_load_acquire(), returning 1 if event->prog was concurrently cleared so samples are not dropped.
These issues were identified via code audit and Sashiko automated review while developing perf/BPF changes; the fixes, regression tests, and changelogs were drafted with an AI coding assistant.
Testing:
- Build-tested kernel/events/core.o across x86_64, arm64, riscv, loongarch, CONFIG_BPF_SYSCALL=n (W=1 clean), and CONFIG_BPF_SYSCALL=y + CONFIG_EVENT_TRACING=y + CONFIG_BPF_EVENTS=n.
- Runtime-tested in x86_64 QEMU/KVM with CONFIG_PROVE_LOCKING=y and CONFIG_PROVE_RCU=y using targeted C regression tests covering:
(1) inherited tracepoint BPF program continuing to fire after an inherited child exits,
(2) bpf_get_attach_cookie_pe() returning the parent's cookie on an inherited child event, and
(3) concurrent sampling overflow and bpf_perf_link_release() detach stress loops (40 iterations, zero lockdep/RCU splats).
Both patches are based on torvalds/master (and apply cleanly to tip/perf/urgent).
Ian Rogers (2):
perf/bpf: Fix BPF program inheritance races, cookie, and leak in
perf_event_alloc()
perf/bpf: Fix racy event->prog dereference in __perf_event_overflow()
kernel/events/core.c | 84 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 65 insertions(+), 19 deletions(-)
base-commit: 19cc08c1a6a650f8e54c0461ff198be6448f425d
Signature · 1 line
2.56.1.839.g22a8290db9-goog