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