From 6ac72fa7f12a779ec9a142cbe6bbf6b58fc5d80a Mon Sep 17 00:00:00 2001 From: wangmingrong1 Date: Tue, 22 Apr 2025 12:01:59 +0800 Subject: [PATCH] kasan: Add configurable kasan initialization variable location This can avoid crashes caused by uninitialized accesses to initialized variables in non-chip memory. Signed-off-by: wangmingrong1 --- mm/kasan/CMakeLists.txt | 6 ++++++ mm/kasan/Kconfig | 7 +++++++ mm/kasan/Make.defs | 4 ++++ mm/kasan/hook.c | 4 ++++ 4 files changed, 21 insertions(+) diff --git a/mm/kasan/CMakeLists.txt b/mm/kasan/CMakeLists.txt index ef3f50ae94..d33e1805fe 100644 --- a/mm/kasan/CMakeLists.txt +++ b/mm/kasan/CMakeLists.txt @@ -25,6 +25,12 @@ if(CONFIG_MM_KASAN) list(APPEND FLAGS ${NO_LTO}) list(APPEND FLAGS -fno-builtin) list(APPEND FLAGS -fno-sanitize=kernel-address) + + if(NOT "${CONFIG_MM_KASAN_MARK_LOCATION}" STREQUAL "") + target_compile_definitions( + mm PRIVATE -DMM_KASAN_MARK_LOCATION="${CONFIG_MM_KASAN_MARK_LOCATION}") + endif() + endif() target_sources(mm PRIVATE ${SRCS}) diff --git a/mm/kasan/Kconfig b/mm/kasan/Kconfig index 8520646a24..94934c6cf2 100644 --- a/mm/kasan/Kconfig +++ b/mm/kasan/Kconfig @@ -57,6 +57,13 @@ config MM_KASAN_INSTRUMENT_ALL to check. Enabling this option will get image size increased and performance decreased significantly. +config MM_KASAN_MARK_LOCATION + string "Kasan's mark storage location" + ---help--- + The section where KASan mark is located. It can prevent + variables located in non-chip memory, and crashes + caused by instrumentation access + if MM_KASAN_INSTRUMENT config MM_KASAN_REGIONS diff --git a/mm/kasan/Make.defs b/mm/kasan/Make.defs index 86ff1b6a45..911e77a792 100644 --- a/mm/kasan/Make.defs +++ b/mm/kasan/Make.defs @@ -28,6 +28,10 @@ ifeq ($(CONFIG_MM_KASAN),y) CFLAGS += -fno-sanitize=kernel-address endif +ifneq ($(CONFIG_MM_KASAN_MARK_LOCATION),"") + CFLAGS += ${DEFINE_PREFIX}MM_KASAN_MARK_LOCATION=CONFIG_MM_KASAN_MARK_LOCATION +endif + # Add the core heap directory to the build DEPPATH += --dep-path kasan diff --git a/mm/kasan/hook.c b/mm/kasan/hook.c index e67b2cee1c..1127a37a08 100644 --- a/mm/kasan/hook.c +++ b/mm/kasan/hook.c @@ -120,7 +120,11 @@ static struct kasan_watchpoint_s g_watchpoint[MM_KASAN_WATCHPOINT]; #endif #ifdef CONFIG_MM_KASAN +# ifdef MM_KASAN_MARK_LOCATION +static uint32_t g_region_init locate_data(MM_KASAN_MARK_LOCATION); +# else static uint32_t g_region_init; +# endif #endif /****************************************************************************