Linux kernel -stable discussions
 help / color / mirror / Atom feed
* Re: [PATCH stable/linux-6.6.y] perf, bpf: Fix NULL event->orig_overflow_handler dereference
  2026-11-24  8:48 [PATCH stable/linux-6.6.y] perf, bpf: Fix NULL event->orig_overflow_handler dereference Luo Gengkun
@ 2026-11-24  8:21 ` Greg Kroah-Hartman
  0 siblings, 0 replies; 2+ messages in thread
From: Greg Kroah-Hartman @ 2026-11-24  8:21 UTC (permalink / raw)
  To: Luo Gengkun
  Cc: stable, Sasha Levin, Peter Zijlstra, Ingo Molnar,
	Arnaldo Carvalho de Melo, Namhyung Kim, Mark Rutland, Jiri Olsa,
	Ian Rogers, Adrian Hunter, James Clark, Alexei Starovoitov,
	David S . Miller, linux-perf-users, linux-kernel, bpf,
	alexander.shishkin

On Tue, Nov 24, 2026 at 08:48:37AM +0000, Luo Gengkun wrote:
> We hit the following problem on x86:
> 
> 	BUG: kernel NULL pointer dereference, address: 0000000000000000
> 	#PF: supervisor instruction fetch in kernel mode
> 	#PF: error_code(0x0010) - not-present page
> 	RIP: 0010:0x0
> 	Call Trace:
> 	 <IRQ>
> 	 bpf_overflow_handler+0xa4/0x120
> 	 __perf_event_overflow+0x11d/0x2f0
> 	 perf_swevent_hrtimer+0x127/0x140
> 	 __hrtimer_run_queues+0x108/0x250
> 	 hrtimer_interrupt+0xfb/0x230
> 	 sysvec_apic_timer_interrupt+0x69/0x90
> 	 </IRQ>
> 	 <TASK>
> 	 asm_sysvec_apic_timer_interrupt+0x1a/0x20
> 	 __perf_event_set_bpf_prog+0xb7/0x180
> 	 _perf_ioctl+0x1f9/0xb40
> 	 perf_ioctl+0x41/0x70
> 	 __se_sys_ioctl+0x82/0xc0
> 	 do_syscall_64+0x44/0xb0
> 	 entry_SYSCALL_64_after_hwframe+0x78/0xe2
> 	 </TASK>
> 
> RIP is 0 with error_code(0x0010) (instruction fetch, not-present), i.e.
> an indirect call through a NULL function pointer.
> 
> perf_event_set_bpf_handler() records the handler currently installed on
> the event and replaces it with bpf_overflow_handler():
> 
> 	event->prog = prog;
> 	event->bpf_cookie = bpf_cookie;
> 	event->orig_overflow_handler = READ_ONCE(event->overflow_handler);
> 	WRITE_ONCE(event->overflow_handler, bpf_overflow_handler);
> 
> WRITE_ONCE() is a single-copy atomic access, not a compiler barrier, and
> the store to orig_overflow_handler is a plain assignment, so the compiler
> may reorder the two stores. GCC 11.4.0 does; disassembly of
> __perf_event_set_bpf_prog():
> 
> 	mov    0x3d0(%rbp),%rax                  ; prev = overflow_handler
> 	mov    %rsi,0x3e8(%rbp)                  ; event->prog = prog
> 	mov    %r13,0x3f0(%rbp)                  ; event->bpf_cookie = cookie
> 	movq   $bpf_overflow_handler,0x3d0(%rbp) ; publish overflow_handler
> 	mov    %rax,0x3e0(%rbp)                  ; orig_overflow_handler = prev
> 
> Use smp_store_release()/smp_load_acquire() to guarantee both the compiler
> and the memory ordering.
> 
> Upstream status:
> Not affected. commit f11f10bfa1ca ("perf/bpf: Call BPF handler directly,
> not through overflow machinery") removed orig_overflow_handler and
> calls bpf_overflow_handler() directly from __perf_event_overflow(), so
> the reordering window no longer exists. That rework was not backported
> to any stable tree.

Please backport that rework instead of doing a one-off patch.

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [PATCH stable/linux-6.6.y] perf, bpf: Fix NULL event->orig_overflow_handler dereference
@ 2026-11-24  8:48 Luo Gengkun
  2026-11-24  8:21 ` Greg Kroah-Hartman
  0 siblings, 1 reply; 2+ messages in thread
From: Luo Gengkun @ 2026-11-24  8:48 UTC (permalink / raw)
  To: stable
  Cc: Luo Gengkun, Greg Kroah-Hartman, Sasha Levin, Peter Zijlstra,
	Ingo Molnar, Arnaldo Carvalho de Melo, Namhyung Kim, Mark Rutland,
	Jiri Olsa, Ian Rogers, Adrian Hunter, James Clark,
	Alexei Starovoitov, David S . Miller, linux-perf-users,
	linux-kernel, bpf, alexander.shishkin

We hit the following problem on x86:

	BUG: kernel NULL pointer dereference, address: 0000000000000000
	#PF: supervisor instruction fetch in kernel mode
	#PF: error_code(0x0010) - not-present page
	RIP: 0010:0x0
	Call Trace:
	 <IRQ>
	 bpf_overflow_handler+0xa4/0x120
	 __perf_event_overflow+0x11d/0x2f0
	 perf_swevent_hrtimer+0x127/0x140
	 __hrtimer_run_queues+0x108/0x250
	 hrtimer_interrupt+0xfb/0x230
	 sysvec_apic_timer_interrupt+0x69/0x90
	 </IRQ>
	 <TASK>
	 asm_sysvec_apic_timer_interrupt+0x1a/0x20
	 __perf_event_set_bpf_prog+0xb7/0x180
	 _perf_ioctl+0x1f9/0xb40
	 perf_ioctl+0x41/0x70
	 __se_sys_ioctl+0x82/0xc0
	 do_syscall_64+0x44/0xb0
	 entry_SYSCALL_64_after_hwframe+0x78/0xe2
	 </TASK>

RIP is 0 with error_code(0x0010) (instruction fetch, not-present), i.e.
an indirect call through a NULL function pointer.

perf_event_set_bpf_handler() records the handler currently installed on
the event and replaces it with bpf_overflow_handler():

	event->prog = prog;
	event->bpf_cookie = bpf_cookie;
	event->orig_overflow_handler = READ_ONCE(event->overflow_handler);
	WRITE_ONCE(event->overflow_handler, bpf_overflow_handler);

WRITE_ONCE() is a single-copy atomic access, not a compiler barrier, and
the store to orig_overflow_handler is a plain assignment, so the compiler
may reorder the two stores. GCC 11.4.0 does; disassembly of
__perf_event_set_bpf_prog():

	mov    0x3d0(%rbp),%rax                  ; prev = overflow_handler
	mov    %rsi,0x3e8(%rbp)                  ; event->prog = prog
	mov    %r13,0x3f0(%rbp)                  ; event->bpf_cookie = cookie
	movq   $bpf_overflow_handler,0x3d0(%rbp) ; publish overflow_handler
	mov    %rax,0x3e0(%rbp)                  ; orig_overflow_handler = prev

Use smp_store_release()/smp_load_acquire() to guarantee both the compiler
and the memory ordering.

Upstream status:
Not affected. commit f11f10bfa1ca ("perf/bpf: Call BPF handler directly,
not through overflow machinery") removed orig_overflow_handler and
calls bpf_overflow_handler() directly from __perf_event_overflow(), so
the reordering window no longer exists. That rework was not backported
to any stable tree.

Affected version:
Introduced by commit aa6a5f3cb2b2 ("perf, bpf: add perf events core support
for BPF_PROG_TYPE_PERF_EVENT programs").

Fixes: aa6a5f3cb2b2 ("perf, bpf: add perf events core support for BPF_PROG_TYPE_PERF_EVENT programs")
Cc: stable@vger.kernel.org # 6.6
Assisted-by: LLM
Signed-off-by: Luo Gengkun <luogengkun@huaweicloud.com>
---
 kernel/events/core.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/kernel/events/core.c b/kernel/events/core.c
index 95743e6c5b0c..3ffb313d610c 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -9717,7 +9717,10 @@ static int __perf_event_overflow(struct perf_event *event,
 		irq_work_queue(&event->pending_irq);
 	}
 
-	READ_ONCE(event->overflow_handler)(event, data, regs);
+	/*
+	 * matches smp_store_release() in perf_event_set_bpf_handler().
+	 */
+	smp_load_acquire(&event->overflow_handler)(event, data, regs);
 
 	if (*perf_event_fasync(event) && event->pending_kill) {
 		event->pending_wakeup = 1;
@@ -10626,7 +10629,12 @@ static int perf_event_set_bpf_handler(struct perf_event *event,
 	event->prog = prog;
 	event->bpf_cookie = bpf_cookie;
 	event->orig_overflow_handler = READ_ONCE(event->overflow_handler);
-	WRITE_ONCE(event->overflow_handler, bpf_overflow_handler);
+	/*
+	 * Ensure the update of overflow_handler is after
+	 * orig_overflow_handler. Otherwise, we may call a NULL
+	 * orig_overflow_handler in bpf_overflow_handler.
+	 */
+	smp_store_release(&event->overflow_handler, bpf_overflow_handler);
 	return 0;
 }
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-24  8:21 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24  8:48 [PATCH stable/linux-6.6.y] perf, bpf: Fix NULL event->orig_overflow_handler dereference Luo Gengkun
2026-11-24  8:21 ` Greg Kroah-Hartman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox