From f2c21b11d83a74a7f0dac5270a9bc22fcd21c3f7 Mon Sep 17 00:00:00 2001 From: wangmingrong1 Date: Fri, 6 Sep 2024 11:14:28 +0800 Subject: [PATCH] kasantest: Add a test set for global variable out of bounds detection Signed-off-by: wangmingrong1 --- testing/kasantest/Makefile | 1 + testing/kasantest/kasantest.c | 29 ++++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/testing/kasantest/Makefile b/testing/kasantest/Makefile index 1f5f02936..a17edcac3 100644 --- a/testing/kasantest/Makefile +++ b/testing/kasantest/Makefile @@ -29,5 +29,6 @@ STACKSIZE = $(CONFIG_DEFAULT_TASK_STACKSIZE) CFLAGS += -Wno-error -Wno-use-after-free CFLAGS += -Wno-array-bounds -Wno-free-nonheap-object +CFLAGS += -Wno-stringop-overflow include $(APPDIR)/Application.mk diff --git a/testing/kasantest/kasantest.c b/testing/kasantest/kasantest.c index e264e7055..b99c8d861 100644 --- a/testing/kasantest/kasantest.c +++ b/testing/kasantest/kasantest.c @@ -72,6 +72,11 @@ static bool test_heap_memset(FAR struct mm_heap_s *heap, size_t size); static bool test_heap_memcpy(FAR struct mm_heap_s *heap, size_t size); static bool test_heap_memmove(FAR struct mm_heap_s *heap, size_t size); +#ifdef CONFIG_MM_KASAN_GLOBAL +static bool test_global_underflow(FAR struct mm_heap_s *heap, size_t size); +static bool test_global_overflow(FAR struct mm_heap_s *heap, size_t size); +#endif + /**************************************************************************** * Private Data ****************************************************************************/ @@ -87,11 +92,19 @@ const static testcase_t g_kasan_test[] = {test_heap_unpoison, "heap unpoison"}, {test_heap_memset, "heap memset"}, {test_heap_memcpy, "heap memcpy"}, - {test_heap_memmove, "heap memmove"} + {test_heap_memmove, "heap memmove"}, +#ifdef CONFIG_MM_KASAN_GLOBAL + {test_global_underflow, "globals underflow"}, + {test_global_overflow, "globals overflow"}, +#endif }; static char g_kasan_heap[65536] aligned_data(8); +#ifdef CONFIG_MM_KASAN_GLOBAL +static char g_kasan_globals[32]; +#endif + /**************************************************************************** * Private Functions ****************************************************************************/ @@ -210,6 +223,20 @@ static bool test_heap_memmove(FAR struct mm_heap_s *heap, size_t size) return false; } +#ifdef CONFIG_MM_KASAN_GLOBAL +static bool test_global_underflow(FAR struct mm_heap_s *heap, size_t size) +{ + memset(g_kasan_globals - 31, 0x12, sizeof(g_kasan_globals)); + return false; +} + +static bool test_global_overflow(FAR struct mm_heap_s *heap, size_t size) +{ + memset(g_kasan_globals, 0xef, sizeof(g_kasan_globals) + 31); + return false; +} +#endif + static int run_test(FAR const testcase_t *test) { size_t heap_size = 65536;