Skip to contentSkip to conversation

riscv: usercfi: bind signal shadow-stack restore to the active shadow stack

PATCH New 黄华成 1 participant 1 message Nov 22

Subsystems (2)
lore
Share & tools
lore

黄华成

author
#0 lore raw

PATCH 1/1 riscv: usercfi: bind signal shadow-stack restore to the active shadow stack

hello,
Write it's for usercfi,
restore_user_shstk() reads the shadow-stack pointer from the user signal frame (sc_extdesc.ss_ptr) and validates the token found there only by the value relation token == shstk_ptr + SHSTK_ENTRY_SIZE. Nothing binds ss_ptr to the shadow-stack position the kernel recorded when the signal was delivered.

A corrupted sigframe can therefore redirect the restore to a stale but still-live token belonging to another signal context (in-bounds and genuinely valid, so the df333b72c0e1 bounds check passes); the kernel accepts it, swaps it out, and rewinds ssp to that token's position. The result is a kernel-mediated shadow-stack rewind to an attacker-chosen depth, after which sspopchk() accepts return addresses that are only valid at that historical depth - a backward-CFI bypass reachable from an ordinary memory-corruption primitive. x86 CET does not have this shape:its sigframe contains no shadow-stack pointer at all.

Bind the restore pointer to get_active_shstk(tsk), the position save_user_shstk() recorded at delivery, and fail with -EINVAL on mismatch. Valid returns are unaffected because on the happy path the frame's ss_ptr equals the active position by construction.

Verified on a fully hardened instrumented target (PIE/ASLR/canary/RELRO/NX, clang Zicfiss): the stale-token replay chain succeeds on the unpatched kernel (67/67 among completed recon rounds) and is blocked on he patched one (52/52), while normal and nested signals, sigaltstack, siglongjmp followed by a later signal, setcontext and swapcontext all behave identically on both kernels, as do the official signal kselftests.

Fixes 66c9c713de59 ("riscv/signal: save and restore the shadow stack on a signal")
---
 arch/riscv/kernel/usercfi.c | 9 +++++++++
 1 file changed, 9 insertions(+)
arch/riscv/kernel/usercfi.c +12 -0
@@ -207,6 +207,15 @@ int restore_user_shstk(struct task_struct *tsk, unsigned long shstk_ptr)
 {
 	unsigned long token = 0;
 
+	/*
+	 * The frame controls ss_ptr; bind it to the kernel-recorded active
+	 * shadow stack so a corrupted sigframe cannot redirect the restore
+	 * at a stale but still-live token from another signal context.
+	 */
+	if (shstk_ptr != get_active_shstk(tsk)) {
+		pr_info_ratelimited("%s[%d]: shadow stack ptr mismatch in %s: expected=%px, got=%px\n",
+				    tsk->comm, task_pid_nr(tsk), __func__,
+				    (void *)get_active_shstk(tsk),
+				    (void *)shstk_ptr);
+		return -EINVAL;
+	}

+
token = amo_user_shstk((unsigned long __user *)shstk_ptr, 0);

if (token == -1)
--
2.43.0

Thanks,
Looking forward to your reply!
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv