[PATCH v1 1/2] perf/bpf: Fix BPF program inheritance races, cookie, and leak in perf_event_alloc()

From: Ian Rogers

Date: Tue Dec 01 2026 - 02:17:22 EST


Fix four issues in how perf_event_alloc() inherits parent_event->prog
and how non-tracing BPF handlers are installed and released:

1. Tracing events (tracepoints, kprobes, uprobes) attach BPF programs to
the shared tp_event->prog_array rather than invoking event->prog via
bpf_overflow_handler(). Commit f11f10bfa1ca ("perf/bpf: Call BPF
handler directly, not through overflow machinery") replaced the
overflow_handler == bpf_overflow_handler check in perf_event_alloc()
with an unconditional parent_event->prog check, causing inherited
tracing children to copy parent_event->prog without attaching to
tp_event->prog_array. When such a child exits, _free_event() calls
perf_event_free_bpf_prog() -> perf_event_detach_bpf_prog(), removing
the parent's BPF program from the shared tp_event->prog_array and
disabling BPF execution for the still-live parent event. Skip
inheritance when perf_event_is_tracing(parent_event) is true.

2. For non-tracing events, perf_event_alloc() reads parent_event->prog
and calls bpf_prog_inc() locklessly, which races with concurrent
bpf_perf_link_release() -> perf_event_free_bpf_handler() clearing
event->prog and dropping its last reference, leading to a potential
NULL dereference or use-after-free. Protect the lockless read and
bpf_prog_inc_not_zero() with both rcu_tasks_trace and rcu read-side
critical sections, paired with smp_store_release() in
perf_event_set_bpf_handler() and xchg() (plus synchronize_rcu() when
bpf_prog_runs_sleepable() is true) in perf_event_free_bpf_handler().

3. Copy parent_event->bpf_cookie alongside parent_event->prog using
smp_load_acquire() / READ_ONCE() paired with WRITE_ONCE() /
smp_store_release() in perf_event_set_bpf_handler(), so inherited
child events preserve the configured BPF cookie for
bpf_get_attach_cookie_pe().

4. If any fallible initialization step in perf_event_alloc() fails after
acquiring the inherited BPF program reference, __free(__free_event)
does not call perf_event_free_bpf_prog(), leaking the bpf_prog
reference. Move perf_event_free_bpf_prog() from _free_event() into
__free_event() (before event->destroy()), matching
security_perf_event_free().

Fixes: aa6a5f3cb2b2 ("perf, bpf: add perf events core support for BPF_PROG_TYPE_PERF_EVENT programs")
Fixes: 82e6b1eee6a8 ("bpf: Allow to specify user-provided bpf_cookie for BPF perf links")
Fixes: f11f10bfa1ca ("perf/bpf: Call BPF handler directly, not through overflow machinery")
Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
kernel/events/core.c | 64 +++++++++++++++++++++++++++++++++-----------
1 file changed, 49 insertions(+), 15 deletions(-)

diff --git a/kernel/events/core.c b/kernel/events/core.c
index 4ab3919ac78e..369595d95b97 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -30,6 +30,7 @@
#include <linux/hardirq.h>
#include <linux/hugetlb.h>
#include <linux/rculist.h>
+#include <linux/rcupdate_trace.h>
#include <linux/uaccess.h>
#include <linux/syscalls.h>
#include <linux/anon_inodes.h>
@@ -5790,6 +5791,7 @@ static void __free_event(struct perf_event *event)
{
struct pmu *pmu = event->pmu;

+ perf_event_free_bpf_prog(event);
security_perf_event_free(event);

if (event->attach_state & PERF_ATTACH_CALLCHAIN)
@@ -5868,7 +5870,6 @@ static void _free_event(struct perf_event *event)
mutex_unlock(&event->mmap_mutex);
}

- perf_event_free_bpf_prog(event);
perf_free_addr_filters(event);

__free_event(event);
@@ -10858,7 +10859,7 @@ static inline int perf_event_set_bpf_handler(struct perf_event *event,
/* hw breakpoint or kernel counter */
return -EINVAL;

- if (event->prog)
+ if (READ_ONCE(event->prog))
return -EEXIST;

if (prog->type != BPF_PROG_TYPE_PERF_EVENT)
@@ -10881,19 +10882,21 @@ static inline int perf_event_set_bpf_handler(struct perf_event *event,
return -EPROTO;
}

- event->prog = prog;
- event->bpf_cookie = bpf_cookie;
+ WRITE_ONCE(event->bpf_cookie, bpf_cookie);
+ /* Pairs with smp_load_acquire() in perf_event_inherit_bpf_prog() */
+ smp_store_release(&event->prog, prog);
return 0;
}

static inline void perf_event_free_bpf_handler(struct perf_event *event)
{
- struct bpf_prog *prog = event->prog;
+ struct bpf_prog *prog = xchg(&event->prog, NULL);

if (!prog)
return;

- event->prog = NULL;
+ if (bpf_prog_runs_sleepable(prog))
+ synchronize_rcu();
bpf_prog_put(prog);
}
#else
@@ -11938,7 +11941,7 @@ int perf_event_set_bpf_prog(struct perf_event *event,

void perf_event_free_bpf_prog(struct perf_event *event)
{
- if (!event->prog)
+ if (!READ_ONCE(event->prog))
return;

if (!perf_event_is_tracing(event)) {
@@ -13517,6 +13520,44 @@ static void account_event(struct perf_event *event)
account_pmu_sb_event(event);
}

+#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_EVENT_TRACING)
+static void perf_event_inherit_bpf_prog(struct perf_event *event,
+ struct perf_event *parent_event)
+{
+ struct bpf_prog *prog;
+ u64 bpf_cookie;
+
+ /*
+ * Only non-tracing events invoke event->prog directly. Tracing events
+ * attach to the shared tp_event->prog_array, so copying
+ * parent_event->prog without attaching would cause child teardown to
+ * detach the parent's program.
+ */
+ if (perf_event_is_tracing(parent_event))
+ return;
+
+ guard(rcu_tasks_trace)();
+ guard(rcu)();
+ /* Pairs with smp_store_release() in perf_event_set_bpf_handler() */
+ prog = smp_load_acquire(&parent_event->prog);
+ if (!prog)
+ return;
+
+ bpf_cookie = READ_ONCE(parent_event->bpf_cookie);
+ prog = bpf_prog_inc_not_zero(prog);
+ if (IS_ERR(prog))
+ return;
+
+ event->bpf_cookie = bpf_cookie;
+ event->prog = prog;
+}
+#else
+static inline void perf_event_inherit_bpf_prog(struct perf_event *event,
+ struct perf_event *parent_event)
+{
+}
+#endif
+
/*
* Allocate and initialize an event structure
*/
@@ -13611,14 +13652,7 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu,
if (!overflow_handler && parent_event) {
overflow_handler = parent_event->overflow_handler;
context = parent_event->overflow_handler_context;
-#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_EVENT_TRACING)
- if (parent_event->prog) {
- struct bpf_prog *prog = parent_event->prog;
-
- bpf_prog_inc(prog);
- event->prog = prog;
- }
-#endif
+ perf_event_inherit_bpf_prog(event, parent_event);
}

if (overflow_handler) {
--
2.56.1.839.g22a8290db9-goog