Skip to content

Commit

Permalink
kasan: clean up comments in tests
Browse files Browse the repository at this point in the history
Clarify and update comments and some of the info messages in KASAN tests.

Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
  • Loading branch information
xairy committed Dec 9, 2020
1 parent a80a6af commit 8fd2d4f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 44 deletions.
94 changes: 52 additions & 42 deletions lib/test_kasan.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,23 @@
#define OOB_TAG_OFF (IS_ENABLED(CONFIG_KASAN_GENERIC) ? 0 : KASAN_GRANULE_SIZE)

/*
* We assign some test results to these globals to make sure the tests
* are not eliminated as dead code.
* Some tests use these global variables to store return values from function
* calls that could otherwise be eliminated by the compiler as dead code.
*/

void *kasan_ptr_result;
int kasan_int_result;

static struct kunit_resource resource;
static struct kunit_kasan_expectation fail_data;
static bool multishot;

/*
* Temporarily enable multi-shot mode. Otherwise, KASAN would only report the
* first detected bug and panic the kernel if panic_on_warn is enabled.
*/
static int kasan_test_init(struct kunit *test)
{
/*
* Temporarily enable multi-shot mode and set panic_on_warn=0.
* Otherwise, we'd only get a report for the first case.
*/
multishot = kasan_save_enable_multi_shot();

return 0;
}

Expand All @@ -56,20 +54,20 @@ static void kasan_test_exit(struct kunit *test)
}

/**
* KUNIT_EXPECT_KASAN_FAIL() - Causes a test failure when the expression does
* not cause a KASAN error. This uses a KUnit resource named "kasan_data." Do
* Do not use this name for a KUnit resource outside here.
*
* KUNIT_EXPECT_KASAN_FAIL() - check that the executed expression produces a
* KASAN report; causes a test failure otherwise. This relies on a KUnit
* resource named "kasan_data". Do not use this name for KUnit resources
* outside of KASAN tests.
*/
#define KUNIT_EXPECT_KASAN_FAIL(test, condition) do { \
#define KUNIT_EXPECT_KASAN_FAIL(test, expression) do { \
fail_data.report_expected = true; \
fail_data.report_found = false; \
kunit_add_named_resource(test, \
NULL, \
NULL, \
&resource, \
"kasan_data", &fail_data); \
condition; \
expression; \
KUNIT_EXPECT_EQ(test, \
fail_data.report_expected, \
fail_data.report_found); \
Expand Down Expand Up @@ -117,11 +115,12 @@ static void kmalloc_pagealloc_oob_right(struct kunit *test)
size_t size = KMALLOC_MAX_CACHE_SIZE + 10;

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

/* Allocate a chunk that does not fit into a SLUB cache to trigger
/*
* Allocate a chunk that does not fit into a SLUB cache to trigger
* the page allocator fallback.
*/
ptr = kmalloc(size, GFP_KERNEL);
Expand All @@ -137,7 +136,7 @@ static void kmalloc_pagealloc_uaf(struct kunit *test)
size_t size = KMALLOC_MAX_CACHE_SIZE + 10;

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

Expand All @@ -154,7 +153,7 @@ static void kmalloc_pagealloc_invalid_free(struct kunit *test)
size_t size = KMALLOC_MAX_CACHE_SIZE + 10;

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

Expand All @@ -168,7 +167,9 @@ static void kmalloc_large_oob_right(struct kunit *test)
{
char *ptr;
size_t size = KMALLOC_MAX_CACHE_SIZE - 256;
/* Allocate a chunk that is large enough, but still fits into a slab

/*
* Allocate a chunk that is large enough, but still fits into a slab
* and does not trigger the page allocator fallback in SLUB.
*/
ptr = kmalloc(size, GFP_KERNEL);
Expand Down Expand Up @@ -218,7 +219,7 @@ static void kmalloc_oob_16(struct kunit *test)

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

Expand Down Expand Up @@ -454,7 +455,7 @@ static void kasan_global_oob(struct kunit *test)

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

Expand All @@ -469,10 +470,13 @@ static void ksize_unpoisons_memory(struct kunit *test)
ptr = kmalloc(size, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
real_size = ksize(ptr);
/* This access doesn't trigger an error. */

/* This access shouldn't trigger a KASAN report. */
ptr[size] = 'x';
/* This one does. */

/* This one must. */
KUNIT_EXPECT_KASAN_FAIL(test, ptr[real_size] = 'y');

kfree(ptr);
}

Expand All @@ -483,7 +487,7 @@ static void kasan_stack_oob(struct kunit *test)
char *p = &stack_array[ARRAY_SIZE(stack_array) + i];

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

Expand All @@ -498,12 +502,12 @@ static void kasan_alloca_oob_left(struct kunit *test)

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

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

Expand All @@ -518,12 +522,12 @@ static void kasan_alloca_oob_right(struct kunit *test)

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

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

Expand Down Expand Up @@ -568,7 +572,7 @@ static void kmem_cache_invalid_free(struct kunit *test)
return;
}

/* Trigger invalid free, the object doesn't get freed */
/* Trigger invalid free, the object doesn't get freed. */
KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p + 1));

/*
Expand All @@ -585,10 +589,12 @@ static void kasan_memchr(struct kunit *test)
char *ptr;
size_t size = 24;

/* See https://bugzilla.kernel.org/show_bug.cgi?id=206337 */
/*
* 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");
kunit_info(test, "skipping, CONFIG_AMD_MEM_ENCRYPT enabled");
return;
}

Expand All @@ -610,10 +616,12 @@ static void kasan_memcmp(struct kunit *test)
size_t size = 24;
int arr[9];

/* See https://bugzilla.kernel.org/show_bug.cgi?id=206337 */
/*
* 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");
kunit_info(test, "skipping, CONFIG_AMD_MEM_ENCRYPT enabled");
return;
}

Expand All @@ -634,10 +642,12 @@ static void kasan_strings(struct kunit *test)
char *ptr;
size_t size = 24;

/* See https://bugzilla.kernel.org/show_bug.cgi?id=206337 */
/*
* 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");
kunit_info(test, "skipping, CONFIG_AMD_MEM_ENCRYPT enabled");
return;
}

Expand Down Expand Up @@ -701,12 +711,12 @@ static void kasan_bitops_generic(struct kunit *test)

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

/*
* Allocate 1 more byte, which causes kzalloc to round up to 16-bytes;
* Allocate 1 more byte, which causes kzalloc to round up to 16 bytes;
* this way we do not actually corrupt other memory.
*/
bits = kzalloc(sizeof(*bits) + 1, GFP_KERNEL);
Expand All @@ -733,7 +743,7 @@ static void kasan_bitops_tags(struct kunit *test)

/* 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");
kunit_info(test, "skipping, CONFIG_KASAN_SW_TAGS required");
return;
}

Expand Down Expand Up @@ -765,7 +775,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.");
kunit_info(test, "skipping, CONFIG_KASAN_VMALLOC required");
return;
}

Expand Down
5 changes: 3 additions & 2 deletions lib/test_kasan_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ static noinline void __init kasan_rcu_uaf(void)
static int __init test_kasan_module_init(void)
{
/*
* Temporarily enable multi-shot mode. Otherwise, we'd only get a
* report for the first case.
* Temporarily enable multi-shot mode. Otherwise, KASAN would only
* report the first detected bug and panic the kernel if panic_on_warn
* is enabled.
*/
bool multishot = kasan_save_enable_multi_shot();

Expand Down

0 comments on commit 8fd2d4f

Please sign in to comment.