Skip to contentSkip to conversation

riscv: mm: Implement arch_within_stack_frames() for HARDENED_USERCOPY

PATCH New Chen Pei 1 participant 1 message Jun 8 RISC-V

lore
Share & tools
lore

Chen Pei

author
#0 lore raw

PATCH v3 1/1 riscv: mm: Implement arch_within_stack_frames() for HARDENED_USERCOPY

Implement arch_within_stack_frames() to enable precise per-frame stack object validation for CONFIG_HARDENED_USERCOPY on RISC-V.

Per the RISC-V ELF psABI Frame Pointer Convention [1], with -fno-omit-frame-pointer (implied by CONFIG_FRAME_POINTER), the RISC-V ABI places the saved frame pointer (fp/s0) and return address (ra) at the top of each frame:

high addr

    +------------------+  <--- fp (s0) -- frame pointer register
    |   saved ra       |      fp - 1*sizeof(void*) (return address)
    |   saved fp       |      fp - 2*sizeof(void*) (caller's frame pointer)
    +------------------+
    |   local variables|
    |   spilled args   |
    +------------------+  <--- sp

low addr

The allowed usercopy region within one frame is
[prev_fp, fp-2*sizeof(void*)), covering local variables but excluding the saved fp/ra slots.

The frame chain is walked from __builtin_frame_address(0), with prev_fp initialized to current_stack_pointer rather than the thread stack base. This ensures objects in already-returned frames are correctly detected as BAD_STACK, since no live frame will cover that region.

[1] https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-cc.adoc#frame-pointer-convention

---

== Testing ==

Build requirements:
CONFIG_HARDENED_USERCOPY=y
CONFIG_FRAME_POINTER=y (already selected by default on riscv)
CONFIG_LKDTM=y
CONFIG_DEBUG_FS=y

Run on QEMU or target board as root:

# Trigger copy_to_user from a stack frame that has already returned:
echo USERCOPY_STACK_FRAME_TO > /sys/kernel/debug/provoke-crash/DIRECT

# Trigger copy_from_user into a stack frame that has already returned:
echo USERCOPY_STACK_FRAME_FROM > /sys/kernel/debug/provoke-crash/DIRECT

# Trigger copy beyond current stack bounds:
echo USERCOPY_STACK_BEYOND > /sys/kernel/debug/provoke-crash/DIRECT

== Kernel test logs ==

  # echo USERCOPY_STACK_FRAME_TO > /sys/kernel/debug/provoke-crash/DIRECT
  lkdtm: Performing direct entry USERCOPY_STACK_FRAME_TO
  lkdtm: good_stack: ff20000000323c98-ff20000000323cb8
  lkdtm: bad_stack : ff20000000323c18-ff20000000323c38
  lkdtm: attempting good copy_to_user of local stack
  lkdtm: attempting bad copy_to_user of distant stack
  usercopy: Kernel memory exposure attempt detected from process stack (offset 38, size 20)!
  ......
  (kernel BUG Call Trace...)
  ......

Changes in v3:
- Put the psABI reference URL on a single line.
- Add one Reviewed-by tags.

Changes in v2:
- Simplify commit messages.
- Drop the redundant #ifdef CONFIG_HAVE_ARCH_WITHIN_STACK_FRAMES.
- Corrected RISC-V stack frame layout description for RV32 compatibility.
- Add a reference link to the RISC-V ELF psABI Frame Pointer Convention.
- Fix potential out-of-bounds read when fp is too close to the stack bottom.

 arch/riscv/Kconfig                   |  1 +
 arch/riscv/include/asm/thread_info.h | 67 ++++++++++++++++++++++++++++
 2 files changed, 68 insertions(+)
arch/riscv/Kconfig +1 -0
index c5754942cf85..ef1a8e89b4a9 100644
@@ -150,6 +150,7 @@ config RISCV
 	select HAVE_ARCH_USERFAULTFD_MINOR if 64BIT && USERFAULTFD
 	select HAVE_ARCH_USERFAULTFD_WP if 64BIT && MMU && USERFAULTFD && RISCV_ISA_SVRSW60T59B
 	select HAVE_ARCH_VMAP_STACK if MMU && 64BIT
+	select HAVE_ARCH_WITHIN_STACK_FRAMES
 	select HAVE_ASM_MODVERSIONS
 	select HAVE_CONTEXT_TRACKING_USER
 	select HAVE_DEBUG_KMEMLEAK
arch/riscv/include/asm/thread_info.h +67 -0

73 lines not on this page · raw message

Signature · 1 line
2.50.1