[RFC PATCH 04/27] x86/cet: Make kernel shadow stack #CP fatal instead of livelocking
From: K Prateek Nayak
Date: Sun Nov 29 2026 - 17:31:31 EST
A kernel-mode shadow stack #CP (NEAR-RET or FAR-RET/IRET) ends up in
do_unexpected_cp(), which does WARN_ONCE() and returns to a shadow stack
with mismatch. Returning executes that instruction again and faults
again. After the first WARN this repeats silently forever. For example,
IRET-to-self in sync_core() on a CPU without SERIALIZE just hangs the
machine.
Nothing can be fixed up at that point. Handle kernel-mode shadow stack
regular stack top vs the shadow stack top for RET, and the RIP in the
IRET/ERETS frame vs the shadow stack LIP for FAR-RET/IRET. Then die().
A code path that returns somewhere the shadow stack doesn't expect now
shows up as a one-line oops naming both targets, instead of a hang or a
WARN storm.
Signed-off-by: K Prateek Nayak <kprateek.nayak@xxxxxxx>
---
arch/x86/kernel/cet.c | 101 ++++++++++++++++++++++++++++++++----------
1 file changed, 77 insertions(+), 24 deletions(-)
diff --git a/arch/x86/kernel/cet.c b/arch/x86/kernel/cet.c
index 4033775a922c..97375a8335c8 100644
--- a/arch/x86/kernel/cet.c
+++ b/arch/x86/kernel/cet.c
@@ -1,7 +1,9 @@
// SPDX-License-Identifier: GPL-2.0
#include <linux/ptrace.h>
+#include <linux/uaccess.h>
#include <asm/bugs.h>
+#include <asm/kdebug.h>
#include <asm/msr.h>
#include <asm/traps.h>
#include <asm/insn-eval.h>
@@ -112,6 +114,77 @@ static void ibt_clear_fred_wfe(struct pt_regs *regs)
regs->fred_cs.wfe = 0;
}
+/*
+ * FRED hardware pushes this frame on to the shadow stack for
+ * kernel=>kernel entry. First (lowest address) in this struct
+ * is the last entry pushed by the hardware.
+ */
+struct fred_shstk_entry_frame {
+ u64 ssef_ssp;
+ u64 ssef_lip;
+ u64 ssef_cs;
+};
+
+/*
+ * The kernel=>kernel entry code pushes SSP on to the entry stack.
+ * This only occurs for kernel=>kernel entry. Not userspace.
+ *
+ * SSP is pushed on to the frame below pt_regs. At the point it is
+ * pushed, it points to a special FRED entry frame.
+ */
+struct fred_kernel_frame {
+ struct fred_shstk_entry_frame *ssef;
+ struct pt_regs regs;
+};
+
+static unsigned long cp_peek(unsigned long addr)
+{
+ unsigned long val = 0;
+
+ copy_from_kernel_nofault(&val, (void *)addr, sizeof(val));
+ return val;
+}
+
+/*
+ * Shadow stack #CPs are faults: regs->ip is the RET/IRET/ERETS that found
+ * the mismatch and returning from here would just execute it again. There
+ * is no way to recover, so say which two return addresses disagreed and
+ * die.
+ */
+static void do_kernel_shstk_cp(struct pt_regs *regs, unsigned long error_code)
+{
+ struct fred_shstk_entry_frame *ssef;
+ static const u8 erets[] = { 0xf2, 0x0f, 0x01, 0xca };
+ unsigned long ssp, ip, shstk_ip, sp_off = 0;
+ u8 insn[sizeof(erets)] = { };
+
+ ssef = container_of(regs, struct fred_kernel_frame, regs)->ssef;
+ ssp = cp_peek((unsigned long)&ssef->ssef_ssp);
+
+ switch (error_code & CP_EC) {
+ case CP_RET:
+ ip = cp_peek(regs->sp);
+ shstk_ip = cp_peek(ssp);
+ break;
+ case CP_IRET:
+ /* ERETS skips the error code slot, IRET does not. */
+ copy_from_kernel_nofault(insn, (void *)regs->ip, sizeof(insn));
+ if (!memcmp(insn, erets, sizeof(erets)))
+ sp_off = 8;
+ ip = cp_peek(regs->sp + sp_off);
+ shstk_ip = cp_peek(ssp + 8);
+ break;
+ default:
+ ip = shstk_ip = 0;
+ break;
+ }
+
+ pr_emerg("Kernel shadow stack violation (%s) at %pS: stack: %pS shadow stack: %pS (ssp=%lx)\n",
+ cp_err_string(error_code), (void *)regs->ip, (void *)ip,
+ (void *)shstk_ip, ssp);
+ die("control protection fault", regs, error_code);
+}
+
static void do_kernel_cp_fault(struct pt_regs *regs, unsigned long error_code)
{
if ((error_code & CP_EC) != CP_ENDBR) {
@@ -156,36 +229,16 @@ DEFINE_IDTENTRY_ERRORCODE(exc_control_protection)
else
do_unexpected_cp(regs, error_code);
} else {
- if (cpu_feature_enabled(X86_FEATURE_IBT))
+ if (cpu_feature_enabled(X86_FEATURE_KERN_SHSTK) &&
+ (error_code & CP_EC) != CP_ENDBR)
+ do_kernel_shstk_cp(regs, error_code);
+ else if (cpu_feature_enabled(X86_FEATURE_IBT))
do_kernel_cp_fault(regs, error_code);
else
do_unexpected_cp(regs, error_code);
}
}
-/*
- * FRED hardware pushes this frame on to the shadow stack for
- * kernel=>kernel entry. First (lowest address) in this struct
- * is the last entry pushed by the hardware.
- */
-struct fred_shstk_entry_frame {
- u64 ssef_ssp;
- u64 ssef_lip;
- u64 ssef_cs;
-};
-
-/*
- * The kernel=>kernel entry code pushes SSP on to the entry stack.
- * This only occurs for kernel=>kernel entry. Not userspace.
- *
- * SSP is pushed on to the frame below pt_regs. At the point it is
- * pushed, it points to a special FRED entry frame.
- */
-struct fred_kernel_frame {
- struct fred_shstk_entry_frame *ssef;
- struct pt_regs regs;
-};
-
/*
* Change the return IP of an event. Use this instead of directly
* manipulating regs->ip.
--
2.43.0