Skip to content

Commit fa1202e

Browse files
committed
x86/speculation: Add command line control for indirect branch speculation
Add command line control for user space indirect branch speculation mitigations. The new option is: spectre_v2_user= The initial options are: - on: Unconditionally enabled - off: Unconditionally disabled -auto: Kernel selects mitigation (default off for now) When the spectre_v2= command line argument is either 'on' or 'off' this implies that the application to application control follows that state even if a contradicting spectre_v2_user= argument is supplied. Originally-by: Tim Chen <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Reviewed-by: Ingo Molnar <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Andy Lutomirski <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Jiri Kosina <[email protected]> Cc: Tom Lendacky <[email protected]> Cc: Josh Poimboeuf <[email protected]> Cc: Andrea Arcangeli <[email protected]> Cc: David Woodhouse <[email protected]> Cc: Andi Kleen <[email protected]> Cc: Dave Hansen <[email protected]> Cc: Casey Schaufler <[email protected]> Cc: Asit Mallick <[email protected]> Cc: Arjan van de Ven <[email protected]> Cc: Jon Masters <[email protected]> Cc: Waiman Long <[email protected]> Cc: Greg KH <[email protected]> Cc: Dave Stewart <[email protected]> Cc: Kees Cook <[email protected]> Cc: [email protected] Link: https://lkml.kernel.org/r/[email protected]
1 parent 495d470 commit fa1202e

File tree

3 files changed

+156
-19
lines changed

3 files changed

+156
-19
lines changed

Documentation/admin-guide/kernel-parameters.txt

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4194,9 +4194,13 @@
41944194

41954195
spectre_v2= [X86] Control mitigation of Spectre variant 2
41964196
(indirect branch speculation) vulnerability.
4197+
The default operation protects the kernel from
4198+
user space attacks.
41974199

4198-
on - unconditionally enable
4199-
off - unconditionally disable
4200+
on - unconditionally enable, implies
4201+
spectre_v2_user=on
4202+
off - unconditionally disable, implies
4203+
spectre_v2_user=off
42004204
auto - kernel detects whether your CPU model is
42014205
vulnerable
42024206

@@ -4206,6 +4210,12 @@
42064210
CONFIG_RETPOLINE configuration option, and the
42074211
compiler with which the kernel was built.
42084212

4213+
Selecting 'on' will also enable the mitigation
4214+
against user space to user space task attacks.
4215+
4216+
Selecting 'off' will disable both the kernel and
4217+
the user space protections.
4218+
42094219
Specific mitigations can also be selected manually:
42104220

42114221
retpoline - replace indirect branches
@@ -4215,6 +4225,24 @@
42154225
Not specifying this option is equivalent to
42164226
spectre_v2=auto.
42174227

4228+
spectre_v2_user=
4229+
[X86] Control mitigation of Spectre variant 2
4230+
(indirect branch speculation) vulnerability between
4231+
user space tasks
4232+
4233+
on - Unconditionally enable mitigations. Is
4234+
enforced by spectre_v2=on
4235+
4236+
off - Unconditionally disable mitigations. Is
4237+
enforced by spectre_v2=off
4238+
4239+
auto - Kernel selects the mitigation depending on
4240+
the available CPU features and vulnerability.
4241+
Default is off.
4242+
4243+
Not specifying this option is equivalent to
4244+
spectre_v2_user=auto.
4245+
42184246
spec_store_bypass_disable=
42194247
[HW] Control Speculative Store Bypass (SSB) Disable mitigation
42204248
(Speculative Store Bypass vulnerability)

arch/x86/include/asm/nospec-branch.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#ifndef _ASM_X86_NOSPEC_BRANCH_H_
44
#define _ASM_X86_NOSPEC_BRANCH_H_
55

6+
#include <linux/static_key.h>
7+
68
#include <asm/alternative.h>
79
#include <asm/alternative-asm.h>
810
#include <asm/cpufeatures.h>
@@ -226,6 +228,12 @@ enum spectre_v2_mitigation {
226228
SPECTRE_V2_IBRS_ENHANCED,
227229
};
228230

231+
/* The indirect branch speculation control variants */
232+
enum spectre_v2_user_mitigation {
233+
SPECTRE_V2_USER_NONE,
234+
SPECTRE_V2_USER_STRICT,
235+
};
236+
229237
/* The Speculative Store Bypass disable variants */
230238
enum ssb_mitigation {
231239
SPEC_STORE_BYPASS_NONE,
@@ -303,6 +311,8 @@ do { \
303311
preempt_enable(); \
304312
} while (0)
305313

314+
DECLARE_STATIC_KEY_FALSE(switch_to_cond_stibp);
315+
306316
#endif /* __ASSEMBLY__ */
307317

308318
/*

arch/x86/kernel/cpu/bugs.c

Lines changed: 116 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ static u64 __ro_after_init x86_spec_ctrl_mask = SPEC_CTRL_IBRS;
5454
u64 __ro_after_init x86_amd_ls_cfg_base;
5555
u64 __ro_after_init x86_amd_ls_cfg_ssbd_mask;
5656

57+
/* Control conditional STIPB in switch_to() */
58+
DEFINE_STATIC_KEY_FALSE(switch_to_cond_stibp);
59+
5760
void __init check_bugs(void)
5861
{
5962
identify_boot_cpu();
@@ -199,6 +202,9 @@ static void x86_amd_ssb_disable(void)
199202
static enum spectre_v2_mitigation spectre_v2_enabled __ro_after_init =
200203
SPECTRE_V2_NONE;
201204

205+
static enum spectre_v2_user_mitigation spectre_v2_user __ro_after_init =
206+
SPECTRE_V2_USER_NONE;
207+
202208
#ifdef RETPOLINE
203209
static bool spectre_v2_bad_module;
204210

@@ -237,6 +243,104 @@ enum spectre_v2_mitigation_cmd {
237243
SPECTRE_V2_CMD_RETPOLINE_AMD,
238244
};
239245

246+
enum spectre_v2_user_cmd {
247+
SPECTRE_V2_USER_CMD_NONE,
248+
SPECTRE_V2_USER_CMD_AUTO,
249+
SPECTRE_V2_USER_CMD_FORCE,
250+
};
251+
252+
static const char * const spectre_v2_user_strings[] = {
253+
[SPECTRE_V2_USER_NONE] = "User space: Vulnerable",
254+
[SPECTRE_V2_USER_STRICT] = "User space: Mitigation: STIBP protection",
255+
};
256+
257+
static const struct {
258+
const char *option;
259+
enum spectre_v2_user_cmd cmd;
260+
bool secure;
261+
} v2_user_options[] __initdata = {
262+
{ "auto", SPECTRE_V2_USER_CMD_AUTO, false },
263+
{ "off", SPECTRE_V2_USER_CMD_NONE, false },
264+
{ "on", SPECTRE_V2_USER_CMD_FORCE, true },
265+
};
266+
267+
static void __init spec_v2_user_print_cond(const char *reason, bool secure)
268+
{
269+
if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2) != secure)
270+
pr_info("spectre_v2_user=%s forced on command line.\n", reason);
271+
}
272+
273+
static enum spectre_v2_user_cmd __init
274+
spectre_v2_parse_user_cmdline(enum spectre_v2_mitigation_cmd v2_cmd)
275+
{
276+
char arg[20];
277+
int ret, i;
278+
279+
switch (v2_cmd) {
280+
case SPECTRE_V2_CMD_NONE:
281+
return SPECTRE_V2_USER_CMD_NONE;
282+
case SPECTRE_V2_CMD_FORCE:
283+
return SPECTRE_V2_USER_CMD_FORCE;
284+
default:
285+
break;
286+
}
287+
288+
ret = cmdline_find_option(boot_command_line, "spectre_v2_user",
289+
arg, sizeof(arg));
290+
if (ret < 0)
291+
return SPECTRE_V2_USER_CMD_AUTO;
292+
293+
for (i = 0; i < ARRAY_SIZE(v2_user_options); i++) {
294+
if (match_option(arg, ret, v2_user_options[i].option)) {
295+
spec_v2_user_print_cond(v2_user_options[i].option,
296+
v2_user_options[i].secure);
297+
return v2_user_options[i].cmd;
298+
}
299+
}
300+
301+
pr_err("Unknown user space protection option (%s). Switching to AUTO select\n", arg);
302+
return SPECTRE_V2_USER_CMD_AUTO;
303+
}
304+
305+
static void __init
306+
spectre_v2_user_select_mitigation(enum spectre_v2_mitigation_cmd v2_cmd)
307+
{
308+
enum spectre_v2_user_mitigation mode = SPECTRE_V2_USER_NONE;
309+
bool smt_possible = IS_ENABLED(CONFIG_SMP);
310+
311+
if (!boot_cpu_has(X86_FEATURE_IBPB) && !boot_cpu_has(X86_FEATURE_STIBP))
312+
return;
313+
314+
if (cpu_smt_control == CPU_SMT_FORCE_DISABLED ||
315+
cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
316+
smt_possible = false;
317+
318+
switch (spectre_v2_parse_user_cmdline(v2_cmd)) {
319+
case SPECTRE_V2_USER_CMD_AUTO:
320+
case SPECTRE_V2_USER_CMD_NONE:
321+
goto set_mode;
322+
case SPECTRE_V2_USER_CMD_FORCE:
323+
mode = SPECTRE_V2_USER_STRICT;
324+
break;
325+
}
326+
327+
/* Initialize Indirect Branch Prediction Barrier */
328+
if (boot_cpu_has(X86_FEATURE_IBPB)) {
329+
setup_force_cpu_cap(X86_FEATURE_USE_IBPB);
330+
pr_info("Spectre v2 mitigation: Enabling Indirect Branch Prediction Barrier\n");
331+
}
332+
333+
/* If enhanced IBRS is enabled no STIPB required */
334+
if (spectre_v2_enabled == SPECTRE_V2_IBRS_ENHANCED)
335+
return;
336+
337+
set_mode:
338+
spectre_v2_user = mode;
339+
/* Only print the STIBP mode when SMT possible */
340+
if (smt_possible)
341+
pr_info("%s\n", spectre_v2_user_strings[mode]);
342+
}
343+
240344
static const char * const spectre_v2_strings[] = {
241345
[SPECTRE_V2_NONE] = "Vulnerable",
242346
[SPECTRE_V2_RETPOLINE_GENERIC] = "Mitigation: Full generic retpoline",
@@ -385,12 +489,6 @@ static void __init spectre_v2_select_mitigation(void)
385489
setup_force_cpu_cap(X86_FEATURE_RSB_CTXSW);
386490
pr_info("Spectre v2 / SpectreRSB mitigation: Filling RSB on context switch\n");
387491

388-
/* Initialize Indirect Branch Prediction Barrier if supported */
389-
if (boot_cpu_has(X86_FEATURE_IBPB)) {
390-
setup_force_cpu_cap(X86_FEATURE_USE_IBPB);
391-
pr_info("Spectre v2 mitigation: Enabling Indirect Branch Prediction Barrier\n");
392-
}
393-
394492
/*
395493
* Retpoline means the kernel is safe because it has no indirect
396494
* branches. Enhanced IBRS protects firmware too, so, enable restricted
@@ -407,23 +505,21 @@ static void __init spectre_v2_select_mitigation(void)
407505
pr_info("Enabling Restricted Speculation for firmware calls\n");
408506
}
409507

508+
/* Set up IBPB and STIBP depending on the general spectre V2 command */
509+
spectre_v2_user_select_mitigation(cmd);
510+
410511
/* Enable STIBP if appropriate */
411512
arch_smt_update();
412513
}
413514

414515
static bool stibp_needed(void)
415516
{
416-
if (spectre_v2_enabled == SPECTRE_V2_NONE)
417-
return false;
418-
419517
/* Enhanced IBRS makes using STIBP unnecessary. */
420518
if (spectre_v2_enabled == SPECTRE_V2_IBRS_ENHANCED)
421519
return false;
422520

423-
if (!boot_cpu_has(X86_FEATURE_STIBP))
424-
return false;
425-
426-
return true;
521+
/* Check for strict user mitigation mode */
522+
return spectre_v2_user == SPECTRE_V2_USER_STRICT;
427523
}
428524

429525
static void update_stibp_msr(void *info)
@@ -844,10 +940,13 @@ static char *stibp_state(void)
844940
if (spectre_v2_enabled == SPECTRE_V2_IBRS_ENHANCED)
845941
return "";
846942

847-
if (x86_spec_ctrl_base & SPEC_CTRL_STIBP)
848-
return ", STIBP";
849-
else
850-
return "";
943+
switch (spectre_v2_user) {
944+
case SPECTRE_V2_USER_NONE:
945+
return ", STIBP: disabled";
946+
case SPECTRE_V2_USER_STRICT:
947+
return ", STIBP: forced";
948+
}
949+
return "";
851950
}
852951

853952
static char *ibpb_state(void)

0 commit comments

Comments
 (0)