linux/arch
Vineet Gupta 5bbbce02e5 bpf, x86: Fix per-CPU address resolution into an extended register
The destination of the per-CPU address MOV is encoded in ModRM.reg,
which is extended by REX.R, but the REX prefix is built with
add_1mod(), which sets REX.B. REX.B extends ModRM.rm and SIB.base, and
this instruction addresses memory as disp32 with no base, so the bit
has no effect at all and the high register bit is simply lost.

Every is_ereg() destination therefore resolves to the wrong register,
picking whichever one shares the low three bits:

  R5 -> RAX    R7 -> RBP    R8 -> RSI    R9 -> RDI

With BPF_REG_5, whose reg2hex is 0, the emitted

  65 49 03 04 25 <off>	add %gs:<off>,%rax

adds the per-CPU offset to RAX rather than R8. The destination keeps
the unadjusted address and RAX is clobbered, so the program goes on to
dereference a pointer that was never made per-CPU:

  BUG: unable to handle page fault for address: 0000607e386a8894
  RIP: bpf_prog_707837aafd2aa9ae_update_percpu_data+0x93/0xc9
  Call Trace:
   __bpf_prog_test_run_raw_tp+0x2dc/0x7d0
   __flush_smp_call_function_queue+0x1e9/0xc80
  Kernel panic - not syncing: Fatal exception in interrupt

R5 is the mildest of the four, aliasing a scratch register and faulting
at the store. R7 aliases RBP and would corrupt the frame pointer, R8
and R9 alias the argument registers.

Use add_2mod() so the register goes through REX.R, matching how
add_2reg() places it in ModRM.reg and how emit_priv_frame_ptr()
hardcodes 0x4c for the same instruction with R9. Encodings for the
non-extended registers are unchanged.

Problem showed up when trying to resurrect BPF_GCC CI (selftests built
with BPF_GCC).

This has gone unnoticed because clang reloads the address into R1
before each per-CPU access, so the destination is never an extended
register. GCC keeps several per-CPU addresses live at once, and
test_progs-bpf_gcc panics the kernel in global_percpu_data/init, where
the address of a .percpu variable ends up in R5.

Fixes: 7bdbf74463 ("bpf: add special internal-only MOV instruction to resolve per-CPU addrs")
Signed-off-by: Vineet Gupta <vineet.gupta@linux.dev>
Reviewed-by: Eduard Zingerman <eddyz87@gmail.com>
Cc: stable@vger.kernel.org
Link: https://patch.msgid.link/20260814220254.3797467-2-vineet.gupta@linux.dev
Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
2026-08-14 23:55:32 -07:00
..
alpha pci-v7.2-changes 2026-06-24 12:48:43 -07:00
arc ARC: configs: Drop redundant I2C_DESIGNWARE_PLATFORM 2026-07-10 15:26:11 -07:00
arm nuvoton: First batch of ARM arch fixes for 7.2 2026-08-04 21:25:25 +02:00
arm64 bpf, arm64: Convert struct_ops arena arguments in the trampoline 2026-08-14 05:55:02 +02:00
csky ring-buffer: Flush and stop persistent ring buffer on panic 2026-05-21 08:20:58 -04:00
hexagon uaccess: unify inline vs outline copy_{from,to}_user() selection 2026-05-28 21:24:44 -07:00
loongarch LoongArch: BPF: Fix memory leak in bpf_jit_free() 2026-07-24 16:33:08 +08:00
m68k m68knommu: fix on top off v7.2-rc4 2026-07-26 08:59:55 -07:00
microblaze microblaze: restore the page alignment of swapper_pg_dir 2026-08-04 20:02:00 -07:00
mips collected fixes for 7.2 2026-07-04 06:05:28 -10:00
nios2 uaccess: unify inline vs outline copy_{from,to}_user() selection 2026-05-28 21:24:44 -07:00
openrisc mm.git review status for mm-hotfixes-stable..mm-nonmm-stable 2026-06-21 13:20:19 -07:00
parisc Replace <linux/mod_devicetable.h> by more specific <linux/device-id/*.h> (headers) 2026-07-03 07:38:16 +02:00
powerpc powerpc/serial: Fix include guard comment 2026-07-28 10:23:32 +05:30
riscv riscv, bpf: Fix missing sign-ext for signed 1-byte and 2-byte kfunc args 2026-08-14 18:31:58 +02:00
s390 bpf, s390: Clear fetch destination on faulting arena atomic 2026-08-12 10:33:53 -07:00
sh Replace <linux/mod_devicetable.h> by more specific <linux/device-id/*.h> (c files) 2026-07-03 07:38:17 +02:00
sparc Replace <linux/mod_devicetable.h> by more specific <linux/device-id/*.h> (c files) 2026-07-03 07:38:17 +02:00
um um: vector: fix use-after-free in vector_mmsg_rx() 2026-07-23 21:31:14 +02:00
x86 bpf, x86: Fix per-CPU address resolution into an extended register 2026-08-14 23:55:32 -07:00
xtensa mm.git review status for mm-hotfixes-stable..mm-nonmm-stable 2026-06-21 13:20:19 -07:00
.gitignore
Kconfig Locking updates for v7.2: 2026-06-15 14:21:14 +05:30