[RFC PATCH 13/27] x86/cet: Introduce regs_set_return_trampoline() for rethook trampoline

From: K Prateek Nayak

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


Rethooks hijack the function return to jump to a return trampoline and
then to the actual caller. Rethooks can be added from three contexts:

caller
traced function
int3
asm_fred_entrypoint_kernel
exec_int3()
rethook_hook()

caller
call traced_function
call ftrace_caller
call ftrace_handlers
rethook_hook()

caller
call traced_function
jmp optprobe_template_entry
call probe_handlers
rethook_hook()

In each case, the return trampoline frame needs to be handled distinctly
relative to the frame where pt_regs are created and SSP is stashed.

Provide a regs_set_return_trampoline() that will be used in subsequent
commits to hijacks the shadow stack entry of return from traced function
correctly using the context saved in the entry metadata.

The trampolines will use a retpoline to insert a shadow stack slot
corresponding to correct return address and return to the caller
correctly.

Signed-off-by: K Prateek Nayak <kprateek.nayak@xxxxxxx>
---
arch/x86/include/asm/insn-eval.h | 5 +++
arch/x86/kernel/cet.c | 77 ++++++++++++++++++++++++++++++++
2 files changed, 82 insertions(+)

diff --git a/arch/x86/include/asm/insn-eval.h b/arch/x86/include/asm/insn-eval.h
index 75d40a399ace..2cc5562c7d29 100644
--- a/arch/x86/include/asm/insn-eval.h
+++ b/arch/x86/include/asm/insn-eval.h
@@ -87,6 +87,7 @@ static inline void insn_assign_reg(unsigned long *reg, u64 val, int bytes)
void regs_set_return_ip(struct pt_regs *regs, unsigned long ip);
void regs_emulate_call(struct pt_regs *regs, unsigned long ret, unsigned long func);
void regs_emulate_ret(struct pt_regs *regs, unsigned long ret_ip);
+void regs_set_ret_trampoline(struct pt_regs *regs, unsigned long ret);

#else /* !CONFIG_X86_CET */

@@ -110,6 +111,10 @@ static inline void regs_emulate_ret(struct pt_regs *regs, unsigned long ret_ip)
regs_set_return_ip(regs, ret_ip);
}

+static inline void regs_set_ret_trampoline(struct pt_regs *regs, unsigned long ret)
+{
+}
+
#endif /* CONFIG_X86_CET */

#endif /* _ASM_X86_INSN_EVAL_H */
diff --git a/arch/x86/kernel/cet.c b/arch/x86/kernel/cet.c
index fb80c841bbfb..da80c6822242 100644
--- a/arch/x86/kernel/cet.c
+++ b/arch/x86/kernel/cet.c
@@ -433,3 +433,80 @@ noinstr void regs_emulate_ret(struct pt_regs *regs, unsigned long ip)
*/
wrss(ip, &ssef->ssef_lip);
}
+
+/*
+ * Hijack the return to jump to @ret. This is called from the int3
+ * handler and a spare frame is available. Use it to stuff the return
+ * trampoline between the actual return and fred_kernel_frame such that
+ * return from caller is gouted to @ret.
+ *
+ * Caller has already pointed the top of regular stack to @ret.
+ */
+noinstr void regs_set_ret_trampoline(struct pt_regs *regs, unsigned long ret)
+{
+ struct fred_shstk_entry_frame *ssef;
+ struct kernel_event_frame *frame;
+ unsigned int offset = 0;
+
+ if (!cpu_feature_enabled(X86_FEATURE_KERN_SHSTK))
+ return;
+
+ WARN_ON_ONCE(user_mode(regs));
+
+ frame = container_of(regs, struct kernel_event_frame, regs);
+
+ ssef = frame->ssef;
+ if (!ssef)
+ return;
+
+ switch (frame->context) {
+ case FRED_ENTRY:
+ /*
+ * Re-direct the ret below the FRED frame to the new return
+ * address such that the traced function's RET will jump to
+ * the trampoline.
+ */
+ wrss(ret, (u64 *)ssef->ssef_ssp);
+ break;
+ case FTRACE_CALLER:
+ /*
+ * The stack at ftrace_.*caller is:
+ *
+ * caller_function
+ * call traced_function
+ * call ftrace_caller
+ * frame->ssp = rdsspq();
+ *
+ * To redirect return of traced traced_function, the
+ * shadow stack entry below the frame->ssp should be
+ * modified.
+ */
+ offset = 1;
+ fallthrough;
+ case OPTPORBE_TRAMPOLINE:
+ /*
+ * Optprobes are eecuted from a jmp to trampoline:
+ *
+ * caller_function
+ * call traced_function
+ * jmp optprobe_template_entry
+ * frame->ssp = rdsspq();
+ *
+ * In this case, the return from traced_function is at
+ * the top of shadow stack at frame->ssp.
+ */
+ wrss(ret, frame->ssp + offset);
+ break;
+ case RETURN_TRAMPOLINE:
+ /*
+ * Trampolines should only be set at fentry. For
+ * chaining tampoline calls, use regs_set_return_ip().
+ */
+ WARN_ONCE(1, "Setting trampoline from return trampoline!\n");
+ break;
+ default:
+ WARN_ONCE(1, "Unknown calling context %d! Expect #CP\n",
+ frame->context);
+ break;
+ }
+}
--
2.43.0