Skip to content

Commit 58b999d

Browse files
xairytorvalds
authored andcommitted
kasan: adopt KUNIT tests to SW_TAGS mode
Now that we have KASAN-KUNIT tests integration, it's easy to see that some KASAN tests are not adopted to the SW_TAGS mode and are failing. Adjust the allocation size for kasan_memchr() and kasan_memcmp() by roung it up to OOB_TAG_OFF so the bad access ends up in a separate memory granule. Add a new kmalloc_uaf_16() tests that relies on UAF, and a new kasan_bitops_tags() test that is tailored to tag-based mode, as it's hard to adopt the existing kmalloc_oob_16() and kasan_bitops_generic() (renamed from kasan_bitops()) without losing the precision. Add new kmalloc_uaf_16() and kasan_bitops_uaf() tests that rely on UAFs, as it's hard to adopt the existing kmalloc_oob_16() and kasan_bitops_oob() (rename from kasan_bitops()) without losing the precision. Disable kasan_global_oob() and kasan_alloca_oob_left/right() as SW_TAGS mode doesn't instrument globals nor dynamic allocas. Signed-off-by: Andrey Konovalov <andreyknvl@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Tested-by: David Gow <davidgow@google.com> Link: https://lkml.kernel.org/r/76eee17b6531ca8b3ca92b240cb2fd23204aaff7.1603129942.git.andreyknvl@google.com Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 8de15e9 commit 58b999d

1 file changed

Lines changed: 107 additions & 42 deletions

File tree

lib/test_kasan.c

Lines changed: 107 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,12 @@ static void kmalloc_oob_16(struct kunit *test)
216216
u64 words[2];
217217
} *ptr1, *ptr2;
218218

219+
/* This test is specifically crafted for the generic mode. */
220+
if (!IS_ENABLED(CONFIG_KASAN_GENERIC)) {
221+
kunit_info(test, "CONFIG_KASAN_GENERIC required\n");
222+
return;
223+
}
224+
219225
ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
220226
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
221227

@@ -227,6 +233,23 @@ static void kmalloc_oob_16(struct kunit *test)
227233
kfree(ptr2);
228234
}
229235

236+
static void kmalloc_uaf_16(struct kunit *test)
237+
{
238+
struct {
239+
u64 words[2];
240+
} *ptr1, *ptr2;
241+
242+
ptr1 = kmalloc(sizeof(*ptr1), GFP_KERNEL);
243+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
244+
245+
ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
246+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
247+
kfree(ptr2);
248+
249+
KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
250+
kfree(ptr1);
251+
}
252+
230253
static void kmalloc_oob_memset_2(struct kunit *test)
231254
{
232255
char *ptr;
@@ -429,6 +452,12 @@ static void kasan_global_oob(struct kunit *test)
429452
volatile int i = 3;
430453
char *p = &global_array[ARRAY_SIZE(global_array) + i];
431454

455+
/* Only generic mode instruments globals. */
456+
if (!IS_ENABLED(CONFIG_KASAN_GENERIC)) {
457+
kunit_info(test, "CONFIG_KASAN_GENERIC required");
458+
return;
459+
}
460+
432461
KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
433462
}
434463

@@ -467,6 +496,12 @@ static void kasan_alloca_oob_left(struct kunit *test)
467496
char alloca_array[i];
468497
char *p = alloca_array - 1;
469498

499+
/* Only generic mode instruments dynamic allocas. */
500+
if (!IS_ENABLED(CONFIG_KASAN_GENERIC)) {
501+
kunit_info(test, "CONFIG_KASAN_GENERIC required");
502+
return;
503+
}
504+
470505
if (!IS_ENABLED(CONFIG_KASAN_STACK)) {
471506
kunit_info(test, "CONFIG_KASAN_STACK is not enabled");
472507
return;
@@ -481,6 +516,12 @@ static void kasan_alloca_oob_right(struct kunit *test)
481516
char alloca_array[i];
482517
char *p = alloca_array + i;
483518

519+
/* Only generic mode instruments dynamic allocas. */
520+
if (!IS_ENABLED(CONFIG_KASAN_GENERIC)) {
521+
kunit_info(test, "CONFIG_KASAN_GENERIC required");
522+
return;
523+
}
524+
484525
if (!IS_ENABLED(CONFIG_KASAN_STACK)) {
485526
kunit_info(test, "CONFIG_KASAN_STACK is not enabled");
486527
return;
@@ -551,6 +592,9 @@ static void kasan_memchr(struct kunit *test)
551592
return;
552593
}
553594

595+
if (OOB_TAG_OFF)
596+
size = round_up(size, OOB_TAG_OFF);
597+
554598
ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
555599
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
556600

@@ -573,6 +617,9 @@ static void kasan_memcmp(struct kunit *test)
573617
return;
574618
}
575619

620+
if (OOB_TAG_OFF)
621+
size = round_up(size, OOB_TAG_OFF);
622+
576623
ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
577624
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
578625
memset(arr, 0, sizeof(arr));
@@ -619,69 +666,85 @@ static void kasan_strings(struct kunit *test)
619666
KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strnlen(ptr, 1));
620667
}
621668

622-
static void kasan_bitops(struct kunit *test)
669+
static void kasan_bitops_modify(struct kunit *test, int nr, void *addr)
670+
{
671+
KUNIT_EXPECT_KASAN_FAIL(test, set_bit(nr, addr));
672+
KUNIT_EXPECT_KASAN_FAIL(test, __set_bit(nr, addr));
673+
KUNIT_EXPECT_KASAN_FAIL(test, clear_bit(nr, addr));
674+
KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit(nr, addr));
675+
KUNIT_EXPECT_KASAN_FAIL(test, clear_bit_unlock(nr, addr));
676+
KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit_unlock(nr, addr));
677+
KUNIT_EXPECT_KASAN_FAIL(test, change_bit(nr, addr));
678+
KUNIT_EXPECT_KASAN_FAIL(test, __change_bit(nr, addr));
679+
}
680+
681+
static void kasan_bitops_test_and_modify(struct kunit *test, int nr, void *addr)
682+
{
683+
KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit(nr, addr));
684+
KUNIT_EXPECT_KASAN_FAIL(test, __test_and_set_bit(nr, addr));
685+
KUNIT_EXPECT_KASAN_FAIL(test, test_and_set_bit_lock(nr, addr));
686+
KUNIT_EXPECT_KASAN_FAIL(test, test_and_clear_bit(nr, addr));
687+
KUNIT_EXPECT_KASAN_FAIL(test, __test_and_clear_bit(nr, addr));
688+
KUNIT_EXPECT_KASAN_FAIL(test, test_and_change_bit(nr, addr));
689+
KUNIT_EXPECT_KASAN_FAIL(test, __test_and_change_bit(nr, addr));
690+
KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = test_bit(nr, addr));
691+
692+
#if defined(clear_bit_unlock_is_negative_byte)
693+
KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result =
694+
clear_bit_unlock_is_negative_byte(nr, addr));
695+
#endif
696+
}
697+
698+
static void kasan_bitops_generic(struct kunit *test)
623699
{
700+
long *bits;
701+
702+
/* This test is specifically crafted for the generic mode. */
703+
if (!IS_ENABLED(CONFIG_KASAN_GENERIC)) {
704+
kunit_info(test, "CONFIG_KASAN_GENERIC required\n");
705+
return;
706+
}
707+
624708
/*
625709
* Allocate 1 more byte, which causes kzalloc to round up to 16-bytes;
626710
* this way we do not actually corrupt other memory.
627711
*/
628-
long *bits = kzalloc(sizeof(*bits) + 1, GFP_KERNEL);
712+
bits = kzalloc(sizeof(*bits) + 1, GFP_KERNEL);
629713
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
630714

631715
/*
632716
* Below calls try to access bit within allocated memory; however, the
633717
* below accesses are still out-of-bounds, since bitops are defined to
634718
* operate on the whole long the bit is in.
635719
*/
636-
KUNIT_EXPECT_KASAN_FAIL(test, set_bit(BITS_PER_LONG, bits));
637-
638-
KUNIT_EXPECT_KASAN_FAIL(test, __set_bit(BITS_PER_LONG, bits));
639-
640-
KUNIT_EXPECT_KASAN_FAIL(test, clear_bit(BITS_PER_LONG, bits));
641-
642-
KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit(BITS_PER_LONG, bits));
643-
644-
KUNIT_EXPECT_KASAN_FAIL(test, clear_bit_unlock(BITS_PER_LONG, bits));
645-
646-
KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit_unlock(BITS_PER_LONG, bits));
647-
648-
KUNIT_EXPECT_KASAN_FAIL(test, change_bit(BITS_PER_LONG, bits));
649-
650-
KUNIT_EXPECT_KASAN_FAIL(test, __change_bit(BITS_PER_LONG, bits));
720+
kasan_bitops_modify(test, BITS_PER_LONG, bits);
651721

652722
/*
653723
* Below calls try to access bit beyond allocated memory.
654724
*/
655-
KUNIT_EXPECT_KASAN_FAIL(test,
656-
test_and_set_bit(BITS_PER_LONG + BITS_PER_BYTE, bits));
657-
658-
KUNIT_EXPECT_KASAN_FAIL(test,
659-
__test_and_set_bit(BITS_PER_LONG + BITS_PER_BYTE, bits));
660-
661-
KUNIT_EXPECT_KASAN_FAIL(test,
662-
test_and_set_bit_lock(BITS_PER_LONG + BITS_PER_BYTE, bits));
725+
kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, bits);
663726

664-
KUNIT_EXPECT_KASAN_FAIL(test,
665-
test_and_clear_bit(BITS_PER_LONG + BITS_PER_BYTE, bits));
727+
kfree(bits);
728+
}
666729

667-
KUNIT_EXPECT_KASAN_FAIL(test,
668-
__test_and_clear_bit(BITS_PER_LONG + BITS_PER_BYTE, bits));
730+
static void kasan_bitops_tags(struct kunit *test)
731+
{
732+
long *bits;
669733

670-
KUNIT_EXPECT_KASAN_FAIL(test,
671-
test_and_change_bit(BITS_PER_LONG + BITS_PER_BYTE, bits));
734+
/* This test is specifically crafted for the tag-based mode. */
735+
if (IS_ENABLED(CONFIG_KASAN_GENERIC)) {
736+
kunit_info(test, "CONFIG_KASAN_SW_TAGS required\n");
737+
return;
738+
}
672739

673-
KUNIT_EXPECT_KASAN_FAIL(test,
674-
__test_and_change_bit(BITS_PER_LONG + BITS_PER_BYTE, bits));
740+
/* Allocation size will be rounded to up granule size, which is 16. */
741+
bits = kzalloc(sizeof(*bits), GFP_KERNEL);
742+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
675743

676-
KUNIT_EXPECT_KASAN_FAIL(test,
677-
kasan_int_result =
678-
test_bit(BITS_PER_LONG + BITS_PER_BYTE, bits));
744+
/* Do the accesses past the 16 allocated bytes. */
745+
kasan_bitops_modify(test, BITS_PER_LONG, &bits[1]);
746+
kasan_bitops_test_and_modify(test, BITS_PER_LONG + BITS_PER_BYTE, &bits[1]);
679747

680-
#if defined(clear_bit_unlock_is_negative_byte)
681-
KUNIT_EXPECT_KASAN_FAIL(test,
682-
kasan_int_result = clear_bit_unlock_is_negative_byte(
683-
BITS_PER_LONG + BITS_PER_BYTE, bits));
684-
#endif
685748
kfree(bits);
686749
}
687750

@@ -728,6 +791,7 @@ static struct kunit_case kasan_kunit_test_cases[] = {
728791
KUNIT_CASE(kmalloc_oob_krealloc_more),
729792
KUNIT_CASE(kmalloc_oob_krealloc_less),
730793
KUNIT_CASE(kmalloc_oob_16),
794+
KUNIT_CASE(kmalloc_uaf_16),
731795
KUNIT_CASE(kmalloc_oob_in_memset),
732796
KUNIT_CASE(kmalloc_oob_memset_2),
733797
KUNIT_CASE(kmalloc_oob_memset_4),
@@ -751,7 +815,8 @@ static struct kunit_case kasan_kunit_test_cases[] = {
751815
KUNIT_CASE(kasan_memchr),
752816
KUNIT_CASE(kasan_memcmp),
753817
KUNIT_CASE(kasan_strings),
754-
KUNIT_CASE(kasan_bitops),
818+
KUNIT_CASE(kasan_bitops_generic),
819+
KUNIT_CASE(kasan_bitops_tags),
755820
KUNIT_CASE(kmalloc_double_kzfree),
756821
KUNIT_CASE(vmalloc_oob),
757822
{}

0 commit comments

Comments
 (0)