Skip to content

Commit

Permalink
kasan: add macros to simplify checking test constraints
Browse files Browse the repository at this point in the history
Some KASAN tests require specific kernel configs to be enabled.
Instead of copy-pasting the checks for these configs add a few helper
macros and use them.

Link: https://linux-review.googlesource.com/id/I237484a7fddfedf4a4aae9cc61ecbcdbe85a0a63
Link: https://lkml.kernel.org/r/6a0fcdb9676b7e869cfc415893ede12d916c246c.1610733117.git.andreyknvl@google.com
Signed-off-by: Andrey Konovalov <[email protected]>
Suggested-by: Alexander Potapenko <[email protected]>
Reviewed-by: Marco Elver <[email protected]>
Reviewed-by: Alexander Potapenko <[email protected]>
Cc: Andrey Ryabinin <[email protected]>
Cc: Branislav Rankov <[email protected]>
Cc: Catalin Marinas <[email protected]>
Cc: Dmitry Vyukov <[email protected]>
Cc: Evgenii Stepanov <[email protected]>
Cc: Kevin Brodsky <[email protected]>
Cc: Peter Collingbourne <[email protected]>
Cc: Vincenzo Frascino <[email protected]>
Cc: Will Deacon <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
xairy authored and torvalds committed Feb 24, 2021
1 parent 0fd3792 commit da17e37
Showing 1 changed file with 31 additions and 70 deletions.
101 changes: 31 additions & 70 deletions lib/test_kasan.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@ static void kasan_test_exit(struct kunit *test)
fail_data.report_found); \
} while (0)

#define KASAN_TEST_NEEDS_CONFIG_ON(test, config) do { \
if (!IS_ENABLED(config)) { \
kunit_info((test), "skipping, " #config " required"); \
return; \
} \
} while (0)

#define KASAN_TEST_NEEDS_CONFIG_OFF(test, config) do { \
if (IS_ENABLED(config)) { \
kunit_info((test), "skipping, " #config " enabled"); \
return; \
} \
} while (0)

static void kmalloc_oob_right(struct kunit *test)
{
char *ptr;
Expand Down Expand Up @@ -114,10 +128,7 @@ static void kmalloc_pagealloc_oob_right(struct kunit *test)
char *ptr;
size_t size = KMALLOC_MAX_CACHE_SIZE + 10;

if (!IS_ENABLED(CONFIG_SLUB)) {
kunit_info(test, "CONFIG_SLUB is not enabled.");
return;
}
KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);

/*
* Allocate a chunk that does not fit into a SLUB cache to trigger
Expand All @@ -135,10 +146,7 @@ static void kmalloc_pagealloc_uaf(struct kunit *test)
char *ptr;
size_t size = KMALLOC_MAX_CACHE_SIZE + 10;

if (!IS_ENABLED(CONFIG_SLUB)) {
kunit_info(test, "CONFIG_SLUB is not enabled.");
return;
}
KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);

ptr = kmalloc(size, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Expand All @@ -152,10 +160,7 @@ static void kmalloc_pagealloc_invalid_free(struct kunit *test)
char *ptr;
size_t size = KMALLOC_MAX_CACHE_SIZE + 10;

if (!IS_ENABLED(CONFIG_SLUB)) {
kunit_info(test, "CONFIG_SLUB is not enabled.");
return;
}
KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);

ptr = kmalloc(size, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Expand Down Expand Up @@ -218,10 +223,7 @@ static void kmalloc_oob_16(struct kunit *test)
} *ptr1, *ptr2;

/* This test is specifically crafted for the generic mode. */
if (!IS_ENABLED(CONFIG_KASAN_GENERIC)) {
kunit_info(test, "CONFIG_KASAN_GENERIC required\n");
return;
}
KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);

ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
Expand Down Expand Up @@ -454,10 +456,7 @@ static void kasan_global_oob(struct kunit *test)
char *p = &global_array[ARRAY_SIZE(global_array) + i];

/* Only generic mode instruments globals. */
if (!IS_ENABLED(CONFIG_KASAN_GENERIC)) {
kunit_info(test, "CONFIG_KASAN_GENERIC required");
return;
}
KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);

KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
}
Expand Down Expand Up @@ -486,10 +485,7 @@ static void kasan_stack_oob(struct kunit *test)
volatile int i = OOB_TAG_OFF;
char *p = &stack_array[ARRAY_SIZE(stack_array) + i];

if (!IS_ENABLED(CONFIG_KASAN_STACK)) {
kunit_info(test, "CONFIG_KASAN_STACK is not enabled");
return;
}
KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);

KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
}
Expand All @@ -501,15 +497,8 @@ static void kasan_alloca_oob_left(struct kunit *test)
char *p = alloca_array - 1;

/* Only generic mode instruments dynamic allocas. */
if (!IS_ENABLED(CONFIG_KASAN_GENERIC)) {
kunit_info(test, "CONFIG_KASAN_GENERIC required");
return;
}

if (!IS_ENABLED(CONFIG_KASAN_STACK)) {
kunit_info(test, "CONFIG_KASAN_STACK is not enabled");
return;
}
KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);

KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
}
Expand All @@ -521,15 +510,8 @@ static void kasan_alloca_oob_right(struct kunit *test)
char *p = alloca_array + i;

/* Only generic mode instruments dynamic allocas. */
if (!IS_ENABLED(CONFIG_KASAN_GENERIC)) {
kunit_info(test, "CONFIG_KASAN_GENERIC required");
return;
}

if (!IS_ENABLED(CONFIG_KASAN_STACK)) {
kunit_info(test, "CONFIG_KASAN_STACK is not enabled");
return;
}
KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);

KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
}
Expand Down Expand Up @@ -593,11 +575,7 @@ static void kasan_memchr(struct kunit *test)
* str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
* See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
*/
if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT)) {
kunit_info(test,
"str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT");
return;
}
KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);

if (OOB_TAG_OFF)
size = round_up(size, OOB_TAG_OFF);
Expand All @@ -621,11 +599,7 @@ static void kasan_memcmp(struct kunit *test)
* str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
* See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
*/
if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT)) {
kunit_info(test,
"str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT");
return;
}
KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);

if (OOB_TAG_OFF)
size = round_up(size, OOB_TAG_OFF);
Expand All @@ -648,11 +622,7 @@ static void kasan_strings(struct kunit *test)
* str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
* See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
*/
if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT)) {
kunit_info(test,
"str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT");
return;
}
KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);

ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
Expand Down Expand Up @@ -713,10 +683,7 @@ static void kasan_bitops_generic(struct kunit *test)
long *bits;

/* This test is specifically crafted for the generic mode. */
if (!IS_ENABLED(CONFIG_KASAN_GENERIC)) {
kunit_info(test, "CONFIG_KASAN_GENERIC required\n");
return;
}
KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);

/*
* Allocate 1 more byte, which causes kzalloc to round up to 16 bytes;
Expand Down Expand Up @@ -744,11 +711,8 @@ static void kasan_bitops_tags(struct kunit *test)
{
long *bits;

/* This test is specifically crafted for the tag-based mode. */
if (IS_ENABLED(CONFIG_KASAN_GENERIC)) {
kunit_info(test, "CONFIG_KASAN_SW_TAGS required\n");
return;
}
/* This test is specifically crafted for tag-based modes. */
KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);

/* Allocation size will be rounded to up granule size, which is 16. */
bits = kzalloc(sizeof(*bits), GFP_KERNEL);
Expand Down Expand Up @@ -777,10 +741,7 @@ static void vmalloc_oob(struct kunit *test)
{
void *area;

if (!IS_ENABLED(CONFIG_KASAN_VMALLOC)) {
kunit_info(test, "CONFIG_KASAN_VMALLOC is not enabled.");
return;
}
KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_VMALLOC);

/*
* We have to be careful not to hit the guard page.
Expand Down

0 comments on commit da17e37

Please sign in to comment.