1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Handle detection, reporting and mitigation of Spectre v1, v2 and v4, as
4 * detailed at:
5 *
6 * https://developer.arm.com/support/arm-security-updates/speculative-processor-vulnerability
7 *
8 * This code was originally written hastily under an awful lot of stress and so
9 * aspects of it are somewhat hacky. Unfortunately, changing anything in here
10 * instantly makes me feel ill. Thanks, Jann. Thann.
11 *
12 * Copyright (C) 2018 ARM Ltd, All Rights Reserved.
13 * Copyright (C) 2020 Google LLC
14 *
15 * "If there's something strange in your neighbourhood, who you gonna call?"
16 *
17 * Authors: Will Deacon <will@kernel.org> and Marc Zyngier <maz@kernel.org>
18 */
19
20 #include <linux/arm-smccc.h>
21 #include <linux/bpf.h>
22 #include <linux/cpu.h>
23 #include <linux/device.h>
24 #include <linux/nospec.h>
25 #include <linux/prctl.h>
26 #include <linux/sched/task_stack.h>
27
28 #include <asm/insn.h>
29 #include <asm/spectre.h>
30 #include <asm/traps.h>
31 #include <asm/vectors.h>
32 #include <asm/virt.h>
33
34 /*
35 * We try to ensure that the mitigation state can never change as the result of
36 * onlining a late CPU.
37 */
update_mitigation_state(enum mitigation_state * oldp,enum mitigation_state new)38 static void update_mitigation_state(enum mitigation_state *oldp,
39 enum mitigation_state new)
40 {
41 enum mitigation_state state;
42
43 do {
44 state = READ_ONCE(*oldp);
45 if (new <= state)
46 break;
47
48 /* Userspace almost certainly can't deal with this. */
49 if (WARN_ON(system_capabilities_finalized()))
50 break;
51 } while (cmpxchg_relaxed(oldp, state, new) != state);
52 }
53
54 /*
55 * Spectre v1.
56 *
57 * The kernel can't protect userspace for this one: it's each person for
58 * themselves. Advertise what we're doing and be done with it.
59 */
cpu_show_spectre_v1(struct device * dev,struct device_attribute * attr,char * buf)60 ssize_t cpu_show_spectre_v1(struct device *dev, struct device_attribute *attr,
61 char *buf)
62 {
63 return sprintf(buf, "Mitigation: __user pointer sanitization\n");
64 }
65
66 /*
67 * Spectre v2.
68 *
69 * This one sucks. A CPU is either:
70 *
71 * - Mitigated in hardware and advertised by ID_AA64PFR0_EL1.CSV2.
72 * - Mitigated in hardware and listed in our "safe list".
73 * - Mitigated in software by firmware.
74 * - Mitigated in software by a CPU-specific dance in the kernel and a
75 * firmware call at EL2.
76 * - Vulnerable.
77 *
78 * It's not unlikely for different CPUs in a big.LITTLE system to fall into
79 * different camps.
80 */
81 static enum mitigation_state spectre_v2_state;
82
83 static bool __read_mostly __nospectre_v2;
parse_spectre_v2_param(char * str)84 static int __init parse_spectre_v2_param(char *str)
85 {
86 __nospectre_v2 = true;
87 return 0;
88 }
89 early_param("nospectre_v2", parse_spectre_v2_param);
90
spectre_v2_mitigations_off(void)91 static bool spectre_v2_mitigations_off(void)
92 {
93 bool ret = __nospectre_v2 || cpu_mitigations_off();
94
95 if (ret)
96 pr_info_once("spectre-v2 mitigation disabled by command line option\n");
97
98 return ret;
99 }
100
get_bhb_affected_string(enum mitigation_state bhb_state)101 static const char *get_bhb_affected_string(enum mitigation_state bhb_state)
102 {
103 switch (bhb_state) {
104 case SPECTRE_UNAFFECTED:
105 return "";
106 default:
107 case SPECTRE_VULNERABLE:
108 return ", but not BHB";
109 case SPECTRE_MITIGATED:
110 return ", BHB";
111 }
112 }
113
_unprivileged_ebpf_enabled(void)114 static bool _unprivileged_ebpf_enabled(void)
115 {
116 #ifdef CONFIG_BPF_SYSCALL
117 return !sysctl_unprivileged_bpf_disabled;
118 #else
119 return false;
120 #endif
121 }
122
cpu_show_spectre_v2(struct device * dev,struct device_attribute * attr,char * buf)123 ssize_t cpu_show_spectre_v2(struct device *dev, struct device_attribute *attr,
124 char *buf)
125 {
126 enum mitigation_state bhb_state = arm64_get_spectre_bhb_state();
127 const char *bhb_str = get_bhb_affected_string(bhb_state);
128 const char *v2_str = "Branch predictor hardening";
129
130 switch (spectre_v2_state) {
131 case SPECTRE_UNAFFECTED:
132 if (bhb_state == SPECTRE_UNAFFECTED)
133 return sprintf(buf, "Not affected\n");
134
135 /*
136 * Platforms affected by Spectre-BHB can't report
137 * "Not affected" for Spectre-v2.
138 */
139 v2_str = "CSV2";
140 fallthrough;
141 case SPECTRE_MITIGATED:
142 if (bhb_state == SPECTRE_MITIGATED && _unprivileged_ebpf_enabled())
143 return sprintf(buf, "Vulnerable: Unprivileged eBPF enabled\n");
144
145 return sprintf(buf, "Mitigation: %s%s\n", v2_str, bhb_str);
146 case SPECTRE_VULNERABLE:
147 fallthrough;
148 default:
149 return sprintf(buf, "Vulnerable\n");
150 }
151 }
152
spectre_v2_get_cpu_hw_mitigation_state(void)153 static enum mitigation_state spectre_v2_get_cpu_hw_mitigation_state(void)
154 {
155 u64 pfr0;
156 static const struct midr_range spectre_v2_safe_list[] = {
157 MIDR_ALL_VERSIONS(MIDR_CORTEX_A35),
158 MIDR_ALL_VERSIONS(MIDR_CORTEX_A53),
159 MIDR_ALL_VERSIONS(MIDR_CORTEX_A55),
160 MIDR_ALL_VERSIONS(MIDR_BRAHMA_B53),
161 MIDR_ALL_VERSIONS(MIDR_HISI_TSV110),
162 MIDR_ALL_VERSIONS(MIDR_QCOM_KRYO_2XX_SILVER),
163 MIDR_ALL_VERSIONS(MIDR_QCOM_KRYO_3XX_SILVER),
164 MIDR_ALL_VERSIONS(MIDR_QCOM_KRYO_4XX_SILVER),
165 { /* sentinel */ }
166 };
167
168 /* If the CPU has CSV2 set, we're safe */
169 pfr0 = read_cpuid(ID_AA64PFR0_EL1);
170 if (cpuid_feature_extract_unsigned_field(pfr0, ID_AA64PFR0_CSV2_SHIFT))
171 return SPECTRE_UNAFFECTED;
172
173 /* Alternatively, we have a list of unaffected CPUs */
174 if (is_midr_in_range_list(read_cpuid_id(), spectre_v2_safe_list))
175 return SPECTRE_UNAFFECTED;
176
177 return SPECTRE_VULNERABLE;
178 }
179
spectre_v2_get_cpu_fw_mitigation_state(void)180 static enum mitigation_state spectre_v2_get_cpu_fw_mitigation_state(void)
181 {
182 int ret;
183 struct arm_smccc_res res;
184
185 arm_smccc_1_1_invoke(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
186 ARM_SMCCC_ARCH_WORKAROUND_1, &res);
187
188 ret = res.a0;
189 switch (ret) {
190 case SMCCC_RET_SUCCESS:
191 return SPECTRE_MITIGATED;
192 case SMCCC_ARCH_WORKAROUND_RET_UNAFFECTED:
193 return SPECTRE_UNAFFECTED;
194 default:
195 fallthrough;
196 case SMCCC_RET_NOT_SUPPORTED:
197 return SPECTRE_VULNERABLE;
198 }
199 }
200
has_spectre_v2(const struct arm64_cpu_capabilities * entry,int scope)201 bool has_spectre_v2(const struct arm64_cpu_capabilities *entry, int scope)
202 {
203 WARN_ON(scope != SCOPE_LOCAL_CPU || preemptible());
204
205 if (spectre_v2_get_cpu_hw_mitigation_state() == SPECTRE_UNAFFECTED)
206 return false;
207
208 if (spectre_v2_get_cpu_fw_mitigation_state() == SPECTRE_UNAFFECTED)
209 return false;
210
211 return true;
212 }
213
214 DEFINE_PER_CPU_READ_MOSTLY(struct bp_hardening_data, bp_hardening_data);
215
arm64_get_spectre_v2_state(void)216 enum mitigation_state arm64_get_spectre_v2_state(void)
217 {
218 return spectre_v2_state;
219 }
220
221 #ifdef CONFIG_KVM
222 #include <asm/cacheflush.h>
223 #include <asm/kvm_asm.h>
224
225 atomic_t arm64_el2_vector_last_slot = ATOMIC_INIT(-1);
226
__copy_hyp_vect_bpi(int slot,const char * hyp_vecs_start,const char * hyp_vecs_end)227 static void __copy_hyp_vect_bpi(int slot, const char *hyp_vecs_start,
228 const char *hyp_vecs_end)
229 {
230 void *dst = lm_alias(__bp_harden_hyp_vecs + slot * SZ_2K);
231 int i;
232
233 for (i = 0; i < SZ_2K; i += 0x80)
234 memcpy(dst + i, hyp_vecs_start, hyp_vecs_end - hyp_vecs_start);
235
236 __flush_icache_range((uintptr_t)dst, (uintptr_t)dst + SZ_2K);
237 }
238
239 static DEFINE_RAW_SPINLOCK(bp_lock);
install_bp_hardening_cb(bp_hardening_cb_t fn)240 static void install_bp_hardening_cb(bp_hardening_cb_t fn)
241 {
242 int cpu, slot = -1;
243 const char *hyp_vecs_start = __smccc_workaround_1_smc;
244 const char *hyp_vecs_end = __smccc_workaround_1_smc +
245 __SMCCC_WORKAROUND_1_SMC_SZ;
246
247 /*
248 * Vinz Clortho takes the hyp_vecs start/end "keys" at
249 * the door when we're a guest. Skip the hyp-vectors work.
250 */
251 if (!is_hyp_mode_available()) {
252 __this_cpu_write(bp_hardening_data.fn, fn);
253 return;
254 }
255
256 raw_spin_lock(&bp_lock);
257 for_each_possible_cpu(cpu) {
258 if (per_cpu(bp_hardening_data.fn, cpu) == fn) {
259 slot = per_cpu(bp_hardening_data.hyp_vectors_slot, cpu);
260 break;
261 }
262 }
263
264 if (slot == -1) {
265 slot = atomic_inc_return(&arm64_el2_vector_last_slot);
266 BUG_ON(slot >= BP_HARDEN_EL2_SLOTS);
267 __copy_hyp_vect_bpi(slot, hyp_vecs_start, hyp_vecs_end);
268 }
269
270 __this_cpu_write(bp_hardening_data.hyp_vectors_slot, slot);
271 __this_cpu_write(bp_hardening_data.fn, fn);
272 __this_cpu_write(bp_hardening_data.template_start, hyp_vecs_start);
273 raw_spin_unlock(&bp_lock);
274 }
275 #else
install_bp_hardening_cb(bp_hardening_cb_t fn)276 static void install_bp_hardening_cb(bp_hardening_cb_t fn)
277 {
278 __this_cpu_write(bp_hardening_data.fn, fn);
279 }
280 #endif /* CONFIG_KVM */
281
call_smc_arch_workaround_1(void)282 static void call_smc_arch_workaround_1(void)
283 {
284 arm_smccc_1_1_smc(ARM_SMCCC_ARCH_WORKAROUND_1, NULL);
285 }
286
call_hvc_arch_workaround_1(void)287 static void call_hvc_arch_workaround_1(void)
288 {
289 arm_smccc_1_1_hvc(ARM_SMCCC_ARCH_WORKAROUND_1, NULL);
290 }
291
qcom_link_stack_sanitisation(void)292 static void qcom_link_stack_sanitisation(void)
293 {
294 u64 tmp;
295
296 asm volatile("mov %0, x30 \n"
297 ".rept 16 \n"
298 "bl . + 4 \n"
299 ".endr \n"
300 "mov x30, %0 \n"
301 : "=&r" (tmp));
302 }
303
spectre_v2_get_sw_mitigation_cb(void)304 static bp_hardening_cb_t spectre_v2_get_sw_mitigation_cb(void)
305 {
306 u32 midr = read_cpuid_id();
307 if (((midr & MIDR_CPU_MODEL_MASK) != MIDR_QCOM_FALKOR) &&
308 ((midr & MIDR_CPU_MODEL_MASK) != MIDR_QCOM_FALKOR_V1))
309 return NULL;
310
311 return qcom_link_stack_sanitisation;
312 }
313
spectre_v2_enable_fw_mitigation(void)314 static enum mitigation_state spectre_v2_enable_fw_mitigation(void)
315 {
316 bp_hardening_cb_t cb;
317 enum mitigation_state state;
318
319 state = spectre_v2_get_cpu_fw_mitigation_state();
320 if (state != SPECTRE_MITIGATED)
321 return state;
322
323 if (spectre_v2_mitigations_off())
324 return SPECTRE_VULNERABLE;
325
326 switch (arm_smccc_1_1_get_conduit()) {
327 case SMCCC_CONDUIT_HVC:
328 cb = call_hvc_arch_workaround_1;
329 break;
330
331 case SMCCC_CONDUIT_SMC:
332 cb = call_smc_arch_workaround_1;
333 break;
334
335 default:
336 return SPECTRE_VULNERABLE;
337 }
338
339 /*
340 * Prefer a CPU-specific workaround if it exists. Note that we
341 * still rely on firmware for the mitigation at EL2.
342 */
343 cb = spectre_v2_get_sw_mitigation_cb() ?: cb;
344 install_bp_hardening_cb(cb);
345 return SPECTRE_MITIGATED;
346 }
347
spectre_v2_enable_mitigation(const struct arm64_cpu_capabilities * __unused)348 void spectre_v2_enable_mitigation(const struct arm64_cpu_capabilities *__unused)
349 {
350 enum mitigation_state state;
351
352 WARN_ON(preemptible());
353
354 state = spectre_v2_get_cpu_hw_mitigation_state();
355 if (state == SPECTRE_VULNERABLE)
356 state = spectre_v2_enable_fw_mitigation();
357
358 update_mitigation_state(&spectre_v2_state, state);
359 }
360
361 /*
362 * Spectre v4.
363 *
364 * If you thought Spectre v2 was nasty, wait until you see this mess. A CPU is
365 * either:
366 *
367 * - Mitigated in hardware and listed in our "safe list".
368 * - Mitigated in hardware via PSTATE.SSBS.
369 * - Mitigated in software by firmware (sometimes referred to as SSBD).
370 *
371 * Wait, that doesn't sound so bad, does it? Keep reading...
372 *
373 * A major source of headaches is that the software mitigation is enabled both
374 * on a per-task basis, but can also be forced on for the kernel, necessitating
375 * both context-switch *and* entry/exit hooks. To make it even worse, some CPUs
376 * allow EL0 to toggle SSBS directly, which can end up with the prctl() state
377 * being stale when re-entering the kernel. The usual big.LITTLE caveats apply,
378 * so you can have systems that have both firmware and SSBS mitigations. This
379 * means we actually have to reject late onlining of CPUs with mitigations if
380 * all of the currently onlined CPUs are safelisted, as the mitigation tends to
381 * be opt-in for userspace. Yes, really, the cure is worse than the disease.
382 *
383 * The only good part is that if the firmware mitigation is present, then it is
384 * present for all CPUs, meaning we don't have to worry about late onlining of a
385 * vulnerable CPU if one of the boot CPUs is using the firmware mitigation.
386 *
387 * Give me a VAX-11/780 any day of the week...
388 */
389 static enum mitigation_state spectre_v4_state;
390
391 /* This is the per-cpu state tracking whether we need to talk to firmware */
392 DEFINE_PER_CPU_READ_MOSTLY(u64, arm64_ssbd_callback_required);
393
394 enum spectre_v4_policy {
395 SPECTRE_V4_POLICY_MITIGATION_DYNAMIC,
396 SPECTRE_V4_POLICY_MITIGATION_ENABLED,
397 SPECTRE_V4_POLICY_MITIGATION_DISABLED,
398 };
399
400 static enum spectre_v4_policy __read_mostly __spectre_v4_policy;
401
402 static const struct spectre_v4_param {
403 const char *str;
404 enum spectre_v4_policy policy;
405 } spectre_v4_params[] = {
406 { "force-on", SPECTRE_V4_POLICY_MITIGATION_ENABLED, },
407 { "force-off", SPECTRE_V4_POLICY_MITIGATION_DISABLED, },
408 { "kernel", SPECTRE_V4_POLICY_MITIGATION_DYNAMIC, },
409 };
parse_spectre_v4_param(char * str)410 static int __init parse_spectre_v4_param(char *str)
411 {
412 int i;
413
414 if (!str || !str[0])
415 return -EINVAL;
416
417 for (i = 0; i < ARRAY_SIZE(spectre_v4_params); i++) {
418 const struct spectre_v4_param *param = &spectre_v4_params[i];
419
420 if (strncmp(str, param->str, strlen(param->str)))
421 continue;
422
423 __spectre_v4_policy = param->policy;
424 return 0;
425 }
426
427 return -EINVAL;
428 }
429 early_param("ssbd", parse_spectre_v4_param);
430
431 /*
432 * Because this was all written in a rush by people working in different silos,
433 * we've ended up with multiple command line options to control the same thing.
434 * Wrap these up in some helpers, which prefer disabling the mitigation if faced
435 * with contradictory parameters. The mitigation is always either "off",
436 * "dynamic" or "on".
437 */
spectre_v4_mitigations_off(void)438 static bool spectre_v4_mitigations_off(void)
439 {
440 bool ret = cpu_mitigations_off() ||
441 __spectre_v4_policy == SPECTRE_V4_POLICY_MITIGATION_DISABLED;
442
443 if (ret)
444 pr_info_once("spectre-v4 mitigation disabled by command-line option\n");
445
446 return ret;
447 }
448
449 /* Do we need to toggle the mitigation state on entry to/exit from the kernel? */
spectre_v4_mitigations_dynamic(void)450 static bool spectre_v4_mitigations_dynamic(void)
451 {
452 return !spectre_v4_mitigations_off() &&
453 __spectre_v4_policy == SPECTRE_V4_POLICY_MITIGATION_DYNAMIC;
454 }
455
spectre_v4_mitigations_on(void)456 static bool spectre_v4_mitigations_on(void)
457 {
458 return !spectre_v4_mitigations_off() &&
459 __spectre_v4_policy == SPECTRE_V4_POLICY_MITIGATION_ENABLED;
460 }
461
cpu_show_spec_store_bypass(struct device * dev,struct device_attribute * attr,char * buf)462 ssize_t cpu_show_spec_store_bypass(struct device *dev,
463 struct device_attribute *attr, char *buf)
464 {
465 switch (spectre_v4_state) {
466 case SPECTRE_UNAFFECTED:
467 return sprintf(buf, "Not affected\n");
468 case SPECTRE_MITIGATED:
469 return sprintf(buf, "Mitigation: Speculative Store Bypass disabled via prctl\n");
470 case SPECTRE_VULNERABLE:
471 fallthrough;
472 default:
473 return sprintf(buf, "Vulnerable\n");
474 }
475 }
476
arm64_get_spectre_v4_state(void)477 enum mitigation_state arm64_get_spectre_v4_state(void)
478 {
479 return spectre_v4_state;
480 }
481
spectre_v4_get_cpu_hw_mitigation_state(void)482 static enum mitigation_state spectre_v4_get_cpu_hw_mitigation_state(void)
483 {
484 static const struct midr_range spectre_v4_safe_list[] = {
485 MIDR_ALL_VERSIONS(MIDR_CORTEX_A35),
486 MIDR_ALL_VERSIONS(MIDR_CORTEX_A53),
487 MIDR_ALL_VERSIONS(MIDR_CORTEX_A55),
488 MIDR_ALL_VERSIONS(MIDR_BRAHMA_B53),
489 MIDR_ALL_VERSIONS(MIDR_QCOM_KRYO_3XX_SILVER),
490 MIDR_ALL_VERSIONS(MIDR_QCOM_KRYO_4XX_SILVER),
491 { /* sentinel */ },
492 };
493
494 if (is_midr_in_range_list(read_cpuid_id(), spectre_v4_safe_list))
495 return SPECTRE_UNAFFECTED;
496
497 /* CPU features are detected first */
498 if (this_cpu_has_cap(ARM64_SSBS))
499 return SPECTRE_MITIGATED;
500
501 return SPECTRE_VULNERABLE;
502 }
503
spectre_v4_get_cpu_fw_mitigation_state(void)504 static enum mitigation_state spectre_v4_get_cpu_fw_mitigation_state(void)
505 {
506 int ret;
507 struct arm_smccc_res res;
508
509 arm_smccc_1_1_invoke(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
510 ARM_SMCCC_ARCH_WORKAROUND_2, &res);
511
512 ret = res.a0;
513 switch (ret) {
514 case SMCCC_RET_SUCCESS:
515 return SPECTRE_MITIGATED;
516 case SMCCC_ARCH_WORKAROUND_RET_UNAFFECTED:
517 fallthrough;
518 case SMCCC_RET_NOT_REQUIRED:
519 return SPECTRE_UNAFFECTED;
520 default:
521 fallthrough;
522 case SMCCC_RET_NOT_SUPPORTED:
523 return SPECTRE_VULNERABLE;
524 }
525 }
526
has_spectre_v4(const struct arm64_cpu_capabilities * cap,int scope)527 bool has_spectre_v4(const struct arm64_cpu_capabilities *cap, int scope)
528 {
529 enum mitigation_state state;
530
531 WARN_ON(scope != SCOPE_LOCAL_CPU || preemptible());
532
533 state = spectre_v4_get_cpu_hw_mitigation_state();
534 if (state == SPECTRE_VULNERABLE)
535 state = spectre_v4_get_cpu_fw_mitigation_state();
536
537 return state != SPECTRE_UNAFFECTED;
538 }
539
ssbs_emulation_handler(struct pt_regs * regs,u32 instr)540 static int ssbs_emulation_handler(struct pt_regs *regs, u32 instr)
541 {
542 if (user_mode(regs))
543 return 1;
544
545 if (instr & BIT(PSTATE_Imm_shift))
546 regs->pstate |= PSR_SSBS_BIT;
547 else
548 regs->pstate &= ~PSR_SSBS_BIT;
549
550 arm64_skip_faulting_instruction(regs, 4);
551 return 0;
552 }
553
554 static struct undef_hook ssbs_emulation_hook = {
555 .instr_mask = ~(1U << PSTATE_Imm_shift),
556 .instr_val = 0xd500401f | PSTATE_SSBS,
557 .fn = ssbs_emulation_handler,
558 };
559
spectre_v4_enable_hw_mitigation(void)560 static enum mitigation_state spectre_v4_enable_hw_mitigation(void)
561 {
562 static bool undef_hook_registered = false;
563 static DEFINE_RAW_SPINLOCK(hook_lock);
564 enum mitigation_state state;
565
566 /*
567 * If the system is mitigated but this CPU doesn't have SSBS, then
568 * we must be on the safelist and there's nothing more to do.
569 */
570 state = spectre_v4_get_cpu_hw_mitigation_state();
571 if (state != SPECTRE_MITIGATED || !this_cpu_has_cap(ARM64_SSBS))
572 return state;
573
574 raw_spin_lock(&hook_lock);
575 if (!undef_hook_registered) {
576 register_undef_hook(&ssbs_emulation_hook);
577 undef_hook_registered = true;
578 }
579 raw_spin_unlock(&hook_lock);
580
581 if (spectre_v4_mitigations_off()) {
582 sysreg_clear_set(sctlr_el1, 0, SCTLR_ELx_DSSBS);
583 asm volatile(SET_PSTATE_SSBS(1));
584 return SPECTRE_VULNERABLE;
585 }
586
587 /* SCTLR_EL1.DSSBS was initialised to 0 during boot */
588 asm volatile(SET_PSTATE_SSBS(0));
589 return SPECTRE_MITIGATED;
590 }
591
592 /*
593 * Patch a branch over the Spectre-v4 mitigation code with a NOP so that
594 * we fallthrough and check whether firmware needs to be called on this CPU.
595 */
spectre_v4_patch_fw_mitigation_enable(struct alt_instr * alt,__le32 * origptr,__le32 * updptr,int nr_inst)596 void __init spectre_v4_patch_fw_mitigation_enable(struct alt_instr *alt,
597 __le32 *origptr,
598 __le32 *updptr, int nr_inst)
599 {
600 BUG_ON(nr_inst != 1); /* Branch -> NOP */
601
602 if (spectre_v4_mitigations_off())
603 return;
604
605 if (cpus_have_final_cap(ARM64_SSBS))
606 return;
607
608 if (spectre_v4_mitigations_dynamic())
609 *updptr = cpu_to_le32(aarch64_insn_gen_nop());
610 }
611
612 /*
613 * Patch a NOP in the Spectre-v4 mitigation code with an SMC/HVC instruction
614 * to call into firmware to adjust the mitigation state.
615 */
smccc_patch_fw_mitigation_conduit(struct alt_instr * alt,__le32 * origptr,__le32 * updptr,int nr_inst)616 void __init smccc_patch_fw_mitigation_conduit(struct alt_instr *alt,
617 __le32 *origptr,
618 __le32 *updptr, int nr_inst)
619 {
620 u32 insn;
621
622 BUG_ON(nr_inst != 1); /* NOP -> HVC/SMC */
623
624 switch (arm_smccc_1_1_get_conduit()) {
625 case SMCCC_CONDUIT_HVC:
626 insn = aarch64_insn_get_hvc_value();
627 break;
628 case SMCCC_CONDUIT_SMC:
629 insn = aarch64_insn_get_smc_value();
630 break;
631 default:
632 return;
633 }
634
635 *updptr = cpu_to_le32(insn);
636 }
637
spectre_v4_enable_fw_mitigation(void)638 static enum mitigation_state spectre_v4_enable_fw_mitigation(void)
639 {
640 enum mitigation_state state;
641
642 state = spectre_v4_get_cpu_fw_mitigation_state();
643 if (state != SPECTRE_MITIGATED)
644 return state;
645
646 if (spectre_v4_mitigations_off()) {
647 arm_smccc_1_1_invoke(ARM_SMCCC_ARCH_WORKAROUND_2, false, NULL);
648 return SPECTRE_VULNERABLE;
649 }
650
651 arm_smccc_1_1_invoke(ARM_SMCCC_ARCH_WORKAROUND_2, true, NULL);
652
653 if (spectre_v4_mitigations_dynamic())
654 __this_cpu_write(arm64_ssbd_callback_required, 1);
655
656 return SPECTRE_MITIGATED;
657 }
658
spectre_v4_enable_mitigation(const struct arm64_cpu_capabilities * __unused)659 void spectre_v4_enable_mitigation(const struct arm64_cpu_capabilities *__unused)
660 {
661 enum mitigation_state state;
662
663 WARN_ON(preemptible());
664
665 state = spectre_v4_enable_hw_mitigation();
666 if (state == SPECTRE_VULNERABLE)
667 state = spectre_v4_enable_fw_mitigation();
668
669 update_mitigation_state(&spectre_v4_state, state);
670 }
671
__update_pstate_ssbs(struct pt_regs * regs,bool state)672 static void __update_pstate_ssbs(struct pt_regs *regs, bool state)
673 {
674 u64 bit = compat_user_mode(regs) ? PSR_AA32_SSBS_BIT : PSR_SSBS_BIT;
675
676 if (state)
677 regs->pstate |= bit;
678 else
679 regs->pstate &= ~bit;
680 }
681
spectre_v4_enable_task_mitigation(struct task_struct * tsk)682 void spectre_v4_enable_task_mitigation(struct task_struct *tsk)
683 {
684 struct pt_regs *regs = task_pt_regs(tsk);
685 bool ssbs = false, kthread = tsk->flags & PF_KTHREAD;
686
687 if (spectre_v4_mitigations_off())
688 ssbs = true;
689 else if (spectre_v4_mitigations_dynamic() && !kthread)
690 ssbs = !test_tsk_thread_flag(tsk, TIF_SSBD);
691
692 __update_pstate_ssbs(regs, ssbs);
693 }
694
695 /*
696 * The Spectre-v4 mitigation can be controlled via a prctl() from userspace.
697 * This is interesting because the "speculation disabled" behaviour can be
698 * configured so that it is preserved across exec(), which means that the
699 * prctl() may be necessary even when PSTATE.SSBS can be toggled directly
700 * from userspace.
701 */
ssbd_prctl_enable_mitigation(struct task_struct * task)702 static void ssbd_prctl_enable_mitigation(struct task_struct *task)
703 {
704 task_clear_spec_ssb_noexec(task);
705 task_set_spec_ssb_disable(task);
706 set_tsk_thread_flag(task, TIF_SSBD);
707 }
708
ssbd_prctl_disable_mitigation(struct task_struct * task)709 static void ssbd_prctl_disable_mitigation(struct task_struct *task)
710 {
711 task_clear_spec_ssb_noexec(task);
712 task_clear_spec_ssb_disable(task);
713 clear_tsk_thread_flag(task, TIF_SSBD);
714 }
715
ssbd_prctl_set(struct task_struct * task,unsigned long ctrl)716 static int ssbd_prctl_set(struct task_struct *task, unsigned long ctrl)
717 {
718 switch (ctrl) {
719 case PR_SPEC_ENABLE:
720 /* Enable speculation: disable mitigation */
721 /*
722 * Force disabled speculation prevents it from being
723 * re-enabled.
724 */
725 if (task_spec_ssb_force_disable(task))
726 return -EPERM;
727
728 /*
729 * If the mitigation is forced on, then speculation is forced
730 * off and we again prevent it from being re-enabled.
731 */
732 if (spectre_v4_mitigations_on())
733 return -EPERM;
734
735 ssbd_prctl_disable_mitigation(task);
736 break;
737 case PR_SPEC_FORCE_DISABLE:
738 /* Force disable speculation: force enable mitigation */
739 /*
740 * If the mitigation is forced off, then speculation is forced
741 * on and we prevent it from being disabled.
742 */
743 if (spectre_v4_mitigations_off())
744 return -EPERM;
745
746 task_set_spec_ssb_force_disable(task);
747 fallthrough;
748 case PR_SPEC_DISABLE:
749 /* Disable speculation: enable mitigation */
750 /* Same as PR_SPEC_FORCE_DISABLE */
751 if (spectre_v4_mitigations_off())
752 return -EPERM;
753
754 ssbd_prctl_enable_mitigation(task);
755 break;
756 case PR_SPEC_DISABLE_NOEXEC:
757 /* Disable speculation until execve(): enable mitigation */
758 /*
759 * If the mitigation state is forced one way or the other, then
760 * we must fail now before we try to toggle it on execve().
761 */
762 if (task_spec_ssb_force_disable(task) ||
763 spectre_v4_mitigations_off() ||
764 spectre_v4_mitigations_on()) {
765 return -EPERM;
766 }
767
768 ssbd_prctl_enable_mitigation(task);
769 task_set_spec_ssb_noexec(task);
770 break;
771 default:
772 return -ERANGE;
773 }
774
775 spectre_v4_enable_task_mitigation(task);
776 return 0;
777 }
778
arch_prctl_spec_ctrl_set(struct task_struct * task,unsigned long which,unsigned long ctrl)779 int arch_prctl_spec_ctrl_set(struct task_struct *task, unsigned long which,
780 unsigned long ctrl)
781 {
782 switch (which) {
783 case PR_SPEC_STORE_BYPASS:
784 return ssbd_prctl_set(task, ctrl);
785 default:
786 return -ENODEV;
787 }
788 }
789
ssbd_prctl_get(struct task_struct * task)790 static int ssbd_prctl_get(struct task_struct *task)
791 {
792 switch (spectre_v4_state) {
793 case SPECTRE_UNAFFECTED:
794 return PR_SPEC_NOT_AFFECTED;
795 case SPECTRE_MITIGATED:
796 if (spectre_v4_mitigations_on())
797 return PR_SPEC_NOT_AFFECTED;
798
799 if (spectre_v4_mitigations_dynamic())
800 break;
801
802 /* Mitigations are disabled, so we're vulnerable. */
803 fallthrough;
804 case SPECTRE_VULNERABLE:
805 fallthrough;
806 default:
807 return PR_SPEC_ENABLE;
808 }
809
810 /* Check the mitigation state for this task */
811 if (task_spec_ssb_force_disable(task))
812 return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE;
813
814 if (task_spec_ssb_noexec(task))
815 return PR_SPEC_PRCTL | PR_SPEC_DISABLE_NOEXEC;
816
817 if (task_spec_ssb_disable(task))
818 return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
819
820 return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
821 }
822
arch_prctl_spec_ctrl_get(struct task_struct * task,unsigned long which)823 int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which)
824 {
825 switch (which) {
826 case PR_SPEC_STORE_BYPASS:
827 return ssbd_prctl_get(task);
828 default:
829 return -ENODEV;
830 }
831 }
832
833 /*
834 * Spectre BHB.
835 *
836 * A CPU is either:
837 * - Mitigated by a branchy loop a CPU specific number of times, and listed
838 * in our "loop mitigated list".
839 * - Mitigated in software by the firmware Spectre v2 call.
840 * - Has the ClearBHB instruction to perform the mitigation.
841 * - Has the 'Exception Clears Branch History Buffer' (ECBHB) feature, so no
842 * software mitigation in the vectors is needed.
843 * - Has CSV2.3, so is unaffected.
844 */
845 static enum mitigation_state spectre_bhb_state;
846
arm64_get_spectre_bhb_state(void)847 enum mitigation_state arm64_get_spectre_bhb_state(void)
848 {
849 return spectre_bhb_state;
850 }
851
852 /*
853 * This must be called with SCOPE_LOCAL_CPU for each type of CPU, before any
854 * SCOPE_SYSTEM call will give the right answer.
855 */
spectre_bhb_loop_affected(int scope)856 u8 spectre_bhb_loop_affected(int scope)
857 {
858 u8 k = 0;
859 static u8 max_bhb_k;
860
861 if (scope == SCOPE_LOCAL_CPU) {
862 static const struct midr_range spectre_bhb_k32_list[] = {
863 MIDR_ALL_VERSIONS(MIDR_CORTEX_A78),
864 MIDR_ALL_VERSIONS(MIDR_CORTEX_A78C),
865 MIDR_ALL_VERSIONS(MIDR_CORTEX_X1),
866 MIDR_ALL_VERSIONS(MIDR_NEOVERSE_V1),
867 {},
868 };
869 static const struct midr_range spectre_bhb_k24_list[] = {
870 MIDR_ALL_VERSIONS(MIDR_CORTEX_A76),
871 MIDR_ALL_VERSIONS(MIDR_CORTEX_A77),
872 MIDR_ALL_VERSIONS(MIDR_NEOVERSE_N1),
873 {},
874 };
875 static const struct midr_range spectre_bhb_k8_list[] = {
876 MIDR_ALL_VERSIONS(MIDR_CORTEX_A72),
877 MIDR_ALL_VERSIONS(MIDR_CORTEX_A57),
878 {},
879 };
880
881 if (is_midr_in_range_list(read_cpuid_id(), spectre_bhb_k32_list))
882 k = 32;
883 else if (is_midr_in_range_list(read_cpuid_id(), spectre_bhb_k24_list))
884 k = 24;
885 else if (is_midr_in_range_list(read_cpuid_id(), spectre_bhb_k8_list))
886 k = 8;
887
888 max_bhb_k = max(max_bhb_k, k);
889 } else {
890 k = max_bhb_k;
891 }
892
893 return k;
894 }
895
spectre_bhb_get_cpu_fw_mitigation_state(void)896 static enum mitigation_state spectre_bhb_get_cpu_fw_mitigation_state(void)
897 {
898 int ret;
899 struct arm_smccc_res res;
900
901 arm_smccc_1_1_invoke(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
902 ARM_SMCCC_ARCH_WORKAROUND_3, &res);
903
904 ret = res.a0;
905 switch (ret) {
906 case SMCCC_RET_SUCCESS:
907 return SPECTRE_MITIGATED;
908 case SMCCC_ARCH_WORKAROUND_RET_UNAFFECTED:
909 return SPECTRE_UNAFFECTED;
910 default:
911 fallthrough;
912 case SMCCC_RET_NOT_SUPPORTED:
913 return SPECTRE_VULNERABLE;
914 }
915 }
916
is_spectre_bhb_fw_affected(int scope)917 static bool is_spectre_bhb_fw_affected(int scope)
918 {
919 static bool system_affected;
920 enum mitigation_state fw_state;
921 bool has_smccc = arm_smccc_1_1_get_conduit() != SMCCC_CONDUIT_NONE;
922 static const struct midr_range spectre_bhb_firmware_mitigated_list[] = {
923 MIDR_ALL_VERSIONS(MIDR_CORTEX_A73),
924 MIDR_ALL_VERSIONS(MIDR_CORTEX_A75),
925 {},
926 };
927 bool cpu_in_list = is_midr_in_range_list(read_cpuid_id(),
928 spectre_bhb_firmware_mitigated_list);
929
930 if (scope != SCOPE_LOCAL_CPU)
931 return system_affected;
932
933 fw_state = spectre_bhb_get_cpu_fw_mitigation_state();
934 if (cpu_in_list || (has_smccc && fw_state == SPECTRE_MITIGATED)) {
935 system_affected = true;
936 return true;
937 }
938
939 return false;
940 }
941
supports_ecbhb(int scope)942 static bool supports_ecbhb(int scope)
943 {
944 u64 mmfr1;
945
946 if (scope == SCOPE_LOCAL_CPU)
947 mmfr1 = read_sysreg_s(SYS_ID_AA64MMFR1_EL1);
948 else
949 mmfr1 = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1);
950
951 return cpuid_feature_extract_unsigned_field(mmfr1,
952 ID_AA64MMFR1_ECBHB_SHIFT);
953 }
954
is_spectre_bhb_affected(const struct arm64_cpu_capabilities * entry,int scope)955 bool is_spectre_bhb_affected(const struct arm64_cpu_capabilities *entry,
956 int scope)
957 {
958 WARN_ON(scope != SCOPE_LOCAL_CPU || preemptible());
959
960 if (supports_csv2p3(scope))
961 return false;
962
963 if (supports_clearbhb(scope))
964 return true;
965
966 if (spectre_bhb_loop_affected(scope))
967 return true;
968
969 if (is_spectre_bhb_fw_affected(scope))
970 return true;
971
972 return false;
973 }
974
this_cpu_set_vectors(enum arm64_bp_harden_el1_vectors slot)975 static void this_cpu_set_vectors(enum arm64_bp_harden_el1_vectors slot)
976 {
977 const char *v = arm64_get_bp_hardening_vector(slot);
978
979 if (slot < 0)
980 return;
981
982 __this_cpu_write(this_cpu_vector, v);
983
984 /*
985 * When KPTI is in use, the vectors are switched when exiting to
986 * user-space.
987 */
988 if (arm64_kernel_unmapped_at_el0())
989 return;
990
991 write_sysreg(v, vbar_el1);
992 isb();
993 }
994
995 #ifdef CONFIG_KVM
kvm_bhb_get_vecs_size(const char * start)996 static int kvm_bhb_get_vecs_size(const char *start)
997 {
998 if (start == __smccc_workaround_3_smc)
999 return __SMCCC_WORKAROUND_3_SMC_SZ;
1000 else if (start == __spectre_bhb_loop_k8 ||
1001 start == __spectre_bhb_loop_k24 ||
1002 start == __spectre_bhb_loop_k32)
1003 return __SPECTRE_BHB_LOOP_SZ;
1004 else if (start == __spectre_bhb_clearbhb)
1005 return __SPECTRE_BHB_CLEARBHB_SZ;
1006
1007 return 0;
1008 }
1009
kvm_setup_bhb_slot(const char * hyp_vecs_start)1010 static void kvm_setup_bhb_slot(const char *hyp_vecs_start)
1011 {
1012 int cpu, slot = -1, size;
1013 const char *hyp_vecs_end;
1014
1015 if (!IS_ENABLED(CONFIG_KVM) || !is_hyp_mode_available())
1016 return;
1017
1018 size = kvm_bhb_get_vecs_size(hyp_vecs_start);
1019 if (WARN_ON_ONCE(!hyp_vecs_start || !size))
1020 return;
1021 hyp_vecs_end = hyp_vecs_start + size;
1022
1023 raw_spin_lock(&bp_lock);
1024 for_each_possible_cpu(cpu) {
1025 if (per_cpu(bp_hardening_data.template_start, cpu) == hyp_vecs_start) {
1026 slot = per_cpu(bp_hardening_data.hyp_vectors_slot, cpu);
1027 break;
1028 }
1029 }
1030
1031 if (slot == -1) {
1032 slot = atomic_inc_return(&arm64_el2_vector_last_slot);
1033 BUG_ON(slot >= BP_HARDEN_EL2_SLOTS);
1034 __copy_hyp_vect_bpi(slot, hyp_vecs_start, hyp_vecs_end);
1035 }
1036
1037 __this_cpu_write(bp_hardening_data.hyp_vectors_slot, slot);
1038 __this_cpu_write(bp_hardening_data.template_start, hyp_vecs_start);
1039 raw_spin_unlock(&bp_lock);
1040 }
1041 #else
1042 #define __smccc_workaround_3_smc NULL
1043 #define __spectre_bhb_loop_k8 NULL
1044 #define __spectre_bhb_loop_k24 NULL
1045 #define __spectre_bhb_loop_k32 NULL
1046 #define __spectre_bhb_clearbhb NULL
1047
kvm_setup_bhb_slot(const char * hyp_vecs_start)1048 static void kvm_setup_bhb_slot(const char *hyp_vecs_start) { }
1049 #endif /* CONFIG_KVM */
1050
spectre_bhb_enable_mitigation(const struct arm64_cpu_capabilities * entry)1051 void spectre_bhb_enable_mitigation(const struct arm64_cpu_capabilities *entry)
1052 {
1053 enum mitigation_state fw_state, state = SPECTRE_VULNERABLE;
1054
1055 if (!is_spectre_bhb_affected(entry, SCOPE_LOCAL_CPU))
1056 return;
1057
1058 if (arm64_get_spectre_v2_state() == SPECTRE_VULNERABLE) {
1059 /* No point mitigating Spectre-BHB alone. */
1060 } else if (!IS_ENABLED(CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY)) {
1061 pr_info_once("spectre-bhb mitigation disabled by compile time option\n");
1062 } else if (cpu_mitigations_off()) {
1063 pr_info_once("spectre-bhb mitigation disabled by command line option\n");
1064 } else if (supports_ecbhb(SCOPE_LOCAL_CPU)) {
1065 state = SPECTRE_MITIGATED;
1066 } else if (supports_clearbhb(SCOPE_LOCAL_CPU)) {
1067 kvm_setup_bhb_slot(__spectre_bhb_clearbhb);
1068 this_cpu_set_vectors(EL1_VECTOR_BHB_CLEAR_INSN);
1069
1070 state = SPECTRE_MITIGATED;
1071 } else if (spectre_bhb_loop_affected(SCOPE_LOCAL_CPU)) {
1072 switch (spectre_bhb_loop_affected(SCOPE_SYSTEM)) {
1073 case 8:
1074 kvm_setup_bhb_slot(__spectre_bhb_loop_k8);
1075 break;
1076 case 24:
1077 kvm_setup_bhb_slot(__spectre_bhb_loop_k24);
1078 break;
1079 case 32:
1080 kvm_setup_bhb_slot(__spectre_bhb_loop_k32);
1081 break;
1082 default:
1083 WARN_ON_ONCE(1);
1084 }
1085 this_cpu_set_vectors(EL1_VECTOR_BHB_LOOP);
1086
1087 state = SPECTRE_MITIGATED;
1088 } else if (is_spectre_bhb_fw_affected(SCOPE_LOCAL_CPU)) {
1089 fw_state = spectre_bhb_get_cpu_fw_mitigation_state();
1090 if (fw_state == SPECTRE_MITIGATED) {
1091 kvm_setup_bhb_slot(__smccc_workaround_3_smc);
1092 this_cpu_set_vectors(EL1_VECTOR_BHB_FW);
1093
1094 state = SPECTRE_MITIGATED;
1095 }
1096 }
1097
1098 update_mitigation_state(&spectre_bhb_state, state);
1099 }
1100
1101 /* Patched to correct the immediate */
spectre_bhb_patch_loop_iter(struct alt_instr * alt,__le32 * origptr,__le32 * updptr,int nr_inst)1102 void noinstr spectre_bhb_patch_loop_iter(struct alt_instr *alt,
1103 __le32 *origptr, __le32 *updptr, int nr_inst)
1104 {
1105 u8 rd;
1106 u32 insn;
1107 u16 loop_count = spectre_bhb_loop_affected(SCOPE_SYSTEM);
1108
1109 BUG_ON(nr_inst != 1); /* MOV -> MOV */
1110
1111 if (!IS_ENABLED(CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY))
1112 return;
1113
1114 insn = le32_to_cpu(*origptr);
1115 rd = aarch64_insn_decode_register(AARCH64_INSN_REGTYPE_RD, insn);
1116 insn = aarch64_insn_gen_movewide(rd, loop_count, 0,
1117 AARCH64_INSN_VARIANT_64BIT,
1118 AARCH64_INSN_MOVEWIDE_ZERO);
1119 *updptr++ = cpu_to_le32(insn);
1120 }
1121
1122 #ifdef CONFIG_BPF_SYSCALL
1123 #define EBPF_WARN "Unprivileged eBPF is enabled, data leaks possible via Spectre v2 BHB attacks!\n"
unpriv_ebpf_notify(int new_state)1124 void unpriv_ebpf_notify(int new_state)
1125 {
1126 if (spectre_v2_state == SPECTRE_VULNERABLE ||
1127 spectre_bhb_state != SPECTRE_MITIGATED)
1128 return;
1129
1130 if (!new_state)
1131 pr_err("WARNING: %s", EBPF_WARN);
1132 }
1133 #endif
1134