1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
5 * Author: Andrey Ryabinin <a.ryabinin@samsung.com>
8 #include <linux/bitops.h>
9 #include <linux/delay.h>
10 #include <linux/kasan.h>
11 #include <linux/kernel.h>
13 #include <linux/mman.h>
14 #include <linux/module.h>
15 #include <linux/printk.h>
16 #include <linux/slab.h>
17 #include <linux/string.h>
18 #include <linux/uaccess.h>
20 #include <linux/vmalloc.h>
24 #include <kunit/test.h>
26 #include "../mm/kasan/kasan.h"
28 #define OOB_TAG_OFF (IS_ENABLED(CONFIG_KASAN_GENERIC) ? 0 : KASAN_SHADOW_SCALE_SIZE)
31 * We assign some test results to these globals to make sure the tests
32 * are not eliminated as dead code.
35 void *kasan_ptr_result;
38 static struct kunit_resource resource;
39 static struct kunit_kasan_expectation fail_data;
40 static bool multishot;
42 static int kasan_test_init(struct kunit *test)
45 * Temporarily enable multi-shot mode and set panic_on_warn=0.
46 * Otherwise, we'd only get a report for the first case.
48 multishot = kasan_save_enable_multi_shot();
53 static void kasan_test_exit(struct kunit *test)
55 kasan_restore_multi_shot(multishot);
59 * KUNIT_EXPECT_KASAN_FAIL() - Causes a test failure when the expression does
60 * not cause a KASAN error. This uses a KUnit resource named "kasan_data." Do
61 * Do not use this name for a KUnit resource outside here.
64 #define KUNIT_EXPECT_KASAN_FAIL(test, condition) do { \
65 fail_data.report_expected = true; \
66 fail_data.report_found = false; \
67 kunit_add_named_resource(test, \
71 "kasan_data", &fail_data); \
73 KUNIT_EXPECT_EQ(test, \
74 fail_data.report_expected, \
75 fail_data.report_found); \
78 static void kmalloc_oob_right(struct kunit *test)
83 ptr = kmalloc(size, GFP_KERNEL);
84 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
86 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 'x');
90 static void kmalloc_oob_left(struct kunit *test)
95 ptr = kmalloc(size, GFP_KERNEL);
96 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
98 KUNIT_EXPECT_KASAN_FAIL(test, *ptr = *(ptr - 1));
102 static void kmalloc_node_oob_right(struct kunit *test)
107 ptr = kmalloc_node(size, GFP_KERNEL, 0);
108 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
110 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
114 static void kmalloc_pagealloc_oob_right(struct kunit *test)
117 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
119 if (!IS_ENABLED(CONFIG_SLUB)) {
120 kunit_info(test, "CONFIG_SLUB is not enabled.");
124 /* Allocate a chunk that does not fit into a SLUB cache to trigger
125 * the page allocator fallback.
127 ptr = kmalloc(size, GFP_KERNEL);
128 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
130 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 0);
134 static void kmalloc_pagealloc_uaf(struct kunit *test)
137 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
139 if (!IS_ENABLED(CONFIG_SLUB)) {
140 kunit_info(test, "CONFIG_SLUB is not enabled.");
144 ptr = kmalloc(size, GFP_KERNEL);
145 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
148 KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] = 0);
151 static void kmalloc_pagealloc_invalid_free(struct kunit *test)
154 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
156 if (!IS_ENABLED(CONFIG_SLUB)) {
157 kunit_info(test, "CONFIG_SLUB is not enabled.");
161 ptr = kmalloc(size, GFP_KERNEL);
162 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
164 KUNIT_EXPECT_KASAN_FAIL(test, kfree(ptr + 1));
167 static void kmalloc_large_oob_right(struct kunit *test)
170 size_t size = KMALLOC_MAX_CACHE_SIZE - 256;
171 /* Allocate a chunk that is large enough, but still fits into a slab
172 * and does not trigger the page allocator fallback in SLUB.
174 ptr = kmalloc(size, GFP_KERNEL);
175 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
177 KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
181 static void kmalloc_oob_krealloc_more(struct kunit *test)
187 ptr1 = kmalloc(size1, GFP_KERNEL);
188 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
190 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
191 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
193 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2 + OOB_TAG_OFF] = 'x');
197 static void kmalloc_oob_krealloc_less(struct kunit *test)
203 ptr1 = kmalloc(size1, GFP_KERNEL);
204 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
206 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
207 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
209 KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2 + OOB_TAG_OFF] = 'x');
213 static void kmalloc_oob_16(struct kunit *test)
219 ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
220 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
222 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
223 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
225 KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
230 static void kmalloc_oob_memset_2(struct kunit *test)
235 ptr = kmalloc(size, GFP_KERNEL);
236 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
238 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 7 + OOB_TAG_OFF, 0, 2));
242 static void kmalloc_oob_memset_4(struct kunit *test)
247 ptr = kmalloc(size, GFP_KERNEL);
248 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
250 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 5 + OOB_TAG_OFF, 0, 4));
255 static void kmalloc_oob_memset_8(struct kunit *test)
260 ptr = kmalloc(size, GFP_KERNEL);
261 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
263 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 1 + OOB_TAG_OFF, 0, 8));
267 static void kmalloc_oob_memset_16(struct kunit *test)
272 ptr = kmalloc(size, GFP_KERNEL);
273 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
275 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 1 + OOB_TAG_OFF, 0, 16));
279 static void kmalloc_oob_in_memset(struct kunit *test)
284 ptr = kmalloc(size, GFP_KERNEL);
285 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
287 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size + 5 + OOB_TAG_OFF));
291 static void kmalloc_memmove_invalid_size(struct kunit *test)
295 volatile size_t invalid_size = -2;
297 ptr = kmalloc(size, GFP_KERNEL);
298 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
300 memset((char *)ptr, 0, 64);
302 KUNIT_EXPECT_KASAN_FAIL(test,
303 memmove((char *)ptr, (char *)ptr + 4, invalid_size));
307 static void kmalloc_uaf(struct kunit *test)
312 ptr = kmalloc(size, GFP_KERNEL);
313 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
316 KUNIT_EXPECT_KASAN_FAIL(test, *(ptr + 8) = 'x');
319 static void kmalloc_uaf_memset(struct kunit *test)
324 ptr = kmalloc(size, GFP_KERNEL);
325 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
328 KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size));
331 static void kmalloc_uaf2(struct kunit *test)
336 ptr1 = kmalloc(size, GFP_KERNEL);
337 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
341 ptr2 = kmalloc(size, GFP_KERNEL);
342 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
344 KUNIT_EXPECT_KASAN_FAIL(test, ptr1[40] = 'x');
345 KUNIT_EXPECT_PTR_NE(test, ptr1, ptr2);
350 static void kfree_via_page(struct kunit *test)
355 unsigned long offset;
357 ptr = kmalloc(size, GFP_KERNEL);
358 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
360 page = virt_to_page(ptr);
361 offset = offset_in_page(ptr);
362 kfree(page_address(page) + offset);
365 static void kfree_via_phys(struct kunit *test)
371 ptr = kmalloc(size, GFP_KERNEL);
372 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
374 phys = virt_to_phys(ptr);
375 kfree(phys_to_virt(phys));
378 static void kmem_cache_oob(struct kunit *test)
382 struct kmem_cache *cache = kmem_cache_create("test_cache",
385 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
386 p = kmem_cache_alloc(cache, GFP_KERNEL);
388 kunit_err(test, "Allocation failed: %s\n", __func__);
389 kmem_cache_destroy(cache);
393 KUNIT_EXPECT_KASAN_FAIL(test, *p = p[size + OOB_TAG_OFF]);
394 kmem_cache_free(cache, p);
395 kmem_cache_destroy(cache);
398 static void memcg_accounted_kmem_cache(struct kunit *test)
403 struct kmem_cache *cache;
405 cache = kmem_cache_create("test_cache", size, 0, SLAB_ACCOUNT, NULL);
406 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
409 * Several allocations with a delay to allow for lazy per memcg kmem
412 for (i = 0; i < 5; i++) {
413 p = kmem_cache_alloc(cache, GFP_KERNEL);
417 kmem_cache_free(cache, p);
422 kmem_cache_destroy(cache);
425 static char global_array[10];
427 static void kasan_global_oob(struct kunit *test)
430 char *p = &global_array[ARRAY_SIZE(global_array) + i];
432 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
435 static void ksize_unpoisons_memory(struct kunit *test)
438 size_t size = 123, real_size;
440 ptr = kmalloc(size, GFP_KERNEL);
441 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
442 real_size = ksize(ptr);
443 /* This access doesn't trigger an error. */
446 KUNIT_EXPECT_KASAN_FAIL(test, ptr[real_size] = 'y');
450 static void kasan_stack_oob(struct kunit *test)
452 char stack_array[10];
453 volatile int i = OOB_TAG_OFF;
454 char *p = &stack_array[ARRAY_SIZE(stack_array) + i];
456 if (!IS_ENABLED(CONFIG_KASAN_STACK)) {
457 kunit_info(test, "CONFIG_KASAN_STACK is not enabled");
461 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
464 static void kasan_alloca_oob_left(struct kunit *test)
467 char alloca_array[i];
468 char *p = alloca_array - 1;
470 if (!IS_ENABLED(CONFIG_KASAN_STACK)) {
471 kunit_info(test, "CONFIG_KASAN_STACK is not enabled");
475 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
478 static void kasan_alloca_oob_right(struct kunit *test)
481 char alloca_array[i];
482 char *p = alloca_array + i;
484 if (!IS_ENABLED(CONFIG_KASAN_STACK)) {
485 kunit_info(test, "CONFIG_KASAN_STACK is not enabled");
489 KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
492 static void kmem_cache_double_free(struct kunit *test)
496 struct kmem_cache *cache;
498 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
499 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
501 p = kmem_cache_alloc(cache, GFP_KERNEL);
503 kunit_err(test, "Allocation failed: %s\n", __func__);
504 kmem_cache_destroy(cache);
508 kmem_cache_free(cache, p);
509 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p));
510 kmem_cache_destroy(cache);
513 static void kmem_cache_invalid_free(struct kunit *test)
517 struct kmem_cache *cache;
519 cache = kmem_cache_create("test_cache", size, 0, SLAB_TYPESAFE_BY_RCU,
521 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache);
523 p = kmem_cache_alloc(cache, GFP_KERNEL);
525 kunit_err(test, "Allocation failed: %s\n", __func__);
526 kmem_cache_destroy(cache);
530 /* Trigger invalid free, the object doesn't get freed */
531 KUNIT_EXPECT_KASAN_FAIL(test, kmem_cache_free(cache, p + 1));
534 * Properly free the object to prevent the "Objects remaining in
535 * test_cache on __kmem_cache_shutdown" BUG failure.
537 kmem_cache_free(cache, p);
539 kmem_cache_destroy(cache);
542 static void kasan_memchr(struct kunit *test)
547 /* See https://bugzilla.kernel.org/show_bug.cgi?id=206337 */
548 if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT)) {
550 "str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT");
554 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
555 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
557 KUNIT_EXPECT_KASAN_FAIL(test,
558 kasan_ptr_result = memchr(ptr, '1', size + 1));
563 static void kasan_memcmp(struct kunit *test)
569 /* See https://bugzilla.kernel.org/show_bug.cgi?id=206337 */
570 if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT)) {
572 "str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT");
576 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
577 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
578 memset(arr, 0, sizeof(arr));
580 KUNIT_EXPECT_KASAN_FAIL(test,
581 kasan_int_result = memcmp(ptr, arr, size+1));
585 static void kasan_strings(struct kunit *test)
590 /* See https://bugzilla.kernel.org/show_bug.cgi?id=206337 */
591 if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT)) {
593 "str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT");
597 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
598 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
603 * Try to cause only 1 invalid access (less spam in dmesg).
604 * For that we need ptr to point to zeroed byte.
605 * Skip metadata that could be stored in freed object so ptr
606 * will likely point to zeroed byte.
609 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strchr(ptr, '1'));
611 KUNIT_EXPECT_KASAN_FAIL(test, kasan_ptr_result = strrchr(ptr, '1'));
613 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strcmp(ptr, "2"));
615 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strncmp(ptr, "2", 1));
617 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strlen(ptr));
619 KUNIT_EXPECT_KASAN_FAIL(test, kasan_int_result = strnlen(ptr, 1));
622 static void kasan_bitops(struct kunit *test)
625 * Allocate 1 more byte, which causes kzalloc to round up to 16-bytes;
626 * this way we do not actually corrupt other memory.
628 long *bits = kzalloc(sizeof(*bits) + 1, GFP_KERNEL);
629 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bits);
632 * Below calls try to access bit within allocated memory; however, the
633 * below accesses are still out-of-bounds, since bitops are defined to
634 * operate on the whole long the bit is in.
636 KUNIT_EXPECT_KASAN_FAIL(test, set_bit(BITS_PER_LONG, bits));
638 KUNIT_EXPECT_KASAN_FAIL(test, __set_bit(BITS_PER_LONG, bits));
640 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit(BITS_PER_LONG, bits));
642 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit(BITS_PER_LONG, bits));
644 KUNIT_EXPECT_KASAN_FAIL(test, clear_bit_unlock(BITS_PER_LONG, bits));
646 KUNIT_EXPECT_KASAN_FAIL(test, __clear_bit_unlock(BITS_PER_LONG, bits));
648 KUNIT_EXPECT_KASAN_FAIL(test, change_bit(BITS_PER_LONG, bits));
650 KUNIT_EXPECT_KASAN_FAIL(test, __change_bit(BITS_PER_LONG, bits));
653 * Below calls try to access bit beyond allocated memory.
655 KUNIT_EXPECT_KASAN_FAIL(test,
656 test_and_set_bit(BITS_PER_LONG + BITS_PER_BYTE, bits));
658 KUNIT_EXPECT_KASAN_FAIL(test,
659 __test_and_set_bit(BITS_PER_LONG + BITS_PER_BYTE, bits));
661 KUNIT_EXPECT_KASAN_FAIL(test,
662 test_and_set_bit_lock(BITS_PER_LONG + BITS_PER_BYTE, bits));
664 KUNIT_EXPECT_KASAN_FAIL(test,
665 test_and_clear_bit(BITS_PER_LONG + BITS_PER_BYTE, bits));
667 KUNIT_EXPECT_KASAN_FAIL(test,
668 __test_and_clear_bit(BITS_PER_LONG + BITS_PER_BYTE, bits));
670 KUNIT_EXPECT_KASAN_FAIL(test,
671 test_and_change_bit(BITS_PER_LONG + BITS_PER_BYTE, bits));
673 KUNIT_EXPECT_KASAN_FAIL(test,
674 __test_and_change_bit(BITS_PER_LONG + BITS_PER_BYTE, bits));
676 KUNIT_EXPECT_KASAN_FAIL(test,
678 test_bit(BITS_PER_LONG + BITS_PER_BYTE, bits));
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));
688 static void kmalloc_double_kzfree(struct kunit *test)
693 ptr = kmalloc(size, GFP_KERNEL);
694 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
696 kfree_sensitive(ptr);
697 KUNIT_EXPECT_KASAN_FAIL(test, kfree_sensitive(ptr));
700 static void vmalloc_oob(struct kunit *test)
704 if (!IS_ENABLED(CONFIG_KASAN_VMALLOC)) {
705 kunit_info(test, "CONFIG_KASAN_VMALLOC is not enabled.");
710 * We have to be careful not to hit the guard page.
711 * The MMU will catch that and crash us.
713 area = vmalloc(3000);
714 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, area);
716 KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)area)[3100]);
720 static struct kunit_case kasan_kunit_test_cases[] = {
721 KUNIT_CASE(kmalloc_oob_right),
722 KUNIT_CASE(kmalloc_oob_left),
723 KUNIT_CASE(kmalloc_node_oob_right),
724 KUNIT_CASE(kmalloc_pagealloc_oob_right),
725 KUNIT_CASE(kmalloc_pagealloc_uaf),
726 KUNIT_CASE(kmalloc_pagealloc_invalid_free),
727 KUNIT_CASE(kmalloc_large_oob_right),
728 KUNIT_CASE(kmalloc_oob_krealloc_more),
729 KUNIT_CASE(kmalloc_oob_krealloc_less),
730 KUNIT_CASE(kmalloc_oob_16),
731 KUNIT_CASE(kmalloc_oob_in_memset),
732 KUNIT_CASE(kmalloc_oob_memset_2),
733 KUNIT_CASE(kmalloc_oob_memset_4),
734 KUNIT_CASE(kmalloc_oob_memset_8),
735 KUNIT_CASE(kmalloc_oob_memset_16),
736 KUNIT_CASE(kmalloc_memmove_invalid_size),
737 KUNIT_CASE(kmalloc_uaf),
738 KUNIT_CASE(kmalloc_uaf_memset),
739 KUNIT_CASE(kmalloc_uaf2),
740 KUNIT_CASE(kfree_via_page),
741 KUNIT_CASE(kfree_via_phys),
742 KUNIT_CASE(kmem_cache_oob),
743 KUNIT_CASE(memcg_accounted_kmem_cache),
744 KUNIT_CASE(kasan_global_oob),
745 KUNIT_CASE(kasan_stack_oob),
746 KUNIT_CASE(kasan_alloca_oob_left),
747 KUNIT_CASE(kasan_alloca_oob_right),
748 KUNIT_CASE(ksize_unpoisons_memory),
749 KUNIT_CASE(kmem_cache_double_free),
750 KUNIT_CASE(kmem_cache_invalid_free),
751 KUNIT_CASE(kasan_memchr),
752 KUNIT_CASE(kasan_memcmp),
753 KUNIT_CASE(kasan_strings),
754 KUNIT_CASE(kasan_bitops),
755 KUNIT_CASE(kmalloc_double_kzfree),
756 KUNIT_CASE(vmalloc_oob),
760 static struct kunit_suite kasan_kunit_test_suite = {
762 .init = kasan_test_init,
763 .test_cases = kasan_kunit_test_cases,
764 .exit = kasan_test_exit,
767 kunit_test_suite(kasan_kunit_test_suite);
769 MODULE_LICENSE("GPL");