mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andrew Cooper <andrew.cooper3@citrix.com>
To: Dave Hansen <dave.hansen@linux.intel.com>, linux-kernel@vger.kernel.org
Cc: Andrew Cooper <andrew.cooper3@citrix.com>,
	Andy Lutomirski <luto@kernel.org>,
	Peter Zijlstra <peterz@infradead.org>,
	Rick P <rick.p.edgecombe@intel.com>,
	the arch/x86 maintainers <x86@kernel.org>,
	Kees Cook <kees@kernel.org>,
	Pawan Gupta <pawan.kumar.gupta@linux.intel.com>,
	Borislav Petkov <bp@alien8.de>, Thomas Gleixner <tglx@kernel.org>
Subject: Re: [RFC][PATCH 20/24] x86/entry: Stash kernel SSP on FRED entry
Date: Thu, 22 Oct 2026 23:14:44 +0100	[thread overview]
Message-ID: <47f352d9-1ad8-44c9-bcf2-af1dbf14b626@citrix.com> (raw)
In-Reply-To: <20261021195147.3B42C03D@davehans-spike.ostc.intel.com>

On 21/10/2026 8:51 pm, Dave Hansen wrote:
> Kernel shadow stacks are intended to preserve control flow integrity
> across all kinds of RIP transitions, not just strict CALLs. One of
> those transitions is when there is a kernel interrupt or exception
> and the kernel stops what it was doing and jumps over to
> asm_fred_entrypoint_kernel.
>
> On those jumps, FRED hardware writes a special frame to the shadow
> stack. That frame includes the return address of the interrupted
> kernel code. When returning from the event via ERETS, the hardware
> compares this shadow stack frame with the return address on the
> regular stack to ensure that they match.
>
> The kernel munges event return addresses for lots of reasons. But,
> with shadow stacks, it also needs to munge the corresponding
> shadow stack frame contents.
>
> Make it easy for RIP-munging code to locate the shadow stack frame.
> Stash off the shadow stack pointer so it sits adjacent to pt_regs.
> Without this, anything wishing to locate the special shadow stack
> frame would need to walk up the stack. I'm not opposed to doing
> it that way, this just seemed simpler for now.
>
> Note, since RDSSPQ is a NOP with shadow stacks off, this assembly
> is safe either way, although it does a unnecessary push/pop.

It's not unnecessary.  The data layout must match what Linux is compiled
with, which is an extra register by pt_regs, even if running on hardware
without shstk.

>
> ---
>
>  b/arch/x86/entry/entry_64_fred.S |   22 ++++++++++++++++++++++
>  1 file changed, 22 insertions(+)
>
> diff -puN arch/x86/entry/entry_64_fred.S~fred-entry-ssp-stash arch/x86/entry/entry_64_fred.S
> --- a/arch/x86/entry/entry_64_fred.S~fred-entry-ssp-stash	2026-10-21 12:36:42.552889917 -0700
> +++ b/arch/x86/entry/entry_64_fred.S	2026-10-21 12:36:42.555889923 -0700
> @@ -16,6 +16,26 @@
>  	.code64
>  	.section .noinstr.text, "ax"
>  
> +/*
> + * Kernel-mode events: save the SSP of the shadow stack frame FRED
> + * pushed (old SSP, LIP, CS) just below pt_regs. See struct
> + * fred_kernel_frame.
> + */
> +.macro PUSH_KERNEL_EVENT_SSP
> +#ifdef CONFIG_X86_USER_SHADOW_STACK
> +	xorl	%eax, %eax
> +	rdsspq	%rax		/* nop if shadow stacks are off  */
> +	pushq	%rax
> +	UNWIND_HINT_REGS offset=8
> +#endif
> +.endm
> +
> +.macro POP_KERNEL_EVENT_SSP
> +#ifdef CONFIG_X86_USER_SHADOW_STACK
> +	addq	$8, %rsp
> +#endif
> +.endm
> +
>  .macro FRED_ENTER
>  	UNWIND_HINT_END_OF_STACK
>  	ANNOTATE_NOENDBR
> @@ -54,7 +74,9 @@ SYM_CODE_END(asm_fred_entrypoint_user)
>  	.org asm_fred_entrypoint_user + 256, 0xcc
>  SYM_CODE_START_NOALIGN(asm_fred_entrypoint_kernel)
>  	FRED_ENTER
> +	PUSH_KERNEL_EVENT_SSP
>  	call	fred_entry_from_kernel
> +	POP_KERNEL_EVENT_SSP
>  	FRED_EXIT
>  	ERETS
>  SYM_CODE_END(asm_fred_entrypoint_kernel)
> _

This only works because FRED_ENTER loads %rsp into %rdi before
PUSH_KERNEL_EVENT_SSP pushes SSP, and having that all hidden is rather rude.

Also, you mean X86_KERN_SHADOW_STACK here, rather than USER.

Personally, I'd suggest:

diff --git a/arch/x86/entry/entry_64_fred.S b/arch/x86/entry/entry_64_fred.S
index b98f8945dfff..9a17dd7e2a96 100644
--- a/arch/x86/entry/entry_64_fred.S
+++ b/arch/x86/entry/entry_64_fred.S
@@ -16,15 +16,27 @@
        .code64
        .section .noinstr.text, "ax"
 
-.macro FRED_ENTER
+.macro FRED_ENTER ssp=0
        UNWIND_HINT_END_OF_STACK
        ANNOTATE_NOENDBR
        PUSH_AND_CLEAR_REGS
        ENCODE_FRAME_POINTER
        movq    %rsp, %rdi      /* %rdi -> pt_regs */
+
+#ifdef CONFIG_X86_KERN_SHADOW_STACK
+       .if \ssp
+       rdsspq  %rax /* NOP if SHSTK is off.  Pre-zeroed. */
+       push    %rax
+       .endif
+#endif
 .endm
 
-.macro FRED_EXIT
+.macro FRED_EXIT ssp=0
+#ifdef CONFIG_X86_KERN_SHADOW_STACK
+       .if \ssp
+       pop     %rax /* Any dead register */
+       .endif
+#endif
        UNWIND_HINT_REGS
        POP_REGS
 .endm
@@ -53,9 +65,9 @@ SYM_CODE_END(asm_fred_entrypoint_user)
  */
        .org asm_fred_entrypoint_user + 256, 0xcc
 SYM_CODE_START_NOALIGN(asm_fred_entrypoint_kernel)
-       FRED_ENTER
+       FRED_ENTER ssp=1
        call    fred_entry_from_kernel
-       FRED_EXIT
+       FRED_EXIT ssp=1
        ERETS
 SYM_CODE_END(asm_fred_entrypoint_kernel)
 

This at least lets you see the relationship between %rdi and what's on
the stack.

Of note,

* You don't need to re-zero the GPR.  They're all 0.
*  You want to use POP rather than ADD %rsp to avoid taking a stack
engine stall in FRED_EXIT.

~Andrew

  reply	other threads:[~2026-10-22 22:14 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-21 19:51 [RFC][PATCH 00/24] x86 Kernel Shadow Stacks Dave Hansen
2026-10-21 19:51 ` [RFC][PATCH 01/24] kernel/fork: Break out vmap task allocation into helper Dave Hansen
2026-10-21 19:51 ` [RFC][PATCH 02/24] kernel/fork: Use sizeof() union instead of TASK_SIZE Dave Hansen
2026-10-21 19:51 ` [RFC][PATCH 03/24] kernel/sched: Move thread stack to its own structure Dave Hansen
2026-10-21 19:51 ` [RFC][PATCH 04/24] kernel/fork: Allow arch-specific stack initialization Dave Hansen
2026-10-21 19:51 ` [RFC][PATCH 05/24] kernel/task: Add shadow stack to thread structure Dave Hansen
2026-10-21 19:51 ` [RFC][PATCH 06/24] kernel/task: Add "runtime" check for shadow stack support Dave Hansen
2026-10-21 19:51 ` [RFC][PATCH 07/24] kernel/fork: Clear shadow stacks alongside normal stacks Dave Hansen
2026-10-21 19:51 ` [RFC][PATCH 08/24] x86/cpu: Add basic x86 kernel shadow stack infrastructure Dave Hansen
2026-10-21 19:51 ` [RFC][PATCH 09/24] x86/cea: Refactor "CEA" macros to deal with multiple stacks Dave Hansen
2026-10-21 19:51 ` [RFC][PATCH 10/24] x86/cea: Refactor stack mapping macros Dave Hansen
2026-10-21 19:51 ` [RFC][PATCH 11/24] x86/mm: Add kernel shadow stack permission macros Dave Hansen
2026-10-21 19:51 ` [RFC][PATCH 12/24] x86/cea: Map and use FRED exception shadow stacks Dave Hansen
2026-10-21 21:40   ` Peter Zijlstra
2026-10-21 22:59     ` Dave Hansen
2026-10-21 19:51 ` [RFC][PATCH 13/24] x86/task: Grow thread_union for " Dave Hansen
2026-10-21 19:51 ` [RFC][PATCH 14/24] x86/mm: Establish shadow stack permissions in thread mapping Dave Hansen
2026-10-21 19:51 ` [RFC][PATCH 15/24] x86/task: Hook up arch hook to detect shadow stack enabling Dave Hansen
2026-10-21 19:51 ` [RFC][PATCH 16/24] x86/mm: Kill shadow stack page permission warning Dave Hansen
2026-10-21 19:51 ` [RFC][PATCH 17/24] x86/task: Context switch the kernel shadow stack register value Dave Hansen
2026-10-21 19:51 ` [RFC][PATCH 18/24] kernel/task: Add a helper to get the thread_union Dave Hansen
2026-10-21 19:51 ` [RFC][PATCH 19/24] x86/process: Initialize cloned task shadow stack Dave Hansen
2026-10-21 19:51 ` [RFC][PATCH 20/24] x86/entry: Stash kernel SSP on FRED entry Dave Hansen
2026-10-22 22:14   ` Andrew Cooper [this message]
2026-10-21 19:51 ` [RFC][PATCH 21/24] x86/extable: Update shadow stack entry frame during RIP overwrites Dave Hansen
2026-10-21 19:51 ` [RFC][PATCH 22/24] kernel/task: Add a helper to locate the top of the shadow stack Dave Hansen
2026-10-21 19:51 ` [RFC][PATCH 23/24] x86/entry: Populate FRED register for user=>kernel transitions Dave Hansen
2026-10-21 19:51 ` [RFC][PATCH 24/24] [DO NOT MERGE] x86/shstk: Depend out everything incompatible with kernel shadow stacks Dave Hansen
2026-10-22 21:35   ` Andrew Cooper
2026-10-22 21:38     ` Andrew Cooper
2026-10-21 21:36 ` [RFC][PATCH 00/24] x86 Kernel Shadow Stacks Peter Zijlstra
2026-10-21 21:53 ` Peter Zijlstra

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=47f352d9-1ad8-44c9-bcf2-af1dbf14b626@citrix.com \
    --to=andrew.cooper3@citrix.com \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=kees@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@kernel.org \
    --cc=pawan.kumar.gupta@linux.intel.com \
    --cc=peterz@infradead.org \
    --cc=rick.p.edgecombe@intel.com \
    --cc=tglx@kernel.org \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®