Skip to content

Commit 0f15adb

Browse files
wildea01ctmarinas
authored andcommitted
arm64: Add skeleton to harden the branch predictor against aliasing attacks
Aliasing attacks against CPU branch predictors can allow an attacker to redirect speculative control flow on some CPUs and potentially divulge information from one context to another. This patch adds initial skeleton code behind a new Kconfig option to enable implementation-specific mitigations against these attacks for CPUs that are affected. Co-developed-by: Marc Zyngier <marc.zyngier@arm.com> Signed-off-by: Will Deacon <will.deacon@arm.com> Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
1 parent 95e3de3 commit 0f15adb

11 files changed

Lines changed: 215 additions & 3 deletions

File tree

‎arch/arm64/Kconfig‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,23 @@ config UNMAP_KERNEL_AT_EL0
874874

875875
If unsure, say Y.
876876

877+
config HARDEN_BRANCH_PREDICTOR
878+
bool "Harden the branch predictor against aliasing attacks" if EXPERT
879+
default y
880+
help
881+
Speculation attacks against some high-performance processors rely on
882+
being able to manipulate the branch predictor for a victim context by
883+
executing aliasing branches in the attacker context. Such attacks
884+
can be partially mitigated against by clearing internal branch
885+
predictor state and limiting the prediction logic in some situations.
886+
887+
This config option will take CPU-specific actions to harden the
888+
branch predictor against aliasing attacks and may rely on specific
889+
instruction sequences or control bits being set by the system
890+
firmware.
891+
892+
If unsure, say Y.
893+
877894
menuconfig ARMV8_DEPRECATED
878895
bool "Emulate deprecated/obsolete ARMv8 instructions"
879896
depends on COMPAT

‎arch/arm64/include/asm/cpucaps.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
#define ARM64_HAS_DCPOP 21
4343
#define ARM64_SVE 22
4444
#define ARM64_UNMAP_KERNEL_AT_EL0 23
45+
#define ARM64_HARDEN_BRANCH_PREDICTOR 24
4546

46-
#define ARM64_NCAPS 24
47+
#define ARM64_NCAPS 25
4748

4849
#endif /* __ASM_CPUCAPS_H */

‎arch/arm64/include/asm/mmu.h‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,43 @@ static inline bool arm64_kernel_unmapped_at_el0(void)
4141
cpus_have_const_cap(ARM64_UNMAP_KERNEL_AT_EL0);
4242
}
4343

44+
typedef void (*bp_hardening_cb_t)(void);
45+
46+
struct bp_hardening_data {
47+
int hyp_vectors_slot;
48+
bp_hardening_cb_t fn;
49+
};
50+
51+
#ifdef CONFIG_HARDEN_BRANCH_PREDICTOR
52+
extern char __bp_harden_hyp_vecs_start[], __bp_harden_hyp_vecs_end[];
53+
54+
DECLARE_PER_CPU_READ_MOSTLY(struct bp_hardening_data, bp_hardening_data);
55+
56+
static inline struct bp_hardening_data *arm64_get_bp_hardening_data(void)
57+
{
58+
return this_cpu_ptr(&bp_hardening_data);
59+
}
60+
61+
static inline void arm64_apply_bp_hardening(void)
62+
{
63+
struct bp_hardening_data *d;
64+
65+
if (!cpus_have_const_cap(ARM64_HARDEN_BRANCH_PREDICTOR))
66+
return;
67+
68+
d = arm64_get_bp_hardening_data();
69+
if (d->fn)
70+
d->fn();
71+
}
72+
#else
73+
static inline struct bp_hardening_data *arm64_get_bp_hardening_data(void)
74+
{
75+
return NULL;
76+
}
77+
78+
static inline void arm64_apply_bp_hardening(void) { }
79+
#endif /* CONFIG_HARDEN_BRANCH_PREDICTOR */
80+
4481
extern void paging_init(void);
4582
extern void bootmem_init(void);
4683
extern void __iomem *early_io_map(phys_addr_t phys, unsigned long virt);

‎arch/arm64/include/asm/sysreg.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@
439439

440440
/* id_aa64pfr0 */
441441
#define ID_AA64PFR0_CSV3_SHIFT 60
442+
#define ID_AA64PFR0_CSV2_SHIFT 56
442443
#define ID_AA64PFR0_SVE_SHIFT 32
443444
#define ID_AA64PFR0_GIC_SHIFT 24
444445
#define ID_AA64PFR0_ASIMD_SHIFT 20

‎arch/arm64/kernel/Makefile‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ arm64-obj-$(CONFIG_ARM64_RELOC_TEST) += arm64-reloc-test.o
5353
arm64-reloc-test-y := reloc_test_core.o reloc_test_syms.o
5454
arm64-obj-$(CONFIG_CRASH_DUMP) += crash_dump.o
5555

56+
ifeq ($(CONFIG_KVM),y)
57+
arm64-obj-$(CONFIG_HARDEN_BRANCH_PREDICTOR) += bpi.o
58+
endif
59+
5660
obj-y += $(arm64-obj-y) vdso/ probes/
5761
obj-m += $(arm64-obj-m)
5862
head-y := head.o

‎arch/arm64/kernel/bpi.S‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Contains CPU specific branch predictor invalidation sequences
3+
*
4+
* Copyright (C) 2018 ARM Ltd.
5+
*
6+
* This program is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License version 2 as
8+
* published by the Free Software Foundation.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
#include <linux/linkage.h>
20+
21+
.macro ventry target
22+
.rept 31
23+
nop
24+
.endr
25+
b \target
26+
.endm
27+
28+
.macro vectors target
29+
ventry \target + 0x000
30+
ventry \target + 0x080
31+
ventry \target + 0x100
32+
ventry \target + 0x180
33+
34+
ventry \target + 0x200
35+
ventry \target + 0x280
36+
ventry \target + 0x300
37+
ventry \target + 0x380
38+
39+
ventry \target + 0x400
40+
ventry \target + 0x480
41+
ventry \target + 0x500
42+
ventry \target + 0x580
43+
44+
ventry \target + 0x600
45+
ventry \target + 0x680
46+
ventry \target + 0x700
47+
ventry \target + 0x780
48+
.endm
49+
50+
.align 11
51+
ENTRY(__bp_harden_hyp_vecs_start)
52+
.rept 4
53+
vectors __kvm_hyp_vector
54+
.endr
55+
ENTRY(__bp_harden_hyp_vecs_end)

‎arch/arm64/kernel/cpu_errata.c‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,80 @@ static int cpu_enable_trap_ctr_access(void *__unused)
4646
return 0;
4747
}
4848

49+
#ifdef CONFIG_HARDEN_BRANCH_PREDICTOR
50+
#include <asm/mmu_context.h>
51+
#include <asm/cacheflush.h>
52+
53+
DEFINE_PER_CPU_READ_MOSTLY(struct bp_hardening_data, bp_hardening_data);
54+
55+
#ifdef CONFIG_KVM
56+
static void __copy_hyp_vect_bpi(int slot, const char *hyp_vecs_start,
57+
const char *hyp_vecs_end)
58+
{
59+
void *dst = lm_alias(__bp_harden_hyp_vecs_start + slot * SZ_2K);
60+
int i;
61+
62+
for (i = 0; i < SZ_2K; i += 0x80)
63+
memcpy(dst + i, hyp_vecs_start, hyp_vecs_end - hyp_vecs_start);
64+
65+
flush_icache_range((uintptr_t)dst, (uintptr_t)dst + SZ_2K);
66+
}
67+
68+
static void __install_bp_hardening_cb(bp_hardening_cb_t fn,
69+
const char *hyp_vecs_start,
70+
const char *hyp_vecs_end)
71+
{
72+
static int last_slot = -1;
73+
static DEFINE_SPINLOCK(bp_lock);
74+
int cpu, slot = -1;
75+
76+
spin_lock(&bp_lock);
77+
for_each_possible_cpu(cpu) {
78+
if (per_cpu(bp_hardening_data.fn, cpu) == fn) {
79+
slot = per_cpu(bp_hardening_data.hyp_vectors_slot, cpu);
80+
break;
81+
}
82+
}
83+
84+
if (slot == -1) {
85+
last_slot++;
86+
BUG_ON(((__bp_harden_hyp_vecs_end - __bp_harden_hyp_vecs_start)
87+
/ SZ_2K) <= last_slot);
88+
slot = last_slot;
89+
__copy_hyp_vect_bpi(slot, hyp_vecs_start, hyp_vecs_end);
90+
}
91+
92+
__this_cpu_write(bp_hardening_data.hyp_vectors_slot, slot);
93+
__this_cpu_write(bp_hardening_data.fn, fn);
94+
spin_unlock(&bp_lock);
95+
}
96+
#else
97+
static void __install_bp_hardening_cb(bp_hardening_cb_t fn,
98+
const char *hyp_vecs_start,
99+
const char *hyp_vecs_end)
100+
{
101+
__this_cpu_write(bp_hardening_data.fn, fn);
102+
}
103+
#endif /* CONFIG_KVM */
104+
105+
static void install_bp_hardening_cb(const struct arm64_cpu_capabilities *entry,
106+
bp_hardening_cb_t fn,
107+
const char *hyp_vecs_start,
108+
const char *hyp_vecs_end)
109+
{
110+
u64 pfr0;
111+
112+
if (!entry->matches(entry, SCOPE_LOCAL_CPU))
113+
return;
114+
115+
pfr0 = read_cpuid(ID_AA64PFR0_EL1);
116+
if (cpuid_feature_extract_unsigned_field(pfr0, ID_AA64PFR0_CSV2_SHIFT))
117+
return;
118+
119+
__install_bp_hardening_cb(fn, hyp_vecs_start, hyp_vecs_end);
120+
}
121+
#endif /* CONFIG_HARDEN_BRANCH_PREDICTOR */
122+
49123
#define MIDR_RANGE(model, min, max) \
50124
.def_scope = SCOPE_LOCAL_CPU, \
51125
.matches = is_affected_midr_range, \

‎arch/arm64/kernel/cpufeature.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ static const struct arm64_ftr_bits ftr_id_aa64isar1[] = {
147147

148148
static const struct arm64_ftr_bits ftr_id_aa64pfr0[] = {
149149
ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_CSV3_SHIFT, 4, 0),
150+
ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_CSV2_SHIFT, 4, 0),
150151
ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_SVE_SHIFT, 4, 0),
151152
ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_GIC_SHIFT, 4, 0),
152153
S_ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_ASIMD_SHIFT, 4, ID_AA64PFR0_ASIMD_NI),

‎arch/arm64/kernel/entry.S‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -721,12 +721,15 @@ el0_ia:
721721
* Instruction abort handling
722722
*/
723723
mrs x26, far_el1
724-
enable_daif
724+
enable_da_f
725+
#ifdef CONFIG_TRACE_IRQFLAGS
726+
bl trace_hardirqs_off
727+
#endif
725728
ct_user_exit
726729
mov x0, x26
727730
mov x1, x25
728731
mov x2, sp
729-
bl do_mem_abort
732+
bl do_el0_ia_bp_hardening
730733
b ret_to_user
731734
el0_fpsimd_acc:
732735
/*

‎arch/arm64/mm/context.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,8 @@ asmlinkage void post_ttbr_update_workaround(void)
249249
"ic iallu; dsb nsh; isb",
250250
ARM64_WORKAROUND_CAVIUM_27456,
251251
CONFIG_CAVIUM_ERRATUM_27456));
252+
253+
arm64_apply_bp_hardening();
252254
}
253255

254256
static int asids_init(void)

0 commit comments

Comments
 (0)