]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - arch/x86/kernel/cpu/bugs.c
x86/speculation: Prevent stale SPEC_CTRL msr content
[mirror_ubuntu-bionic-kernel.git] / arch / x86 / kernel / cpu / bugs.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 1994 Linus Torvalds
4 *
5 * Cyrix stuff, June 1998 by:
6 * - Rafael R. Reilova (moved everything from head.S),
7 * <rreilova@ececs.uc.edu>
8 * - Channing Corn (tests & fixes),
9 * - Andrew D. Balsa (code cleanup).
10 */
11 #include <linux/init.h>
12 #include <linux/utsname.h>
13 #include <linux/cpu.h>
14 #include <linux/module.h>
15 #include <linux/nospec.h>
16 #include <linux/prctl.h>
17 #include <linux/sched/smt.h>
18
19 #include <asm/spec-ctrl.h>
20 #include <asm/cmdline.h>
21 #include <asm/bugs.h>
22 #include <asm/processor.h>
23 #include <asm/processor-flags.h>
24 #include <asm/fpu/internal.h>
25 #include <asm/msr.h>
26 #include <asm/vmx.h>
27 #include <asm/paravirt.h>
28 #include <asm/alternative.h>
29 #include <asm/pgtable.h>
30 #include <asm/set_memory.h>
31 #include <asm/intel-family.h>
32 #include <asm/e820/api.h>
33
34 static void __init spectre_v2_select_mitigation(void);
35 static void __init ssb_select_mitigation(void);
36 static void __init l1tf_select_mitigation(void);
37
38 /* The base value of the SPEC_CTRL MSR that always has to be preserved. */
39 u64 x86_spec_ctrl_base;
40 EXPORT_SYMBOL_GPL(x86_spec_ctrl_base);
41 static DEFINE_MUTEX(spec_ctrl_mutex);
42
43 /*
44 * The vendor and possibly platform specific bits which can be modified in
45 * x86_spec_ctrl_base.
46 */
47 static u64 __ro_after_init x86_spec_ctrl_mask = SPEC_CTRL_IBRS;
48
49 /*
50 * AMD specific MSR info for Speculative Store Bypass control.
51 * x86_amd_ls_cfg_ssbd_mask is initialized in identify_boot_cpu().
52 */
53 u64 __ro_after_init x86_amd_ls_cfg_base;
54 u64 __ro_after_init x86_amd_ls_cfg_ssbd_mask;
55
56 /* Control conditional STIPB in switch_to() */
57 DEFINE_STATIC_KEY_FALSE(switch_to_cond_stibp);
58 /* Control conditional IBPB in switch_mm() */
59 DEFINE_STATIC_KEY_FALSE(switch_mm_cond_ibpb);
60 /* Control unconditional IBPB in switch_mm() */
61 DEFINE_STATIC_KEY_FALSE(switch_mm_always_ibpb);
62
63 void __init check_bugs(void)
64 {
65 identify_boot_cpu();
66
67 /*
68 * identify_boot_cpu() initialized SMT support information, let the
69 * core code know.
70 */
71 cpu_smt_check_topology_early();
72
73 if (!IS_ENABLED(CONFIG_SMP)) {
74 pr_info("CPU: ");
75 print_cpu_info(&boot_cpu_data);
76 }
77
78 /*
79 * Read the SPEC_CTRL MSR to account for reserved bits which may
80 * have unknown values. AMD64_LS_CFG MSR is cached in the early AMD
81 * init code as it is not enumerated and depends on the family.
82 */
83 if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
84 rdmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
85
86 /* Allow STIBP in MSR_SPEC_CTRL if supported */
87 if (boot_cpu_has(X86_FEATURE_STIBP))
88 x86_spec_ctrl_mask |= SPEC_CTRL_STIBP;
89
90 /* Select the proper spectre mitigation before patching alternatives */
91 spectre_v2_select_mitigation();
92
93 /*
94 * Select proper mitigation for any exposure to the Speculative Store
95 * Bypass vulnerability.
96 */
97 ssb_select_mitigation();
98
99 l1tf_select_mitigation();
100
101 #ifdef CONFIG_X86_32
102 /*
103 * Check whether we are able to run this kernel safely on SMP.
104 *
105 * - i386 is no longer supported.
106 * - In order to run on anything without a TSC, we need to be
107 * compiled for a i486.
108 */
109 if (boot_cpu_data.x86 < 4)
110 panic("Kernel requires i486+ for 'invlpg' and other features");
111
112 init_utsname()->machine[1] =
113 '0' + (boot_cpu_data.x86 > 6 ? 6 : boot_cpu_data.x86);
114 alternative_instructions();
115
116 fpu__init_check_bugs();
117 #else /* CONFIG_X86_64 */
118 alternative_instructions();
119
120 /*
121 * Make sure the first 2MB area is not mapped by huge pages
122 * There are typically fixed size MTRRs in there and overlapping
123 * MTRRs into large pages causes slow downs.
124 *
125 * Right now we don't do that with gbpages because there seems
126 * very little benefit for that case.
127 */
128 if (!direct_gbpages)
129 set_memory_4k((unsigned long)__va(0), 1);
130 #endif
131 }
132
133 void
134 x86_virt_spec_ctrl(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl, bool setguest)
135 {
136 u64 msrval, guestval, hostval = x86_spec_ctrl_base;
137 struct thread_info *ti = current_thread_info();
138
139 /* Is MSR_SPEC_CTRL implemented ? */
140 if (static_cpu_has(X86_FEATURE_MSR_SPEC_CTRL)) {
141 /*
142 * Restrict guest_spec_ctrl to supported values. Clear the
143 * modifiable bits in the host base value and or the
144 * modifiable bits from the guest value.
145 */
146 guestval = hostval & ~x86_spec_ctrl_mask;
147 guestval |= guest_spec_ctrl & x86_spec_ctrl_mask;
148
149 /* SSBD controlled in MSR_SPEC_CTRL */
150 if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) ||
151 static_cpu_has(X86_FEATURE_AMD_SSBD))
152 hostval |= ssbd_tif_to_spec_ctrl(ti->flags);
153
154 /* Conditional STIBP enabled? */
155 if (static_branch_unlikely(&switch_to_cond_stibp))
156 hostval |= stibp_tif_to_spec_ctrl(ti->flags);
157
158 if (hostval != guestval) {
159 msrval = setguest ? guestval : hostval;
160 wrmsrl(MSR_IA32_SPEC_CTRL, msrval);
161 }
162 }
163
164 /*
165 * If SSBD is not handled in MSR_SPEC_CTRL on AMD, update
166 * MSR_AMD64_L2_CFG or MSR_VIRT_SPEC_CTRL if supported.
167 */
168 if (!static_cpu_has(X86_FEATURE_LS_CFG_SSBD) &&
169 !static_cpu_has(X86_FEATURE_VIRT_SSBD))
170 return;
171
172 /*
173 * If the host has SSBD mitigation enabled, force it in the host's
174 * virtual MSR value. If its not permanently enabled, evaluate
175 * current's TIF_SSBD thread flag.
176 */
177 if (static_cpu_has(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE))
178 hostval = SPEC_CTRL_SSBD;
179 else
180 hostval = ssbd_tif_to_spec_ctrl(ti->flags);
181
182 /* Sanitize the guest value */
183 guestval = guest_virt_spec_ctrl & SPEC_CTRL_SSBD;
184
185 if (hostval != guestval) {
186 unsigned long tif;
187
188 tif = setguest ? ssbd_spec_ctrl_to_tif(guestval) :
189 ssbd_spec_ctrl_to_tif(hostval);
190
191 speculation_ctrl_update(tif);
192 }
193 }
194 EXPORT_SYMBOL_GPL(x86_virt_spec_ctrl);
195
196 static void x86_amd_ssb_disable(void)
197 {
198 u64 msrval = x86_amd_ls_cfg_base | x86_amd_ls_cfg_ssbd_mask;
199
200 if (boot_cpu_has(X86_FEATURE_VIRT_SSBD))
201 wrmsrl(MSR_AMD64_VIRT_SPEC_CTRL, SPEC_CTRL_SSBD);
202 else if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD))
203 wrmsrl(MSR_AMD64_LS_CFG, msrval);
204 }
205
206 #undef pr_fmt
207 #define pr_fmt(fmt) "Spectre V2 : " fmt
208
209 static enum spectre_v2_mitigation spectre_v2_enabled __ro_after_init =
210 SPECTRE_V2_NONE;
211
212 static enum spectre_v2_user_mitigation spectre_v2_user __ro_after_init =
213 SPECTRE_V2_USER_NONE;
214
215 #ifdef RETPOLINE
216 static bool spectre_v2_bad_module;
217
218 bool retpoline_module_ok(bool has_retpoline)
219 {
220 if (spectre_v2_enabled == SPECTRE_V2_NONE || has_retpoline)
221 return true;
222
223 pr_err("System may be vulnerable to spectre v2\n");
224 spectre_v2_bad_module = true;
225 return false;
226 }
227
228 static inline const char *spectre_v2_module_string(void)
229 {
230 return spectre_v2_bad_module ? " - vulnerable module loaded" : "";
231 }
232 #else
233 static inline const char *spectre_v2_module_string(void) { return ""; }
234 #endif
235
236 static inline bool match_option(const char *arg, int arglen, const char *opt)
237 {
238 int len = strlen(opt);
239
240 return len == arglen && !strncmp(arg, opt, len);
241 }
242
243 /* The kernel command line selection for spectre v2 */
244 enum spectre_v2_mitigation_cmd {
245 SPECTRE_V2_CMD_NONE,
246 SPECTRE_V2_CMD_AUTO,
247 SPECTRE_V2_CMD_FORCE,
248 SPECTRE_V2_CMD_RETPOLINE,
249 SPECTRE_V2_CMD_RETPOLINE_GENERIC,
250 SPECTRE_V2_CMD_RETPOLINE_AMD,
251 };
252
253 enum spectre_v2_user_cmd {
254 SPECTRE_V2_USER_CMD_NONE,
255 SPECTRE_V2_USER_CMD_AUTO,
256 SPECTRE_V2_USER_CMD_FORCE,
257 };
258
259 static const char * const spectre_v2_user_strings[] = {
260 [SPECTRE_V2_USER_NONE] = "User space: Vulnerable",
261 [SPECTRE_V2_USER_STRICT] = "User space: Mitigation: STIBP protection",
262 };
263
264 static const struct {
265 const char *option;
266 enum spectre_v2_user_cmd cmd;
267 bool secure;
268 } v2_user_options[] __initdata = {
269 { "auto", SPECTRE_V2_USER_CMD_AUTO, false },
270 { "off", SPECTRE_V2_USER_CMD_NONE, false },
271 { "on", SPECTRE_V2_USER_CMD_FORCE, true },
272 };
273
274 static void __init spec_v2_user_print_cond(const char *reason, bool secure)
275 {
276 if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2) != secure)
277 pr_info("spectre_v2_user=%s forced on command line.\n", reason);
278 }
279
280 static enum spectre_v2_user_cmd __init
281 spectre_v2_parse_user_cmdline(enum spectre_v2_mitigation_cmd v2_cmd)
282 {
283 char arg[20];
284 int ret, i;
285
286 switch (v2_cmd) {
287 case SPECTRE_V2_CMD_NONE:
288 return SPECTRE_V2_USER_CMD_NONE;
289 case SPECTRE_V2_CMD_FORCE:
290 return SPECTRE_V2_USER_CMD_FORCE;
291 default:
292 break;
293 }
294
295 ret = cmdline_find_option(boot_command_line, "spectre_v2_user",
296 arg, sizeof(arg));
297 if (ret < 0)
298 return SPECTRE_V2_USER_CMD_AUTO;
299
300 for (i = 0; i < ARRAY_SIZE(v2_user_options); i++) {
301 if (match_option(arg, ret, v2_user_options[i].option)) {
302 spec_v2_user_print_cond(v2_user_options[i].option,
303 v2_user_options[i].secure);
304 return v2_user_options[i].cmd;
305 }
306 }
307
308 pr_err("Unknown user space protection option (%s). Switching to AUTO select\n", arg);
309 return SPECTRE_V2_USER_CMD_AUTO;
310 }
311
312 static void __init
313 spectre_v2_user_select_mitigation(enum spectre_v2_mitigation_cmd v2_cmd)
314 {
315 enum spectre_v2_user_mitigation mode = SPECTRE_V2_USER_NONE;
316 bool smt_possible = IS_ENABLED(CONFIG_SMP);
317
318 if (!boot_cpu_has(X86_FEATURE_IBPB) && !boot_cpu_has(X86_FEATURE_STIBP))
319 return;
320
321 if (cpu_smt_control == CPU_SMT_FORCE_DISABLED ||
322 cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
323 smt_possible = false;
324
325 switch (spectre_v2_parse_user_cmdline(v2_cmd)) {
326 case SPECTRE_V2_USER_CMD_AUTO:
327 case SPECTRE_V2_USER_CMD_NONE:
328 goto set_mode;
329 case SPECTRE_V2_USER_CMD_FORCE:
330 mode = SPECTRE_V2_USER_STRICT;
331 break;
332 }
333
334 /* Initialize Indirect Branch Prediction Barrier */
335 if (boot_cpu_has(X86_FEATURE_IBPB)) {
336 setup_force_cpu_cap(X86_FEATURE_USE_IBPB);
337
338 switch (mode) {
339 case SPECTRE_V2_USER_STRICT:
340 static_branch_enable(&switch_mm_always_ibpb);
341 break;
342 default:
343 break;
344 }
345
346 pr_info("mitigation: Enabling %s Indirect Branch Prediction Barrier\n",
347 mode == SPECTRE_V2_USER_STRICT ? "always-on" : "conditional");
348 }
349
350 /* If enhanced IBRS is enabled no STIPB required */
351 if (spectre_v2_enabled == SPECTRE_V2_IBRS_ENHANCED)
352 return;
353
354 set_mode:
355 spectre_v2_user = mode;
356 /* Only print the STIBP mode when SMT possible */
357 if (smt_possible)
358 pr_info("%s\n", spectre_v2_user_strings[mode]);
359 }
360
361 static const char * const spectre_v2_strings[] = {
362 [SPECTRE_V2_NONE] = "Vulnerable",
363 [SPECTRE_V2_RETPOLINE_GENERIC] = "Mitigation: Full generic retpoline",
364 [SPECTRE_V2_RETPOLINE_AMD] = "Mitigation: Full AMD retpoline",
365 [SPECTRE_V2_IBRS_ENHANCED] = "Mitigation: Enhanced IBRS",
366 };
367
368 static const struct {
369 const char *option;
370 enum spectre_v2_mitigation_cmd cmd;
371 bool secure;
372 } mitigation_options[] __initdata = {
373 { "off", SPECTRE_V2_CMD_NONE, false },
374 { "on", SPECTRE_V2_CMD_FORCE, true },
375 { "retpoline", SPECTRE_V2_CMD_RETPOLINE, false },
376 { "retpoline,amd", SPECTRE_V2_CMD_RETPOLINE_AMD, false },
377 { "retpoline,generic", SPECTRE_V2_CMD_RETPOLINE_GENERIC, false },
378 { "auto", SPECTRE_V2_CMD_AUTO, false },
379 };
380
381 static void __init spec_v2_print_cond(const char *reason, bool secure)
382 {
383 if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2) != secure)
384 pr_info("%s selected on command line.\n", reason);
385 }
386
387 static enum spectre_v2_mitigation_cmd __init spectre_v2_parse_cmdline(void)
388 {
389 enum spectre_v2_mitigation_cmd cmd = SPECTRE_V2_CMD_AUTO;
390 char arg[20];
391 int ret, i;
392
393 if (cmdline_find_option_bool(boot_command_line, "nospectre_v2"))
394 return SPECTRE_V2_CMD_NONE;
395
396 ret = cmdline_find_option(boot_command_line, "spectre_v2", arg, sizeof(arg));
397 if (ret < 0)
398 return SPECTRE_V2_CMD_AUTO;
399
400 for (i = 0; i < ARRAY_SIZE(mitigation_options); i++) {
401 if (!match_option(arg, ret, mitigation_options[i].option))
402 continue;
403 cmd = mitigation_options[i].cmd;
404 break;
405 }
406
407 if (i >= ARRAY_SIZE(mitigation_options)) {
408 pr_err("unknown option (%s). Switching to AUTO select\n", arg);
409 return SPECTRE_V2_CMD_AUTO;
410 }
411
412 if ((cmd == SPECTRE_V2_CMD_RETPOLINE ||
413 cmd == SPECTRE_V2_CMD_RETPOLINE_AMD ||
414 cmd == SPECTRE_V2_CMD_RETPOLINE_GENERIC) &&
415 !IS_ENABLED(CONFIG_RETPOLINE)) {
416 pr_err("%s selected but not compiled in. Switching to AUTO select\n", mitigation_options[i].option);
417 return SPECTRE_V2_CMD_AUTO;
418 }
419
420 if (cmd == SPECTRE_V2_CMD_RETPOLINE_AMD &&
421 boot_cpu_data.x86_vendor != X86_VENDOR_AMD) {
422 pr_err("retpoline,amd selected but CPU is not AMD. Switching to AUTO select\n");
423 return SPECTRE_V2_CMD_AUTO;
424 }
425
426 spec_v2_print_cond(mitigation_options[i].option,
427 mitigation_options[i].secure);
428 return cmd;
429 }
430
431 static void __init spectre_v2_select_mitigation(void)
432 {
433 enum spectre_v2_mitigation_cmd cmd = spectre_v2_parse_cmdline();
434 enum spectre_v2_mitigation mode = SPECTRE_V2_NONE;
435
436 /*
437 * If the CPU is not affected and the command line mode is NONE or AUTO
438 * then nothing to do.
439 */
440 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2) &&
441 (cmd == SPECTRE_V2_CMD_NONE || cmd == SPECTRE_V2_CMD_AUTO))
442 return;
443
444 switch (cmd) {
445 case SPECTRE_V2_CMD_NONE:
446 return;
447
448 case SPECTRE_V2_CMD_FORCE:
449 case SPECTRE_V2_CMD_AUTO:
450 if (boot_cpu_has(X86_FEATURE_IBRS_ENHANCED)) {
451 mode = SPECTRE_V2_IBRS_ENHANCED;
452 /* Force it so VMEXIT will restore correctly */
453 x86_spec_ctrl_base |= SPEC_CTRL_IBRS;
454 wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
455 goto specv2_set_mode;
456 }
457 if (IS_ENABLED(CONFIG_RETPOLINE))
458 goto retpoline_auto;
459 break;
460 case SPECTRE_V2_CMD_RETPOLINE_AMD:
461 if (IS_ENABLED(CONFIG_RETPOLINE))
462 goto retpoline_amd;
463 break;
464 case SPECTRE_V2_CMD_RETPOLINE_GENERIC:
465 if (IS_ENABLED(CONFIG_RETPOLINE))
466 goto retpoline_generic;
467 break;
468 case SPECTRE_V2_CMD_RETPOLINE:
469 if (IS_ENABLED(CONFIG_RETPOLINE))
470 goto retpoline_auto;
471 break;
472 }
473 pr_err("Spectre mitigation: kernel not compiled with retpoline; no mitigation available!");
474 return;
475
476 retpoline_auto:
477 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD) {
478 retpoline_amd:
479 if (!boot_cpu_has(X86_FEATURE_LFENCE_RDTSC)) {
480 pr_err("Spectre mitigation: LFENCE not serializing, switching to generic retpoline\n");
481 goto retpoline_generic;
482 }
483 mode = SPECTRE_V2_RETPOLINE_AMD;
484 setup_force_cpu_cap(X86_FEATURE_RETPOLINE_AMD);
485 setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
486 } else {
487 retpoline_generic:
488 mode = SPECTRE_V2_RETPOLINE_GENERIC;
489 setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
490 }
491
492 specv2_set_mode:
493 spectre_v2_enabled = mode;
494 pr_info("%s\n", spectre_v2_strings[mode]);
495
496 /*
497 * If spectre v2 protection has been enabled, unconditionally fill
498 * RSB during a context switch; this protects against two independent
499 * issues:
500 *
501 * - RSB underflow (and switch to BTB) on Skylake+
502 * - SpectreRSB variant of spectre v2 on X86_BUG_SPECTRE_V2 CPUs
503 */
504 setup_force_cpu_cap(X86_FEATURE_RSB_CTXSW);
505 pr_info("Spectre v2 / SpectreRSB mitigation: Filling RSB on context switch\n");
506
507 /*
508 * Retpoline means the kernel is safe because it has no indirect
509 * branches. Enhanced IBRS protects firmware too, so, enable restricted
510 * speculation around firmware calls only when Enhanced IBRS isn't
511 * supported.
512 *
513 * Use "mode" to check Enhanced IBRS instead of boot_cpu_has(), because
514 * the user might select retpoline on the kernel command line and if
515 * the CPU supports Enhanced IBRS, kernel might un-intentionally not
516 * enable IBRS around firmware calls.
517 */
518 if (boot_cpu_has(X86_FEATURE_IBRS) && mode != SPECTRE_V2_IBRS_ENHANCED) {
519 setup_force_cpu_cap(X86_FEATURE_USE_IBRS_FW);
520 pr_info("Enabling Restricted Speculation for firmware calls\n");
521 }
522
523 /* Set up IBPB and STIBP depending on the general spectre V2 command */
524 spectre_v2_user_select_mitigation(cmd);
525
526 /* Enable STIBP if appropriate */
527 arch_smt_update();
528 }
529
530 static bool stibp_needed(void)
531 {
532 /* Enhanced IBRS makes using STIBP unnecessary. */
533 if (spectre_v2_enabled == SPECTRE_V2_IBRS_ENHANCED)
534 return false;
535
536 /* Check for strict user mitigation mode */
537 return spectre_v2_user == SPECTRE_V2_USER_STRICT;
538 }
539
540 static void update_stibp_msr(void *info)
541 {
542 wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
543 }
544
545 void arch_smt_update(void)
546 {
547 u64 mask;
548
549 if (!stibp_needed())
550 return;
551
552 mutex_lock(&spec_ctrl_mutex);
553
554 mask = x86_spec_ctrl_base & ~SPEC_CTRL_STIBP;
555 if (sched_smt_active())
556 mask |= SPEC_CTRL_STIBP;
557
558 if (mask != x86_spec_ctrl_base) {
559 pr_info("Spectre v2 cross-process SMT mitigation: %s STIBP\n",
560 mask & SPEC_CTRL_STIBP ? "Enabling" : "Disabling");
561 x86_spec_ctrl_base = mask;
562 on_each_cpu(update_stibp_msr, NULL, 1);
563 }
564 mutex_unlock(&spec_ctrl_mutex);
565 }
566
567 #undef pr_fmt
568 #define pr_fmt(fmt) "Speculative Store Bypass: " fmt
569
570 static enum ssb_mitigation ssb_mode __ro_after_init = SPEC_STORE_BYPASS_NONE;
571
572 /* The kernel command line selection */
573 enum ssb_mitigation_cmd {
574 SPEC_STORE_BYPASS_CMD_NONE,
575 SPEC_STORE_BYPASS_CMD_AUTO,
576 SPEC_STORE_BYPASS_CMD_ON,
577 SPEC_STORE_BYPASS_CMD_PRCTL,
578 SPEC_STORE_BYPASS_CMD_SECCOMP,
579 };
580
581 static const char * const ssb_strings[] = {
582 [SPEC_STORE_BYPASS_NONE] = "Vulnerable",
583 [SPEC_STORE_BYPASS_DISABLE] = "Mitigation: Speculative Store Bypass disabled",
584 [SPEC_STORE_BYPASS_PRCTL] = "Mitigation: Speculative Store Bypass disabled via prctl",
585 [SPEC_STORE_BYPASS_SECCOMP] = "Mitigation: Speculative Store Bypass disabled via prctl and seccomp",
586 };
587
588 static const struct {
589 const char *option;
590 enum ssb_mitigation_cmd cmd;
591 } ssb_mitigation_options[] __initdata = {
592 { "auto", SPEC_STORE_BYPASS_CMD_AUTO }, /* Platform decides */
593 { "on", SPEC_STORE_BYPASS_CMD_ON }, /* Disable Speculative Store Bypass */
594 { "off", SPEC_STORE_BYPASS_CMD_NONE }, /* Don't touch Speculative Store Bypass */
595 { "prctl", SPEC_STORE_BYPASS_CMD_PRCTL }, /* Disable Speculative Store Bypass via prctl */
596 { "seccomp", SPEC_STORE_BYPASS_CMD_SECCOMP }, /* Disable Speculative Store Bypass via prctl and seccomp */
597 };
598
599 static enum ssb_mitigation_cmd __init ssb_parse_cmdline(void)
600 {
601 enum ssb_mitigation_cmd cmd = SPEC_STORE_BYPASS_CMD_AUTO;
602 char arg[20];
603 int ret, i;
604
605 if (cmdline_find_option_bool(boot_command_line, "nospec_store_bypass_disable")) {
606 return SPEC_STORE_BYPASS_CMD_NONE;
607 } else {
608 ret = cmdline_find_option(boot_command_line, "spec_store_bypass_disable",
609 arg, sizeof(arg));
610 if (ret < 0)
611 return SPEC_STORE_BYPASS_CMD_AUTO;
612
613 for (i = 0; i < ARRAY_SIZE(ssb_mitigation_options); i++) {
614 if (!match_option(arg, ret, ssb_mitigation_options[i].option))
615 continue;
616
617 cmd = ssb_mitigation_options[i].cmd;
618 break;
619 }
620
621 if (i >= ARRAY_SIZE(ssb_mitigation_options)) {
622 pr_err("unknown option (%s). Switching to AUTO select\n", arg);
623 return SPEC_STORE_BYPASS_CMD_AUTO;
624 }
625 }
626
627 return cmd;
628 }
629
630 static enum ssb_mitigation __init __ssb_select_mitigation(void)
631 {
632 enum ssb_mitigation mode = SPEC_STORE_BYPASS_NONE;
633 enum ssb_mitigation_cmd cmd;
634
635 if (!boot_cpu_has(X86_FEATURE_SSBD))
636 return mode;
637
638 cmd = ssb_parse_cmdline();
639 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS) &&
640 (cmd == SPEC_STORE_BYPASS_CMD_NONE ||
641 cmd == SPEC_STORE_BYPASS_CMD_AUTO))
642 return mode;
643
644 switch (cmd) {
645 case SPEC_STORE_BYPASS_CMD_AUTO:
646 case SPEC_STORE_BYPASS_CMD_SECCOMP:
647 /*
648 * Choose prctl+seccomp as the default mode if seccomp is
649 * enabled.
650 */
651 if (IS_ENABLED(CONFIG_SECCOMP))
652 mode = SPEC_STORE_BYPASS_SECCOMP;
653 else
654 mode = SPEC_STORE_BYPASS_PRCTL;
655 break;
656 case SPEC_STORE_BYPASS_CMD_ON:
657 mode = SPEC_STORE_BYPASS_DISABLE;
658 break;
659 case SPEC_STORE_BYPASS_CMD_PRCTL:
660 mode = SPEC_STORE_BYPASS_PRCTL;
661 break;
662 case SPEC_STORE_BYPASS_CMD_NONE:
663 break;
664 }
665
666 /*
667 * We have three CPU feature flags that are in play here:
668 * - X86_BUG_SPEC_STORE_BYPASS - CPU is susceptible.
669 * - X86_FEATURE_SSBD - CPU is able to turn off speculative store bypass
670 * - X86_FEATURE_SPEC_STORE_BYPASS_DISABLE - engage the mitigation
671 */
672 if (mode == SPEC_STORE_BYPASS_DISABLE) {
673 setup_force_cpu_cap(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE);
674 /*
675 * Intel uses the SPEC CTRL MSR Bit(2) for this, while AMD may
676 * use a completely different MSR and bit dependent on family.
677 */
678 if (!static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) &&
679 !static_cpu_has(X86_FEATURE_AMD_SSBD)) {
680 x86_amd_ssb_disable();
681 } else {
682 x86_spec_ctrl_base |= SPEC_CTRL_SSBD;
683 x86_spec_ctrl_mask |= SPEC_CTRL_SSBD;
684 wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
685 }
686 }
687
688 return mode;
689 }
690
691 static void ssb_select_mitigation(void)
692 {
693 ssb_mode = __ssb_select_mitigation();
694
695 if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
696 pr_info("%s\n", ssb_strings[ssb_mode]);
697 }
698
699 #undef pr_fmt
700 #define pr_fmt(fmt) "Speculation prctl: " fmt
701
702 static void task_update_spec_tif(struct task_struct *tsk)
703 {
704 /* Force the update of the real TIF bits */
705 set_tsk_thread_flag(tsk, TIF_SPEC_FORCE_UPDATE);
706
707 /*
708 * Immediately update the speculation control MSRs for the current
709 * task, but for a non-current task delay setting the CPU
710 * mitigation until it is scheduled next.
711 *
712 * This can only happen for SECCOMP mitigation. For PRCTL it's
713 * always the current task.
714 */
715 if (tsk == current)
716 speculation_ctrl_update_current();
717 }
718
719 static int ssb_prctl_set(struct task_struct *task, unsigned long ctrl)
720 {
721 if (ssb_mode != SPEC_STORE_BYPASS_PRCTL &&
722 ssb_mode != SPEC_STORE_BYPASS_SECCOMP)
723 return -ENXIO;
724
725 switch (ctrl) {
726 case PR_SPEC_ENABLE:
727 /* If speculation is force disabled, enable is not allowed */
728 if (task_spec_ssb_force_disable(task))
729 return -EPERM;
730 task_clear_spec_ssb_disable(task);
731 task_update_spec_tif(task);
732 break;
733 case PR_SPEC_DISABLE:
734 task_set_spec_ssb_disable(task);
735 task_update_spec_tif(task);
736 break;
737 case PR_SPEC_FORCE_DISABLE:
738 task_set_spec_ssb_disable(task);
739 task_set_spec_ssb_force_disable(task);
740 task_update_spec_tif(task);
741 break;
742 default:
743 return -ERANGE;
744 }
745 return 0;
746 }
747
748 int arch_prctl_spec_ctrl_set(struct task_struct *task, unsigned long which,
749 unsigned long ctrl)
750 {
751 switch (which) {
752 case PR_SPEC_STORE_BYPASS:
753 return ssb_prctl_set(task, ctrl);
754 default:
755 return -ENODEV;
756 }
757 }
758
759 #ifdef CONFIG_SECCOMP
760 void arch_seccomp_spec_mitigate(struct task_struct *task)
761 {
762 if (ssb_mode == SPEC_STORE_BYPASS_SECCOMP)
763 ssb_prctl_set(task, PR_SPEC_FORCE_DISABLE);
764 }
765 #endif
766
767 static int ssb_prctl_get(struct task_struct *task)
768 {
769 switch (ssb_mode) {
770 case SPEC_STORE_BYPASS_DISABLE:
771 return PR_SPEC_DISABLE;
772 case SPEC_STORE_BYPASS_SECCOMP:
773 case SPEC_STORE_BYPASS_PRCTL:
774 if (task_spec_ssb_force_disable(task))
775 return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE;
776 if (task_spec_ssb_disable(task))
777 return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
778 return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
779 default:
780 if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
781 return PR_SPEC_ENABLE;
782 return PR_SPEC_NOT_AFFECTED;
783 }
784 }
785
786 int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which)
787 {
788 switch (which) {
789 case PR_SPEC_STORE_BYPASS:
790 return ssb_prctl_get(task);
791 default:
792 return -ENODEV;
793 }
794 }
795
796 void x86_spec_ctrl_setup_ap(void)
797 {
798 if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
799 wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
800
801 if (ssb_mode == SPEC_STORE_BYPASS_DISABLE)
802 x86_amd_ssb_disable();
803 }
804
805 #undef pr_fmt
806 #define pr_fmt(fmt) "L1TF: " fmt
807
808 /* Default mitigation for L1TF-affected CPUs */
809 enum l1tf_mitigations l1tf_mitigation __ro_after_init = L1TF_MITIGATION_FLUSH;
810 #if IS_ENABLED(CONFIG_KVM_INTEL)
811 EXPORT_SYMBOL_GPL(l1tf_mitigation);
812
813 enum vmx_l1d_flush_state l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
814 EXPORT_SYMBOL_GPL(l1tf_vmx_mitigation);
815 #endif
816
817 /*
818 * These CPUs all support 44bits physical address space internally in the
819 * cache but CPUID can report a smaller number of physical address bits.
820 *
821 * The L1TF mitigation uses the top most address bit for the inversion of
822 * non present PTEs. When the installed memory reaches into the top most
823 * address bit due to memory holes, which has been observed on machines
824 * which report 36bits physical address bits and have 32G RAM installed,
825 * then the mitigation range check in l1tf_select_mitigation() triggers.
826 * This is a false positive because the mitigation is still possible due to
827 * the fact that the cache uses 44bit internally. Use the cache bits
828 * instead of the reported physical bits and adjust them on the affected
829 * machines to 44bit if the reported bits are less than 44.
830 */
831 static void override_cache_bits(struct cpuinfo_x86 *c)
832 {
833 if (c->x86 != 6)
834 return;
835
836 switch (c->x86_model) {
837 case INTEL_FAM6_NEHALEM:
838 case INTEL_FAM6_WESTMERE:
839 case INTEL_FAM6_SANDYBRIDGE:
840 case INTEL_FAM6_IVYBRIDGE:
841 case INTEL_FAM6_HASWELL_CORE:
842 case INTEL_FAM6_HASWELL_ULT:
843 case INTEL_FAM6_HASWELL_GT3E:
844 case INTEL_FAM6_BROADWELL_CORE:
845 case INTEL_FAM6_BROADWELL_GT3E:
846 case INTEL_FAM6_SKYLAKE_MOBILE:
847 case INTEL_FAM6_SKYLAKE_DESKTOP:
848 case INTEL_FAM6_KABYLAKE_MOBILE:
849 case INTEL_FAM6_KABYLAKE_DESKTOP:
850 if (c->x86_cache_bits < 44)
851 c->x86_cache_bits = 44;
852 break;
853 }
854 }
855
856 static void __init l1tf_select_mitigation(void)
857 {
858 u64 half_pa;
859
860 if (!boot_cpu_has_bug(X86_BUG_L1TF))
861 return;
862
863 override_cache_bits(&boot_cpu_data);
864
865 switch (l1tf_mitigation) {
866 case L1TF_MITIGATION_OFF:
867 case L1TF_MITIGATION_FLUSH_NOWARN:
868 case L1TF_MITIGATION_FLUSH:
869 break;
870 case L1TF_MITIGATION_FLUSH_NOSMT:
871 case L1TF_MITIGATION_FULL:
872 cpu_smt_disable(false);
873 break;
874 case L1TF_MITIGATION_FULL_FORCE:
875 cpu_smt_disable(true);
876 break;
877 }
878
879 #if CONFIG_PGTABLE_LEVELS == 2
880 pr_warn("Kernel not compiled for PAE. No mitigation for L1TF\n");
881 return;
882 #endif
883
884 half_pa = (u64)l1tf_pfn_limit() << PAGE_SHIFT;
885 if (e820__mapped_any(half_pa, ULLONG_MAX - half_pa, E820_TYPE_RAM)) {
886 pr_warn("System has more than MAX_PA/2 memory. L1TF mitigation not effective.\n");
887 return;
888 }
889
890 setup_force_cpu_cap(X86_FEATURE_L1TF_PTEINV);
891 }
892
893 static int __init l1tf_cmdline(char *str)
894 {
895 if (!boot_cpu_has_bug(X86_BUG_L1TF))
896 return 0;
897
898 if (!str)
899 return -EINVAL;
900
901 if (!strcmp(str, "off"))
902 l1tf_mitigation = L1TF_MITIGATION_OFF;
903 else if (!strcmp(str, "flush,nowarn"))
904 l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOWARN;
905 else if (!strcmp(str, "flush"))
906 l1tf_mitigation = L1TF_MITIGATION_FLUSH;
907 else if (!strcmp(str, "flush,nosmt"))
908 l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOSMT;
909 else if (!strcmp(str, "full"))
910 l1tf_mitigation = L1TF_MITIGATION_FULL;
911 else if (!strcmp(str, "full,force"))
912 l1tf_mitigation = L1TF_MITIGATION_FULL_FORCE;
913
914 return 0;
915 }
916 early_param("l1tf", l1tf_cmdline);
917
918 #undef pr_fmt
919
920 #ifdef CONFIG_SYSFS
921
922 #define L1TF_DEFAULT_MSG "Mitigation: PTE Inversion"
923
924 #if IS_ENABLED(CONFIG_KVM_INTEL)
925 static const char * const l1tf_vmx_states[] = {
926 [VMENTER_L1D_FLUSH_AUTO] = "auto",
927 [VMENTER_L1D_FLUSH_NEVER] = "vulnerable",
928 [VMENTER_L1D_FLUSH_COND] = "conditional cache flushes",
929 [VMENTER_L1D_FLUSH_ALWAYS] = "cache flushes",
930 [VMENTER_L1D_FLUSH_EPT_DISABLED] = "EPT disabled",
931 [VMENTER_L1D_FLUSH_NOT_REQUIRED] = "flush not necessary"
932 };
933
934 static ssize_t l1tf_show_state(char *buf)
935 {
936 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO)
937 return sprintf(buf, "%s\n", L1TF_DEFAULT_MSG);
938
939 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_EPT_DISABLED ||
940 (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER &&
941 sched_smt_active())) {
942 return sprintf(buf, "%s; VMX: %s\n", L1TF_DEFAULT_MSG,
943 l1tf_vmx_states[l1tf_vmx_mitigation]);
944 }
945
946 return sprintf(buf, "%s; VMX: %s, SMT %s\n", L1TF_DEFAULT_MSG,
947 l1tf_vmx_states[l1tf_vmx_mitigation],
948 sched_smt_active() ? "vulnerable" : "disabled");
949 }
950 #else
951 static ssize_t l1tf_show_state(char *buf)
952 {
953 return sprintf(buf, "%s\n", L1TF_DEFAULT_MSG);
954 }
955 #endif
956
957 static char *stibp_state(void)
958 {
959 if (spectre_v2_enabled == SPECTRE_V2_IBRS_ENHANCED)
960 return "";
961
962 switch (spectre_v2_user) {
963 case SPECTRE_V2_USER_NONE:
964 return ", STIBP: disabled";
965 case SPECTRE_V2_USER_STRICT:
966 return ", STIBP: forced";
967 }
968 return "";
969 }
970
971 static char *ibpb_state(void)
972 {
973 if (boot_cpu_has(X86_FEATURE_IBPB)) {
974 switch (spectre_v2_user) {
975 case SPECTRE_V2_USER_NONE:
976 return ", IBPB: disabled";
977 case SPECTRE_V2_USER_STRICT:
978 return ", IBPB: always-on";
979 }
980 }
981 return "";
982 }
983
984 static ssize_t cpu_show_common(struct device *dev, struct device_attribute *attr,
985 char *buf, unsigned int bug)
986 {
987 if (!boot_cpu_has_bug(bug))
988 return sprintf(buf, "Not affected\n");
989
990 switch (bug) {
991 case X86_BUG_CPU_MELTDOWN:
992 if (boot_cpu_has(X86_FEATURE_PTI))
993 return sprintf(buf, "Mitigation: PTI\n");
994
995 break;
996
997 case X86_BUG_SPECTRE_V1:
998 return sprintf(buf, "Mitigation: __user pointer sanitization\n");
999
1000 case X86_BUG_SPECTRE_V2:
1001 return sprintf(buf, "%s%s%s%s%s%s\n", spectre_v2_strings[spectre_v2_enabled],
1002 ibpb_state(),
1003 boot_cpu_has(X86_FEATURE_USE_IBRS_FW) ? ", IBRS_FW" : "",
1004 stibp_state(),
1005 boot_cpu_has(X86_FEATURE_RSB_CTXSW) ? ", RSB filling" : "",
1006 spectre_v2_module_string());
1007
1008 case X86_BUG_SPEC_STORE_BYPASS:
1009 return sprintf(buf, "%s\n", ssb_strings[ssb_mode]);
1010
1011 case X86_BUG_L1TF:
1012 if (boot_cpu_has(X86_FEATURE_L1TF_PTEINV))
1013 return l1tf_show_state(buf);
1014 break;
1015 default:
1016 break;
1017 }
1018
1019 return sprintf(buf, "Vulnerable\n");
1020 }
1021
1022 ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr, char *buf)
1023 {
1024 return cpu_show_common(dev, attr, buf, X86_BUG_CPU_MELTDOWN);
1025 }
1026
1027 ssize_t cpu_show_spectre_v1(struct device *dev, struct device_attribute *attr, char *buf)
1028 {
1029 return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V1);
1030 }
1031
1032 ssize_t cpu_show_spectre_v2(struct device *dev, struct device_attribute *attr, char *buf)
1033 {
1034 return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V2);
1035 }
1036
1037 ssize_t cpu_show_spec_store_bypass(struct device *dev, struct device_attribute *attr, char *buf)
1038 {
1039 return cpu_show_common(dev, attr, buf, X86_BUG_SPEC_STORE_BYPASS);
1040 }
1041
1042 ssize_t cpu_show_l1tf(struct device *dev, struct device_attribute *attr, char *buf)
1043 {
1044 return cpu_show_common(dev, attr, buf, X86_BUG_L1TF);
1045 }
1046 #endif