Skip to content

Commit 61b38f7

Browse files
misalehMarc Zyngier
authored andcommitted
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>
1 parent d683a85 commit 61b38f7

File tree

5 files changed

+24
-4
lines changed

5 files changed

+24
-4
lines changed

arch/arm64/kvm/hyp/nvhe/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,9 @@ KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_FTRACE) $(CC_FLAGS_SCS), $(KBUILD_CFLAG
9999
# causes a build failure. Remove profile optimization flags.
100100
KBUILD_CFLAGS := $(filter-out -fprofile-sample-use=% -fprofile-use=%, $(KBUILD_CFLAGS))
101101
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

include/linux/ubsan.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#ifndef _LINUX_UBSAN_H
33
#define _LINUX_UBSAN_H
44

5-
#ifdef CONFIG_UBSAN_TRAP
5+
#if defined(CONFIG_UBSAN_TRAP) || defined(CONFIG_UBSAN_KVM_EL2)
66
const char *report_ubsan_failure(u32 check_type);
77
#else
88
static inline const char *report_ubsan_failure(u32 check_type)

lib/Kconfig.ubsan

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,13 @@ config TEST_UBSAN
165165
This is a test module for UBSAN.
166166
It triggers various undefined behavior, and detect it.
167167

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+
168177
endif # if UBSAN

lib/ubsan.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
#include "ubsan.h"
2121

22-
#ifdef CONFIG_UBSAN_TRAP
22+
#if defined(CONFIG_UBSAN_TRAP) || defined(CONFIG_UBSAN_KVM_EL2)
2323
/*
2424
* Only include matches for UBSAN checks that are actually compiled in.
2525
* The mappings of struct SanitizerKind (the -fsanitize=xxx args) to
@@ -97,7 +97,9 @@ const char *report_ubsan_failure(u32 check_type)
9797
}
9898
}
9999

100-
#else
100+
#endif
101+
102+
#ifndef CONFIG_UBSAN_TRAP
101103
static const char * const type_check_kinds[] = {
102104
"load of",
103105
"store to",

scripts/Makefile.ubsan

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# SPDX-License-Identifier: GPL-2.0
22

3+
# Shared with KVM/arm64.
4+
export CFLAGS_UBSAN_TRAP := $(call cc-option,-fsanitize-trap=undefined,-fsanitize-undefined-trap-on-error)
5+
36
# Enable available and selected UBSAN features.
47
ubsan-cflags-$(CONFIG_UBSAN_ALIGNMENT) += -fsanitize=alignment
58
ubsan-cflags-$(CONFIG_UBSAN_BOUNDS_STRICT) += -fsanitize=bounds-strict
@@ -10,7 +13,7 @@ ubsan-cflags-$(CONFIG_UBSAN_DIV_ZERO) += -fsanitize=integer-divide-by-zero
1013
ubsan-cflags-$(CONFIG_UBSAN_UNREACHABLE) += -fsanitize=unreachable
1114
ubsan-cflags-$(CONFIG_UBSAN_BOOL) += -fsanitize=bool
1215
ubsan-cflags-$(CONFIG_UBSAN_ENUM) += -fsanitize=enum
13-
ubsan-cflags-$(CONFIG_UBSAN_TRAP) += $(call cc-option,-fsanitize-trap=undefined,-fsanitize-undefined-trap-on-error)
16+
ubsan-cflags-$(CONFIG_UBSAN_TRAP) += $(CFLAGS_UBSAN_TRAP)
1417

1518
export CFLAGS_UBSAN := $(ubsan-cflags-y)
1619

0 commit comments

Comments
 (0)