Skip to contentSkip to conversation

perf/bpf: Fix BPF program inheritance and overflow races

PATCH New Ian Rogers 2 participants 4 messages Dec 1

Subsystems (2)
lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

cover 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
author reviewer
#1 lore raw

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

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")
---
 kernel/events/core.c | 64 +++++++++++++++++++++++++++++++++-----------
 1 file changed, 49 insertions(+), 15 deletions(-)
kernel/events/core.c +49 -15
index 4ab3919ac78e..369595d95b97 100644
@@ -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) {
Signature · 1 line
2.56.1.839.g22a8290db9-goog
author reviewer
#2 lore raw

PATCH 2/2 perf/bpf: Fix racy event->prog dereference in __perf_event_overflow()

In reply to Ian Rogers

__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")
---
 kernel/events/core.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
kernel/events/core.c +16 -4
index 369595d95b97..526c486d1647 100644
@@ -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;
Signature · 1 line
2.56.1.839.g22a8290db9-goog