Skip to content

Commit 7118754

Browse files
KAGA-KOKOgregkh
authored andcommitted
x86/speculation: Add command line control for indirect branch speculation
commit fa1202e upstream 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 <tim.c.chen@linux.intel.com> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Reviewed-by: Ingo Molnar <mingo@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Andy Lutomirski <luto@kernel.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Jiri Kosina <jkosina@suse.cz> Cc: Tom Lendacky <thomas.lendacky@amd.com> Cc: Josh Poimboeuf <jpoimboe@redhat.com> Cc: Andrea Arcangeli <aarcange@redhat.com> Cc: David Woodhouse <dwmw@amazon.co.uk> Cc: Andi Kleen <ak@linux.intel.com> Cc: Dave Hansen <dave.hansen@intel.com> Cc: Casey Schaufler <casey.schaufler@intel.com> Cc: Asit Mallick <asit.k.mallick@intel.com> Cc: Arjan van de Ven <arjan@linux.intel.com> Cc: Jon Masters <jcm@redhat.com> Cc: Waiman Long <longman9394@gmail.com> Cc: Greg KH <gregkh@linuxfoundation.org> Cc: Dave Stewart <david.c.stewart@intel.com> Cc: Kees Cook <keescook@chromium.org> Cc: stable@vger.kernel.org Link: https://lkml.kernel.org/r/20181125185005.082720373@linutronix.de Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 8a34c70 commit 7118754

3 files changed

Lines changed: 156 additions & 19 deletions

File tree

‎Documentation/admin-guide/kernel-parameters.txt‎

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

41704170
spectre_v2= [X86] Control mitigation of Spectre variant 2
41714171
(indirect branch speculation) vulnerability.
4172+
The default operation protects the kernel from
4173+
user space attacks.
41724174

4173-
on - unconditionally enable
4174-
off - unconditionally disable
4175+
on - unconditionally enable, implies
4176+
spectre_v2_user=on
4177+
off - unconditionally disable, implies
4178+
spectre_v2_user=off
41754179
auto - kernel detects whether your CPU model is
41764180
vulnerable
41774181

@@ -4181,6 +4185,12 @@
41814185
CONFIG_RETPOLINE configuration option, and the
41824186
compiler with which the kernel was built.
41834187

4188+
Selecting 'on' will also enable the mitigation
4189+
against user space to user space task attacks.
4190+
4191+
Selecting 'off' will disable both the kernel and
4192+
the user space protections.
4193+
41844194
Specific mitigations can also be selected manually:
41854195

41864196
retpoline - replace indirect branches
@@ -4190,6 +4200,24 @@
41904200
Not specifying this option is equivalent to
41914201
spectre_v2=auto.
41924202

4203+
spectre_v2_user=
4204+
[X86] Control mitigation of Spectre variant 2
4205+
(indirect branch speculation) vulnerability between
4206+
user space tasks
4207+
4208+
on - Unconditionally enable mitigations. Is
4209+
enforced by spectre_v2=on
4210+
4211+
off - Unconditionally disable mitigations. Is
4212+
enforced by spectre_v2=off
4213+
4214+
auto - Kernel selects the mitigation depending on
4215+
the available CPU features and vulnerability.
4216+
Default is off.
4217+
4218+
Not specifying this option is equivalent to
4219+
spectre_v2_user=auto.
4220+
41934221
spec_store_bypass_disable=
41944222
[HW] Control Speculative Store Bypass (SSB) Disable mitigation
41954223
(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",
@@ -383,12 +487,6 @@ static void __init spectre_v2_select_mitigation(void)
383487
setup_force_cpu_cap(X86_FEATURE_RSB_CTXSW);
384488
pr_info("Spectre v2 / SpectreRSB mitigation: Filling RSB on context switch\n");
385489

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

506+
/* Set up IBPB and STIBP depending on the general spectre V2 command */
507+
spectre_v2_user_select_mitigation(cmd);
508+
408509
/* Enable STIBP if appropriate */
409510
arch_smt_update();
410511
}
411512

412513
static bool stibp_needed(void)
413514
{
414-
if (spectre_v2_enabled == SPECTRE_V2_NONE)
415-
return false;
416-
417515
/* Enhanced IBRS makes using STIBP unnecessary. */
418516
if (spectre_v2_enabled == SPECTRE_V2_IBRS_ENHANCED)
419517
return false;
420518

421-
if (!boot_cpu_has(X86_FEATURE_STIBP))
422-
return false;
423-
424-
return true;
519+
/* Check for strict user mitigation mode */
520+
return spectre_v2_user == SPECTRE_V2_USER_STRICT;
425521
}
426522

427523
static void update_stibp_msr(void *info)
@@ -842,10 +938,13 @@ static char *stibp_state(void)
842938
if (spectre_v2_enabled == SPECTRE_V2_IBRS_ENHANCED)
843939
return "";
844940

845-
if (x86_spec_ctrl_base & SPEC_CTRL_STIBP)
846-
return ", STIBP";
847-
else
848-
return "";
941+
switch (spectre_v2_user) {
942+
case SPECTRE_V2_USER_NONE:
943+
return ", STIBP: disabled";
944+
case SPECTRE_V2_USER_STRICT:
945+
return ", STIBP: forced";
946+
}
947+
return "";
849948
}
850949

851950
static char *ibpb_state(void)

0 commit comments

Comments
 (0)