]> git.ipfire.org Git - thirdparty/kernel/stable.git/blob - arch/x86/kernel/cpu/bugs.c
27f5004a8e24faaaf73ad8b5bf96a4c2a3e8567e
[thirdparty/kernel/stable.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/cpu.h>
13 #include <linux/module.h>
14 #include <linux/nospec.h>
15 #include <linux/prctl.h>
16 #include <linux/sched/smt.h>
17 #include <linux/pgtable.h>
18 #include <linux/bpf.h>
19
20 #include <asm/spec-ctrl.h>
21 #include <asm/cmdline.h>
22 #include <asm/bugs.h>
23 #include <asm/processor.h>
24 #include <asm/processor-flags.h>
25 #include <asm/fpu/api.h>
26 #include <asm/msr.h>
27 #include <asm/vmx.h>
28 #include <asm/paravirt.h>
29 #include <asm/intel-family.h>
30 #include <asm/e820/api.h>
31 #include <asm/hypervisor.h>
32 #include <asm/tlbflush.h>
33 #include <asm/cpu.h>
34
35 #include "cpu.h"
36
37 static void __init spectre_v1_select_mitigation(void);
38 static void __init spectre_v2_select_mitigation(void);
39 static void __init retbleed_select_mitigation(void);
40 static void __init spectre_v2_user_select_mitigation(void);
41 static void __init ssb_select_mitigation(void);
42 static void __init l1tf_select_mitigation(void);
43 static void __init mds_select_mitigation(void);
44 static void __init md_clear_update_mitigation(void);
45 static void __init md_clear_select_mitigation(void);
46 static void __init taa_select_mitigation(void);
47 static void __init mmio_select_mitigation(void);
48 static void __init srbds_select_mitigation(void);
49 static void __init l1d_flush_select_mitigation(void);
50 static void __init srso_select_mitigation(void);
51 static void __init gds_select_mitigation(void);
52
53 /* The base value of the SPEC_CTRL MSR without task-specific bits set */
54 u64 x86_spec_ctrl_base;
55 EXPORT_SYMBOL_GPL(x86_spec_ctrl_base);
56
57 /* The current value of the SPEC_CTRL MSR with task-specific bits set */
58 DEFINE_PER_CPU(u64, x86_spec_ctrl_current);
59 EXPORT_PER_CPU_SYMBOL_GPL(x86_spec_ctrl_current);
60
61 u64 x86_pred_cmd __ro_after_init = PRED_CMD_IBPB;
62 EXPORT_SYMBOL_GPL(x86_pred_cmd);
63
64 static DEFINE_MUTEX(spec_ctrl_mutex);
65
66 void (*x86_return_thunk)(void) __ro_after_init = __x86_return_thunk;
67
68 /* Update SPEC_CTRL MSR and its cached copy unconditionally */
69 static void update_spec_ctrl(u64 val)
70 {
71 this_cpu_write(x86_spec_ctrl_current, val);
72 wrmsrl(MSR_IA32_SPEC_CTRL, val);
73 }
74
75 /*
76 * Keep track of the SPEC_CTRL MSR value for the current task, which may differ
77 * from x86_spec_ctrl_base due to STIBP/SSB in __speculation_ctrl_update().
78 */
79 void update_spec_ctrl_cond(u64 val)
80 {
81 if (this_cpu_read(x86_spec_ctrl_current) == val)
82 return;
83
84 this_cpu_write(x86_spec_ctrl_current, val);
85
86 /*
87 * When KERNEL_IBRS this MSR is written on return-to-user, unless
88 * forced the update can be delayed until that time.
89 */
90 if (!cpu_feature_enabled(X86_FEATURE_KERNEL_IBRS))
91 wrmsrl(MSR_IA32_SPEC_CTRL, val);
92 }
93
94 noinstr u64 spec_ctrl_current(void)
95 {
96 return this_cpu_read(x86_spec_ctrl_current);
97 }
98 EXPORT_SYMBOL_GPL(spec_ctrl_current);
99
100 /*
101 * AMD specific MSR info for Speculative Store Bypass control.
102 * x86_amd_ls_cfg_ssbd_mask is initialized in identify_boot_cpu().
103 */
104 u64 __ro_after_init x86_amd_ls_cfg_base;
105 u64 __ro_after_init x86_amd_ls_cfg_ssbd_mask;
106
107 /* Control conditional STIBP in switch_to() */
108 DEFINE_STATIC_KEY_FALSE(switch_to_cond_stibp);
109 /* Control conditional IBPB in switch_mm() */
110 DEFINE_STATIC_KEY_FALSE(switch_mm_cond_ibpb);
111 /* Control unconditional IBPB in switch_mm() */
112 DEFINE_STATIC_KEY_FALSE(switch_mm_always_ibpb);
113
114 /* Control MDS CPU buffer clear before idling (halt, mwait) */
115 DEFINE_STATIC_KEY_FALSE(mds_idle_clear);
116 EXPORT_SYMBOL_GPL(mds_idle_clear);
117
118 /*
119 * Controls whether l1d flush based mitigations are enabled,
120 * based on hw features and admin setting via boot parameter
121 * defaults to false
122 */
123 DEFINE_STATIC_KEY_FALSE(switch_mm_cond_l1d_flush);
124
125 /* Controls CPU Fill buffer clear before KVM guest MMIO accesses */
126 DEFINE_STATIC_KEY_FALSE(mmio_stale_data_clear);
127 EXPORT_SYMBOL_GPL(mmio_stale_data_clear);
128
129 void __init cpu_select_mitigations(void)
130 {
131 /*
132 * Read the SPEC_CTRL MSR to account for reserved bits which may
133 * have unknown values. AMD64_LS_CFG MSR is cached in the early AMD
134 * init code as it is not enumerated and depends on the family.
135 */
136 if (cpu_feature_enabled(X86_FEATURE_MSR_SPEC_CTRL)) {
137 rdmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
138
139 /*
140 * Previously running kernel (kexec), may have some controls
141 * turned ON. Clear them and let the mitigations setup below
142 * rediscover them based on configuration.
143 */
144 x86_spec_ctrl_base &= ~SPEC_CTRL_MITIGATIONS_MASK;
145 }
146
147 /* Select the proper CPU mitigations before patching alternatives: */
148 spectre_v1_select_mitigation();
149 spectre_v2_select_mitigation();
150 /*
151 * retbleed_select_mitigation() relies on the state set by
152 * spectre_v2_select_mitigation(); specifically it wants to know about
153 * spectre_v2=ibrs.
154 */
155 retbleed_select_mitigation();
156 /*
157 * spectre_v2_user_select_mitigation() relies on the state set by
158 * retbleed_select_mitigation(); specifically the STIBP selection is
159 * forced for UNRET or IBPB.
160 */
161 spectre_v2_user_select_mitigation();
162 ssb_select_mitigation();
163 l1tf_select_mitigation();
164 md_clear_select_mitigation();
165 srbds_select_mitigation();
166 l1d_flush_select_mitigation();
167
168 /*
169 * srso_select_mitigation() depends and must run after
170 * retbleed_select_mitigation().
171 */
172 srso_select_mitigation();
173 gds_select_mitigation();
174 }
175
176 /*
177 * NOTE: This function is *only* called for SVM, since Intel uses
178 * MSR_IA32_SPEC_CTRL for SSBD.
179 */
180 void
181 x86_virt_spec_ctrl(u64 guest_virt_spec_ctrl, bool setguest)
182 {
183 u64 guestval, hostval;
184 struct thread_info *ti = current_thread_info();
185
186 /*
187 * If SSBD is not handled in MSR_SPEC_CTRL on AMD, update
188 * MSR_AMD64_L2_CFG or MSR_VIRT_SPEC_CTRL if supported.
189 */
190 if (!static_cpu_has(X86_FEATURE_LS_CFG_SSBD) &&
191 !static_cpu_has(X86_FEATURE_VIRT_SSBD))
192 return;
193
194 /*
195 * If the host has SSBD mitigation enabled, force it in the host's
196 * virtual MSR value. If its not permanently enabled, evaluate
197 * current's TIF_SSBD thread flag.
198 */
199 if (static_cpu_has(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE))
200 hostval = SPEC_CTRL_SSBD;
201 else
202 hostval = ssbd_tif_to_spec_ctrl(ti->flags);
203
204 /* Sanitize the guest value */
205 guestval = guest_virt_spec_ctrl & SPEC_CTRL_SSBD;
206
207 if (hostval != guestval) {
208 unsigned long tif;
209
210 tif = setguest ? ssbd_spec_ctrl_to_tif(guestval) :
211 ssbd_spec_ctrl_to_tif(hostval);
212
213 speculation_ctrl_update(tif);
214 }
215 }
216 EXPORT_SYMBOL_GPL(x86_virt_spec_ctrl);
217
218 static void x86_amd_ssb_disable(void)
219 {
220 u64 msrval = x86_amd_ls_cfg_base | x86_amd_ls_cfg_ssbd_mask;
221
222 if (boot_cpu_has(X86_FEATURE_VIRT_SSBD))
223 wrmsrl(MSR_AMD64_VIRT_SPEC_CTRL, SPEC_CTRL_SSBD);
224 else if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD))
225 wrmsrl(MSR_AMD64_LS_CFG, msrval);
226 }
227
228 #undef pr_fmt
229 #define pr_fmt(fmt) "MDS: " fmt
230
231 /* Default mitigation for MDS-affected CPUs */
232 static enum mds_mitigations mds_mitigation __ro_after_init = MDS_MITIGATION_FULL;
233 static bool mds_nosmt __ro_after_init = false;
234
235 static const char * const mds_strings[] = {
236 [MDS_MITIGATION_OFF] = "Vulnerable",
237 [MDS_MITIGATION_FULL] = "Mitigation: Clear CPU buffers",
238 [MDS_MITIGATION_VMWERV] = "Vulnerable: Clear CPU buffers attempted, no microcode",
239 };
240
241 static void __init mds_select_mitigation(void)
242 {
243 if (!boot_cpu_has_bug(X86_BUG_MDS) || cpu_mitigations_off()) {
244 mds_mitigation = MDS_MITIGATION_OFF;
245 return;
246 }
247
248 if (mds_mitigation == MDS_MITIGATION_FULL) {
249 if (!boot_cpu_has(X86_FEATURE_MD_CLEAR))
250 mds_mitigation = MDS_MITIGATION_VMWERV;
251
252 setup_force_cpu_cap(X86_FEATURE_CLEAR_CPU_BUF);
253
254 if (!boot_cpu_has(X86_BUG_MSBDS_ONLY) &&
255 (mds_nosmt || cpu_mitigations_auto_nosmt()))
256 cpu_smt_disable(false);
257 }
258 }
259
260 static int __init mds_cmdline(char *str)
261 {
262 if (!boot_cpu_has_bug(X86_BUG_MDS))
263 return 0;
264
265 if (!str)
266 return -EINVAL;
267
268 if (!strcmp(str, "off"))
269 mds_mitigation = MDS_MITIGATION_OFF;
270 else if (!strcmp(str, "full"))
271 mds_mitigation = MDS_MITIGATION_FULL;
272 else if (!strcmp(str, "full,nosmt")) {
273 mds_mitigation = MDS_MITIGATION_FULL;
274 mds_nosmt = true;
275 }
276
277 return 0;
278 }
279 early_param("mds", mds_cmdline);
280
281 #undef pr_fmt
282 #define pr_fmt(fmt) "TAA: " fmt
283
284 enum taa_mitigations {
285 TAA_MITIGATION_OFF,
286 TAA_MITIGATION_UCODE_NEEDED,
287 TAA_MITIGATION_VERW,
288 TAA_MITIGATION_TSX_DISABLED,
289 };
290
291 /* Default mitigation for TAA-affected CPUs */
292 static enum taa_mitigations taa_mitigation __ro_after_init = TAA_MITIGATION_VERW;
293 static bool taa_nosmt __ro_after_init;
294
295 static const char * const taa_strings[] = {
296 [TAA_MITIGATION_OFF] = "Vulnerable",
297 [TAA_MITIGATION_UCODE_NEEDED] = "Vulnerable: Clear CPU buffers attempted, no microcode",
298 [TAA_MITIGATION_VERW] = "Mitigation: Clear CPU buffers",
299 [TAA_MITIGATION_TSX_DISABLED] = "Mitigation: TSX disabled",
300 };
301
302 static void __init taa_select_mitigation(void)
303 {
304 u64 ia32_cap;
305
306 if (!boot_cpu_has_bug(X86_BUG_TAA)) {
307 taa_mitigation = TAA_MITIGATION_OFF;
308 return;
309 }
310
311 /* TSX previously disabled by tsx=off */
312 if (!boot_cpu_has(X86_FEATURE_RTM)) {
313 taa_mitigation = TAA_MITIGATION_TSX_DISABLED;
314 return;
315 }
316
317 if (cpu_mitigations_off()) {
318 taa_mitigation = TAA_MITIGATION_OFF;
319 return;
320 }
321
322 /*
323 * TAA mitigation via VERW is turned off if both
324 * tsx_async_abort=off and mds=off are specified.
325 */
326 if (taa_mitigation == TAA_MITIGATION_OFF &&
327 mds_mitigation == MDS_MITIGATION_OFF)
328 return;
329
330 if (boot_cpu_has(X86_FEATURE_MD_CLEAR))
331 taa_mitigation = TAA_MITIGATION_VERW;
332 else
333 taa_mitigation = TAA_MITIGATION_UCODE_NEEDED;
334
335 /*
336 * VERW doesn't clear the CPU buffers when MD_CLEAR=1 and MDS_NO=1.
337 * A microcode update fixes this behavior to clear CPU buffers. It also
338 * adds support for MSR_IA32_TSX_CTRL which is enumerated by the
339 * ARCH_CAP_TSX_CTRL_MSR bit.
340 *
341 * On MDS_NO=1 CPUs if ARCH_CAP_TSX_CTRL_MSR is not set, microcode
342 * update is required.
343 */
344 ia32_cap = x86_read_arch_cap_msr();
345 if ( (ia32_cap & ARCH_CAP_MDS_NO) &&
346 !(ia32_cap & ARCH_CAP_TSX_CTRL_MSR))
347 taa_mitigation = TAA_MITIGATION_UCODE_NEEDED;
348
349 /*
350 * TSX is enabled, select alternate mitigation for TAA which is
351 * the same as MDS. Enable MDS static branch to clear CPU buffers.
352 *
353 * For guests that can't determine whether the correct microcode is
354 * present on host, enable the mitigation for UCODE_NEEDED as well.
355 */
356 setup_force_cpu_cap(X86_FEATURE_CLEAR_CPU_BUF);
357
358 if (taa_nosmt || cpu_mitigations_auto_nosmt())
359 cpu_smt_disable(false);
360 }
361
362 static int __init tsx_async_abort_parse_cmdline(char *str)
363 {
364 if (!boot_cpu_has_bug(X86_BUG_TAA))
365 return 0;
366
367 if (!str)
368 return -EINVAL;
369
370 if (!strcmp(str, "off")) {
371 taa_mitigation = TAA_MITIGATION_OFF;
372 } else if (!strcmp(str, "full")) {
373 taa_mitigation = TAA_MITIGATION_VERW;
374 } else if (!strcmp(str, "full,nosmt")) {
375 taa_mitigation = TAA_MITIGATION_VERW;
376 taa_nosmt = true;
377 }
378
379 return 0;
380 }
381 early_param("tsx_async_abort", tsx_async_abort_parse_cmdline);
382
383 #undef pr_fmt
384 #define pr_fmt(fmt) "MMIO Stale Data: " fmt
385
386 enum mmio_mitigations {
387 MMIO_MITIGATION_OFF,
388 MMIO_MITIGATION_UCODE_NEEDED,
389 MMIO_MITIGATION_VERW,
390 };
391
392 /* Default mitigation for Processor MMIO Stale Data vulnerabilities */
393 static enum mmio_mitigations mmio_mitigation __ro_after_init = MMIO_MITIGATION_VERW;
394 static bool mmio_nosmt __ro_after_init = false;
395
396 static const char * const mmio_strings[] = {
397 [MMIO_MITIGATION_OFF] = "Vulnerable",
398 [MMIO_MITIGATION_UCODE_NEEDED] = "Vulnerable: Clear CPU buffers attempted, no microcode",
399 [MMIO_MITIGATION_VERW] = "Mitigation: Clear CPU buffers",
400 };
401
402 static void __init mmio_select_mitigation(void)
403 {
404 u64 ia32_cap;
405
406 if (!boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA) ||
407 boot_cpu_has_bug(X86_BUG_MMIO_UNKNOWN) ||
408 cpu_mitigations_off()) {
409 mmio_mitigation = MMIO_MITIGATION_OFF;
410 return;
411 }
412
413 if (mmio_mitigation == MMIO_MITIGATION_OFF)
414 return;
415
416 ia32_cap = x86_read_arch_cap_msr();
417
418 /*
419 * Enable CPU buffer clear mitigation for host and VMM, if also affected
420 * by MDS or TAA. Otherwise, enable mitigation for VMM only.
421 */
422 if (boot_cpu_has_bug(X86_BUG_MDS) || (boot_cpu_has_bug(X86_BUG_TAA) &&
423 boot_cpu_has(X86_FEATURE_RTM)))
424 setup_force_cpu_cap(X86_FEATURE_CLEAR_CPU_BUF);
425
426 /*
427 * X86_FEATURE_CLEAR_CPU_BUF could be enabled by other VERW based
428 * mitigations, disable KVM-only mitigation in that case.
429 */
430 if (boot_cpu_has(X86_FEATURE_CLEAR_CPU_BUF))
431 static_branch_disable(&mmio_stale_data_clear);
432 else
433 static_branch_enable(&mmio_stale_data_clear);
434
435 /*
436 * If Processor-MMIO-Stale-Data bug is present and Fill Buffer data can
437 * be propagated to uncore buffers, clearing the Fill buffers on idle
438 * is required irrespective of SMT state.
439 */
440 if (!(ia32_cap & ARCH_CAP_FBSDP_NO))
441 static_branch_enable(&mds_idle_clear);
442
443 /*
444 * Check if the system has the right microcode.
445 *
446 * CPU Fill buffer clear mitigation is enumerated by either an explicit
447 * FB_CLEAR or by the presence of both MD_CLEAR and L1D_FLUSH on MDS
448 * affected systems.
449 */
450 if ((ia32_cap & ARCH_CAP_FB_CLEAR) ||
451 (boot_cpu_has(X86_FEATURE_MD_CLEAR) &&
452 boot_cpu_has(X86_FEATURE_FLUSH_L1D) &&
453 !(ia32_cap & ARCH_CAP_MDS_NO)))
454 mmio_mitigation = MMIO_MITIGATION_VERW;
455 else
456 mmio_mitigation = MMIO_MITIGATION_UCODE_NEEDED;
457
458 if (mmio_nosmt || cpu_mitigations_auto_nosmt())
459 cpu_smt_disable(false);
460 }
461
462 static int __init mmio_stale_data_parse_cmdline(char *str)
463 {
464 if (!boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA))
465 return 0;
466
467 if (!str)
468 return -EINVAL;
469
470 if (!strcmp(str, "off")) {
471 mmio_mitigation = MMIO_MITIGATION_OFF;
472 } else if (!strcmp(str, "full")) {
473 mmio_mitigation = MMIO_MITIGATION_VERW;
474 } else if (!strcmp(str, "full,nosmt")) {
475 mmio_mitigation = MMIO_MITIGATION_VERW;
476 mmio_nosmt = true;
477 }
478
479 return 0;
480 }
481 early_param("mmio_stale_data", mmio_stale_data_parse_cmdline);
482
483 #undef pr_fmt
484 #define pr_fmt(fmt) "Register File Data Sampling: " fmt
485
486 enum rfds_mitigations {
487 RFDS_MITIGATION_OFF,
488 RFDS_MITIGATION_VERW,
489 RFDS_MITIGATION_UCODE_NEEDED,
490 };
491
492 /* Default mitigation for Register File Data Sampling */
493 static enum rfds_mitigations rfds_mitigation __ro_after_init =
494 IS_ENABLED(CONFIG_MITIGATION_RFDS) ? RFDS_MITIGATION_VERW : RFDS_MITIGATION_OFF;
495
496 static const char * const rfds_strings[] = {
497 [RFDS_MITIGATION_OFF] = "Vulnerable",
498 [RFDS_MITIGATION_VERW] = "Mitigation: Clear Register File",
499 [RFDS_MITIGATION_UCODE_NEEDED] = "Vulnerable: No microcode",
500 };
501
502 static void __init rfds_select_mitigation(void)
503 {
504 if (!boot_cpu_has_bug(X86_BUG_RFDS) || cpu_mitigations_off()) {
505 rfds_mitigation = RFDS_MITIGATION_OFF;
506 return;
507 }
508 if (rfds_mitigation == RFDS_MITIGATION_OFF)
509 return;
510
511 if (x86_read_arch_cap_msr() & ARCH_CAP_RFDS_CLEAR)
512 setup_force_cpu_cap(X86_FEATURE_CLEAR_CPU_BUF);
513 else
514 rfds_mitigation = RFDS_MITIGATION_UCODE_NEEDED;
515 }
516
517 static __init int rfds_parse_cmdline(char *str)
518 {
519 if (!str)
520 return -EINVAL;
521
522 if (!boot_cpu_has_bug(X86_BUG_RFDS))
523 return 0;
524
525 if (!strcmp(str, "off"))
526 rfds_mitigation = RFDS_MITIGATION_OFF;
527 else if (!strcmp(str, "on"))
528 rfds_mitigation = RFDS_MITIGATION_VERW;
529
530 return 0;
531 }
532 early_param("reg_file_data_sampling", rfds_parse_cmdline);
533
534 #undef pr_fmt
535 #define pr_fmt(fmt) "" fmt
536
537 static void __init md_clear_update_mitigation(void)
538 {
539 if (cpu_mitigations_off())
540 return;
541
542 if (!boot_cpu_has(X86_FEATURE_CLEAR_CPU_BUF))
543 goto out;
544
545 /*
546 * X86_FEATURE_CLEAR_CPU_BUF is now enabled. Update MDS, TAA and MMIO
547 * Stale Data mitigation, if necessary.
548 */
549 if (mds_mitigation == MDS_MITIGATION_OFF &&
550 boot_cpu_has_bug(X86_BUG_MDS)) {
551 mds_mitigation = MDS_MITIGATION_FULL;
552 mds_select_mitigation();
553 }
554 if (taa_mitigation == TAA_MITIGATION_OFF &&
555 boot_cpu_has_bug(X86_BUG_TAA)) {
556 taa_mitigation = TAA_MITIGATION_VERW;
557 taa_select_mitigation();
558 }
559 /*
560 * MMIO_MITIGATION_OFF is not checked here so that mmio_stale_data_clear
561 * gets updated correctly as per X86_FEATURE_CLEAR_CPU_BUF state.
562 */
563 if (boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA)) {
564 mmio_mitigation = MMIO_MITIGATION_VERW;
565 mmio_select_mitigation();
566 }
567 if (rfds_mitigation == RFDS_MITIGATION_OFF &&
568 boot_cpu_has_bug(X86_BUG_RFDS)) {
569 rfds_mitigation = RFDS_MITIGATION_VERW;
570 rfds_select_mitigation();
571 }
572 out:
573 if (boot_cpu_has_bug(X86_BUG_MDS))
574 pr_info("MDS: %s\n", mds_strings[mds_mitigation]);
575 if (boot_cpu_has_bug(X86_BUG_TAA))
576 pr_info("TAA: %s\n", taa_strings[taa_mitigation]);
577 if (boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA))
578 pr_info("MMIO Stale Data: %s\n", mmio_strings[mmio_mitigation]);
579 else if (boot_cpu_has_bug(X86_BUG_MMIO_UNKNOWN))
580 pr_info("MMIO Stale Data: Unknown: No mitigations\n");
581 if (boot_cpu_has_bug(X86_BUG_RFDS))
582 pr_info("Register File Data Sampling: %s\n", rfds_strings[rfds_mitigation]);
583 }
584
585 static void __init md_clear_select_mitigation(void)
586 {
587 mds_select_mitigation();
588 taa_select_mitigation();
589 mmio_select_mitigation();
590 rfds_select_mitigation();
591
592 /*
593 * As these mitigations are inter-related and rely on VERW instruction
594 * to clear the microarchitural buffers, update and print their status
595 * after mitigation selection is done for each of these vulnerabilities.
596 */
597 md_clear_update_mitigation();
598 }
599
600 #undef pr_fmt
601 #define pr_fmt(fmt) "SRBDS: " fmt
602
603 enum srbds_mitigations {
604 SRBDS_MITIGATION_OFF,
605 SRBDS_MITIGATION_UCODE_NEEDED,
606 SRBDS_MITIGATION_FULL,
607 SRBDS_MITIGATION_TSX_OFF,
608 SRBDS_MITIGATION_HYPERVISOR,
609 };
610
611 static enum srbds_mitigations srbds_mitigation __ro_after_init = SRBDS_MITIGATION_FULL;
612
613 static const char * const srbds_strings[] = {
614 [SRBDS_MITIGATION_OFF] = "Vulnerable",
615 [SRBDS_MITIGATION_UCODE_NEEDED] = "Vulnerable: No microcode",
616 [SRBDS_MITIGATION_FULL] = "Mitigation: Microcode",
617 [SRBDS_MITIGATION_TSX_OFF] = "Mitigation: TSX disabled",
618 [SRBDS_MITIGATION_HYPERVISOR] = "Unknown: Dependent on hypervisor status",
619 };
620
621 static bool srbds_off;
622
623 void update_srbds_msr(void)
624 {
625 u64 mcu_ctrl;
626
627 if (!boot_cpu_has_bug(X86_BUG_SRBDS))
628 return;
629
630 if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
631 return;
632
633 if (srbds_mitigation == SRBDS_MITIGATION_UCODE_NEEDED)
634 return;
635
636 /*
637 * A MDS_NO CPU for which SRBDS mitigation is not needed due to TSX
638 * being disabled and it hasn't received the SRBDS MSR microcode.
639 */
640 if (!boot_cpu_has(X86_FEATURE_SRBDS_CTRL))
641 return;
642
643 rdmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl);
644
645 switch (srbds_mitigation) {
646 case SRBDS_MITIGATION_OFF:
647 case SRBDS_MITIGATION_TSX_OFF:
648 mcu_ctrl |= RNGDS_MITG_DIS;
649 break;
650 case SRBDS_MITIGATION_FULL:
651 mcu_ctrl &= ~RNGDS_MITG_DIS;
652 break;
653 default:
654 break;
655 }
656
657 wrmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl);
658 }
659
660 static void __init srbds_select_mitigation(void)
661 {
662 u64 ia32_cap;
663
664 if (!boot_cpu_has_bug(X86_BUG_SRBDS))
665 return;
666
667 /*
668 * Check to see if this is one of the MDS_NO systems supporting TSX that
669 * are only exposed to SRBDS when TSX is enabled or when CPU is affected
670 * by Processor MMIO Stale Data vulnerability.
671 */
672 ia32_cap = x86_read_arch_cap_msr();
673 if ((ia32_cap & ARCH_CAP_MDS_NO) && !boot_cpu_has(X86_FEATURE_RTM) &&
674 !boot_cpu_has_bug(X86_BUG_MMIO_STALE_DATA))
675 srbds_mitigation = SRBDS_MITIGATION_TSX_OFF;
676 else if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
677 srbds_mitigation = SRBDS_MITIGATION_HYPERVISOR;
678 else if (!boot_cpu_has(X86_FEATURE_SRBDS_CTRL))
679 srbds_mitigation = SRBDS_MITIGATION_UCODE_NEEDED;
680 else if (cpu_mitigations_off() || srbds_off)
681 srbds_mitigation = SRBDS_MITIGATION_OFF;
682
683 update_srbds_msr();
684 pr_info("%s\n", srbds_strings[srbds_mitigation]);
685 }
686
687 static int __init srbds_parse_cmdline(char *str)
688 {
689 if (!str)
690 return -EINVAL;
691
692 if (!boot_cpu_has_bug(X86_BUG_SRBDS))
693 return 0;
694
695 srbds_off = !strcmp(str, "off");
696 return 0;
697 }
698 early_param("srbds", srbds_parse_cmdline);
699
700 #undef pr_fmt
701 #define pr_fmt(fmt) "L1D Flush : " fmt
702
703 enum l1d_flush_mitigations {
704 L1D_FLUSH_OFF = 0,
705 L1D_FLUSH_ON,
706 };
707
708 static enum l1d_flush_mitigations l1d_flush_mitigation __initdata = L1D_FLUSH_OFF;
709
710 static void __init l1d_flush_select_mitigation(void)
711 {
712 if (!l1d_flush_mitigation || !boot_cpu_has(X86_FEATURE_FLUSH_L1D))
713 return;
714
715 static_branch_enable(&switch_mm_cond_l1d_flush);
716 pr_info("Conditional flush on switch_mm() enabled\n");
717 }
718
719 static int __init l1d_flush_parse_cmdline(char *str)
720 {
721 if (!strcmp(str, "on"))
722 l1d_flush_mitigation = L1D_FLUSH_ON;
723
724 return 0;
725 }
726 early_param("l1d_flush", l1d_flush_parse_cmdline);
727
728 #undef pr_fmt
729 #define pr_fmt(fmt) "GDS: " fmt
730
731 enum gds_mitigations {
732 GDS_MITIGATION_OFF,
733 GDS_MITIGATION_UCODE_NEEDED,
734 GDS_MITIGATION_FORCE,
735 GDS_MITIGATION_FULL,
736 GDS_MITIGATION_FULL_LOCKED,
737 GDS_MITIGATION_HYPERVISOR,
738 };
739
740 #if IS_ENABLED(CONFIG_MITIGATION_GDS_FORCE)
741 static enum gds_mitigations gds_mitigation __ro_after_init = GDS_MITIGATION_FORCE;
742 #else
743 static enum gds_mitigations gds_mitigation __ro_after_init = GDS_MITIGATION_FULL;
744 #endif
745
746 static const char * const gds_strings[] = {
747 [GDS_MITIGATION_OFF] = "Vulnerable",
748 [GDS_MITIGATION_UCODE_NEEDED] = "Vulnerable: No microcode",
749 [GDS_MITIGATION_FORCE] = "Mitigation: AVX disabled, no microcode",
750 [GDS_MITIGATION_FULL] = "Mitigation: Microcode",
751 [GDS_MITIGATION_FULL_LOCKED] = "Mitigation: Microcode (locked)",
752 [GDS_MITIGATION_HYPERVISOR] = "Unknown: Dependent on hypervisor status",
753 };
754
755 bool gds_ucode_mitigated(void)
756 {
757 return (gds_mitigation == GDS_MITIGATION_FULL ||
758 gds_mitigation == GDS_MITIGATION_FULL_LOCKED);
759 }
760 EXPORT_SYMBOL_GPL(gds_ucode_mitigated);
761
762 void update_gds_msr(void)
763 {
764 u64 mcu_ctrl_after;
765 u64 mcu_ctrl;
766
767 switch (gds_mitigation) {
768 case GDS_MITIGATION_OFF:
769 rdmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl);
770 mcu_ctrl |= GDS_MITG_DIS;
771 break;
772 case GDS_MITIGATION_FULL_LOCKED:
773 /*
774 * The LOCKED state comes from the boot CPU. APs might not have
775 * the same state. Make sure the mitigation is enabled on all
776 * CPUs.
777 */
778 case GDS_MITIGATION_FULL:
779 rdmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl);
780 mcu_ctrl &= ~GDS_MITG_DIS;
781 break;
782 case GDS_MITIGATION_FORCE:
783 case GDS_MITIGATION_UCODE_NEEDED:
784 case GDS_MITIGATION_HYPERVISOR:
785 return;
786 }
787
788 wrmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl);
789
790 /*
791 * Check to make sure that the WRMSR value was not ignored. Writes to
792 * GDS_MITG_DIS will be ignored if this processor is locked but the boot
793 * processor was not.
794 */
795 rdmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl_after);
796 WARN_ON_ONCE(mcu_ctrl != mcu_ctrl_after);
797 }
798
799 static void __init gds_select_mitigation(void)
800 {
801 u64 mcu_ctrl;
802
803 if (!boot_cpu_has_bug(X86_BUG_GDS))
804 return;
805
806 if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
807 gds_mitigation = GDS_MITIGATION_HYPERVISOR;
808 goto out;
809 }
810
811 if (cpu_mitigations_off())
812 gds_mitigation = GDS_MITIGATION_OFF;
813 /* Will verify below that mitigation _can_ be disabled */
814
815 /* No microcode */
816 if (!(x86_read_arch_cap_msr() & ARCH_CAP_GDS_CTRL)) {
817 if (gds_mitigation == GDS_MITIGATION_FORCE) {
818 /*
819 * This only needs to be done on the boot CPU so do it
820 * here rather than in update_gds_msr()
821 */
822 setup_clear_cpu_cap(X86_FEATURE_AVX);
823 pr_warn("Microcode update needed! Disabling AVX as mitigation.\n");
824 } else {
825 gds_mitigation = GDS_MITIGATION_UCODE_NEEDED;
826 }
827 goto out;
828 }
829
830 /* Microcode has mitigation, use it */
831 if (gds_mitigation == GDS_MITIGATION_FORCE)
832 gds_mitigation = GDS_MITIGATION_FULL;
833
834 rdmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl);
835 if (mcu_ctrl & GDS_MITG_LOCKED) {
836 if (gds_mitigation == GDS_MITIGATION_OFF)
837 pr_warn("Mitigation locked. Disable failed.\n");
838
839 /*
840 * The mitigation is selected from the boot CPU. All other CPUs
841 * _should_ have the same state. If the boot CPU isn't locked
842 * but others are then update_gds_msr() will WARN() of the state
843 * mismatch. If the boot CPU is locked update_gds_msr() will
844 * ensure the other CPUs have the mitigation enabled.
845 */
846 gds_mitigation = GDS_MITIGATION_FULL_LOCKED;
847 }
848
849 update_gds_msr();
850 out:
851 pr_info("%s\n", gds_strings[gds_mitigation]);
852 }
853
854 static int __init gds_parse_cmdline(char *str)
855 {
856 if (!str)
857 return -EINVAL;
858
859 if (!boot_cpu_has_bug(X86_BUG_GDS))
860 return 0;
861
862 if (!strcmp(str, "off"))
863 gds_mitigation = GDS_MITIGATION_OFF;
864 else if (!strcmp(str, "force"))
865 gds_mitigation = GDS_MITIGATION_FORCE;
866
867 return 0;
868 }
869 early_param("gather_data_sampling", gds_parse_cmdline);
870
871 #undef pr_fmt
872 #define pr_fmt(fmt) "Spectre V1 : " fmt
873
874 enum spectre_v1_mitigation {
875 SPECTRE_V1_MITIGATION_NONE,
876 SPECTRE_V1_MITIGATION_AUTO,
877 };
878
879 static enum spectre_v1_mitigation spectre_v1_mitigation __ro_after_init =
880 SPECTRE_V1_MITIGATION_AUTO;
881
882 static const char * const spectre_v1_strings[] = {
883 [SPECTRE_V1_MITIGATION_NONE] = "Vulnerable: __user pointer sanitization and usercopy barriers only; no swapgs barriers",
884 [SPECTRE_V1_MITIGATION_AUTO] = "Mitigation: usercopy/swapgs barriers and __user pointer sanitization",
885 };
886
887 /*
888 * Does SMAP provide full mitigation against speculative kernel access to
889 * userspace?
890 */
891 static bool smap_works_speculatively(void)
892 {
893 if (!boot_cpu_has(X86_FEATURE_SMAP))
894 return false;
895
896 /*
897 * On CPUs which are vulnerable to Meltdown, SMAP does not
898 * prevent speculative access to user data in the L1 cache.
899 * Consider SMAP to be non-functional as a mitigation on these
900 * CPUs.
901 */
902 if (boot_cpu_has(X86_BUG_CPU_MELTDOWN))
903 return false;
904
905 return true;
906 }
907
908 static void __init spectre_v1_select_mitigation(void)
909 {
910 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V1) || cpu_mitigations_off()) {
911 spectre_v1_mitigation = SPECTRE_V1_MITIGATION_NONE;
912 return;
913 }
914
915 if (spectre_v1_mitigation == SPECTRE_V1_MITIGATION_AUTO) {
916 /*
917 * With Spectre v1, a user can speculatively control either
918 * path of a conditional swapgs with a user-controlled GS
919 * value. The mitigation is to add lfences to both code paths.
920 *
921 * If FSGSBASE is enabled, the user can put a kernel address in
922 * GS, in which case SMAP provides no protection.
923 *
924 * If FSGSBASE is disabled, the user can only put a user space
925 * address in GS. That makes an attack harder, but still
926 * possible if there's no SMAP protection.
927 */
928 if (boot_cpu_has(X86_FEATURE_FSGSBASE) ||
929 !smap_works_speculatively()) {
930 /*
931 * Mitigation can be provided from SWAPGS itself or
932 * PTI as the CR3 write in the Meltdown mitigation
933 * is serializing.
934 *
935 * If neither is there, mitigate with an LFENCE to
936 * stop speculation through swapgs.
937 */
938 if (boot_cpu_has_bug(X86_BUG_SWAPGS) &&
939 !boot_cpu_has(X86_FEATURE_PTI))
940 setup_force_cpu_cap(X86_FEATURE_FENCE_SWAPGS_USER);
941
942 /*
943 * Enable lfences in the kernel entry (non-swapgs)
944 * paths, to prevent user entry from speculatively
945 * skipping swapgs.
946 */
947 setup_force_cpu_cap(X86_FEATURE_FENCE_SWAPGS_KERNEL);
948 }
949 }
950
951 pr_info("%s\n", spectre_v1_strings[spectre_v1_mitigation]);
952 }
953
954 static int __init nospectre_v1_cmdline(char *str)
955 {
956 spectre_v1_mitigation = SPECTRE_V1_MITIGATION_NONE;
957 return 0;
958 }
959 early_param("nospectre_v1", nospectre_v1_cmdline);
960
961 enum spectre_v2_mitigation spectre_v2_enabled __ro_after_init = SPECTRE_V2_NONE;
962
963 #undef pr_fmt
964 #define pr_fmt(fmt) "RETBleed: " fmt
965
966 enum retbleed_mitigation {
967 RETBLEED_MITIGATION_NONE,
968 RETBLEED_MITIGATION_UNRET,
969 RETBLEED_MITIGATION_IBPB,
970 RETBLEED_MITIGATION_IBRS,
971 RETBLEED_MITIGATION_EIBRS,
972 RETBLEED_MITIGATION_STUFF,
973 };
974
975 enum retbleed_mitigation_cmd {
976 RETBLEED_CMD_OFF,
977 RETBLEED_CMD_AUTO,
978 RETBLEED_CMD_UNRET,
979 RETBLEED_CMD_IBPB,
980 RETBLEED_CMD_STUFF,
981 };
982
983 static const char * const retbleed_strings[] = {
984 [RETBLEED_MITIGATION_NONE] = "Vulnerable",
985 [RETBLEED_MITIGATION_UNRET] = "Mitigation: untrained return thunk",
986 [RETBLEED_MITIGATION_IBPB] = "Mitigation: IBPB",
987 [RETBLEED_MITIGATION_IBRS] = "Mitigation: IBRS",
988 [RETBLEED_MITIGATION_EIBRS] = "Mitigation: Enhanced IBRS",
989 [RETBLEED_MITIGATION_STUFF] = "Mitigation: Stuffing",
990 };
991
992 static enum retbleed_mitigation retbleed_mitigation __ro_after_init =
993 RETBLEED_MITIGATION_NONE;
994 static enum retbleed_mitigation_cmd retbleed_cmd __ro_after_init =
995 RETBLEED_CMD_AUTO;
996
997 static int __ro_after_init retbleed_nosmt = false;
998
999 static int __init retbleed_parse_cmdline(char *str)
1000 {
1001 if (!str)
1002 return -EINVAL;
1003
1004 while (str) {
1005 char *next = strchr(str, ',');
1006 if (next) {
1007 *next = 0;
1008 next++;
1009 }
1010
1011 if (!strcmp(str, "off")) {
1012 retbleed_cmd = RETBLEED_CMD_OFF;
1013 } else if (!strcmp(str, "auto")) {
1014 retbleed_cmd = RETBLEED_CMD_AUTO;
1015 } else if (!strcmp(str, "unret")) {
1016 retbleed_cmd = RETBLEED_CMD_UNRET;
1017 } else if (!strcmp(str, "ibpb")) {
1018 retbleed_cmd = RETBLEED_CMD_IBPB;
1019 } else if (!strcmp(str, "stuff")) {
1020 retbleed_cmd = RETBLEED_CMD_STUFF;
1021 } else if (!strcmp(str, "nosmt")) {
1022 retbleed_nosmt = true;
1023 } else if (!strcmp(str, "force")) {
1024 setup_force_cpu_bug(X86_BUG_RETBLEED);
1025 } else {
1026 pr_err("Ignoring unknown retbleed option (%s).", str);
1027 }
1028
1029 str = next;
1030 }
1031
1032 return 0;
1033 }
1034 early_param("retbleed", retbleed_parse_cmdline);
1035
1036 #define RETBLEED_UNTRAIN_MSG "WARNING: BTB untrained return thunk mitigation is only effective on AMD/Hygon!\n"
1037 #define RETBLEED_INTEL_MSG "WARNING: Spectre v2 mitigation leaves CPU vulnerable to RETBleed attacks, data leaks possible!\n"
1038
1039 static void __init retbleed_select_mitigation(void)
1040 {
1041 bool mitigate_smt = false;
1042
1043 if (!boot_cpu_has_bug(X86_BUG_RETBLEED) || cpu_mitigations_off())
1044 return;
1045
1046 switch (retbleed_cmd) {
1047 case RETBLEED_CMD_OFF:
1048 return;
1049
1050 case RETBLEED_CMD_UNRET:
1051 if (IS_ENABLED(CONFIG_MITIGATION_UNRET_ENTRY)) {
1052 retbleed_mitigation = RETBLEED_MITIGATION_UNRET;
1053 } else {
1054 pr_err("WARNING: kernel not compiled with MITIGATION_UNRET_ENTRY.\n");
1055 goto do_cmd_auto;
1056 }
1057 break;
1058
1059 case RETBLEED_CMD_IBPB:
1060 if (!boot_cpu_has(X86_FEATURE_IBPB)) {
1061 pr_err("WARNING: CPU does not support IBPB.\n");
1062 goto do_cmd_auto;
1063 } else if (IS_ENABLED(CONFIG_MITIGATION_IBPB_ENTRY)) {
1064 retbleed_mitigation = RETBLEED_MITIGATION_IBPB;
1065 } else {
1066 pr_err("WARNING: kernel not compiled with MITIGATION_IBPB_ENTRY.\n");
1067 goto do_cmd_auto;
1068 }
1069 break;
1070
1071 case RETBLEED_CMD_STUFF:
1072 if (IS_ENABLED(CONFIG_MITIGATION_CALL_DEPTH_TRACKING) &&
1073 spectre_v2_enabled == SPECTRE_V2_RETPOLINE) {
1074 retbleed_mitigation = RETBLEED_MITIGATION_STUFF;
1075
1076 } else {
1077 if (IS_ENABLED(CONFIG_MITIGATION_CALL_DEPTH_TRACKING))
1078 pr_err("WARNING: retbleed=stuff depends on spectre_v2=retpoline\n");
1079 else
1080 pr_err("WARNING: kernel not compiled with MITIGATION_CALL_DEPTH_TRACKING.\n");
1081
1082 goto do_cmd_auto;
1083 }
1084 break;
1085
1086 do_cmd_auto:
1087 case RETBLEED_CMD_AUTO:
1088 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD ||
1089 boot_cpu_data.x86_vendor == X86_VENDOR_HYGON) {
1090 if (IS_ENABLED(CONFIG_MITIGATION_UNRET_ENTRY))
1091 retbleed_mitigation = RETBLEED_MITIGATION_UNRET;
1092 else if (IS_ENABLED(CONFIG_MITIGATION_IBPB_ENTRY) &&
1093 boot_cpu_has(X86_FEATURE_IBPB))
1094 retbleed_mitigation = RETBLEED_MITIGATION_IBPB;
1095 }
1096
1097 /*
1098 * The Intel mitigation (IBRS or eIBRS) was already selected in
1099 * spectre_v2_select_mitigation(). 'retbleed_mitigation' will
1100 * be set accordingly below.
1101 */
1102
1103 break;
1104 }
1105
1106 switch (retbleed_mitigation) {
1107 case RETBLEED_MITIGATION_UNRET:
1108 setup_force_cpu_cap(X86_FEATURE_RETHUNK);
1109 setup_force_cpu_cap(X86_FEATURE_UNRET);
1110
1111 x86_return_thunk = retbleed_return_thunk;
1112
1113 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD &&
1114 boot_cpu_data.x86_vendor != X86_VENDOR_HYGON)
1115 pr_err(RETBLEED_UNTRAIN_MSG);
1116
1117 mitigate_smt = true;
1118 break;
1119
1120 case RETBLEED_MITIGATION_IBPB:
1121 setup_force_cpu_cap(X86_FEATURE_ENTRY_IBPB);
1122 setup_force_cpu_cap(X86_FEATURE_IBPB_ON_VMEXIT);
1123 mitigate_smt = true;
1124 break;
1125
1126 case RETBLEED_MITIGATION_STUFF:
1127 setup_force_cpu_cap(X86_FEATURE_RETHUNK);
1128 setup_force_cpu_cap(X86_FEATURE_CALL_DEPTH);
1129
1130 x86_return_thunk = call_depth_return_thunk;
1131 break;
1132
1133 default:
1134 break;
1135 }
1136
1137 if (mitigate_smt && !boot_cpu_has(X86_FEATURE_STIBP) &&
1138 (retbleed_nosmt || cpu_mitigations_auto_nosmt()))
1139 cpu_smt_disable(false);
1140
1141 /*
1142 * Let IBRS trump all on Intel without affecting the effects of the
1143 * retbleed= cmdline option except for call depth based stuffing
1144 */
1145 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) {
1146 switch (spectre_v2_enabled) {
1147 case SPECTRE_V2_IBRS:
1148 retbleed_mitigation = RETBLEED_MITIGATION_IBRS;
1149 break;
1150 case SPECTRE_V2_EIBRS:
1151 case SPECTRE_V2_EIBRS_RETPOLINE:
1152 case SPECTRE_V2_EIBRS_LFENCE:
1153 retbleed_mitigation = RETBLEED_MITIGATION_EIBRS;
1154 break;
1155 default:
1156 if (retbleed_mitigation != RETBLEED_MITIGATION_STUFF)
1157 pr_err(RETBLEED_INTEL_MSG);
1158 }
1159 }
1160
1161 pr_info("%s\n", retbleed_strings[retbleed_mitigation]);
1162 }
1163
1164 #undef pr_fmt
1165 #define pr_fmt(fmt) "Spectre V2 : " fmt
1166
1167 static enum spectre_v2_user_mitigation spectre_v2_user_stibp __ro_after_init =
1168 SPECTRE_V2_USER_NONE;
1169 static enum spectre_v2_user_mitigation spectre_v2_user_ibpb __ro_after_init =
1170 SPECTRE_V2_USER_NONE;
1171
1172 #ifdef CONFIG_MITIGATION_RETPOLINE
1173 static bool spectre_v2_bad_module;
1174
1175 bool retpoline_module_ok(bool has_retpoline)
1176 {
1177 if (spectre_v2_enabled == SPECTRE_V2_NONE || has_retpoline)
1178 return true;
1179
1180 pr_err("System may be vulnerable to spectre v2\n");
1181 spectre_v2_bad_module = true;
1182 return false;
1183 }
1184
1185 static inline const char *spectre_v2_module_string(void)
1186 {
1187 return spectre_v2_bad_module ? " - vulnerable module loaded" : "";
1188 }
1189 #else
1190 static inline const char *spectre_v2_module_string(void) { return ""; }
1191 #endif
1192
1193 #define SPECTRE_V2_LFENCE_MSG "WARNING: LFENCE mitigation is not recommended for this CPU, data leaks possible!\n"
1194 #define SPECTRE_V2_EIBRS_EBPF_MSG "WARNING: Unprivileged eBPF is enabled with eIBRS on, data leaks possible via Spectre v2 BHB attacks!\n"
1195 #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"
1196 #define SPECTRE_V2_IBRS_PERF_MSG "WARNING: IBRS mitigation selected on Enhanced IBRS CPU, this may cause unnecessary performance loss\n"
1197
1198 #ifdef CONFIG_BPF_SYSCALL
1199 void unpriv_ebpf_notify(int new_state)
1200 {
1201 if (new_state)
1202 return;
1203
1204 /* Unprivileged eBPF is enabled */
1205
1206 switch (spectre_v2_enabled) {
1207 case SPECTRE_V2_EIBRS:
1208 pr_err(SPECTRE_V2_EIBRS_EBPF_MSG);
1209 break;
1210 case SPECTRE_V2_EIBRS_LFENCE:
1211 if (sched_smt_active())
1212 pr_err(SPECTRE_V2_EIBRS_LFENCE_EBPF_SMT_MSG);
1213 break;
1214 default:
1215 break;
1216 }
1217 }
1218 #endif
1219
1220 static inline bool match_option(const char *arg, int arglen, const char *opt)
1221 {
1222 int len = strlen(opt);
1223
1224 return len == arglen && !strncmp(arg, opt, len);
1225 }
1226
1227 /* The kernel command line selection for spectre v2 */
1228 enum spectre_v2_mitigation_cmd {
1229 SPECTRE_V2_CMD_NONE,
1230 SPECTRE_V2_CMD_AUTO,
1231 SPECTRE_V2_CMD_FORCE,
1232 SPECTRE_V2_CMD_RETPOLINE,
1233 SPECTRE_V2_CMD_RETPOLINE_GENERIC,
1234 SPECTRE_V2_CMD_RETPOLINE_LFENCE,
1235 SPECTRE_V2_CMD_EIBRS,
1236 SPECTRE_V2_CMD_EIBRS_RETPOLINE,
1237 SPECTRE_V2_CMD_EIBRS_LFENCE,
1238 SPECTRE_V2_CMD_IBRS,
1239 };
1240
1241 enum spectre_v2_user_cmd {
1242 SPECTRE_V2_USER_CMD_NONE,
1243 SPECTRE_V2_USER_CMD_AUTO,
1244 SPECTRE_V2_USER_CMD_FORCE,
1245 SPECTRE_V2_USER_CMD_PRCTL,
1246 SPECTRE_V2_USER_CMD_PRCTL_IBPB,
1247 SPECTRE_V2_USER_CMD_SECCOMP,
1248 SPECTRE_V2_USER_CMD_SECCOMP_IBPB,
1249 };
1250
1251 static const char * const spectre_v2_user_strings[] = {
1252 [SPECTRE_V2_USER_NONE] = "User space: Vulnerable",
1253 [SPECTRE_V2_USER_STRICT] = "User space: Mitigation: STIBP protection",
1254 [SPECTRE_V2_USER_STRICT_PREFERRED] = "User space: Mitigation: STIBP always-on protection",
1255 [SPECTRE_V2_USER_PRCTL] = "User space: Mitigation: STIBP via prctl",
1256 [SPECTRE_V2_USER_SECCOMP] = "User space: Mitigation: STIBP via seccomp and prctl",
1257 };
1258
1259 static const struct {
1260 const char *option;
1261 enum spectre_v2_user_cmd cmd;
1262 bool secure;
1263 } v2_user_options[] __initconst = {
1264 { "auto", SPECTRE_V2_USER_CMD_AUTO, false },
1265 { "off", SPECTRE_V2_USER_CMD_NONE, false },
1266 { "on", SPECTRE_V2_USER_CMD_FORCE, true },
1267 { "prctl", SPECTRE_V2_USER_CMD_PRCTL, false },
1268 { "prctl,ibpb", SPECTRE_V2_USER_CMD_PRCTL_IBPB, false },
1269 { "seccomp", SPECTRE_V2_USER_CMD_SECCOMP, false },
1270 { "seccomp,ibpb", SPECTRE_V2_USER_CMD_SECCOMP_IBPB, false },
1271 };
1272
1273 static void __init spec_v2_user_print_cond(const char *reason, bool secure)
1274 {
1275 if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2) != secure)
1276 pr_info("spectre_v2_user=%s forced on command line.\n", reason);
1277 }
1278
1279 static __ro_after_init enum spectre_v2_mitigation_cmd spectre_v2_cmd;
1280
1281 static enum spectre_v2_user_cmd __init
1282 spectre_v2_parse_user_cmdline(void)
1283 {
1284 char arg[20];
1285 int ret, i;
1286
1287 switch (spectre_v2_cmd) {
1288 case SPECTRE_V2_CMD_NONE:
1289 return SPECTRE_V2_USER_CMD_NONE;
1290 case SPECTRE_V2_CMD_FORCE:
1291 return SPECTRE_V2_USER_CMD_FORCE;
1292 default:
1293 break;
1294 }
1295
1296 ret = cmdline_find_option(boot_command_line, "spectre_v2_user",
1297 arg, sizeof(arg));
1298 if (ret < 0)
1299 return SPECTRE_V2_USER_CMD_AUTO;
1300
1301 for (i = 0; i < ARRAY_SIZE(v2_user_options); i++) {
1302 if (match_option(arg, ret, v2_user_options[i].option)) {
1303 spec_v2_user_print_cond(v2_user_options[i].option,
1304 v2_user_options[i].secure);
1305 return v2_user_options[i].cmd;
1306 }
1307 }
1308
1309 pr_err("Unknown user space protection option (%s). Switching to AUTO select\n", arg);
1310 return SPECTRE_V2_USER_CMD_AUTO;
1311 }
1312
1313 static inline bool spectre_v2_in_ibrs_mode(enum spectre_v2_mitigation mode)
1314 {
1315 return spectre_v2_in_eibrs_mode(mode) || mode == SPECTRE_V2_IBRS;
1316 }
1317
1318 static void __init
1319 spectre_v2_user_select_mitigation(void)
1320 {
1321 enum spectre_v2_user_mitigation mode = SPECTRE_V2_USER_NONE;
1322 bool smt_possible = IS_ENABLED(CONFIG_SMP);
1323 enum spectre_v2_user_cmd cmd;
1324
1325 if (!boot_cpu_has(X86_FEATURE_IBPB) && !boot_cpu_has(X86_FEATURE_STIBP))
1326 return;
1327
1328 if (cpu_smt_control == CPU_SMT_FORCE_DISABLED ||
1329 cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
1330 smt_possible = false;
1331
1332 cmd = spectre_v2_parse_user_cmdline();
1333 switch (cmd) {
1334 case SPECTRE_V2_USER_CMD_NONE:
1335 goto set_mode;
1336 case SPECTRE_V2_USER_CMD_FORCE:
1337 mode = SPECTRE_V2_USER_STRICT;
1338 break;
1339 case SPECTRE_V2_USER_CMD_AUTO:
1340 case SPECTRE_V2_USER_CMD_PRCTL:
1341 case SPECTRE_V2_USER_CMD_PRCTL_IBPB:
1342 mode = SPECTRE_V2_USER_PRCTL;
1343 break;
1344 case SPECTRE_V2_USER_CMD_SECCOMP:
1345 case SPECTRE_V2_USER_CMD_SECCOMP_IBPB:
1346 if (IS_ENABLED(CONFIG_SECCOMP))
1347 mode = SPECTRE_V2_USER_SECCOMP;
1348 else
1349 mode = SPECTRE_V2_USER_PRCTL;
1350 break;
1351 }
1352
1353 /* Initialize Indirect Branch Prediction Barrier */
1354 if (boot_cpu_has(X86_FEATURE_IBPB)) {
1355 setup_force_cpu_cap(X86_FEATURE_USE_IBPB);
1356
1357 spectre_v2_user_ibpb = mode;
1358 switch (cmd) {
1359 case SPECTRE_V2_USER_CMD_NONE:
1360 break;
1361 case SPECTRE_V2_USER_CMD_FORCE:
1362 case SPECTRE_V2_USER_CMD_PRCTL_IBPB:
1363 case SPECTRE_V2_USER_CMD_SECCOMP_IBPB:
1364 static_branch_enable(&switch_mm_always_ibpb);
1365 spectre_v2_user_ibpb = SPECTRE_V2_USER_STRICT;
1366 break;
1367 case SPECTRE_V2_USER_CMD_PRCTL:
1368 case SPECTRE_V2_USER_CMD_AUTO:
1369 case SPECTRE_V2_USER_CMD_SECCOMP:
1370 static_branch_enable(&switch_mm_cond_ibpb);
1371 break;
1372 }
1373
1374 pr_info("mitigation: Enabling %s Indirect Branch Prediction Barrier\n",
1375 static_key_enabled(&switch_mm_always_ibpb) ?
1376 "always-on" : "conditional");
1377 }
1378
1379 /*
1380 * If no STIBP, Intel enhanced IBRS is enabled, or SMT impossible, STIBP
1381 * is not required.
1382 *
1383 * Intel's Enhanced IBRS also protects against cross-thread branch target
1384 * injection in user-mode as the IBRS bit remains always set which
1385 * implicitly enables cross-thread protections. However, in legacy IBRS
1386 * mode, the IBRS bit is set only on kernel entry and cleared on return
1387 * to userspace. AMD Automatic IBRS also does not protect userspace.
1388 * These modes therefore disable the implicit cross-thread protection,
1389 * so allow for STIBP to be selected in those cases.
1390 */
1391 if (!boot_cpu_has(X86_FEATURE_STIBP) ||
1392 !smt_possible ||
1393 (spectre_v2_in_eibrs_mode(spectre_v2_enabled) &&
1394 !boot_cpu_has(X86_FEATURE_AUTOIBRS)))
1395 return;
1396
1397 /*
1398 * At this point, an STIBP mode other than "off" has been set.
1399 * If STIBP support is not being forced, check if STIBP always-on
1400 * is preferred.
1401 */
1402 if (mode != SPECTRE_V2_USER_STRICT &&
1403 boot_cpu_has(X86_FEATURE_AMD_STIBP_ALWAYS_ON))
1404 mode = SPECTRE_V2_USER_STRICT_PREFERRED;
1405
1406 if (retbleed_mitigation == RETBLEED_MITIGATION_UNRET ||
1407 retbleed_mitigation == RETBLEED_MITIGATION_IBPB) {
1408 if (mode != SPECTRE_V2_USER_STRICT &&
1409 mode != SPECTRE_V2_USER_STRICT_PREFERRED)
1410 pr_info("Selecting STIBP always-on mode to complement retbleed mitigation\n");
1411 mode = SPECTRE_V2_USER_STRICT_PREFERRED;
1412 }
1413
1414 spectre_v2_user_stibp = mode;
1415
1416 set_mode:
1417 pr_info("%s\n", spectre_v2_user_strings[mode]);
1418 }
1419
1420 static const char * const spectre_v2_strings[] = {
1421 [SPECTRE_V2_NONE] = "Vulnerable",
1422 [SPECTRE_V2_RETPOLINE] = "Mitigation: Retpolines",
1423 [SPECTRE_V2_LFENCE] = "Mitigation: LFENCE",
1424 [SPECTRE_V2_EIBRS] = "Mitigation: Enhanced / Automatic IBRS",
1425 [SPECTRE_V2_EIBRS_LFENCE] = "Mitigation: Enhanced / Automatic IBRS + LFENCE",
1426 [SPECTRE_V2_EIBRS_RETPOLINE] = "Mitigation: Enhanced / Automatic IBRS + Retpolines",
1427 [SPECTRE_V2_IBRS] = "Mitigation: IBRS",
1428 };
1429
1430 static const struct {
1431 const char *option;
1432 enum spectre_v2_mitigation_cmd cmd;
1433 bool secure;
1434 } mitigation_options[] __initconst = {
1435 { "off", SPECTRE_V2_CMD_NONE, false },
1436 { "on", SPECTRE_V2_CMD_FORCE, true },
1437 { "retpoline", SPECTRE_V2_CMD_RETPOLINE, false },
1438 { "retpoline,amd", SPECTRE_V2_CMD_RETPOLINE_LFENCE, false },
1439 { "retpoline,lfence", SPECTRE_V2_CMD_RETPOLINE_LFENCE, false },
1440 { "retpoline,generic", SPECTRE_V2_CMD_RETPOLINE_GENERIC, false },
1441 { "eibrs", SPECTRE_V2_CMD_EIBRS, false },
1442 { "eibrs,lfence", SPECTRE_V2_CMD_EIBRS_LFENCE, false },
1443 { "eibrs,retpoline", SPECTRE_V2_CMD_EIBRS_RETPOLINE, false },
1444 { "auto", SPECTRE_V2_CMD_AUTO, false },
1445 { "ibrs", SPECTRE_V2_CMD_IBRS, false },
1446 };
1447
1448 static void __init spec_v2_print_cond(const char *reason, bool secure)
1449 {
1450 if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2) != secure)
1451 pr_info("%s selected on command line.\n", reason);
1452 }
1453
1454 static enum spectre_v2_mitigation_cmd __init spectre_v2_parse_cmdline(void)
1455 {
1456 enum spectre_v2_mitigation_cmd cmd = SPECTRE_V2_CMD_AUTO;
1457 char arg[20];
1458 int ret, i;
1459
1460 if (cmdline_find_option_bool(boot_command_line, "nospectre_v2") ||
1461 cpu_mitigations_off())
1462 return SPECTRE_V2_CMD_NONE;
1463
1464 ret = cmdline_find_option(boot_command_line, "spectre_v2", arg, sizeof(arg));
1465 if (ret < 0)
1466 return SPECTRE_V2_CMD_AUTO;
1467
1468 for (i = 0; i < ARRAY_SIZE(mitigation_options); i++) {
1469 if (!match_option(arg, ret, mitigation_options[i].option))
1470 continue;
1471 cmd = mitigation_options[i].cmd;
1472 break;
1473 }
1474
1475 if (i >= ARRAY_SIZE(mitigation_options)) {
1476 pr_err("unknown option (%s). Switching to AUTO select\n", arg);
1477 return SPECTRE_V2_CMD_AUTO;
1478 }
1479
1480 if ((cmd == SPECTRE_V2_CMD_RETPOLINE ||
1481 cmd == SPECTRE_V2_CMD_RETPOLINE_LFENCE ||
1482 cmd == SPECTRE_V2_CMD_RETPOLINE_GENERIC ||
1483 cmd == SPECTRE_V2_CMD_EIBRS_LFENCE ||
1484 cmd == SPECTRE_V2_CMD_EIBRS_RETPOLINE) &&
1485 !IS_ENABLED(CONFIG_MITIGATION_RETPOLINE)) {
1486 pr_err("%s selected but not compiled in. Switching to AUTO select\n",
1487 mitigation_options[i].option);
1488 return SPECTRE_V2_CMD_AUTO;
1489 }
1490
1491 if ((cmd == SPECTRE_V2_CMD_EIBRS ||
1492 cmd == SPECTRE_V2_CMD_EIBRS_LFENCE ||
1493 cmd == SPECTRE_V2_CMD_EIBRS_RETPOLINE) &&
1494 !boot_cpu_has(X86_FEATURE_IBRS_ENHANCED)) {
1495 pr_err("%s selected but CPU doesn't have Enhanced or Automatic IBRS. Switching to AUTO select\n",
1496 mitigation_options[i].option);
1497 return SPECTRE_V2_CMD_AUTO;
1498 }
1499
1500 if ((cmd == SPECTRE_V2_CMD_RETPOLINE_LFENCE ||
1501 cmd == SPECTRE_V2_CMD_EIBRS_LFENCE) &&
1502 !boot_cpu_has(X86_FEATURE_LFENCE_RDTSC)) {
1503 pr_err("%s selected, but CPU doesn't have a serializing LFENCE. Switching to AUTO select\n",
1504 mitigation_options[i].option);
1505 return SPECTRE_V2_CMD_AUTO;
1506 }
1507
1508 if (cmd == SPECTRE_V2_CMD_IBRS && !IS_ENABLED(CONFIG_MITIGATION_IBRS_ENTRY)) {
1509 pr_err("%s selected but not compiled in. Switching to AUTO select\n",
1510 mitigation_options[i].option);
1511 return SPECTRE_V2_CMD_AUTO;
1512 }
1513
1514 if (cmd == SPECTRE_V2_CMD_IBRS && boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) {
1515 pr_err("%s selected but not Intel CPU. Switching to AUTO select\n",
1516 mitigation_options[i].option);
1517 return SPECTRE_V2_CMD_AUTO;
1518 }
1519
1520 if (cmd == SPECTRE_V2_CMD_IBRS && !boot_cpu_has(X86_FEATURE_IBRS)) {
1521 pr_err("%s selected but CPU doesn't have IBRS. Switching to AUTO select\n",
1522 mitigation_options[i].option);
1523 return SPECTRE_V2_CMD_AUTO;
1524 }
1525
1526 if (cmd == SPECTRE_V2_CMD_IBRS && cpu_feature_enabled(X86_FEATURE_XENPV)) {
1527 pr_err("%s selected but running as XenPV guest. Switching to AUTO select\n",
1528 mitigation_options[i].option);
1529 return SPECTRE_V2_CMD_AUTO;
1530 }
1531
1532 spec_v2_print_cond(mitigation_options[i].option,
1533 mitigation_options[i].secure);
1534 return cmd;
1535 }
1536
1537 static enum spectre_v2_mitigation __init spectre_v2_select_retpoline(void)
1538 {
1539 if (!IS_ENABLED(CONFIG_MITIGATION_RETPOLINE)) {
1540 pr_err("Kernel not compiled with retpoline; no mitigation available!");
1541 return SPECTRE_V2_NONE;
1542 }
1543
1544 return SPECTRE_V2_RETPOLINE;
1545 }
1546
1547 /* Disable in-kernel use of non-RSB RET predictors */
1548 static void __init spec_ctrl_disable_kernel_rrsba(void)
1549 {
1550 u64 ia32_cap;
1551
1552 if (!boot_cpu_has(X86_FEATURE_RRSBA_CTRL))
1553 return;
1554
1555 ia32_cap = x86_read_arch_cap_msr();
1556
1557 if (ia32_cap & ARCH_CAP_RRSBA) {
1558 x86_spec_ctrl_base |= SPEC_CTRL_RRSBA_DIS_S;
1559 update_spec_ctrl(x86_spec_ctrl_base);
1560 }
1561 }
1562
1563 static void __init spectre_v2_determine_rsb_fill_type_at_vmexit(enum spectre_v2_mitigation mode)
1564 {
1565 /*
1566 * Similar to context switches, there are two types of RSB attacks
1567 * after VM exit:
1568 *
1569 * 1) RSB underflow
1570 *
1571 * 2) Poisoned RSB entry
1572 *
1573 * When retpoline is enabled, both are mitigated by filling/clearing
1574 * the RSB.
1575 *
1576 * When IBRS is enabled, while #1 would be mitigated by the IBRS branch
1577 * prediction isolation protections, RSB still needs to be cleared
1578 * because of #2. Note that SMEP provides no protection here, unlike
1579 * user-space-poisoned RSB entries.
1580 *
1581 * eIBRS should protect against RSB poisoning, but if the EIBRS_PBRSB
1582 * bug is present then a LITE version of RSB protection is required,
1583 * just a single call needs to retire before a RET is executed.
1584 */
1585 switch (mode) {
1586 case SPECTRE_V2_NONE:
1587 return;
1588
1589 case SPECTRE_V2_EIBRS_LFENCE:
1590 case SPECTRE_V2_EIBRS:
1591 if (boot_cpu_has_bug(X86_BUG_EIBRS_PBRSB)) {
1592 setup_force_cpu_cap(X86_FEATURE_RSB_VMEXIT_LITE);
1593 pr_info("Spectre v2 / PBRSB-eIBRS: Retire a single CALL on VMEXIT\n");
1594 }
1595 return;
1596
1597 case SPECTRE_V2_EIBRS_RETPOLINE:
1598 case SPECTRE_V2_RETPOLINE:
1599 case SPECTRE_V2_LFENCE:
1600 case SPECTRE_V2_IBRS:
1601 setup_force_cpu_cap(X86_FEATURE_RSB_VMEXIT);
1602 pr_info("Spectre v2 / SpectreRSB : Filling RSB on VMEXIT\n");
1603 return;
1604 }
1605
1606 pr_warn_once("Unknown Spectre v2 mode, disabling RSB mitigation at VM exit");
1607 dump_stack();
1608 }
1609
1610 /*
1611 * Set BHI_DIS_S to prevent indirect branches in kernel to be influenced by
1612 * branch history in userspace. Not needed if BHI_NO is set.
1613 */
1614 static bool __init spec_ctrl_bhi_dis(void)
1615 {
1616 if (!boot_cpu_has(X86_FEATURE_BHI_CTRL))
1617 return false;
1618
1619 x86_spec_ctrl_base |= SPEC_CTRL_BHI_DIS_S;
1620 update_spec_ctrl(x86_spec_ctrl_base);
1621 setup_force_cpu_cap(X86_FEATURE_CLEAR_BHB_HW);
1622
1623 return true;
1624 }
1625
1626 enum bhi_mitigations {
1627 BHI_MITIGATION_OFF,
1628 BHI_MITIGATION_ON,
1629 BHI_MITIGATION_AUTO,
1630 };
1631
1632 static enum bhi_mitigations bhi_mitigation __ro_after_init =
1633 IS_ENABLED(CONFIG_SPECTRE_BHI_ON) ? BHI_MITIGATION_ON :
1634 IS_ENABLED(CONFIG_SPECTRE_BHI_OFF) ? BHI_MITIGATION_OFF :
1635 BHI_MITIGATION_AUTO;
1636
1637 static int __init spectre_bhi_parse_cmdline(char *str)
1638 {
1639 if (!str)
1640 return -EINVAL;
1641
1642 if (!strcmp(str, "off"))
1643 bhi_mitigation = BHI_MITIGATION_OFF;
1644 else if (!strcmp(str, "on"))
1645 bhi_mitigation = BHI_MITIGATION_ON;
1646 else if (!strcmp(str, "auto"))
1647 bhi_mitigation = BHI_MITIGATION_AUTO;
1648 else
1649 pr_err("Ignoring unknown spectre_bhi option (%s)", str);
1650
1651 return 0;
1652 }
1653 early_param("spectre_bhi", spectre_bhi_parse_cmdline);
1654
1655 static void __init bhi_select_mitigation(void)
1656 {
1657 if (bhi_mitigation == BHI_MITIGATION_OFF)
1658 return;
1659
1660 /* Retpoline mitigates against BHI unless the CPU has RRSBA behavior */
1661 if (cpu_feature_enabled(X86_FEATURE_RETPOLINE) &&
1662 !(x86_read_arch_cap_msr() & ARCH_CAP_RRSBA))
1663 return;
1664
1665 if (spec_ctrl_bhi_dis())
1666 return;
1667
1668 if (!IS_ENABLED(CONFIG_X86_64))
1669 return;
1670
1671 /* Mitigate KVM by default */
1672 setup_force_cpu_cap(X86_FEATURE_CLEAR_BHB_LOOP_ON_VMEXIT);
1673 pr_info("Spectre BHI mitigation: SW BHB clearing on vm exit\n");
1674
1675 if (bhi_mitigation == BHI_MITIGATION_AUTO)
1676 return;
1677
1678 /* Mitigate syscalls when the mitigation is forced =on */
1679 setup_force_cpu_cap(X86_FEATURE_CLEAR_BHB_LOOP);
1680 pr_info("Spectre BHI mitigation: SW BHB clearing on syscall\n");
1681 }
1682
1683 static void __init spectre_v2_select_mitigation(void)
1684 {
1685 enum spectre_v2_mitigation_cmd cmd = spectre_v2_parse_cmdline();
1686 enum spectre_v2_mitigation mode = SPECTRE_V2_NONE;
1687
1688 /*
1689 * If the CPU is not affected and the command line mode is NONE or AUTO
1690 * then nothing to do.
1691 */
1692 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2) &&
1693 (cmd == SPECTRE_V2_CMD_NONE || cmd == SPECTRE_V2_CMD_AUTO))
1694 return;
1695
1696 switch (cmd) {
1697 case SPECTRE_V2_CMD_NONE:
1698 return;
1699
1700 case SPECTRE_V2_CMD_FORCE:
1701 case SPECTRE_V2_CMD_AUTO:
1702 if (boot_cpu_has(X86_FEATURE_IBRS_ENHANCED)) {
1703 mode = SPECTRE_V2_EIBRS;
1704 break;
1705 }
1706
1707 if (IS_ENABLED(CONFIG_MITIGATION_IBRS_ENTRY) &&
1708 boot_cpu_has_bug(X86_BUG_RETBLEED) &&
1709 retbleed_cmd != RETBLEED_CMD_OFF &&
1710 retbleed_cmd != RETBLEED_CMD_STUFF &&
1711 boot_cpu_has(X86_FEATURE_IBRS) &&
1712 boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) {
1713 mode = SPECTRE_V2_IBRS;
1714 break;
1715 }
1716
1717 mode = spectre_v2_select_retpoline();
1718 break;
1719
1720 case SPECTRE_V2_CMD_RETPOLINE_LFENCE:
1721 pr_err(SPECTRE_V2_LFENCE_MSG);
1722 mode = SPECTRE_V2_LFENCE;
1723 break;
1724
1725 case SPECTRE_V2_CMD_RETPOLINE_GENERIC:
1726 mode = SPECTRE_V2_RETPOLINE;
1727 break;
1728
1729 case SPECTRE_V2_CMD_RETPOLINE:
1730 mode = spectre_v2_select_retpoline();
1731 break;
1732
1733 case SPECTRE_V2_CMD_IBRS:
1734 mode = SPECTRE_V2_IBRS;
1735 break;
1736
1737 case SPECTRE_V2_CMD_EIBRS:
1738 mode = SPECTRE_V2_EIBRS;
1739 break;
1740
1741 case SPECTRE_V2_CMD_EIBRS_LFENCE:
1742 mode = SPECTRE_V2_EIBRS_LFENCE;
1743 break;
1744
1745 case SPECTRE_V2_CMD_EIBRS_RETPOLINE:
1746 mode = SPECTRE_V2_EIBRS_RETPOLINE;
1747 break;
1748 }
1749
1750 if (mode == SPECTRE_V2_EIBRS && unprivileged_ebpf_enabled())
1751 pr_err(SPECTRE_V2_EIBRS_EBPF_MSG);
1752
1753 if (spectre_v2_in_ibrs_mode(mode)) {
1754 if (boot_cpu_has(X86_FEATURE_AUTOIBRS)) {
1755 msr_set_bit(MSR_EFER, _EFER_AUTOIBRS);
1756 } else {
1757 x86_spec_ctrl_base |= SPEC_CTRL_IBRS;
1758 update_spec_ctrl(x86_spec_ctrl_base);
1759 }
1760 }
1761
1762 switch (mode) {
1763 case SPECTRE_V2_NONE:
1764 case SPECTRE_V2_EIBRS:
1765 break;
1766
1767 case SPECTRE_V2_IBRS:
1768 setup_force_cpu_cap(X86_FEATURE_KERNEL_IBRS);
1769 if (boot_cpu_has(X86_FEATURE_IBRS_ENHANCED))
1770 pr_warn(SPECTRE_V2_IBRS_PERF_MSG);
1771 break;
1772
1773 case SPECTRE_V2_LFENCE:
1774 case SPECTRE_V2_EIBRS_LFENCE:
1775 setup_force_cpu_cap(X86_FEATURE_RETPOLINE_LFENCE);
1776 fallthrough;
1777
1778 case SPECTRE_V2_RETPOLINE:
1779 case SPECTRE_V2_EIBRS_RETPOLINE:
1780 setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
1781 break;
1782 }
1783
1784 /*
1785 * Disable alternate RSB predictions in kernel when indirect CALLs and
1786 * JMPs gets protection against BHI and Intramode-BTI, but RET
1787 * prediction from a non-RSB predictor is still a risk.
1788 */
1789 if (mode == SPECTRE_V2_EIBRS_LFENCE ||
1790 mode == SPECTRE_V2_EIBRS_RETPOLINE ||
1791 mode == SPECTRE_V2_RETPOLINE)
1792 spec_ctrl_disable_kernel_rrsba();
1793
1794 if (boot_cpu_has(X86_BUG_BHI))
1795 bhi_select_mitigation();
1796
1797 spectre_v2_enabled = mode;
1798 pr_info("%s\n", spectre_v2_strings[mode]);
1799
1800 /*
1801 * If Spectre v2 protection has been enabled, fill the RSB during a
1802 * context switch. In general there are two types of RSB attacks
1803 * across context switches, for which the CALLs/RETs may be unbalanced.
1804 *
1805 * 1) RSB underflow
1806 *
1807 * Some Intel parts have "bottomless RSB". When the RSB is empty,
1808 * speculated return targets may come from the branch predictor,
1809 * which could have a user-poisoned BTB or BHB entry.
1810 *
1811 * AMD has it even worse: *all* returns are speculated from the BTB,
1812 * regardless of the state of the RSB.
1813 *
1814 * When IBRS or eIBRS is enabled, the "user -> kernel" attack
1815 * scenario is mitigated by the IBRS branch prediction isolation
1816 * properties, so the RSB buffer filling wouldn't be necessary to
1817 * protect against this type of attack.
1818 *
1819 * The "user -> user" attack scenario is mitigated by RSB filling.
1820 *
1821 * 2) Poisoned RSB entry
1822 *
1823 * If the 'next' in-kernel return stack is shorter than 'prev',
1824 * 'next' could be tricked into speculating with a user-poisoned RSB
1825 * entry.
1826 *
1827 * The "user -> kernel" attack scenario is mitigated by SMEP and
1828 * eIBRS.
1829 *
1830 * The "user -> user" scenario, also known as SpectreBHB, requires
1831 * RSB clearing.
1832 *
1833 * So to mitigate all cases, unconditionally fill RSB on context
1834 * switches.
1835 *
1836 * FIXME: Is this pointless for retbleed-affected AMD?
1837 */
1838 setup_force_cpu_cap(X86_FEATURE_RSB_CTXSW);
1839 pr_info("Spectre v2 / SpectreRSB mitigation: Filling RSB on context switch\n");
1840
1841 spectre_v2_determine_rsb_fill_type_at_vmexit(mode);
1842
1843 /*
1844 * Retpoline protects the kernel, but doesn't protect firmware. IBRS
1845 * and Enhanced IBRS protect firmware too, so enable IBRS around
1846 * firmware calls only when IBRS / Enhanced / Automatic IBRS aren't
1847 * otherwise enabled.
1848 *
1849 * Use "mode" to check Enhanced IBRS instead of boot_cpu_has(), because
1850 * the user might select retpoline on the kernel command line and if
1851 * the CPU supports Enhanced IBRS, kernel might un-intentionally not
1852 * enable IBRS around firmware calls.
1853 */
1854 if (boot_cpu_has_bug(X86_BUG_RETBLEED) &&
1855 boot_cpu_has(X86_FEATURE_IBPB) &&
1856 (boot_cpu_data.x86_vendor == X86_VENDOR_AMD ||
1857 boot_cpu_data.x86_vendor == X86_VENDOR_HYGON)) {
1858
1859 if (retbleed_cmd != RETBLEED_CMD_IBPB) {
1860 setup_force_cpu_cap(X86_FEATURE_USE_IBPB_FW);
1861 pr_info("Enabling Speculation Barrier for firmware calls\n");
1862 }
1863
1864 } else if (boot_cpu_has(X86_FEATURE_IBRS) && !spectre_v2_in_ibrs_mode(mode)) {
1865 setup_force_cpu_cap(X86_FEATURE_USE_IBRS_FW);
1866 pr_info("Enabling Restricted Speculation for firmware calls\n");
1867 }
1868
1869 /* Set up IBPB and STIBP depending on the general spectre V2 command */
1870 spectre_v2_cmd = cmd;
1871 }
1872
1873 static void update_stibp_msr(void * __unused)
1874 {
1875 u64 val = spec_ctrl_current() | (x86_spec_ctrl_base & SPEC_CTRL_STIBP);
1876 update_spec_ctrl(val);
1877 }
1878
1879 /* Update x86_spec_ctrl_base in case SMT state changed. */
1880 static void update_stibp_strict(void)
1881 {
1882 u64 mask = x86_spec_ctrl_base & ~SPEC_CTRL_STIBP;
1883
1884 if (sched_smt_active())
1885 mask |= SPEC_CTRL_STIBP;
1886
1887 if (mask == x86_spec_ctrl_base)
1888 return;
1889
1890 pr_info("Update user space SMT mitigation: STIBP %s\n",
1891 mask & SPEC_CTRL_STIBP ? "always-on" : "off");
1892 x86_spec_ctrl_base = mask;
1893 on_each_cpu(update_stibp_msr, NULL, 1);
1894 }
1895
1896 /* Update the static key controlling the evaluation of TIF_SPEC_IB */
1897 static void update_indir_branch_cond(void)
1898 {
1899 if (sched_smt_active())
1900 static_branch_enable(&switch_to_cond_stibp);
1901 else
1902 static_branch_disable(&switch_to_cond_stibp);
1903 }
1904
1905 #undef pr_fmt
1906 #define pr_fmt(fmt) fmt
1907
1908 /* Update the static key controlling the MDS CPU buffer clear in idle */
1909 static void update_mds_branch_idle(void)
1910 {
1911 u64 ia32_cap = x86_read_arch_cap_msr();
1912
1913 /*
1914 * Enable the idle clearing if SMT is active on CPUs which are
1915 * affected only by MSBDS and not any other MDS variant.
1916 *
1917 * The other variants cannot be mitigated when SMT is enabled, so
1918 * clearing the buffers on idle just to prevent the Store Buffer
1919 * repartitioning leak would be a window dressing exercise.
1920 */
1921 if (!boot_cpu_has_bug(X86_BUG_MSBDS_ONLY))
1922 return;
1923
1924 if (sched_smt_active()) {
1925 static_branch_enable(&mds_idle_clear);
1926 } else if (mmio_mitigation == MMIO_MITIGATION_OFF ||
1927 (ia32_cap & ARCH_CAP_FBSDP_NO)) {
1928 static_branch_disable(&mds_idle_clear);
1929 }
1930 }
1931
1932 #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"
1933 #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"
1934 #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"
1935
1936 void cpu_bugs_smt_update(void)
1937 {
1938 mutex_lock(&spec_ctrl_mutex);
1939
1940 if (sched_smt_active() && unprivileged_ebpf_enabled() &&
1941 spectre_v2_enabled == SPECTRE_V2_EIBRS_LFENCE)
1942 pr_warn_once(SPECTRE_V2_EIBRS_LFENCE_EBPF_SMT_MSG);
1943
1944 switch (spectre_v2_user_stibp) {
1945 case SPECTRE_V2_USER_NONE:
1946 break;
1947 case SPECTRE_V2_USER_STRICT:
1948 case SPECTRE_V2_USER_STRICT_PREFERRED:
1949 update_stibp_strict();
1950 break;
1951 case SPECTRE_V2_USER_PRCTL:
1952 case SPECTRE_V2_USER_SECCOMP:
1953 update_indir_branch_cond();
1954 break;
1955 }
1956
1957 switch (mds_mitigation) {
1958 case MDS_MITIGATION_FULL:
1959 case MDS_MITIGATION_VMWERV:
1960 if (sched_smt_active() && !boot_cpu_has(X86_BUG_MSBDS_ONLY))
1961 pr_warn_once(MDS_MSG_SMT);
1962 update_mds_branch_idle();
1963 break;
1964 case MDS_MITIGATION_OFF:
1965 break;
1966 }
1967
1968 switch (taa_mitigation) {
1969 case TAA_MITIGATION_VERW:
1970 case TAA_MITIGATION_UCODE_NEEDED:
1971 if (sched_smt_active())
1972 pr_warn_once(TAA_MSG_SMT);
1973 break;
1974 case TAA_MITIGATION_TSX_DISABLED:
1975 case TAA_MITIGATION_OFF:
1976 break;
1977 }
1978
1979 switch (mmio_mitigation) {
1980 case MMIO_MITIGATION_VERW:
1981 case MMIO_MITIGATION_UCODE_NEEDED:
1982 if (sched_smt_active())
1983 pr_warn_once(MMIO_MSG_SMT);
1984 break;
1985 case MMIO_MITIGATION_OFF:
1986 break;
1987 }
1988
1989 mutex_unlock(&spec_ctrl_mutex);
1990 }
1991
1992 #undef pr_fmt
1993 #define pr_fmt(fmt) "Speculative Store Bypass: " fmt
1994
1995 static enum ssb_mitigation ssb_mode __ro_after_init = SPEC_STORE_BYPASS_NONE;
1996
1997 /* The kernel command line selection */
1998 enum ssb_mitigation_cmd {
1999 SPEC_STORE_BYPASS_CMD_NONE,
2000 SPEC_STORE_BYPASS_CMD_AUTO,
2001 SPEC_STORE_BYPASS_CMD_ON,
2002 SPEC_STORE_BYPASS_CMD_PRCTL,
2003 SPEC_STORE_BYPASS_CMD_SECCOMP,
2004 };
2005
2006 static const char * const ssb_strings[] = {
2007 [SPEC_STORE_BYPASS_NONE] = "Vulnerable",
2008 [SPEC_STORE_BYPASS_DISABLE] = "Mitigation: Speculative Store Bypass disabled",
2009 [SPEC_STORE_BYPASS_PRCTL] = "Mitigation: Speculative Store Bypass disabled via prctl",
2010 [SPEC_STORE_BYPASS_SECCOMP] = "Mitigation: Speculative Store Bypass disabled via prctl and seccomp",
2011 };
2012
2013 static const struct {
2014 const char *option;
2015 enum ssb_mitigation_cmd cmd;
2016 } ssb_mitigation_options[] __initconst = {
2017 { "auto", SPEC_STORE_BYPASS_CMD_AUTO }, /* Platform decides */
2018 { "on", SPEC_STORE_BYPASS_CMD_ON }, /* Disable Speculative Store Bypass */
2019 { "off", SPEC_STORE_BYPASS_CMD_NONE }, /* Don't touch Speculative Store Bypass */
2020 { "prctl", SPEC_STORE_BYPASS_CMD_PRCTL }, /* Disable Speculative Store Bypass via prctl */
2021 { "seccomp", SPEC_STORE_BYPASS_CMD_SECCOMP }, /* Disable Speculative Store Bypass via prctl and seccomp */
2022 };
2023
2024 static enum ssb_mitigation_cmd __init ssb_parse_cmdline(void)
2025 {
2026 enum ssb_mitigation_cmd cmd = SPEC_STORE_BYPASS_CMD_AUTO;
2027 char arg[20];
2028 int ret, i;
2029
2030 if (cmdline_find_option_bool(boot_command_line, "nospec_store_bypass_disable") ||
2031 cpu_mitigations_off()) {
2032 return SPEC_STORE_BYPASS_CMD_NONE;
2033 } else {
2034 ret = cmdline_find_option(boot_command_line, "spec_store_bypass_disable",
2035 arg, sizeof(arg));
2036 if (ret < 0)
2037 return SPEC_STORE_BYPASS_CMD_AUTO;
2038
2039 for (i = 0; i < ARRAY_SIZE(ssb_mitigation_options); i++) {
2040 if (!match_option(arg, ret, ssb_mitigation_options[i].option))
2041 continue;
2042
2043 cmd = ssb_mitigation_options[i].cmd;
2044 break;
2045 }
2046
2047 if (i >= ARRAY_SIZE(ssb_mitigation_options)) {
2048 pr_err("unknown option (%s). Switching to AUTO select\n", arg);
2049 return SPEC_STORE_BYPASS_CMD_AUTO;
2050 }
2051 }
2052
2053 return cmd;
2054 }
2055
2056 static enum ssb_mitigation __init __ssb_select_mitigation(void)
2057 {
2058 enum ssb_mitigation mode = SPEC_STORE_BYPASS_NONE;
2059 enum ssb_mitigation_cmd cmd;
2060
2061 if (!boot_cpu_has(X86_FEATURE_SSBD))
2062 return mode;
2063
2064 cmd = ssb_parse_cmdline();
2065 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS) &&
2066 (cmd == SPEC_STORE_BYPASS_CMD_NONE ||
2067 cmd == SPEC_STORE_BYPASS_CMD_AUTO))
2068 return mode;
2069
2070 switch (cmd) {
2071 case SPEC_STORE_BYPASS_CMD_SECCOMP:
2072 /*
2073 * Choose prctl+seccomp as the default mode if seccomp is
2074 * enabled.
2075 */
2076 if (IS_ENABLED(CONFIG_SECCOMP))
2077 mode = SPEC_STORE_BYPASS_SECCOMP;
2078 else
2079 mode = SPEC_STORE_BYPASS_PRCTL;
2080 break;
2081 case SPEC_STORE_BYPASS_CMD_ON:
2082 mode = SPEC_STORE_BYPASS_DISABLE;
2083 break;
2084 case SPEC_STORE_BYPASS_CMD_AUTO:
2085 case SPEC_STORE_BYPASS_CMD_PRCTL:
2086 mode = SPEC_STORE_BYPASS_PRCTL;
2087 break;
2088 case SPEC_STORE_BYPASS_CMD_NONE:
2089 break;
2090 }
2091
2092 /*
2093 * We have three CPU feature flags that are in play here:
2094 * - X86_BUG_SPEC_STORE_BYPASS - CPU is susceptible.
2095 * - X86_FEATURE_SSBD - CPU is able to turn off speculative store bypass
2096 * - X86_FEATURE_SPEC_STORE_BYPASS_DISABLE - engage the mitigation
2097 */
2098 if (mode == SPEC_STORE_BYPASS_DISABLE) {
2099 setup_force_cpu_cap(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE);
2100 /*
2101 * Intel uses the SPEC CTRL MSR Bit(2) for this, while AMD may
2102 * use a completely different MSR and bit dependent on family.
2103 */
2104 if (!static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) &&
2105 !static_cpu_has(X86_FEATURE_AMD_SSBD)) {
2106 x86_amd_ssb_disable();
2107 } else {
2108 x86_spec_ctrl_base |= SPEC_CTRL_SSBD;
2109 update_spec_ctrl(x86_spec_ctrl_base);
2110 }
2111 }
2112
2113 return mode;
2114 }
2115
2116 static void ssb_select_mitigation(void)
2117 {
2118 ssb_mode = __ssb_select_mitigation();
2119
2120 if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
2121 pr_info("%s\n", ssb_strings[ssb_mode]);
2122 }
2123
2124 #undef pr_fmt
2125 #define pr_fmt(fmt) "Speculation prctl: " fmt
2126
2127 static void task_update_spec_tif(struct task_struct *tsk)
2128 {
2129 /* Force the update of the real TIF bits */
2130 set_tsk_thread_flag(tsk, TIF_SPEC_FORCE_UPDATE);
2131
2132 /*
2133 * Immediately update the speculation control MSRs for the current
2134 * task, but for a non-current task delay setting the CPU
2135 * mitigation until it is scheduled next.
2136 *
2137 * This can only happen for SECCOMP mitigation. For PRCTL it's
2138 * always the current task.
2139 */
2140 if (tsk == current)
2141 speculation_ctrl_update_current();
2142 }
2143
2144 static int l1d_flush_prctl_set(struct task_struct *task, unsigned long ctrl)
2145 {
2146
2147 if (!static_branch_unlikely(&switch_mm_cond_l1d_flush))
2148 return -EPERM;
2149
2150 switch (ctrl) {
2151 case PR_SPEC_ENABLE:
2152 set_ti_thread_flag(&task->thread_info, TIF_SPEC_L1D_FLUSH);
2153 return 0;
2154 case PR_SPEC_DISABLE:
2155 clear_ti_thread_flag(&task->thread_info, TIF_SPEC_L1D_FLUSH);
2156 return 0;
2157 default:
2158 return -ERANGE;
2159 }
2160 }
2161
2162 static int ssb_prctl_set(struct task_struct *task, unsigned long ctrl)
2163 {
2164 if (ssb_mode != SPEC_STORE_BYPASS_PRCTL &&
2165 ssb_mode != SPEC_STORE_BYPASS_SECCOMP)
2166 return -ENXIO;
2167
2168 switch (ctrl) {
2169 case PR_SPEC_ENABLE:
2170 /* If speculation is force disabled, enable is not allowed */
2171 if (task_spec_ssb_force_disable(task))
2172 return -EPERM;
2173 task_clear_spec_ssb_disable(task);
2174 task_clear_spec_ssb_noexec(task);
2175 task_update_spec_tif(task);
2176 break;
2177 case PR_SPEC_DISABLE:
2178 task_set_spec_ssb_disable(task);
2179 task_clear_spec_ssb_noexec(task);
2180 task_update_spec_tif(task);
2181 break;
2182 case PR_SPEC_FORCE_DISABLE:
2183 task_set_spec_ssb_disable(task);
2184 task_set_spec_ssb_force_disable(task);
2185 task_clear_spec_ssb_noexec(task);
2186 task_update_spec_tif(task);
2187 break;
2188 case PR_SPEC_DISABLE_NOEXEC:
2189 if (task_spec_ssb_force_disable(task))
2190 return -EPERM;
2191 task_set_spec_ssb_disable(task);
2192 task_set_spec_ssb_noexec(task);
2193 task_update_spec_tif(task);
2194 break;
2195 default:
2196 return -ERANGE;
2197 }
2198 return 0;
2199 }
2200
2201 static bool is_spec_ib_user_controlled(void)
2202 {
2203 return spectre_v2_user_ibpb == SPECTRE_V2_USER_PRCTL ||
2204 spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP ||
2205 spectre_v2_user_stibp == SPECTRE_V2_USER_PRCTL ||
2206 spectre_v2_user_stibp == SPECTRE_V2_USER_SECCOMP;
2207 }
2208
2209 static int ib_prctl_set(struct task_struct *task, unsigned long ctrl)
2210 {
2211 switch (ctrl) {
2212 case PR_SPEC_ENABLE:
2213 if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
2214 spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
2215 return 0;
2216
2217 /*
2218 * With strict mode for both IBPB and STIBP, the instruction
2219 * code paths avoid checking this task flag and instead,
2220 * unconditionally run the instruction. However, STIBP and IBPB
2221 * are independent and either can be set to conditionally
2222 * enabled regardless of the mode of the other.
2223 *
2224 * If either is set to conditional, allow the task flag to be
2225 * updated, unless it was force-disabled by a previous prctl
2226 * call. Currently, this is possible on an AMD CPU which has the
2227 * feature X86_FEATURE_AMD_STIBP_ALWAYS_ON. In this case, if the
2228 * kernel is booted with 'spectre_v2_user=seccomp', then
2229 * spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP and
2230 * spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED.
2231 */
2232 if (!is_spec_ib_user_controlled() ||
2233 task_spec_ib_force_disable(task))
2234 return -EPERM;
2235
2236 task_clear_spec_ib_disable(task);
2237 task_update_spec_tif(task);
2238 break;
2239 case PR_SPEC_DISABLE:
2240 case PR_SPEC_FORCE_DISABLE:
2241 /*
2242 * Indirect branch speculation is always allowed when
2243 * mitigation is force disabled.
2244 */
2245 if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
2246 spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
2247 return -EPERM;
2248
2249 if (!is_spec_ib_user_controlled())
2250 return 0;
2251
2252 task_set_spec_ib_disable(task);
2253 if (ctrl == PR_SPEC_FORCE_DISABLE)
2254 task_set_spec_ib_force_disable(task);
2255 task_update_spec_tif(task);
2256 if (task == current)
2257 indirect_branch_prediction_barrier();
2258 break;
2259 default:
2260 return -ERANGE;
2261 }
2262 return 0;
2263 }
2264
2265 int arch_prctl_spec_ctrl_set(struct task_struct *task, unsigned long which,
2266 unsigned long ctrl)
2267 {
2268 switch (which) {
2269 case PR_SPEC_STORE_BYPASS:
2270 return ssb_prctl_set(task, ctrl);
2271 case PR_SPEC_INDIRECT_BRANCH:
2272 return ib_prctl_set(task, ctrl);
2273 case PR_SPEC_L1D_FLUSH:
2274 return l1d_flush_prctl_set(task, ctrl);
2275 default:
2276 return -ENODEV;
2277 }
2278 }
2279
2280 #ifdef CONFIG_SECCOMP
2281 void arch_seccomp_spec_mitigate(struct task_struct *task)
2282 {
2283 if (ssb_mode == SPEC_STORE_BYPASS_SECCOMP)
2284 ssb_prctl_set(task, PR_SPEC_FORCE_DISABLE);
2285 if (spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP ||
2286 spectre_v2_user_stibp == SPECTRE_V2_USER_SECCOMP)
2287 ib_prctl_set(task, PR_SPEC_FORCE_DISABLE);
2288 }
2289 #endif
2290
2291 static int l1d_flush_prctl_get(struct task_struct *task)
2292 {
2293 if (!static_branch_unlikely(&switch_mm_cond_l1d_flush))
2294 return PR_SPEC_FORCE_DISABLE;
2295
2296 if (test_ti_thread_flag(&task->thread_info, TIF_SPEC_L1D_FLUSH))
2297 return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
2298 else
2299 return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
2300 }
2301
2302 static int ssb_prctl_get(struct task_struct *task)
2303 {
2304 switch (ssb_mode) {
2305 case SPEC_STORE_BYPASS_NONE:
2306 if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
2307 return PR_SPEC_ENABLE;
2308 return PR_SPEC_NOT_AFFECTED;
2309 case SPEC_STORE_BYPASS_DISABLE:
2310 return PR_SPEC_DISABLE;
2311 case SPEC_STORE_BYPASS_SECCOMP:
2312 case SPEC_STORE_BYPASS_PRCTL:
2313 if (task_spec_ssb_force_disable(task))
2314 return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE;
2315 if (task_spec_ssb_noexec(task))
2316 return PR_SPEC_PRCTL | PR_SPEC_DISABLE_NOEXEC;
2317 if (task_spec_ssb_disable(task))
2318 return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
2319 return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
2320 }
2321 BUG();
2322 }
2323
2324 static int ib_prctl_get(struct task_struct *task)
2325 {
2326 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
2327 return PR_SPEC_NOT_AFFECTED;
2328
2329 if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
2330 spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
2331 return PR_SPEC_ENABLE;
2332 else if (is_spec_ib_user_controlled()) {
2333 if (task_spec_ib_force_disable(task))
2334 return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE;
2335 if (task_spec_ib_disable(task))
2336 return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
2337 return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
2338 } else if (spectre_v2_user_ibpb == SPECTRE_V2_USER_STRICT ||
2339 spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT ||
2340 spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED)
2341 return PR_SPEC_DISABLE;
2342 else
2343 return PR_SPEC_NOT_AFFECTED;
2344 }
2345
2346 int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which)
2347 {
2348 switch (which) {
2349 case PR_SPEC_STORE_BYPASS:
2350 return ssb_prctl_get(task);
2351 case PR_SPEC_INDIRECT_BRANCH:
2352 return ib_prctl_get(task);
2353 case PR_SPEC_L1D_FLUSH:
2354 return l1d_flush_prctl_get(task);
2355 default:
2356 return -ENODEV;
2357 }
2358 }
2359
2360 void x86_spec_ctrl_setup_ap(void)
2361 {
2362 if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
2363 update_spec_ctrl(x86_spec_ctrl_base);
2364
2365 if (ssb_mode == SPEC_STORE_BYPASS_DISABLE)
2366 x86_amd_ssb_disable();
2367 }
2368
2369 bool itlb_multihit_kvm_mitigation;
2370 EXPORT_SYMBOL_GPL(itlb_multihit_kvm_mitigation);
2371
2372 #undef pr_fmt
2373 #define pr_fmt(fmt) "L1TF: " fmt
2374
2375 /* Default mitigation for L1TF-affected CPUs */
2376 enum l1tf_mitigations l1tf_mitigation __ro_after_init = L1TF_MITIGATION_FLUSH;
2377 #if IS_ENABLED(CONFIG_KVM_INTEL)
2378 EXPORT_SYMBOL_GPL(l1tf_mitigation);
2379 #endif
2380 enum vmx_l1d_flush_state l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
2381 EXPORT_SYMBOL_GPL(l1tf_vmx_mitigation);
2382
2383 /*
2384 * These CPUs all support 44bits physical address space internally in the
2385 * cache but CPUID can report a smaller number of physical address bits.
2386 *
2387 * The L1TF mitigation uses the top most address bit for the inversion of
2388 * non present PTEs. When the installed memory reaches into the top most
2389 * address bit due to memory holes, which has been observed on machines
2390 * which report 36bits physical address bits and have 32G RAM installed,
2391 * then the mitigation range check in l1tf_select_mitigation() triggers.
2392 * This is a false positive because the mitigation is still possible due to
2393 * the fact that the cache uses 44bit internally. Use the cache bits
2394 * instead of the reported physical bits and adjust them on the affected
2395 * machines to 44bit if the reported bits are less than 44.
2396 */
2397 static void override_cache_bits(struct cpuinfo_x86 *c)
2398 {
2399 if (c->x86 != 6)
2400 return;
2401
2402 switch (c->x86_model) {
2403 case INTEL_FAM6_NEHALEM:
2404 case INTEL_FAM6_WESTMERE:
2405 case INTEL_FAM6_SANDYBRIDGE:
2406 case INTEL_FAM6_IVYBRIDGE:
2407 case INTEL_FAM6_HASWELL:
2408 case INTEL_FAM6_HASWELL_L:
2409 case INTEL_FAM6_HASWELL_G:
2410 case INTEL_FAM6_BROADWELL:
2411 case INTEL_FAM6_BROADWELL_G:
2412 case INTEL_FAM6_SKYLAKE_L:
2413 case INTEL_FAM6_SKYLAKE:
2414 case INTEL_FAM6_KABYLAKE_L:
2415 case INTEL_FAM6_KABYLAKE:
2416 if (c->x86_cache_bits < 44)
2417 c->x86_cache_bits = 44;
2418 break;
2419 }
2420 }
2421
2422 static void __init l1tf_select_mitigation(void)
2423 {
2424 u64 half_pa;
2425
2426 if (!boot_cpu_has_bug(X86_BUG_L1TF))
2427 return;
2428
2429 if (cpu_mitigations_off())
2430 l1tf_mitigation = L1TF_MITIGATION_OFF;
2431 else if (cpu_mitigations_auto_nosmt())
2432 l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOSMT;
2433
2434 override_cache_bits(&boot_cpu_data);
2435
2436 switch (l1tf_mitigation) {
2437 case L1TF_MITIGATION_OFF:
2438 case L1TF_MITIGATION_FLUSH_NOWARN:
2439 case L1TF_MITIGATION_FLUSH:
2440 break;
2441 case L1TF_MITIGATION_FLUSH_NOSMT:
2442 case L1TF_MITIGATION_FULL:
2443 cpu_smt_disable(false);
2444 break;
2445 case L1TF_MITIGATION_FULL_FORCE:
2446 cpu_smt_disable(true);
2447 break;
2448 }
2449
2450 #if CONFIG_PGTABLE_LEVELS == 2
2451 pr_warn("Kernel not compiled for PAE. No mitigation for L1TF\n");
2452 return;
2453 #endif
2454
2455 half_pa = (u64)l1tf_pfn_limit() << PAGE_SHIFT;
2456 if (l1tf_mitigation != L1TF_MITIGATION_OFF &&
2457 e820__mapped_any(half_pa, ULLONG_MAX - half_pa, E820_TYPE_RAM)) {
2458 pr_warn("System has more than MAX_PA/2 memory. L1TF mitigation not effective.\n");
2459 pr_info("You may make it effective by booting the kernel with mem=%llu parameter.\n",
2460 half_pa);
2461 pr_info("However, doing so will make a part of your RAM unusable.\n");
2462 pr_info("Reading https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html might help you decide.\n");
2463 return;
2464 }
2465
2466 setup_force_cpu_cap(X86_FEATURE_L1TF_PTEINV);
2467 }
2468
2469 static int __init l1tf_cmdline(char *str)
2470 {
2471 if (!boot_cpu_has_bug(X86_BUG_L1TF))
2472 return 0;
2473
2474 if (!str)
2475 return -EINVAL;
2476
2477 if (!strcmp(str, "off"))
2478 l1tf_mitigation = L1TF_MITIGATION_OFF;
2479 else if (!strcmp(str, "flush,nowarn"))
2480 l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOWARN;
2481 else if (!strcmp(str, "flush"))
2482 l1tf_mitigation = L1TF_MITIGATION_FLUSH;
2483 else if (!strcmp(str, "flush,nosmt"))
2484 l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOSMT;
2485 else if (!strcmp(str, "full"))
2486 l1tf_mitigation = L1TF_MITIGATION_FULL;
2487 else if (!strcmp(str, "full,force"))
2488 l1tf_mitigation = L1TF_MITIGATION_FULL_FORCE;
2489
2490 return 0;
2491 }
2492 early_param("l1tf", l1tf_cmdline);
2493
2494 #undef pr_fmt
2495 #define pr_fmt(fmt) "Speculative Return Stack Overflow: " fmt
2496
2497 enum srso_mitigation {
2498 SRSO_MITIGATION_NONE,
2499 SRSO_MITIGATION_UCODE_NEEDED,
2500 SRSO_MITIGATION_SAFE_RET_UCODE_NEEDED,
2501 SRSO_MITIGATION_MICROCODE,
2502 SRSO_MITIGATION_SAFE_RET,
2503 SRSO_MITIGATION_IBPB,
2504 SRSO_MITIGATION_IBPB_ON_VMEXIT,
2505 };
2506
2507 enum srso_mitigation_cmd {
2508 SRSO_CMD_OFF,
2509 SRSO_CMD_MICROCODE,
2510 SRSO_CMD_SAFE_RET,
2511 SRSO_CMD_IBPB,
2512 SRSO_CMD_IBPB_ON_VMEXIT,
2513 };
2514
2515 static const char * const srso_strings[] = {
2516 [SRSO_MITIGATION_NONE] = "Vulnerable",
2517 [SRSO_MITIGATION_UCODE_NEEDED] = "Vulnerable: No microcode",
2518 [SRSO_MITIGATION_SAFE_RET_UCODE_NEEDED] = "Vulnerable: Safe RET, no microcode",
2519 [SRSO_MITIGATION_MICROCODE] = "Vulnerable: Microcode, no safe RET",
2520 [SRSO_MITIGATION_SAFE_RET] = "Mitigation: Safe RET",
2521 [SRSO_MITIGATION_IBPB] = "Mitigation: IBPB",
2522 [SRSO_MITIGATION_IBPB_ON_VMEXIT] = "Mitigation: IBPB on VMEXIT only"
2523 };
2524
2525 static enum srso_mitigation srso_mitigation __ro_after_init = SRSO_MITIGATION_NONE;
2526 static enum srso_mitigation_cmd srso_cmd __ro_after_init = SRSO_CMD_SAFE_RET;
2527
2528 static int __init srso_parse_cmdline(char *str)
2529 {
2530 if (!str)
2531 return -EINVAL;
2532
2533 if (!strcmp(str, "off"))
2534 srso_cmd = SRSO_CMD_OFF;
2535 else if (!strcmp(str, "microcode"))
2536 srso_cmd = SRSO_CMD_MICROCODE;
2537 else if (!strcmp(str, "safe-ret"))
2538 srso_cmd = SRSO_CMD_SAFE_RET;
2539 else if (!strcmp(str, "ibpb"))
2540 srso_cmd = SRSO_CMD_IBPB;
2541 else if (!strcmp(str, "ibpb-vmexit"))
2542 srso_cmd = SRSO_CMD_IBPB_ON_VMEXIT;
2543 else
2544 pr_err("Ignoring unknown SRSO option (%s).", str);
2545
2546 return 0;
2547 }
2548 early_param("spec_rstack_overflow", srso_parse_cmdline);
2549
2550 #define SRSO_NOTICE "WARNING: See https://kernel.org/doc/html/latest/admin-guide/hw-vuln/srso.html for mitigation options."
2551
2552 static void __init srso_select_mitigation(void)
2553 {
2554 bool has_microcode = boot_cpu_has(X86_FEATURE_IBPB_BRTYPE);
2555
2556 if (cpu_mitigations_off())
2557 return;
2558
2559 if (!boot_cpu_has_bug(X86_BUG_SRSO)) {
2560 if (boot_cpu_has(X86_FEATURE_SBPB))
2561 x86_pred_cmd = PRED_CMD_SBPB;
2562 return;
2563 }
2564
2565 if (has_microcode) {
2566 /*
2567 * Zen1/2 with SMT off aren't vulnerable after the right
2568 * IBPB microcode has been applied.
2569 *
2570 * Zen1/2 don't have SBPB, no need to try to enable it here.
2571 */
2572 if (boot_cpu_data.x86 < 0x19 && !cpu_smt_possible()) {
2573 setup_force_cpu_cap(X86_FEATURE_SRSO_NO);
2574 return;
2575 }
2576
2577 if (retbleed_mitigation == RETBLEED_MITIGATION_IBPB) {
2578 srso_mitigation = SRSO_MITIGATION_IBPB;
2579 goto out;
2580 }
2581 } else {
2582 pr_warn("IBPB-extending microcode not applied!\n");
2583 pr_warn(SRSO_NOTICE);
2584
2585 /* may be overwritten by SRSO_CMD_SAFE_RET below */
2586 srso_mitigation = SRSO_MITIGATION_UCODE_NEEDED;
2587 }
2588
2589 switch (srso_cmd) {
2590 case SRSO_CMD_OFF:
2591 if (boot_cpu_has(X86_FEATURE_SBPB))
2592 x86_pred_cmd = PRED_CMD_SBPB;
2593 return;
2594
2595 case SRSO_CMD_MICROCODE:
2596 if (has_microcode) {
2597 srso_mitigation = SRSO_MITIGATION_MICROCODE;
2598 pr_warn(SRSO_NOTICE);
2599 }
2600 break;
2601
2602 case SRSO_CMD_SAFE_RET:
2603 if (IS_ENABLED(CONFIG_MITIGATION_SRSO)) {
2604 /*
2605 * Enable the return thunk for generated code
2606 * like ftrace, static_call, etc.
2607 */
2608 setup_force_cpu_cap(X86_FEATURE_RETHUNK);
2609 setup_force_cpu_cap(X86_FEATURE_UNRET);
2610
2611 if (boot_cpu_data.x86 == 0x19) {
2612 setup_force_cpu_cap(X86_FEATURE_SRSO_ALIAS);
2613 x86_return_thunk = srso_alias_return_thunk;
2614 } else {
2615 setup_force_cpu_cap(X86_FEATURE_SRSO);
2616 x86_return_thunk = srso_return_thunk;
2617 }
2618 if (has_microcode)
2619 srso_mitigation = SRSO_MITIGATION_SAFE_RET;
2620 else
2621 srso_mitigation = SRSO_MITIGATION_SAFE_RET_UCODE_NEEDED;
2622 } else {
2623 pr_err("WARNING: kernel not compiled with MITIGATION_SRSO.\n");
2624 }
2625 break;
2626
2627 case SRSO_CMD_IBPB:
2628 if (IS_ENABLED(CONFIG_MITIGATION_IBPB_ENTRY)) {
2629 if (has_microcode) {
2630 setup_force_cpu_cap(X86_FEATURE_ENTRY_IBPB);
2631 srso_mitigation = SRSO_MITIGATION_IBPB;
2632 }
2633 } else {
2634 pr_err("WARNING: kernel not compiled with MITIGATION_IBPB_ENTRY.\n");
2635 }
2636 break;
2637
2638 case SRSO_CMD_IBPB_ON_VMEXIT:
2639 if (IS_ENABLED(CONFIG_MITIGATION_SRSO)) {
2640 if (!boot_cpu_has(X86_FEATURE_ENTRY_IBPB) && has_microcode) {
2641 setup_force_cpu_cap(X86_FEATURE_IBPB_ON_VMEXIT);
2642 srso_mitigation = SRSO_MITIGATION_IBPB_ON_VMEXIT;
2643 }
2644 } else {
2645 pr_err("WARNING: kernel not compiled with MITIGATION_SRSO.\n");
2646 }
2647 break;
2648 }
2649
2650 out:
2651 pr_info("%s\n", srso_strings[srso_mitigation]);
2652 }
2653
2654 #undef pr_fmt
2655 #define pr_fmt(fmt) fmt
2656
2657 #ifdef CONFIG_SYSFS
2658
2659 #define L1TF_DEFAULT_MSG "Mitigation: PTE Inversion"
2660
2661 #if IS_ENABLED(CONFIG_KVM_INTEL)
2662 static const char * const l1tf_vmx_states[] = {
2663 [VMENTER_L1D_FLUSH_AUTO] = "auto",
2664 [VMENTER_L1D_FLUSH_NEVER] = "vulnerable",
2665 [VMENTER_L1D_FLUSH_COND] = "conditional cache flushes",
2666 [VMENTER_L1D_FLUSH_ALWAYS] = "cache flushes",
2667 [VMENTER_L1D_FLUSH_EPT_DISABLED] = "EPT disabled",
2668 [VMENTER_L1D_FLUSH_NOT_REQUIRED] = "flush not necessary"
2669 };
2670
2671 static ssize_t l1tf_show_state(char *buf)
2672 {
2673 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO)
2674 return sysfs_emit(buf, "%s\n", L1TF_DEFAULT_MSG);
2675
2676 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_EPT_DISABLED ||
2677 (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER &&
2678 sched_smt_active())) {
2679 return sysfs_emit(buf, "%s; VMX: %s\n", L1TF_DEFAULT_MSG,
2680 l1tf_vmx_states[l1tf_vmx_mitigation]);
2681 }
2682
2683 return sysfs_emit(buf, "%s; VMX: %s, SMT %s\n", L1TF_DEFAULT_MSG,
2684 l1tf_vmx_states[l1tf_vmx_mitigation],
2685 sched_smt_active() ? "vulnerable" : "disabled");
2686 }
2687
2688 static ssize_t itlb_multihit_show_state(char *buf)
2689 {
2690 if (!boot_cpu_has(X86_FEATURE_MSR_IA32_FEAT_CTL) ||
2691 !boot_cpu_has(X86_FEATURE_VMX))
2692 return sysfs_emit(buf, "KVM: Mitigation: VMX unsupported\n");
2693 else if (!(cr4_read_shadow() & X86_CR4_VMXE))
2694 return sysfs_emit(buf, "KVM: Mitigation: VMX disabled\n");
2695 else if (itlb_multihit_kvm_mitigation)
2696 return sysfs_emit(buf, "KVM: Mitigation: Split huge pages\n");
2697 else
2698 return sysfs_emit(buf, "KVM: Vulnerable\n");
2699 }
2700 #else
2701 static ssize_t l1tf_show_state(char *buf)
2702 {
2703 return sysfs_emit(buf, "%s\n", L1TF_DEFAULT_MSG);
2704 }
2705
2706 static ssize_t itlb_multihit_show_state(char *buf)
2707 {
2708 return sysfs_emit(buf, "Processor vulnerable\n");
2709 }
2710 #endif
2711
2712 static ssize_t mds_show_state(char *buf)
2713 {
2714 if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
2715 return sysfs_emit(buf, "%s; SMT Host state unknown\n",
2716 mds_strings[mds_mitigation]);
2717 }
2718
2719 if (boot_cpu_has(X86_BUG_MSBDS_ONLY)) {
2720 return sysfs_emit(buf, "%s; SMT %s\n", mds_strings[mds_mitigation],
2721 (mds_mitigation == MDS_MITIGATION_OFF ? "vulnerable" :
2722 sched_smt_active() ? "mitigated" : "disabled"));
2723 }
2724
2725 return sysfs_emit(buf, "%s; SMT %s\n", mds_strings[mds_mitigation],
2726 sched_smt_active() ? "vulnerable" : "disabled");
2727 }
2728
2729 static ssize_t tsx_async_abort_show_state(char *buf)
2730 {
2731 if ((taa_mitigation == TAA_MITIGATION_TSX_DISABLED) ||
2732 (taa_mitigation == TAA_MITIGATION_OFF))
2733 return sysfs_emit(buf, "%s\n", taa_strings[taa_mitigation]);
2734
2735 if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
2736 return sysfs_emit(buf, "%s; SMT Host state unknown\n",
2737 taa_strings[taa_mitigation]);
2738 }
2739
2740 return sysfs_emit(buf, "%s; SMT %s\n", taa_strings[taa_mitigation],
2741 sched_smt_active() ? "vulnerable" : "disabled");
2742 }
2743
2744 static ssize_t mmio_stale_data_show_state(char *buf)
2745 {
2746 if (boot_cpu_has_bug(X86_BUG_MMIO_UNKNOWN))
2747 return sysfs_emit(buf, "Unknown: No mitigations\n");
2748
2749 if (mmio_mitigation == MMIO_MITIGATION_OFF)
2750 return sysfs_emit(buf, "%s\n", mmio_strings[mmio_mitigation]);
2751
2752 if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
2753 return sysfs_emit(buf, "%s; SMT Host state unknown\n",
2754 mmio_strings[mmio_mitigation]);
2755 }
2756
2757 return sysfs_emit(buf, "%s; SMT %s\n", mmio_strings[mmio_mitigation],
2758 sched_smt_active() ? "vulnerable" : "disabled");
2759 }
2760
2761 static ssize_t rfds_show_state(char *buf)
2762 {
2763 return sysfs_emit(buf, "%s\n", rfds_strings[rfds_mitigation]);
2764 }
2765
2766 static char *stibp_state(void)
2767 {
2768 if (spectre_v2_in_eibrs_mode(spectre_v2_enabled) &&
2769 !boot_cpu_has(X86_FEATURE_AUTOIBRS))
2770 return "";
2771
2772 switch (spectre_v2_user_stibp) {
2773 case SPECTRE_V2_USER_NONE:
2774 return "; STIBP: disabled";
2775 case SPECTRE_V2_USER_STRICT:
2776 return "; STIBP: forced";
2777 case SPECTRE_V2_USER_STRICT_PREFERRED:
2778 return "; STIBP: always-on";
2779 case SPECTRE_V2_USER_PRCTL:
2780 case SPECTRE_V2_USER_SECCOMP:
2781 if (static_key_enabled(&switch_to_cond_stibp))
2782 return "; STIBP: conditional";
2783 }
2784 return "";
2785 }
2786
2787 static char *ibpb_state(void)
2788 {
2789 if (boot_cpu_has(X86_FEATURE_IBPB)) {
2790 if (static_key_enabled(&switch_mm_always_ibpb))
2791 return "; IBPB: always-on";
2792 if (static_key_enabled(&switch_mm_cond_ibpb))
2793 return "; IBPB: conditional";
2794 return "; IBPB: disabled";
2795 }
2796 return "";
2797 }
2798
2799 static char *pbrsb_eibrs_state(void)
2800 {
2801 if (boot_cpu_has_bug(X86_BUG_EIBRS_PBRSB)) {
2802 if (boot_cpu_has(X86_FEATURE_RSB_VMEXIT_LITE) ||
2803 boot_cpu_has(X86_FEATURE_RSB_VMEXIT))
2804 return "; PBRSB-eIBRS: SW sequence";
2805 else
2806 return "; PBRSB-eIBRS: Vulnerable";
2807 } else {
2808 return "; PBRSB-eIBRS: Not affected";
2809 }
2810 }
2811
2812 static const char *spectre_bhi_state(void)
2813 {
2814 if (!boot_cpu_has_bug(X86_BUG_BHI))
2815 return "; BHI: Not affected";
2816 else if (boot_cpu_has(X86_FEATURE_CLEAR_BHB_HW))
2817 return "; BHI: BHI_DIS_S";
2818 else if (boot_cpu_has(X86_FEATURE_CLEAR_BHB_LOOP))
2819 return "; BHI: SW loop, KVM: SW loop";
2820 else if (boot_cpu_has(X86_FEATURE_RETPOLINE) &&
2821 !(x86_read_arch_cap_msr() & ARCH_CAP_RRSBA))
2822 return "; BHI: Retpoline";
2823 else if (boot_cpu_has(X86_FEATURE_CLEAR_BHB_LOOP_ON_VMEXIT))
2824 return "; BHI: Syscall hardening, KVM: SW loop";
2825
2826 return "; BHI: Vulnerable (Syscall hardening enabled)";
2827 }
2828
2829 static ssize_t spectre_v2_show_state(char *buf)
2830 {
2831 if (spectre_v2_enabled == SPECTRE_V2_LFENCE)
2832 return sysfs_emit(buf, "Vulnerable: LFENCE\n");
2833
2834 if (spectre_v2_enabled == SPECTRE_V2_EIBRS && unprivileged_ebpf_enabled())
2835 return sysfs_emit(buf, "Vulnerable: eIBRS with unprivileged eBPF\n");
2836
2837 if (sched_smt_active() && unprivileged_ebpf_enabled() &&
2838 spectre_v2_enabled == SPECTRE_V2_EIBRS_LFENCE)
2839 return sysfs_emit(buf, "Vulnerable: eIBRS+LFENCE with unprivileged eBPF and SMT\n");
2840
2841 return sysfs_emit(buf, "%s%s%s%s%s%s%s%s\n",
2842 spectre_v2_strings[spectre_v2_enabled],
2843 ibpb_state(),
2844 boot_cpu_has(X86_FEATURE_USE_IBRS_FW) ? "; IBRS_FW" : "",
2845 stibp_state(),
2846 boot_cpu_has(X86_FEATURE_RSB_CTXSW) ? "; RSB filling" : "",
2847 pbrsb_eibrs_state(),
2848 spectre_bhi_state(),
2849 /* this should always be at the end */
2850 spectre_v2_module_string());
2851 }
2852
2853 static ssize_t srbds_show_state(char *buf)
2854 {
2855 return sysfs_emit(buf, "%s\n", srbds_strings[srbds_mitigation]);
2856 }
2857
2858 static ssize_t retbleed_show_state(char *buf)
2859 {
2860 if (retbleed_mitigation == RETBLEED_MITIGATION_UNRET ||
2861 retbleed_mitigation == RETBLEED_MITIGATION_IBPB) {
2862 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD &&
2863 boot_cpu_data.x86_vendor != X86_VENDOR_HYGON)
2864 return sysfs_emit(buf, "Vulnerable: untrained return thunk / IBPB on non-AMD based uarch\n");
2865
2866 return sysfs_emit(buf, "%s; SMT %s\n", retbleed_strings[retbleed_mitigation],
2867 !sched_smt_active() ? "disabled" :
2868 spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT ||
2869 spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED ?
2870 "enabled with STIBP protection" : "vulnerable");
2871 }
2872
2873 return sysfs_emit(buf, "%s\n", retbleed_strings[retbleed_mitigation]);
2874 }
2875
2876 static ssize_t srso_show_state(char *buf)
2877 {
2878 if (boot_cpu_has(X86_FEATURE_SRSO_NO))
2879 return sysfs_emit(buf, "Mitigation: SMT disabled\n");
2880
2881 return sysfs_emit(buf, "%s\n", srso_strings[srso_mitigation]);
2882 }
2883
2884 static ssize_t gds_show_state(char *buf)
2885 {
2886 return sysfs_emit(buf, "%s\n", gds_strings[gds_mitigation]);
2887 }
2888
2889 static ssize_t cpu_show_common(struct device *dev, struct device_attribute *attr,
2890 char *buf, unsigned int bug)
2891 {
2892 if (!boot_cpu_has_bug(bug))
2893 return sysfs_emit(buf, "Not affected\n");
2894
2895 switch (bug) {
2896 case X86_BUG_CPU_MELTDOWN:
2897 if (boot_cpu_has(X86_FEATURE_PTI))
2898 return sysfs_emit(buf, "Mitigation: PTI\n");
2899
2900 if (hypervisor_is_type(X86_HYPER_XEN_PV))
2901 return sysfs_emit(buf, "Unknown (XEN PV detected, hypervisor mitigation required)\n");
2902
2903 break;
2904
2905 case X86_BUG_SPECTRE_V1:
2906 return sysfs_emit(buf, "%s\n", spectre_v1_strings[spectre_v1_mitigation]);
2907
2908 case X86_BUG_SPECTRE_V2:
2909 return spectre_v2_show_state(buf);
2910
2911 case X86_BUG_SPEC_STORE_BYPASS:
2912 return sysfs_emit(buf, "%s\n", ssb_strings[ssb_mode]);
2913
2914 case X86_BUG_L1TF:
2915 if (boot_cpu_has(X86_FEATURE_L1TF_PTEINV))
2916 return l1tf_show_state(buf);
2917 break;
2918
2919 case X86_BUG_MDS:
2920 return mds_show_state(buf);
2921
2922 case X86_BUG_TAA:
2923 return tsx_async_abort_show_state(buf);
2924
2925 case X86_BUG_ITLB_MULTIHIT:
2926 return itlb_multihit_show_state(buf);
2927
2928 case X86_BUG_SRBDS:
2929 return srbds_show_state(buf);
2930
2931 case X86_BUG_MMIO_STALE_DATA:
2932 case X86_BUG_MMIO_UNKNOWN:
2933 return mmio_stale_data_show_state(buf);
2934
2935 case X86_BUG_RETBLEED:
2936 return retbleed_show_state(buf);
2937
2938 case X86_BUG_SRSO:
2939 return srso_show_state(buf);
2940
2941 case X86_BUG_GDS:
2942 return gds_show_state(buf);
2943
2944 case X86_BUG_RFDS:
2945 return rfds_show_state(buf);
2946
2947 default:
2948 break;
2949 }
2950
2951 return sysfs_emit(buf, "Vulnerable\n");
2952 }
2953
2954 ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr, char *buf)
2955 {
2956 return cpu_show_common(dev, attr, buf, X86_BUG_CPU_MELTDOWN);
2957 }
2958
2959 ssize_t cpu_show_spectre_v1(struct device *dev, struct device_attribute *attr, char *buf)
2960 {
2961 return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V1);
2962 }
2963
2964 ssize_t cpu_show_spectre_v2(struct device *dev, struct device_attribute *attr, char *buf)
2965 {
2966 return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V2);
2967 }
2968
2969 ssize_t cpu_show_spec_store_bypass(struct device *dev, struct device_attribute *attr, char *buf)
2970 {
2971 return cpu_show_common(dev, attr, buf, X86_BUG_SPEC_STORE_BYPASS);
2972 }
2973
2974 ssize_t cpu_show_l1tf(struct device *dev, struct device_attribute *attr, char *buf)
2975 {
2976 return cpu_show_common(dev, attr, buf, X86_BUG_L1TF);
2977 }
2978
2979 ssize_t cpu_show_mds(struct device *dev, struct device_attribute *attr, char *buf)
2980 {
2981 return cpu_show_common(dev, attr, buf, X86_BUG_MDS);
2982 }
2983
2984 ssize_t cpu_show_tsx_async_abort(struct device *dev, struct device_attribute *attr, char *buf)
2985 {
2986 return cpu_show_common(dev, attr, buf, X86_BUG_TAA);
2987 }
2988
2989 ssize_t cpu_show_itlb_multihit(struct device *dev, struct device_attribute *attr, char *buf)
2990 {
2991 return cpu_show_common(dev, attr, buf, X86_BUG_ITLB_MULTIHIT);
2992 }
2993
2994 ssize_t cpu_show_srbds(struct device *dev, struct device_attribute *attr, char *buf)
2995 {
2996 return cpu_show_common(dev, attr, buf, X86_BUG_SRBDS);
2997 }
2998
2999 ssize_t cpu_show_mmio_stale_data(struct device *dev, struct device_attribute *attr, char *buf)
3000 {
3001 if (boot_cpu_has_bug(X86_BUG_MMIO_UNKNOWN))
3002 return cpu_show_common(dev, attr, buf, X86_BUG_MMIO_UNKNOWN);
3003 else
3004 return cpu_show_common(dev, attr, buf, X86_BUG_MMIO_STALE_DATA);
3005 }
3006
3007 ssize_t cpu_show_retbleed(struct device *dev, struct device_attribute *attr, char *buf)
3008 {
3009 return cpu_show_common(dev, attr, buf, X86_BUG_RETBLEED);
3010 }
3011
3012 ssize_t cpu_show_spec_rstack_overflow(struct device *dev, struct device_attribute *attr, char *buf)
3013 {
3014 return cpu_show_common(dev, attr, buf, X86_BUG_SRSO);
3015 }
3016
3017 ssize_t cpu_show_gds(struct device *dev, struct device_attribute *attr, char *buf)
3018 {
3019 return cpu_show_common(dev, attr, buf, X86_BUG_GDS);
3020 }
3021
3022 ssize_t cpu_show_reg_file_data_sampling(struct device *dev, struct device_attribute *attr, char *buf)
3023 {
3024 return cpu_show_common(dev, attr, buf, X86_BUG_RFDS);
3025 }
3026 #endif
3027
3028 void __warn_thunk(void)
3029 {
3030 WARN_ONCE(1, "Unpatched return thunk in use. This should not happen!\n");
3031 }