Commit 85f195b1 authored by Marco Elver's avatar Marco Elver Committed by Andrew Morton

kasan: test: fix test for new meminstrinsic instrumentation

The tests for memset/memmove have been failing since they haven't been
instrumented in 69d4c0d3.

Fix the test to recognize when memintrinsics aren't instrumented, and skip
test cases accordingly.  We also need to conditionally pass -fno-builtin
to the test, otherwise the instrumentation pass won't recognize
memintrinsics and end up not instrumenting them either.

Link: https://lkml.kernel.org/r/20230224085942.1791837-3-elver@google.com
Fixes: 69d4c0d3 ("entry, kasan, x86: Disallow overriding mem*() functions")
Reported-by: default avatarLinux Kernel Functional Testing <lkft@linaro.org>
Signed-off-by: default avatarMarco Elver <elver@google.com>
Reviewed-by: default avatarAndrey Konovalov <andreyknvl@gmail.com>
Tested-by: default avatarLinux Kernel Functional Testing <lkft@linaro.org>
Tested-by: default avatarNaresh Kamboju <naresh.kamboju@linaro.org>
Cc: Alexander Potapenko <glider@google.com>
Cc: Andrey Ryabinin <ryabinin.a.a@gmail.com>
Cc: Borislav Petkov (AMD) <bp@alien8.de>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jakub Jelinek <jakub@redhat.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Nathan Chancellor <nathan@kernel.org>
Cc: Nick Desaulniers <ndesaulniers@google.com>
Cc: Nicolas Schier <nicolas@fjasle.eu>
Cc: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Vincenzo Frascino <vincenzo.frascino@arm.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
parent 36be5cba
...@@ -35,7 +35,14 @@ CFLAGS_shadow.o := $(CC_FLAGS_KASAN_RUNTIME) ...@@ -35,7 +35,14 @@ CFLAGS_shadow.o := $(CC_FLAGS_KASAN_RUNTIME)
CFLAGS_hw_tags.o := $(CC_FLAGS_KASAN_RUNTIME) CFLAGS_hw_tags.o := $(CC_FLAGS_KASAN_RUNTIME)
CFLAGS_sw_tags.o := $(CC_FLAGS_KASAN_RUNTIME) CFLAGS_sw_tags.o := $(CC_FLAGS_KASAN_RUNTIME)
CFLAGS_KASAN_TEST := $(CFLAGS_KASAN) -fno-builtin $(call cc-disable-warning, vla) CFLAGS_KASAN_TEST := $(CFLAGS_KASAN) $(call cc-disable-warning, vla)
ifndef CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX
# If compiler instruments memintrinsics by prefixing them with __asan/__hwasan,
# we need to treat them normally (as builtins), otherwise the compiler won't
# recognize them as instrumentable. If it doesn't instrument them, we need to
# pass -fno-builtin, so the compiler doesn't inline them.
CFLAGS_KASAN_TEST += -fno-builtin
endif
CFLAGS_kasan_test.o := $(CFLAGS_KASAN_TEST) CFLAGS_kasan_test.o := $(CFLAGS_KASAN_TEST)
CFLAGS_kasan_test_module.o := $(CFLAGS_KASAN_TEST) CFLAGS_kasan_test_module.o := $(CFLAGS_KASAN_TEST)
......
...@@ -165,6 +165,15 @@ static void kasan_test_exit(struct kunit *test) ...@@ -165,6 +165,15 @@ static void kasan_test_exit(struct kunit *test)
kunit_skip((test), "Test requires " #config "=n"); \ kunit_skip((test), "Test requires " #config "=n"); \
} while (0) } while (0)
#define KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test) do { \
if (IS_ENABLED(CONFIG_KASAN_HW_TAGS)) \
break; /* No compiler instrumentation. */ \
if (IS_ENABLED(CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX)) \
break; /* Should always be instrumented! */ \
if (IS_ENABLED(CONFIG_GENERIC_ENTRY)) \
kunit_skip((test), "Test requires checked mem*()"); \
} while (0)
static void kmalloc_oob_right(struct kunit *test) static void kmalloc_oob_right(struct kunit *test)
{ {
char *ptr; char *ptr;
...@@ -454,6 +463,8 @@ static void kmalloc_oob_16(struct kunit *test) ...@@ -454,6 +463,8 @@ static void kmalloc_oob_16(struct kunit *test)
u64 words[2]; u64 words[2];
} *ptr1, *ptr2; } *ptr1, *ptr2;
KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test);
/* This test is specifically crafted for the generic mode. */ /* This test is specifically crafted for the generic mode. */
KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC); KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
...@@ -476,6 +487,8 @@ static void kmalloc_uaf_16(struct kunit *test) ...@@ -476,6 +487,8 @@ static void kmalloc_uaf_16(struct kunit *test)
u64 words[2]; u64 words[2];
} *ptr1, *ptr2; } *ptr1, *ptr2;
KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test);
ptr1 = kmalloc(sizeof(*ptr1), GFP_KERNEL); ptr1 = kmalloc(sizeof(*ptr1), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
...@@ -498,6 +511,8 @@ static void kmalloc_oob_memset_2(struct kunit *test) ...@@ -498,6 +511,8 @@ static void kmalloc_oob_memset_2(struct kunit *test)
char *ptr; char *ptr;
size_t size = 128 - KASAN_GRANULE_SIZE; size_t size = 128 - KASAN_GRANULE_SIZE;
KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test);
ptr = kmalloc(size, GFP_KERNEL); ptr = kmalloc(size, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
...@@ -511,6 +526,8 @@ static void kmalloc_oob_memset_4(struct kunit *test) ...@@ -511,6 +526,8 @@ static void kmalloc_oob_memset_4(struct kunit *test)
char *ptr; char *ptr;
size_t size = 128 - KASAN_GRANULE_SIZE; size_t size = 128 - KASAN_GRANULE_SIZE;
KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test);
ptr = kmalloc(size, GFP_KERNEL); ptr = kmalloc(size, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
...@@ -524,6 +541,8 @@ static void kmalloc_oob_memset_8(struct kunit *test) ...@@ -524,6 +541,8 @@ static void kmalloc_oob_memset_8(struct kunit *test)
char *ptr; char *ptr;
size_t size = 128 - KASAN_GRANULE_SIZE; size_t size = 128 - KASAN_GRANULE_SIZE;
KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test);
ptr = kmalloc(size, GFP_KERNEL); ptr = kmalloc(size, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
...@@ -537,6 +556,8 @@ static void kmalloc_oob_memset_16(struct kunit *test) ...@@ -537,6 +556,8 @@ static void kmalloc_oob_memset_16(struct kunit *test)
char *ptr; char *ptr;
size_t size = 128 - KASAN_GRANULE_SIZE; size_t size = 128 - KASAN_GRANULE_SIZE;
KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test);
ptr = kmalloc(size, GFP_KERNEL); ptr = kmalloc(size, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
...@@ -550,6 +571,8 @@ static void kmalloc_oob_in_memset(struct kunit *test) ...@@ -550,6 +571,8 @@ static void kmalloc_oob_in_memset(struct kunit *test)
char *ptr; char *ptr;
size_t size = 128 - KASAN_GRANULE_SIZE; size_t size = 128 - KASAN_GRANULE_SIZE;
KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test);
ptr = kmalloc(size, GFP_KERNEL); ptr = kmalloc(size, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
...@@ -566,6 +589,8 @@ static void kmalloc_memmove_negative_size(struct kunit *test) ...@@ -566,6 +589,8 @@ static void kmalloc_memmove_negative_size(struct kunit *test)
size_t size = 64; size_t size = 64;
size_t invalid_size = -2; size_t invalid_size = -2;
KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test);
/* /*
* Hardware tag-based mode doesn't check memmove for negative size. * Hardware tag-based mode doesn't check memmove for negative size.
* As a result, this test introduces a side-effect memory corruption, * As a result, this test introduces a side-effect memory corruption,
...@@ -590,6 +615,8 @@ static void kmalloc_memmove_invalid_size(struct kunit *test) ...@@ -590,6 +615,8 @@ static void kmalloc_memmove_invalid_size(struct kunit *test)
size_t size = 64; size_t size = 64;
size_t invalid_size = size; size_t invalid_size = size;
KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test);
ptr = kmalloc(size, GFP_KERNEL); ptr = kmalloc(size, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
...@@ -618,6 +645,8 @@ static void kmalloc_uaf_memset(struct kunit *test) ...@@ -618,6 +645,8 @@ static void kmalloc_uaf_memset(struct kunit *test)
char *ptr; char *ptr;
size_t size = 33; size_t size = 33;
KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test);
/* /*
* Only generic KASAN uses quarantine, which is required to avoid a * Only generic KASAN uses quarantine, which is required to avoid a
* kernel memory corruption this test causes. * kernel memory corruption this test causes.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment