public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Borislav Petkov <bp@suse.de>
To: Peter Zijlstra <peterz@infradead.org>
Cc: Thadeu Lima de Souza Cascardo <cascardo@canonical.com>,
	linux-kernel@vger.kernel.org, linux-efi@vger.kernel.org,
	x86@kernel.org, ardb@kernel.org, tglx@linutronix.de,
	gregkh@linuxfoundation.org, torvalds@linux-foundation.org,
	Guenter Roeck <linux@roeck-us.net>,
	Josh Poimboeuf <jpoimboe@kernel.org>,
	stable@vger.kernel.org, Andrew Cooper <Andrew.Cooper3@citrix.com>
Subject: Re: [PATCH] efi/x86: use naked RET on mixed mode call wrapper
Date: Mon, 18 Jul 2022 18:28:27 +0200	[thread overview]
Message-ID: <YtWKK2ZLib1R7itI@zn.tnic> (raw)
In-Reply-To: <YtVG8VBmFikS6GMn@worktop.programming.kicks-ass.net>

On Mon, Jul 18, 2022 at 01:41:37PM +0200, Peter Zijlstra wrote:
> diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/nospec-branch.h
> index 10a3bfc1eb23..f934dcdb7c0d 100644
> --- a/arch/x86/include/asm/nospec-branch.h
> +++ b/arch/x86/include/asm/nospec-branch.h
> @@ -297,6 +297,8 @@ do {									\
>  	alternative_msr_write(MSR_IA32_SPEC_CTRL,			\
>  			      spec_ctrl_current() | SPEC_CTRL_IBRS,	\
>  			      X86_FEATURE_USE_IBRS_FW);			\
> +	altnerative_msr_write(MSR_IA32_PRED_CMD, PRED_CMD_IBPB,		\
> +			      X86_FEATURE_USE_IBPB_FW);			\
>  } while (0)

So I'm being told we need to untrain on return from EFI to protect the
kernel from it. Ontop of yours.

Asm looks correct but lemme run it through the test builds first...

---
diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/nospec-branch.h
index 38a3e86e665e..e58f18555022 100644
--- a/arch/x86/include/asm/nospec-branch.h
+++ b/arch/x86/include/asm/nospec-branch.h
@@ -62,6 +62,12 @@
 	dec	reg;				\
 	jnz	771b;
 
+#ifdef CONFIG_CPU_UNRET_ENTRY
+#define CALL_ZEN_UNTRAIN_RET	"call zen_untrain_ret"
+#else
+#define CALL_ZEN_UNTRAIN_RET	""
+#endif
+
 #ifdef __ASSEMBLY__
 
 /*
@@ -128,12 +134,6 @@
 .Lskip_rsb_\@:
 .endm
 
-#ifdef CONFIG_CPU_UNRET_ENTRY
-#define CALL_ZEN_UNTRAIN_RET	"call zen_untrain_ret"
-#else
-#define CALL_ZEN_UNTRAIN_RET	""
-#endif
-
 /*
  * Mitigate RETBleed for AMD/Hygon Zen uarch. Requires KERNEL CR3 because the
  * return thunk isn't mapped into the userspace tables (then again, AMD
@@ -169,6 +169,16 @@ extern void __x86_return_thunk(void);
 extern void zen_untrain_ret(void);
 extern void entry_ibpb(void);
 
+#if defined(CONFIG_CPU_UNRET_ENTRY) || defined(CONFIG_CPU_IBPB_ENTRY)
+# define UNTRAIN_RET						\
+	asm volatile(ALTERNATIVE_2(ANNOTATE_RETPOLINE_SAFE "nop", \
+		     CALL_ZEN_UNTRAIN_RET, X86_FEATURE_UNRET,	\
+		     "call entry_ibpb", X86_FEATURE_ENTRY_IBPB)	\
+		     ::)
+#else
+# define UNTRAIN_RET
+#endif
+
 #ifdef CONFIG_RETPOLINE
 
 #define GEN(reg) \
@@ -306,6 +316,7 @@ do {									\
 	alternative_msr_write(MSR_IA32_SPEC_CTRL,			\
 			      spec_ctrl_current(),			\
 			      X86_FEATURE_USE_IBRS_FW);			\
+	UNTRAIN_RET;							\
 	preempt_enable();						\
 } while (0)
 

-- 
Regards/Gruss,
    Boris.

SUSE Software Solutions Germany GmbH
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Martje Boudien Moerman
(HRB 36809, AG Nürnberg)

  parent reply	other threads:[~2022-07-18 16:29 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-15 19:45 [PATCH] efi/x86: use naked RET on mixed mode call wrapper Thadeu Lima de Souza Cascardo
2022-07-15 22:51 ` Guenter Roeck
2022-07-18 11:41 ` Peter Zijlstra
2022-07-18 13:59   ` Thadeu Lima de Souza Cascardo
2022-07-18 16:10     ` Peter Zijlstra
2022-07-18 16:28   ` Borislav Petkov [this message]
2022-07-18 16:38     ` Peter Zijlstra
2022-07-18 17:19       ` Andrew Cooper
2022-07-18 18:34     ` Linus Torvalds
2022-07-18 18:46       ` Borislav Petkov
2022-07-19 15:22         ` Ard Biesheuvel
2022-07-19 17:37           ` Borislav Petkov
2022-07-19 19:45 ` [tip: x86/urgent] x86/amd: Use IBPB for firmware calls tip-bot2 for 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=YtWKK2ZLib1R7itI@zn.tnic \
    --to=bp@suse.de \
    --cc=Andrew.Cooper3@citrix.com \
    --cc=ardb@kernel.org \
    --cc=cascardo@canonical.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=jpoimboe@kernel.org \
    --cc=linux-efi@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@roeck-us.net \
    --cc=peterz@infradead.org \
    --cc=stable@vger.kernel.org \
    --cc=tglx@linutronix.de \
    --cc=torvalds@linux-foundation.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