[RFC PATCH 15/27] x86/rethook: Make the trampoline insert and return shadow stack aware

From: K Prateek Nayak

Date: Sun Nov 29 2026 - 17:34:53 EST


Use the CET helper regs_set_ret_trampoline() to insert the rethook
trampoline and regs_set_return_ip() to correct the return address to the
caller from the return trampoline's handler.

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

diff --git a/arch/x86/kernel/rethook.c b/arch/x86/kernel/rethook.c
index 974ef400b1b3..a5f92c3feedd 100644
--- a/arch/x86/kernel/rethook.c
+++ b/arch/x86/kernel/rethook.c
@@ -145,17 +145,23 @@ void arch_rethook_fixup_return(struct pt_regs *regs,

/* Replace fake return address with real one. */
*frame_pointer = correct_ret_addr;
+ regs_set_return_ip(regs, correct_ret_addr);
}
NOKPROBE_SYMBOL(arch_rethook_fixup_return);

void arch_rethook_prepare(struct rethook_node *rh, struct pt_regs *regs, bool mcount)
{
+ unsigned long ip = (unsigned long)arch_rethook_trampoline;
unsigned long *stack = (unsigned long *)regs->sp;

rh->ret_addr = stack[0];
rh->frame = regs->sp;

/* Replace the return addr with trampoline addr */
- stack[0] = (unsigned long) arch_rethook_trampoline;
+ stack[0] = ip;
+
+ /* First return trampoline; Override shadow stack. */
+ if (rh->ret_addr != ip)
+ regs_set_ret_trampoline(regs, ip);
}
NOKPROBE_SYMBOL(arch_rethook_prepare);
--
2.43.0