mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 1/1] riscv: usercfi: bind signal shadow-stack restore to the active shadow stack
@ 2026-11-22 18:17 黄华成
  0 siblings, 0 replies; only message in thread
From: 黄华成 @ 2026-11-22 18:17 UTC (permalink / raw)
  To: linux-riscv; +Cc: palmer, pjw, aou, alex, debug, linux-kernel, stable

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(+)

diff --git a/arch/riscv/kernel/usercfi.c b/arch/riscv/kernel/usercfi.c
--- a/arch/riscv/kernel/usercfi.c
+++ b/arch/riscv/kernel/usercfi.c
@@ -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!

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-22 18:19 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-22 18:17 [PATCH 1/1] riscv: usercfi: bind signal shadow-stack restore to the active shadow stack 黄华成

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®