[RFC PATCH 06/27] x86/process: Initialize and enable kernel shadow stack at boot
From: K Prateek Nayak
Date: Sun Nov 29 2026 - 17:32:10 EST
Current scheme of dynamically toggling shadow stacks at context-switch
is heavy handed and unnecessary if all tasks start off with kernel
shadow stack initialized before the CPU can context-switch.
Initialize forked task with a restore token and enable shadow stacks
early into the boot after the CPU has been identified and feature flags
are patched.
This gets us the simple pattern for SSP handling at context-switch of:
rstorssp -8(%rax)
saveprevssp
The init_task has its shadow stack data mapped into the kernel image
and needs the CPA helper to change it to RO mapping unlike AP which goes
via copy_process() and has its shadow stack area vmalloced.
The first context switch is only possible after BSP has patched
alternatives and it is safe to patch out the shadow stack handling in
the switch path.
Signed-off-by: K Prateek Nayak <kprateek.nayak@xxxxxxx>
---
arch/x86/entry/entry_64.S | 88 ++++-------------------------------
arch/x86/include/asm/cpu.h | 62 ++++++++++++++++++++++++
arch/x86/kernel/cpu/common.c | 2 +
arch/x86/kernel/process.c | 54 ++++++++++++++++-----
arch/x86/kernel/smpboot.c | 2 +
arch/x86/kernel/vmlinux.lds.S | 8 ++++
arch/x86/mm/pat/set_memory.c | 17 +++++++
7 files changed, 141 insertions(+), 92 deletions(-)
diff --git a/arch/x86/entry/entry_64.S b/arch/x86/entry/entry_64.S
index 6d5c09cb9680..c255722cd66d 100644
--- a/arch/x86/entry/entry_64.S
+++ b/arch/x86/entry/entry_64.S
@@ -170,83 +170,14 @@ SYM_INNER_LABEL(entry_SYSRETQ_end, SYM_L_GLOBAL)
int3
SYM_CODE_END(entry_SYSCALL_64)
-.macro SAVE_KERNEL_SHADOW_STACK
+.macro SWITCH_KERNEL_SHADOW_STACK
#ifdef CONFIG_X86_KERN_SHADOW_STACK
- /* kshstk_ssp==0 means no kernel shadow stack, skip handling */
- cmpq $0, TASK_thread_kshstk_ssp(%rdi)
- je .Lprev_no_shstk\@
- rdsspq %rax
- movq %rax, TASK_thread_kshstk_ssp(%rdi)
-.Lprev_no_shstk\@:
-#endif
-.endm
-
-.macro ENABLE_KERNEL_SHADOW_STACK
- movl $MSR_IA32_S_CET, %ecx
- rdmsr
- orl $(CET_SHSTK_EN | CET_WRSS_EN), %eax
- wrmsr
-.endm
-
-.macro DISABLE_KERNEL_SHADOW_STACK
- movl $MSR_IA32_S_CET, %ecx
- rdmsr
- andl $~(CET_SHSTK_EN | CET_WRSS_EN), %eax
- wrmsr
-.endm
-
-/*
- * Clobbers rax, rcx, rdx, r8, r9. rdi/rsi must survive for the rest
- * of __switch_to_asm.
- *
- * Dumb and slow. Always does WRMSR, regardless of if changes need to
- * be made.
- */
-.macro LOAD_KERNEL_SHADOW_STACK
-#ifdef CONFIG_X86_KERN_SHADOW_STACK
- movq TASK_thread_kshstk_ssp(%rsi), %r8
- testq %r8, %r8
- jz .Lnext_no_shstk\@
-
- ENABLE_KERNEL_SHADOW_STACK
-
- /*
- * This should be just a:
- *
- * rstorssp
- * saveprevssp
- *
- * sequence. But, currently, the previous task might not
- * have a valid SSP, like the init_task. This needs fixing.
- * But, for now, just hack a Prev-SSP token in.
- *
- * The right fix is to give all tasks a valid SSP universally
- * and then initialize the token once at task creation time,
- * probably near the spot that ret_from_fork_asm is also
- * written to the shadow stack.
- */
- leaq -8(%r8), %r9
- movq %r8, %rax
- /*
- * Set type=0. Indicates a "Restore token" to satisfy RSTORSSP:
- * Set mode=1. Indicates 64-bit mode.
- * */
- orq $SHSTK_TOKEN_MODE64, %rax
- wrssq %rax, (%r9)
-
- rstorssp (%r9)
-
- /*
- * Discard the Previous-SSP token left by RSTORSSP.
- *
- * This will go away once SAVEPREVSSP is being used properly.
- */
- movl $1, %eax
- incsspq %rax
- jmp .Lshstk_done\@
-
-.Lnext_no_shstk\@:
- DISABLE_KERNEL_SHADOW_STACK
+ ALTERNATIVE "jmp .Lshstk_done\@", "", X86_FEATURE_KERN_SHSTK
+ rdsspq %rbx
+ movq %rbx, TASK_thread_kshstk_ssp(%rdi)
+ movq TASK_thread_kshstk_ssp(%rsi), %rbx
+ rstorssp -8(%rbx)
+ saveprevssp
.Lshstk_done\@:
#endif
.endm
@@ -273,7 +204,7 @@ SYM_FUNC_START(__switch_to_asm)
movq %rsp, TASK_threadsp(%rdi)
movq TASK_threadsp(%rsi), %rsp
- SAVE_KERNEL_SHADOW_STACK
+ SWITCH_KERNEL_SHADOW_STACK
#ifdef CONFIG_STACKPROTECTOR
movq TASK_stack_canary(%rsi), %rbx
@@ -289,9 +220,6 @@ SYM_FUNC_START(__switch_to_asm)
*/
FILL_RETURN_BUFFER %r12, RSB_CLEAR_LOOPS, X86_FEATURE_RSB_CTXSW
- /* Note that this clobbers tons of stuff: */
- LOAD_KERNEL_SHADOW_STACK
-
/* restore callee-saved registers */
popq %r15
popq %r14
diff --git a/arch/x86/include/asm/cpu.h b/arch/x86/include/asm/cpu.h
index 01313070b2da..5f2e5b150b61 100644
--- a/arch/x86/include/asm/cpu.h
+++ b/arch/x86/include/asm/cpu.h
@@ -9,6 +9,9 @@
#include <linux/percpu.h>
#include <asm/ibt.h>
+#include <asm/msr-index.h>
+#include <asm/thread_info.h>
+#include <asm/special_insns.h>
#include <asm/cpuid/leaf_types.h>
#ifndef CONFIG_SMP
@@ -62,6 +65,65 @@ static inline void init_ia32_feat_ctl(struct cpuinfo_x86 *c) {}
extern __noendbr void cet_disable(void);
+#ifdef CONFIG_X86_KERN_SHADOW_STACK
+
+extern int set_init_task_shstk_attr(struct task_struct *tsk);
+extern void prep_kernel_shadow_stack(struct task_struct *tsk,
+ unsigned long ret_addr,
+ bool base_configured);
+
+/*
+ * This function should always be inlined. cpu_setup_kernel_shstk()
+ * soes not unwind the full stack and the shadow stack is only capable
+ * of handling a single unbalanced ret to _RET_IP_ of setup.
+ */
+static __always_inline void enable_kernel_shstk(unsigned long shstk_top)
+{
+ asm volatile ("rdmsr\n"
+ "orl %1, %%eax\n"
+ "wrmsr\n"
+ "rstorssp -8(%2)\n"
+ "incsspq %3\n"
+ :
+ : "c" (MSR_IA32_S_CET),
+ "i" ((u32)(CET_SHSTK_EN | CET_WRSS_EN)),
+ "r" (shstk_top),
+ "r" (1ULL)
+ : "eax", "edx", "cc", "memory");
+}
+
+/*
+ * Setup the shadow stack for this CPU.
+ *
+ * Note: The shadow stack is setup in a way that it can handle at
+ * most one retun to _RET_IP_. The expectations is that the caller
+ * returns to an noreturn function that sets up the idle task.
+ */
+static __always_inline void cpu_setup_kernel_shstk(bool boot_cpu)
+{
+ struct task_struct *tsk = current;
+ unsigned long shstk_top;
+
+ if (!cpu_feature_enabled(X86_FEATURE_KERN_SHSTK))
+ return;
+
+ prep_kernel_shadow_stack(tsk, _RET_IP_, !boot_cpu);
+ if (boot_cpu)
+ BUG_ON(set_init_task_shstk_attr(tsk));
+
+ /* All taks must have a valid shadow stack. */
+ BUG_ON(!tsk->thread.kshstk_ssp);
+ shstk_top = (unsigned long)tsk->thread.kshstk_ssp;
+
+ wrmsrq(MSR_IA32_FRED_SSP0, shstk_top);
+ enable_kernel_shstk(shstk_top);
+}
+
+#else /* !CONFIG_X86_KERN_SHADOW_STACK */
+
+static inline void cpu_setup_kernel_shstk(bool boot_cpu) { }
+
+#endif /* CONFIG_X86_KERN_SHADOW_STACK */
struct cpu_signature;
void intel_collect_cpu_info(struct cpu_signature *sig);
diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
index b8b9364754e7..370a4301c55d 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -2648,6 +2648,8 @@ void __init arch_cpu_finalize_init(void)
alternative_instructions();
+ cpu_setup_kernel_shstk(true);
+
if (IS_ENABLED(CONFIG_X86_64)) {
USER_PTR_MAX = TASK_SIZE_MAX;
diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c
index b5dd664c78c9..7b3a9f112451 100644
--- a/arch/x86/kernel/process.c
+++ b/arch/x86/kernel/process.c
@@ -167,13 +167,16 @@ __visible void ret_from_fork(struct task_struct *prev, struct pt_regs *regs,
syscall_exit_to_user_mode(regs);
}
+#ifdef CONFIG_X86_KERN_SHADOW_STACK
+
/*
* The goal here is to build a shadow stack for the new thread. It
* just needs to survive the first RET from ret_from_fork_asm().
*/
-static void prep_kernel_shadow_stack(struct task_struct *tsk)
+void prep_kernel_shadow_stack(struct task_struct *tsk,
+ unsigned long ret_addr,
+ bool base_configured)
{
-#ifdef CONFIG_X86_KERN_SHADOW_STACK
unsigned long *ss_top_slot;
u8 *shstk_base_writable;
u8 *shstk_base_vmap;
@@ -183,24 +186,49 @@ static void prep_kernel_shadow_stack(struct task_struct *tsk)
return;
/* Start with the shadow stack permission mapping: */
- shstk_base_vmap = &task_thread_union(tsk)->stacks.shadow_stack[0];
+ shstk_base_vmap = task_thread_union(tsk)->stacks.shadow_stack;
- /* Walk the page tables and find the direct map alias of the sshstk */
- shstk_base_writable = page_address(vmalloc_to_page(shstk_base_vmap));
+ /*
+ * Walk the page tables and find the direct map alias of the sshstk
+ * if permissions are already configured. Otherwise, use the
+ * base vmap as is since it is writable as is the case of init_task.
+ */
+ shstk_base_writable = (base_configured) ?
+ page_address(vmalloc_to_page(shstk_base_vmap)) :
+ shstk_base_vmap;
/* Point at the end of the shadow stack: */
ss_top = shstk_base_writable + TASK_SHADOW_STACK_SIZE;
/* Back up one ulong from the end to write the first return pointer: */
ss_top_slot = (unsigned long *)ss_top - 1;
- /* Write the return address for the first RET: */
- *ss_top_slot = (unsigned long)ret_from_fork_asm;
+ /*
+ * Write the return address for the first RET: and point the task's
+ * kshstk_ssp to the top slot.
+ */
+ *ss_top_slot = (unsigned long)ret_addr;
+ tsk->thread.kshstk_ssp = task_shstk_top(tsk) - sizeof(unsigned long);
- // Needs helper?
- tsk->thread.kshstk_ssp = shstk_base_vmap + TASK_SHADOW_STACK_SIZE - sizeof(unsigned long);
-#endif
+ /*
+ * Back up one more frame and write the token for rstorssp when this
+ * shadow stack switches in. this token will be consumed when this
+ * shadow stack is toggled busy.
+ */
+ ss_top_slot -= 1;
+ *ss_top_slot = (unsigned long)tsk->thread.kshstk_ssp | SHSTK_TOKEN_MODE64;
+}
+
+#else /* !CONFIG_X86_KERN_SHADOW_STACK */
+
+static void prep_kernel_shadow_stack(struct task_struct *tsk,
+ unsigned long ret_addr,
+ bool base_configured)
+{
}
+#endif /* CONFIG_X86_KERN_SHADOW_STACK */
+
+
int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
{
u64 clone_flags = args->flags;
@@ -209,6 +237,7 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
struct inactive_task_frame *frame;
struct fork_frame *fork_frame;
struct pt_regs *childregs;
+ unsigned long ret_addr;
unsigned long new_ssp;
int ret = 0;
@@ -216,10 +245,11 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
fork_frame = container_of(childregs, struct fork_frame, regs);
frame = &fork_frame->frame;
+ ret_addr = (unsigned long) ret_from_fork_asm;
frame->bp = encode_frame_pointer(childregs);
- frame->ret_addr = (unsigned long) ret_from_fork_asm;
+ frame->ret_addr = ret_addr;
p->thread.sp = (unsigned long) fork_frame;
- prep_kernel_shadow_stack(p);
+ prep_kernel_shadow_stack(p, ret_addr, true);
p->thread.io_bitmap = NULL;
clear_tsk_thread_flag(p, TIF_IO_BITMAP);
p->thread.iopl_warn = 0;
diff --git a/arch/x86/kernel/smpboot.c b/arch/x86/kernel/smpboot.c
index ba01a9e919b7..6ff2730a39c0 100644
--- a/arch/x86/kernel/smpboot.c
+++ b/arch/x86/kernel/smpboot.c
@@ -189,6 +189,8 @@ static void ap_starting(void)
/* Save the processor parameters. */
identify_secondary_cpu(cpuid);
+ cpu_setup_kernel_shstk(false);
+
/*
* The topology information must be up to date before
* notify_cpu_starting().
diff --git a/arch/x86/kernel/vmlinux.lds.S b/arch/x86/kernel/vmlinux.lds.S
index 2438b89a4620..3aff2d25ba98 100644
--- a/arch/x86/kernel/vmlinux.lds.S
+++ b/arch/x86/kernel/vmlinux.lds.S
@@ -185,6 +185,14 @@ SECTIONS
/* init_task */
INIT_TASK_DATA(THREAD_SIZE)
+#ifdef CONFIG_X86_KERN_SHADOW_STACK
+ /*
+ * The rest of init_thread_union: init_task's shadow stack,
+ * i.e. thread_union.stacks.shadow_stack[].
+ */
+ . += TASK_SHADOW_STACK_SIZE;
+#endif
+
/* equivalent to task_pt_regs(&init_task) */
__top_init_kernel_stack = __end_init_stack - TOP_OF_KERNEL_STACK_PADDING - PTREGS_SIZE;
diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c
index 4652487b5572..7258514ec414 100644
--- a/arch/x86/mm/pat/set_memory.c
+++ b/arch/x86/mm/pat/set_memory.c
@@ -24,6 +24,7 @@
#include <linux/memregion.h>
#include <linux/cleanup.h>
+#include <asm/cpu.h>
#include <asm/e820/api.h>
#include <asm/processor.h>
#include <asm/tlbflush.h>
@@ -2823,6 +2824,22 @@ int __init kernel_unmap_pages_in_pgd(pgd_t *pgd, unsigned long address,
return retval;
}
+#ifdef CONFIG_X86_KERN_SHADOW_STACK
+
+int set_init_task_shstk_attr(struct task_struct *tsk)
+{
+ int numpages = TASK_SHADOW_STACK_SIZE >> PAGE_SHIFT;
+ unsigned long addr;
+
+ addr = (unsigned long)task_thread_union(tsk)->stacks.shadow_stack;
+ return change_page_attr_set_clr(&addr, numpages,
+ __pgprot(_PAGE_DIRTY),
+ __pgprot(_PAGE_RW),
+ 0, 0, NULL);
+}
+
+#endif
+
/*
* The testcases use internal knowledge of the implementation that shouldn't
* be exposed to the rest of the kernel. Include these directly here.
--
2.43.0