From: Ian Rogers <irogers@google.com>
To: Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
Namhyung Kim <namhyung@kernel.org>,
Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>
Cc: Eduard Zingerman <eddyz87@gmail.com>,
Kumar Kartikeya Dwivedi <memxor@gmail.com>,
Mark Rutland <mark.rutland@arm.com>,
Alexander Shishkin <alexander.shishkin@linux.intel.com>,
Jiri Olsa <jolsa@kernel.org>,
Adrian Hunter <adrian.hunter@intel.com>,
James Clark <james.clark@linaro.org>, Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
linux-perf-users@vger.kernel.org, bpf@vger.kernel.org,
linux-kernel@vger.kernel.org, Ian Rogers <irogers@google.com>
Subject: [PATCH v1 1/2] perf/bpf: Fix BPF program inheritance races, cookie, and leak in perf_event_alloc()
Date: Mon, 30 Nov 2026 23:16:52 -0800 [thread overview]
Message-ID: <20261201071653.827828-2-irogers@google.com> (raw)
In-Reply-To: <20261201071653.827828-1-irogers@google.com>
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@google.com>
---
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
next prev parent reply other threads:[~2026-12-01 7:17 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 7:16 [PATCH v1 0/2] perf/bpf: Fix BPF program inheritance and overflow races Ian Rogers
2026-12-01 7:16 ` Ian Rogers [this message]
2026-12-01 7:16 ` [PATCH v1 2/2] perf/bpf: Fix racy event->prog dereference in __perf_event_overflow() Ian Rogers
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261201071653.827828-2-irogers@google.com \
--to=irogers@google.com \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=james.clark@linaro.org \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=memxor@gmail.com \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
--cc=song@kernel.org \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®