BPF List
 help / color / mirror / Atom feed
* [PATCH stable/linux-6.6.y 0/9] perf/bpf: Fix NULL pointer dereference
@ 2026-11-26  4:34 Luo Gengkun
  2026-11-26  4:34 ` [PATCH stable/linux-6.6.y 1/9] perf/bpf: Reorder bpf_overflow_handler() ahead of __perf_event_overflow() Luo Gengkun
                   ` (8 more replies)
  0 siblings, 9 replies; 11+ messages in thread
From: Luo Gengkun @ 2026-11-26  4:34 UTC (permalink / raw)
  To: stable, Greg Kroah-Hartman, Sasha Levin
  Cc: Will Deacon, Mark Rutland, Russell King, Catalin Marinas,
	Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo, Jiri Olsa,
	Namhyung Kim, Ian Rogers, Adrian Hunter, Song Liu, Kyle Huey,
	Andrii Nakryiko, linux-arm-kernel, linux-kernel, linux-perf-users,
	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

For a software event the overflow comes from perf_swevent_hrtimer() in
hardirq context on the same CPU, so no memory ordering is involved at all,
only the store order; the publish lands first and the hrtimer interrupt
fires in between. bpf_overflow_handler() then runs the attached BPF program
and, for a non-zero return value, calls the still-NULL
orig_overflow_handler (struct perf_event is allocated with __GFP_ZERO).

Upstream status:
Not affected. f11f10bfa1ca ("perf/bpf: Call BPF handler directly, not
through overflow machinery", v6.10) 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, and there is no standalone upstream fix for the
ordering itself.

Affected version:
Introduced by aa6a5f3cb2b2 ("perf, bpf: add perf events core support for
BPF_PROG_TYPE_PERF_EVENT programs", v4.9) and still present in the 5.10,
5.15, 6.1 and 6.6 LTS trees; v6.10 removed the field altogether.

To backport f11f10bfa1ca ("perf/bpf: Call BPF handler directly, not
through overflow machinery"), that commit has to come with the series it
was posted in, and with the fixups it later needed. f11f is not a
standalone change: it calls bpf_overflow_handler() from
__perf_event_overflow(), i.e. from a position the series first moves that
function to, and the new call site reads struct perf_event members that
the series first un-conditionalizes. On its own it does not produce a
tree that builds in every Kconfig combination, and it regresses bpftrace
until the follow-up fix is in.

The series f11f was posted in, and which this backport carries in full:

  Link: https://lore.kernel.org/r/20240412015019.7060-1-khuey@kylehuey.com

  1. 4c03fe11b96b perf/bpf: Reorder bpf_overflow_handler() ahead of
     __perf_event_overflow()                             [prerequisite]
  2. 924d934393f9 perf/bpf: Create bpf_overflow_handler() stub for
     !CONFIG_BPF_SYSCALL                                 [prerequisite]
  3. 14e40a9578b7 perf/bpf: Remove #ifdef CONFIG_BPF_SYSCALL from struct
     perf_event members                                  [prerequisite]
  4. f11f10bfa1ca perf/bpf: Call BPF handler directly, not through
     overflow machinery                                 [the fix]
  5. 76f6d5884582 perf/bpf: Remove unneeded uses_default_overflow_handler()
  6. c4fcc7d1f415 perf/bpf: Allow a BPF program to suppress all sample
     side effects                                  [rest of the series]

  f11f (0004 here) is the fix. 0001-0003 are prerequisites: 0001 moves
  bpf_overflow_handler() ahead of __perf_event_overflow(), which is where
  0004 calls it from; 0002 and 0003 make the new call site, which sits
  outside #ifdef CONFIG_BPF_SYSCALL and reads event->prog
  unconditionally, work when BPF is disabled. 0005 and 0006 are part of
  the same series: they clean up what the refactoring left behind and add
  the ability to suppress side effects, which is the point of the rework
  but not the reason we are backporting it.

The follow-up fixes, i.e. what upstream had to correct after the series
landed:

  7. 93d3fde7fd19 perf/bpf: Change the !CONFIG_BPF_SYSCALL stubs to
     static inlines                                  [compile fix, Ingo]
  8. 854dd99b5ddc perf/bpf: Mark perf_event_set_bpf_handler() and
     perf_event_free_bpf_handler() as inline too     [compile fix, Ingo]
  9. 100bff23818e perf/bpf: Don't call bpf_overflow_handler() for tracing
     events                                          [Fixes: f11f10bfa1ca]

The result is a verbatim backport of the series and its follow-up fixes
(one hunk re-based, see "Adaptation notes"), so the tree stays identical
to mainline and later perf/bpf backports continue to apply.

Adaptation notes
----------------

Only 0005 needs manual handling.  0001-0004 and 0006-0009 apply with
plain "git apply".  0005 conflicts on include/linux/perf_event.h because
the hunk trailer references perf_event_header__init_id(), which does not
exist in 6.6.y (perf_event_header__init() still has the extra
type/misc/size arguments).  It was applied by replacing exactly the
removed block; the resulting file is byte-identical to the mainline
result for that hunk.

Ingo Molnar (2):
  perf/bpf: Change the !CONFIG_BPF_SYSCALL stubs to static inlines
  perf/bpf: Mark perf_event_set_bpf_handler() and
    perf_event_free_bpf_handler() as inline too

Kyle Huey (7):
  perf/bpf: Reorder bpf_overflow_handler() ahead of
    __perf_event_overflow()
  perf/bpf: Create bpf_overflow_handler() stub for !CONFIG_BPF_SYSCALL
  perf/bpf: Remove #ifdef CONFIG_BPF_SYSCALL from struct perf_event
    members
  perf/bpf: Call BPF handler directly, not through overflow machinery
  perf/bpf: Remove unneeded uses_default_overflow_handler()
  perf/bpf: Allow a BPF program to suppress all sample side effects
  perf/bpf: Don't call bpf_overflow_handler() for tracing events

 arch/arm/kernel/hw_breakpoint.c   |   8 +-
 arch/arm64/kernel/hw_breakpoint.c |   4 +-
 include/linux/perf_event.h        |  23 +---
 kernel/events/core.c              | 193 +++++++++++++++---------------
 4 files changed, 108 insertions(+), 120 deletions(-)

-- 
2.34.1


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

end of thread, other threads:[~2026-11-26  4:22 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26  4:34 [PATCH stable/linux-6.6.y 0/9] perf/bpf: Fix NULL pointer dereference Luo Gengkun
2026-11-26  4:34 ` [PATCH stable/linux-6.6.y 1/9] perf/bpf: Reorder bpf_overflow_handler() ahead of __perf_event_overflow() Luo Gengkun
2026-11-26  4:34 ` [PATCH stable/linux-6.6.y 2/9] perf/bpf: Create bpf_overflow_handler() stub for !CONFIG_BPF_SYSCALL Luo Gengkun
2026-11-26  4:34 ` [PATCH stable/linux-6.6.y 3/9] perf/bpf: Remove #ifdef CONFIG_BPF_SYSCALL from struct perf_event members Luo Gengkun
2026-11-26  4:34 ` [PATCH stable/linux-6.6.y 4/9] perf/bpf: Call BPF handler directly, not through overflow machinery Luo Gengkun
2026-11-26  4:22   ` sashiko-bot
2026-11-26  4:34 ` [PATCH stable/linux-6.6.y 5/9] perf/bpf: Remove unneeded uses_default_overflow_handler() Luo Gengkun
2026-11-26  4:34 ` [PATCH stable/linux-6.6.y 6/9] perf/bpf: Allow a BPF program to suppress all sample side effects Luo Gengkun
2026-11-26  4:34 ` [PATCH stable/linux-6.6.y 7/9] perf/bpf: Change the !CONFIG_BPF_SYSCALL stubs to static inlines Luo Gengkun
2026-11-26  4:34 ` [PATCH stable/linux-6.6.y 8/9] perf/bpf: Mark perf_event_set_bpf_handler() and perf_event_free_bpf_handler() as inline too Luo Gengkun
2026-11-26  4:34 ` [PATCH stable/linux-6.6.y 9/9] perf/bpf: Don't call bpf_overflow_handler() for tracing events Luo Gengkun

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