riscv: mm: Implement arch_within_stack_frames() for HARDENED_USERCOPY
PATCH v2 1/1 riscv: mm: Implement arch_within_stack_frames() for HARDENED_USERCOPY
From: Chen Pei <[email protected]>
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 |
+------------------+ <--- splow 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.
---
== 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 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 | 68 ++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+)
arch/riscv/Kconfig +1 -0
@@ -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 +68 -0
74 lines not on this page · raw message
Signature · 1 line
2.50.1
Hi Chen Pei,
Thanks for the patch.
Quoted 4 lines · On 4/14/26 18:01, [email protected] wrote:
From: Chen Pei <[email protected]>
Implement
arch_within_stack_frames()to enable precise per-frame stack object validation forCONFIG_HARDENED_USERCOPYon RISC-V.
[...]
Quoted 5 lines
arch/riscv/include/asm/thread_info.h +0
-0
arch/riscv/include/asm/thread_info.h struct thread_info { · line 110
Quoted 10 lines
arch/riscv/include/asm/thread_info.h +7
-0@@ -101,6 +101,74 @@ struct thread_info {void arch_release_task_struct (struct task_struct *tsk);int arch_dup_task_struct (struct task_struct *dst,struct task_struct *src); +/* +* RISC-V stack frame layout (with frame pointer enabled). +* +* Reference: RISC-V ELF psABI, Frame Pointer Convention +* https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/ +* riscv-cc.adoc#frame-pointer-convention +*
I don't know what the maintainers would prefer, but I would personally prefer to break the line "limit" here and put the URL in one line, so that it's clickable in editors.
Otherwise:
Thanks,
Vivian "dramforever" Wang