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