[RFC PATCH 26/27] x86/syscall: Toggle shadow stack busy on entry and exit from syscall
From: K Prateek Nayak
Date: Sun Nov 29 2026 - 17:38:01 EST
On IDT systems, a syscall to Ring-0 will set SSP to 0. Intel SDM for
"SYSENTER - Fast System Call" reads:
When shadow stacks are enabled at privilege level where SYSENTER
instruction is invoked, the SSP is saved to the IA32_PL3_SSP MSR.
If shadow stacks are enabled at privilege level 0, the SSP is
loaded with 0.
Before IRQs are enable, execute a setssbsy to load the SSP0 shadow stack
and clear the busy status before SYSRET to allow IST Gate 0 events from
userspace to mark it busy.
Kernel events at level 0 will not inspect the busy bit.
Signed-off-by: K Prateek Nayak <kprateek.nayak@xxxxxxx>
---
arch/x86/entry/calling.h | 14 ++++++++++++++
arch/x86/entry/entry_64.S | 6 ++++++
arch/x86/entry/entry_64_compat.S | 10 ++++++++++
3 files changed, 30 insertions(+)
diff --git a/arch/x86/entry/calling.h b/arch/x86/entry/calling.h
index 402c08bac5b5..2d471ec9d8e8 100644
--- a/arch/x86/entry/calling.h
+++ b/arch/x86/entry/calling.h
@@ -453,6 +453,16 @@ For 32-bit we have the following conventions - kernel is built with
addq $16, %rsp
.endm
+.macro SET_SHSTK_BUSY
+ ALTERNATIVE "", "setssbsy", X86_FEATURE_KERN_SHSTK
+.endm
+
+.macro CLEAR_SHSTK_BUSY scratch_reg
+ ALTERNATIVE "", \
+ __stringify(rdsspq \scratch_reg; clrssbsy (\scratch_reg)), \
+ X86_FEATURE_KERN_SHSTK
+.endm
+
#else /* !CONFIG_X86_KERN_SHADOW_STACK */
.macro PUSH_KERN_SHSTK_FRAME
@@ -463,6 +473,10 @@ For 32-bit we have the following conventions - kernel is built with
.endm
.macro POP_USER_SHSTK_FRAME
.endm
+.macro SET_SHSTK_BUSY
+.endm
+.macro CLEAR_SHSTK_BUSY scratch_reg
+.endm
#endif /* CONFIG_X86_KERN_SHADOW_STACK */
diff --git a/arch/x86/entry/entry_64.S b/arch/x86/entry/entry_64.S
index 6ce8c523b2d6..53ea96470f15 100644
--- a/arch/x86/entry/entry_64.S
+++ b/arch/x86/entry/entry_64.S
@@ -109,6 +109,9 @@ SYM_INNER_LABEL(entry_SYSCALL_64_after_hwframe, SYM_L_GLOBAL)
PUSH_AND_CLEAR_REGS rax=$-ENOSYS
+ /* SYSCALL zeroed SSP; claim the task's supervisor token before any CALL. */
+ SET_SHSTK_BUSY
+
/* IRQs are off. */
movq %rsp, %rdi
/* Sign extend the lower 32bit as syscall numbers are treated as int */
@@ -156,6 +159,9 @@ syscall_return_via_sysret:
*/
STACKLEAK_ERASE_NOCLOBBER
+ /* SYSRET does not free the token; SSP is back on it here. */
+ CLEAR_SHSTK_BUSY scratch_reg=%rdi
+
SWITCH_TO_USER_CR3_STACK scratch_reg=%rdi
popq %rdi
diff --git a/arch/x86/entry/entry_64_compat.S b/arch/x86/entry/entry_64_compat.S
index a45e1125fc6c..59998d70c800 100644
--- a/arch/x86/entry/entry_64_compat.S
+++ b/arch/x86/entry/entry_64_compat.S
@@ -87,6 +87,9 @@ SYM_INNER_LABEL(entry_SYSENTER_compat_after_hwframe, SYM_L_GLOBAL)
PUSH_AND_CLEAR_REGS rax=$-ENOSYS
UNWIND_HINT_REGS
+ /* SYSENTER zeroed SSP; claim the task's supervisor token before any CALL. */
+ SET_SHSTK_BUSY
+
cld
/*
@@ -210,6 +213,9 @@ SYM_INNER_LABEL(entry_SYSCALL_compat_after_hwframe, SYM_L_GLOBAL)
PUSH_AND_CLEAR_REGS rcx=%rbp rax=$-ENOSYS
UNWIND_HINT_REGS
+ /* SYSCALL zeroed SSP; claim the task's supervisor token before any CALL. */
+ SET_SHSTK_BUSY
+
IBRS_ENTER
UNTRAIN_RET
CLEAR_BRANCH_HISTORY
@@ -271,6 +277,10 @@ SYM_INNER_LABEL(entry_SYSRETL_compat_unsafe_stack, SYM_L_GLOBAL)
*
* %r8/%r9 are zeroed before the sysret, thus safe to clobber.
*/
+
+ /* SYSRET does not free the token; SSP is back on it here. */
+ CLEAR_SHSTK_BUSY scratch_reg=%r8
+
SWITCH_TO_USER_CR3_NOSTACK scratch_reg=%r8 scratch_reg2=%r9
xorl %r8d, %r8d
--
2.43.0