[RFC PATCH 10/27] x86/kprobes: Prepare optprobe trampoline to handle shadow stacks

From: K Prateek Nayak

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


Optimized probes can be executed from a trampoline where, although it
cannot hijack the regs->ip to steer return from the caller, it can still
be used to attach a retprobe trampoline.

The path into the optprobe trampoline looks like:

caller
call traced_function
jmp optprobe_template_entry

Similar to FRED frame, extend a container outside pt_regs to notify this
is an optprobe frame the handler is on using frame metadata.

The return trampoline will be populated at the top of SSP such that
traced function's return jumps to the return trampoline and the
trampoline can direct the return from within in case there are multiple
trampolines chained on the same function.

Signed-off-by: K Prateek Nayak <kprateek.nayak@xxxxxxx>
---
arch/x86/kernel/cet.c | 3 ++-
arch/x86/kernel/ftrace_64.S | 2 +-
arch/x86/kernel/kprobes/opt.c | 26 ++++++++++++++++++++++++++
3 files changed, 29 insertions(+), 2 deletions(-)

diff --git a/arch/x86/kernel/cet.c b/arch/x86/kernel/cet.c
index 66c9ce531249..b037508215c9 100644
--- a/arch/x86/kernel/cet.c
+++ b/arch/x86/kernel/cet.c
@@ -121,7 +121,8 @@ enum fred_exit_work {
enum frame_context {
FRED_ENTRY = 1,
FTRACE_CALLER,
- RETURN_TRAMPOLINE
+ RETURN_TRAMPOLINE,
+ OPTPORBE_TRAMPOLINE
};

/*
diff --git a/arch/x86/kernel/ftrace_64.S b/arch/x86/kernel/ftrace_64.S
index a6211070d6e5..9060c16d34b3 100644
--- a/arch/x86/kernel/ftrace_64.S
+++ b/arch/x86/kernel/ftrace_64.S
@@ -459,7 +459,7 @@ SYM_CODE_START(return_to_handler)
movq %rsp, %rdi

/*
- * Final RET will jump back to the old return address. Since, the return
+ * 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.
diff --git a/arch/x86/kernel/kprobes/opt.c b/arch/x86/kernel/kprobes/opt.c
index 3f8fea52619f..5694cf566022 100644
--- a/arch/x86/kernel/kprobes/opt.c
+++ b/arch/x86/kernel/kprobes/opt.c
@@ -34,6 +34,30 @@

#include "common.h"

+#ifdef CONFIG_X86_KERN_SHADOW_STACK
+
+/*
+ * Push a NULL on the stack to such that CET helpers will
+ * see Shadow stack pointer as NULL and skip shadow stack
+ * corrects internally.
+ */
+#define OPTPROBE_PUSH_SHSTK_FRAME \
+ " xor %eax, %eax\n" \
+ " rdsspq %rax\n" \
+ " pushq %rax\n" \
+ " pushq $0x400\n"
+
+/* RAX is dead and will be restored after. */
+#define OPTPROBE_POP_SHSTK_FRAME \
+ " addq $16, %rsp\n"
+
+#else /* !CONFIG_X86_KERN_SHADOW_STACK */
+
+#define OPTPROBE_PUSH_SHSTK_FRAME
+#define OPTPROBE_POP_SHSTK_FRAME
+
+#endif /* CONFIG_X86_KERN_SHADOW_STACK */
+
unsigned long __recover_optprobed_insn(kprobe_opcode_t *buf, unsigned long addr)
{
struct optimized_kprobe *op;
@@ -115,6 +139,7 @@ asm (
ASM_NOP3
SAVE_REGS_STRING
" movq %rsp, %rsi\n"
+ OPTPROBE_PUSH_SHSTK_FRAME
".global optprobe_template_val\n"
"optprobe_template_val:\n"
ASM_NOP5
@@ -122,6 +147,7 @@ asm (
".global optprobe_template_call\n"
"optprobe_template_call:\n"
ASM_NOP5
+ OPTPROBE_POP_SHSTK_FRAME
/* Copy 'regs->flags' into 'regs->ss'. */
" movq 18*8(%rsp), %rdx\n"
" movq %rdx, 20*8(%rsp)\n"
--
2.43.0