[RFC PATCH 08/27] x86/retpoline: Handle retpolies and thunks with kernel shadow stack
From: K Prateek Nayak
Date: Sun Nov 29 2026 - 17:32:44 EST
Retpolines need to correct the return address on the shadow stack to
match that of the indirect call target such that the return doesn't lead
to a #CP. Direct jump to an out of line trampoline that handles shadow
stack and does the final RET which allows keeping the retpoline thunk
size below 32-bytes.
SRSO adds a CALL, LEA, RET pattern which needs to pop an intermediate
shadow stack slot since the correct return address exists right below.
The handle_interrupted_saferet() handling with shadow stack is hard and
there are no processors that need Safe-RET sequence to mitigate SRSO and
support FRED (touch wood!).
Make any CPUs that toggle Safe-RET with X86_FEATURE_KERN_SHSTK call an
alternalte Safe-RET thunk that handle_interrupted_saferet() is unaware
of. If FRED enabled systems ever need Safe-RET (hopefully never),
handle_interrupted_saferet() has to spot if the control is past
incsspq or not (perhaps using another global symbol):
o If control is past incsspq, emulate a POP + RET like normal.
o If incsspq has not yet executed, use the FRED shadow stack frame's SSP
to redirect the SSP at return to one slot above (similar to how
int3_emulate_ret() does) in addition to handling the rest of the LEA +
RET sequence.
Call depth thunk only needs to pop 16 entries when the RSB is stuffed
which is also copied for the RSB_FILL_LOOP.
Retpolines cannot clobber registers and need to handle it on stack :-(
Saferet sequences are longer :-(
Survives a spectre_v2=retpolines,generic :-)
Signed-off-by: K Prateek Nayak <kprateek.nayak@xxxxxxx>
---
arch/x86/Kconfig | 12 ----
arch/x86/include/asm/alternative.h | 3 +-
arch/x86/include/asm/nospec-branch.h | 27 +++++++-
arch/x86/lib/retpoline.S | 92 +++++++++++++++++++++++++++-
4 files changed, 116 insertions(+), 18 deletions(-)
diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
index a5bad1fd3010..559b9bc95c38 100644
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -1907,18 +1907,6 @@ config X86_KERN_SHADOW_STACK
depends on VMAP_STACK
depends on AS_WRUSS
select X86_CET
- # The retpoline sequence is "call; mov %reg,(%rsp); ret": the
- # CALL pushes a shadow stack entry and the RET then uses a
- # rewritten return address. Mismatch every time.
- depends on !MITIGATION_RETPOLINE
- # The indirect call thunks are retpoline-based (see above) and
- # call_depth_return_thunk() refills the RSB with 16 CALLs, then
- # does "add $128,%rsp; ret", leaving 16 stale shadow stack entries.
- depends on !MITIGATION_CALL_DEPTH_TRACKING
- # Safe-RET is "call srso_safe_ret" followed by
- # "lea 8(%rsp),%rsp; ret": the RET consumes the caller's return
- # address while the shadow stack top holds the CALL's.
- depends on !MITIGATION_SRSO
# Replaces return addresses on the stack with return_to_handler,
# and return_to_handler itself returns with a retpoline-style
# "call; mov %rdi,(%rsp); ret".
diff --git a/arch/x86/include/asm/alternative.h b/arch/x86/include/asm/alternative.h
index 08af86ef090a..83ee5527ed4a 100644
--- a/arch/x86/include/asm/alternative.h
+++ b/arch/x86/include/asm/alternative.h
@@ -329,7 +329,8 @@ void nop_func(void);
.endm
.macro ALTERNATIVE_3 oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2, newinstr3, ft_flags3
- __ALTERNATIVE(ALTERNATIVE_2(\oldinstr, \newinstr1, \ft_flags1, \newinstr2, \ft_flags2),
+ __ALTERNATIVE(__ALTERNATIVE(__ALTERNATIVE(\oldinstr, \newinstr1, \ft_flags1),
+ \newinstr2, \ft_flags2),
\newinstr3, \ft_flags3)
.endm
diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/nospec-branch.h
index 1abeb9af000b..0872dceb3819 100644
--- a/arch/x86/include/asm/nospec-branch.h
+++ b/arch/x86/include/asm/nospec-branch.h
@@ -121,6 +121,7 @@
*/
#define RETPOLINE_THUNK_SIZE 32
+
#define RSB_CLEAR_LOOPS 32 /* To forcibly overwrite all entries */
/*
@@ -132,6 +133,24 @@
int3; \
772:
+#ifdef CONFIG_X86_KERN_SHADOW_STACK
+
+/* INCSSP #UDs when the supervisor shadow stack is not enabled. */
+#define __FIXUP_SHSTK_SLOTS(reg, nr) \
+ xor reg, reg; \
+ rdsspq reg; \
+ test reg, reg; \
+ jz 773f; \
+ mov $nr, reg; \
+ incsspq reg; \
+773:
+
+#else /* !CONFIG_X86_KERN_SHADOW_STACK */
+
+#define __FIXUP_SHSTK_SLOTS(reg, nr)
+
+#endif /* CONFIG_X86_KERN_SHADOW_STACK */
+
/*
* Stuff the entire RSB.
*
@@ -149,6 +168,7 @@
dec reg; \
jnz 771b; \
/* barrier for jnz misprediction */ \
+ __FIXUP_SHSTK_SLOTS(reg, nr); \
lfence; \
CREDIT_CALL_DEPTH \
CALL_THUNKS_DEBUG_INC_CTXSW
@@ -173,10 +193,11 @@
* On PBRSB-vulnerable CPUs, it is not safe for a RET to be executed
* before this point.
*/
-#define __FILL_ONE_RETURN \
+#define __FILL_ONE_RETURN(reg) \
__FILL_RETURN_SLOT \
add $(BITS_PER_LONG/8), %_ASM_SP; \
- lfence;
+ lfence; \
+ __FIXUP_SHSTK_SLOTS(reg, 1);
/*
* Helper for detecting if an interrupt occurred at an unsafe location within
@@ -289,7 +310,7 @@
.macro FILL_RETURN_BUFFER reg:req nr:req ftr:req ftr2=ALT_NOT(X86_FEATURE_ALWAYS)
ALTERNATIVE_2 "jmp .Lskip_rsb_\@", \
__stringify(__FILL_RETURN_BUFFER(\reg,\nr)), \ftr, \
- __stringify(nop;nop;__FILL_ONE_RETURN), \ftr2
+ __stringify(nop;nop;__FILL_ONE_RETURN(\reg)), \ftr2
.Lskip_rsb_\@:
.endm
diff --git a/arch/x86/lib/retpoline.S b/arch/x86/lib/retpoline.S
index f9ca1d8eabca..d4a8c2b434a1 100644
--- a/arch/x86/lib/retpoline.S
+++ b/arch/x86/lib/retpoline.S
@@ -29,6 +29,37 @@
RET
.endm
+#ifdef CONFIG_X86_KERN_SHADOW_STACK
+
+SYM_CODE_START(__x86_shstk_poline_ret)
+ UNWIND_HINT_FUNC
+ ANNOTATE_NOENDBR
+ push %rax
+ xorl %eax, %eax
+ rdsspq %rax
+ testq %rax, %rax
+ jz 1f
+ push %rcx
+ movq 16(%rsp), %rcx
+ wrssq %rcx, (%rax)
+ pop %rcx
+1: pop %rax
+ RET
+SYM_CODE_END(__x86_shstk_poline_ret)
+
+.macro RETPOLINE_SHSTK reg
+ POLINE \reg
+ jmp __x86_shstk_poline_ret
+.endm
+
+#else /* CONFIG_X86_KERN_SHADOW_STACK */
+
+.macro RETPOLINE_SHSTK reg
+ RETPOLINE \reg
+.endm
+
+#endif
+
.macro THUNK reg
.align RETPOLINE_THUNK_SIZE
@@ -36,9 +67,11 @@ SYM_INNER_LABEL(__x86_indirect_thunk_\reg, SYM_L_GLOBAL)
UNWIND_HINT_UNDEFINED
ANNOTATE_NOENDBR
- ALTERNATIVE_2 __stringify(RETPOLINE \reg), \
+ ALTERNATIVE_3 __stringify(RETPOLINE \reg), \
__stringify(lfence; ANNOTATE_RETPOLINE_SAFE; jmp *%\reg; int3), X86_FEATURE_RETPOLINE_LFENCE, \
+ __stringify(RETPOLINE_SHSTK \reg), X86_FEATURE_KERN_SHSTK, \
__stringify(ANNOTATE_RETPOLINE_SAFE; jmp *%\reg), ALT_NOT(X86_FEATURE_RETPOLINE)
+
SYM_PIC_ALIAS(__x86_indirect_thunk_\reg)
.endm
@@ -228,10 +261,50 @@ SYM_CODE_START_NOALIGN(srso_alias_safe_ret)
int3
SYM_FUNC_END(srso_alias_safe_ret)
+/*
+ * Safe-RET sequence system with X86_FEATURE_KERN_SHSTK.
+ *
+ * This does not need to adhere to constraints of
+ * handle_interrupted_saferet() since none of the FRED enabled systems
+ * need Safe-RET mitigation. It only exists as a precaution for systems
+ * that enable kernel shadow stack and somehow lands here.
+ *
+ * If this ever needs a handle_interrupted_saferet() like handling,
+ * check if incsspq has been handled or not, and if it hasn't been
+ * handled, use the FRED event frame to pop the shadow stack by
+ * redirecting the SSP on top of the frame and continue as usual.
+ */
+SYM_CODE_START_NOALIGN(srso_alias_safe_ret_shstk)
+#ifdef CONFIG_X86_KERN_SHADOW_STACK
+ movq %rcx, (%_ASM_SP)
+ movq $0, %rcx
+ rdsspq %rcx
+ jrcxz 1f
+ movl $1, %ecx
+ incsspq %rcx
+1:
+ movq (%_ASM_SP), %rcx
+#endif
+ lea 8(%_ASM_SP), %_ASM_SP
+ UNWIND_HINT_FUNC
+ ANNOTATE_UNRET_SAFE
+ ret
+ /* End of Safe-RET sequence */
+ int3
+SYM_FUNC_END(srso_alias_safe_ret_shstk)
+
SYM_CODE_START_NOALIGN(srso_alias_return_thunk)
UNWIND_HINT_FUNC
ANNOTATE_NOENDBR
- call srso_alias_safe_ret
+
+ /*
+ * Switch to kernel shadow stack handling before
+ * it is toggled on.
+ */
+ ALTERNATIVE "call srso_alias_safe_ret_shstk", \
+ "call srso_alias_safe_ret", \
+ ALT_NOT(X86_FEATURE_KERN_SHSTK)
+
ud2
SYM_CODE_END(srso_alias_return_thunk)
.popsection
@@ -394,6 +467,20 @@ __EXPORT_THUNK(entry_untrain_ret)
#ifdef CONFIG_MITIGATION_CALL_DEPTH_TRACKING
+.macro FIXUP_CALL_DEPTH_SHSTK
+#ifdef CONFIG_X86_KERN_SHADOW_STACK
+ push %rax
+ xor %eax, %eax
+ rdsspq %rax
+ test %rax, %rax
+ jz .Lshstk_skip\@
+ movl $16, %eax
+ incsspq %rax
+.Lshstk_skip\@:
+ pop %rax
+#endif
+.endm
+
.align 64
SYM_FUNC_START(call_depth_return_thunk)
ANNOTATE_NOENDBR
@@ -417,6 +504,7 @@ SYM_FUNC_START(call_depth_return_thunk)
.endr
add $(8*16), %rsp
+ FIXUP_CALL_DEPTH_SHSTK
CREDIT_CALL_DEPTH
ANNOTATE_UNRET_SAFE
--
2.43.0