Re: [PATCH stable/linux-6.6.y] perf, bpf: Fix NULL event->orig_overflow_handler dereference
From: Greg Kroah-Hartman
Date: Tue Nov 24 2026 - 03:21:27 EST
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