Skip to content

Commit 85f195b

Browse files
melverakpm00
authored andcommitted
kasan: test: fix test for new meminstrinsic instrumentation
The tests for memset/memmove have been failing since they haven't been instrumented in 69d4c0d. 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/[email protected] Fixes: 69d4c0d ("entry, kasan, x86: Disallow overriding mem*() functions") Reported-by: Linux Kernel Functional Testing <[email protected]> Signed-off-by: Marco Elver <[email protected]> Reviewed-by: Andrey Konovalov <[email protected]> Tested-by: Linux Kernel Functional Testing <[email protected]> Tested-by: Naresh Kamboju <[email protected]> Cc: Alexander Potapenko <[email protected]> Cc: Andrey Ryabinin <[email protected]> Cc: Borislav Petkov (AMD) <[email protected]> Cc: Dave Hansen <[email protected]> Cc: Dmitry Vyukov <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Jakub Jelinek <[email protected]> Cc: Kees Cook <[email protected]> Cc: Nathan Chancellor <[email protected]> Cc: Nick Desaulniers <[email protected]> Cc: Nicolas Schier <[email protected]> Cc: Peter Zijlstra (Intel) <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Vincenzo Frascino <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 36be5cb commit 85f195b

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

mm/kasan/Makefile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,14 @@ CFLAGS_shadow.o := $(CC_FLAGS_KASAN_RUNTIME)
3535
CFLAGS_hw_tags.o := $(CC_FLAGS_KASAN_RUNTIME)
3636
CFLAGS_sw_tags.o := $(CC_FLAGS_KASAN_RUNTIME)
3737

38-
CFLAGS_KASAN_TEST := $(CFLAGS_KASAN) -fno-builtin $(call cc-disable-warning, vla)
38+
CFLAGS_KASAN_TEST := $(CFLAGS_KASAN) $(call cc-disable-warning, vla)
39+
ifndef CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX
40+
# If compiler instruments memintrinsics by prefixing them with __asan/__hwasan,
41+
# we need to treat them normally (as builtins), otherwise the compiler won't
42+
# recognize them as instrumentable. If it doesn't instrument them, we need to
43+
# pass -fno-builtin, so the compiler doesn't inline them.
44+
CFLAGS_KASAN_TEST += -fno-builtin
45+
endif
3946

4047
CFLAGS_kasan_test.o := $(CFLAGS_KASAN_TEST)
4148
CFLAGS_kasan_test_module.o := $(CFLAGS_KASAN_TEST)

mm/kasan/kasan_test.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,15 @@ static void kasan_test_exit(struct kunit *test)
165165
kunit_skip((test), "Test requires " #config "=n"); \
166166
} while (0)
167167

168+
#define KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test) do { \
169+
if (IS_ENABLED(CONFIG_KASAN_HW_TAGS)) \
170+
break; /* No compiler instrumentation. */ \
171+
if (IS_ENABLED(CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX)) \
172+
break; /* Should always be instrumented! */ \
173+
if (IS_ENABLED(CONFIG_GENERIC_ENTRY)) \
174+
kunit_skip((test), "Test requires checked mem*()"); \
175+
} while (0)
176+
168177
static void kmalloc_oob_right(struct kunit *test)
169178
{
170179
char *ptr;
@@ -454,6 +463,8 @@ static void kmalloc_oob_16(struct kunit *test)
454463
u64 words[2];
455464
} *ptr1, *ptr2;
456465

466+
KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test);
467+
457468
/* This test is specifically crafted for the generic mode. */
458469
KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
459470

@@ -476,6 +487,8 @@ static void kmalloc_uaf_16(struct kunit *test)
476487
u64 words[2];
477488
} *ptr1, *ptr2;
478489

490+
KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test);
491+
479492
ptr1 = kmalloc(sizeof(*ptr1), GFP_KERNEL);
480493
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
481494

@@ -498,6 +511,8 @@ static void kmalloc_oob_memset_2(struct kunit *test)
498511
char *ptr;
499512
size_t size = 128 - KASAN_GRANULE_SIZE;
500513

514+
KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test);
515+
501516
ptr = kmalloc(size, GFP_KERNEL);
502517
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
503518

@@ -511,6 +526,8 @@ static void kmalloc_oob_memset_4(struct kunit *test)
511526
char *ptr;
512527
size_t size = 128 - KASAN_GRANULE_SIZE;
513528

529+
KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test);
530+
514531
ptr = kmalloc(size, GFP_KERNEL);
515532
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
516533

@@ -524,6 +541,8 @@ static void kmalloc_oob_memset_8(struct kunit *test)
524541
char *ptr;
525542
size_t size = 128 - KASAN_GRANULE_SIZE;
526543

544+
KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test);
545+
527546
ptr = kmalloc(size, GFP_KERNEL);
528547
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
529548

@@ -537,6 +556,8 @@ static void kmalloc_oob_memset_16(struct kunit *test)
537556
char *ptr;
538557
size_t size = 128 - KASAN_GRANULE_SIZE;
539558

559+
KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test);
560+
540561
ptr = kmalloc(size, GFP_KERNEL);
541562
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
542563

@@ -550,6 +571,8 @@ static void kmalloc_oob_in_memset(struct kunit *test)
550571
char *ptr;
551572
size_t size = 128 - KASAN_GRANULE_SIZE;
552573

574+
KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test);
575+
553576
ptr = kmalloc(size, GFP_KERNEL);
554577
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
555578

@@ -566,6 +589,8 @@ static void kmalloc_memmove_negative_size(struct kunit *test)
566589
size_t size = 64;
567590
size_t invalid_size = -2;
568591

592+
KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test);
593+
569594
/*
570595
* Hardware tag-based mode doesn't check memmove for negative size.
571596
* As a result, this test introduces a side-effect memory corruption,
@@ -590,6 +615,8 @@ static void kmalloc_memmove_invalid_size(struct kunit *test)
590615
size_t size = 64;
591616
size_t invalid_size = size;
592617

618+
KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test);
619+
593620
ptr = kmalloc(size, GFP_KERNEL);
594621
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
595622

@@ -618,6 +645,8 @@ static void kmalloc_uaf_memset(struct kunit *test)
618645
char *ptr;
619646
size_t size = 33;
620647

648+
KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test);
649+
621650
/*
622651
* Only generic KASAN uses quarantine, which is required to avoid a
623652
* kernel memory corruption this test causes.

0 commit comments

Comments
 (0)