Re: [RFC PATCH 04/27] x86/cet: Make kernel shadow stack #CP fatal instead of livelocking
From: Andy Lutomirski
Date: Thu Dec 03 2026 - 21:26:46 EST
On Thu, Dec 3, 2026, at 5:14 PM, Andy Lutomirski wrote:
> On Sun, Nov 29, 2026, at 2:29 PM, K Prateek Nayak wrote:
>> 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.
>>
>
>> 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);
>> }
>> }
>
>
> When I did all the vmap stack overrun code, I made a point of
> explicitly analyzing and testing the behavior of the related faults,
> *including when either the code that faulted or the handler, at any
> stage including asm, overran the stack*.
>
> This needs the same treatment. If we somehow overrun the shadow stack
> (unlikely but far from impossible, especially if something like the BPF
> JIT or retpolines mess up outrageously) or if we somehow corrupt SSP.
>
> And we need to cover the case where SSP points to non-shstk memory.
> (Off the top of my head this will end up double-faulting, and we will
> hit either the IST or the FRED_RSP table, and that needs testing too.)
>
> All of these should result in cleanly killing the offending task,
> printing a log, *and still working correctly if it happens multiple
> times*, which means we need to be able to escape from #DF context and
> survive the do_exit experience. Which will be great fun.
>
> Maybe this series already handles all of this. Does it?
I meant to mention: there should be LKDTM mechanisms to deliberately trigger these sorts of failures.