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 2/2] perf/bpf: Fix racy event->prog dereference in __perf_event_overflow()
Date: Mon, 30 Nov 2026 23:16:53 -0800 [thread overview]
Message-ID: <20261201071653.827828-3-irogers@google.com> (raw)
In-Reply-To: <20261201071653.827828-1-irogers@google.com>
__perf_event_overflow() checks event->prog and dereferences
event->prog->type using plain loads outside an RCU read-side critical
section before calling bpf_overflow_handler(). When a BPF perf link
attached to an active sampling event is released concurrently via
bpf_perf_link_release() -> perf_event_free_bpf_handler(), event->prog
can be cleared and freed on another CPU between the check and the
dereference (causing a NULL dereference or use-after-free in IRQ/NMI
context), or cleared just before bpf_overflow_handler() reads it (causing
bpf_overflow_handler() to return 0 and silently drop the sample).
Guard the fast path in __perf_event_overflow() with
!perf_event_is_tracing(event) && READ_ONCE(event->prog) without
dereferencing event->prog outside RCU, and move the
prog->type == BPF_PROG_TYPE_PERF_EVENT check inside
bpf_overflow_handler() under rcu_read_lock() using smp_load_acquire().
If event->prog was cleared before bpf_overflow_handler() reads it, return
1 so normal overflow sample delivery is not suppressed.
Fixes: 100bff23818e ("perf/bpf: Don't call bpf_overflow_handler() for tracing events")
Signed-off-by: Ian Rogers <irogers@google.com>
---
kernel/events/core.c | 20 ++++++++++++++++----
1 file changed, 16 insertions(+), 4 deletions(-)
diff --git a/kernel/events/core.c b/kernel/events/core.c
index 369595d95b97..526c486d1647 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -10836,13 +10836,16 @@ static int bpf_overflow_handler(struct perf_event *event,
if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1))
goto out;
rcu_read_lock();
- prog = READ_ONCE(event->prog);
- if (prog) {
+ /* Pairs with smp_store_release() in perf_event_set_bpf_handler() */
+ prog = smp_load_acquire(&event->prog);
+ if (prog && prog->type == BPF_PROG_TYPE_PERF_EVENT) {
perf_prepare_sample(data, event, regs);
/* bpf_perf_event_data::addr is readable without PERF_SAMPLE_ADDR */
if (!(data->sample_flags & PERF_SAMPLE_ADDR))
data->addr = 0;
ret = bpf_prog_run(prog, &ctx);
+ } else {
+ ret = 1;
}
rcu_read_unlock();
out:
@@ -10919,6 +10922,15 @@ static inline void perf_event_free_bpf_handler(struct perf_event *event)
}
#endif
+#ifdef CONFIG_EVENT_TRACING
+static bool perf_event_is_tracing(struct perf_event *event);
+#else
+static inline bool perf_event_is_tracing(struct perf_event *event)
+{
+ return false;
+}
+#endif
+
/*
* Generic event overflow handling, sampling.
*/
@@ -10942,7 +10954,7 @@ static int __perf_event_overflow(struct perf_event *event,
if (event->attr.aux_pause)
perf_event_aux_pause(event->aux_event, true);
- if (event->prog && event->prog->type == BPF_PROG_TYPE_PERF_EVENT &&
+ if (!perf_event_is_tracing(event) && READ_ONCE(event->prog) &&
!bpf_overflow_handler(event, data, regs))
goto out;
@@ -11854,7 +11866,7 @@ static void perf_event_free_filter(struct perf_event *event)
* returns true if the event is a tracepoint, or a kprobe/upprobe created
* with perf_event_open()
*/
-static inline bool perf_event_is_tracing(struct perf_event *event)
+static bool perf_event_is_tracing(struct perf_event *event)
{
if (event->pmu == &perf_tracepoint)
return true;
--
2.56.1.839.g22a8290db9-goog
next prev parent reply other threads:[~2026-12-01 7:17 UTC|newest]
Thread overview: 4+ 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 ` [PATCH v1 1/2] perf/bpf: Fix BPF program inheritance races, cookie, and leak in perf_event_alloc() Ian Rogers
2026-12-01 7:16 ` Ian Rogers [this message]
2026-12-01 7:30 ` [PATCH v1 0/2] perf/bpf: Fix BPF program inheritance and overflow races sashiko-bot
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-3-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.