mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Guikun Yang <yangguikun19@qq.com>
To: "Peter Zijlstra (Intel)" <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	Arnaldo Carvalho de Melo <acme@kernel.org>
Cc: Luis Chamberlain <mcgrof@kernel.org>,
	Petr Pavlu <petr.pavlu@suse.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	Borislav Petkov <bp@alien8.de>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	x86@kernel.org, linux-modules@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [Bug report] x86-64 apply_returns() decodes attacker-controlled addresses from unvalidated .return_sites entries - wild read on module load
Date: Sat, 14 Nov 2026 16:00:00 +0800	[thread overview]
Message-ID: <tencent_4AF4D4976F851143A745D39A286A1FC2A807@qq.com> (raw)
In-Reply-To: <srcu-struct-ptrs-wildwrite-20261114@qq.com>

From: Guikun Yang <yangguikun19@qq.com>
To: "Peter Zijlstra (Intel)" <peterz@infradead.org>,
    Ingo Molnar <mingo@redhat.com>,
    Arnaldo Carvalho de Melo <acme@kernel.org>
Cc: Luis Chamberlain <mcgrof@kernel.org>, Petr Pavlu <petr.pavlu@suse.com>,
    Thomas Gleixner <tglx@linutronix.de>, Borislav Petkov <bp@alien8.de>,
    Dave Hansen <dave.hansen@linux.intel.com>, x86@kernel.org,
    linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org
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@qq.com>,
SRCU pointer section <srcu-struct-ptrs-wildwrite-20261114@qq.com>);
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


  parent reply	other threads:[~2026-11-14  3:11 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mod-r-offset-oob-20261113@qq.com>
2026-11-14  2:00 ` [Bug report] srcu_module_coming() dereferences unvalidated ___srcu_struct_ptrs entries - wild kernel-memory write " Guikun Yang
     [not found] ` <srcu-struct-ptrs-wildwrite-20261114@qq.com>
2026-11-14  8:00   ` Guikun Yang [this message]
2026-11-14  6:20     ` [Bug report] x86-64 apply_returns() decodes attacker-controlled addresses from unvalidated .return_sites entries - wild read " Borislav Petkov
     [not found] ` <tencent_0836A27053A7BED946E49CE04D27A31E7D09@qq.com>
2026-11-16 18:11   ` [Bug report] srcu_module_coming() dereferences unvalidated ___srcu_struct_ptrs entries - wild kernel-memory write " Paul E. McKenney
2026-11-17  2:00   ` [PATCH] srcu: Validate ___srcu_struct_ptrs entries against the module range Guikun Yang
2026-11-17 16:24     ` Bradley Morgan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=tencent_4AF4D4976F851143A745D39A286A1FC2A807@qq.com \
    --to=yangguikun19@qq.com \
    --cc=acme@kernel.org \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-modules@vger.kernel.org \
    --cc=mcgrof@kernel.org \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=petr.pavlu@suse.com \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®