[RFC PATCH 11/27] x86/rethook: Prepare the rethook trampoline to handle shadow stack

From: K Prateek Nayak

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


Rethook prepare caches the original return IP from the regs->sp state it
was presented and insert the trampoline that later returns back to the
cached IP.

It is possible for multiple handlers to install multiple return
trampolines such that the return from one jumps to another, and
eventually landing to traced functions' caller's frame.

Since return trampoline has hijacked a genuine return, it must balance
the call, not only from the RSB perspective but also from shadow stack
perspective.

Similar to the return_to_handler, populate a dummy slot before
triggering the rethook handlers. The rethook handler must populate the
correct return IP in the slot using regs_set_return_ip() or expect a #CP
for missing redirection.

Since the return trampolines (although limited to two) can be chained in
weird manner to jump between each other based on the order of
pre-handler's running, the tail call technique to populate the return from
the ftrace handler turn complicated.

Instead, use a retpoline to return into the next trampoline until
the execution finds its way back to the correct caller.

Signed-off-by: K Prateek Nayak <kprateek.nayak@xxxxxxx>
---
arch/x86/kernel/rethook.c | 34 ++++++++++++++++++++++++++++++++++
1 file changed, 34 insertions(+)

diff --git a/arch/x86/kernel/rethook.c b/arch/x86/kernel/rethook.c
index 85e2f2d16a90..974ef400b1b3 100644
--- a/arch/x86/kernel/rethook.c
+++ b/arch/x86/kernel/rethook.c
@@ -6,6 +6,9 @@
#include <linux/rethook.h>
#include <linux/kprobes.h>
#include <linux/objtool.h>
+#include <linux/annotate.h>
+
+#include <asm/insn-eval.h>

#include "kprobes/common.h"

@@ -15,6 +18,35 @@ __visible void arch_rethook_trampoline_callback(struct pt_regs *regs);
#define ANNOTATE_NOENDBR
#endif

+#ifdef CONFIG_X86_KERN_SHADOW_STACK
+
+/*
+ * The return to caller of traced function was hijacked by the trampoline and
+ * the trampoline now corrects the return address. Add a spare slot at SSP that
+ * the fixup function can use to syncing the return IP on stack from
+ * arch_rethook_trampoline_callback().
+ *
+ * Clobbers RAX which is dead in both context.
+ */
+#define RETHOOK_PUSH_SHSTK_FRAME \
+ ANNOTATE_INTRA_FUNCTION_CALL "\n" \
+ " call 1f\n" \
+ " int3\n" \
+ "1:\n" \
+ " xor %eax, %eax\n" \
+ " rdsspq %rax\n" \
+ " movq %rax, (%rsp)\n" \
+ " pushq $0x300\n"
+
+#define RETHOOK_POP_SHSTK_FRAME \
+ " addq $16, %rsp\n"
+
+#else /* !CONFIG_X86_KERN_SHADOW_STACK */
+
+#define RETHOOK_PUSH_SHSTK_FRAME
+#define RETHOOK_POP_SHSTK_FRAME
+
+#endif /* CONFIG_X86_KERN_SHADOW_STACK */
/*
* When a target function returns, this code saves registers and calls
* arch_rethook_trampoline_callback(), which calls the rethook handler.
@@ -35,7 +67,9 @@ asm(
" pushfq\n"
SAVE_REGS_STRING
" movq %rsp, %rdi\n"
+ RETHOOK_PUSH_SHSTK_FRAME
" call arch_rethook_trampoline_callback\n"
+ RETHOOK_POP_SHSTK_FRAME
RESTORE_REGS_STRING
/* In the callback function, 'regs->flags' is copied to 'regs->ss'. */
" addq $16, %rsp\n"
--
2.43.0