Skip to content

Commit eca4ba5

Browse files
ptosioupton
authored andcommitted
KVM: arm64: nVHE: Support CONFIG_CFI_CLANG at EL2
The compiler implements kCFI by adding type information (u32) above every function that might be indirectly called and, whenever a function pointer is called, injects a read-and-compare of that u32 against the value corresponding to the expected type. In case of a mismatch, a BRK instruction gets executed. When the hypervisor triggers such an exception in nVHE, it panics and triggers and exception return to EL1. Therefore, teach nvhe_hyp_panic_handler() to detect kCFI errors from the ESR and report them. If necessary, remind the user that EL2 kCFI is not affected by CONFIG_CFI_PERMISSIVE. Pass $(CC_FLAGS_CFI) to the compiler when building the nVHE hyp code. Use SYM_TYPED_FUNC_START() for __pkvm_init_switch_pgd, as nVHE can't call it directly and must use a PA function pointer from C (because it is part of the idmap page), which would trigger a kCFI failure if the type ID wasn't present. Signed-off-by: Pierre-Clément Tosi <[email protected]> Acked-by: Will Deacon <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Oliver Upton <[email protected]>
1 parent 8f3873a commit eca4ba5

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

arch/arm64/kvm/handle_exit.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,14 @@ static void print_nvhe_hyp_panic(const char *name, u64 panic_addr)
417417
(void *)(panic_addr + kaslr_offset()));
418418
}
419419

420+
static void kvm_nvhe_report_cfi_failure(u64 panic_addr)
421+
{
422+
print_nvhe_hyp_panic("CFI failure", panic_addr);
423+
424+
if (IS_ENABLED(CONFIG_CFI_PERMISSIVE))
425+
kvm_err(" (CONFIG_CFI_PERMISSIVE ignored for hyp failures)\n");
426+
}
427+
420428
void __noreturn __cold nvhe_hyp_panic_handler(u64 esr, u64 spsr,
421429
u64 elr_virt, u64 elr_phys,
422430
u64 par, uintptr_t vcpu,
@@ -446,6 +454,8 @@ void __noreturn __cold nvhe_hyp_panic_handler(u64 esr, u64 spsr,
446454
kvm_err("nVHE hyp BUG at: %s:%u!\n", file, line);
447455
else
448456
print_nvhe_hyp_panic("BUG", panic_addr);
457+
} else if (IS_ENABLED(CONFIG_CFI_CLANG) && esr_is_cfi_brk(esr)) {
458+
kvm_nvhe_report_cfi_failure(panic_addr);
449459
} else {
450460
print_nvhe_hyp_panic("panic", panic_addr);
451461
}

arch/arm64/kvm/hyp/nvhe/Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ quiet_cmd_hyprel = HYPREL $@
8989
quiet_cmd_hypcopy = HYPCOPY $@
9090
cmd_hypcopy = $(OBJCOPY) --prefix-symbols=__kvm_nvhe_ $< $@
9191

92-
# Remove ftrace, Shadow Call Stack, and CFI CFLAGS.
93-
# This is equivalent to the 'notrace', '__noscs', and '__nocfi' annotations.
94-
KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_FTRACE) $(CC_FLAGS_SCS) $(CC_FLAGS_CFI), $(KBUILD_CFLAGS))
92+
# Remove ftrace and Shadow Call Stack CFLAGS.
93+
# This is equivalent to the 'notrace' and '__noscs' annotations.
94+
KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_FTRACE) $(CC_FLAGS_SCS), $(KBUILD_CFLAGS))
9595
# Starting from 13.0.0 llvm emits SHT_REL section '.llvm.call-graph-profile'
9696
# when profile optimization is applied. gen-hyprel does not support SHT_REL and
9797
# causes a build failure. Remove profile optimization flags.

arch/arm64/kvm/hyp/nvhe/hyp-init.S

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
#include <linux/arm-smccc.h>
8+
#include <linux/cfi_types.h>
89
#include <linux/linkage.h>
910

1011
#include <asm/alternative.h>
@@ -268,8 +269,11 @@ SYM_CODE_END(__kvm_handle_stub_hvc)
268269
/*
269270
* void __pkvm_init_switch_pgd(phys_addr_t pgd, unsigned long sp,
270271
* void (*fn)(void));
272+
*
273+
* SYM_TYPED_FUNC_START() allows C to call this ID-mapped function indirectly
274+
* using a physical pointer without triggering a kCFI failure.
271275
*/
272-
SYM_FUNC_START(__pkvm_init_switch_pgd)
276+
SYM_TYPED_FUNC_START(__pkvm_init_switch_pgd)
273277
/* Turn the MMU off */
274278
pre_disable_mmu_workaround
275279
mrs x3, sctlr_el2

0 commit comments

Comments
 (0)