[RFC PATCH 09/27] x86/ftrace: Prepare ftrace trampolines to handle shadow stack

From: K Prateek Nayak

Date: Sun Nov 29 2026 - 17:33:06 EST


Kprobes can hook onto function entry via optprobes, int3 kprobes, or via
ftrace_regs_caller and hijack the return. From a shadow stack
perspective, the ftrace_caller's stack looks like:

caller
call traced_function
call ftrace_regs_caller

Rethooks and return_to_handler can hijack the return address from
traced_function; IPMODIFY and direct caller ftrace handlers can steer
the return from traced function.

To be aware of the shadow stack context that needs correction, the CET
helpers need to know the context from where pt_regs came from.

Similar to the FRED frame handling, push the SSP at ftrace_regs_caller
and a context metadata flag onto the stack that can be inspected from
the handler.

Based on the event type, the CET helpers can be made aware if the slot
that needs modifying is at SSP, or just below. return_to_handler() now
does the INTRA_FUNCTION_CALL early which allows using the stack slot
pushed for the pt_regs container. !X86_KERN_SHADOW_STACK build will now
do an extra pop.

Note: The return_to_handler now sets regs->cs to 0 to prevent false
positives of !user_mode_regs() in CET helpers. Since a full pt_regs is
not present, it also blocks the ftrace helper from returning the
incomplete pt_regs pointer.

Signed-off-by: K Prateek Nayak <kprateek.nayak@xxxxxxx>
---
arch/x86/kernel/cet.c | 4 +-
arch/x86/kernel/ftrace_64.S | 121 +++++++++++++++++++++++++++++++-----
2 files changed, 108 insertions(+), 17 deletions(-)

diff --git a/arch/x86/kernel/cet.c b/arch/x86/kernel/cet.c
index bab8ff1c64c5..66c9ce531249 100644
--- a/arch/x86/kernel/cet.c
+++ b/arch/x86/kernel/cet.c
@@ -119,7 +119,9 @@ enum fred_exit_work {
};

enum frame_context {
- FRED_ENTRY = 1
+ FRED_ENTRY = 1,
+ FTRACE_CALLER,
+ RETURN_TRAMPOLINE
};

/*
diff --git a/arch/x86/kernel/ftrace_64.S b/arch/x86/kernel/ftrace_64.S
index 62c1c93aa1c6..a6211070d6e5 100644
--- a/arch/x86/kernel/ftrace_64.S
+++ b/arch/x86/kernel/ftrace_64.S
@@ -131,6 +131,62 @@

.endm

+#ifdef CONFIG_X86_KERN_SHADOW_STACK
+
+/*
+ * The ftrace handler might use regs_set_return_ip() to steer the return
+ * from ftrace_caller which also tries to do shadow stack handling
+ * assuming pt_regs is part of the standared FRED entry frame.
+ *
+ * Avoid any shadow stack manipulations by setting the two slots above
+ * pt_regs to zeros. regs_set_return_ip() et al. will see shadow stack
+ * pointer to be NULL and skip any shadow stack handling.
+ *
+ * Ftrace caller will correct the shadow stack if the handler has
+ * hijacked any of the return or has decided to do a tail call.
+ */
+.macro FTRACE_PUSH_SHSTK_FRAME
+ xor %eax, %eax
+ rdsspq %rax
+ pushq %rax
+ pushq $0x200
+ .endm
+
+.macro FTRACE_POP_SHSTK_FRAME
+ addq $16, %rsp
+ .endm
+
+.macro FTRACE_FIX_DIRECT_CALL_SHSTK
+ xor %ecx, %ecx
+ rdsspq %rcx
+ testq %rcx, %rcx
+ jz .Lshstk_done\@
+ wrssq %rax, (%rcx)
+.Lshstk_done\@:
+ .endm
+
+.macro FTRACE_PUSH_RETURN_HANDLER_SHSTK_FRAME
+ xor %eax, %eax
+ rdsspq %rax
+ movq %rax, (%rsp) /* Pushed by intra function call. */
+ pushq $0x300
+ .endm
+
+#else /* !CONFIG_X86_KERN_SHADOW_STACK */
+
+.macro FTRACE_PUSH_SHSTK_FRAME
+ .endm
+.macro FTRACE_POP_SHSTK_FRAME
+ .endm
+.macro FTRACE_FIX_DIRECT_CALL_SHSTK
+ .endm
+
+.macro FTRACE_PUSH_RETURN_HANDLER_SHSTK_FRAME
+ addq $8, %rsp
+ .endm
+
+#endif /* CONFIG_X86_KERN_SHADOW_STACK */
+
SYM_TYPED_FUNC_START(ftrace_stub)
CALL_DEPTH_ACCOUNT
RET
@@ -177,10 +233,18 @@ SYM_INNER_LABEL(ftrace_caller_op_ptr, SYM_L_GLOBAL)
/* Account for the function call below */
CALL_DEPTH_ACCOUNT

+ /*
+ * Push a blank frame above pt_regs as to not confuse
+ * the shadow stack handlers.
+ */
+ FTRACE_PUSH_SHSTK_FRAME
+
SYM_INNER_LABEL(ftrace_call, SYM_L_GLOBAL)
ANNOTATE_NOENDBR
call ftrace_stub

+ FTRACE_POP_SHSTK_FRAME
+
/* Handlers can change the RIP */
movq RIP(%rsp), %rax
movq %rax, MCOUNT_REG_SIZE(%rsp)
@@ -242,10 +306,15 @@ SYM_INNER_LABEL(ftrace_regs_caller_op_ptr, SYM_L_GLOBAL)
/* Account for the function call below */
CALL_DEPTH_ACCOUNT

+ /* See comment in ftrace_caller above. */
+ FTRACE_PUSH_SHSTK_FRAME
+
SYM_INNER_LABEL(ftrace_regs_call, SYM_L_GLOBAL)
ANNOTATE_NOENDBR
call ftrace_stub

+ FTRACE_POP_SHSTK_FRAME
+
/* Copy flags back to SS, to restore them */
movq EFLAGS(%rsp), %rax
movq %rax, MCOUNT_REG_SIZE(%rsp)
@@ -290,6 +359,9 @@ SYM_INNER_LABEL(ftrace_regs_caller_end, SYM_L_GLOBAL)
jz 2f
andq $0xfffffffffffffffe, %rax
movq %rax, MCOUNT_REG_SIZE+8(%rsp)
+
+ FTRACE_FIX_DIRECT_CALL_SHSTK
+
restore_mcount_regs
/* Restore flags */
popfq
@@ -300,21 +372,26 @@ SYM_INNER_LABEL(ftrace_regs_caller_end, SYM_L_GLOBAL)
movq %rdi, MCOUNT_REG_SIZE-8(%rsp)
movq %rax, MCOUNT_REG_SIZE(%rsp)

- restore_mcount_regs 8
- /* Restore flags */
- popfq
- UNWIND_HINT_FUNC
-
/*
- * The above left an extra return value on the stack; effectively
- * doing a tail-call without using a register. This PUSH;RET
- * pattern unbalances the RSB, inject a pointless CALL to rebalance.
+ * Restore below will leve an extra return value on the stack;
+ * effectively doing a tail-call without using a register.
+ * This PUSH;RET pattern unbalances the RSB, inject a pointless
+ * CALL to rebalance.
*/
ANNOTATE_INTRA_FUNCTION_CALL
CALL .Ldo_rebalance
int3
.Ldo_rebalance:
add $8, %rsp
+
+ /* Populate the return into the frame from above. */
+ FTRACE_FIX_DIRECT_CALL_SHSTK
+
+ restore_mcount_regs 8
+ /* Restore flags */
+ popfq
+ UNWIND_HINT_FUNC
+
ALTERNATIVE __stringify(RET), \
__stringify(ANNOTATE_UNRET_SAFE; ret; int3), \
X86_FEATURE_CALL_DEPTH
@@ -378,26 +455,38 @@ SYM_CODE_START(return_to_handler)
movq %rdx, RDX(%rsp)
movq %rbp, RBP(%rsp)
movq %rdi, RSP(%rsp)
+ movq $0, CS(%rsp)
movq %rsp, %rdi

+ /*
+ * Final RET will jump back to the old return address. Since, the return
+ * frame was hijacked, use a retpoline here to keep the RSB balanced.
+ *
+ * For shadow stack, this adds the frame to stuff the return address into.
+ */
+ ANNOTATE_INTRA_FUNCTION_CALL
+ call .Ldo_rop
+ int3
+.Ldo_rop:
+ FTRACE_PUSH_RETURN_HANDLER_SHSTK_FRAME
+
call ftrace_return_to_handler

+ FTRACE_POP_SHSTK_FRAME
+
movq %rax, %rdi
movq RDX(%rsp), %rdx
movq RAX(%rsp), %rax

- addq $(FRAME_SIZE) + 8, %rsp
+ addq $(FRAME_SIZE), %rsp

/*
- * Jump back to the old return address. This cannot be JMP_NOSPEC rdi
- * since IBT would demand that contain ENDBR, which simply isn't so for
- * return addresses. Use a retpoline here to keep the RSB balanced.
+ * This cannot be JMP_NOSPEC rdi since IBT would demand that contain ENDBR,
+ * which simply isn't so for return addresses. This is the second part of
+ * the repoline sequence; CALL has already been balanced above.
*/
- ANNOTATE_INTRA_FUNCTION_CALL
- call .Ldo_rop
- int3
-.Ldo_rop:
mov %rdi, (%rsp)
+
ALTERNATIVE __stringify(RET), \
__stringify(ANNOTATE_UNRET_SAFE; ret; int3), \
X86_FEATURE_CALL_DEPTH
--
2.43.0