Message ID | 20240510112645.3625702-11-ptosi@google.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | KVM: arm64: Add support for hypervisor kCFI | expand |
On Fri, May 10, 2024 at 12:26:39PM +0100, Pierre-Clément Tosi wrote: > 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 <ptosi@google.com> > --- > arch/arm64/include/asm/esr.h | 6 ++++++ > arch/arm64/kvm/handle_exit.c | 11 +++++++++++ > arch/arm64/kvm/hyp/nvhe/Makefile | 6 +++--- > arch/arm64/kvm/hyp/nvhe/hyp-init.S | 6 +++++- > 4 files changed, 25 insertions(+), 4 deletions(-) > > diff --git a/arch/arm64/include/asm/esr.h b/arch/arm64/include/asm/esr.h > index 2bcf216be376..9eb9e6aa70cf 100644 > --- a/arch/arm64/include/asm/esr.h > +++ b/arch/arm64/include/asm/esr.h > @@ -391,6 +391,12 @@ static inline bool esr_is_data_abort(unsigned long esr) > return ec == ESR_ELx_EC_DABT_LOW || ec == ESR_ELx_EC_DABT_CUR; > } > > +static inline bool esr_is_cfi_brk(unsigned long esr) > +{ > + return ESR_ELx_EC(esr) == ESR_ELx_EC_BRK64 && > + (esr_brk_comment(esr) & ~CFI_BRK_IMM_MASK) == CFI_BRK_IMM_BASE; > +} This can now be used by early_brk64(). > static inline bool esr_fsc_is_translation_fault(unsigned long esr) > { > /* Translation fault, level -1 */ > diff --git a/arch/arm64/kvm/handle_exit.c b/arch/arm64/kvm/handle_exit.c > index 0bcafb3179d6..0db23a6304ce 100644 > --- a/arch/arm64/kvm/handle_exit.c > +++ b/arch/arm64/kvm/handle_exit.c > @@ -383,6 +383,15 @@ void handle_exit_early(struct kvm_vcpu *vcpu, int exception_index) > kvm_handle_guest_serror(vcpu, kvm_vcpu_get_esr(vcpu)); > } > > +static void kvm_nvhe_report_cfi_failure(u64 panic_addr) > +{ > + kvm_err("nVHE hyp CFI failure at: [<%016llx>] %pB!\n", panic_addr, > + (void *)(panic_addr + kaslr_offset())); Perhaps add a helper for displaying a hyp panic banner so that we remain consistent? > + > + if (IS_ENABLED(CONFIG_CFI_PERMISSIVE)) > + kvm_err(" (CONFIG_CFI_PERMISSIVE ignored for hyp failures)\n"); > +} > + > void __noreturn __cold nvhe_hyp_panic_handler(u64 esr, u64 spsr, > u64 elr_virt, u64 elr_phys, > u64 par, uintptr_t vcpu, > @@ -413,6 +422,8 @@ void __noreturn __cold nvhe_hyp_panic_handler(u64 esr, u64 spsr, > else > kvm_err("nVHE hyp BUG at: [<%016llx>] %pB!\n", panic_addr, > (void *)(panic_addr + kaslr_offset())); > + } else if (IS_ENABLED(CONFIG_CFI_CLANG) && esr_is_cfi_brk(esr)) { > + kvm_nvhe_report_cfi_failure(panic_addr); > } else { > kvm_err("nVHE hyp panic at: [<%016llx>] %pB!\n", panic_addr, > (void *)(panic_addr + kaslr_offset())); > diff --git a/arch/arm64/kvm/hyp/nvhe/Makefile b/arch/arm64/kvm/hyp/nvhe/Makefile > index 2250253a6429..2eb915d8943f 100644 > --- a/arch/arm64/kvm/hyp/nvhe/Makefile > +++ b/arch/arm64/kvm/hyp/nvhe/Makefile > @@ -89,9 +89,9 @@ quiet_cmd_hyprel = HYPREL $@ > quiet_cmd_hypcopy = HYPCOPY $@ > cmd_hypcopy = $(OBJCOPY) --prefix-symbols=__kvm_nvhe_ $< $@ > > -# Remove ftrace, Shadow Call Stack, and CFI CFLAGS. > -# This is equivalent to the 'notrace', '__noscs', and '__nocfi' annotations. > -KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_FTRACE) $(CC_FLAGS_SCS) $(CC_FLAGS_CFI), $(KBUILD_CFLAGS)) > +# Remove ftrace and Shadow Call Stack CFLAGS. > +# This is equivalent to the 'notrace' and '__noscs' annotations. > +KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_FTRACE) $(CC_FLAGS_SCS), $(KBUILD_CFLAGS)) > # Starting from 13.0.0 llvm emits SHT_REL section '.llvm.call-graph-profile' > # when profile optimization is applied. gen-hyprel does not support SHT_REL and > # causes a build failure. Remove profile optimization flags. > diff --git a/arch/arm64/kvm/hyp/nvhe/hyp-init.S b/arch/arm64/kvm/hyp/nvhe/hyp-init.S > index 5a15737b4233..33fb5732ab83 100644 > --- a/arch/arm64/kvm/hyp/nvhe/hyp-init.S > +++ b/arch/arm64/kvm/hyp/nvhe/hyp-init.S > @@ -5,6 +5,7 @@ > */ > > #include <linux/arm-smccc.h> > +#include <linux/cfi_types.h> > #include <linux/linkage.h> > > #include <asm/alternative.h> > @@ -268,8 +269,11 @@ SYM_CODE_END(__kvm_handle_stub_hvc) > /* > * void __pkvm_init_switch_pgd(struct kvm_nvhe_init_params *params, > * void (*finalize_fn)(void)); > + * > + * SYM_TYPED_FUNC_START() allows C to call this ID-mapped function indirectly > + * using a physical pointer without triggering a kCFI failure. > */ > -SYM_FUNC_START(__pkvm_init_switch_pgd) > +SYM_TYPED_FUNC_START(__pkvm_init_switch_pgd) > /* Load the inputs from the VA pointer before turning the MMU off */ > ldr x5, [x0, #NVHE_INIT_PGD_PA] > ldr x0, [x0, #NVHE_INIT_STACK_HYP_VA] Unrelated hunk? Will
Hi Will, On Mon, May 13, 2024 at 06:30:05PM +0100, Will Deacon wrote: > On Fri, May 10, 2024 at 12:26:39PM +0100, Pierre-Clément Tosi wrote: > > [...] > > > > 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 <ptosi@google.com> > > --- > > arch/arm64/include/asm/esr.h | 6 ++++++ > > arch/arm64/kvm/handle_exit.c | 11 +++++++++++ > > arch/arm64/kvm/hyp/nvhe/Makefile | 6 +++--- > > arch/arm64/kvm/hyp/nvhe/hyp-init.S | 6 +++++- > > 4 files changed, 25 insertions(+), 4 deletions(-) > > > > [...] > > > > diff --git a/arch/arm64/kvm/hyp/nvhe/hyp-init.S b/arch/arm64/kvm/hyp/nvhe/hyp-init.S > > index 5a15737b4233..33fb5732ab83 100644 > > --- a/arch/arm64/kvm/hyp/nvhe/hyp-init.S > > +++ b/arch/arm64/kvm/hyp/nvhe/hyp-init.S > > @@ -5,6 +5,7 @@ > > */ > > > > #include <linux/arm-smccc.h> > > +#include <linux/cfi_types.h> > > #include <linux/linkage.h> > > > > #include <asm/alternative.h> > > @@ -268,8 +269,11 @@ SYM_CODE_END(__kvm_handle_stub_hvc) > > /* > > * void __pkvm_init_switch_pgd(struct kvm_nvhe_init_params *params, > > * void (*finalize_fn)(void)); > > + * > > + * SYM_TYPED_FUNC_START() allows C to call this ID-mapped function indirectly > > + * using a physical pointer without triggering a kCFI failure. > > */ > > -SYM_FUNC_START(__pkvm_init_switch_pgd) > > +SYM_TYPED_FUNC_START(__pkvm_init_switch_pgd) > > /* Load the inputs from the VA pointer before turning the MMU off */ > > ldr x5, [x0, #NVHE_INIT_PGD_PA] > > ldr x0, [x0, #NVHE_INIT_STACK_HYP_VA] > > Unrelated hunk? No, this is needed to prevent a kCFI failure at EL2. Please let me know if the comment and commit message aren't clear enough. > > Will
On Wed, May 29, 2024 at 01:30:15PM +0100, Pierre-Clément Tosi wrote: > On Mon, May 13, 2024 at 06:30:05PM +0100, Will Deacon wrote: > > On Fri, May 10, 2024 at 12:26:39PM +0100, Pierre-Clément Tosi wrote: > > > [...] > > > > > > 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 <ptosi@google.com> > > > --- > > > arch/arm64/include/asm/esr.h | 6 ++++++ > > > arch/arm64/kvm/handle_exit.c | 11 +++++++++++ > > > arch/arm64/kvm/hyp/nvhe/Makefile | 6 +++--- > > > arch/arm64/kvm/hyp/nvhe/hyp-init.S | 6 +++++- > > > 4 files changed, 25 insertions(+), 4 deletions(-) > > > > > > [...] > > > > > > diff --git a/arch/arm64/kvm/hyp/nvhe/hyp-init.S b/arch/arm64/kvm/hyp/nvhe/hyp-init.S > > > index 5a15737b4233..33fb5732ab83 100644 > > > --- a/arch/arm64/kvm/hyp/nvhe/hyp-init.S > > > +++ b/arch/arm64/kvm/hyp/nvhe/hyp-init.S > > > @@ -5,6 +5,7 @@ > > > */ > > > > > > #include <linux/arm-smccc.h> > > > +#include <linux/cfi_types.h> > > > #include <linux/linkage.h> > > > > > > #include <asm/alternative.h> > > > @@ -268,8 +269,11 @@ SYM_CODE_END(__kvm_handle_stub_hvc) > > > /* > > > * void __pkvm_init_switch_pgd(struct kvm_nvhe_init_params *params, > > > * void (*finalize_fn)(void)); > > > + * > > > + * SYM_TYPED_FUNC_START() allows C to call this ID-mapped function indirectly > > > + * using a physical pointer without triggering a kCFI failure. > > > */ > > > -SYM_FUNC_START(__pkvm_init_switch_pgd) > > > +SYM_TYPED_FUNC_START(__pkvm_init_switch_pgd) > > > /* Load the inputs from the VA pointer before turning the MMU off */ > > > ldr x5, [x0, #NVHE_INIT_PGD_PA] > > > ldr x0, [x0, #NVHE_INIT_STACK_HYP_VA] > > > > Unrelated hunk? > > No, this is needed to prevent a kCFI failure at EL2. > > Please let me know if the comment and commit message aren't clear enough. I'm not disputing that this hunk is needed, but I think it should be in place before the patch enabling CFI. For example, by merging it in with patch 2? Will
diff --git a/arch/arm64/include/asm/esr.h b/arch/arm64/include/asm/esr.h index 2bcf216be376..9eb9e6aa70cf 100644 --- a/arch/arm64/include/asm/esr.h +++ b/arch/arm64/include/asm/esr.h @@ -391,6 +391,12 @@ static inline bool esr_is_data_abort(unsigned long esr) return ec == ESR_ELx_EC_DABT_LOW || ec == ESR_ELx_EC_DABT_CUR; } +static inline bool esr_is_cfi_brk(unsigned long esr) +{ + return ESR_ELx_EC(esr) == ESR_ELx_EC_BRK64 && + (esr_brk_comment(esr) & ~CFI_BRK_IMM_MASK) == CFI_BRK_IMM_BASE; +} + static inline bool esr_fsc_is_translation_fault(unsigned long esr) { /* Translation fault, level -1 */ diff --git a/arch/arm64/kvm/handle_exit.c b/arch/arm64/kvm/handle_exit.c index 0bcafb3179d6..0db23a6304ce 100644 --- a/arch/arm64/kvm/handle_exit.c +++ b/arch/arm64/kvm/handle_exit.c @@ -383,6 +383,15 @@ void handle_exit_early(struct kvm_vcpu *vcpu, int exception_index) kvm_handle_guest_serror(vcpu, kvm_vcpu_get_esr(vcpu)); } +static void kvm_nvhe_report_cfi_failure(u64 panic_addr) +{ + kvm_err("nVHE hyp CFI failure at: [<%016llx>] %pB!\n", panic_addr, + (void *)(panic_addr + kaslr_offset())); + + if (IS_ENABLED(CONFIG_CFI_PERMISSIVE)) + kvm_err(" (CONFIG_CFI_PERMISSIVE ignored for hyp failures)\n"); +} + void __noreturn __cold nvhe_hyp_panic_handler(u64 esr, u64 spsr, u64 elr_virt, u64 elr_phys, u64 par, uintptr_t vcpu, @@ -413,6 +422,8 @@ void __noreturn __cold nvhe_hyp_panic_handler(u64 esr, u64 spsr, else kvm_err("nVHE hyp BUG at: [<%016llx>] %pB!\n", panic_addr, (void *)(panic_addr + kaslr_offset())); + } else if (IS_ENABLED(CONFIG_CFI_CLANG) && esr_is_cfi_brk(esr)) { + kvm_nvhe_report_cfi_failure(panic_addr); } else { kvm_err("nVHE hyp panic at: [<%016llx>] %pB!\n", panic_addr, (void *)(panic_addr + kaslr_offset())); diff --git a/arch/arm64/kvm/hyp/nvhe/Makefile b/arch/arm64/kvm/hyp/nvhe/Makefile index 2250253a6429..2eb915d8943f 100644 --- a/arch/arm64/kvm/hyp/nvhe/Makefile +++ b/arch/arm64/kvm/hyp/nvhe/Makefile @@ -89,9 +89,9 @@ quiet_cmd_hyprel = HYPREL $@ quiet_cmd_hypcopy = HYPCOPY $@ cmd_hypcopy = $(OBJCOPY) --prefix-symbols=__kvm_nvhe_ $< $@ -# Remove ftrace, Shadow Call Stack, and CFI CFLAGS. -# This is equivalent to the 'notrace', '__noscs', and '__nocfi' annotations. -KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_FTRACE) $(CC_FLAGS_SCS) $(CC_FLAGS_CFI), $(KBUILD_CFLAGS)) +# Remove ftrace and Shadow Call Stack CFLAGS. +# This is equivalent to the 'notrace' and '__noscs' annotations. +KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_FTRACE) $(CC_FLAGS_SCS), $(KBUILD_CFLAGS)) # Starting from 13.0.0 llvm emits SHT_REL section '.llvm.call-graph-profile' # when profile optimization is applied. gen-hyprel does not support SHT_REL and # causes a build failure. Remove profile optimization flags. diff --git a/arch/arm64/kvm/hyp/nvhe/hyp-init.S b/arch/arm64/kvm/hyp/nvhe/hyp-init.S index 5a15737b4233..33fb5732ab83 100644 --- a/arch/arm64/kvm/hyp/nvhe/hyp-init.S +++ b/arch/arm64/kvm/hyp/nvhe/hyp-init.S @@ -5,6 +5,7 @@ */ #include <linux/arm-smccc.h> +#include <linux/cfi_types.h> #include <linux/linkage.h> #include <asm/alternative.h> @@ -268,8 +269,11 @@ SYM_CODE_END(__kvm_handle_stub_hvc) /* * void __pkvm_init_switch_pgd(struct kvm_nvhe_init_params *params, * void (*finalize_fn)(void)); + * + * SYM_TYPED_FUNC_START() allows C to call this ID-mapped function indirectly + * using a physical pointer without triggering a kCFI failure. */ -SYM_FUNC_START(__pkvm_init_switch_pgd) +SYM_TYPED_FUNC_START(__pkvm_init_switch_pgd) /* Load the inputs from the VA pointer before turning the MMU off */ ldr x5, [x0, #NVHE_INIT_PGD_PA] ldr x0, [x0, #NVHE_INIT_STACK_HYP_VA]
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 <ptosi@google.com> --- arch/arm64/include/asm/esr.h | 6 ++++++ arch/arm64/kvm/handle_exit.c | 11 +++++++++++ arch/arm64/kvm/hyp/nvhe/Makefile | 6 +++--- arch/arm64/kvm/hyp/nvhe/hyp-init.S | 6 +++++- 4 files changed, 25 insertions(+), 4 deletions(-)