Re: [RFC PATCH 04/27] x86/cet: Make kernel shadow stack #CP fatal instead of livelocking

From: K Prateek Nayak

Date: Thu Dec 03 2026 - 22:39:42 EST


Hello Andy,

On 12/4/2026 7:56 AM, Andy Lutomirski wrote:
> [You don't often get email from luto@xxxxxxxxxx. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]
>
> 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.

Ack! That should be easy to do by making a bunch of INTRA_FUNCTION_CALL
and correcting the rsp while letting the SSP go off the end of the
shadow stack page.

>> 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.)

Yes, you are right. If the SSP points to a mapping with incorrect
permission we will get a page fault with SS flag set but since that
IDT event cannot push the shadow stack frame either, it'll be
upgraded to a double fault.

>> 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'm not sure what the right call is for a #CP in kernel mode - that
really means someone has messed up the stack / has redirected regs->ip
to redirect execution without relying on the CET aware helpers.

Since a return address in kernel has been hijacked, I don't think it
can be considered safe to allow program to return to the hijacked
address and eventually kill the task on exit to usermode right?

> I meant to mention: there should be LKDTM mechanisms to deliberately
> trigger these sorts of failures.

Ack! Triggers for the #DF from incorrect SPP and overrunning the
shadow stack can be added - I'll do so in my next iteration or send it
on top of Dave's next iteration behind an LKDTM trigger.

For #CP in kernel mode, I'm not too sure - I'll love to hear more form
the folks who have dealt with security aspects if the fault handler
should attempt some form of recovery - we can match the return address
on stack to that on the shadow stack but the integrity of the stack
at that point is questionable.

--
Thanks and Regards,
Prateek