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 #include <linux/pgtable.h>
19 #include <linux/bpf.h>
20
21 #include <asm/spec-ctrl.h>
22 #include <asm/cmdline.h>
23 #include <asm/bugs.h>
24 #include <asm/processor.h>
25 #include <asm/processor-flags.h>
26 #include <asm/fpu/internal.h>
27 #include <asm/msr.h>
28 #include <asm/vmx.h>
29 #include <asm/paravirt.h>
30 #include <asm/alternative.h>
31 #include <asm/set_memory.h>
32 #include <asm/intel-family.h>
33 #include <asm/e820/api.h>
34 #include <asm/hypervisor.h>
35 #include <asm/tlbflush.h>
36
37 #include "cpu.h"
38
39 static void __init spectre_v1_select_mitigation(void);
40 static void __init spectre_v2_select_mitigation(void);
41 static void __init retbleed_select_mitigation(void);
42 static void __init spectre_v2_user_select_mitigation(void);
43 static void __init ssb_select_mitigation(void);
44 static void __init l1tf_select_mitigation(void);
45 static void __init mds_select_mitigation(void);
46 static void __init md_clear_update_mitigation(void);
47 static void __init md_clear_select_mitigation(void);
48 static void __init taa_select_mitigation(void);
49 static void __init mmio_select_mitigation(void);
50 static void __init srbds_select_mitigation(void);
51
52 /* The base value of the SPEC_CTRL MSR without task-specific bits set */
53 u64 x86_spec_ctrl_base;
54 EXPORT_SYMBOL_GPL(x86_spec_ctrl_base);
55
56 /* The current value of the SPEC_CTRL MSR with task-specific bits set */
57 DEFINE_PER_CPU(u64, x86_spec_ctrl_current);
58 EXPORT_SYMBOL_GPL(x86_spec_ctrl_current);
59
60 static DEFINE_MUTEX(spec_ctrl_mutex);
61
62 /* Update SPEC_CTRL MSR and its cached copy unconditionally */
update_spec_ctrl(u64 val)63 static void update_spec_ctrl(u64 val)
64 {
65 this_cpu_write(x86_spec_ctrl_current, val);
66 wrmsrl(MSR_IA32_SPEC_CTRL, val);
67 }
68
69 /*
70 * Keep track of the SPEC_CTRL MSR value for the current task, which may differ
71 * from x86_spec_ctrl_base due to STIBP/SSB in __speculation_ctrl_update().
72 */
update_spec_ctrl_cond(u64 val)73 void update_spec_ctrl_cond(u64 val)
74 {
75 if (this_cpu_read(x86_spec_ctrl_current) == val)
76 return;
77
78 this_cpu_write(x86_spec_ctrl_current, val);
79
80 /*
81 * When KERNEL_IBRS this MSR is written on return-to-user, unless
82 * forced the update can be delayed until that time.
83 */
84 if (!cpu_feature_enabled(X86_FEATURE_KERNEL_IBRS))
85 wrmsrl(MSR_IA32_SPEC_CTRL, val);
86 }
87
spec_ctrl_current(void)88 u64 spec_ctrl_current(void)
89 {
90 return this_cpu_read(x86_spec_ctrl_current);
91 }
92 EXPORT_SYMBOL_GPL(spec_ctrl_current);
93
94 /*
95 * AMD specific MSR info for Speculative Store Bypass control.
96 * x86_amd_ls_cfg_ssbd_mask is initialized in identify_boot_cpu().
97 */
98 u64 __ro_after_init x86_amd_ls_cfg_base;
99 u64 __ro_after_init x86_amd_ls_cfg_ssbd_mask;
100
101 /* Control conditional STIBP in switch_to() */
102 DEFINE_STATIC_KEY_FALSE(switch_to_cond_stibp);
103 /* Control conditional IBPB in switch_mm() */
104 DEFINE_STATIC_KEY_FALSE(switch_mm_cond_ibpb);
105 /* Control unconditional IBPB in switch_mm() */
106 DEFINE_STATIC_KEY_FALSE(switch_mm_always_ibpb);
107
108 /* Control MDS CPU buffer clear before returning to user space */
109 DEFINE_STATIC_KEY_FALSE(mds_user_clear);
110 EXPORT_SYMBOL_GPL(mds_user_clear);
111 /* Control MDS CPU buffer clear before idling (halt, mwait) */
112 DEFINE_STATIC_KEY_FALSE(mds_idle_clear);
113 EXPORT_SYMBOL_GPL(mds_idle_clear);
114
115 /* Controls CPU Fill buffer clear before KVM guest MMIO accesses */
116 DEFINE_STATIC_KEY_FALSE(mmio_stale_data_clear);
117 EXPORT_SYMBOL_GPL(mmio_stale_data_clear);
118
check_bugs(void)119 void __init check_bugs(void)
120 {
121 identify_boot_cpu();
122
123 /*
124 * identify_boot_cpu() initialized SMT support information, let the
125 * core code know.
126 */
127 cpu_smt_check_topology();
128
129 if (!IS_ENABLED(CONFIG_SMP)) {
130 pr_info("CPU: ");
131 print_cpu_info(&boot_cpu_data);
132 }
133
134 /*
135 * Read the SPEC_CTRL MSR to account for reserved bits which may
136 * have unknown values. AMD64_LS_CFG MSR is cached in the early AMD
137 * init code as it is not enumerated and depends on the family.
138 */
139 if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
140 rdmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
141
142 /* Select the proper CPU mitigations before patching alternatives: */
143 spectre_v1_select_mitigation();
144 spectre_v2_select_mitigation();
145 /*
146 * retbleed_select_mitigation() relies on the state set by
147 * spectre_v2_select_mitigation(); specifically it wants to know about
148 * spectre_v2=ibrs.
149 */
150 retbleed_select_mitigation();
151 /*
152 * spectre_v2_user_select_mitigation() relies on the state set by
153 * retbleed_select_mitigation(); specifically the STIBP selection is
154 * forced for UNRET or IBPB.
155 */
156 spectre_v2_user_select_mitigation();
157 ssb_select_mitigation();
158 l1tf_select_mitigation();
159 md_clear_select_mitigation();
160 srbds_select_mitigation();
161
162 arch_smt_update();
163
164 #ifdef CONFIG_X86_32
165 /*
166 * Check whether we are able to run this kernel safely on SMP.
167 *
168 * - i386 is no longer supported.
169 * - In order to run on anything without a TSC, we need to be
170 * compiled for a i486.
171 */
172 if (boot_cpu_data.x86 < 4)
173 panic("Kernel requires i486+ for 'invlpg' and other features");
174
175 init_utsname()->machine[1] =
176 '0' + (boot_cpu_data.x86 > 6 ? 6 : boot_cpu_data.x86);
177 alternative_instructions();
178
179 fpu__init_check_bugs();
180 #else /* CONFIG_X86_64 */
181 alternative_instructions();
182
183 /*
184 * Make sure the first 2MB area is not mapped by huge pages
185 * There are typically fixed size MTRRs in there and overlapping
186 * MTRRs into large pages causes slow downs.
187 *
188 * Right now we don't do that with gbpages because there seems
189 * very little benefit for that case.
190 */
191 if (!direct_gbpages)
192 set_memory_4k((unsigned long)__va(0), 1);
193 #endif
194 }
195
196 /*
197 * NOTE: For VMX, this function is not called in the vmexit path.
198 * It uses vmx_spec_ctrl_restore_host() instead.
199 */
200 void
x86_virt_spec_ctrl(u64 guest_spec_ctrl,u64 guest_virt_spec_ctrl,bool setguest)201 x86_virt_spec_ctrl(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl, bool setguest)
202 {
203 u64 msrval, guestval = guest_spec_ctrl, hostval = spec_ctrl_current();
204 struct thread_info *ti = current_thread_info();
205
206 if (static_cpu_has(X86_FEATURE_MSR_SPEC_CTRL)) {
207 if (hostval != guestval) {
208 msrval = setguest ? guestval : hostval;
209 wrmsrl(MSR_IA32_SPEC_CTRL, msrval);
210 }
211 }
212
213 /*
214 * If SSBD is not handled in MSR_SPEC_CTRL on AMD, update
215 * MSR_AMD64_L2_CFG or MSR_VIRT_SPEC_CTRL if supported.
216 */
217 if (!static_cpu_has(X86_FEATURE_LS_CFG_SSBD) &&
218 !static_cpu_has(X86_FEATURE_VIRT_SSBD))
219 return;
220
221 /*
222 * If the host has SSBD mitigation enabled, force it in the host's
223 * virtual MSR value. If its not permanently enabled, evaluate
224 * current's TIF_SSBD thread flag.
225 */
226 if (static_cpu_has(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE))
227 hostval = SPEC_CTRL_SSBD;
228 else
229 hostval = ssbd_tif_to_spec_ctrl(ti->flags);
230
231 /* Sanitize the guest value */
232 guestval = guest_virt_spec_ctrl & SPEC_CTRL_SSBD;
233
234 if (hostval != guestval) {
235 unsigned long tif;
236
237 tif = setguest ? ssbd_spec_ctrl_to_tif(guestval) :
238 ssbd_spec_ctrl_to_tif(hostval);
239
240 speculation_ctrl_update(tif);
241 }
242 }
243 EXPORT_SYMBOL_GPL(x86_virt_spec_ctrl);
244
x86_amd_ssb_disable(void)245 static void x86_amd_ssb_disable(void)
246 {
247 u64 msrval = x86_amd_ls_cfg_base | x86_amd_ls_cfg_ssbd_mask;
248
249 if (boot_cpu_has(X86_FEATURE_VIRT_SSBD))
250 wrmsrl(MSR_AMD64_VIRT_SPEC_CTRL, SPEC_CTRL_SSBD);
251 else if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD))
252 wrmsrl(MSR_AMD64_LS_CFG, msrval);
253 }
254
255 #undef pr_fmt
256 #define pr_fmt(fmt) "MDS: " fmt
257
258 /* Default mitigation for MDS-affected CPUs */
259 static enum mds_mitigations mds_mitigation __ro_after_init = MDS_MITIGATION_FULL;
260 static bool mds_nosmt __ro_after_init = false;
261
262 static const char * const mds_strings[] = {
263 [MDS_MITIGATION_OFF] = "Vulnerable",
264 [MDS_MITIGATION_FULL] = "Mitigation: Clear CPU buffers",
265 [MDS_MITIGATION_VMWERV] = "Vulnerable: Clear CPU buffers attempted, no microcode",
266 };
267
mds_select_mitigation(void)268 static void __init mds_select_mitigation(void)
269 {
270 if (!boot_cpu_has_bug(X86_BUG_MDS) || cpu_mitigations_off()) {
271 mds_mitigation = MDS_MITIGATION_OFF;
272 return;
273 }
274
275 if (mds_mitigation == MDS_MITIGATION_FULL) {
276 if (!boot_cpu_has(X86_FEATURE_MD_CLEAR))
277 mds_mitigation = MDS_MITIGATION_VMWERV;
278
279 static_branch_enable(&mds_user_clear);
280
281 if (!boot_cpu_has(X86_BUG_MSBDS_ONLY) &&
282 (mds_nosmt || cpu_mitigations_auto_nosmt()))
283 cpu_smt_disable(false);
284 }
285 }
286
mds_cmdline(char * str)287 static int __init mds_cmdline(char *str)
288 {
289 if (!boot_cpu_has_bug(X86_BUG_MDS))
290 return 0;
291
292 if (!str)
293 return -EINVAL;
294
295 if (!strcmp(str, "off"))
296 mds_mitigation = MDS_MITIGATION_OFF;
297 else if (!strcmp(str, "full"))
298 mds_mitigation = MDS_MITIGATION_FULL;
299 else if (!strcmp(str, "full,nosmt")) {
300 mds_mitigation = MDS_MITIGATION_FULL;
301 mds_nosmt = true;
302 }
303
304 return 0;
305 }
306 early_param("mds", mds_cmdline);
307
308 #undef pr_fmt
309 #define pr_fmt(fmt) "TAA: " fmt
310
311 enum taa_mitigations {
312 TAA_MITIGATION_OFF,
313 TAA_MITIGATION_UCODE_NEEDED,
314 TAA_MITIGATION_VERW,
315 TAA_MITIGATION_TSX_DISABLED,
316 };
317
318 /* Default mitigation for TAA-affected CPUs */
319 static enum taa_mitigations taa_mitigation __ro_after_init = TAA_MITIGATION_VERW;
320 static bool taa_nosmt __ro_after_init;
321
322 static const char * const taa_strings[] = {
323 [TAA_MITIGATION_OFF] = "Vulnerable",
324 [TAA_MITIGATION_UCODE_NEEDED] = "Vulnerable: Clear CPU buffers attempted, no microcode",
325 [TAA_MITIGATION_VERW] = "Mitigation: Clear CPU buffers",
326 [TAA_MITIGATION_TSX_DISABLED] = "Mitigation: TSX disabled",
327 };
328
taa_select_mitigation(void)329 static void __init taa_select_mitigation(void)
330 {
331 u64 ia32_cap;
332
333 if (!boot_cpu_has_bug(X86_BUG_TAA)) {
334 taa_mitigation = TAA_MITIGATION_OFF;
335 return;
336 }
337
338 /* TSX previously disabled by tsx=off */
339 if (!boot_cpu_has(X86_FEATURE_RTM)) {
340 taa_mitigation = TAA_MITIGATION_TSX_DISABLED;
341 return;
342 }
343
344 if (cpu_mitigations_off()) {
345 taa_mitigation = TAA_MITIGATION_OFF;
346 return;
347 }
348
349 /*
350 * TAA mitigation via VERW is turned off if both
351 * tsx_async_abort=off and mds=off are specified.
352 */
353 if (taa_mitigation == TAA_MITIGATION_OFF &&
354 mds_mitigation == MDS_MITIGATION_OFF)
355 return;
356
357 if (boot_cpu_has(X86_FEATURE_MD_CLEAR))
358 taa_mitigation = TAA_MITIGATION_VERW;
359 else
360 taa_mitigation = TAA_MITIGATION_UCODE_NEEDED;
361
362 /*
363 * VERW doesn't clear the CPU buffers when MD_CLEAR=1 and MDS_NO=1.
364 * A microcode update fixes this behavior to clear CPU buffers. It also
365 * adds support for MSR_IA32_TSX_CTRL which is enumerated by the
366 * ARCH_CAP_TSX_CTRL_MSR bit.
367 *
368 * On MDS_NO=1 CPUs if ARCH_CAP_TSX_CTRL_MSR is not set, microcode
369 * update is required.
370 */
371 ia32_cap = x86_read_arch_cap_msr();
372 if ( (ia32_cap & ARCH_CAP_MDS_NO) &&
373 !(ia32_cap & ARCH_CAP_TSX_CTRL_MSR))
374 taa_mitigation = TAA_MITIGATION_UCODE_NEEDED;
375
376 /*
377 * TSX is enabled, select alternate mitigation for TAA which is
378 * the same as MDS. Enable MDS static branch to clear CPU buffers.
379 *
380 * For guests that can't determine whether the correct microcode is
381 * present on host, enable the mitigation for UCODE_NEEDED as well.
382 */
383 static_branch_enable(&mds_user_clear);
384
385 if (taa_nosmt || cpu_mitigations_auto_nosmt())
386 cpu_smt_disable(false);
387 }
388
tsx_async_abort_parse_cmdline(char * str)389 static int __init tsx_async_abort_parse_cmdline(char *str)
390 {
391 if (!boot_cpu_has_bug(X86_BUG_TAA))
392 return 0;
393
394 if (!str)
395 return -EINVAL;
396
397 if (!strcmp(str, "off")) {
398 taa_mitigation = TAA_MITIGATION_OFF;
399 } else if (!strcmp(str, "full")) {
400 taa_mitigation = TAA_MITIGATION_VERW;
401 } else if (!strcmp(str, "full,nosmt")) {
402 taa_mitigation = TAA_MITIGATION_VERW;
403 taa_nosmt = true;
404 }
405
406 return 0;
407 }
408 early_param("tsx_async_abort", tsx_async_abort_parse_cmdline);
409
410 #undef pr_fmt
411 #define pr_fmt(fmt) "MMIO Stale Data: " fmt
412
413 enum mmio_mitigations {
414 MMIO_MITIGATION_OFF,
415 MMIO_MITIGATION_UCODE_NEEDED,
416 MMIO_MITIGATION_VERW,
417 };
418
419 /* Default mitigation for Processor MMIO Stale Data vulnerabilities */
420 static enum mmio_mitigations mmio_mitigation __ro_after_init = MMIO_MITIGATION_VERW;
421 static bool mmio_nosmt __ro_after_init = false;
422
423 static const char * const mmio_strings[] = {
424 [MMIO_MITIGATION_OFF] = "Vulnerable",
425 [MMIO_MITIGATION_UCODE_NEEDED] = "Vulnerable: Clear CPU buffers attempted, no microcode",
426 [MMIO_MITIGATION_VERW] = "Mitigation: Clear CPU buffers",
427 };
428
mmio_select_mitigation(void)429 static void __init mmio_select_mitigation(void)
430 {
431 u64 ia32_cap;
432
433 if (!boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA) ||
434 boot_cpu_has_bug(X86_BUG_MMIO_UNKNOWN) ||
435 cpu_mitigations_off()) {
436 mmio_mitigation = MMIO_MITIGATION_OFF;
437 return;
438 }
439
440 if (mmio_mitigation == MMIO_MITIGATION_OFF)
441 return;
442
443 ia32_cap = x86_read_arch_cap_msr();
444
445 /*
446 * Enable CPU buffer clear mitigation for host and VMM, if also affected
447 * by MDS or TAA. Otherwise, enable mitigation for VMM only.
448 */
449 if (boot_cpu_has_bug(X86_BUG_MDS) || (boot_cpu_has_bug(X86_BUG_TAA) &&
450 boot_cpu_has(X86_FEATURE_RTM)))
451 static_branch_enable(&mds_user_clear);
452 else
453 static_branch_enable(&mmio_stale_data_clear);
454
455 /*
456 * If Processor-MMIO-Stale-Data bug is present and Fill Buffer data can
457 * be propagated to uncore buffers, clearing the Fill buffers on idle
458 * is required irrespective of SMT state.
459 */
460 if (!(ia32_cap & ARCH_CAP_FBSDP_NO))
461 static_branch_enable(&mds_idle_clear);
462
463 /*
464 * Check if the system has the right microcode.
465 *
466 * CPU Fill buffer clear mitigation is enumerated by either an explicit
467 * FB_CLEAR or by the presence of both MD_CLEAR and L1D_FLUSH on MDS
468 * affected systems.
469 */
470 if ((ia32_cap & ARCH_CAP_FB_CLEAR) ||
471 (boot_cpu_has(X86_FEATURE_MD_CLEAR) &&
472 boot_cpu_has(X86_FEATURE_FLUSH_L1D) &&
473 !(ia32_cap & ARCH_CAP_MDS_NO)))
474 mmio_mitigation = MMIO_MITIGATION_VERW;
475 else
476 mmio_mitigation = MMIO_MITIGATION_UCODE_NEEDED;
477
478 if (mmio_nosmt || cpu_mitigations_auto_nosmt())
479 cpu_smt_disable(false);
480 }
481
mmio_stale_data_parse_cmdline(char * str)482 static int __init mmio_stale_data_parse_cmdline(char *str)
483 {
484 if (!boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA))
485 return 0;
486
487 if (!str)
488 return -EINVAL;
489
490 if (!strcmp(str, "off")) {
491 mmio_mitigation = MMIO_MITIGATION_OFF;
492 } else if (!strcmp(str, "full")) {
493 mmio_mitigation = MMIO_MITIGATION_VERW;
494 } else if (!strcmp(str, "full,nosmt")) {
495 mmio_mitigation = MMIO_MITIGATION_VERW;
496 mmio_nosmt = true;
497 }
498
499 return 0;
500 }
501 early_param("mmio_stale_data", mmio_stale_data_parse_cmdline);
502
503 #undef pr_fmt
504 #define pr_fmt(fmt) "" fmt
505
md_clear_update_mitigation(void)506 static void __init md_clear_update_mitigation(void)
507 {
508 if (cpu_mitigations_off())
509 return;
510
511 if (!static_key_enabled(&mds_user_clear))
512 goto out;
513
514 /*
515 * mds_user_clear is now enabled. Update MDS, TAA and MMIO Stale Data
516 * mitigation, if necessary.
517 */
518 if (mds_mitigation == MDS_MITIGATION_OFF &&
519 boot_cpu_has_bug(X86_BUG_MDS)) {
520 mds_mitigation = MDS_MITIGATION_FULL;
521 mds_select_mitigation();
522 }
523 if (taa_mitigation == TAA_MITIGATION_OFF &&
524 boot_cpu_has_bug(X86_BUG_TAA)) {
525 taa_mitigation = TAA_MITIGATION_VERW;
526 taa_select_mitigation();
527 }
528 if (mmio_mitigation == MMIO_MITIGATION_OFF &&
529 boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA)) {
530 mmio_mitigation = MMIO_MITIGATION_VERW;
531 mmio_select_mitigation();
532 }
533 out:
534 if (boot_cpu_has_bug(X86_BUG_MDS))
535 pr_info("MDS: %s\n", mds_strings[mds_mitigation]);
536 if (boot_cpu_has_bug(X86_BUG_TAA))
537 pr_info("TAA: %s\n", taa_strings[taa_mitigation]);
538 if (boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA))
539 pr_info("MMIO Stale Data: %s\n", mmio_strings[mmio_mitigation]);
540 else if (boot_cpu_has_bug(X86_BUG_MMIO_UNKNOWN))
541 pr_info("MMIO Stale Data: Unknown: No mitigations\n");
542 }
543
md_clear_select_mitigation(void)544 static void __init md_clear_select_mitigation(void)
545 {
546 mds_select_mitigation();
547 taa_select_mitigation();
548 mmio_select_mitigation();
549
550 /*
551 * As MDS, TAA and MMIO Stale Data mitigations are inter-related, update
552 * and print their mitigation after MDS, TAA and MMIO Stale Data
553 * mitigation selection is done.
554 */
555 md_clear_update_mitigation();
556 }
557
558 #undef pr_fmt
559 #define pr_fmt(fmt) "SRBDS: " fmt
560
561 enum srbds_mitigations {
562 SRBDS_MITIGATION_OFF,
563 SRBDS_MITIGATION_UCODE_NEEDED,
564 SRBDS_MITIGATION_FULL,
565 SRBDS_MITIGATION_TSX_OFF,
566 SRBDS_MITIGATION_HYPERVISOR,
567 };
568
569 static enum srbds_mitigations srbds_mitigation __ro_after_init = SRBDS_MITIGATION_FULL;
570
571 static const char * const srbds_strings[] = {
572 [SRBDS_MITIGATION_OFF] = "Vulnerable",
573 [SRBDS_MITIGATION_UCODE_NEEDED] = "Vulnerable: No microcode",
574 [SRBDS_MITIGATION_FULL] = "Mitigation: Microcode",
575 [SRBDS_MITIGATION_TSX_OFF] = "Mitigation: TSX disabled",
576 [SRBDS_MITIGATION_HYPERVISOR] = "Unknown: Dependent on hypervisor status",
577 };
578
579 static bool srbds_off;
580
update_srbds_msr(void)581 void update_srbds_msr(void)
582 {
583 u64 mcu_ctrl;
584
585 if (!boot_cpu_has_bug(X86_BUG_SRBDS))
586 return;
587
588 if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
589 return;
590
591 if (srbds_mitigation == SRBDS_MITIGATION_UCODE_NEEDED)
592 return;
593
594 rdmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl);
595
596 switch (srbds_mitigation) {
597 case SRBDS_MITIGATION_OFF:
598 case SRBDS_MITIGATION_TSX_OFF:
599 mcu_ctrl |= RNGDS_MITG_DIS;
600 break;
601 case SRBDS_MITIGATION_FULL:
602 mcu_ctrl &= ~RNGDS_MITG_DIS;
603 break;
604 default:
605 break;
606 }
607
608 wrmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl);
609 }
610
srbds_select_mitigation(void)611 static void __init srbds_select_mitigation(void)
612 {
613 u64 ia32_cap;
614
615 if (!boot_cpu_has_bug(X86_BUG_SRBDS))
616 return;
617
618 /*
619 * Check to see if this is one of the MDS_NO systems supporting TSX that
620 * are only exposed to SRBDS when TSX is enabled or when CPU is affected
621 * by Processor MMIO Stale Data vulnerability.
622 */
623 ia32_cap = x86_read_arch_cap_msr();
624 if ((ia32_cap & ARCH_CAP_MDS_NO) && !boot_cpu_has(X86_FEATURE_RTM) &&
625 !boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA))
626 srbds_mitigation = SRBDS_MITIGATION_TSX_OFF;
627 else if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
628 srbds_mitigation = SRBDS_MITIGATION_HYPERVISOR;
629 else if (!boot_cpu_has(X86_FEATURE_SRBDS_CTRL))
630 srbds_mitigation = SRBDS_MITIGATION_UCODE_NEEDED;
631 else if (cpu_mitigations_off() || srbds_off)
632 srbds_mitigation = SRBDS_MITIGATION_OFF;
633
634 update_srbds_msr();
635 pr_info("%s\n", srbds_strings[srbds_mitigation]);
636 }
637
srbds_parse_cmdline(char * str)638 static int __init srbds_parse_cmdline(char *str)
639 {
640 if (!str)
641 return -EINVAL;
642
643 if (!boot_cpu_has_bug(X86_BUG_SRBDS))
644 return 0;
645
646 srbds_off = !strcmp(str, "off");
647 return 0;
648 }
649 early_param("srbds", srbds_parse_cmdline);
650
651 #undef pr_fmt
652 #define pr_fmt(fmt) "Spectre V1 : " fmt
653
654 enum spectre_v1_mitigation {
655 SPECTRE_V1_MITIGATION_NONE,
656 SPECTRE_V1_MITIGATION_AUTO,
657 };
658
659 static enum spectre_v1_mitigation spectre_v1_mitigation __ro_after_init =
660 SPECTRE_V1_MITIGATION_AUTO;
661
662 static const char * const spectre_v1_strings[] = {
663 [SPECTRE_V1_MITIGATION_NONE] = "Vulnerable: __user pointer sanitization and usercopy barriers only; no swapgs barriers",
664 [SPECTRE_V1_MITIGATION_AUTO] = "Mitigation: usercopy/swapgs barriers and __user pointer sanitization",
665 };
666
667 /*
668 * Does SMAP provide full mitigation against speculative kernel access to
669 * userspace?
670 */
smap_works_speculatively(void)671 static bool smap_works_speculatively(void)
672 {
673 if (!boot_cpu_has(X86_FEATURE_SMAP))
674 return false;
675
676 /*
677 * On CPUs which are vulnerable to Meltdown, SMAP does not
678 * prevent speculative access to user data in the L1 cache.
679 * Consider SMAP to be non-functional as a mitigation on these
680 * CPUs.
681 */
682 if (boot_cpu_has(X86_BUG_CPU_MELTDOWN))
683 return false;
684
685 return true;
686 }
687
spectre_v1_select_mitigation(void)688 static void __init spectre_v1_select_mitigation(void)
689 {
690 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V1) || cpu_mitigations_off()) {
691 spectre_v1_mitigation = SPECTRE_V1_MITIGATION_NONE;
692 return;
693 }
694
695 if (spectre_v1_mitigation == SPECTRE_V1_MITIGATION_AUTO) {
696 /*
697 * With Spectre v1, a user can speculatively control either
698 * path of a conditional swapgs with a user-controlled GS
699 * value. The mitigation is to add lfences to both code paths.
700 *
701 * If FSGSBASE is enabled, the user can put a kernel address in
702 * GS, in which case SMAP provides no protection.
703 *
704 * If FSGSBASE is disabled, the user can only put a user space
705 * address in GS. That makes an attack harder, but still
706 * possible if there's no SMAP protection.
707 */
708 if (boot_cpu_has(X86_FEATURE_FSGSBASE) ||
709 !smap_works_speculatively()) {
710 /*
711 * Mitigation can be provided from SWAPGS itself or
712 * PTI as the CR3 write in the Meltdown mitigation
713 * is serializing.
714 *
715 * If neither is there, mitigate with an LFENCE to
716 * stop speculation through swapgs.
717 */
718 if (boot_cpu_has_bug(X86_BUG_SWAPGS) &&
719 !boot_cpu_has(X86_FEATURE_PTI))
720 setup_force_cpu_cap(X86_FEATURE_FENCE_SWAPGS_USER);
721
722 /*
723 * Enable lfences in the kernel entry (non-swapgs)
724 * paths, to prevent user entry from speculatively
725 * skipping swapgs.
726 */
727 setup_force_cpu_cap(X86_FEATURE_FENCE_SWAPGS_KERNEL);
728 }
729 }
730
731 pr_info("%s\n", spectre_v1_strings[spectre_v1_mitigation]);
732 }
733
nospectre_v1_cmdline(char * str)734 static int __init nospectre_v1_cmdline(char *str)
735 {
736 spectre_v1_mitigation = SPECTRE_V1_MITIGATION_NONE;
737 return 0;
738 }
739 early_param("nospectre_v1", nospectre_v1_cmdline);
740
741 static enum spectre_v2_mitigation spectre_v2_enabled __ro_after_init =
742 SPECTRE_V2_NONE;
743
744 #undef pr_fmt
745 #define pr_fmt(fmt) "RETBleed: " fmt
746
747 enum retbleed_mitigation {
748 RETBLEED_MITIGATION_NONE,
749 RETBLEED_MITIGATION_UNRET,
750 RETBLEED_MITIGATION_IBPB,
751 RETBLEED_MITIGATION_IBRS,
752 RETBLEED_MITIGATION_EIBRS,
753 };
754
755 enum retbleed_mitigation_cmd {
756 RETBLEED_CMD_OFF,
757 RETBLEED_CMD_AUTO,
758 RETBLEED_CMD_UNRET,
759 RETBLEED_CMD_IBPB,
760 };
761
762 const char * const retbleed_strings[] = {
763 [RETBLEED_MITIGATION_NONE] = "Vulnerable",
764 [RETBLEED_MITIGATION_UNRET] = "Mitigation: untrained return thunk",
765 [RETBLEED_MITIGATION_IBPB] = "Mitigation: IBPB",
766 [RETBLEED_MITIGATION_IBRS] = "Mitigation: IBRS",
767 [RETBLEED_MITIGATION_EIBRS] = "Mitigation: Enhanced IBRS",
768 };
769
770 static enum retbleed_mitigation retbleed_mitigation __ro_after_init =
771 RETBLEED_MITIGATION_NONE;
772 static enum retbleed_mitigation_cmd retbleed_cmd __ro_after_init =
773 RETBLEED_CMD_AUTO;
774
775 static int __ro_after_init retbleed_nosmt = false;
776
retbleed_parse_cmdline(char * str)777 static int __init retbleed_parse_cmdline(char *str)
778 {
779 if (!str)
780 return -EINVAL;
781
782 while (str) {
783 char *next = strchr(str, ',');
784 if (next) {
785 *next = 0;
786 next++;
787 }
788
789 if (!strcmp(str, "off")) {
790 retbleed_cmd = RETBLEED_CMD_OFF;
791 } else if (!strcmp(str, "auto")) {
792 retbleed_cmd = RETBLEED_CMD_AUTO;
793 } else if (!strcmp(str, "unret")) {
794 retbleed_cmd = RETBLEED_CMD_UNRET;
795 } else if (!strcmp(str, "ibpb")) {
796 retbleed_cmd = RETBLEED_CMD_IBPB;
797 } else if (!strcmp(str, "nosmt")) {
798 retbleed_nosmt = true;
799 } else {
800 pr_err("Ignoring unknown retbleed option (%s).", str);
801 }
802
803 str = next;
804 }
805
806 return 0;
807 }
808 early_param("retbleed", retbleed_parse_cmdline);
809
810 #define RETBLEED_UNTRAIN_MSG "WARNING: BTB untrained return thunk mitigation is only effective on AMD/Hygon!\n"
811 #define RETBLEED_INTEL_MSG "WARNING: Spectre v2 mitigation leaves CPU vulnerable to RETBleed attacks, data leaks possible!\n"
812
retbleed_select_mitigation(void)813 static void __init retbleed_select_mitigation(void)
814 {
815 bool mitigate_smt = false;
816
817 if (!boot_cpu_has_bug(X86_BUG_RETBLEED) || cpu_mitigations_off())
818 return;
819
820 switch (retbleed_cmd) {
821 case RETBLEED_CMD_OFF:
822 return;
823
824 case RETBLEED_CMD_UNRET:
825 if (IS_ENABLED(CONFIG_CPU_UNRET_ENTRY)) {
826 retbleed_mitigation = RETBLEED_MITIGATION_UNRET;
827 } else {
828 pr_err("WARNING: kernel not compiled with CPU_UNRET_ENTRY.\n");
829 goto do_cmd_auto;
830 }
831 break;
832
833 case RETBLEED_CMD_IBPB:
834 if (!boot_cpu_has(X86_FEATURE_IBPB)) {
835 pr_err("WARNING: CPU does not support IBPB.\n");
836 goto do_cmd_auto;
837 } else if (IS_ENABLED(CONFIG_CPU_IBPB_ENTRY)) {
838 retbleed_mitigation = RETBLEED_MITIGATION_IBPB;
839 } else {
840 pr_err("WARNING: kernel not compiled with CPU_IBPB_ENTRY.\n");
841 goto do_cmd_auto;
842 }
843 break;
844
845 do_cmd_auto:
846 case RETBLEED_CMD_AUTO:
847 default:
848 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD ||
849 boot_cpu_data.x86_vendor == X86_VENDOR_HYGON) {
850 if (IS_ENABLED(CONFIG_CPU_UNRET_ENTRY))
851 retbleed_mitigation = RETBLEED_MITIGATION_UNRET;
852 else if (IS_ENABLED(CONFIG_CPU_IBPB_ENTRY) && boot_cpu_has(X86_FEATURE_IBPB))
853 retbleed_mitigation = RETBLEED_MITIGATION_IBPB;
854 }
855
856 /*
857 * The Intel mitigation (IBRS or eIBRS) was already selected in
858 * spectre_v2_select_mitigation(). 'retbleed_mitigation' will
859 * be set accordingly below.
860 */
861
862 break;
863 }
864
865 switch (retbleed_mitigation) {
866 case RETBLEED_MITIGATION_UNRET:
867 setup_force_cpu_cap(X86_FEATURE_RETHUNK);
868 setup_force_cpu_cap(X86_FEATURE_UNRET);
869
870 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD &&
871 boot_cpu_data.x86_vendor != X86_VENDOR_HYGON)
872 pr_err(RETBLEED_UNTRAIN_MSG);
873
874 mitigate_smt = true;
875 break;
876
877 case RETBLEED_MITIGATION_IBPB:
878 setup_force_cpu_cap(X86_FEATURE_ENTRY_IBPB);
879 mitigate_smt = true;
880 break;
881
882 default:
883 break;
884 }
885
886 if (mitigate_smt && !boot_cpu_has(X86_FEATURE_STIBP) &&
887 (retbleed_nosmt || cpu_mitigations_auto_nosmt()))
888 cpu_smt_disable(false);
889
890 /*
891 * Let IBRS trump all on Intel without affecting the effects of the
892 * retbleed= cmdline option.
893 */
894 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) {
895 switch (spectre_v2_enabled) {
896 case SPECTRE_V2_IBRS:
897 retbleed_mitigation = RETBLEED_MITIGATION_IBRS;
898 break;
899 case SPECTRE_V2_EIBRS:
900 case SPECTRE_V2_EIBRS_RETPOLINE:
901 case SPECTRE_V2_EIBRS_LFENCE:
902 retbleed_mitigation = RETBLEED_MITIGATION_EIBRS;
903 break;
904 default:
905 pr_err(RETBLEED_INTEL_MSG);
906 }
907 }
908
909 pr_info("%s\n", retbleed_strings[retbleed_mitigation]);
910 }
911
912 #undef pr_fmt
913 #define pr_fmt(fmt) "Spectre V2 : " fmt
914
915 static enum spectre_v2_user_mitigation spectre_v2_user_stibp __ro_after_init =
916 SPECTRE_V2_USER_NONE;
917 static enum spectre_v2_user_mitigation spectre_v2_user_ibpb __ro_after_init =
918 SPECTRE_V2_USER_NONE;
919
920 #ifdef CONFIG_RETPOLINE
921 static bool spectre_v2_bad_module;
922
retpoline_module_ok(bool has_retpoline)923 bool retpoline_module_ok(bool has_retpoline)
924 {
925 if (spectre_v2_enabled == SPECTRE_V2_NONE || has_retpoline)
926 return true;
927
928 pr_err("System may be vulnerable to spectre v2\n");
929 spectre_v2_bad_module = true;
930 return false;
931 }
932
spectre_v2_module_string(void)933 static inline const char *spectre_v2_module_string(void)
934 {
935 return spectre_v2_bad_module ? " - vulnerable module loaded" : "";
936 }
937 #else
spectre_v2_module_string(void)938 static inline const char *spectre_v2_module_string(void) { return ""; }
939 #endif
940
941 #define SPECTRE_V2_LFENCE_MSG "WARNING: LFENCE mitigation is not recommended for this CPU, data leaks possible!\n"
942 #define SPECTRE_V2_EIBRS_EBPF_MSG "WARNING: Unprivileged eBPF is enabled with eIBRS on, data leaks possible via Spectre v2 BHB attacks!\n"
943 #define SPECTRE_V2_EIBRS_LFENCE_EBPF_SMT_MSG "WARNING: Unprivileged eBPF is enabled with eIBRS+LFENCE mitigation and SMT, data leaks possible via Spectre v2 BHB attacks!\n"
944 #define SPECTRE_V2_IBRS_PERF_MSG "WARNING: IBRS mitigation selected on Enhanced IBRS CPU, this may cause unnecessary performance loss\n"
945
946 #ifdef CONFIG_BPF_SYSCALL
unpriv_ebpf_notify(int new_state)947 void unpriv_ebpf_notify(int new_state)
948 {
949 if (new_state)
950 return;
951
952 /* Unprivileged eBPF is enabled */
953
954 switch (spectre_v2_enabled) {
955 case SPECTRE_V2_EIBRS:
956 pr_err(SPECTRE_V2_EIBRS_EBPF_MSG);
957 break;
958 case SPECTRE_V2_EIBRS_LFENCE:
959 if (sched_smt_active())
960 pr_err(SPECTRE_V2_EIBRS_LFENCE_EBPF_SMT_MSG);
961 break;
962 default:
963 break;
964 }
965 }
966 #endif
967
match_option(const char * arg,int arglen,const char * opt)968 static inline bool match_option(const char *arg, int arglen, const char *opt)
969 {
970 int len = strlen(opt);
971
972 return len == arglen && !strncmp(arg, opt, len);
973 }
974
975 /* The kernel command line selection for spectre v2 */
976 enum spectre_v2_mitigation_cmd {
977 SPECTRE_V2_CMD_NONE,
978 SPECTRE_V2_CMD_AUTO,
979 SPECTRE_V2_CMD_FORCE,
980 SPECTRE_V2_CMD_RETPOLINE,
981 SPECTRE_V2_CMD_RETPOLINE_GENERIC,
982 SPECTRE_V2_CMD_RETPOLINE_LFENCE,
983 SPECTRE_V2_CMD_EIBRS,
984 SPECTRE_V2_CMD_EIBRS_RETPOLINE,
985 SPECTRE_V2_CMD_EIBRS_LFENCE,
986 SPECTRE_V2_CMD_IBRS,
987 };
988
989 enum spectre_v2_user_cmd {
990 SPECTRE_V2_USER_CMD_NONE,
991 SPECTRE_V2_USER_CMD_AUTO,
992 SPECTRE_V2_USER_CMD_FORCE,
993 SPECTRE_V2_USER_CMD_PRCTL,
994 SPECTRE_V2_USER_CMD_PRCTL_IBPB,
995 SPECTRE_V2_USER_CMD_SECCOMP,
996 SPECTRE_V2_USER_CMD_SECCOMP_IBPB,
997 };
998
999 static const char * const spectre_v2_user_strings[] = {
1000 [SPECTRE_V2_USER_NONE] = "User space: Vulnerable",
1001 [SPECTRE_V2_USER_STRICT] = "User space: Mitigation: STIBP protection",
1002 [SPECTRE_V2_USER_STRICT_PREFERRED] = "User space: Mitigation: STIBP always-on protection",
1003 [SPECTRE_V2_USER_PRCTL] = "User space: Mitigation: STIBP via prctl",
1004 [SPECTRE_V2_USER_SECCOMP] = "User space: Mitigation: STIBP via seccomp and prctl",
1005 };
1006
1007 static const struct {
1008 const char *option;
1009 enum spectre_v2_user_cmd cmd;
1010 bool secure;
1011 } v2_user_options[] __initconst = {
1012 { "auto", SPECTRE_V2_USER_CMD_AUTO, false },
1013 { "off", SPECTRE_V2_USER_CMD_NONE, false },
1014 { "on", SPECTRE_V2_USER_CMD_FORCE, true },
1015 { "prctl", SPECTRE_V2_USER_CMD_PRCTL, false },
1016 { "prctl,ibpb", SPECTRE_V2_USER_CMD_PRCTL_IBPB, false },
1017 { "seccomp", SPECTRE_V2_USER_CMD_SECCOMP, false },
1018 { "seccomp,ibpb", SPECTRE_V2_USER_CMD_SECCOMP_IBPB, false },
1019 };
1020
spec_v2_user_print_cond(const char * reason,bool secure)1021 static void __init spec_v2_user_print_cond(const char *reason, bool secure)
1022 {
1023 if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2) != secure)
1024 pr_info("spectre_v2_user=%s forced on command line.\n", reason);
1025 }
1026
1027 static __ro_after_init enum spectre_v2_mitigation_cmd spectre_v2_cmd;
1028
1029 static enum spectre_v2_user_cmd __init
spectre_v2_parse_user_cmdline(void)1030 spectre_v2_parse_user_cmdline(void)
1031 {
1032 char arg[20];
1033 int ret, i;
1034
1035 switch (spectre_v2_cmd) {
1036 case SPECTRE_V2_CMD_NONE:
1037 return SPECTRE_V2_USER_CMD_NONE;
1038 case SPECTRE_V2_CMD_FORCE:
1039 return SPECTRE_V2_USER_CMD_FORCE;
1040 default:
1041 break;
1042 }
1043
1044 ret = cmdline_find_option(boot_command_line, "spectre_v2_user",
1045 arg, sizeof(arg));
1046 if (ret < 0)
1047 return SPECTRE_V2_USER_CMD_AUTO;
1048
1049 for (i = 0; i < ARRAY_SIZE(v2_user_options); i++) {
1050 if (match_option(arg, ret, v2_user_options[i].option)) {
1051 spec_v2_user_print_cond(v2_user_options[i].option,
1052 v2_user_options[i].secure);
1053 return v2_user_options[i].cmd;
1054 }
1055 }
1056
1057 pr_err("Unknown user space protection option (%s). Switching to AUTO select\n", arg);
1058 return SPECTRE_V2_USER_CMD_AUTO;
1059 }
1060
spectre_v2_in_ibrs_mode(enum spectre_v2_mitigation mode)1061 static inline bool spectre_v2_in_ibrs_mode(enum spectre_v2_mitigation mode)
1062 {
1063 return mode == SPECTRE_V2_IBRS ||
1064 mode == SPECTRE_V2_EIBRS ||
1065 mode == SPECTRE_V2_EIBRS_RETPOLINE ||
1066 mode == SPECTRE_V2_EIBRS_LFENCE;
1067 }
1068
1069 static void __init
spectre_v2_user_select_mitigation(void)1070 spectre_v2_user_select_mitigation(void)
1071 {
1072 enum spectre_v2_user_mitigation mode = SPECTRE_V2_USER_NONE;
1073 bool smt_possible = IS_ENABLED(CONFIG_SMP);
1074 enum spectre_v2_user_cmd cmd;
1075
1076 if (!boot_cpu_has(X86_FEATURE_IBPB) && !boot_cpu_has(X86_FEATURE_STIBP))
1077 return;
1078
1079 if (cpu_smt_control == CPU_SMT_FORCE_DISABLED ||
1080 cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
1081 smt_possible = false;
1082
1083 cmd = spectre_v2_parse_user_cmdline();
1084 switch (cmd) {
1085 case SPECTRE_V2_USER_CMD_NONE:
1086 goto set_mode;
1087 case SPECTRE_V2_USER_CMD_FORCE:
1088 mode = SPECTRE_V2_USER_STRICT;
1089 break;
1090 case SPECTRE_V2_USER_CMD_PRCTL:
1091 case SPECTRE_V2_USER_CMD_PRCTL_IBPB:
1092 mode = SPECTRE_V2_USER_PRCTL;
1093 break;
1094 case SPECTRE_V2_USER_CMD_AUTO:
1095 case SPECTRE_V2_USER_CMD_SECCOMP:
1096 case SPECTRE_V2_USER_CMD_SECCOMP_IBPB:
1097 if (IS_ENABLED(CONFIG_SECCOMP))
1098 mode = SPECTRE_V2_USER_SECCOMP;
1099 else
1100 mode = SPECTRE_V2_USER_PRCTL;
1101 break;
1102 }
1103
1104 /* Initialize Indirect Branch Prediction Barrier */
1105 if (boot_cpu_has(X86_FEATURE_IBPB)) {
1106 setup_force_cpu_cap(X86_FEATURE_USE_IBPB);
1107
1108 spectre_v2_user_ibpb = mode;
1109 switch (cmd) {
1110 case SPECTRE_V2_USER_CMD_FORCE:
1111 case SPECTRE_V2_USER_CMD_PRCTL_IBPB:
1112 case SPECTRE_V2_USER_CMD_SECCOMP_IBPB:
1113 static_branch_enable(&switch_mm_always_ibpb);
1114 spectre_v2_user_ibpb = SPECTRE_V2_USER_STRICT;
1115 break;
1116 case SPECTRE_V2_USER_CMD_PRCTL:
1117 case SPECTRE_V2_USER_CMD_AUTO:
1118 case SPECTRE_V2_USER_CMD_SECCOMP:
1119 static_branch_enable(&switch_mm_cond_ibpb);
1120 break;
1121 default:
1122 break;
1123 }
1124
1125 pr_info("mitigation: Enabling %s Indirect Branch Prediction Barrier\n",
1126 static_key_enabled(&switch_mm_always_ibpb) ?
1127 "always-on" : "conditional");
1128 }
1129
1130 /*
1131 * If no STIBP, IBRS or enhanced IBRS is enabled, or SMT impossible,
1132 * STIBP is not required.
1133 */
1134 if (!boot_cpu_has(X86_FEATURE_STIBP) ||
1135 !smt_possible ||
1136 spectre_v2_in_ibrs_mode(spectre_v2_enabled))
1137 return;
1138
1139 /*
1140 * At this point, an STIBP mode other than "off" has been set.
1141 * If STIBP support is not being forced, check if STIBP always-on
1142 * is preferred.
1143 */
1144 if (mode != SPECTRE_V2_USER_STRICT &&
1145 boot_cpu_has(X86_FEATURE_AMD_STIBP_ALWAYS_ON))
1146 mode = SPECTRE_V2_USER_STRICT_PREFERRED;
1147
1148 if (retbleed_mitigation == RETBLEED_MITIGATION_UNRET ||
1149 retbleed_mitigation == RETBLEED_MITIGATION_IBPB) {
1150 if (mode != SPECTRE_V2_USER_STRICT &&
1151 mode != SPECTRE_V2_USER_STRICT_PREFERRED)
1152 pr_info("Selecting STIBP always-on mode to complement retbleed mitigation\n");
1153 mode = SPECTRE_V2_USER_STRICT_PREFERRED;
1154 }
1155
1156 spectre_v2_user_stibp = mode;
1157
1158 set_mode:
1159 pr_info("%s\n", spectre_v2_user_strings[mode]);
1160 }
1161
1162 static const char * const spectre_v2_strings[] = {
1163 [SPECTRE_V2_NONE] = "Vulnerable",
1164 [SPECTRE_V2_RETPOLINE] = "Mitigation: Retpolines",
1165 [SPECTRE_V2_LFENCE] = "Mitigation: LFENCE",
1166 [SPECTRE_V2_EIBRS] = "Mitigation: Enhanced IBRS",
1167 [SPECTRE_V2_EIBRS_LFENCE] = "Mitigation: Enhanced IBRS + LFENCE",
1168 [SPECTRE_V2_EIBRS_RETPOLINE] = "Mitigation: Enhanced IBRS + Retpolines",
1169 [SPECTRE_V2_IBRS] = "Mitigation: IBRS",
1170 };
1171
1172 static const struct {
1173 const char *option;
1174 enum spectre_v2_mitigation_cmd cmd;
1175 bool secure;
1176 } mitigation_options[] __initconst = {
1177 { "off", SPECTRE_V2_CMD_NONE, false },
1178 { "on", SPECTRE_V2_CMD_FORCE, true },
1179 { "retpoline", SPECTRE_V2_CMD_RETPOLINE, false },
1180 { "retpoline,amd", SPECTRE_V2_CMD_RETPOLINE_LFENCE, false },
1181 { "retpoline,lfence", SPECTRE_V2_CMD_RETPOLINE_LFENCE, false },
1182 { "retpoline,generic", SPECTRE_V2_CMD_RETPOLINE_GENERIC, false },
1183 { "eibrs", SPECTRE_V2_CMD_EIBRS, false },
1184 { "eibrs,lfence", SPECTRE_V2_CMD_EIBRS_LFENCE, false },
1185 { "eibrs,retpoline", SPECTRE_V2_CMD_EIBRS_RETPOLINE, false },
1186 { "auto", SPECTRE_V2_CMD_AUTO, false },
1187 { "ibrs", SPECTRE_V2_CMD_IBRS, false },
1188 };
1189
spec_v2_print_cond(const char * reason,bool secure)1190 static void __init spec_v2_print_cond(const char *reason, bool secure)
1191 {
1192 if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2) != secure)
1193 pr_info("%s selected on command line.\n", reason);
1194 }
1195
spectre_v2_parse_cmdline(void)1196 static enum spectre_v2_mitigation_cmd __init spectre_v2_parse_cmdline(void)
1197 {
1198 enum spectre_v2_mitigation_cmd cmd = SPECTRE_V2_CMD_AUTO;
1199 char arg[20];
1200 int ret, i;
1201
1202 if (cmdline_find_option_bool(boot_command_line, "nospectre_v2") ||
1203 cpu_mitigations_off())
1204 return SPECTRE_V2_CMD_NONE;
1205
1206 ret = cmdline_find_option(boot_command_line, "spectre_v2", arg, sizeof(arg));
1207 if (ret < 0)
1208 return SPECTRE_V2_CMD_AUTO;
1209
1210 for (i = 0; i < ARRAY_SIZE(mitigation_options); i++) {
1211 if (!match_option(arg, ret, mitigation_options[i].option))
1212 continue;
1213 cmd = mitigation_options[i].cmd;
1214 break;
1215 }
1216
1217 if (i >= ARRAY_SIZE(mitigation_options)) {
1218 pr_err("unknown option (%s). Switching to AUTO select\n", arg);
1219 return SPECTRE_V2_CMD_AUTO;
1220 }
1221
1222 if ((cmd == SPECTRE_V2_CMD_RETPOLINE ||
1223 cmd == SPECTRE_V2_CMD_RETPOLINE_LFENCE ||
1224 cmd == SPECTRE_V2_CMD_RETPOLINE_GENERIC ||
1225 cmd == SPECTRE_V2_CMD_EIBRS_LFENCE ||
1226 cmd == SPECTRE_V2_CMD_EIBRS_RETPOLINE) &&
1227 !IS_ENABLED(CONFIG_RETPOLINE)) {
1228 pr_err("%s selected but not compiled in. Switching to AUTO select\n",
1229 mitigation_options[i].option);
1230 return SPECTRE_V2_CMD_AUTO;
1231 }
1232
1233 if ((cmd == SPECTRE_V2_CMD_EIBRS ||
1234 cmd == SPECTRE_V2_CMD_EIBRS_LFENCE ||
1235 cmd == SPECTRE_V2_CMD_EIBRS_RETPOLINE) &&
1236 !boot_cpu_has(X86_FEATURE_IBRS_ENHANCED)) {
1237 pr_err("%s selected but CPU doesn't have eIBRS. Switching to AUTO select\n",
1238 mitigation_options[i].option);
1239 return SPECTRE_V2_CMD_AUTO;
1240 }
1241
1242 if ((cmd == SPECTRE_V2_CMD_RETPOLINE_LFENCE ||
1243 cmd == SPECTRE_V2_CMD_EIBRS_LFENCE) &&
1244 !boot_cpu_has(X86_FEATURE_LFENCE_RDTSC)) {
1245 pr_err("%s selected, but CPU doesn't have a serializing LFENCE. Switching to AUTO select\n",
1246 mitigation_options[i].option);
1247 return SPECTRE_V2_CMD_AUTO;
1248 }
1249
1250 if (cmd == SPECTRE_V2_CMD_IBRS && !IS_ENABLED(CONFIG_CPU_IBRS_ENTRY)) {
1251 pr_err("%s selected but not compiled in. Switching to AUTO select\n",
1252 mitigation_options[i].option);
1253 return SPECTRE_V2_CMD_AUTO;
1254 }
1255
1256 if (cmd == SPECTRE_V2_CMD_IBRS && boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) {
1257 pr_err("%s selected but not Intel CPU. Switching to AUTO select\n",
1258 mitigation_options[i].option);
1259 return SPECTRE_V2_CMD_AUTO;
1260 }
1261
1262 if (cmd == SPECTRE_V2_CMD_IBRS && !boot_cpu_has(X86_FEATURE_IBRS)) {
1263 pr_err("%s selected but CPU doesn't have IBRS. Switching to AUTO select\n",
1264 mitigation_options[i].option);
1265 return SPECTRE_V2_CMD_AUTO;
1266 }
1267
1268 if (cmd == SPECTRE_V2_CMD_IBRS && boot_cpu_has(X86_FEATURE_XENPV)) {
1269 pr_err("%s selected but running as XenPV guest. Switching to AUTO select\n",
1270 mitigation_options[i].option);
1271 return SPECTRE_V2_CMD_AUTO;
1272 }
1273
1274 spec_v2_print_cond(mitigation_options[i].option,
1275 mitigation_options[i].secure);
1276 return cmd;
1277 }
1278
spectre_v2_select_retpoline(void)1279 static enum spectre_v2_mitigation __init spectre_v2_select_retpoline(void)
1280 {
1281 if (!IS_ENABLED(CONFIG_RETPOLINE)) {
1282 pr_err("Kernel not compiled with retpoline; no mitigation available!");
1283 return SPECTRE_V2_NONE;
1284 }
1285
1286 return SPECTRE_V2_RETPOLINE;
1287 }
1288
1289 /* Disable in-kernel use of non-RSB RET predictors */
spec_ctrl_disable_kernel_rrsba(void)1290 static void __init spec_ctrl_disable_kernel_rrsba(void)
1291 {
1292 u64 ia32_cap;
1293
1294 if (!boot_cpu_has(X86_FEATURE_RRSBA_CTRL))
1295 return;
1296
1297 ia32_cap = x86_read_arch_cap_msr();
1298
1299 if (ia32_cap & ARCH_CAP_RRSBA) {
1300 x86_spec_ctrl_base |= SPEC_CTRL_RRSBA_DIS_S;
1301 update_spec_ctrl(x86_spec_ctrl_base);
1302 }
1303 }
1304
spectre_v2_determine_rsb_fill_type_at_vmexit(enum spectre_v2_mitigation mode)1305 static void __init spectre_v2_determine_rsb_fill_type_at_vmexit(enum spectre_v2_mitigation mode)
1306 {
1307 /*
1308 * Similar to context switches, there are two types of RSB attacks
1309 * after VM exit:
1310 *
1311 * 1) RSB underflow
1312 *
1313 * 2) Poisoned RSB entry
1314 *
1315 * When retpoline is enabled, both are mitigated by filling/clearing
1316 * the RSB.
1317 *
1318 * When IBRS is enabled, while #1 would be mitigated by the IBRS branch
1319 * prediction isolation protections, RSB still needs to be cleared
1320 * because of #2. Note that SMEP provides no protection here, unlike
1321 * user-space-poisoned RSB entries.
1322 *
1323 * eIBRS should protect against RSB poisoning, but if the EIBRS_PBRSB
1324 * bug is present then a LITE version of RSB protection is required,
1325 * just a single call needs to retire before a RET is executed.
1326 */
1327 switch (mode) {
1328 case SPECTRE_V2_NONE:
1329 return;
1330
1331 case SPECTRE_V2_EIBRS_LFENCE:
1332 case SPECTRE_V2_EIBRS:
1333 if (boot_cpu_has_bug(X86_BUG_EIBRS_PBRSB)) {
1334 setup_force_cpu_cap(X86_FEATURE_RSB_VMEXIT_LITE);
1335 pr_info("Spectre v2 / PBRSB-eIBRS: Retire a single CALL on VMEXIT\n");
1336 }
1337 return;
1338
1339 case SPECTRE_V2_EIBRS_RETPOLINE:
1340 case SPECTRE_V2_RETPOLINE:
1341 case SPECTRE_V2_LFENCE:
1342 case SPECTRE_V2_IBRS:
1343 setup_force_cpu_cap(X86_FEATURE_RSB_VMEXIT);
1344 pr_info("Spectre v2 / SpectreRSB : Filling RSB on VMEXIT\n");
1345 return;
1346 }
1347
1348 pr_warn_once("Unknown Spectre v2 mode, disabling RSB mitigation at VM exit");
1349 dump_stack();
1350 }
1351
spectre_v2_select_mitigation(void)1352 static void __init spectre_v2_select_mitigation(void)
1353 {
1354 enum spectre_v2_mitigation_cmd cmd = spectre_v2_parse_cmdline();
1355 enum spectre_v2_mitigation mode = SPECTRE_V2_NONE;
1356
1357 /*
1358 * If the CPU is not affected and the command line mode is NONE or AUTO
1359 * then nothing to do.
1360 */
1361 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2) &&
1362 (cmd == SPECTRE_V2_CMD_NONE || cmd == SPECTRE_V2_CMD_AUTO))
1363 return;
1364
1365 switch (cmd) {
1366 case SPECTRE_V2_CMD_NONE:
1367 return;
1368
1369 case SPECTRE_V2_CMD_FORCE:
1370 case SPECTRE_V2_CMD_AUTO:
1371 if (boot_cpu_has(X86_FEATURE_IBRS_ENHANCED)) {
1372 mode = SPECTRE_V2_EIBRS;
1373 break;
1374 }
1375
1376 if (IS_ENABLED(CONFIG_CPU_IBRS_ENTRY) &&
1377 boot_cpu_has_bug(X86_BUG_RETBLEED) &&
1378 retbleed_cmd != RETBLEED_CMD_OFF &&
1379 boot_cpu_has(X86_FEATURE_IBRS) &&
1380 boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) {
1381 mode = SPECTRE_V2_IBRS;
1382 break;
1383 }
1384
1385 mode = spectre_v2_select_retpoline();
1386 break;
1387
1388 case SPECTRE_V2_CMD_RETPOLINE_LFENCE:
1389 pr_err(SPECTRE_V2_LFENCE_MSG);
1390 mode = SPECTRE_V2_LFENCE;
1391 break;
1392
1393 case SPECTRE_V2_CMD_RETPOLINE_GENERIC:
1394 mode = SPECTRE_V2_RETPOLINE;
1395 break;
1396
1397 case SPECTRE_V2_CMD_RETPOLINE:
1398 mode = spectre_v2_select_retpoline();
1399 break;
1400
1401 case SPECTRE_V2_CMD_IBRS:
1402 mode = SPECTRE_V2_IBRS;
1403 break;
1404
1405 case SPECTRE_V2_CMD_EIBRS:
1406 mode = SPECTRE_V2_EIBRS;
1407 break;
1408
1409 case SPECTRE_V2_CMD_EIBRS_LFENCE:
1410 mode = SPECTRE_V2_EIBRS_LFENCE;
1411 break;
1412
1413 case SPECTRE_V2_CMD_EIBRS_RETPOLINE:
1414 mode = SPECTRE_V2_EIBRS_RETPOLINE;
1415 break;
1416 }
1417
1418 if (mode == SPECTRE_V2_EIBRS && unprivileged_ebpf_enabled())
1419 pr_err(SPECTRE_V2_EIBRS_EBPF_MSG);
1420
1421 if (spectre_v2_in_ibrs_mode(mode)) {
1422 x86_spec_ctrl_base |= SPEC_CTRL_IBRS;
1423 update_spec_ctrl(x86_spec_ctrl_base);
1424 }
1425
1426 switch (mode) {
1427 case SPECTRE_V2_NONE:
1428 case SPECTRE_V2_EIBRS:
1429 break;
1430
1431 case SPECTRE_V2_IBRS:
1432 setup_force_cpu_cap(X86_FEATURE_KERNEL_IBRS);
1433 if (boot_cpu_has(X86_FEATURE_IBRS_ENHANCED))
1434 pr_warn(SPECTRE_V2_IBRS_PERF_MSG);
1435 break;
1436
1437 case SPECTRE_V2_LFENCE:
1438 case SPECTRE_V2_EIBRS_LFENCE:
1439 setup_force_cpu_cap(X86_FEATURE_RETPOLINE_LFENCE);
1440 fallthrough;
1441
1442 case SPECTRE_V2_RETPOLINE:
1443 case SPECTRE_V2_EIBRS_RETPOLINE:
1444 setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
1445 break;
1446 }
1447
1448 /*
1449 * Disable alternate RSB predictions in kernel when indirect CALLs and
1450 * JMPs gets protection against BHI and Intramode-BTI, but RET
1451 * prediction from a non-RSB predictor is still a risk.
1452 */
1453 if (mode == SPECTRE_V2_EIBRS_LFENCE ||
1454 mode == SPECTRE_V2_EIBRS_RETPOLINE ||
1455 mode == SPECTRE_V2_RETPOLINE)
1456 spec_ctrl_disable_kernel_rrsba();
1457
1458 spectre_v2_enabled = mode;
1459 pr_info("%s\n", spectre_v2_strings[mode]);
1460
1461 /*
1462 * If Spectre v2 protection has been enabled, fill the RSB during a
1463 * context switch. In general there are two types of RSB attacks
1464 * across context switches, for which the CALLs/RETs may be unbalanced.
1465 *
1466 * 1) RSB underflow
1467 *
1468 * Some Intel parts have "bottomless RSB". When the RSB is empty,
1469 * speculated return targets may come from the branch predictor,
1470 * which could have a user-poisoned BTB or BHB entry.
1471 *
1472 * AMD has it even worse: *all* returns are speculated from the BTB,
1473 * regardless of the state of the RSB.
1474 *
1475 * When IBRS or eIBRS is enabled, the "user -> kernel" attack
1476 * scenario is mitigated by the IBRS branch prediction isolation
1477 * properties, so the RSB buffer filling wouldn't be necessary to
1478 * protect against this type of attack.
1479 *
1480 * The "user -> user" attack scenario is mitigated by RSB filling.
1481 *
1482 * 2) Poisoned RSB entry
1483 *
1484 * If the 'next' in-kernel return stack is shorter than 'prev',
1485 * 'next' could be tricked into speculating with a user-poisoned RSB
1486 * entry.
1487 *
1488 * The "user -> kernel" attack scenario is mitigated by SMEP and
1489 * eIBRS.
1490 *
1491 * The "user -> user" scenario, also known as SpectreBHB, requires
1492 * RSB clearing.
1493 *
1494 * So to mitigate all cases, unconditionally fill RSB on context
1495 * switches.
1496 *
1497 * FIXME: Is this pointless for retbleed-affected AMD?
1498 */
1499 setup_force_cpu_cap(X86_FEATURE_RSB_CTXSW);
1500 pr_info("Spectre v2 / SpectreRSB mitigation: Filling RSB on context switch\n");
1501
1502 spectre_v2_determine_rsb_fill_type_at_vmexit(mode);
1503
1504 /*
1505 * Retpoline protects the kernel, but doesn't protect firmware. IBRS
1506 * and Enhanced IBRS protect firmware too, so enable IBRS around
1507 * firmware calls only when IBRS / Enhanced IBRS aren't otherwise
1508 * enabled.
1509 *
1510 * Use "mode" to check Enhanced IBRS instead of boot_cpu_has(), because
1511 * the user might select retpoline on the kernel command line and if
1512 * the CPU supports Enhanced IBRS, kernel might un-intentionally not
1513 * enable IBRS around firmware calls.
1514 */
1515 if (boot_cpu_has_bug(X86_BUG_RETBLEED) &&
1516 boot_cpu_has(X86_FEATURE_IBPB) &&
1517 (boot_cpu_data.x86_vendor == X86_VENDOR_AMD ||
1518 boot_cpu_data.x86_vendor == X86_VENDOR_HYGON)) {
1519
1520 if (retbleed_cmd != RETBLEED_CMD_IBPB) {
1521 setup_force_cpu_cap(X86_FEATURE_USE_IBPB_FW);
1522 pr_info("Enabling Speculation Barrier for firmware calls\n");
1523 }
1524
1525 } else if (boot_cpu_has(X86_FEATURE_IBRS) && !spectre_v2_in_ibrs_mode(mode)) {
1526 setup_force_cpu_cap(X86_FEATURE_USE_IBRS_FW);
1527 pr_info("Enabling Restricted Speculation for firmware calls\n");
1528 }
1529
1530 /* Set up IBPB and STIBP depending on the general spectre V2 command */
1531 spectre_v2_cmd = cmd;
1532 }
1533
update_stibp_msr(void * __unused)1534 static void update_stibp_msr(void * __unused)
1535 {
1536 u64 val = spec_ctrl_current() | (x86_spec_ctrl_base & SPEC_CTRL_STIBP);
1537 update_spec_ctrl(val);
1538 }
1539
1540 /* Update x86_spec_ctrl_base in case SMT state changed. */
update_stibp_strict(void)1541 static void update_stibp_strict(void)
1542 {
1543 u64 mask = x86_spec_ctrl_base & ~SPEC_CTRL_STIBP;
1544
1545 if (sched_smt_active())
1546 mask |= SPEC_CTRL_STIBP;
1547
1548 if (mask == x86_spec_ctrl_base)
1549 return;
1550
1551 pr_info("Update user space SMT mitigation: STIBP %s\n",
1552 mask & SPEC_CTRL_STIBP ? "always-on" : "off");
1553 x86_spec_ctrl_base = mask;
1554 on_each_cpu(update_stibp_msr, NULL, 1);
1555 }
1556
1557 /* Update the static key controlling the evaluation of TIF_SPEC_IB */
update_indir_branch_cond(void)1558 static void update_indir_branch_cond(void)
1559 {
1560 if (sched_smt_active())
1561 static_branch_enable(&switch_to_cond_stibp);
1562 else
1563 static_branch_disable(&switch_to_cond_stibp);
1564 }
1565
1566 #undef pr_fmt
1567 #define pr_fmt(fmt) fmt
1568
1569 /* Update the static key controlling the MDS CPU buffer clear in idle */
update_mds_branch_idle(void)1570 static void update_mds_branch_idle(void)
1571 {
1572 u64 ia32_cap = x86_read_arch_cap_msr();
1573
1574 /*
1575 * Enable the idle clearing if SMT is active on CPUs which are
1576 * affected only by MSBDS and not any other MDS variant.
1577 *
1578 * The other variants cannot be mitigated when SMT is enabled, so
1579 * clearing the buffers on idle just to prevent the Store Buffer
1580 * repartitioning leak would be a window dressing exercise.
1581 */
1582 if (!boot_cpu_has_bug(X86_BUG_MSBDS_ONLY))
1583 return;
1584
1585 if (sched_smt_active()) {
1586 static_branch_enable(&mds_idle_clear);
1587 } else if (mmio_mitigation == MMIO_MITIGATION_OFF ||
1588 (ia32_cap & ARCH_CAP_FBSDP_NO)) {
1589 static_branch_disable(&mds_idle_clear);
1590 }
1591 }
1592
1593 #define MDS_MSG_SMT "MDS CPU bug present and SMT on, data leak possible. See https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/mds.html for more details.\n"
1594 #define TAA_MSG_SMT "TAA CPU bug present and SMT on, data leak possible. See https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/tsx_async_abort.html for more details.\n"
1595 #define MMIO_MSG_SMT "MMIO Stale Data CPU bug present and SMT on, data leak possible. See https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/processor_mmio_stale_data.html for more details.\n"
1596
cpu_bugs_smt_update(void)1597 void cpu_bugs_smt_update(void)
1598 {
1599 mutex_lock(&spec_ctrl_mutex);
1600
1601 if (sched_smt_active() && unprivileged_ebpf_enabled() &&
1602 spectre_v2_enabled == SPECTRE_V2_EIBRS_LFENCE)
1603 pr_warn_once(SPECTRE_V2_EIBRS_LFENCE_EBPF_SMT_MSG);
1604
1605 switch (spectre_v2_user_stibp) {
1606 case SPECTRE_V2_USER_NONE:
1607 break;
1608 case SPECTRE_V2_USER_STRICT:
1609 case SPECTRE_V2_USER_STRICT_PREFERRED:
1610 update_stibp_strict();
1611 break;
1612 case SPECTRE_V2_USER_PRCTL:
1613 case SPECTRE_V2_USER_SECCOMP:
1614 update_indir_branch_cond();
1615 break;
1616 }
1617
1618 switch (mds_mitigation) {
1619 case MDS_MITIGATION_FULL:
1620 case MDS_MITIGATION_VMWERV:
1621 if (sched_smt_active() && !boot_cpu_has(X86_BUG_MSBDS_ONLY))
1622 pr_warn_once(MDS_MSG_SMT);
1623 update_mds_branch_idle();
1624 break;
1625 case MDS_MITIGATION_OFF:
1626 break;
1627 }
1628
1629 switch (taa_mitigation) {
1630 case TAA_MITIGATION_VERW:
1631 case TAA_MITIGATION_UCODE_NEEDED:
1632 if (sched_smt_active())
1633 pr_warn_once(TAA_MSG_SMT);
1634 break;
1635 case TAA_MITIGATION_TSX_DISABLED:
1636 case TAA_MITIGATION_OFF:
1637 break;
1638 }
1639
1640 switch (mmio_mitigation) {
1641 case MMIO_MITIGATION_VERW:
1642 case MMIO_MITIGATION_UCODE_NEEDED:
1643 if (sched_smt_active())
1644 pr_warn_once(MMIO_MSG_SMT);
1645 break;
1646 case MMIO_MITIGATION_OFF:
1647 break;
1648 }
1649
1650 mutex_unlock(&spec_ctrl_mutex);
1651 }
1652
1653 #undef pr_fmt
1654 #define pr_fmt(fmt) "Speculative Store Bypass: " fmt
1655
1656 static enum ssb_mitigation ssb_mode __ro_after_init = SPEC_STORE_BYPASS_NONE;
1657
1658 /* The kernel command line selection */
1659 enum ssb_mitigation_cmd {
1660 SPEC_STORE_BYPASS_CMD_NONE,
1661 SPEC_STORE_BYPASS_CMD_AUTO,
1662 SPEC_STORE_BYPASS_CMD_ON,
1663 SPEC_STORE_BYPASS_CMD_PRCTL,
1664 SPEC_STORE_BYPASS_CMD_SECCOMP,
1665 };
1666
1667 static const char * const ssb_strings[] = {
1668 [SPEC_STORE_BYPASS_NONE] = "Vulnerable",
1669 [SPEC_STORE_BYPASS_DISABLE] = "Mitigation: Speculative Store Bypass disabled",
1670 [SPEC_STORE_BYPASS_PRCTL] = "Mitigation: Speculative Store Bypass disabled via prctl",
1671 [SPEC_STORE_BYPASS_SECCOMP] = "Mitigation: Speculative Store Bypass disabled via prctl and seccomp",
1672 };
1673
1674 static const struct {
1675 const char *option;
1676 enum ssb_mitigation_cmd cmd;
1677 } ssb_mitigation_options[] __initconst = {
1678 { "auto", SPEC_STORE_BYPASS_CMD_AUTO }, /* Platform decides */
1679 { "on", SPEC_STORE_BYPASS_CMD_ON }, /* Disable Speculative Store Bypass */
1680 { "off", SPEC_STORE_BYPASS_CMD_NONE }, /* Don't touch Speculative Store Bypass */
1681 { "prctl", SPEC_STORE_BYPASS_CMD_PRCTL }, /* Disable Speculative Store Bypass via prctl */
1682 { "seccomp", SPEC_STORE_BYPASS_CMD_SECCOMP }, /* Disable Speculative Store Bypass via prctl and seccomp */
1683 };
1684
ssb_parse_cmdline(void)1685 static enum ssb_mitigation_cmd __init ssb_parse_cmdline(void)
1686 {
1687 enum ssb_mitigation_cmd cmd = SPEC_STORE_BYPASS_CMD_AUTO;
1688 char arg[20];
1689 int ret, i;
1690
1691 if (cmdline_find_option_bool(boot_command_line, "nospec_store_bypass_disable") ||
1692 cpu_mitigations_off()) {
1693 return SPEC_STORE_BYPASS_CMD_NONE;
1694 } else {
1695 ret = cmdline_find_option(boot_command_line, "spec_store_bypass_disable",
1696 arg, sizeof(arg));
1697 if (ret < 0)
1698 return SPEC_STORE_BYPASS_CMD_AUTO;
1699
1700 for (i = 0; i < ARRAY_SIZE(ssb_mitigation_options); i++) {
1701 if (!match_option(arg, ret, ssb_mitigation_options[i].option))
1702 continue;
1703
1704 cmd = ssb_mitigation_options[i].cmd;
1705 break;
1706 }
1707
1708 if (i >= ARRAY_SIZE(ssb_mitigation_options)) {
1709 pr_err("unknown option (%s). Switching to AUTO select\n", arg);
1710 return SPEC_STORE_BYPASS_CMD_AUTO;
1711 }
1712 }
1713
1714 return cmd;
1715 }
1716
__ssb_select_mitigation(void)1717 static enum ssb_mitigation __init __ssb_select_mitigation(void)
1718 {
1719 enum ssb_mitigation mode = SPEC_STORE_BYPASS_NONE;
1720 enum ssb_mitigation_cmd cmd;
1721
1722 if (!boot_cpu_has(X86_FEATURE_SSBD))
1723 return mode;
1724
1725 cmd = ssb_parse_cmdline();
1726 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS) &&
1727 (cmd == SPEC_STORE_BYPASS_CMD_NONE ||
1728 cmd == SPEC_STORE_BYPASS_CMD_AUTO))
1729 return mode;
1730
1731 switch (cmd) {
1732 case SPEC_STORE_BYPASS_CMD_AUTO:
1733 case SPEC_STORE_BYPASS_CMD_SECCOMP:
1734 /*
1735 * Choose prctl+seccomp as the default mode if seccomp is
1736 * enabled.
1737 */
1738 if (IS_ENABLED(CONFIG_SECCOMP))
1739 mode = SPEC_STORE_BYPASS_SECCOMP;
1740 else
1741 mode = SPEC_STORE_BYPASS_PRCTL;
1742 break;
1743 case SPEC_STORE_BYPASS_CMD_ON:
1744 mode = SPEC_STORE_BYPASS_DISABLE;
1745 break;
1746 case SPEC_STORE_BYPASS_CMD_PRCTL:
1747 mode = SPEC_STORE_BYPASS_PRCTL;
1748 break;
1749 case SPEC_STORE_BYPASS_CMD_NONE:
1750 break;
1751 }
1752
1753 /*
1754 * We have three CPU feature flags that are in play here:
1755 * - X86_BUG_SPEC_STORE_BYPASS - CPU is susceptible.
1756 * - X86_FEATURE_SSBD - CPU is able to turn off speculative store bypass
1757 * - X86_FEATURE_SPEC_STORE_BYPASS_DISABLE - engage the mitigation
1758 */
1759 if (mode == SPEC_STORE_BYPASS_DISABLE) {
1760 setup_force_cpu_cap(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE);
1761 /*
1762 * Intel uses the SPEC CTRL MSR Bit(2) for this, while AMD may
1763 * use a completely different MSR and bit dependent on family.
1764 */
1765 if (!static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) &&
1766 !static_cpu_has(X86_FEATURE_AMD_SSBD)) {
1767 x86_amd_ssb_disable();
1768 } else {
1769 x86_spec_ctrl_base |= SPEC_CTRL_SSBD;
1770 update_spec_ctrl(x86_spec_ctrl_base);
1771 }
1772 }
1773
1774 return mode;
1775 }
1776
ssb_select_mitigation(void)1777 static void ssb_select_mitigation(void)
1778 {
1779 ssb_mode = __ssb_select_mitigation();
1780
1781 if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
1782 pr_info("%s\n", ssb_strings[ssb_mode]);
1783 }
1784
1785 #undef pr_fmt
1786 #define pr_fmt(fmt) "Speculation prctl: " fmt
1787
task_update_spec_tif(struct task_struct * tsk)1788 static void task_update_spec_tif(struct task_struct *tsk)
1789 {
1790 /* Force the update of the real TIF bits */
1791 set_tsk_thread_flag(tsk, TIF_SPEC_FORCE_UPDATE);
1792
1793 /*
1794 * Immediately update the speculation control MSRs for the current
1795 * task, but for a non-current task delay setting the CPU
1796 * mitigation until it is scheduled next.
1797 *
1798 * This can only happen for SECCOMP mitigation. For PRCTL it's
1799 * always the current task.
1800 */
1801 if (tsk == current)
1802 speculation_ctrl_update_current();
1803 }
1804
ssb_prctl_set(struct task_struct * task,unsigned long ctrl)1805 static int ssb_prctl_set(struct task_struct *task, unsigned long ctrl)
1806 {
1807 if (ssb_mode != SPEC_STORE_BYPASS_PRCTL &&
1808 ssb_mode != SPEC_STORE_BYPASS_SECCOMP)
1809 return -ENXIO;
1810
1811 switch (ctrl) {
1812 case PR_SPEC_ENABLE:
1813 /* If speculation is force disabled, enable is not allowed */
1814 if (task_spec_ssb_force_disable(task))
1815 return -EPERM;
1816 task_clear_spec_ssb_disable(task);
1817 task_clear_spec_ssb_noexec(task);
1818 task_update_spec_tif(task);
1819 break;
1820 case PR_SPEC_DISABLE:
1821 task_set_spec_ssb_disable(task);
1822 task_clear_spec_ssb_noexec(task);
1823 task_update_spec_tif(task);
1824 break;
1825 case PR_SPEC_FORCE_DISABLE:
1826 task_set_spec_ssb_disable(task);
1827 task_set_spec_ssb_force_disable(task);
1828 task_clear_spec_ssb_noexec(task);
1829 task_update_spec_tif(task);
1830 break;
1831 case PR_SPEC_DISABLE_NOEXEC:
1832 if (task_spec_ssb_force_disable(task))
1833 return -EPERM;
1834 task_set_spec_ssb_disable(task);
1835 task_set_spec_ssb_noexec(task);
1836 task_update_spec_tif(task);
1837 break;
1838 default:
1839 return -ERANGE;
1840 }
1841 return 0;
1842 }
1843
is_spec_ib_user_controlled(void)1844 static bool is_spec_ib_user_controlled(void)
1845 {
1846 return spectre_v2_user_ibpb == SPECTRE_V2_USER_PRCTL ||
1847 spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP ||
1848 spectre_v2_user_stibp == SPECTRE_V2_USER_PRCTL ||
1849 spectre_v2_user_stibp == SPECTRE_V2_USER_SECCOMP;
1850 }
1851
ib_prctl_set(struct task_struct * task,unsigned long ctrl)1852 static int ib_prctl_set(struct task_struct *task, unsigned long ctrl)
1853 {
1854 switch (ctrl) {
1855 case PR_SPEC_ENABLE:
1856 if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
1857 spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
1858 return 0;
1859
1860 /*
1861 * With strict mode for both IBPB and STIBP, the instruction
1862 * code paths avoid checking this task flag and instead,
1863 * unconditionally run the instruction. However, STIBP and IBPB
1864 * are independent and either can be set to conditionally
1865 * enabled regardless of the mode of the other.
1866 *
1867 * If either is set to conditional, allow the task flag to be
1868 * updated, unless it was force-disabled by a previous prctl
1869 * call. Currently, this is possible on an AMD CPU which has the
1870 * feature X86_FEATURE_AMD_STIBP_ALWAYS_ON. In this case, if the
1871 * kernel is booted with 'spectre_v2_user=seccomp', then
1872 * spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP and
1873 * spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED.
1874 */
1875 if (!is_spec_ib_user_controlled() ||
1876 task_spec_ib_force_disable(task))
1877 return -EPERM;
1878
1879 task_clear_spec_ib_disable(task);
1880 task_update_spec_tif(task);
1881 break;
1882 case PR_SPEC_DISABLE:
1883 case PR_SPEC_FORCE_DISABLE:
1884 /*
1885 * Indirect branch speculation is always allowed when
1886 * mitigation is force disabled.
1887 */
1888 if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
1889 spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
1890 return -EPERM;
1891
1892 if (!is_spec_ib_user_controlled())
1893 return 0;
1894
1895 task_set_spec_ib_disable(task);
1896 if (ctrl == PR_SPEC_FORCE_DISABLE)
1897 task_set_spec_ib_force_disable(task);
1898 task_update_spec_tif(task);
1899 break;
1900 default:
1901 return -ERANGE;
1902 }
1903 return 0;
1904 }
1905
arch_prctl_spec_ctrl_set(struct task_struct * task,unsigned long which,unsigned long ctrl)1906 int arch_prctl_spec_ctrl_set(struct task_struct *task, unsigned long which,
1907 unsigned long ctrl)
1908 {
1909 switch (which) {
1910 case PR_SPEC_STORE_BYPASS:
1911 return ssb_prctl_set(task, ctrl);
1912 case PR_SPEC_INDIRECT_BRANCH:
1913 return ib_prctl_set(task, ctrl);
1914 default:
1915 return -ENODEV;
1916 }
1917 }
1918
1919 #ifdef CONFIG_SECCOMP
arch_seccomp_spec_mitigate(struct task_struct * task)1920 void arch_seccomp_spec_mitigate(struct task_struct *task)
1921 {
1922 if (ssb_mode == SPEC_STORE_BYPASS_SECCOMP)
1923 ssb_prctl_set(task, PR_SPEC_FORCE_DISABLE);
1924 if (spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP ||
1925 spectre_v2_user_stibp == SPECTRE_V2_USER_SECCOMP)
1926 ib_prctl_set(task, PR_SPEC_FORCE_DISABLE);
1927 }
1928 #endif
1929
ssb_prctl_get(struct task_struct * task)1930 static int ssb_prctl_get(struct task_struct *task)
1931 {
1932 switch (ssb_mode) {
1933 case SPEC_STORE_BYPASS_DISABLE:
1934 return PR_SPEC_DISABLE;
1935 case SPEC_STORE_BYPASS_SECCOMP:
1936 case SPEC_STORE_BYPASS_PRCTL:
1937 if (task_spec_ssb_force_disable(task))
1938 return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE;
1939 if (task_spec_ssb_noexec(task))
1940 return PR_SPEC_PRCTL | PR_SPEC_DISABLE_NOEXEC;
1941 if (task_spec_ssb_disable(task))
1942 return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
1943 return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
1944 default:
1945 if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
1946 return PR_SPEC_ENABLE;
1947 return PR_SPEC_NOT_AFFECTED;
1948 }
1949 }
1950
ib_prctl_get(struct task_struct * task)1951 static int ib_prctl_get(struct task_struct *task)
1952 {
1953 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
1954 return PR_SPEC_NOT_AFFECTED;
1955
1956 if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
1957 spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
1958 return PR_SPEC_ENABLE;
1959 else if (is_spec_ib_user_controlled()) {
1960 if (task_spec_ib_force_disable(task))
1961 return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE;
1962 if (task_spec_ib_disable(task))
1963 return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
1964 return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
1965 } else if (spectre_v2_user_ibpb == SPECTRE_V2_USER_STRICT ||
1966 spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT ||
1967 spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED)
1968 return PR_SPEC_DISABLE;
1969 else
1970 return PR_SPEC_NOT_AFFECTED;
1971 }
1972
arch_prctl_spec_ctrl_get(struct task_struct * task,unsigned long which)1973 int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which)
1974 {
1975 switch (which) {
1976 case PR_SPEC_STORE_BYPASS:
1977 return ssb_prctl_get(task);
1978 case PR_SPEC_INDIRECT_BRANCH:
1979 return ib_prctl_get(task);
1980 default:
1981 return -ENODEV;
1982 }
1983 }
1984
x86_spec_ctrl_setup_ap(void)1985 void x86_spec_ctrl_setup_ap(void)
1986 {
1987 if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
1988 update_spec_ctrl(x86_spec_ctrl_base);
1989
1990 if (ssb_mode == SPEC_STORE_BYPASS_DISABLE)
1991 x86_amd_ssb_disable();
1992 }
1993
1994 bool itlb_multihit_kvm_mitigation;
1995 EXPORT_SYMBOL_GPL(itlb_multihit_kvm_mitigation);
1996
1997 #undef pr_fmt
1998 #define pr_fmt(fmt) "L1TF: " fmt
1999
2000 /* Default mitigation for L1TF-affected CPUs */
2001 enum l1tf_mitigations l1tf_mitigation __ro_after_init = L1TF_MITIGATION_FLUSH;
2002 #if IS_ENABLED(CONFIG_KVM_INTEL)
2003 EXPORT_SYMBOL_GPL(l1tf_mitigation);
2004 #endif
2005 enum vmx_l1d_flush_state l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
2006 EXPORT_SYMBOL_GPL(l1tf_vmx_mitigation);
2007
2008 /*
2009 * These CPUs all support 44bits physical address space internally in the
2010 * cache but CPUID can report a smaller number of physical address bits.
2011 *
2012 * The L1TF mitigation uses the top most address bit for the inversion of
2013 * non present PTEs. When the installed memory reaches into the top most
2014 * address bit due to memory holes, which has been observed on machines
2015 * which report 36bits physical address bits and have 32G RAM installed,
2016 * then the mitigation range check in l1tf_select_mitigation() triggers.
2017 * This is a false positive because the mitigation is still possible due to
2018 * the fact that the cache uses 44bit internally. Use the cache bits
2019 * instead of the reported physical bits and adjust them on the affected
2020 * machines to 44bit if the reported bits are less than 44.
2021 */
override_cache_bits(struct cpuinfo_x86 * c)2022 static void override_cache_bits(struct cpuinfo_x86 *c)
2023 {
2024 if (c->x86 != 6)
2025 return;
2026
2027 switch (c->x86_model) {
2028 case INTEL_FAM6_NEHALEM:
2029 case INTEL_FAM6_WESTMERE:
2030 case INTEL_FAM6_SANDYBRIDGE:
2031 case INTEL_FAM6_IVYBRIDGE:
2032 case INTEL_FAM6_HASWELL:
2033 case INTEL_FAM6_HASWELL_L:
2034 case INTEL_FAM6_HASWELL_G:
2035 case INTEL_FAM6_BROADWELL:
2036 case INTEL_FAM6_BROADWELL_G:
2037 case INTEL_FAM6_SKYLAKE_L:
2038 case INTEL_FAM6_SKYLAKE:
2039 case INTEL_FAM6_KABYLAKE_L:
2040 case INTEL_FAM6_KABYLAKE:
2041 if (c->x86_cache_bits < 44)
2042 c->x86_cache_bits = 44;
2043 break;
2044 }
2045 }
2046
l1tf_select_mitigation(void)2047 static void __init l1tf_select_mitigation(void)
2048 {
2049 u64 half_pa;
2050
2051 if (!boot_cpu_has_bug(X86_BUG_L1TF))
2052 return;
2053
2054 if (cpu_mitigations_off())
2055 l1tf_mitigation = L1TF_MITIGATION_OFF;
2056 else if (cpu_mitigations_auto_nosmt())
2057 l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOSMT;
2058
2059 override_cache_bits(&boot_cpu_data);
2060
2061 switch (l1tf_mitigation) {
2062 case L1TF_MITIGATION_OFF:
2063 case L1TF_MITIGATION_FLUSH_NOWARN:
2064 case L1TF_MITIGATION_FLUSH:
2065 break;
2066 case L1TF_MITIGATION_FLUSH_NOSMT:
2067 case L1TF_MITIGATION_FULL:
2068 cpu_smt_disable(false);
2069 break;
2070 case L1TF_MITIGATION_FULL_FORCE:
2071 cpu_smt_disable(true);
2072 break;
2073 }
2074
2075 #if CONFIG_PGTABLE_LEVELS == 2
2076 pr_warn("Kernel not compiled for PAE. No mitigation for L1TF\n");
2077 return;
2078 #endif
2079
2080 half_pa = (u64)l1tf_pfn_limit() << PAGE_SHIFT;
2081 if (l1tf_mitigation != L1TF_MITIGATION_OFF &&
2082 e820__mapped_any(half_pa, ULLONG_MAX - half_pa, E820_TYPE_RAM)) {
2083 pr_warn("System has more than MAX_PA/2 memory. L1TF mitigation not effective.\n");
2084 pr_info("You may make it effective by booting the kernel with mem=%llu parameter.\n",
2085 half_pa);
2086 pr_info("However, doing so will make a part of your RAM unusable.\n");
2087 pr_info("Reading https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html might help you decide.\n");
2088 return;
2089 }
2090
2091 setup_force_cpu_cap(X86_FEATURE_L1TF_PTEINV);
2092 }
2093
l1tf_cmdline(char * str)2094 static int __init l1tf_cmdline(char *str)
2095 {
2096 if (!boot_cpu_has_bug(X86_BUG_L1TF))
2097 return 0;
2098
2099 if (!str)
2100 return -EINVAL;
2101
2102 if (!strcmp(str, "off"))
2103 l1tf_mitigation = L1TF_MITIGATION_OFF;
2104 else if (!strcmp(str, "flush,nowarn"))
2105 l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOWARN;
2106 else if (!strcmp(str, "flush"))
2107 l1tf_mitigation = L1TF_MITIGATION_FLUSH;
2108 else if (!strcmp(str, "flush,nosmt"))
2109 l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOSMT;
2110 else if (!strcmp(str, "full"))
2111 l1tf_mitigation = L1TF_MITIGATION_FULL;
2112 else if (!strcmp(str, "full,force"))
2113 l1tf_mitigation = L1TF_MITIGATION_FULL_FORCE;
2114
2115 return 0;
2116 }
2117 early_param("l1tf", l1tf_cmdline);
2118
2119 #undef pr_fmt
2120 #define pr_fmt(fmt) fmt
2121
2122 #ifdef CONFIG_SYSFS
2123
2124 #define L1TF_DEFAULT_MSG "Mitigation: PTE Inversion"
2125
2126 #if IS_ENABLED(CONFIG_KVM_INTEL)
2127 static const char * const l1tf_vmx_states[] = {
2128 [VMENTER_L1D_FLUSH_AUTO] = "auto",
2129 [VMENTER_L1D_FLUSH_NEVER] = "vulnerable",
2130 [VMENTER_L1D_FLUSH_COND] = "conditional cache flushes",
2131 [VMENTER_L1D_FLUSH_ALWAYS] = "cache flushes",
2132 [VMENTER_L1D_FLUSH_EPT_DISABLED] = "EPT disabled",
2133 [VMENTER_L1D_FLUSH_NOT_REQUIRED] = "flush not necessary"
2134 };
2135
l1tf_show_state(char * buf)2136 static ssize_t l1tf_show_state(char *buf)
2137 {
2138 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO)
2139 return sprintf(buf, "%s\n", L1TF_DEFAULT_MSG);
2140
2141 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_EPT_DISABLED ||
2142 (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER &&
2143 sched_smt_active())) {
2144 return sprintf(buf, "%s; VMX: %s\n", L1TF_DEFAULT_MSG,
2145 l1tf_vmx_states[l1tf_vmx_mitigation]);
2146 }
2147
2148 return sprintf(buf, "%s; VMX: %s, SMT %s\n", L1TF_DEFAULT_MSG,
2149 l1tf_vmx_states[l1tf_vmx_mitigation],
2150 sched_smt_active() ? "vulnerable" : "disabled");
2151 }
2152
itlb_multihit_show_state(char * buf)2153 static ssize_t itlb_multihit_show_state(char *buf)
2154 {
2155 if (!boot_cpu_has(X86_FEATURE_MSR_IA32_FEAT_CTL) ||
2156 !boot_cpu_has(X86_FEATURE_VMX))
2157 return sprintf(buf, "KVM: Mitigation: VMX unsupported\n");
2158 else if (!(cr4_read_shadow() & X86_CR4_VMXE))
2159 return sprintf(buf, "KVM: Mitigation: VMX disabled\n");
2160 else if (itlb_multihit_kvm_mitigation)
2161 return sprintf(buf, "KVM: Mitigation: Split huge pages\n");
2162 else
2163 return sprintf(buf, "KVM: Vulnerable\n");
2164 }
2165 #else
l1tf_show_state(char * buf)2166 static ssize_t l1tf_show_state(char *buf)
2167 {
2168 return sprintf(buf, "%s\n", L1TF_DEFAULT_MSG);
2169 }
2170
itlb_multihit_show_state(char * buf)2171 static ssize_t itlb_multihit_show_state(char *buf)
2172 {
2173 return sprintf(buf, "Processor vulnerable\n");
2174 }
2175 #endif
2176
mds_show_state(char * buf)2177 static ssize_t mds_show_state(char *buf)
2178 {
2179 if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
2180 return sprintf(buf, "%s; SMT Host state unknown\n",
2181 mds_strings[mds_mitigation]);
2182 }
2183
2184 if (boot_cpu_has(X86_BUG_MSBDS_ONLY)) {
2185 return sprintf(buf, "%s; SMT %s\n", mds_strings[mds_mitigation],
2186 (mds_mitigation == MDS_MITIGATION_OFF ? "vulnerable" :
2187 sched_smt_active() ? "mitigated" : "disabled"));
2188 }
2189
2190 return sprintf(buf, "%s; SMT %s\n", mds_strings[mds_mitigation],
2191 sched_smt_active() ? "vulnerable" : "disabled");
2192 }
2193
tsx_async_abort_show_state(char * buf)2194 static ssize_t tsx_async_abort_show_state(char *buf)
2195 {
2196 if ((taa_mitigation == TAA_MITIGATION_TSX_DISABLED) ||
2197 (taa_mitigation == TAA_MITIGATION_OFF))
2198 return sprintf(buf, "%s\n", taa_strings[taa_mitigation]);
2199
2200 if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
2201 return sprintf(buf, "%s; SMT Host state unknown\n",
2202 taa_strings[taa_mitigation]);
2203 }
2204
2205 return sprintf(buf, "%s; SMT %s\n", taa_strings[taa_mitigation],
2206 sched_smt_active() ? "vulnerable" : "disabled");
2207 }
2208
mmio_stale_data_show_state(char * buf)2209 static ssize_t mmio_stale_data_show_state(char *buf)
2210 {
2211 if (boot_cpu_has_bug(X86_BUG_MMIO_UNKNOWN))
2212 return sysfs_emit(buf, "Unknown: No mitigations\n");
2213
2214 if (mmio_mitigation == MMIO_MITIGATION_OFF)
2215 return sysfs_emit(buf, "%s\n", mmio_strings[mmio_mitigation]);
2216
2217 if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
2218 return sysfs_emit(buf, "%s; SMT Host state unknown\n",
2219 mmio_strings[mmio_mitigation]);
2220 }
2221
2222 return sysfs_emit(buf, "%s; SMT %s\n", mmio_strings[mmio_mitigation],
2223 sched_smt_active() ? "vulnerable" : "disabled");
2224 }
2225
stibp_state(void)2226 static char *stibp_state(void)
2227 {
2228 if (spectre_v2_in_ibrs_mode(spectre_v2_enabled))
2229 return "";
2230
2231 switch (spectre_v2_user_stibp) {
2232 case SPECTRE_V2_USER_NONE:
2233 return ", STIBP: disabled";
2234 case SPECTRE_V2_USER_STRICT:
2235 return ", STIBP: forced";
2236 case SPECTRE_V2_USER_STRICT_PREFERRED:
2237 return ", STIBP: always-on";
2238 case SPECTRE_V2_USER_PRCTL:
2239 case SPECTRE_V2_USER_SECCOMP:
2240 if (static_key_enabled(&switch_to_cond_stibp))
2241 return ", STIBP: conditional";
2242 }
2243 return "";
2244 }
2245
ibpb_state(void)2246 static char *ibpb_state(void)
2247 {
2248 if (boot_cpu_has(X86_FEATURE_IBPB)) {
2249 if (static_key_enabled(&switch_mm_always_ibpb))
2250 return ", IBPB: always-on";
2251 if (static_key_enabled(&switch_mm_cond_ibpb))
2252 return ", IBPB: conditional";
2253 return ", IBPB: disabled";
2254 }
2255 return "";
2256 }
2257
pbrsb_eibrs_state(void)2258 static char *pbrsb_eibrs_state(void)
2259 {
2260 if (boot_cpu_has_bug(X86_BUG_EIBRS_PBRSB)) {
2261 if (boot_cpu_has(X86_FEATURE_RSB_VMEXIT_LITE) ||
2262 boot_cpu_has(X86_FEATURE_RSB_VMEXIT))
2263 return ", PBRSB-eIBRS: SW sequence";
2264 else
2265 return ", PBRSB-eIBRS: Vulnerable";
2266 } else {
2267 return ", PBRSB-eIBRS: Not affected";
2268 }
2269 }
2270
spectre_v2_show_state(char * buf)2271 static ssize_t spectre_v2_show_state(char *buf)
2272 {
2273 if (spectre_v2_enabled == SPECTRE_V2_LFENCE)
2274 return sprintf(buf, "Vulnerable: LFENCE\n");
2275
2276 if (spectre_v2_enabled == SPECTRE_V2_EIBRS && unprivileged_ebpf_enabled())
2277 return sprintf(buf, "Vulnerable: eIBRS with unprivileged eBPF\n");
2278
2279 if (sched_smt_active() && unprivileged_ebpf_enabled() &&
2280 spectre_v2_enabled == SPECTRE_V2_EIBRS_LFENCE)
2281 return sprintf(buf, "Vulnerable: eIBRS+LFENCE with unprivileged eBPF and SMT\n");
2282
2283 return sprintf(buf, "%s%s%s%s%s%s%s\n",
2284 spectre_v2_strings[spectre_v2_enabled],
2285 ibpb_state(),
2286 boot_cpu_has(X86_FEATURE_USE_IBRS_FW) ? ", IBRS_FW" : "",
2287 stibp_state(),
2288 boot_cpu_has(X86_FEATURE_RSB_CTXSW) ? ", RSB filling" : "",
2289 pbrsb_eibrs_state(),
2290 spectre_v2_module_string());
2291 }
2292
srbds_show_state(char * buf)2293 static ssize_t srbds_show_state(char *buf)
2294 {
2295 return sprintf(buf, "%s\n", srbds_strings[srbds_mitigation]);
2296 }
2297
retbleed_show_state(char * buf)2298 static ssize_t retbleed_show_state(char *buf)
2299 {
2300 if (retbleed_mitigation == RETBLEED_MITIGATION_UNRET ||
2301 retbleed_mitigation == RETBLEED_MITIGATION_IBPB) {
2302 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD &&
2303 boot_cpu_data.x86_vendor != X86_VENDOR_HYGON)
2304 return sprintf(buf, "Vulnerable: untrained return thunk / IBPB on non-AMD based uarch\n");
2305
2306 return sprintf(buf, "%s; SMT %s\n",
2307 retbleed_strings[retbleed_mitigation],
2308 !sched_smt_active() ? "disabled" :
2309 spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT ||
2310 spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED ?
2311 "enabled with STIBP protection" : "vulnerable");
2312 }
2313
2314 return sprintf(buf, "%s\n", retbleed_strings[retbleed_mitigation]);
2315 }
2316
cpu_show_common(struct device * dev,struct device_attribute * attr,char * buf,unsigned int bug)2317 static ssize_t cpu_show_common(struct device *dev, struct device_attribute *attr,
2318 char *buf, unsigned int bug)
2319 {
2320 if (!boot_cpu_has_bug(bug))
2321 return sprintf(buf, "Not affected\n");
2322
2323 switch (bug) {
2324 case X86_BUG_CPU_MELTDOWN:
2325 if (boot_cpu_has(X86_FEATURE_PTI))
2326 return sprintf(buf, "Mitigation: PTI\n");
2327
2328 if (hypervisor_is_type(X86_HYPER_XEN_PV))
2329 return sprintf(buf, "Unknown (XEN PV detected, hypervisor mitigation required)\n");
2330
2331 break;
2332
2333 case X86_BUG_SPECTRE_V1:
2334 return sprintf(buf, "%s\n", spectre_v1_strings[spectre_v1_mitigation]);
2335
2336 case X86_BUG_SPECTRE_V2:
2337 return spectre_v2_show_state(buf);
2338
2339 case X86_BUG_SPEC_STORE_BYPASS:
2340 return sprintf(buf, "%s\n", ssb_strings[ssb_mode]);
2341
2342 case X86_BUG_L1TF:
2343 if (boot_cpu_has(X86_FEATURE_L1TF_PTEINV))
2344 return l1tf_show_state(buf);
2345 break;
2346
2347 case X86_BUG_MDS:
2348 return mds_show_state(buf);
2349
2350 case X86_BUG_TAA:
2351 return tsx_async_abort_show_state(buf);
2352
2353 case X86_BUG_ITLB_MULTIHIT:
2354 return itlb_multihit_show_state(buf);
2355
2356 case X86_BUG_SRBDS:
2357 return srbds_show_state(buf);
2358
2359 case X86_BUG_MMIO_STALE_DATA:
2360 case X86_BUG_MMIO_UNKNOWN:
2361 return mmio_stale_data_show_state(buf);
2362
2363 case X86_BUG_RETBLEED:
2364 return retbleed_show_state(buf);
2365
2366 default:
2367 break;
2368 }
2369
2370 return sprintf(buf, "Vulnerable\n");
2371 }
2372
cpu_show_meltdown(struct device * dev,struct device_attribute * attr,char * buf)2373 ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr, char *buf)
2374 {
2375 return cpu_show_common(dev, attr, buf, X86_BUG_CPU_MELTDOWN);
2376 }
2377
cpu_show_spectre_v1(struct device * dev,struct device_attribute * attr,char * buf)2378 ssize_t cpu_show_spectre_v1(struct device *dev, struct device_attribute *attr, char *buf)
2379 {
2380 return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V1);
2381 }
2382
cpu_show_spectre_v2(struct device * dev,struct device_attribute * attr,char * buf)2383 ssize_t cpu_show_spectre_v2(struct device *dev, struct device_attribute *attr, char *buf)
2384 {
2385 return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V2);
2386 }
2387
cpu_show_spec_store_bypass(struct device * dev,struct device_attribute * attr,char * buf)2388 ssize_t cpu_show_spec_store_bypass(struct device *dev, struct device_attribute *attr, char *buf)
2389 {
2390 return cpu_show_common(dev, attr, buf, X86_BUG_SPEC_STORE_BYPASS);
2391 }
2392
cpu_show_l1tf(struct device * dev,struct device_attribute * attr,char * buf)2393 ssize_t cpu_show_l1tf(struct device *dev, struct device_attribute *attr, char *buf)
2394 {
2395 return cpu_show_common(dev, attr, buf, X86_BUG_L1TF);
2396 }
2397
cpu_show_mds(struct device * dev,struct device_attribute * attr,char * buf)2398 ssize_t cpu_show_mds(struct device *dev, struct device_attribute *attr, char *buf)
2399 {
2400 return cpu_show_common(dev, attr, buf, X86_BUG_MDS);
2401 }
2402
cpu_show_tsx_async_abort(struct device * dev,struct device_attribute * attr,char * buf)2403 ssize_t cpu_show_tsx_async_abort(struct device *dev, struct device_attribute *attr, char *buf)
2404 {
2405 return cpu_show_common(dev, attr, buf, X86_BUG_TAA);
2406 }
2407
cpu_show_itlb_multihit(struct device * dev,struct device_attribute * attr,char * buf)2408 ssize_t cpu_show_itlb_multihit(struct device *dev, struct device_attribute *attr, char *buf)
2409 {
2410 return cpu_show_common(dev, attr, buf, X86_BUG_ITLB_MULTIHIT);
2411 }
2412
cpu_show_srbds(struct device * dev,struct device_attribute * attr,char * buf)2413 ssize_t cpu_show_srbds(struct device *dev, struct device_attribute *attr, char *buf)
2414 {
2415 return cpu_show_common(dev, attr, buf, X86_BUG_SRBDS);
2416 }
2417
cpu_show_mmio_stale_data(struct device * dev,struct device_attribute * attr,char * buf)2418 ssize_t cpu_show_mmio_stale_data(struct device *dev, struct device_attribute *attr, char *buf)
2419 {
2420 if (boot_cpu_has_bug(X86_BUG_MMIO_UNKNOWN))
2421 return cpu_show_common(dev, attr, buf, X86_BUG_MMIO_UNKNOWN);
2422 else
2423 return cpu_show_common(dev, attr, buf, X86_BUG_MMIO_STALE_DATA);
2424 }
2425
cpu_show_retbleed(struct device * dev,struct device_attribute * attr,char * buf)2426 ssize_t cpu_show_retbleed(struct device *dev, struct device_attribute *attr, char *buf)
2427 {
2428 return cpu_show_common(dev, attr, buf, X86_BUG_RETBLEED);
2429 }
2430 #endif
2431