[RFC PATCH 12/27] x86/cet: Make regs_set_return_ip() helpers context aware
From: K Prateek Nayak
Date: Sun Nov 29 2026 - 17:33:58 EST
All the known locations that can genuinely change the control flow have
been instrumented to advertise their context. Use the context
information to set the return IP to the correct shadow stack slot from
regs_set_return_ip().
All the emulation helpers are still expected to be called from a #BP
that inserts a FRED frame and any violations from other contexts are
warned about to help debugs and any shadow stack adjustments for these
contexts are skipped.
Signed-off-by: K Prateek Nayak <kprateek.nayak@xxxxxxx>
---
arch/x86/kernel/cet.c | 41 ++++++++++++++++++++++++++++++++++++++---
1 file changed, 38 insertions(+), 3 deletions(-)
diff --git a/arch/x86/kernel/cet.c b/arch/x86/kernel/cet.c
index b037508215c9..fb80c841bbfb 100644
--- a/arch/x86/kernel/cet.c
+++ b/arch/x86/kernel/cet.c
@@ -275,6 +275,7 @@ DEFINE_IDTENTRY_ERRORCODE(exc_control_protection)
noinstr void regs_set_return_ip(struct pt_regs *regs, unsigned long ip)
{
struct fred_shstk_entry_frame *ssef;
+ struct kernel_event_frame *frame;
regs->ip = ip;
@@ -284,11 +285,34 @@ noinstr void regs_set_return_ip(struct pt_regs *regs, unsigned long ip)
/* Only kernel-mode events save the 'ssef' pointer. */
WARN_ON_ONCE(user_mode(regs));
- ssef = container_of(regs, struct kernel_event_frame, regs)->ssef;
+ frame = container_of(regs, struct kernel_event_frame, regs);
+
+ ssef = frame->ssef;
if (!ssef)
return;
- wrss(ip, &ssef->ssef_lip);
+ switch (frame->context) {
+ case FRED_ENTRY:
+ wrss(ip, &ssef->ssef_lip);
+ break;
+ case FTRACE_CALLER:
+ fallthrough;
+ case RETURN_TRAMPOLINE:
+ /*
+ * In case of both RETURN_TRAMPOLINE and FTRACE_CALLER,
+ * regs_set_return_ip() is used to hijack the return
+ * from the current frame. Just set the location at SSP.
+ */
+ wrss(ip, frame->ssp);
+ break;
+ case OPTPORBE_TRAMPOLINE:
+ /* regs->ip changes from optprobes are ignored. */
+ break;
+ default:
+ WARN_ONCE(1, "Unknown calling context %d! Expect #CP\n",
+ frame->context);
+ break;
+ }
}
/*
@@ -317,6 +341,10 @@ noinstr void regs_emulate_call(struct pt_regs *regs, unsigned long ret, unsigned
if (!ssef)
return;
+ /* A CALL emulation for !INT3 events are ignored. */
+ if (WARN_ON_ONCE(frame->context != FRED_ENTRY))
+ return;
+
/* Someone else emulated a CALL for same frame? */
if (WARN_ON_ONCE(frame->fred_exit_work_flags))
return;
@@ -371,6 +399,7 @@ noinstr void regs_emulate_call(struct pt_regs *regs, unsigned long ret, unsigned
noinstr void regs_emulate_ret(struct pt_regs *regs, unsigned long ip)
{
struct fred_shstk_entry_frame *ssef;
+ struct kernel_event_frame *frame;
regs->ip = ip;
@@ -379,10 +408,16 @@ noinstr void regs_emulate_ret(struct pt_regs *regs, unsigned long ip)
WARN_ON_ONCE(user_mode(regs));
- ssef = container_of(regs, struct kernel_event_frame, regs)->ssef;
+ frame = container_of(regs, struct kernel_event_frame, regs);
+
+ ssef = frame->ssef;
if (!ssef)
return;
+ /* A RET emulation for !INT3 events are ignored. */
+ if (WARN_ON_ONCE(frame->context != FRED_ENTRY))
+ return;
+
/*
* Return target should point to the old SP on top of the shadow
* stack that was popped from the regular stack.
--
2.43.0