blob: 78b9514a3844051916aec16ba69d8a8fe1937417 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 1994 Linus Torvalds
4 *
5 * Cyrix stuff, June 1998 by:
6 * - Rafael R. Reilova (moved everything from head.S),
7 * <rreilova@ececs.uc.edu>
8 * - Channing Corn (tests & fixes),
9 * - Andrew D. Balsa (code cleanup).
10 */
11#include <linux/init.h>
12#include <linux/utsname.h>
13#include <linux/cpu.h>
14#include <linux/module.h>
15#include <linux/nospec.h>
16#include <linux/prctl.h>
17#include <linux/sched/smt.h>
Olivier Deprez157378f2022-04-04 15:47:50 +020018#include <linux/pgtable.h>
19#include <linux/bpf.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000020
21#include <asm/spec-ctrl.h>
22#include <asm/cmdline.h>
23#include <asm/bugs.h>
24#include <asm/processor.h>
25#include <asm/processor-flags.h>
26#include <asm/fpu/internal.h>
27#include <asm/msr.h>
28#include <asm/vmx.h>
29#include <asm/paravirt.h>
30#include <asm/alternative.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000031#include <asm/set_memory.h>
32#include <asm/intel-family.h>
33#include <asm/e820/api.h>
34#include <asm/hypervisor.h>
Olivier Deprez157378f2022-04-04 15:47:50 +020035#include <asm/tlbflush.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000036
David Brazdil0f672f62019-12-10 10:32:29 +000037#include "cpu.h"
38
39static void __init spectre_v1_select_mitigation(void);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000040static void __init spectre_v2_select_mitigation(void);
41static void __init ssb_select_mitigation(void);
42static void __init l1tf_select_mitigation(void);
David Brazdil0f672f62019-12-10 10:32:29 +000043static void __init mds_select_mitigation(void);
44static void __init mds_print_mitigation(void);
45static void __init taa_select_mitigation(void);
Olivier Deprez0e641232021-09-23 10:07:05 +020046static void __init srbds_select_mitigation(void);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000047
48/* The base value of the SPEC_CTRL MSR that always has to be preserved. */
49u64 x86_spec_ctrl_base;
50EXPORT_SYMBOL_GPL(x86_spec_ctrl_base);
51static DEFINE_MUTEX(spec_ctrl_mutex);
52
53/*
54 * The vendor and possibly platform specific bits which can be modified in
55 * x86_spec_ctrl_base.
56 */
57static u64 __ro_after_init x86_spec_ctrl_mask = SPEC_CTRL_IBRS;
58
59/*
60 * AMD specific MSR info for Speculative Store Bypass control.
61 * x86_amd_ls_cfg_ssbd_mask is initialized in identify_boot_cpu().
62 */
63u64 __ro_after_init x86_amd_ls_cfg_base;
64u64 __ro_after_init x86_amd_ls_cfg_ssbd_mask;
65
David Brazdil0f672f62019-12-10 10:32:29 +000066/* Control conditional STIBP in switch_to() */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000067DEFINE_STATIC_KEY_FALSE(switch_to_cond_stibp);
68/* Control conditional IBPB in switch_mm() */
69DEFINE_STATIC_KEY_FALSE(switch_mm_cond_ibpb);
70/* Control unconditional IBPB in switch_mm() */
71DEFINE_STATIC_KEY_FALSE(switch_mm_always_ibpb);
72
David Brazdil0f672f62019-12-10 10:32:29 +000073/* Control MDS CPU buffer clear before returning to user space */
74DEFINE_STATIC_KEY_FALSE(mds_user_clear);
75EXPORT_SYMBOL_GPL(mds_user_clear);
76/* Control MDS CPU buffer clear before idling (halt, mwait) */
77DEFINE_STATIC_KEY_FALSE(mds_idle_clear);
78EXPORT_SYMBOL_GPL(mds_idle_clear);
79
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000080void __init check_bugs(void)
81{
82 identify_boot_cpu();
83
84 /*
85 * identify_boot_cpu() initialized SMT support information, let the
86 * core code know.
87 */
David Brazdil0f672f62019-12-10 10:32:29 +000088 cpu_smt_check_topology();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000089
90 if (!IS_ENABLED(CONFIG_SMP)) {
91 pr_info("CPU: ");
92 print_cpu_info(&boot_cpu_data);
93 }
94
95 /*
96 * Read the SPEC_CTRL MSR to account for reserved bits which may
97 * have unknown values. AMD64_LS_CFG MSR is cached in the early AMD
98 * init code as it is not enumerated and depends on the family.
99 */
100 if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
101 rdmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
102
103 /* Allow STIBP in MSR_SPEC_CTRL if supported */
104 if (boot_cpu_has(X86_FEATURE_STIBP))
105 x86_spec_ctrl_mask |= SPEC_CTRL_STIBP;
106
David Brazdil0f672f62019-12-10 10:32:29 +0000107 /* Select the proper CPU mitigations before patching alternatives: */
108 spectre_v1_select_mitigation();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000109 spectre_v2_select_mitigation();
David Brazdil0f672f62019-12-10 10:32:29 +0000110 ssb_select_mitigation();
111 l1tf_select_mitigation();
112 mds_select_mitigation();
113 taa_select_mitigation();
Olivier Deprez0e641232021-09-23 10:07:05 +0200114 srbds_select_mitigation();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000115
116 /*
David Brazdil0f672f62019-12-10 10:32:29 +0000117 * As MDS and TAA mitigations are inter-related, print MDS
118 * mitigation until after TAA mitigation selection is done.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000119 */
David Brazdil0f672f62019-12-10 10:32:29 +0000120 mds_print_mitigation();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000121
David Brazdil0f672f62019-12-10 10:32:29 +0000122 arch_smt_update();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000123
124#ifdef CONFIG_X86_32
125 /*
126 * Check whether we are able to run this kernel safely on SMP.
127 *
128 * - i386 is no longer supported.
129 * - In order to run on anything without a TSC, we need to be
130 * compiled for a i486.
131 */
132 if (boot_cpu_data.x86 < 4)
133 panic("Kernel requires i486+ for 'invlpg' and other features");
134
135 init_utsname()->machine[1] =
136 '0' + (boot_cpu_data.x86 > 6 ? 6 : boot_cpu_data.x86);
137 alternative_instructions();
138
139 fpu__init_check_bugs();
140#else /* CONFIG_X86_64 */
141 alternative_instructions();
142
143 /*
144 * Make sure the first 2MB area is not mapped by huge pages
145 * There are typically fixed size MTRRs in there and overlapping
146 * MTRRs into large pages causes slow downs.
147 *
148 * Right now we don't do that with gbpages because there seems
149 * very little benefit for that case.
150 */
151 if (!direct_gbpages)
152 set_memory_4k((unsigned long)__va(0), 1);
153#endif
154}
155
156void
157x86_virt_spec_ctrl(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl, bool setguest)
158{
159 u64 msrval, guestval, hostval = x86_spec_ctrl_base;
160 struct thread_info *ti = current_thread_info();
161
162 /* Is MSR_SPEC_CTRL implemented ? */
163 if (static_cpu_has(X86_FEATURE_MSR_SPEC_CTRL)) {
164 /*
165 * Restrict guest_spec_ctrl to supported values. Clear the
166 * modifiable bits in the host base value and or the
167 * modifiable bits from the guest value.
168 */
169 guestval = hostval & ~x86_spec_ctrl_mask;
170 guestval |= guest_spec_ctrl & x86_spec_ctrl_mask;
171
172 /* SSBD controlled in MSR_SPEC_CTRL */
173 if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) ||
174 static_cpu_has(X86_FEATURE_AMD_SSBD))
175 hostval |= ssbd_tif_to_spec_ctrl(ti->flags);
176
177 /* Conditional STIBP enabled? */
178 if (static_branch_unlikely(&switch_to_cond_stibp))
179 hostval |= stibp_tif_to_spec_ctrl(ti->flags);
180
181 if (hostval != guestval) {
182 msrval = setguest ? guestval : hostval;
183 wrmsrl(MSR_IA32_SPEC_CTRL, msrval);
184 }
185 }
186
187 /*
188 * If SSBD is not handled in MSR_SPEC_CTRL on AMD, update
189 * MSR_AMD64_L2_CFG or MSR_VIRT_SPEC_CTRL if supported.
190 */
191 if (!static_cpu_has(X86_FEATURE_LS_CFG_SSBD) &&
192 !static_cpu_has(X86_FEATURE_VIRT_SSBD))
193 return;
194
195 /*
196 * If the host has SSBD mitigation enabled, force it in the host's
197 * virtual MSR value. If its not permanently enabled, evaluate
198 * current's TIF_SSBD thread flag.
199 */
200 if (static_cpu_has(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE))
201 hostval = SPEC_CTRL_SSBD;
202 else
203 hostval = ssbd_tif_to_spec_ctrl(ti->flags);
204
205 /* Sanitize the guest value */
206 guestval = guest_virt_spec_ctrl & SPEC_CTRL_SSBD;
207
208 if (hostval != guestval) {
209 unsigned long tif;
210
211 tif = setguest ? ssbd_spec_ctrl_to_tif(guestval) :
212 ssbd_spec_ctrl_to_tif(hostval);
213
214 speculation_ctrl_update(tif);
215 }
216}
217EXPORT_SYMBOL_GPL(x86_virt_spec_ctrl);
218
219static void x86_amd_ssb_disable(void)
220{
221 u64 msrval = x86_amd_ls_cfg_base | x86_amd_ls_cfg_ssbd_mask;
222
223 if (boot_cpu_has(X86_FEATURE_VIRT_SSBD))
224 wrmsrl(MSR_AMD64_VIRT_SPEC_CTRL, SPEC_CTRL_SSBD);
225 else if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD))
226 wrmsrl(MSR_AMD64_LS_CFG, msrval);
227}
228
229#undef pr_fmt
David Brazdil0f672f62019-12-10 10:32:29 +0000230#define pr_fmt(fmt) "MDS: " fmt
231
232/* Default mitigation for MDS-affected CPUs */
233static enum mds_mitigations mds_mitigation __ro_after_init = MDS_MITIGATION_FULL;
234static bool mds_nosmt __ro_after_init = false;
235
236static const char * const mds_strings[] = {
237 [MDS_MITIGATION_OFF] = "Vulnerable",
238 [MDS_MITIGATION_FULL] = "Mitigation: Clear CPU buffers",
239 [MDS_MITIGATION_VMWERV] = "Vulnerable: Clear CPU buffers attempted, no microcode",
240};
241
242static void __init mds_select_mitigation(void)
243{
244 if (!boot_cpu_has_bug(X86_BUG_MDS) || cpu_mitigations_off()) {
245 mds_mitigation = MDS_MITIGATION_OFF;
246 return;
247 }
248
249 if (mds_mitigation == MDS_MITIGATION_FULL) {
250 if (!boot_cpu_has(X86_FEATURE_MD_CLEAR))
251 mds_mitigation = MDS_MITIGATION_VMWERV;
252
253 static_branch_enable(&mds_user_clear);
254
255 if (!boot_cpu_has(X86_BUG_MSBDS_ONLY) &&
256 (mds_nosmt || cpu_mitigations_auto_nosmt()))
257 cpu_smt_disable(false);
258 }
259}
260
261static void __init mds_print_mitigation(void)
262{
263 if (!boot_cpu_has_bug(X86_BUG_MDS) || cpu_mitigations_off())
264 return;
265
266 pr_info("%s\n", mds_strings[mds_mitigation]);
267}
268
269static int __init mds_cmdline(char *str)
270{
271 if (!boot_cpu_has_bug(X86_BUG_MDS))
272 return 0;
273
274 if (!str)
275 return -EINVAL;
276
277 if (!strcmp(str, "off"))
278 mds_mitigation = MDS_MITIGATION_OFF;
279 else if (!strcmp(str, "full"))
280 mds_mitigation = MDS_MITIGATION_FULL;
281 else if (!strcmp(str, "full,nosmt")) {
282 mds_mitigation = MDS_MITIGATION_FULL;
283 mds_nosmt = true;
284 }
285
286 return 0;
287}
288early_param("mds", mds_cmdline);
289
290#undef pr_fmt
291#define pr_fmt(fmt) "TAA: " fmt
292
Olivier Deprez157378f2022-04-04 15:47:50 +0200293enum taa_mitigations {
294 TAA_MITIGATION_OFF,
295 TAA_MITIGATION_UCODE_NEEDED,
296 TAA_MITIGATION_VERW,
297 TAA_MITIGATION_TSX_DISABLED,
298};
299
David Brazdil0f672f62019-12-10 10:32:29 +0000300/* Default mitigation for TAA-affected CPUs */
301static enum taa_mitigations taa_mitigation __ro_after_init = TAA_MITIGATION_VERW;
302static bool taa_nosmt __ro_after_init;
303
304static const char * const taa_strings[] = {
305 [TAA_MITIGATION_OFF] = "Vulnerable",
306 [TAA_MITIGATION_UCODE_NEEDED] = "Vulnerable: Clear CPU buffers attempted, no microcode",
307 [TAA_MITIGATION_VERW] = "Mitigation: Clear CPU buffers",
308 [TAA_MITIGATION_TSX_DISABLED] = "Mitigation: TSX disabled",
309};
310
311static void __init taa_select_mitigation(void)
312{
313 u64 ia32_cap;
314
315 if (!boot_cpu_has_bug(X86_BUG_TAA)) {
316 taa_mitigation = TAA_MITIGATION_OFF;
317 return;
318 }
319
320 /* TSX previously disabled by tsx=off */
321 if (!boot_cpu_has(X86_FEATURE_RTM)) {
322 taa_mitigation = TAA_MITIGATION_TSX_DISABLED;
323 goto out;
324 }
325
326 if (cpu_mitigations_off()) {
327 taa_mitigation = TAA_MITIGATION_OFF;
328 return;
329 }
330
331 /*
332 * TAA mitigation via VERW is turned off if both
333 * tsx_async_abort=off and mds=off are specified.
334 */
335 if (taa_mitigation == TAA_MITIGATION_OFF &&
336 mds_mitigation == MDS_MITIGATION_OFF)
337 goto out;
338
339 if (boot_cpu_has(X86_FEATURE_MD_CLEAR))
340 taa_mitigation = TAA_MITIGATION_VERW;
341 else
342 taa_mitigation = TAA_MITIGATION_UCODE_NEEDED;
343
344 /*
345 * VERW doesn't clear the CPU buffers when MD_CLEAR=1 and MDS_NO=1.
346 * A microcode update fixes this behavior to clear CPU buffers. It also
347 * adds support for MSR_IA32_TSX_CTRL which is enumerated by the
348 * ARCH_CAP_TSX_CTRL_MSR bit.
349 *
350 * On MDS_NO=1 CPUs if ARCH_CAP_TSX_CTRL_MSR is not set, microcode
351 * update is required.
352 */
353 ia32_cap = x86_read_arch_cap_msr();
354 if ( (ia32_cap & ARCH_CAP_MDS_NO) &&
355 !(ia32_cap & ARCH_CAP_TSX_CTRL_MSR))
356 taa_mitigation = TAA_MITIGATION_UCODE_NEEDED;
357
358 /*
359 * TSX is enabled, select alternate mitigation for TAA which is
360 * the same as MDS. Enable MDS static branch to clear CPU buffers.
361 *
362 * For guests that can't determine whether the correct microcode is
363 * present on host, enable the mitigation for UCODE_NEEDED as well.
364 */
365 static_branch_enable(&mds_user_clear);
366
367 if (taa_nosmt || cpu_mitigations_auto_nosmt())
368 cpu_smt_disable(false);
369
370 /*
371 * Update MDS mitigation, if necessary, as the mds_user_clear is
372 * now enabled for TAA mitigation.
373 */
374 if (mds_mitigation == MDS_MITIGATION_OFF &&
375 boot_cpu_has_bug(X86_BUG_MDS)) {
376 mds_mitigation = MDS_MITIGATION_FULL;
377 mds_select_mitigation();
378 }
379out:
380 pr_info("%s\n", taa_strings[taa_mitigation]);
381}
382
383static int __init tsx_async_abort_parse_cmdline(char *str)
384{
385 if (!boot_cpu_has_bug(X86_BUG_TAA))
386 return 0;
387
388 if (!str)
389 return -EINVAL;
390
391 if (!strcmp(str, "off")) {
392 taa_mitigation = TAA_MITIGATION_OFF;
393 } else if (!strcmp(str, "full")) {
394 taa_mitigation = TAA_MITIGATION_VERW;
395 } else if (!strcmp(str, "full,nosmt")) {
396 taa_mitigation = TAA_MITIGATION_VERW;
397 taa_nosmt = true;
398 }
399
400 return 0;
401}
402early_param("tsx_async_abort", tsx_async_abort_parse_cmdline);
403
404#undef pr_fmt
Olivier Deprez0e641232021-09-23 10:07:05 +0200405#define pr_fmt(fmt) "SRBDS: " fmt
406
407enum srbds_mitigations {
408 SRBDS_MITIGATION_OFF,
409 SRBDS_MITIGATION_UCODE_NEEDED,
410 SRBDS_MITIGATION_FULL,
411 SRBDS_MITIGATION_TSX_OFF,
412 SRBDS_MITIGATION_HYPERVISOR,
413};
414
415static enum srbds_mitigations srbds_mitigation __ro_after_init = SRBDS_MITIGATION_FULL;
416
417static const char * const srbds_strings[] = {
418 [SRBDS_MITIGATION_OFF] = "Vulnerable",
419 [SRBDS_MITIGATION_UCODE_NEEDED] = "Vulnerable: No microcode",
420 [SRBDS_MITIGATION_FULL] = "Mitigation: Microcode",
421 [SRBDS_MITIGATION_TSX_OFF] = "Mitigation: TSX disabled",
422 [SRBDS_MITIGATION_HYPERVISOR] = "Unknown: Dependent on hypervisor status",
423};
424
425static bool srbds_off;
426
427void update_srbds_msr(void)
428{
429 u64 mcu_ctrl;
430
431 if (!boot_cpu_has_bug(X86_BUG_SRBDS))
432 return;
433
434 if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
435 return;
436
437 if (srbds_mitigation == SRBDS_MITIGATION_UCODE_NEEDED)
438 return;
439
440 rdmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl);
441
442 switch (srbds_mitigation) {
443 case SRBDS_MITIGATION_OFF:
444 case SRBDS_MITIGATION_TSX_OFF:
445 mcu_ctrl |= RNGDS_MITG_DIS;
446 break;
447 case SRBDS_MITIGATION_FULL:
448 mcu_ctrl &= ~RNGDS_MITG_DIS;
449 break;
450 default:
451 break;
452 }
453
454 wrmsrl(MSR_IA32_MCU_OPT_CTRL, mcu_ctrl);
455}
456
457static void __init srbds_select_mitigation(void)
458{
459 u64 ia32_cap;
460
461 if (!boot_cpu_has_bug(X86_BUG_SRBDS))
462 return;
463
464 /*
465 * Check to see if this is one of the MDS_NO systems supporting
466 * TSX that are only exposed to SRBDS when TSX is enabled.
467 */
468 ia32_cap = x86_read_arch_cap_msr();
469 if ((ia32_cap & ARCH_CAP_MDS_NO) && !boot_cpu_has(X86_FEATURE_RTM))
470 srbds_mitigation = SRBDS_MITIGATION_TSX_OFF;
471 else if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
472 srbds_mitigation = SRBDS_MITIGATION_HYPERVISOR;
473 else if (!boot_cpu_has(X86_FEATURE_SRBDS_CTRL))
474 srbds_mitigation = SRBDS_MITIGATION_UCODE_NEEDED;
475 else if (cpu_mitigations_off() || srbds_off)
476 srbds_mitigation = SRBDS_MITIGATION_OFF;
477
478 update_srbds_msr();
479 pr_info("%s\n", srbds_strings[srbds_mitigation]);
480}
481
482static int __init srbds_parse_cmdline(char *str)
483{
484 if (!str)
485 return -EINVAL;
486
487 if (!boot_cpu_has_bug(X86_BUG_SRBDS))
488 return 0;
489
490 srbds_off = !strcmp(str, "off");
491 return 0;
492}
493early_param("srbds", srbds_parse_cmdline);
494
495#undef pr_fmt
David Brazdil0f672f62019-12-10 10:32:29 +0000496#define pr_fmt(fmt) "Spectre V1 : " fmt
497
498enum spectre_v1_mitigation {
499 SPECTRE_V1_MITIGATION_NONE,
500 SPECTRE_V1_MITIGATION_AUTO,
501};
502
503static enum spectre_v1_mitigation spectre_v1_mitigation __ro_after_init =
504 SPECTRE_V1_MITIGATION_AUTO;
505
506static const char * const spectre_v1_strings[] = {
507 [SPECTRE_V1_MITIGATION_NONE] = "Vulnerable: __user pointer sanitization and usercopy barriers only; no swapgs barriers",
508 [SPECTRE_V1_MITIGATION_AUTO] = "Mitigation: usercopy/swapgs barriers and __user pointer sanitization",
509};
510
511/*
512 * Does SMAP provide full mitigation against speculative kernel access to
513 * userspace?
514 */
515static bool smap_works_speculatively(void)
516{
517 if (!boot_cpu_has(X86_FEATURE_SMAP))
518 return false;
519
520 /*
521 * On CPUs which are vulnerable to Meltdown, SMAP does not
522 * prevent speculative access to user data in the L1 cache.
523 * Consider SMAP to be non-functional as a mitigation on these
524 * CPUs.
525 */
526 if (boot_cpu_has(X86_BUG_CPU_MELTDOWN))
527 return false;
528
529 return true;
530}
531
532static void __init spectre_v1_select_mitigation(void)
533{
534 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V1) || cpu_mitigations_off()) {
535 spectre_v1_mitigation = SPECTRE_V1_MITIGATION_NONE;
536 return;
537 }
538
539 if (spectre_v1_mitigation == SPECTRE_V1_MITIGATION_AUTO) {
540 /*
541 * With Spectre v1, a user can speculatively control either
542 * path of a conditional swapgs with a user-controlled GS
543 * value. The mitigation is to add lfences to both code paths.
544 *
545 * If FSGSBASE is enabled, the user can put a kernel address in
546 * GS, in which case SMAP provides no protection.
547 *
David Brazdil0f672f62019-12-10 10:32:29 +0000548 * If FSGSBASE is disabled, the user can only put a user space
549 * address in GS. That makes an attack harder, but still
550 * possible if there's no SMAP protection.
551 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200552 if (boot_cpu_has(X86_FEATURE_FSGSBASE) ||
553 !smap_works_speculatively()) {
David Brazdil0f672f62019-12-10 10:32:29 +0000554 /*
555 * Mitigation can be provided from SWAPGS itself or
556 * PTI as the CR3 write in the Meltdown mitigation
557 * is serializing.
558 *
559 * If neither is there, mitigate with an LFENCE to
560 * stop speculation through swapgs.
561 */
562 if (boot_cpu_has_bug(X86_BUG_SWAPGS) &&
563 !boot_cpu_has(X86_FEATURE_PTI))
564 setup_force_cpu_cap(X86_FEATURE_FENCE_SWAPGS_USER);
565
566 /*
567 * Enable lfences in the kernel entry (non-swapgs)
568 * paths, to prevent user entry from speculatively
569 * skipping swapgs.
570 */
571 setup_force_cpu_cap(X86_FEATURE_FENCE_SWAPGS_KERNEL);
572 }
573 }
574
575 pr_info("%s\n", spectre_v1_strings[spectre_v1_mitigation]);
576}
577
578static int __init nospectre_v1_cmdline(char *str)
579{
580 spectre_v1_mitigation = SPECTRE_V1_MITIGATION_NONE;
581 return 0;
582}
583early_param("nospectre_v1", nospectre_v1_cmdline);
584
585#undef pr_fmt
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000586#define pr_fmt(fmt) "Spectre V2 : " fmt
587
588static enum spectre_v2_mitigation spectre_v2_enabled __ro_after_init =
589 SPECTRE_V2_NONE;
590
Olivier Deprez0e641232021-09-23 10:07:05 +0200591static enum spectre_v2_user_mitigation spectre_v2_user_stibp __ro_after_init =
592 SPECTRE_V2_USER_NONE;
593static enum spectre_v2_user_mitigation spectre_v2_user_ibpb __ro_after_init =
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000594 SPECTRE_V2_USER_NONE;
595
David Brazdil0f672f62019-12-10 10:32:29 +0000596#ifdef CONFIG_RETPOLINE
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000597static bool spectre_v2_bad_module;
598
599bool retpoline_module_ok(bool has_retpoline)
600{
601 if (spectre_v2_enabled == SPECTRE_V2_NONE || has_retpoline)
602 return true;
603
604 pr_err("System may be vulnerable to spectre v2\n");
605 spectre_v2_bad_module = true;
606 return false;
607}
608
609static inline const char *spectre_v2_module_string(void)
610{
611 return spectre_v2_bad_module ? " - vulnerable module loaded" : "";
612}
613#else
614static inline const char *spectre_v2_module_string(void) { return ""; }
615#endif
616
Olivier Deprez157378f2022-04-04 15:47:50 +0200617#define SPECTRE_V2_LFENCE_MSG "WARNING: LFENCE mitigation is not recommended for this CPU, data leaks possible!\n"
618#define SPECTRE_V2_EIBRS_EBPF_MSG "WARNING: Unprivileged eBPF is enabled with eIBRS on, data leaks possible via Spectre v2 BHB attacks!\n"
619#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"
620
621#ifdef CONFIG_BPF_SYSCALL
622void unpriv_ebpf_notify(int new_state)
623{
624 if (new_state)
625 return;
626
627 /* Unprivileged eBPF is enabled */
628
629 switch (spectre_v2_enabled) {
630 case SPECTRE_V2_EIBRS:
631 pr_err(SPECTRE_V2_EIBRS_EBPF_MSG);
632 break;
633 case SPECTRE_V2_EIBRS_LFENCE:
634 if (sched_smt_active())
635 pr_err(SPECTRE_V2_EIBRS_LFENCE_EBPF_SMT_MSG);
636 break;
637 default:
638 break;
639 }
640}
641#endif
642
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000643static inline bool match_option(const char *arg, int arglen, const char *opt)
644{
645 int len = strlen(opt);
646
647 return len == arglen && !strncmp(arg, opt, len);
648}
649
650/* The kernel command line selection for spectre v2 */
651enum spectre_v2_mitigation_cmd {
652 SPECTRE_V2_CMD_NONE,
653 SPECTRE_V2_CMD_AUTO,
654 SPECTRE_V2_CMD_FORCE,
655 SPECTRE_V2_CMD_RETPOLINE,
656 SPECTRE_V2_CMD_RETPOLINE_GENERIC,
Olivier Deprez157378f2022-04-04 15:47:50 +0200657 SPECTRE_V2_CMD_RETPOLINE_LFENCE,
658 SPECTRE_V2_CMD_EIBRS,
659 SPECTRE_V2_CMD_EIBRS_RETPOLINE,
660 SPECTRE_V2_CMD_EIBRS_LFENCE,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000661};
662
663enum spectre_v2_user_cmd {
664 SPECTRE_V2_USER_CMD_NONE,
665 SPECTRE_V2_USER_CMD_AUTO,
666 SPECTRE_V2_USER_CMD_FORCE,
667 SPECTRE_V2_USER_CMD_PRCTL,
668 SPECTRE_V2_USER_CMD_PRCTL_IBPB,
669 SPECTRE_V2_USER_CMD_SECCOMP,
670 SPECTRE_V2_USER_CMD_SECCOMP_IBPB,
671};
672
673static const char * const spectre_v2_user_strings[] = {
David Brazdil0f672f62019-12-10 10:32:29 +0000674 [SPECTRE_V2_USER_NONE] = "User space: Vulnerable",
675 [SPECTRE_V2_USER_STRICT] = "User space: Mitigation: STIBP protection",
676 [SPECTRE_V2_USER_STRICT_PREFERRED] = "User space: Mitigation: STIBP always-on protection",
677 [SPECTRE_V2_USER_PRCTL] = "User space: Mitigation: STIBP via prctl",
678 [SPECTRE_V2_USER_SECCOMP] = "User space: Mitigation: STIBP via seccomp and prctl",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000679};
680
681static const struct {
682 const char *option;
683 enum spectre_v2_user_cmd cmd;
684 bool secure;
David Brazdil0f672f62019-12-10 10:32:29 +0000685} v2_user_options[] __initconst = {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000686 { "auto", SPECTRE_V2_USER_CMD_AUTO, false },
687 { "off", SPECTRE_V2_USER_CMD_NONE, false },
688 { "on", SPECTRE_V2_USER_CMD_FORCE, true },
689 { "prctl", SPECTRE_V2_USER_CMD_PRCTL, false },
690 { "prctl,ibpb", SPECTRE_V2_USER_CMD_PRCTL_IBPB, false },
691 { "seccomp", SPECTRE_V2_USER_CMD_SECCOMP, false },
692 { "seccomp,ibpb", SPECTRE_V2_USER_CMD_SECCOMP_IBPB, false },
693};
694
695static void __init spec_v2_user_print_cond(const char *reason, bool secure)
696{
697 if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2) != secure)
698 pr_info("spectre_v2_user=%s forced on command line.\n", reason);
699}
700
701static enum spectre_v2_user_cmd __init
702spectre_v2_parse_user_cmdline(enum spectre_v2_mitigation_cmd v2_cmd)
703{
704 char arg[20];
705 int ret, i;
706
707 switch (v2_cmd) {
708 case SPECTRE_V2_CMD_NONE:
709 return SPECTRE_V2_USER_CMD_NONE;
710 case SPECTRE_V2_CMD_FORCE:
711 return SPECTRE_V2_USER_CMD_FORCE;
712 default:
713 break;
714 }
715
716 ret = cmdline_find_option(boot_command_line, "spectre_v2_user",
717 arg, sizeof(arg));
718 if (ret < 0)
719 return SPECTRE_V2_USER_CMD_AUTO;
720
721 for (i = 0; i < ARRAY_SIZE(v2_user_options); i++) {
722 if (match_option(arg, ret, v2_user_options[i].option)) {
723 spec_v2_user_print_cond(v2_user_options[i].option,
724 v2_user_options[i].secure);
725 return v2_user_options[i].cmd;
726 }
727 }
728
729 pr_err("Unknown user space protection option (%s). Switching to AUTO select\n", arg);
730 return SPECTRE_V2_USER_CMD_AUTO;
731}
732
Olivier Deprez157378f2022-04-04 15:47:50 +0200733static inline bool spectre_v2_in_eibrs_mode(enum spectre_v2_mitigation mode)
734{
735 return (mode == SPECTRE_V2_EIBRS ||
736 mode == SPECTRE_V2_EIBRS_RETPOLINE ||
737 mode == SPECTRE_V2_EIBRS_LFENCE);
738}
739
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000740static void __init
741spectre_v2_user_select_mitigation(enum spectre_v2_mitigation_cmd v2_cmd)
742{
743 enum spectre_v2_user_mitigation mode = SPECTRE_V2_USER_NONE;
744 bool smt_possible = IS_ENABLED(CONFIG_SMP);
745 enum spectre_v2_user_cmd cmd;
746
747 if (!boot_cpu_has(X86_FEATURE_IBPB) && !boot_cpu_has(X86_FEATURE_STIBP))
748 return;
749
750 if (cpu_smt_control == CPU_SMT_FORCE_DISABLED ||
751 cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
752 smt_possible = false;
753
754 cmd = spectre_v2_parse_user_cmdline(v2_cmd);
755 switch (cmd) {
756 case SPECTRE_V2_USER_CMD_NONE:
757 goto set_mode;
758 case SPECTRE_V2_USER_CMD_FORCE:
759 mode = SPECTRE_V2_USER_STRICT;
760 break;
761 case SPECTRE_V2_USER_CMD_PRCTL:
762 case SPECTRE_V2_USER_CMD_PRCTL_IBPB:
763 mode = SPECTRE_V2_USER_PRCTL;
764 break;
765 case SPECTRE_V2_USER_CMD_AUTO:
766 case SPECTRE_V2_USER_CMD_SECCOMP:
767 case SPECTRE_V2_USER_CMD_SECCOMP_IBPB:
768 if (IS_ENABLED(CONFIG_SECCOMP))
769 mode = SPECTRE_V2_USER_SECCOMP;
770 else
771 mode = SPECTRE_V2_USER_PRCTL;
772 break;
773 }
774
775 /* Initialize Indirect Branch Prediction Barrier */
776 if (boot_cpu_has(X86_FEATURE_IBPB)) {
777 setup_force_cpu_cap(X86_FEATURE_USE_IBPB);
778
Olivier Deprez0e641232021-09-23 10:07:05 +0200779 spectre_v2_user_ibpb = mode;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000780 switch (cmd) {
781 case SPECTRE_V2_USER_CMD_FORCE:
782 case SPECTRE_V2_USER_CMD_PRCTL_IBPB:
783 case SPECTRE_V2_USER_CMD_SECCOMP_IBPB:
784 static_branch_enable(&switch_mm_always_ibpb);
Olivier Deprez0e641232021-09-23 10:07:05 +0200785 spectre_v2_user_ibpb = SPECTRE_V2_USER_STRICT;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000786 break;
787 case SPECTRE_V2_USER_CMD_PRCTL:
788 case SPECTRE_V2_USER_CMD_AUTO:
789 case SPECTRE_V2_USER_CMD_SECCOMP:
790 static_branch_enable(&switch_mm_cond_ibpb);
791 break;
792 default:
793 break;
794 }
795
796 pr_info("mitigation: Enabling %s Indirect Branch Prediction Barrier\n",
797 static_key_enabled(&switch_mm_always_ibpb) ?
798 "always-on" : "conditional");
799 }
800
Olivier Deprez0e641232021-09-23 10:07:05 +0200801 /*
Olivier Deprez157378f2022-04-04 15:47:50 +0200802 * If no STIBP, enhanced IBRS is enabled or SMT impossible, STIBP is not
Olivier Deprez0e641232021-09-23 10:07:05 +0200803 * required.
804 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200805 if (!boot_cpu_has(X86_FEATURE_STIBP) ||
806 !smt_possible ||
807 spectre_v2_in_eibrs_mode(spectre_v2_enabled))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000808 return;
809
810 /*
Olivier Deprez0e641232021-09-23 10:07:05 +0200811 * At this point, an STIBP mode other than "off" has been set.
812 * If STIBP support is not being forced, check if STIBP always-on
813 * is preferred.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000814 */
Olivier Deprez0e641232021-09-23 10:07:05 +0200815 if (mode != SPECTRE_V2_USER_STRICT &&
816 boot_cpu_has(X86_FEATURE_AMD_STIBP_ALWAYS_ON))
817 mode = SPECTRE_V2_USER_STRICT_PREFERRED;
818
Olivier Deprez0e641232021-09-23 10:07:05 +0200819 spectre_v2_user_stibp = mode;
820
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000821set_mode:
Olivier Deprez0e641232021-09-23 10:07:05 +0200822 pr_info("%s\n", spectre_v2_user_strings[mode]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000823}
824
825static const char * const spectre_v2_strings[] = {
826 [SPECTRE_V2_NONE] = "Vulnerable",
Olivier Deprez157378f2022-04-04 15:47:50 +0200827 [SPECTRE_V2_RETPOLINE] = "Mitigation: Retpolines",
828 [SPECTRE_V2_LFENCE] = "Mitigation: LFENCE",
829 [SPECTRE_V2_EIBRS] = "Mitigation: Enhanced IBRS",
830 [SPECTRE_V2_EIBRS_LFENCE] = "Mitigation: Enhanced IBRS + LFENCE",
831 [SPECTRE_V2_EIBRS_RETPOLINE] = "Mitigation: Enhanced IBRS + Retpolines",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000832};
833
834static const struct {
835 const char *option;
836 enum spectre_v2_mitigation_cmd cmd;
837 bool secure;
David Brazdil0f672f62019-12-10 10:32:29 +0000838} mitigation_options[] __initconst = {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000839 { "off", SPECTRE_V2_CMD_NONE, false },
840 { "on", SPECTRE_V2_CMD_FORCE, true },
841 { "retpoline", SPECTRE_V2_CMD_RETPOLINE, false },
Olivier Deprez157378f2022-04-04 15:47:50 +0200842 { "retpoline,amd", SPECTRE_V2_CMD_RETPOLINE_LFENCE, false },
843 { "retpoline,lfence", SPECTRE_V2_CMD_RETPOLINE_LFENCE, false },
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000844 { "retpoline,generic", SPECTRE_V2_CMD_RETPOLINE_GENERIC, false },
Olivier Deprez157378f2022-04-04 15:47:50 +0200845 { "eibrs", SPECTRE_V2_CMD_EIBRS, false },
846 { "eibrs,lfence", SPECTRE_V2_CMD_EIBRS_LFENCE, false },
847 { "eibrs,retpoline", SPECTRE_V2_CMD_EIBRS_RETPOLINE, false },
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000848 { "auto", SPECTRE_V2_CMD_AUTO, false },
849};
850
851static void __init spec_v2_print_cond(const char *reason, bool secure)
852{
853 if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2) != secure)
854 pr_info("%s selected on command line.\n", reason);
855}
856
857static enum spectre_v2_mitigation_cmd __init spectre_v2_parse_cmdline(void)
858{
859 enum spectre_v2_mitigation_cmd cmd = SPECTRE_V2_CMD_AUTO;
860 char arg[20];
861 int ret, i;
862
David Brazdil0f672f62019-12-10 10:32:29 +0000863 if (cmdline_find_option_bool(boot_command_line, "nospectre_v2") ||
864 cpu_mitigations_off())
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000865 return SPECTRE_V2_CMD_NONE;
866
867 ret = cmdline_find_option(boot_command_line, "spectre_v2", arg, sizeof(arg));
868 if (ret < 0)
869 return SPECTRE_V2_CMD_AUTO;
870
871 for (i = 0; i < ARRAY_SIZE(mitigation_options); i++) {
872 if (!match_option(arg, ret, mitigation_options[i].option))
873 continue;
874 cmd = mitigation_options[i].cmd;
875 break;
876 }
877
878 if (i >= ARRAY_SIZE(mitigation_options)) {
879 pr_err("unknown option (%s). Switching to AUTO select\n", arg);
880 return SPECTRE_V2_CMD_AUTO;
881 }
882
883 if ((cmd == SPECTRE_V2_CMD_RETPOLINE ||
Olivier Deprez157378f2022-04-04 15:47:50 +0200884 cmd == SPECTRE_V2_CMD_RETPOLINE_LFENCE ||
885 cmd == SPECTRE_V2_CMD_RETPOLINE_GENERIC ||
886 cmd == SPECTRE_V2_CMD_EIBRS_LFENCE ||
887 cmd == SPECTRE_V2_CMD_EIBRS_RETPOLINE) &&
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000888 !IS_ENABLED(CONFIG_RETPOLINE)) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200889 pr_err("%s selected but not compiled in. Switching to AUTO select\n",
890 mitigation_options[i].option);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000891 return SPECTRE_V2_CMD_AUTO;
892 }
893
Olivier Deprez157378f2022-04-04 15:47:50 +0200894 if ((cmd == SPECTRE_V2_CMD_EIBRS ||
895 cmd == SPECTRE_V2_CMD_EIBRS_LFENCE ||
896 cmd == SPECTRE_V2_CMD_EIBRS_RETPOLINE) &&
897 !boot_cpu_has(X86_FEATURE_IBRS_ENHANCED)) {
898 pr_err("%s selected but CPU doesn't have eIBRS. Switching to AUTO select\n",
899 mitigation_options[i].option);
900 return SPECTRE_V2_CMD_AUTO;
901 }
902
903 if ((cmd == SPECTRE_V2_CMD_RETPOLINE_LFENCE ||
904 cmd == SPECTRE_V2_CMD_EIBRS_LFENCE) &&
905 !boot_cpu_has(X86_FEATURE_LFENCE_RDTSC)) {
906 pr_err("%s selected, but CPU doesn't have a serializing LFENCE. Switching to AUTO select\n",
907 mitigation_options[i].option);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000908 return SPECTRE_V2_CMD_AUTO;
909 }
910
911 spec_v2_print_cond(mitigation_options[i].option,
912 mitigation_options[i].secure);
913 return cmd;
914}
915
Olivier Deprez157378f2022-04-04 15:47:50 +0200916static enum spectre_v2_mitigation __init spectre_v2_select_retpoline(void)
917{
918 if (!IS_ENABLED(CONFIG_RETPOLINE)) {
919 pr_err("Kernel not compiled with retpoline; no mitigation available!");
920 return SPECTRE_V2_NONE;
921 }
922
923 return SPECTRE_V2_RETPOLINE;
924}
925
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000926static void __init spectre_v2_select_mitigation(void)
927{
928 enum spectre_v2_mitigation_cmd cmd = spectre_v2_parse_cmdline();
929 enum spectre_v2_mitigation mode = SPECTRE_V2_NONE;
930
931 /*
932 * If the CPU is not affected and the command line mode is NONE or AUTO
933 * then nothing to do.
934 */
935 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2) &&
936 (cmd == SPECTRE_V2_CMD_NONE || cmd == SPECTRE_V2_CMD_AUTO))
937 return;
938
939 switch (cmd) {
940 case SPECTRE_V2_CMD_NONE:
941 return;
942
943 case SPECTRE_V2_CMD_FORCE:
944 case SPECTRE_V2_CMD_AUTO:
945 if (boot_cpu_has(X86_FEATURE_IBRS_ENHANCED)) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200946 mode = SPECTRE_V2_EIBRS;
947 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000948 }
Olivier Deprez157378f2022-04-04 15:47:50 +0200949
950 mode = spectre_v2_select_retpoline();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000951 break;
Olivier Deprez157378f2022-04-04 15:47:50 +0200952
953 case SPECTRE_V2_CMD_RETPOLINE_LFENCE:
954 pr_err(SPECTRE_V2_LFENCE_MSG);
955 mode = SPECTRE_V2_LFENCE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000956 break;
Olivier Deprez157378f2022-04-04 15:47:50 +0200957
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000958 case SPECTRE_V2_CMD_RETPOLINE_GENERIC:
Olivier Deprez157378f2022-04-04 15:47:50 +0200959 mode = SPECTRE_V2_RETPOLINE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000960 break;
Olivier Deprez157378f2022-04-04 15:47:50 +0200961
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000962 case SPECTRE_V2_CMD_RETPOLINE:
Olivier Deprez157378f2022-04-04 15:47:50 +0200963 mode = spectre_v2_select_retpoline();
964 break;
965
966 case SPECTRE_V2_CMD_EIBRS:
967 mode = SPECTRE_V2_EIBRS;
968 break;
969
970 case SPECTRE_V2_CMD_EIBRS_LFENCE:
971 mode = SPECTRE_V2_EIBRS_LFENCE;
972 break;
973
974 case SPECTRE_V2_CMD_EIBRS_RETPOLINE:
975 mode = SPECTRE_V2_EIBRS_RETPOLINE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000976 break;
977 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000978
Olivier Deprez157378f2022-04-04 15:47:50 +0200979 if (mode == SPECTRE_V2_EIBRS && unprivileged_ebpf_enabled())
980 pr_err(SPECTRE_V2_EIBRS_EBPF_MSG);
981
982 if (spectre_v2_in_eibrs_mode(mode)) {
983 /* Force it so VMEXIT will restore correctly */
984 x86_spec_ctrl_base |= SPEC_CTRL_IBRS;
985 wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000986 }
987
Olivier Deprez157378f2022-04-04 15:47:50 +0200988 switch (mode) {
989 case SPECTRE_V2_NONE:
990 case SPECTRE_V2_EIBRS:
991 break;
992
993 case SPECTRE_V2_LFENCE:
994 case SPECTRE_V2_EIBRS_LFENCE:
995 setup_force_cpu_cap(X86_FEATURE_RETPOLINE_LFENCE);
996 fallthrough;
997
998 case SPECTRE_V2_RETPOLINE:
999 case SPECTRE_V2_EIBRS_RETPOLINE:
1000 setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
1001 break;
1002 }
1003
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001004 spectre_v2_enabled = mode;
1005 pr_info("%s\n", spectre_v2_strings[mode]);
1006
1007 /*
1008 * If spectre v2 protection has been enabled, unconditionally fill
1009 * RSB during a context switch; this protects against two independent
1010 * issues:
1011 *
1012 * - RSB underflow (and switch to BTB) on Skylake+
1013 * - SpectreRSB variant of spectre v2 on X86_BUG_SPECTRE_V2 CPUs
1014 */
1015 setup_force_cpu_cap(X86_FEATURE_RSB_CTXSW);
1016 pr_info("Spectre v2 / SpectreRSB mitigation: Filling RSB on context switch\n");
1017
1018 /*
1019 * Retpoline means the kernel is safe because it has no indirect
1020 * branches. Enhanced IBRS protects firmware too, so, enable restricted
1021 * speculation around firmware calls only when Enhanced IBRS isn't
1022 * supported.
1023 *
1024 * Use "mode" to check Enhanced IBRS instead of boot_cpu_has(), because
1025 * the user might select retpoline on the kernel command line and if
1026 * the CPU supports Enhanced IBRS, kernel might un-intentionally not
1027 * enable IBRS around firmware calls.
1028 */
Olivier Deprez157378f2022-04-04 15:47:50 +02001029 if (boot_cpu_has(X86_FEATURE_IBRS) && !spectre_v2_in_eibrs_mode(mode)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001030 setup_force_cpu_cap(X86_FEATURE_USE_IBRS_FW);
1031 pr_info("Enabling Restricted Speculation for firmware calls\n");
1032 }
1033
1034 /* Set up IBPB and STIBP depending on the general spectre V2 command */
1035 spectre_v2_user_select_mitigation(cmd);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001036}
1037
1038static void update_stibp_msr(void * __unused)
1039{
1040 wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
1041}
1042
1043/* Update x86_spec_ctrl_base in case SMT state changed. */
1044static void update_stibp_strict(void)
1045{
1046 u64 mask = x86_spec_ctrl_base & ~SPEC_CTRL_STIBP;
1047
1048 if (sched_smt_active())
1049 mask |= SPEC_CTRL_STIBP;
1050
1051 if (mask == x86_spec_ctrl_base)
1052 return;
1053
1054 pr_info("Update user space SMT mitigation: STIBP %s\n",
1055 mask & SPEC_CTRL_STIBP ? "always-on" : "off");
1056 x86_spec_ctrl_base = mask;
1057 on_each_cpu(update_stibp_msr, NULL, 1);
1058}
1059
1060/* Update the static key controlling the evaluation of TIF_SPEC_IB */
1061static void update_indir_branch_cond(void)
1062{
1063 if (sched_smt_active())
1064 static_branch_enable(&switch_to_cond_stibp);
1065 else
1066 static_branch_disable(&switch_to_cond_stibp);
1067}
1068
David Brazdil0f672f62019-12-10 10:32:29 +00001069#undef pr_fmt
1070#define pr_fmt(fmt) fmt
1071
1072/* Update the static key controlling the MDS CPU buffer clear in idle */
1073static void update_mds_branch_idle(void)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001074{
David Brazdil0f672f62019-12-10 10:32:29 +00001075 /*
1076 * Enable the idle clearing if SMT is active on CPUs which are
1077 * affected only by MSBDS and not any other MDS variant.
1078 *
1079 * The other variants cannot be mitigated when SMT is enabled, so
1080 * clearing the buffers on idle just to prevent the Store Buffer
1081 * repartitioning leak would be a window dressing exercise.
1082 */
1083 if (!boot_cpu_has_bug(X86_BUG_MSBDS_ONLY))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001084 return;
1085
David Brazdil0f672f62019-12-10 10:32:29 +00001086 if (sched_smt_active())
1087 static_branch_enable(&mds_idle_clear);
1088 else
1089 static_branch_disable(&mds_idle_clear);
1090}
1091
1092#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"
1093#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"
1094
1095void cpu_bugs_smt_update(void)
1096{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001097 mutex_lock(&spec_ctrl_mutex);
1098
Olivier Deprez157378f2022-04-04 15:47:50 +02001099 if (sched_smt_active() && unprivileged_ebpf_enabled() &&
1100 spectre_v2_enabled == SPECTRE_V2_EIBRS_LFENCE)
1101 pr_warn_once(SPECTRE_V2_EIBRS_LFENCE_EBPF_SMT_MSG);
1102
Olivier Deprez0e641232021-09-23 10:07:05 +02001103 switch (spectre_v2_user_stibp) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001104 case SPECTRE_V2_USER_NONE:
1105 break;
1106 case SPECTRE_V2_USER_STRICT:
David Brazdil0f672f62019-12-10 10:32:29 +00001107 case SPECTRE_V2_USER_STRICT_PREFERRED:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001108 update_stibp_strict();
1109 break;
1110 case SPECTRE_V2_USER_PRCTL:
1111 case SPECTRE_V2_USER_SECCOMP:
1112 update_indir_branch_cond();
1113 break;
1114 }
1115
David Brazdil0f672f62019-12-10 10:32:29 +00001116 switch (mds_mitigation) {
1117 case MDS_MITIGATION_FULL:
1118 case MDS_MITIGATION_VMWERV:
1119 if (sched_smt_active() && !boot_cpu_has(X86_BUG_MSBDS_ONLY))
1120 pr_warn_once(MDS_MSG_SMT);
1121 update_mds_branch_idle();
1122 break;
1123 case MDS_MITIGATION_OFF:
1124 break;
1125 }
1126
1127 switch (taa_mitigation) {
1128 case TAA_MITIGATION_VERW:
1129 case TAA_MITIGATION_UCODE_NEEDED:
1130 if (sched_smt_active())
1131 pr_warn_once(TAA_MSG_SMT);
1132 break;
1133 case TAA_MITIGATION_TSX_DISABLED:
1134 case TAA_MITIGATION_OFF:
1135 break;
1136 }
1137
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001138 mutex_unlock(&spec_ctrl_mutex);
1139}
1140
1141#undef pr_fmt
1142#define pr_fmt(fmt) "Speculative Store Bypass: " fmt
1143
1144static enum ssb_mitigation ssb_mode __ro_after_init = SPEC_STORE_BYPASS_NONE;
1145
1146/* The kernel command line selection */
1147enum ssb_mitigation_cmd {
1148 SPEC_STORE_BYPASS_CMD_NONE,
1149 SPEC_STORE_BYPASS_CMD_AUTO,
1150 SPEC_STORE_BYPASS_CMD_ON,
1151 SPEC_STORE_BYPASS_CMD_PRCTL,
1152 SPEC_STORE_BYPASS_CMD_SECCOMP,
1153};
1154
1155static const char * const ssb_strings[] = {
1156 [SPEC_STORE_BYPASS_NONE] = "Vulnerable",
1157 [SPEC_STORE_BYPASS_DISABLE] = "Mitigation: Speculative Store Bypass disabled",
1158 [SPEC_STORE_BYPASS_PRCTL] = "Mitigation: Speculative Store Bypass disabled via prctl",
1159 [SPEC_STORE_BYPASS_SECCOMP] = "Mitigation: Speculative Store Bypass disabled via prctl and seccomp",
1160};
1161
1162static const struct {
1163 const char *option;
1164 enum ssb_mitigation_cmd cmd;
David Brazdil0f672f62019-12-10 10:32:29 +00001165} ssb_mitigation_options[] __initconst = {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001166 { "auto", SPEC_STORE_BYPASS_CMD_AUTO }, /* Platform decides */
1167 { "on", SPEC_STORE_BYPASS_CMD_ON }, /* Disable Speculative Store Bypass */
1168 { "off", SPEC_STORE_BYPASS_CMD_NONE }, /* Don't touch Speculative Store Bypass */
1169 { "prctl", SPEC_STORE_BYPASS_CMD_PRCTL }, /* Disable Speculative Store Bypass via prctl */
1170 { "seccomp", SPEC_STORE_BYPASS_CMD_SECCOMP }, /* Disable Speculative Store Bypass via prctl and seccomp */
1171};
1172
1173static enum ssb_mitigation_cmd __init ssb_parse_cmdline(void)
1174{
1175 enum ssb_mitigation_cmd cmd = SPEC_STORE_BYPASS_CMD_AUTO;
1176 char arg[20];
1177 int ret, i;
1178
David Brazdil0f672f62019-12-10 10:32:29 +00001179 if (cmdline_find_option_bool(boot_command_line, "nospec_store_bypass_disable") ||
1180 cpu_mitigations_off()) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001181 return SPEC_STORE_BYPASS_CMD_NONE;
1182 } else {
1183 ret = cmdline_find_option(boot_command_line, "spec_store_bypass_disable",
1184 arg, sizeof(arg));
1185 if (ret < 0)
1186 return SPEC_STORE_BYPASS_CMD_AUTO;
1187
1188 for (i = 0; i < ARRAY_SIZE(ssb_mitigation_options); i++) {
1189 if (!match_option(arg, ret, ssb_mitigation_options[i].option))
1190 continue;
1191
1192 cmd = ssb_mitigation_options[i].cmd;
1193 break;
1194 }
1195
1196 if (i >= ARRAY_SIZE(ssb_mitigation_options)) {
1197 pr_err("unknown option (%s). Switching to AUTO select\n", arg);
1198 return SPEC_STORE_BYPASS_CMD_AUTO;
1199 }
1200 }
1201
1202 return cmd;
1203}
1204
1205static enum ssb_mitigation __init __ssb_select_mitigation(void)
1206{
1207 enum ssb_mitigation mode = SPEC_STORE_BYPASS_NONE;
1208 enum ssb_mitigation_cmd cmd;
1209
1210 if (!boot_cpu_has(X86_FEATURE_SSBD))
1211 return mode;
1212
1213 cmd = ssb_parse_cmdline();
1214 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS) &&
1215 (cmd == SPEC_STORE_BYPASS_CMD_NONE ||
1216 cmd == SPEC_STORE_BYPASS_CMD_AUTO))
1217 return mode;
1218
1219 switch (cmd) {
1220 case SPEC_STORE_BYPASS_CMD_AUTO:
1221 case SPEC_STORE_BYPASS_CMD_SECCOMP:
1222 /*
1223 * Choose prctl+seccomp as the default mode if seccomp is
1224 * enabled.
1225 */
1226 if (IS_ENABLED(CONFIG_SECCOMP))
1227 mode = SPEC_STORE_BYPASS_SECCOMP;
1228 else
1229 mode = SPEC_STORE_BYPASS_PRCTL;
1230 break;
1231 case SPEC_STORE_BYPASS_CMD_ON:
1232 mode = SPEC_STORE_BYPASS_DISABLE;
1233 break;
1234 case SPEC_STORE_BYPASS_CMD_PRCTL:
1235 mode = SPEC_STORE_BYPASS_PRCTL;
1236 break;
1237 case SPEC_STORE_BYPASS_CMD_NONE:
1238 break;
1239 }
1240
1241 /*
David Brazdil0f672f62019-12-10 10:32:29 +00001242 * If SSBD is controlled by the SPEC_CTRL MSR, then set the proper
1243 * bit in the mask to allow guests to use the mitigation even in the
1244 * case where the host does not enable it.
1245 */
1246 if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) ||
1247 static_cpu_has(X86_FEATURE_AMD_SSBD)) {
1248 x86_spec_ctrl_mask |= SPEC_CTRL_SSBD;
1249 }
1250
1251 /*
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001252 * We have three CPU feature flags that are in play here:
1253 * - X86_BUG_SPEC_STORE_BYPASS - CPU is susceptible.
1254 * - X86_FEATURE_SSBD - CPU is able to turn off speculative store bypass
1255 * - X86_FEATURE_SPEC_STORE_BYPASS_DISABLE - engage the mitigation
1256 */
1257 if (mode == SPEC_STORE_BYPASS_DISABLE) {
1258 setup_force_cpu_cap(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE);
1259 /*
1260 * Intel uses the SPEC CTRL MSR Bit(2) for this, while AMD may
1261 * use a completely different MSR and bit dependent on family.
1262 */
1263 if (!static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) &&
1264 !static_cpu_has(X86_FEATURE_AMD_SSBD)) {
1265 x86_amd_ssb_disable();
1266 } else {
1267 x86_spec_ctrl_base |= SPEC_CTRL_SSBD;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001268 wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
1269 }
1270 }
1271
1272 return mode;
1273}
1274
1275static void ssb_select_mitigation(void)
1276{
1277 ssb_mode = __ssb_select_mitigation();
1278
1279 if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
1280 pr_info("%s\n", ssb_strings[ssb_mode]);
1281}
1282
1283#undef pr_fmt
1284#define pr_fmt(fmt) "Speculation prctl: " fmt
1285
1286static void task_update_spec_tif(struct task_struct *tsk)
1287{
1288 /* Force the update of the real TIF bits */
1289 set_tsk_thread_flag(tsk, TIF_SPEC_FORCE_UPDATE);
1290
1291 /*
1292 * Immediately update the speculation control MSRs for the current
1293 * task, but for a non-current task delay setting the CPU
1294 * mitigation until it is scheduled next.
1295 *
1296 * This can only happen for SECCOMP mitigation. For PRCTL it's
1297 * always the current task.
1298 */
1299 if (tsk == current)
1300 speculation_ctrl_update_current();
1301}
1302
1303static int ssb_prctl_set(struct task_struct *task, unsigned long ctrl)
1304{
1305 if (ssb_mode != SPEC_STORE_BYPASS_PRCTL &&
1306 ssb_mode != SPEC_STORE_BYPASS_SECCOMP)
1307 return -ENXIO;
1308
1309 switch (ctrl) {
1310 case PR_SPEC_ENABLE:
1311 /* If speculation is force disabled, enable is not allowed */
1312 if (task_spec_ssb_force_disable(task))
1313 return -EPERM;
1314 task_clear_spec_ssb_disable(task);
David Brazdil0f672f62019-12-10 10:32:29 +00001315 task_clear_spec_ssb_noexec(task);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001316 task_update_spec_tif(task);
1317 break;
1318 case PR_SPEC_DISABLE:
1319 task_set_spec_ssb_disable(task);
David Brazdil0f672f62019-12-10 10:32:29 +00001320 task_clear_spec_ssb_noexec(task);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001321 task_update_spec_tif(task);
1322 break;
1323 case PR_SPEC_FORCE_DISABLE:
1324 task_set_spec_ssb_disable(task);
1325 task_set_spec_ssb_force_disable(task);
David Brazdil0f672f62019-12-10 10:32:29 +00001326 task_clear_spec_ssb_noexec(task);
1327 task_update_spec_tif(task);
1328 break;
1329 case PR_SPEC_DISABLE_NOEXEC:
1330 if (task_spec_ssb_force_disable(task))
1331 return -EPERM;
1332 task_set_spec_ssb_disable(task);
1333 task_set_spec_ssb_noexec(task);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001334 task_update_spec_tif(task);
1335 break;
1336 default:
1337 return -ERANGE;
1338 }
1339 return 0;
1340}
1341
Olivier Deprez0e641232021-09-23 10:07:05 +02001342static bool is_spec_ib_user_controlled(void)
1343{
1344 return spectre_v2_user_ibpb == SPECTRE_V2_USER_PRCTL ||
1345 spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP ||
1346 spectre_v2_user_stibp == SPECTRE_V2_USER_PRCTL ||
1347 spectre_v2_user_stibp == SPECTRE_V2_USER_SECCOMP;
1348}
1349
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001350static int ib_prctl_set(struct task_struct *task, unsigned long ctrl)
1351{
1352 switch (ctrl) {
1353 case PR_SPEC_ENABLE:
Olivier Deprez0e641232021-09-23 10:07:05 +02001354 if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
1355 spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001356 return 0;
Olivier Deprez0e641232021-09-23 10:07:05 +02001357
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001358 /*
Olivier Deprez0e641232021-09-23 10:07:05 +02001359 * With strict mode for both IBPB and STIBP, the instruction
1360 * code paths avoid checking this task flag and instead,
1361 * unconditionally run the instruction. However, STIBP and IBPB
1362 * are independent and either can be set to conditionally
1363 * enabled regardless of the mode of the other.
1364 *
1365 * If either is set to conditional, allow the task flag to be
1366 * updated, unless it was force-disabled by a previous prctl
1367 * call. Currently, this is possible on an AMD CPU which has the
1368 * feature X86_FEATURE_AMD_STIBP_ALWAYS_ON. In this case, if the
1369 * kernel is booted with 'spectre_v2_user=seccomp', then
1370 * spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP and
1371 * spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001372 */
Olivier Deprez0e641232021-09-23 10:07:05 +02001373 if (!is_spec_ib_user_controlled() ||
1374 task_spec_ib_force_disable(task))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001375 return -EPERM;
Olivier Deprez0e641232021-09-23 10:07:05 +02001376
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001377 task_clear_spec_ib_disable(task);
1378 task_update_spec_tif(task);
1379 break;
1380 case PR_SPEC_DISABLE:
1381 case PR_SPEC_FORCE_DISABLE:
1382 /*
1383 * Indirect branch speculation is always allowed when
1384 * mitigation is force disabled.
1385 */
Olivier Deprez0e641232021-09-23 10:07:05 +02001386 if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
1387 spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001388 return -EPERM;
Olivier Deprez0e641232021-09-23 10:07:05 +02001389
1390 if (!is_spec_ib_user_controlled())
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001391 return 0;
Olivier Deprez0e641232021-09-23 10:07:05 +02001392
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001393 task_set_spec_ib_disable(task);
1394 if (ctrl == PR_SPEC_FORCE_DISABLE)
1395 task_set_spec_ib_force_disable(task);
1396 task_update_spec_tif(task);
1397 break;
1398 default:
1399 return -ERANGE;
1400 }
1401 return 0;
1402}
1403
1404int arch_prctl_spec_ctrl_set(struct task_struct *task, unsigned long which,
1405 unsigned long ctrl)
1406{
1407 switch (which) {
1408 case PR_SPEC_STORE_BYPASS:
1409 return ssb_prctl_set(task, ctrl);
1410 case PR_SPEC_INDIRECT_BRANCH:
1411 return ib_prctl_set(task, ctrl);
1412 default:
1413 return -ENODEV;
1414 }
1415}
1416
1417#ifdef CONFIG_SECCOMP
1418void arch_seccomp_spec_mitigate(struct task_struct *task)
1419{
1420 if (ssb_mode == SPEC_STORE_BYPASS_SECCOMP)
1421 ssb_prctl_set(task, PR_SPEC_FORCE_DISABLE);
Olivier Deprez0e641232021-09-23 10:07:05 +02001422 if (spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP ||
1423 spectre_v2_user_stibp == SPECTRE_V2_USER_SECCOMP)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001424 ib_prctl_set(task, PR_SPEC_FORCE_DISABLE);
1425}
1426#endif
1427
1428static int ssb_prctl_get(struct task_struct *task)
1429{
1430 switch (ssb_mode) {
1431 case SPEC_STORE_BYPASS_DISABLE:
1432 return PR_SPEC_DISABLE;
1433 case SPEC_STORE_BYPASS_SECCOMP:
1434 case SPEC_STORE_BYPASS_PRCTL:
1435 if (task_spec_ssb_force_disable(task))
1436 return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE;
David Brazdil0f672f62019-12-10 10:32:29 +00001437 if (task_spec_ssb_noexec(task))
1438 return PR_SPEC_PRCTL | PR_SPEC_DISABLE_NOEXEC;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001439 if (task_spec_ssb_disable(task))
1440 return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
1441 return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
1442 default:
1443 if (boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
1444 return PR_SPEC_ENABLE;
1445 return PR_SPEC_NOT_AFFECTED;
1446 }
1447}
1448
1449static int ib_prctl_get(struct task_struct *task)
1450{
1451 if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
1452 return PR_SPEC_NOT_AFFECTED;
1453
Olivier Deprez0e641232021-09-23 10:07:05 +02001454 if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
1455 spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001456 return PR_SPEC_ENABLE;
Olivier Deprez0e641232021-09-23 10:07:05 +02001457 else if (is_spec_ib_user_controlled()) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001458 if (task_spec_ib_force_disable(task))
1459 return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE;
1460 if (task_spec_ib_disable(task))
1461 return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
1462 return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
Olivier Deprez0e641232021-09-23 10:07:05 +02001463 } else if (spectre_v2_user_ibpb == SPECTRE_V2_USER_STRICT ||
1464 spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT ||
1465 spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001466 return PR_SPEC_DISABLE;
Olivier Deprez0e641232021-09-23 10:07:05 +02001467 else
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001468 return PR_SPEC_NOT_AFFECTED;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001469}
1470
1471int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which)
1472{
1473 switch (which) {
1474 case PR_SPEC_STORE_BYPASS:
1475 return ssb_prctl_get(task);
1476 case PR_SPEC_INDIRECT_BRANCH:
1477 return ib_prctl_get(task);
1478 default:
1479 return -ENODEV;
1480 }
1481}
1482
1483void x86_spec_ctrl_setup_ap(void)
1484{
1485 if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
1486 wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
1487
1488 if (ssb_mode == SPEC_STORE_BYPASS_DISABLE)
1489 x86_amd_ssb_disable();
1490}
1491
David Brazdil0f672f62019-12-10 10:32:29 +00001492bool itlb_multihit_kvm_mitigation;
1493EXPORT_SYMBOL_GPL(itlb_multihit_kvm_mitigation);
1494
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001495#undef pr_fmt
1496#define pr_fmt(fmt) "L1TF: " fmt
1497
1498/* Default mitigation for L1TF-affected CPUs */
1499enum l1tf_mitigations l1tf_mitigation __ro_after_init = L1TF_MITIGATION_FLUSH;
1500#if IS_ENABLED(CONFIG_KVM_INTEL)
1501EXPORT_SYMBOL_GPL(l1tf_mitigation);
1502#endif
1503enum vmx_l1d_flush_state l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
1504EXPORT_SYMBOL_GPL(l1tf_vmx_mitigation);
1505
1506/*
1507 * These CPUs all support 44bits physical address space internally in the
1508 * cache but CPUID can report a smaller number of physical address bits.
1509 *
1510 * The L1TF mitigation uses the top most address bit for the inversion of
1511 * non present PTEs. When the installed memory reaches into the top most
1512 * address bit due to memory holes, which has been observed on machines
1513 * which report 36bits physical address bits and have 32G RAM installed,
1514 * then the mitigation range check in l1tf_select_mitigation() triggers.
1515 * This is a false positive because the mitigation is still possible due to
1516 * the fact that the cache uses 44bit internally. Use the cache bits
1517 * instead of the reported physical bits and adjust them on the affected
1518 * machines to 44bit if the reported bits are less than 44.
1519 */
1520static void override_cache_bits(struct cpuinfo_x86 *c)
1521{
1522 if (c->x86 != 6)
1523 return;
1524
1525 switch (c->x86_model) {
1526 case INTEL_FAM6_NEHALEM:
1527 case INTEL_FAM6_WESTMERE:
1528 case INTEL_FAM6_SANDYBRIDGE:
1529 case INTEL_FAM6_IVYBRIDGE:
David Brazdil0f672f62019-12-10 10:32:29 +00001530 case INTEL_FAM6_HASWELL:
1531 case INTEL_FAM6_HASWELL_L:
1532 case INTEL_FAM6_HASWELL_G:
1533 case INTEL_FAM6_BROADWELL:
1534 case INTEL_FAM6_BROADWELL_G:
1535 case INTEL_FAM6_SKYLAKE_L:
1536 case INTEL_FAM6_SKYLAKE:
1537 case INTEL_FAM6_KABYLAKE_L:
1538 case INTEL_FAM6_KABYLAKE:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001539 if (c->x86_cache_bits < 44)
1540 c->x86_cache_bits = 44;
1541 break;
1542 }
1543}
1544
1545static void __init l1tf_select_mitigation(void)
1546{
1547 u64 half_pa;
1548
1549 if (!boot_cpu_has_bug(X86_BUG_L1TF))
1550 return;
1551
David Brazdil0f672f62019-12-10 10:32:29 +00001552 if (cpu_mitigations_off())
1553 l1tf_mitigation = L1TF_MITIGATION_OFF;
1554 else if (cpu_mitigations_auto_nosmt())
1555 l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOSMT;
1556
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001557 override_cache_bits(&boot_cpu_data);
1558
1559 switch (l1tf_mitigation) {
1560 case L1TF_MITIGATION_OFF:
1561 case L1TF_MITIGATION_FLUSH_NOWARN:
1562 case L1TF_MITIGATION_FLUSH:
1563 break;
1564 case L1TF_MITIGATION_FLUSH_NOSMT:
1565 case L1TF_MITIGATION_FULL:
1566 cpu_smt_disable(false);
1567 break;
1568 case L1TF_MITIGATION_FULL_FORCE:
1569 cpu_smt_disable(true);
1570 break;
1571 }
1572
1573#if CONFIG_PGTABLE_LEVELS == 2
1574 pr_warn("Kernel not compiled for PAE. No mitigation for L1TF\n");
1575 return;
1576#endif
1577
1578 half_pa = (u64)l1tf_pfn_limit() << PAGE_SHIFT;
David Brazdil0f672f62019-12-10 10:32:29 +00001579 if (l1tf_mitigation != L1TF_MITIGATION_OFF &&
1580 e820__mapped_any(half_pa, ULLONG_MAX - half_pa, E820_TYPE_RAM)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001581 pr_warn("System has more than MAX_PA/2 memory. L1TF mitigation not effective.\n");
1582 pr_info("You may make it effective by booting the kernel with mem=%llu parameter.\n",
1583 half_pa);
1584 pr_info("However, doing so will make a part of your RAM unusable.\n");
David Brazdil0f672f62019-12-10 10:32:29 +00001585 pr_info("Reading https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html might help you decide.\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001586 return;
1587 }
1588
1589 setup_force_cpu_cap(X86_FEATURE_L1TF_PTEINV);
1590}
1591
1592static int __init l1tf_cmdline(char *str)
1593{
1594 if (!boot_cpu_has_bug(X86_BUG_L1TF))
1595 return 0;
1596
1597 if (!str)
1598 return -EINVAL;
1599
1600 if (!strcmp(str, "off"))
1601 l1tf_mitigation = L1TF_MITIGATION_OFF;
1602 else if (!strcmp(str, "flush,nowarn"))
1603 l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOWARN;
1604 else if (!strcmp(str, "flush"))
1605 l1tf_mitigation = L1TF_MITIGATION_FLUSH;
1606 else if (!strcmp(str, "flush,nosmt"))
1607 l1tf_mitigation = L1TF_MITIGATION_FLUSH_NOSMT;
1608 else if (!strcmp(str, "full"))
1609 l1tf_mitigation = L1TF_MITIGATION_FULL;
1610 else if (!strcmp(str, "full,force"))
1611 l1tf_mitigation = L1TF_MITIGATION_FULL_FORCE;
1612
1613 return 0;
1614}
1615early_param("l1tf", l1tf_cmdline);
1616
1617#undef pr_fmt
David Brazdil0f672f62019-12-10 10:32:29 +00001618#define pr_fmt(fmt) fmt
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001619
1620#ifdef CONFIG_SYSFS
1621
1622#define L1TF_DEFAULT_MSG "Mitigation: PTE Inversion"
1623
1624#if IS_ENABLED(CONFIG_KVM_INTEL)
1625static const char * const l1tf_vmx_states[] = {
1626 [VMENTER_L1D_FLUSH_AUTO] = "auto",
1627 [VMENTER_L1D_FLUSH_NEVER] = "vulnerable",
1628 [VMENTER_L1D_FLUSH_COND] = "conditional cache flushes",
1629 [VMENTER_L1D_FLUSH_ALWAYS] = "cache flushes",
1630 [VMENTER_L1D_FLUSH_EPT_DISABLED] = "EPT disabled",
1631 [VMENTER_L1D_FLUSH_NOT_REQUIRED] = "flush not necessary"
1632};
1633
1634static ssize_t l1tf_show_state(char *buf)
1635{
1636 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO)
1637 return sprintf(buf, "%s\n", L1TF_DEFAULT_MSG);
1638
1639 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_EPT_DISABLED ||
1640 (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER &&
1641 sched_smt_active())) {
1642 return sprintf(buf, "%s; VMX: %s\n", L1TF_DEFAULT_MSG,
1643 l1tf_vmx_states[l1tf_vmx_mitigation]);
1644 }
1645
1646 return sprintf(buf, "%s; VMX: %s, SMT %s\n", L1TF_DEFAULT_MSG,
1647 l1tf_vmx_states[l1tf_vmx_mitigation],
1648 sched_smt_active() ? "vulnerable" : "disabled");
1649}
David Brazdil0f672f62019-12-10 10:32:29 +00001650
1651static ssize_t itlb_multihit_show_state(char *buf)
1652{
Olivier Deprez157378f2022-04-04 15:47:50 +02001653 if (!boot_cpu_has(X86_FEATURE_MSR_IA32_FEAT_CTL) ||
1654 !boot_cpu_has(X86_FEATURE_VMX))
1655 return sprintf(buf, "KVM: Mitigation: VMX unsupported\n");
1656 else if (!(cr4_read_shadow() & X86_CR4_VMXE))
1657 return sprintf(buf, "KVM: Mitigation: VMX disabled\n");
1658 else if (itlb_multihit_kvm_mitigation)
David Brazdil0f672f62019-12-10 10:32:29 +00001659 return sprintf(buf, "KVM: Mitigation: Split huge pages\n");
1660 else
1661 return sprintf(buf, "KVM: Vulnerable\n");
1662}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001663#else
1664static ssize_t l1tf_show_state(char *buf)
1665{
1666 return sprintf(buf, "%s\n", L1TF_DEFAULT_MSG);
1667}
David Brazdil0f672f62019-12-10 10:32:29 +00001668
1669static ssize_t itlb_multihit_show_state(char *buf)
1670{
1671 return sprintf(buf, "Processor vulnerable\n");
1672}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001673#endif
1674
David Brazdil0f672f62019-12-10 10:32:29 +00001675static ssize_t mds_show_state(char *buf)
1676{
1677 if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
1678 return sprintf(buf, "%s; SMT Host state unknown\n",
1679 mds_strings[mds_mitigation]);
1680 }
1681
1682 if (boot_cpu_has(X86_BUG_MSBDS_ONLY)) {
1683 return sprintf(buf, "%s; SMT %s\n", mds_strings[mds_mitigation],
1684 (mds_mitigation == MDS_MITIGATION_OFF ? "vulnerable" :
1685 sched_smt_active() ? "mitigated" : "disabled"));
1686 }
1687
1688 return sprintf(buf, "%s; SMT %s\n", mds_strings[mds_mitigation],
1689 sched_smt_active() ? "vulnerable" : "disabled");
1690}
1691
1692static ssize_t tsx_async_abort_show_state(char *buf)
1693{
1694 if ((taa_mitigation == TAA_MITIGATION_TSX_DISABLED) ||
1695 (taa_mitigation == TAA_MITIGATION_OFF))
1696 return sprintf(buf, "%s\n", taa_strings[taa_mitigation]);
1697
1698 if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
1699 return sprintf(buf, "%s; SMT Host state unknown\n",
1700 taa_strings[taa_mitigation]);
1701 }
1702
1703 return sprintf(buf, "%s; SMT %s\n", taa_strings[taa_mitigation],
1704 sched_smt_active() ? "vulnerable" : "disabled");
1705}
1706
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001707static char *stibp_state(void)
1708{
Olivier Deprez157378f2022-04-04 15:47:50 +02001709 if (spectre_v2_in_eibrs_mode(spectre_v2_enabled))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001710 return "";
1711
Olivier Deprez0e641232021-09-23 10:07:05 +02001712 switch (spectre_v2_user_stibp) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001713 case SPECTRE_V2_USER_NONE:
1714 return ", STIBP: disabled";
1715 case SPECTRE_V2_USER_STRICT:
1716 return ", STIBP: forced";
David Brazdil0f672f62019-12-10 10:32:29 +00001717 case SPECTRE_V2_USER_STRICT_PREFERRED:
1718 return ", STIBP: always-on";
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001719 case SPECTRE_V2_USER_PRCTL:
1720 case SPECTRE_V2_USER_SECCOMP:
1721 if (static_key_enabled(&switch_to_cond_stibp))
1722 return ", STIBP: conditional";
1723 }
1724 return "";
1725}
1726
1727static char *ibpb_state(void)
1728{
1729 if (boot_cpu_has(X86_FEATURE_IBPB)) {
1730 if (static_key_enabled(&switch_mm_always_ibpb))
1731 return ", IBPB: always-on";
1732 if (static_key_enabled(&switch_mm_cond_ibpb))
1733 return ", IBPB: conditional";
1734 return ", IBPB: disabled";
1735 }
1736 return "";
1737}
1738
Olivier Deprez157378f2022-04-04 15:47:50 +02001739static ssize_t spectre_v2_show_state(char *buf)
1740{
1741 if (spectre_v2_enabled == SPECTRE_V2_LFENCE)
1742 return sprintf(buf, "Vulnerable: LFENCE\n");
1743
1744 if (spectre_v2_enabled == SPECTRE_V2_EIBRS && unprivileged_ebpf_enabled())
1745 return sprintf(buf, "Vulnerable: eIBRS with unprivileged eBPF\n");
1746
1747 if (sched_smt_active() && unprivileged_ebpf_enabled() &&
1748 spectre_v2_enabled == SPECTRE_V2_EIBRS_LFENCE)
1749 return sprintf(buf, "Vulnerable: eIBRS+LFENCE with unprivileged eBPF and SMT\n");
1750
1751 return sprintf(buf, "%s%s%s%s%s%s\n",
1752 spectre_v2_strings[spectre_v2_enabled],
1753 ibpb_state(),
1754 boot_cpu_has(X86_FEATURE_USE_IBRS_FW) ? ", IBRS_FW" : "",
1755 stibp_state(),
1756 boot_cpu_has(X86_FEATURE_RSB_CTXSW) ? ", RSB filling" : "",
1757 spectre_v2_module_string());
1758}
1759
Olivier Deprez0e641232021-09-23 10:07:05 +02001760static ssize_t srbds_show_state(char *buf)
1761{
1762 return sprintf(buf, "%s\n", srbds_strings[srbds_mitigation]);
1763}
1764
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001765static ssize_t cpu_show_common(struct device *dev, struct device_attribute *attr,
1766 char *buf, unsigned int bug)
1767{
1768 if (!boot_cpu_has_bug(bug))
1769 return sprintf(buf, "Not affected\n");
1770
1771 switch (bug) {
1772 case X86_BUG_CPU_MELTDOWN:
1773 if (boot_cpu_has(X86_FEATURE_PTI))
1774 return sprintf(buf, "Mitigation: PTI\n");
1775
1776 if (hypervisor_is_type(X86_HYPER_XEN_PV))
1777 return sprintf(buf, "Unknown (XEN PV detected, hypervisor mitigation required)\n");
1778
1779 break;
1780
1781 case X86_BUG_SPECTRE_V1:
David Brazdil0f672f62019-12-10 10:32:29 +00001782 return sprintf(buf, "%s\n", spectre_v1_strings[spectre_v1_mitigation]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001783
1784 case X86_BUG_SPECTRE_V2:
Olivier Deprez157378f2022-04-04 15:47:50 +02001785 return spectre_v2_show_state(buf);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001786
1787 case X86_BUG_SPEC_STORE_BYPASS:
1788 return sprintf(buf, "%s\n", ssb_strings[ssb_mode]);
1789
1790 case X86_BUG_L1TF:
1791 if (boot_cpu_has(X86_FEATURE_L1TF_PTEINV))
1792 return l1tf_show_state(buf);
1793 break;
David Brazdil0f672f62019-12-10 10:32:29 +00001794
1795 case X86_BUG_MDS:
1796 return mds_show_state(buf);
1797
1798 case X86_BUG_TAA:
1799 return tsx_async_abort_show_state(buf);
1800
1801 case X86_BUG_ITLB_MULTIHIT:
1802 return itlb_multihit_show_state(buf);
1803
Olivier Deprez0e641232021-09-23 10:07:05 +02001804 case X86_BUG_SRBDS:
1805 return srbds_show_state(buf);
1806
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001807 default:
1808 break;
1809 }
1810
1811 return sprintf(buf, "Vulnerable\n");
1812}
1813
1814ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr, char *buf)
1815{
1816 return cpu_show_common(dev, attr, buf, X86_BUG_CPU_MELTDOWN);
1817}
1818
1819ssize_t cpu_show_spectre_v1(struct device *dev, struct device_attribute *attr, char *buf)
1820{
1821 return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V1);
1822}
1823
1824ssize_t cpu_show_spectre_v2(struct device *dev, struct device_attribute *attr, char *buf)
1825{
1826 return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V2);
1827}
1828
1829ssize_t cpu_show_spec_store_bypass(struct device *dev, struct device_attribute *attr, char *buf)
1830{
1831 return cpu_show_common(dev, attr, buf, X86_BUG_SPEC_STORE_BYPASS);
1832}
1833
1834ssize_t cpu_show_l1tf(struct device *dev, struct device_attribute *attr, char *buf)
1835{
1836 return cpu_show_common(dev, attr, buf, X86_BUG_L1TF);
1837}
David Brazdil0f672f62019-12-10 10:32:29 +00001838
1839ssize_t cpu_show_mds(struct device *dev, struct device_attribute *attr, char *buf)
1840{
1841 return cpu_show_common(dev, attr, buf, X86_BUG_MDS);
1842}
1843
1844ssize_t cpu_show_tsx_async_abort(struct device *dev, struct device_attribute *attr, char *buf)
1845{
1846 return cpu_show_common(dev, attr, buf, X86_BUG_TAA);
1847}
1848
1849ssize_t cpu_show_itlb_multihit(struct device *dev, struct device_attribute *attr, char *buf)
1850{
1851 return cpu_show_common(dev, attr, buf, X86_BUG_ITLB_MULTIHIT);
1852}
Olivier Deprez0e641232021-09-23 10:07:05 +02001853
1854ssize_t cpu_show_srbds(struct device *dev, struct device_attribute *attr, char *buf)
1855{
1856 return cpu_show_common(dev, attr, buf, X86_BUG_SRBDS);
1857}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001858#endif