From: K Prateek Nayak <kprateek.nayak@amd.com>
To: Dave Hansen <dave.hansen@linux.intel.com>,
Thomas Gleixner <tglx@kernel.org>, Ingo Molnar <mingo@redhat.com>,
Borislav Petkov <bp@alien8.de>,
the arch/x86 maintainers <x86@kernel.org>,
<linux-kernel@vger.kernel.org>
Cc: Andy Lutomirski <luto@kernel.org>,
Peter Zijlstra <peterz@infradead.org>,
Rick Edgecombe <rick.p.edgecombe@intel.com>,
Andrew Cooper <andrew.cooper3@citrix.com>,
Kees Cook <kees@kernel.org>,
Pawan Gupta <pawan.kumar.gupta@linux.intel.com>,
K Prateek Nayak <kprateek.nayak@amd.com>
Subject: [RFC PATCH 08/27] x86/retpoline: Handle retpolies and thunks with kernel shadow stack
Date: Sun, 29 Nov 2026 22:29:33 +0000 [thread overview]
Message-ID: <20261129222953.9124-9-kprateek.nayak@amd.com> (raw)
In-Reply-To: <20261129222953.9124-1-kprateek.nayak@amd.com>
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@amd.com>
---
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
next prev parent reply other threads:[~2026-11-29 22:32 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-29 22:29 [RFC PATCH 00/27] x86/shstk: More shadow stack enablement and path to IDT support K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 01/27] x86/entry: Parameterize kernel shadow stack handling at FRED entry K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 02/27] x86/perf_regs: Rename pt_regs_offset array to prevent build failure K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 03/27] x86/cet: Fix build for KERN_SHADOW_STACK and !USER_SHADOW_STACK K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 04/27] x86/cet: Make kernel shadow stack #CP fatal instead of livelocking K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 05/27] x86/sync_core: Don't use IRET-to-self with kernel shadow stacks K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 06/27] x86/process: Initialize and enable kernel shadow stack at boot K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 07/27] x86/entry: Handle int3 text patching with kernel shadow stack K Prateek Nayak
2026-11-29 22:29 ` K Prateek Nayak [this message]
2026-11-29 22:29 ` [RFC PATCH 09/27] x86/ftrace: Prepare ftrace trampolines to handle " K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 10/27] x86/kprobes: Prepare optprobe trampoline to handle shadow stacks K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 11/27] x86/rethook: Prepare the rethook trampoline to handle shadow stack K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 12/27] x86/cet: Make regs_set_return_ip() helpers context aware K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 13/27] x86/cet: Introduce regs_set_return_trampoline() for rethook trampoline K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 14/27] x86/ftrace,livepatch: Use CET helpers for ftrace regs->ip and trampoline redirection K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 15/27] x86/rethook: Make the trampoline insert and return shadow stack aware K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 16/27] x86/error_inject: Use CET helpers to insert just_return_func trampoline K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 17/27] x86/kprobes: Use CET helpers in kprobe core to modify regs->ip K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 18/27] x86/bpf: Make BPF JIT aware of CET handling K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 19/27] x86/shstk: Remove profiling related dependencies for KERN_SHADOW_STACK K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 20/27] x86/shstk: Disable kenrel shadow stack when CPU is offlined K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 21/27] x86/shstsk: Allow suspend-resume and hibernation with kernel shadow stack K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 22/27] x86/shstk: Save and restore shadow stack pointer during kexec K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 23/27] x86/idt: Map MCE shadow stack and populate the INT_SPP table K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 24/27] x86/process: Initialize shadow stack for IDT systems K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 25/27] x86/entry: Prepare IDT entry for shadow stacks K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 26/27] x86/syscall: Toggle shadow stack busy on entry and exit from syscall K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 27/27] x86/entry: Handle shadow stack on nested NMI K Prateek Nayak
2026-11-29 23:00 ` [RFC PATCH 00/27] x86/shstk: More shadow stack enablement and path to IDT support Andrew Cooper
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261129222953.9124-9-kprateek.nayak@amd.com \
--to=kprateek.nayak@amd.com \
--cc=andrew.cooper3@citrix.com \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=kees@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=luto@kernel.org \
--cc=mingo@redhat.com \
--cc=pawan.kumar.gupta@linux.intel.com \
--cc=peterz@infradead.org \
--cc=rick.p.edgecombe@intel.com \
--cc=tglx@kernel.org \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®