kasan/kconfig: Add config CONFIG_MM_KASAN_INSTRUMENT
Some checks failed
Build Documentation / build-html (push) Has been cancelled

It will be used to distinguish between hardware KASan and software KASan. Hardware KASan does not need to use plug-in

Signed-off-by: wangmingrong1 <wangmingrong1@xiaomi.com>
This commit is contained in:
wangmingrong1
2025-01-13 17:45:51 +08:00
committed by Xiang Xiao
parent 9b4cd4e0a4
commit df7d062713
20 changed files with 42 additions and 36 deletions

View File

@@ -41,7 +41,7 @@ Usage
To enable Generic KASAN, configure the kernel with::
CONFIG_MM_KASAN=y
CONFIG_MM_KASAN_ALL=y
CONFIG_MM_KASAN_INSTRUMENT_ALL=y
CONFIG_MM_KASAN_GENERIC=y
If you want to enable global variable out of bounds detection,
@@ -52,7 +52,7 @@ you can add configurations based on the above::
To enable Software Tag-Based KASAN, configure the kernel with::
CONFIG_MM_KASAN=y
CONFIG_MM_KASAN_ALL=y
CONFIG_MM_KASAN_INSTRUMENT_ALL=y
CONFIG_MM_KASAN_SW_TAGS=y
Implementation details

View File

@@ -134,7 +134,7 @@ if(CONFIG_MM_UBSAN_TRAP_ON_ERROR)
add_compile_options(-fsanitize-undefined-trap-on-error)
endif()
if(CONFIG_MM_KASAN_ALL)
if(CONFIG_MM_KASAN_INSTRUMENT_ALL)
add_compile_options(-fsanitize=kernel-address)
endif()

View File

@@ -135,7 +135,7 @@ if(CONFIG_MM_UBSAN_TRAP_ON_ERROR)
add_compile_options(-fsanitize-undefined-trap-on-error)
endif()
if(CONFIG_MM_KASAN_ALL)
if(CONFIG_MM_KASAN_INSTRUMENT_ALL)
add_compile_options(-fsanitize=kernel-address)
endif()

View File

@@ -149,7 +149,7 @@ if(CONFIG_MM_UBSAN_TRAP_ON_ERROR)
add_compile_options(-fsanitize-undefined-trap-on-error)
endif()
if(CONFIG_MM_KASAN_ALL)
if(CONFIG_MM_KASAN_INSTRUMENT_ALL)
add_compile_options(-fsanitize=kernel-address)
endif()

View File

@@ -106,7 +106,7 @@ if(CONFIG_MM_UBSAN_TRAP_ON_ERROR)
add_compile_options(-fsanitize-undefined-trap-on-error)
endif()
if(CONFIG_MM_KASAN_ALL)
if(CONFIG_MM_KASAN_INSTRUMENT_ALL)
add_compile_options(-fsanitize=kernel-address)
endif()

View File

@@ -94,7 +94,7 @@ ifeq ($(CONFIG_MM_UBSAN_TRAP_ON_ERROR),y)
ARCHOPTIMIZATION += -fsanitize-undefined-trap-on-error
endif
ifeq ($(CONFIG_MM_KASAN_ALL),y)
ifeq ($(CONFIG_MM_KASAN_INSTRUMENT_ALL),y)
ARCHOPTIMIZATION += -fsanitize=kernel-address
endif

View File

@@ -84,7 +84,7 @@ ifeq ($(CONFIG_MM_UBSAN_TRAP_ON_ERROR),y)
ARCHOPTIMIZATION += -fsanitize-undefined-trap-on-error
endif
ifeq ($(CONFIG_MM_KASAN_ALL),y)
ifeq ($(CONFIG_MM_KASAN_INSTRUMENT_ALL),y)
ARCHOPTIMIZATION += -fsanitize=kernel-address
endif

View File

@@ -113,7 +113,7 @@ if(CONFIG_MM_UBSAN_TRAP_ON_ERROR)
add_compile_options(-fsanitize-undefined-trap-on-error)
endif()
if(CONFIG_MM_KASAN_ALL)
if(CONFIG_MM_KASAN_INSTRUMENT_ALL)
add_compile_options(-fsanitize=kernel-address)
endif()

View File

@@ -365,7 +365,7 @@ if(CONFIG_RISCV_TOOLCHAIN STREQUAL GNU_RVG)
endif()
if(CONFIG_MM_KASAN_ALL)
if(CONFIG_MM_KASAN_INSTRUMENT_ALL)
add_compile_options(-fsanitize=kernel-address)
endif()

View File

@@ -292,7 +292,7 @@ else
endif
endif
ifeq ($(CONFIG_MM_KASAN_ALL),y)
ifeq ($(CONFIG_MM_KASAN_INSTRUMENT_ALL),y)
ARCHOPTIMIZATION += -fsanitize=kernel-address
endif

View File

@@ -103,7 +103,7 @@ if(CONFIG_SIM_ASAN)
add_compile_options(-fsanitize=pointer-compare)
add_compile_options(-fsanitize=pointer-subtract)
add_link_options(-fsanitize=address)
elseif(CONFIG_MM_KASAN_ALL)
elseif(CONFIG_MM_KASAN_INSTRUMENT_ALL)
add_compile_options(-fsanitize=kernel-address)
endif()

View File

@@ -72,7 +72,7 @@ ifeq ($(CONFIG_MM_UBSAN_TRAP_ON_ERROR),y)
ARCHOPTIMIZATION += -fsanitize-undefined-trap-on-error
endif
ifeq ($(CONFIG_MM_KASAN_ALL),y)
ifeq ($(CONFIG_MM_KASAN_INSTRUMENT_ALL),y)
ARCHOPTIMIZATION += -fsanitize=kernel-address
endif

View File

@@ -55,7 +55,7 @@ NM = $(CROSSDEV)nm
OBJCOPY = $(CROSSDEV)objcopy
OBJDUMP = $(CROSSDEV)objdump
ifeq ($(CONFIG_MM_KASAN_ALL),y)
ifeq ($(CONFIG_MM_KASAN_INSTRUMENT_ALL),y)
ARCHOPTIMIZATION += -fsanitize=kernel-address
endif

View File

@@ -129,7 +129,7 @@ ifeq ($(CONFIG_LIBCXX),y)
CXXFLAGS += -D_LIBCPP_DISABLE_AVAILABILITY
endif
ifeq ($(CONFIG_MM_KASAN_ALL),y)
ifeq ($(CONFIG_MM_KASAN_INSTRUMENT_ALL),y)
ARCHOPTIMIZATION += -fsanitize=kernel-address
endif
ifeq ($(CONFIG_MM_KASAN_GLOBAL),y)

View File

@@ -85,7 +85,7 @@ add_compile_options(-mlongcalls)
add_compile_options(-mtext-section-literals)
if(CONFIG_MM_KASAN_ALL)
if(CONFIG_MM_KASAN_INSTRUMENT_ALL)
add_compile_options(-fsanitize=kernel-address)
endif()

View File

@@ -45,7 +45,7 @@ endif
ARCHCPUFLAGS = -mlongcalls
ifeq ($(CONFIG_MM_KASAN_ALL),y)
ifeq ($(CONFIG_MM_KASAN_INSTRUMENT_ALL),y)
ARCHOPTIMIZATION += -fsanitize=kernel-address
endif

View File

@@ -45,7 +45,7 @@ endif
ARCHCPUFLAGS = -mlongcalls
ifeq ($(CONFIG_MM_KASAN_ALL),y)
ifeq ($(CONFIG_MM_KASAN_INSTRUMENT_ALL),y)
ARCHOPTIMIZATION += -fsanitize=kernel-address
endif

View File

@@ -89,7 +89,7 @@ endif
ifeq ($(CONFIG_SIM_ASAN),y)
ARCHOPTIMIZATION += -fsanitize=address -fsanitize-address-use-after-scope
ARCHOPTIMIZATION += -fsanitize=pointer-compare -fsanitize=pointer-subtract
else ifeq ($(CONFIG_MM_KASAN_ALL),y)
else ifeq ($(CONFIG_MM_KASAN_INSTRUMENT_ALL),y)
ARCHOPTIMIZATION += -fsanitize=kernel-address
endif

View File

@@ -89,7 +89,7 @@ size_t ARCH_LIBCFUN(strnlen)(FAR const char *s, size_t maxlen);
FAR char *ARCH_LIBCFUN(strrchr)(FAR const char *s, int c);
#endif
# ifdef CONFIG_MM_KASAN
# ifdef CONFIG_MM_KASAN_INSTRUMENT
# ifndef CONFIG_MM_KASAN_DISABLE_READS_CHECK
extern void __asan_loadN(FAR void *addr, size_t size);
# endif
@@ -106,7 +106,7 @@ extern void __asan_storeN(FAR void *addr, size_t size);
FAR void *memchr(FAR const void *s, int c, size_t n)
{
# ifdef CONFIG_MM_KASAN
# ifdef CONFIG_MM_KASAN_INSTRUMENT
# ifndef CONFIG_MM_KASAN_DISABLE_READS_CHECK
__asan_loadN((FAR void *)s, n);
# endif
@@ -119,7 +119,7 @@ FAR void *memchr(FAR const void *s, int c, size_t n)
#ifdef CONFIG_LIBC_ARCH_MEMCPY
FAR void *memcpy(FAR void *dest, FAR const void *src, FAR size_t n)
{
# ifdef CONFIG_MM_KASAN
# ifdef CONFIG_MM_KASAN_INSTRUMENT
# ifndef CONFIG_MM_KASAN_DISABLE_WRITES_CHECK
__asan_storeN(dest, n);
# endif
@@ -134,7 +134,7 @@ FAR void *memcpy(FAR void *dest, FAR const void *src, FAR size_t n)
#ifdef CONFIG_LIBC_ARCH_MEMCMP
int memcmp(FAR const void *s1, FAR const void *s2, size_t n)
{
# ifdef CONFIG_MM_KASAN
# ifdef CONFIG_MM_KASAN_INSTRUMENT
# ifndef CONFIG_MM_KASAN_DISABLE_READS_CHECK
__asan_loadN((FAR void *)s1, n);
__asan_loadN((FAR void *)s2, n);
@@ -147,7 +147,7 @@ int memcmp(FAR const void *s1, FAR const void *s2, size_t n)
#ifdef CONFIG_LIBC_ARCH_MEMMOVE
FAR void *memmove(FAR void *dest, FAR const void *src, FAR size_t n)
{
# ifdef CONFIG_MM_KASAN
# ifdef CONFIG_MM_KASAN_INSTRUMENT
# ifndef CONFIG_MM_KASAN_DISABLE_WRITES_CHECK
__asan_storeN(dest, n);
# endif
@@ -162,7 +162,7 @@ FAR void *memmove(FAR void *dest, FAR const void *src, FAR size_t n)
#ifdef CONFIG_LIBC_ARCH_MEMSET
FAR void *memset(FAR void *s, int c, FAR size_t n)
{
# ifdef CONFIG_MM_KASAN
# ifdef CONFIG_MM_KASAN_INSTRUMENT
# ifndef CONFIG_MM_KASAN_DISABLE_WRITES_CHECK
__asan_storeN(s, n);
# endif
@@ -174,7 +174,7 @@ FAR void *memset(FAR void *s, int c, FAR size_t n)
#ifdef CONFIG_LIBC_ARCH_STRCMP
int strcmp(FAR const char *s1, FAR const char *s2)
{
# ifdef CONFIG_MM_KASAN
# ifdef CONFIG_MM_KASAN_INSTRUMENT
# ifndef CONFIG_MM_KASAN_DISABLE_READS_CHECK
__asan_loadN((FAR void *)s1, ARCH_LIBCFUN(strlen)(s1) + 1);
__asan_loadN((FAR void *)s2, ARCH_LIBCFUN(strlen)(s2) + 1);
@@ -187,7 +187,7 @@ int strcmp(FAR const char *s1, FAR const char *s2)
#ifdef CONFIG_LIBC_ARCH_STRCPY
FAR char *strcpy(FAR char *dest, FAR const char *src)
{
# ifdef CONFIG_MM_KASAN
# ifdef CONFIG_MM_KASAN_INSTRUMENT
# ifndef CONFIG_MM_KASAN_DISABLE_WRITES_CHECK
__asan_storeN(dest, ARCH_LIBCFUN(strlen)(src) + 1);
# endif
@@ -203,7 +203,7 @@ FAR char *strcpy(FAR char *dest, FAR const char *src)
size_t strlen(FAR const char *s)
{
size_t ret = ARCH_LIBCFUN(strlen)(s);
# ifdef CONFIG_MM_KASAN
# ifdef CONFIG_MM_KASAN_INSTRUMENT
# ifndef CONFIG_MM_KASAN_DISABLE_READS_CHECK
__asan_loadN((FAR void *)s, ret + 1);
# endif
@@ -215,7 +215,7 @@ size_t strlen(FAR const char *s)
#ifdef CONFIG_LIBC_ARCH_STRNCPY
FAR char *strncpy(FAR char *dest, FAR const char *src, size_t n)
{
# ifdef CONFIG_MM_KASAN
# ifdef CONFIG_MM_KASAN_INSTRUMENT
# ifndef CONFIG_MM_KASAN_DISABLE_WRITES_CHECK
__asan_storeN(dest, n);
# endif
@@ -230,7 +230,7 @@ FAR char *strncpy(FAR char *dest, FAR const char *src, size_t n)
#ifdef CONFIG_LIBC_ARCH_STRCHR
FAR char *strchr(FAR const char *s, int c)
{
# ifdef CONFIG_MM_KASAN
# ifdef CONFIG_MM_KASAN_INSTRUMENT
# ifndef CONFIG_MM_KASAN_DISABLE_READS_CHECK
__asan_loadN((FAR void *)s, ARCH_LIBCFUN(strlen)(s) + 1);
# endif
@@ -243,7 +243,7 @@ FAR char *strchr(FAR const char *s, int c)
#ifdef CONFIG_LIBC_ARCH_STRCHNUL
FAR char *strchrnul(FAR const char *s, int c);
{
# ifdef CONFIG_MM_KASAN
# ifdef CONFIG_MM_KASAN_INSTRUMENT
# ifndef CONFIG_MM_KASAN_DISABLE_READS_CHECK
__asan_loadN((FAR void *)s, ARCH_LIBCFUN(strlen)(s) + 1);
# endif
@@ -255,7 +255,7 @@ FAR char *strchrnul(FAR const char *s, int c);
#ifdef CONFIG_LIBC_ARCH_STRNCMP
int strncmp(FAR const char *s1, FAR const char *s2, size_t n)
{
# ifdef CONFIG_MM_KASAN
# ifdef CONFIG_MM_KASAN_INSTRUMENT
# ifndef CONFIG_MM_KASAN_DISABLE_READS_CHECK
size_t size_s1 = ARCH_LIBCFUN(strnlen)(s1, n);
size_t size_s2 = ARCH_LIBCFUN(strnlen)(s2, n);
@@ -274,7 +274,7 @@ int strncmp(FAR const char *s1, FAR const char *s2, size_t n)
size_t strnlen(FAR const char *s, size_t maxlen)
{
size_t ret = ARCH_LIBCFUN(strnlen)(s, maxlen);
# ifdef CONFIG_MM_KASAN
# ifdef CONFIG_MM_KASAN_INSTRUMENT
# ifndef CONFIG_MM_KASAN_DISABLE_READS_CHECK
__asan_loadN((FAR void *)s, ret);
# endif
@@ -287,7 +287,7 @@ size_t strnlen(FAR const char *s, size_t maxlen)
#ifdef CONFIG_LIBC_ARCH_STRRCHR
FAR char *strrchr(FAR const char *s, int c)
{
# ifdef CONFIG_MM_KASAN
# ifdef CONFIG_MM_KASAN_INSTRUMENT
# ifndef CONFIG_MM_KASAN_DISABLE_READS_CHECK
__asan_loadN((FAR void *)s, ARCH_LIBCFUN(strlen)(s) + 1);
# endif

View File

@@ -11,6 +11,9 @@ config MM_KASAN
bugs in native code. After turn on this option, Please
add -fsanitize=kernel-address to CFLAGS/CXXFLAGS too.
config MM_KASAN_INSTRUMENT
bool
if MM_KASAN
choice
@@ -24,20 +27,23 @@ config MM_KASAN_NONE
config MM_KASAN_GENERIC
bool "KAsan generic mode"
select MM_KASAN_INSTRUMENT
---help---
KASan generic mode that does not require hardware support at all
config MM_KASAN_SW_TAGS
bool "KAsan SW tags"
select ARM64_TBI
select MM_KASAN_INSTRUMENT
depends on ARCH_ARM64
---help---
KAsan based on software tags
endchoice
config MM_KASAN_ALL
config MM_KASAN_INSTRUMENT_ALL
bool "Enable KASan for the entire image"
depends on MM_KASAN_INSTRUMENT
default y
---help---
This option activates address sanitizer for the entire image.
@@ -99,7 +105,7 @@ config MM_KASAN_SKIP_ZERO_TAGS
config MM_KASAN_GLOBAL
bool "Enable global data check"
depends on MM_KASAN_ALL
depends on MM_KASAN_INSTRUMENT_ALL
default n
---help---
This option enables KASan global data check.