Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

KVM: arm64: Introduce CONFIG_UBSAN_KVM_EL2

Add a new Kconfig CONFIG_UBSAN_KVM_EL2 for KVM which enables
UBSAN for EL2 code (in protected/nvhe/hvhe) modes.
This will re-use the same checks enabled for the kernel for
the hypervisor. The only difference is that for EL2 it always
emits a "brk" instead of implementing hooks as the hypervisor
can't print reports.

The KVM code will re-use the same code for the kernel
"report_ubsan_failure()" so #ifdefs are changed to also have this
code for CONFIG_UBSAN_KVM_EL2

Signed-off-by: Mostafa Saleh <smostafa@google.com>
Reviewed-by: Kees Cook <kees@kernel.org>
Link: https://lore.kernel.org/r/20250430162713.1997569-4-smostafa@google.com
Signed-off-by: Marc Zyngier <maz@kernel.org>

authored by

Mostafa Saleh and committed by
Marc Zyngier
61b38f75 d683a856

+24 -4
+6
arch/arm64/kvm/hyp/nvhe/Makefile
··· 99 99 # causes a build failure. Remove profile optimization flags. 100 100 KBUILD_CFLAGS := $(filter-out -fprofile-sample-use=% -fprofile-use=%, $(KBUILD_CFLAGS)) 101 101 KBUILD_CFLAGS += -fno-asynchronous-unwind-tables -fno-unwind-tables 102 + 103 + ifeq ($(CONFIG_UBSAN_KVM_EL2),y) 104 + UBSAN_SANITIZE := y 105 + # Always use brk and not hooks 106 + ccflags-y += $(CFLAGS_UBSAN_TRAP) 107 + endif
+1 -1
include/linux/ubsan.h
··· 2 2 #ifndef _LINUX_UBSAN_H 3 3 #define _LINUX_UBSAN_H 4 4 5 - #ifdef CONFIG_UBSAN_TRAP 5 + #if defined(CONFIG_UBSAN_TRAP) || defined(CONFIG_UBSAN_KVM_EL2) 6 6 const char *report_ubsan_failure(u32 check_type); 7 7 #else 8 8 static inline const char *report_ubsan_failure(u32 check_type)
+9
lib/Kconfig.ubsan
··· 165 165 This is a test module for UBSAN. 166 166 It triggers various undefined behavior, and detect it. 167 167 168 + config UBSAN_KVM_EL2 169 + bool "UBSAN for KVM code at EL2" 170 + depends on ARM64 171 + help 172 + Enable UBSAN when running on ARM64 with KVM in a split mode 173 + (nvhe/hvhe/protected) for the hypervisor code running in EL2. 174 + In this mode, any UBSAN violation in EL2 would panic the kernel 175 + and information similar to UBSAN_TRAP would be printed. 176 + 168 177 endif # if UBSAN
+4 -2
lib/ubsan.c
··· 19 19 20 20 #include "ubsan.h" 21 21 22 - #ifdef CONFIG_UBSAN_TRAP 22 + #if defined(CONFIG_UBSAN_TRAP) || defined(CONFIG_UBSAN_KVM_EL2) 23 23 /* 24 24 * Only include matches for UBSAN checks that are actually compiled in. 25 25 * The mappings of struct SanitizerKind (the -fsanitize=xxx args) to ··· 97 97 } 98 98 } 99 99 100 - #else 100 + #endif 101 + 102 + #ifndef CONFIG_UBSAN_TRAP 101 103 static const char * const type_check_kinds[] = { 102 104 "load of", 103 105 "store to",
+4 -1
scripts/Makefile.ubsan
··· 1 1 # SPDX-License-Identifier: GPL-2.0 2 2 3 + # Shared with KVM/arm64. 4 + export CFLAGS_UBSAN_TRAP := $(call cc-option,-fsanitize-trap=undefined,-fsanitize-undefined-trap-on-error) 5 + 3 6 # Enable available and selected UBSAN features. 4 7 ubsan-cflags-$(CONFIG_UBSAN_ALIGNMENT) += -fsanitize=alignment 5 8 ubsan-cflags-$(CONFIG_UBSAN_BOUNDS_STRICT) += -fsanitize=bounds-strict ··· 13 10 ubsan-cflags-$(CONFIG_UBSAN_UNREACHABLE) += -fsanitize=unreachable 14 11 ubsan-cflags-$(CONFIG_UBSAN_BOOL) += -fsanitize=bool 15 12 ubsan-cflags-$(CONFIG_UBSAN_ENUM) += -fsanitize=enum 16 - ubsan-cflags-$(CONFIG_UBSAN_TRAP) += $(call cc-option,-fsanitize-trap=undefined,-fsanitize-undefined-trap-on-error) 13 + ubsan-cflags-$(CONFIG_UBSAN_TRAP) += $(CFLAGS_UBSAN_TRAP) 17 14 18 15 export CFLAGS_UBSAN := $(ubsan-cflags-y) 19 16