[Bug report] x86-64 apply_returns() decodes attacker-controlled addresses from unvalidated .return_sites entries - wild read on module load
From: Guikun Yang
Date: Fri Nov 13 2026 - 22:12:15 EST
From: Guikun Yang <yangguikun19@xxxxxx>
To: "Peter Zijlstra (Intel)" <peterz@xxxxxxxxxxxxx>,
Ingo Molnar <mingo@xxxxxxxxxx>,
Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
Cc: Luis Chamberlain <mcgrof@xxxxxxxxxx>, Petr Pavlu <petr.pavlu@xxxxxxxx>,
Thomas Gleixner <tglx@xxxxxxxxxxxxx>, Borislav Petkov <bp@xxxxxxxxx>,
Dave Hansen <dave.hansen@xxxxxxxxxxxxxxx>, x86@xxxxxxxxxx,
linux-modules@xxxxxxxxxxxxxxx, linux-kernel@xxxxxxxxxxxxxxx
Subject: [Bug report] x86-64 apply_returns() decodes attacker-controlled
addresses from unvalidated .return_sites entries - wild read on module load
Hi Peter, Ingo,
The .return_sites section of a module is consumed by apply_returns()
without any validation of the computed addresses. Each entry is an s32
relative offset whose final value is fully attacker-controlled via the
R_X86_64_PC32 relocation addend (that relocation path has no overflow
check either), and the decoder dereferences the resulting address
byte-by-byte inside init_module() - a wild kernel read, deterministic,
KASLR-independent.
BUG: unable to handle page fault for address: ffffffff402000e7
Oops: 0000 [#1] SMP DEBUG_PAGEALLOC KASAN NOPTI
RIP: 0010:insn_get_prefixes.part.0+0xc1/0x14c0
RBX: ffffffff402000e7 (the forged site pointer)
Call Trace:
insn_decode
apply_returns
module_finalize
load_module
__do_sys_init_module
The faulting address is the module's .return_sites location plus the
forged s32 - pointing into the unmapped hole below the kernel image.
## The path
module_finalize() hands the section over verbatim
(arch/x86/kernel/module.c):
if (returns) {
void *rseg = (void *)returns->sh_addr;
apply_returns(rseg, rseg + returns->sh_size);
}
apply_returns() computes and decodes without any bounds check
(arch/x86/kernel/alternative.c):
for (s = start; s < end; s++) {
void *dest = NULL, *addr = (void *)s + *s;
struct insn insn;
...
ret = insn_decode_kernel(&insn, addr); /* wild read */
insn_decode_kernel() expands to insn_decode(..., MAX_INSN_SIZE,
INSN_MODE_KERN); the buffer length only bounds the decoder's own
15-byte window, not the validity of the address. insn_get_prefixes()
fetches bytes with get_unaligned() one at a time from that address.
## How the s32 becomes attacker-controlled
objtool emits .return_sites as zero bytes plus R_X86_64_PC32
relocations against .text symbols. __write_relocate_add() handles that
type as:
case R_X86_64_PC32:
case R_X86_64_PLT32:
val -= (u64)loc;
size = 4;
break;
with no overflow check (R_X86_64_32/32S have one; PC32/PLT32 do not).
Modifying one r_addend in .rela.return_sites therefore controls the
s32 that ends up in the section - the write target bytes are already
zero, so nothing else in the file needs to change.
## Impact: not only a crash
The decode-failure and thunk-mismatch paths both print kernel memory
from the wild address:
WARN_ONCE(dest != &__x86_return_thunk,
"missing return thunk: %pS-%pS: %*ph",
addr, dest, 5, addr);
That %*ph dumps five bytes read from the attacker-chosen address into
the kernel log. A local user who can read dmesg gets a five-byte
peek at any address within +/-2GB of the module's .return_sites
per load attempt - a repeatable kernel-memory disclosure, in addition
to the deterministic page fault.
## apply_retpolines() is the same pattern
The sibling consumer (arch/x86/kernel/alternative.c) is identical in
shape:
for (s = start; s < end; s++) {
void *addr = (void *)s + *s;
...
ret = insn_decode_kernel(&insn, addr);
and is reachable the same way through .retpoline_sites. Both should
be fixed together.
## The fix has an in-tree precedent in the same call sequence
callthunks_patch_module_calls() processes .call_sites in the same
module_finalize() path and already validates exactly this class of
address (arch/x86/kernel/callthunks.c):
static __init_or_module void patch_call(void *addr, const struct core_text *ct)
{
...
if (!within_coretext(ct, addr))
return;
with ct built from mod->mem[MOD_TEXT]. The suggested fix mirrors it
for .return_sites and .retpoline_sites - reject any computed site
address outside the module's text before decoding:
struct core_text ct = {
.base = (unsigned long)mod->mem[MOD_TEXT].base,
.end = (unsigned long)mod->mem[MOD_TEXT].base + mod->mem[MOD_TEXT].size,
};
for (s = start; s < end; s++) {
void *addr = (void *)s + *s;
+ if (!within_coretext(&ct, addr))
+ continue;
This is the third report in this series of unvalidated module
section contents (relocation r_offset <mod-r-offset-oob-20261113@xxxxxx>,
SRCU pointer section <srcu-struct-ptrs-wildwrite-20261114@xxxxxx>);
callthunks shows the loader already knows how to bound this class of
address - apply_returns/apply_retpolines are the paths it was never
applied to.
## Reproducer
Carrier is a real xt_mark.ko built from the same tree as the test
kernel. A single entry of .rela.return_sites has its r_addend changed
by 0x80000000; nothing else in the file differs. Same boot:
1. load original xt_mark.ko -> init_module returns 0
2. unload -> 0
3. load the modified module -> the Oops above
The synthesizer (with sanity checks on sh_link/sh_info/entsize/
relocation type/target-zero), the A/B pair and the serial log are
available on request.
Tested on 7.3.0; arch/x86/kernel/alternative.c and module.c are
unchanged in mainline as of 2026-11-14 (both fetched and compared
byte-for-byte).
Thanks,
Guikun Yang