linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH RESEND] x86/speculation: Fix user-mode spectre-v2 protection with KERNEL_IBRS
@ 2023-02-20 12:01 KP Singh
  2023-02-20 12:13 ` Josh Poimboeuf
  2023-02-27 19:59 ` [tip: x86/urgent] x86/speculation: Allow enabling STIBP with legacy IBRS tip-bot2 for KP Singh
  0 siblings, 2 replies; 38+ messages in thread
From: KP Singh @ 2023-02-20 12:01 UTC (permalink / raw)
  To: linux-kernel
  Cc: pjt, evn, jpoimboe, tglx, mingo, bp, dave.hansen, x86, hpa,
	peterz, pawan.kumar.gupta, kim.phillips, alexandre.chartre,
	daniel.sneddon, José Oliveira, Rodrigo Branco,
	Alexandra Sandulescu, Jim Mattson, stable, KP Singh

With the introduction of KERNEL_IBRS, STIBP is no longer needed
to prevent cross thread training in the kernel space. When KERNEL_IBRS
was added, it also disabled the user-mode protections for spectre_v2.
KERNEL_IBRS does not mitigate cross thread training in the userspace.

In order to demonstrate the issue, one needs to avoid syscalls in the
victim as syscalls can shorten the window size due to
a user -> kernel -> user transition which sets the
IBRS bit when entering kernel space and clearing any training the
attacker may have done.

Allow users to select a spectre_v2_user mitigation (STIBP always on,
opt-in via prctl) when KERNEL_IBRS is enabled.

Reported-by: José Oliveira <joseloliveira11@gmail.com>
Reported-by: Rodrigo Branco <rodrigo@kernelhacking.com>
Reviewed-by: Alexandra Sandulescu <aesa@google.com>
Reviewed-by: Jim Mattson <jmattson@google.com>
Fixes: 7c693f54c873 ("x86/speculation: Add spectre_v2=ibrs option to support Kernel IBRS")
Cc: stable@vger.kernel.org
Signed-off-by: KP Singh <kpsingh@kernel.org>
---
 arch/x86/kernel/cpu/bugs.c | 25 +++++++++++++++++--------
 1 file changed, 17 insertions(+), 8 deletions(-)

diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c
index bca0bd8f4846..b05ca1575d81 100644
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -1132,6 +1132,19 @@ static inline bool spectre_v2_in_ibrs_mode(enum spectre_v2_mitigation mode)
 	       mode == SPECTRE_V2_EIBRS_LFENCE;
 }
 
+static inline bool spectre_v2_user_no_stibp(enum spectre_v2_mitigation mode)
+{
+	/* When IBRS or enhanced IBRS is enabled, STIBP is not needed.
+	 *
+	 * However, With KERNEL_IBRS, the IBRS bit is cleared on return
+	 * to user and the user-mode code needs to be able to enable protection
+	 * from cross-thread training, either by always enabling STIBP or
+	 * by enabling it via prctl.
+	 */
+	return (spectre_v2_in_ibrs_mode(mode) &&
+		!cpu_feature_enabled(X86_FEATURE_KERNEL_IBRS));
+}
+
 static void __init
 spectre_v2_user_select_mitigation(void)
 {
@@ -1193,13 +1206,8 @@ spectre_v2_user_select_mitigation(void)
 			"always-on" : "conditional");
 	}
 
-	/*
-	 * If no STIBP, IBRS or enhanced IBRS is enabled, or SMT impossible,
-	 * STIBP is not required.
-	 */
-	if (!boot_cpu_has(X86_FEATURE_STIBP) ||
-	    !smt_possible ||
-	    spectre_v2_in_ibrs_mode(spectre_v2_enabled))
+	if (!boot_cpu_has(X86_FEATURE_STIBP) || !smt_possible ||
+	    spectre_v2_user_no_stibp(spectre_v2_enabled))
 		return;
 
 	/*
@@ -1496,6 +1504,7 @@ static void __init spectre_v2_select_mitigation(void)
 		break;
 
 	case SPECTRE_V2_IBRS:
+		pr_err("enabling KERNEL_IBRS");
 		setup_force_cpu_cap(X86_FEATURE_KERNEL_IBRS);
 		if (boot_cpu_has(X86_FEATURE_IBRS_ENHANCED))
 			pr_warn(SPECTRE_V2_IBRS_PERF_MSG);
@@ -2327,7 +2336,7 @@ static ssize_t mmio_stale_data_show_state(char *buf)
 
 static char *stibp_state(void)
 {
-	if (spectre_v2_in_ibrs_mode(spectre_v2_enabled))
+	if (spectre_v2_user_no_stibp(spectre_v2_enabled))
 		return "";
 
 	switch (spectre_v2_user_stibp) {
-- 
2.39.2.637.g21b0678d19-goog


^ permalink raw reply related	[flat|nested] 38+ messages in thread

end of thread, other threads:[~2023-02-27 19:59 UTC | newest]

Thread overview: 38+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-20 12:01 [PATCH RESEND] x86/speculation: Fix user-mode spectre-v2 protection with KERNEL_IBRS KP Singh
2023-02-20 12:13 ` Josh Poimboeuf
2023-02-20 12:20   ` KP Singh
2023-02-20 12:34     ` KP Singh
2023-02-20 14:31       ` Borislav Petkov
2023-02-20 15:38         ` Dave Hansen
2023-02-20 19:57         ` Andrew Cooper
2023-02-20 21:10           ` Borislav Petkov
2023-02-20 23:01             ` KP Singh
2023-02-20 23:30             ` Andrew Cooper
2023-02-20 23:45               ` KP Singh
2023-02-21 18:52                 ` KP Singh
2023-02-21 10:59               ` Borislav Petkov
2023-02-20 16:34       ` Josh Poimboeuf
2023-02-20 17:46         ` Borislav Petkov
2023-02-20 17:59           ` Josh Poimboeuf
2023-02-20 18:01             ` KP Singh
2023-02-20 18:22               ` Borislav Petkov
2023-02-20 18:44                 ` KP Singh
2023-02-20 18:51                   ` Borislav Petkov
2023-02-20 18:56                     ` KP Singh
2023-02-20 19:02                       ` Borislav Petkov
2023-02-20 19:10                         ` KP Singh
2023-02-20 18:27               ` [PATCH] x86/bugs: Allow STIBP with IBRS Josh Poimboeuf
2023-02-20 18:33                 ` KP Singh
2023-02-20 18:59                   ` Josh Poimboeuf
2023-02-20 19:04                     ` KP Singh
2023-02-20 19:19                       ` Josh Poimboeuf
2023-02-20 18:34                 ` Borislav Petkov
2023-02-20 19:09                   ` Josh Poimboeuf
2023-02-20 19:16                     ` KP Singh
2023-02-20 19:35                       ` Josh Poimboeuf
2023-02-20 19:38                         ` KP Singh
2023-02-20 19:20                     ` Borislav Petkov
2023-02-22  1:20                     ` Pawan Gupta
2023-02-22  1:26                       ` KP Singh
2023-02-22  1:38                         ` Pawan Gupta
2023-02-27 19:59 ` [tip: x86/urgent] x86/speculation: Allow enabling STIBP with legacy IBRS tip-bot2 for KP Singh

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).