[tip: x86/urgent] x86/entry: Make SafeRET robust also against NMIs

tip-bot2 for Atish Patra posted 1 patch 6 hours ago
arch/x86/entry/entry_64.S            |  7 +-
arch/x86/include/asm/nospec-branch.h | 83 ++++++++++++++-------------
arch/x86/kernel/cpu/bugs.c           | 39 +-------------
arch/x86/lib/retpoline.S             |  6 +-
4 files changed, 54 insertions(+), 81 deletions(-)
[tip: x86/urgent] x86/entry: Make SafeRET robust also against NMIs
Posted by tip-bot2 for Atish Patra 6 hours ago
The following commit has been merged into the x86/urgent branch of tip:

Commit-ID:     86d477c009ed7f3a24b420b7f4aa314fabe36be3
Gitweb:        https://git.kernel.org/tip/86d477c009ed7f3a24b420b7f4aa314fabe36be3
Author:        Atish Patra <atishp@meta.com>
AuthorDate:    Wed, 18 Nov 2026 19:19:17 -08:00
Committer:     Borislav Petkov (AMD) <bp@alien8.de>
CommitterDate: Sun, 22 Nov 2026 19:39:15 -08:00

x86/entry: Make SafeRET robust also against NMIs

On NMI entry, the kernel copies three exception frames which are used in
crazy ways in order to be able to handle nested NMIs.

However, when that handler interrupts a SafeRET sequence of a #VC
exception, it would copy the SafeRET-unfixed rSP and rIP into those
frames.

paranoid_entry() later will fix up the original frame but the IRET frame
which is the one actually used to return from the NMI handler remains
unfixed, leaving the two frames inconsistent.

And because this is a #VC exception which gets to use
__sev_es_ist_enter() to handle NMIs raised in the #VC exception entry
code, that function will use the fixed up stack pointer from pt_regs.

However, in doing so, it will overwrite the return address which the
stale IRET frame still needs, leading up to the NMI returning to the
old, now stale VC stack slot.

Convert the SafeRET frame fixup code to asm and change it so that it
can get a frame passed in and it can correct it before it gets worked on
later.

And because it is an asm macro, it doesn't need the LFENCE speculation
stopper anymore.

Fixes: 7e7f81cf6f5c ("x86/bugs: Make Safe-RET robust against interrupt injection")
Signed-off-by: Atish Patra <atishp@meta.com>
Co-developed-by: Thomas Gleixner <tglx@kernel.org>
Signed-off-by: Thomas Gleixner <tglx@kernel.org>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Cc: <stable@kernel.org>
---
 arch/x86/entry/entry_64.S            |  7 +-
 arch/x86/include/asm/nospec-branch.h | 83 ++++++++++++++-------------
 arch/x86/kernel/cpu/bugs.c           | 39 +-------------
 arch/x86/lib/retpoline.S             |  6 +-
 4 files changed, 54 insertions(+), 81 deletions(-)

diff --git a/arch/x86/entry/entry_64.S b/arch/x86/entry/entry_64.S
index 253f0a5..7cfc617 100644
--- a/arch/x86/entry/entry_64.S
+++ b/arch/x86/entry/entry_64.S
@@ -937,7 +937,7 @@ SYM_CODE_START(paranoid_entry)
 	IBRS_ENTER save_reg=%r15
 	UNTRAIN_RET_FROM_CALL
 
-	HANDLE_INTR_SAFERET 8(%rsp)
+	FIXUP_INTR_SAFERET %rsp
 
 	RET
 SYM_CODE_END(paranoid_entry)
@@ -1044,7 +1044,7 @@ SYM_CODE_START(error_entry)
 
 	VALIDATE_UNRET_END
 
-	HANDLE_INTR_SAFERET 8(%rsp)
+	FIXUP_INTR_SAFERET %rsp
 
 	cmpq	$.Lgs_change, RIP+8(%rsp)
 	jne	.Lerror_entry_done_lfence
@@ -1205,6 +1205,9 @@ SYM_CODE_START(asm_exc_nmi)
 	jmp	swapgs_restore_regs_and_return_to_usermode
 
 .Lnmi_from_kernel:
+	/* Repair the source frame before inspecting or copying it. */
+	FIXUP_NMI_INTR_SAFERET %rsp
+
 	/*
 	 * Here's what our stack frame will look like:
 	 * +---------------------------------------------------------+
diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/nospec-branch.h
index 1abeb9a..6455443 100644
--- a/arch/x86/include/asm/nospec-branch.h
+++ b/arch/x86/include/asm/nospec-branch.h
@@ -185,42 +185,39 @@
  *
  * The Safe-RET sequence is:
  *
- * CALL
- * LEA 8(%RSP), %RSP
- * RET
+ *	CALL	L1
+ * L1:	LEA	8(%RSP), %RSP
+ * L2:	RET
  *
- * The two CMPs below check whether RIP points to after the CALL or after the
- * LEA.
+ * The C equivalent of the ASM sequence below is:
  *
- * The LFENCE below is to address this particular speculation case:
+ *	if (ptr->RIP > L2)	// After RET?
+ *		goto done;
+ *	if (ptr->RIP < L1)	// Before LEA ?
+ *		goto done;
+ *	if (ptr->RIP == L1)	// At LEA ?
+ *		ptr->RSP += 8;	// Emulate LEA
+ *	tmp = *ptr->RSP;	// Read original return IP
+ *	ptr->RIP = tmp;		// Overwrite interrupt return IP
+ *	ptr->RSP += 8;		// Emulate RET
  *
- * 1. Userspace runs and poisons the BTB around the safe-RET routine
- *
- * 2. Userspace triggers some kind of exception
- *
- * 3. Kernel executes error_entry() and mis-speculates the branch into thinking
- *    it actually came from kernel space
- *
- * 4. The kernel then further mis-speculates that the exception occurred due
- *    to an interrupted safe-RET
- *
- * 5. The handle_interrupted_saferet() routine speculatively executes and
- *    speculatively does a safe-RET. But this is unsafe since it was never
- *    untrained.
- *
- * The LFENCE fixes this by ensuring step 5 is never reached speculatively.
- * Note that this LFENCE only occurs if safe-RET was actually interrupted (so
- * it's outside of the normal path).
+ * This ensures that this interrupt returns to the original return IP without
+ * resuming the interrupted safe-RET sequence.
  */
-#define __HANDLE_INTR_SAFERET(name, pt_regs)		\
-	cmpq	$(name), RIP+pt_regs;			\
-	jb	1f;					\
-	cmpq	$(name)+5, RIP+pt_regs;			\
-	ja	1f;					\
-	lfence;						\
-	leaq	pt_regs, %rdi;				\
-	call	handle_interrupted_saferet;		\
-	1:
+#define __FIXUP_INTR_SAFERET(name, ptr, rip_offs, rsp_offs)	\
+	cmpq	$(name)+5, rip_offs(ptr);			\
+	ja	2f;						\
+	cmpq	$(name), rip_offs(ptr);				\
+	jb	2f;						\
+	jne	1f;						\
+	addq	$8, rsp_offs(ptr);				\
+	1:;							\
+	movq	rsp_offs(ptr), %rdx;				\
+	movq	(%rdx), %rdx;					\
+	movq	%rdx, rip_offs(ptr);				\
+	addq	$8, rsp_offs(ptr);				\
+	2:
+
 
 #ifdef __ASSEMBLER__
 
@@ -305,7 +302,7 @@
 .macro CALL_UNTRAIN_RET
 #if defined(CONFIG_MITIGATION_UNRET_ENTRY) || defined(CONFIG_MITIGATION_SRSO)
 	ALTERNATIVE_2 "", "call entry_untrain_ret", X86_FEATURE_UNRET, \
-		          "call srso_alias_untrain_ret", X86_FEATURE_SRSO_ALIAS
+			  "call srso_alias_untrain_ret", X86_FEATURE_SRSO_ALIAS
 #endif
 .endm
 
@@ -339,12 +336,25 @@
 #define UNTRAIN_RET_FROM_CALL \
 	__UNTRAIN_RET X86_FEATURE_ENTRY_IBPB, __stringify(RESET_CALL_DEPTH_FROM_CALL)
 
-.macro HANDLE_INTR_SAFERET pt_regs
+.macro FIXUP_INTR_SAFERET pt_regs
 #ifdef CONFIG_MITIGATION_SRSO
 	ALTERNATIVE_2 "", \
-	__stringify(__HANDLE_INTR_SAFERET(srso_safe_ret, \pt_regs)), X86_FEATURE_SRSO, \
-	__stringify(__HANDLE_INTR_SAFERET(srso_alias_safe_ret, \pt_regs)), X86_FEATURE_SRSO_ALIAS
+	__stringify(__FIXUP_INTR_SAFERET(srso_safe_ret, \pt_regs, RIP+8, RSP+8)), X86_FEATURE_SRSO, \
+	__stringify(__FIXUP_INTR_SAFERET(srso_alias_safe_ret, \pt_regs, RIP+8, RSP+8)), X86_FEATURE_SRSO_ALIAS
+#endif
+.endm
 
+/*
+ * Note the offsets encoding below:
+ *
+ * The IRET frame at call site in asm_exc_nmi() starts at 8(%rsp) (NMI handler
+ * users %rdx as a temp var so it pushes it on the stack on entry).
+ */
+.macro FIXUP_NMI_INTR_SAFERET fptr
+#ifdef CONFIG_MITIGATION_SRSO
+	ALTERNATIVE_2 "", \
+	__stringify(__FIXUP_INTR_SAFERET(srso_safe_ret, \fptr, RIP-RIP+8, RSP-RIP+8)), X86_FEATURE_SRSO, \
+	__stringify(__FIXUP_INTR_SAFERET(srso_alias_safe_ret, \fptr, RIP-RIP+8, RSP-RIP+8)), X86_FEATURE_SRSO_ALIAS
 #endif
 .endm
 
@@ -681,7 +691,6 @@ static __always_inline void x86_idle_clear_cpu_buffers(void)
 
 void srso_safe_ret(void);
 void srso_alias_safe_ret(void);
-void handle_interrupted_saferet(struct pt_regs *regs);
 
 #endif /* __ASSEMBLER__ */
 
diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c
index 4ced10c..8c2c831 100644
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -3787,42 +3787,3 @@ void __warn_thunk(void)
 {
 	WARN_ONCE(1, "Unpatched return thunk in use. This should not happen!\n");
 }
-
-#ifdef CONFIG_MITIGATION_SRSO
-/*
- * Called during exception/interrupt entry if interrupted during the
- * safe-RET sequence.  The safe-RET sequence consists of 3 instructions:
- *
- *	CALL
- *	LEA 8(%RSP), %RSP
- *	RET
- *
- * An interrupt after the CALL or after the LEA could potentially lead
- * to branch predictor poisoning and results in the sequence not being
- * able to be safely resumed.
- *
- * Therefore, modify the regs state as if the remaining part of the
- * safe-RET sequence executed so the interrupt returns back to the
- * desired return target, instead of the to the safe-RET sequence.
- */
-void noinstr handle_interrupted_saferet(struct pt_regs *regs)
-{
-	unsigned long rip = regs->ip;
-
-	if (rip == (unsigned long) srso_safe_ret ||
-	    rip == (unsigned long) srso_alias_safe_ret) {
-	    /* Modify stack pointer as if LEA executed: */
-	    regs->sp += 8;
-	}
-
-	/*
-	 * Adjust registers as if RET executed:
-	 *
-	 * 1. Read the return address off the stack and into rIP:
-	 */
-	regs->ip = *(unsigned long *)(regs->sp);
-
-	/* 2. Pop rIP off the stack: */
-	regs->sp += 8;
-}
-#endif /* CONFIG_MITIGATION_SRSO */
diff --git a/arch/x86/lib/retpoline.S b/arch/x86/lib/retpoline.S
index f9ca1d8..376119f 100644
--- a/arch/x86/lib/retpoline.S
+++ b/arch/x86/lib/retpoline.S
@@ -210,13 +210,13 @@ SYM_CODE_START_NOALIGN(srso_alias_safe_ret)
 
 	/*
 	 * Tell objtool that those are not function pointers referenced by
-	 * __HANDLE_INTR_SAFERET(). Below too.
+	 * FIXUP_[NMI_]INTR_SAFERET(). Below too.
 	 */
 	ANNOTATE_NOENDBR
 
 	/*
 	 * Safe-RET sequence. If you need to change it, adjust
-	 * handle_interrupted_saferet() too.
+	 * FIXUP_[NMI_]INTR_SAFERET() too.
 	 */
 	lea 8(%_ASM_SP), %_ASM_SP
 	UNWIND_HINT_FUNC
@@ -261,7 +261,7 @@ SYM_CODE_START_LOCAL_NOALIGN(srso_untrain_ret)
 SYM_INNER_LABEL(srso_safe_ret, SYM_L_GLOBAL)
 	/*
 	 * Safe-RET sequence. If you need to change it, adjust
-	 * handle_interrupted_saferet() too.
+	 * FIXUP_[NMI_]INTR_SAFERET() too.
 	 */
 	lea 8(%_ASM_SP), %_ASM_SP
 	ret