xen/arm32: Branch predictor hardening (XSA-254 variant 2)
v4 cover xen/arm32: Branch predictor hardening (XSA-254 variant 2)
Hi all,
This series provides a skeleton for mitigating branch predictor hardening for arm32 on exception entry.
It also implements mitigation for Cortex-A12, A15 and A17. SoC vendors with affected CPUs are strongly encouraged to update.
For more information about the impact of this issue and the software mitigations for Arm processors, please see http://www.arm.com/security-update.
Cheers,
Julien Grall (7):
xen/arm32: entry: Consolidate DEFINE_TRAP_ENTRY_* macros xen/arm32: Add missing MIDR values for Cortex-A17 and A12 xen/arm32: entry: Add missing trap_reset entry xen/arm32: Add skeleton to harden branch predictor aliasing attacks xen/arm32: Invalidate BTB on guest exit for Cortex A17 and 12 xen/arm32: Invalidate icache on guest exist for Cortex-A15 xen/arm32: entry: Document the purpose of r11 in the traps handler
xen/arch/arm/Kconfig | 3 + xen/arch/arm/arm32/entry.S | 147 +++++++++++++++++++++++++++++++++------- xen/arch/arm/arm32/traps.c | 5 ++ xen/arch/arm/cpuerrata.c | 62 +++++++++++++++++ xen/include/asm-arm/processor.h | 4 ++ 5 files changed, 196 insertions(+), 25 deletions(-)
Signature · 7 lines
2.11.0 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v4 1/7 xen/arm32: entry: Consolidate DEFINE_TRAP_ENTRY_* macros
The only difference between all the DEFINE_TRAP_ENTRY_* macros are the interrupts (Asynchronous Abort, IRQ, FIQ) unmasked.
Rather than duplicating the code, introduce __DEFINE_TRAP_ENTRY macro that will take the list of interrupts to unmask.
This is part of XSA-254.
---
Changes in v2:
- Add Stefano's reviewed-by
--- xen/arch/arm/arm32/entry.S | 36 +++++++++++++----------------------- 1 file changed, 13 insertions(+), 23 deletions(-)
xen/arch/arm/arm32/entry.S +13 -23
@@ -111,39 +111,29 @@ abort_guest_exit_end: skip_check: mov pc, lr -#define DEFINE_TRAP_ENTRY(trap) \ +/* + * Macro to define trap entry. The iflags corresponds to the list of + * interrupts (Asynchronous Abort, IRQ, FIQ) to unmask. + */ +#define __DEFINE_TRAP_ENTRY(trap, iflags) \ ALIGN; \ trap_##trap: \ SAVE_ALL; \ - cpsie i; /* local_irq_enable */ \ - cpsie a; /* asynchronous abort enable */ \ + cpsie iflags; \ adr lr, return_from_trap; \ mov r0, sp; \ mov r11, sp; \ bic sp, #7; /* Align the stack pointer (noop on guest trap) */ \ b do_trap_##trap -#define DEFINE_TRAP_ENTRY_NOIRQ(trap) \ - ALIGN; \ -trap_##trap: \ - SAVE_ALL; \ - cpsie a; /* asynchronous abort enable */ \ - adr lr, return_from_trap; \ - mov r0, sp; \ - mov r11, sp; \ - bic sp, #7; /* Align the stack pointer (noop on guest trap) */ \ - b do_trap_##trap +/* Trap handler which unmask IRQ/Abort, keep FIQ masked */ +#define DEFINE_TRAP_ENTRY(trap) __DEFINE_TRAP_ENTRY(trap, ai) -#define DEFINE_TRAP_ENTRY_NOABORT(trap) \ - ALIGN; \ -trap_##trap: \ - SAVE_ALL; \ - cpsie i; /* local_irq_enable */ \ - adr lr, return_from_trap; \ - mov r0, sp; \ - mov r11, sp; \ - bic sp, #7; /* Align the stack pointer (noop on guest trap) */ \ - b do_trap_##trap +/* Trap handler which unmask Abort, keep IRQ/FIQ masked */ +#define DEFINE_TRAP_ENTRY_NOIRQ(trap) __DEFINE_TRAP_ENTRY(trap, a) + +/* Trap handler which unmask IRQ, keep Abort/FIQ masked */ +#define DEFINE_TRAP_ENTRY_NOABORT(trap) __DEFINE_TRAP_ENTRY(trap, i) .align 5 GLOBAL(hyp_traps_vector)
Signature · 7 lines
2.11.0 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v4 2/7 xen/arm32: Add missing MIDR values for Cortex-A17 and A12
In reply to Julien GrallCortex-A17 and A12 MIDR will be used in a follow-up patch for hardening the branch predictor.
This is part of XSA-254.
---
Changes in v2:
- Add Stefano's reviewed-by
--- xen/include/asm-arm/processor.h | 4 ++++ 1 file changed, 4 insertions(+)
xen/include/asm-arm/processor.h +4 -0
@@ -44,6 +44,8 @@#define ARM_CPU_IMP_ARM 0x41 +#define ARM_CPU_PART_CORTEX_A12 0xC0D +#define ARM_CPU_PART_CORTEX_A17 0xC0E#define ARM_CPU_PART_CORTEX_A15 0xC0F#define ARM_CPU_PART_CORTEX_A53 0xD03#define ARM_CPU_PART_CORTEX_A57 0xD07
@@ -51,6 +53,8 @@#define ARM_CPU_PART_CORTEX_A73 0xD09#define ARM_CPU_PART_CORTEX_A75 0xD0A +#define MIDR_CORTEX_A12 MIDR_CPU_MODEL(ARM_CPU_IMP_ARM, ARM_CPU_PART_CORTEX_A12) +#define MIDR_CORTEX_A17 MIDR_CPU_MODEL(ARM_CPU_IMP_ARM, ARM_CPU_PART_CORTEX_A17)#define MIDR_CORTEX_A15 MIDR_CPU_MODEL(ARM_CPU_IMP_ARM, ARM_CPU_PART_CORTEX_A15)#define MIDR_CORTEX_A53 MIDR_CPU_MODEL(ARM_CPU_IMP_ARM, ARM_CPU_PART_CORTEX_A53)#define MIDR_CORTEX_A57 MIDR_CPU_MODEL(ARM_CPU_IMP_ARM, ARM_CPU_PART_CORTEX_A57)
Signature · 7 lines
2.11.0 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v4 3/7 xen/arm32: entry: Add missing trap_reset entry
In reply to Julien GrallAt the moment, the reset vector is defined as .word 0 (e.g andeq r0, r0, r0).
This is rather unintuitive and will result to execute the trap undefined. Instead introduce trap helpers for reset and will generate an error message in the unlikely case that reset will be called.
This is part of XSA-254.
---
Changes in v2:
- Replace .word 0 by trap_reset
--- xen/arch/arm/arm32/entry.S | 3 ++- xen/arch/arm/arm32/traps.c | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-)
xen/arch/arm/arm32/entry.S +2 -1
@@ -137,7 +137,7 @@ trap_##trap: \ .align 5 GLOBAL(hyp_traps_vector) - .word 0 /* 0x00 - Reset */ + b trap_reset /* 0x00 - Reset */ b trap_undefined_instruction /* 0x04 - Undefined Instruction */ b trap_hypervisor_call /* 0x08 - Hypervisor Call */ b trap_prefetch_abort /* 0x0c - Prefetch Abort */
@@ -146,6 +146,7 @@ GLOBAL(hyp_traps_vector) b trap_irq /* 0x18 - IRQ */ b trap_fiq /* 0x1c - FIQ */ +DEFINE_TRAP_ENTRY(reset) DEFINE_TRAP_ENTRY(undefined_instruction) DEFINE_TRAP_ENTRY(hypervisor_call) DEFINE_TRAP_ENTRY(prefetch_abort)
xen/arch/arm/arm32/traps.c +5 -0
@@ -23,6 +23,11 @@#include <asm/processor.h> +void do_trap_reset (struct cpu_user_regs *regs) +{ +do_unexpected_trap ("Reset" , regs); +} +void do_trap_undefined_instruction (struct cpu_user_regs *regs) {uint32_t pc = regs->pc;
Signature · 7 lines
2.11.0 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v4 4/7 xen/arm32: Add skeleton to harden branch predictor aliasing attacks
In reply to Julien GrallAliasing attacked 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 initiatial skeleton code behind a new Kconfig option to enable implementation-specific mitigations against these attacks for CPUs that are affected.
Most of mitigations will have to be applied when entering to the hypervisor from the guest context.
Because the attack is against branch predictor, it is not possible to safely use branch instruction before the mitigation is applied. Therefore this has to be done in the vector entry before jump to the helper handling a given exception.
However, on arm32, each vector contain a single instruction. This means that the hardened vector tables may rely on the state of registers that does not hold when in the hypervisor (e.g SP is 8 bytes aligned). Therefore hypervisor code running with guest vectors table should be minimized and always have IRQs and SErrors masked to reduce the risk to use them.
This patch provides an infrastructure to switch vector tables before entering to the guest and when leaving it.
Note that alternative could have been used, but older Xen (4.8 or earlier) doesn't have support. So avoid using alternative to ease backporting.
This is part of XSA-254.
---
Changes in v2:
- Clarify the commit message
--- xen/arch/arm/Kconfig | 3 +++ xen/arch/arm/arm32/entry.S | 41 ++++++++++++++++++++++++++++++++++++++++- xen/arch/arm/cpuerrata.c | 30 ++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-)
xen/arch/arm/Kconfig +3 -0
@@ -191,6 +191,9 @@ config HARDEN_BRANCH_PREDICTOR config ARM64_HARDEN_BRANCH_PREDICTOR def_bool y if ARM_64 && HARDEN_BRANCH_PREDICTOR +config ARM32_HARDEN_BRANCH_PREDICTOR + def_bool y if ARM_32 && HARDEN_BRANCH_PREDICTOR + source "common/Kconfig" source "drivers/Kconfig"
xen/arch/arm/arm32/entry.S +40 -1
58 lines not on this page · raw message
xen/arch/arm/cpuerrata.c +30 -0
@@ -170,6 +170,36 @@ static int enable_psci_bp_hardening(void *data)#endif /* CONFIG_ARM64_HARDEN_BRANCH_PREDICTOR */ +/* Hardening Branch predictor code for Arm32 */ +#ifdef CONFIG_ARM32_HARDEN_BRANCH_PREDICTOR + +/* +* Per-CPU vector tables to use when returning to the guests. They will +* only be used on platform requiring to harden the branch predictor. +*/ +DEFINE_PER_CPU_READ_MOSTLY(const char *, bp_harden_vecs); + +extern char hyp_traps_vector_bp_inv[]; + +static void __maybe_unused +install_bp_hardening_vecs (const struct arm_cpu_capabilities *entry, +const char *hyp_vecs,const char *desc) +{ +/* +* Enable callbacks are called on every CPU based on the +* capabilities. So double-check whether the CPU matches the +* entry. +*/ +if ( !entry->matches (entry) ) +return ; + +printk (XENLOG_INFO "CPU%u will %s on guest exit\n" , +smp_processor_id (), desc); +this_cpu (bp_harden_vecs) = hyp_vecs; +} + +#endif +#define MIDR_RANGE (model, min, max) \ .matches = is_affected_midr_range, \ .midr_model = model, \
Signature · 7 lines
2.11.0 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v4 5/7 xen/arm32: Invalidate BTB on guest exit for Cortex A17 and 12
In reply to Julien GrallIn order to avoid aliasing attackes agains the branch predictor, let's invalidate the BTB on guest exist. This is made complicated by the fact that we cannot take a branch invalidating the BTB.
This is based on the fourth version posted by Marc Zyngier on Linux-arm mailing list (see [1]).
This is part of XSA-254.
[1] https://www.spinics.net/lists/arm-kernel/msg632062.html
---
Changes in v3:
- Drop Stefano's reviewed-by
- Use the latest version of the Linux patch. This will improve code readability.
Changes in v2:
- Add Stefano's reviewed-by
--- xen/arch/arm/arm32/entry.S | 38 ++++++++++++++++++++++++++++++++++++++ xen/arch/arm/cpuerrata.c | 19 +++++++++++++++++++ 2 files changed, 57 insertions(+)
xen/arch/arm/arm32/entry.S +38 -0
@@ -160,6 +160,44 @@ GLOBAL(hyp_traps_vector) b trap_irq /* 0x18 - IRQ */ b trap_fiq /* 0x1c - FIQ */ +#ifdef CONFIG_HARDEN_BRANCH_PREDICTOR + + .align 5 +GLOBAL(hyp_traps_vector_bp_inv) + /* + * We encode the exception entry in the bottom 3 bits of + * SP, and we have to guarantee to be 8 bytes aligned. + */ + add sp, sp, #1 /* Reset 7 */ + add sp, sp, #1 /* Undef 6 */ + add sp, sp, #1 /* Hypervisor Call 5 */ + add sp, sp, #1 /* Prefetch abort 4 */ + add sp, sp, #1 /* Data abort 3 */ + add sp, sp, #1 /* Hypervisor 2 */ + add sp, sp, #1 /* IRQ 1 */ + nop /* FIQ 0 */ + + mcr p15, 0, r0, c7, c5, 6 /* BPIALL */ + isb + +.macro vect_br val, targ + eor sp, sp, #\val + tst sp, #7 + eorne sp, sp, #\val + beq \targ +.endm + + vect_br 0, trap_fiq + vect_br 1, trap_irq + vect_br 2, trap_guest_sync + vect_br 3, trap_data_abort + vect_br 4, trap_prefetch_abort + vect_br 5, trap_hypervisor_call + vect_br 6, trap_undefined_instruction + vect_br 7, trap_reset + +#endif /* CONFIG_HARDEN_BRANCH_PREDICTOR */ + DEFINE_TRAP_ENTRY(reset) DEFINE_TRAP_ENTRY(undefined_instruction) DEFINE_TRAP_ENTRY(hypervisor_call)
xen/arch/arm/cpuerrata.c +19 -0
@@ -198,6 +198,13 @@ install_bp_hardening_vecs(const struct arm_cpu_capabilities *entry, this_cpu(bp_harden_vecs) = hyp_vecs; } +static int enable_bp_inv_hardening (void *data) +{ +install_bp_hardening_vecs (data, hyp_traps_vector_bp_inv, +"execute BPIALL" ); +return 0 ; +} +#endif #define MIDR_RANGE (model, min, max) \
@@ -284,6 +291,18 @@ static const struct arm_cpu_capabilities arm_errata[] = { .enable = enable_psci_bp_hardening, },#endif +#ifdef CONFIG_ARM32_HARDEN_BRANCH_PREDICTOR + { + .capability = ARM_HARDEN_BRANCH_PREDICTOR, + MIDR_ALL_VERSIONS(MIDR_CORTEX_A12), + .enable = enable_bp_inv_hardening, + }, + { + .capability = ARM_HARDEN_BRANCH_PREDICTOR, + MIDR_ALL_VERSIONS(MIDR_CORTEX_A17), + .enable = enable_bp_inv_hardening, + }, +#endif {}, };
Signature · 7 lines
2.11.0 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v4 6/7 xen/arm32: Invalidate icache on guest exist for Cortex-A15
In reply to Julien GrallIn order to avoid aliasing attacks against the branch predictor on Cortex A-15, let's invalidate the BTB on guest exit, which can only be done by invalidating the icache (with ACTLR[0] being set).
We use the same hack as for A12/A17 to perform the vector decoding.
This is based on Linux patch from the kpti branch in [1].
[1] https://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux.git
---
Changes in v2:
- Add Stefano's reviewed-by
--- xen/arch/arm/arm32/entry.S | 21 +++++++++++++++++++++ xen/arch/arm/cpuerrata.c | 13 +++++++++++++ 2 files changed, 34 insertions(+)
xen/arch/arm/arm32/entry.S +21 -0
@@ -163,6 +163,26 @@ GLOBAL(hyp_traps_vector) #ifdef CONFIG_HARDEN_BRANCH_PREDICTOR .align 5 +GLOBAL(hyp_traps_vector_ic_inv) + /* + * We encode the exception entry in the bottom 3 bits of + * SP, and we have to guarantee to be 8 bytes aligned. + */ + add sp, sp, #1 /* Reset 7 */ + add sp, sp, #1 /* Undef 6 */ + add sp, sp, #1 /* Hypervisor call 5 */ + add sp, sp, #1 /* Prefetch abort 4 */ + add sp, sp, #1 /* Data abort 3 */ + add sp, sp, #1 /* Hypervisor 2 */ + add sp, sp, #1 /* IRQ 1 */ + nop /* FIQ 0 */ + + mcr p15, 0, r0, c7, c5, 0 /* ICIALLU */ + isb + + b decode_vectors + + .align 5 GLOBAL(hyp_traps_vector_bp_inv) /* * We encode the exception entry in the bottom 3 bits of
@@ -180,6 +200,7 @@ GLOBAL(hyp_traps_vector_bp_inv) mcr p15, 0, r0, c7, c5, 6 /* BPIALL */ isb +decode_vectors: .macro vect_br val, targ eor sp, sp, #\val tst sp, #7
xen/arch/arm/cpuerrata.c +13 -0
@@ -180,6 +180,7 @@ static int enable_psci_bp_hardening(void *data) DEFINE_PER_CPU_READ_MOSTLY(const char *, bp_harden_vecs);extern char hyp_traps_vector_bp_inv[]; +extern char hyp_traps_vector_ic_inv[]; static void __maybe_unused install_bp_hardening_vecs(const struct arm_cpu_capabilities *entry,
@@ -205,6 +206,13 @@ static int enable_bp_inv_hardening(void *data) return 0; } +static int enable_ic_inv_hardening (void *data) +{ +install_bp_hardening_vecs (data, hyp_traps_vector_ic_inv, +"execute ICIALLU" ); +return 0 ; +} +#endif #define MIDR_RANGE (model, min, max) \
@@ -302,6 +310,11 @@ static const struct arm_cpu_capabilities arm_errata[] = { MIDR_ALL_VERSIONS(MIDR_CORTEX_A17), .enable = enable_bp_inv_hardening, }, + { + .capability =ARM_HARDEN_BRANCH_PREDICTOR , +MIDR_ALL_VERSIONS (MIDR_CORTEX_A15 ), + .enable = enable_ic_inv_hardening, + }, #endif {}, };
Signature · 7 lines
2.11.0 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v4 7/7 xen/arm32: entry: Document the purpose of r11 in the traps handler
In reply to Julien GrallIt took me a bit of time to understand why __DEFINE_TRAP_ENTRY is storing the original stack pointer in r11. It is working in pair with return_traps_entry where sp will be restored from r11.
This is fine because per the AAPCS r11 must be preserved by the subroutine. So in return_from_trap, r11 will still contain the original stack pointer.
Add some documentation in the code to point the 2 sides to each other.
---
Changes in v2:
- Add Stefano's reviewed-by
--- xen/arch/arm/arm32/entry.S | 8 ++++++++ 1 file changed, 8 insertions(+)
xen/arch/arm/arm32/entry.S +8 -0
@@ -136,6 +136,10 @@ trap_##trap: \ cpsie iflags; \ adr lr, return_from_trap; \ mov r0, sp; \ + /* \ + * Save the stack pointer in r11. It will be restored after the \ + * trap has been handled (see return_from_trap). \ + */ \ mov r11, sp; \ bic sp, #7; /* Align the stack pointer (noop on guest trap) */ \ b do_trap_##trap
@@ -229,6 +233,10 @@ DEFINE_TRAP_ENTRY_NOIRQ(fiq) DEFINE_TRAP_ENTRY_NOABORT(data_abort) return_from_trap: + /* + * Restore the stack pointer from r11. It was saved on exception + * entry (see __DEFINE_TRAP_ENTRY). + */ mov sp, r11 ENTRY(return_to_new_vcpu32) ldr r11, [sp, #UREGS_cpsr]
Signature · 7 lines
2.11.0 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Committed, thanks
Quoted 22 lines · Julien Grall
Hi all,
This series provides a skeleton for mitigating branch predictor hardening for arm32 on exception entry.
It also implements mitigation for Cortex-A12, A15 and A17. SoC vendors with affected CPUs are strongly encouraged to update.
For more information about the impact of this issue and the software mitigations for Arm processors, please see http://www.arm.com/security-update.
Cheers,
Julien Grall (7):
xen/arm32: entry: Consolidate DEFINE_TRAP_ENTRY_* macros xen/arm32: Add missing MIDR values for Cortex-A17 and A12 xen/arm32: entry: Add missingtrap_resetentry xen/arm32: Add skeleton to harden branch predictor aliasing attacks xen/arm32: Invalidate BTB on guest exit for Cortex A17 and 12 xen/arm32: Invalidate icache on guest exist for Cortex-A15 xen/arm32: entry: Document the purpose of r11 in the traps handler
Quoted 10 lines
xen/arch/arm/Kconfig | 3 + xen/arch/arm/arm32/entry.S | 147 +++++++++++++++++++++++++++++++++------- xen/arch/arm/arm32/traps.c | 5 ++ xen/arch/arm/cpuerrata.c | 62 +++++++++++++++++ xen/include/asm-arm/processor.h | 4 ++ 5 files changed, 196 insertions(+), 25 deletions(-)Signature · 1 line
2.11.0
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel