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 #define pr_fmt(fmt) "kasan test: %s " fmt, __func__
10 #include <linux/bitops.h>
11 #include <linux/delay.h>
12 #include <linux/kasan.h>
13 #include <linux/kernel.h>
15 #include <linux/mman.h>
16 #include <linux/module.h>
17 #include <linux/printk.h>
18 #include <linux/slab.h>
19 #include <linux/string.h>
20 #include <linux/uaccess.h>
22 #include <linux/vmalloc.h>
26 #include <kunit/test.h>
28 #include "../mm/kasan/kasan.h"
30 #define OOB_TAG_OFF (IS_ENABLED(CONFIG_KASAN_GENERIC) ? 0 : KASAN_SHADOW_SCALE_SIZE)
33 * We assign some test results to these globals to make sure the tests
34 * are not eliminated as dead code.
37 void *kasan_ptr_result;
40 static struct kunit_resource resource;
41 static struct kunit_kasan_expectation fail_data;
42 static bool multishot;
44 static int kasan_test_init(struct kunit *test)
47 * Temporarily enable multi-shot mode and set panic_on_warn=0.
48 * Otherwise, we'd only get a report for the first case.
50 multishot = kasan_save_enable_multi_shot();
55 static void kasan_test_exit(struct kunit *test)
57 kasan_restore_multi_shot(multishot);
61 * KUNIT_EXPECT_KASAN_FAIL() - Causes a test failure when the expression does
62 * not cause a KASAN error. This uses a KUnit resource named "kasan_data." Do
63 * Do not use this name for a KUnit resource outside here.
66 #define KUNIT_EXPECT_KASAN_FAIL(test, condition) do { \
67 fail_data.report_expected = true; \
68 fail_data.report_found = false; \
69 kunit_add_named_resource(test, \
73 "kasan_data", &fail_data); \
75 KUNIT_EXPECT_EQ(test, \
76 fail_data.report_expected, \
77 fail_data.report_found); \
83 * Note: test functions are marked noinline so that their names appear in
86 static noinline void __init kmalloc_oob_right(void)
91 pr_info("out-of-bounds to right\n");
92 ptr = kmalloc(size, GFP_KERNEL);
94 pr_err("Allocation failed\n");
98 ptr[size + OOB_TAG_OFF] = 'x';
103 static noinline void __init kmalloc_oob_left(void)
108 pr_info("out-of-bounds to left\n");
109 ptr = kmalloc(size, GFP_KERNEL);
111 pr_err("Allocation failed\n");
119 static noinline void __init kmalloc_node_oob_right(void)
124 pr_info("kmalloc_node(): out-of-bounds to right\n");
125 ptr = kmalloc_node(size, GFP_KERNEL, 0);
127 pr_err("Allocation failed\n");
136 static noinline void __init kmalloc_pagealloc_oob_right(void)
139 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
141 /* Allocate a chunk that does not fit into a SLUB cache to trigger
142 * the page allocator fallback.
144 pr_info("kmalloc pagealloc allocation: out-of-bounds to right\n");
145 ptr = kmalloc(size, GFP_KERNEL);
147 pr_err("Allocation failed\n");
151 ptr[size + OOB_TAG_OFF] = 0;
156 static noinline void __init kmalloc_pagealloc_uaf(void)
159 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
161 pr_info("kmalloc pagealloc allocation: use-after-free\n");
162 ptr = kmalloc(size, GFP_KERNEL);
164 pr_err("Allocation failed\n");
172 static noinline void __init kmalloc_pagealloc_invalid_free(void)
175 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
177 pr_info("kmalloc pagealloc allocation: invalid-free\n");
178 ptr = kmalloc(size, GFP_KERNEL);
180 pr_err("Allocation failed\n");
188 static noinline void __init kmalloc_large_oob_right(void)
191 size_t size = KMALLOC_MAX_CACHE_SIZE - 256;
192 /* Allocate a chunk that is large enough, but still fits into a slab
193 * and does not trigger the page allocator fallback in SLUB.
195 pr_info("kmalloc large allocation: out-of-bounds to right\n");
196 ptr = kmalloc(size, GFP_KERNEL);
198 pr_err("Allocation failed\n");
206 static noinline void __init kmalloc_oob_krealloc_more(void)
212 pr_info("out-of-bounds after krealloc more\n");
213 ptr1 = kmalloc(size1, GFP_KERNEL);
214 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
215 if (!ptr1 || !ptr2) {
216 pr_err("Allocation failed\n");
222 ptr2[size2 + OOB_TAG_OFF] = 'x';
227 static noinline void __init kmalloc_oob_krealloc_less(void)
233 pr_info("out-of-bounds after krealloc less\n");
234 ptr1 = kmalloc(size1, GFP_KERNEL);
235 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
236 if (!ptr1 || !ptr2) {
237 pr_err("Allocation failed\n");
242 ptr2[size2 + OOB_TAG_OFF] = 'x';
247 static noinline void __init kmalloc_oob_16(void)
253 pr_info("kmalloc out-of-bounds for 16-bytes access\n");
254 ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
255 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
256 if (!ptr1 || !ptr2) {
257 pr_err("Allocation failed\n");
267 static noinline void __init kmalloc_oob_memset_2(void)
272 pr_info("out-of-bounds in memset2\n");
273 ptr = kmalloc(size, GFP_KERNEL);
275 pr_err("Allocation failed\n");
279 memset(ptr + 7 + OOB_TAG_OFF, 0, 2);
284 static noinline void __init kmalloc_oob_memset_4(void)
289 pr_info("out-of-bounds in memset4\n");
290 ptr = kmalloc(size, GFP_KERNEL);
292 pr_err("Allocation failed\n");
296 memset(ptr + 5 + OOB_TAG_OFF, 0, 4);
302 static noinline void __init kmalloc_oob_memset_8(void)
307 pr_info("out-of-bounds in memset8\n");
308 ptr = kmalloc(size, GFP_KERNEL);
310 pr_err("Allocation failed\n");
314 memset(ptr + 1 + OOB_TAG_OFF, 0, 8);
319 static noinline void __init kmalloc_oob_memset_16(void)
324 pr_info("out-of-bounds in memset16\n");
325 ptr = kmalloc(size, GFP_KERNEL);
327 pr_err("Allocation failed\n");
331 memset(ptr + 1 + OOB_TAG_OFF, 0, 16);
336 static noinline void __init kmalloc_oob_in_memset(void)
341 pr_info("out-of-bounds in memset\n");
342 ptr = kmalloc(size, GFP_KERNEL);
344 pr_err("Allocation failed\n");
348 memset(ptr, 0, size + 5 + OOB_TAG_OFF);
353 static noinline void __init kmalloc_memmove_invalid_size(void)
357 volatile size_t invalid_size = -2;
359 pr_info("invalid size in memmove\n");
360 ptr = kmalloc(size, GFP_KERNEL);
362 pr_err("Allocation failed\n");
366 memset((char *)ptr, 0, 64);
367 memmove((char *)ptr, (char *)ptr + 4, invalid_size);
371 static noinline void __init kmalloc_uaf(void)
376 pr_info("use-after-free\n");
377 ptr = kmalloc(size, GFP_KERNEL);
379 pr_err("Allocation failed\n");
387 static noinline void __init kmalloc_uaf_memset(void)
392 pr_info("use-after-free in memset\n");
393 ptr = kmalloc(size, GFP_KERNEL);
395 pr_err("Allocation failed\n");
400 memset(ptr, 0, size);
403 static noinline void __init kmalloc_uaf2(void)
408 pr_info("use-after-free after another kmalloc\n");
409 ptr1 = kmalloc(size, GFP_KERNEL);
411 pr_err("Allocation failed\n");
416 ptr2 = kmalloc(size, GFP_KERNEL);
418 pr_err("Allocation failed\n");
424 pr_err("Could not detect use-after-free: ptr1 == ptr2\n");
428 static noinline void __init kfree_via_page(void)
433 unsigned long offset;
435 pr_info("invalid-free false positive (via page)\n");
436 ptr = kmalloc(size, GFP_KERNEL);
438 pr_err("Allocation failed\n");
442 page = virt_to_page(ptr);
443 offset = offset_in_page(ptr);
444 kfree(page_address(page) + offset);
447 static noinline void __init kfree_via_phys(void)
453 pr_info("invalid-free false positive (via phys)\n");
454 ptr = kmalloc(size, GFP_KERNEL);
456 pr_err("Allocation failed\n");
460 phys = virt_to_phys(ptr);
461 kfree(phys_to_virt(phys));
464 static noinline void __init kmem_cache_oob(void)
468 struct kmem_cache *cache = kmem_cache_create("test_cache",
472 pr_err("Cache allocation failed\n");
475 pr_info("out-of-bounds in kmem_cache_alloc\n");
476 p = kmem_cache_alloc(cache, GFP_KERNEL);
478 pr_err("Allocation failed\n");
479 kmem_cache_destroy(cache);
483 *p = p[size + OOB_TAG_OFF];
485 kmem_cache_free(cache, p);
486 kmem_cache_destroy(cache);
489 static noinline void __init memcg_accounted_kmem_cache(void)
494 struct kmem_cache *cache;
496 cache = kmem_cache_create("test_cache", size, 0, SLAB_ACCOUNT, NULL);
498 pr_err("Cache allocation failed\n");
502 pr_info("allocate memcg accounted object\n");
504 * Several allocations with a delay to allow for lazy per memcg kmem
507 for (i = 0; i < 5; i++) {
508 p = kmem_cache_alloc(cache, GFP_KERNEL);
512 kmem_cache_free(cache, p);
517 kmem_cache_destroy(cache);
520 static char global_array[10];
522 static noinline void __init kasan_global_oob(void)
525 char *p = &global_array[ARRAY_SIZE(global_array) + i];
527 pr_info("out-of-bounds global variable\n");
531 static noinline void __init kasan_stack_oob(void)
533 char stack_array[10];
534 volatile int i = OOB_TAG_OFF;
535 char *p = &stack_array[ARRAY_SIZE(stack_array) + i];
537 pr_info("out-of-bounds on stack\n");
541 static noinline void __init ksize_unpoisons_memory(void)
544 size_t size = 123, real_size;
546 pr_info("ksize() unpoisons the whole allocated chunk\n");
547 ptr = kmalloc(size, GFP_KERNEL);
549 pr_err("Allocation failed\n");
552 real_size = ksize(ptr);
553 /* This access doesn't trigger an error. */
556 ptr[real_size] = 'y';
560 static noinline void __init copy_user_test(void)
563 char __user *usermem;
567 kmem = kmalloc(size, GFP_KERNEL);
571 usermem = (char __user *)vm_mmap(NULL, 0, PAGE_SIZE,
572 PROT_READ | PROT_WRITE | PROT_EXEC,
573 MAP_ANONYMOUS | MAP_PRIVATE, 0);
574 if (IS_ERR(usermem)) {
575 pr_err("Failed to allocate user memory\n");
580 pr_info("out-of-bounds in copy_from_user()\n");
581 unused = copy_from_user(kmem, usermem, size + 1 + OOB_TAG_OFF);
583 pr_info("out-of-bounds in copy_to_user()\n");
584 unused = copy_to_user(usermem, kmem, size + 1 + OOB_TAG_OFF);
586 pr_info("out-of-bounds in __copy_from_user()\n");
587 unused = __copy_from_user(kmem, usermem, size + 1 + OOB_TAG_OFF);
589 pr_info("out-of-bounds in __copy_to_user()\n");
590 unused = __copy_to_user(usermem, kmem, size + 1 + OOB_TAG_OFF);
592 pr_info("out-of-bounds in __copy_from_user_inatomic()\n");
593 unused = __copy_from_user_inatomic(kmem, usermem, size + 1 + OOB_TAG_OFF);
595 pr_info("out-of-bounds in __copy_to_user_inatomic()\n");
596 unused = __copy_to_user_inatomic(usermem, kmem, size + 1 + OOB_TAG_OFF);
598 pr_info("out-of-bounds in strncpy_from_user()\n");
599 unused = strncpy_from_user(kmem, usermem, size + 1 + OOB_TAG_OFF);
601 vm_munmap((unsigned long)usermem, PAGE_SIZE);
605 static noinline void __init kasan_alloca_oob_left(void)
608 char alloca_array[i];
609 char *p = alloca_array - 1;
611 pr_info("out-of-bounds to left on alloca\n");
615 static noinline void __init kasan_alloca_oob_right(void)
618 char alloca_array[i];
619 char *p = alloca_array + i;
621 pr_info("out-of-bounds to right on alloca\n");
625 static noinline void __init kmem_cache_double_free(void)
629 struct kmem_cache *cache;
631 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
633 pr_err("Cache allocation failed\n");
636 pr_info("double-free on heap object\n");
637 p = kmem_cache_alloc(cache, GFP_KERNEL);
639 pr_err("Allocation failed\n");
640 kmem_cache_destroy(cache);
644 kmem_cache_free(cache, p);
645 kmem_cache_free(cache, p);
646 kmem_cache_destroy(cache);
649 static noinline void __init kmem_cache_invalid_free(void)
653 struct kmem_cache *cache;
655 cache = kmem_cache_create("test_cache", size, 0, SLAB_TYPESAFE_BY_RCU,
658 pr_err("Cache allocation failed\n");
661 pr_info("invalid-free of heap object\n");
662 p = kmem_cache_alloc(cache, GFP_KERNEL);
664 pr_err("Allocation failed\n");
665 kmem_cache_destroy(cache);
669 /* Trigger invalid free, the object doesn't get freed */
670 kmem_cache_free(cache, p + 1);
673 * Properly free the object to prevent the "Objects remaining in
674 * test_cache on __kmem_cache_shutdown" BUG failure.
676 kmem_cache_free(cache, p);
678 kmem_cache_destroy(cache);
681 static noinline void __init kasan_memchr(void)
686 pr_info("out-of-bounds in memchr\n");
687 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
691 kasan_ptr_result = memchr(ptr, '1', size + 1);
695 static noinline void __init kasan_memcmp(void)
701 pr_info("out-of-bounds in memcmp\n");
702 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
706 memset(arr, 0, sizeof(arr));
707 kasan_int_result = memcmp(ptr, arr, size + 1);
711 static noinline void __init kasan_strings(void)
716 pr_info("use-after-free in strchr\n");
717 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
724 * Try to cause only 1 invalid access (less spam in dmesg).
725 * For that we need ptr to point to zeroed byte.
726 * Skip metadata that could be stored in freed object so ptr
727 * will likely point to zeroed byte.
730 kasan_ptr_result = strchr(ptr, '1');
732 pr_info("use-after-free in strrchr\n");
733 kasan_ptr_result = strrchr(ptr, '1');
735 pr_info("use-after-free in strcmp\n");
736 kasan_int_result = strcmp(ptr, "2");
738 pr_info("use-after-free in strncmp\n");
739 kasan_int_result = strncmp(ptr, "2", 1);
741 pr_info("use-after-free in strlen\n");
742 kasan_int_result = strlen(ptr);
744 pr_info("use-after-free in strnlen\n");
745 kasan_int_result = strnlen(ptr, 1);
748 static noinline void __init kasan_bitops(void)
751 * Allocate 1 more byte, which causes kzalloc to round up to 16-bytes;
752 * this way we do not actually corrupt other memory.
754 long *bits = kzalloc(sizeof(*bits) + 1, GFP_KERNEL);
759 * Below calls try to access bit within allocated memory; however, the
760 * below accesses are still out-of-bounds, since bitops are defined to
761 * operate on the whole long the bit is in.
763 pr_info("out-of-bounds in set_bit\n");
764 set_bit(BITS_PER_LONG, bits);
766 pr_info("out-of-bounds in __set_bit\n");
767 __set_bit(BITS_PER_LONG, bits);
769 pr_info("out-of-bounds in clear_bit\n");
770 clear_bit(BITS_PER_LONG, bits);
772 pr_info("out-of-bounds in __clear_bit\n");
773 __clear_bit(BITS_PER_LONG, bits);
775 pr_info("out-of-bounds in clear_bit_unlock\n");
776 clear_bit_unlock(BITS_PER_LONG, bits);
778 pr_info("out-of-bounds in __clear_bit_unlock\n");
779 __clear_bit_unlock(BITS_PER_LONG, bits);
781 pr_info("out-of-bounds in change_bit\n");
782 change_bit(BITS_PER_LONG, bits);
784 pr_info("out-of-bounds in __change_bit\n");
785 __change_bit(BITS_PER_LONG, bits);
788 * Below calls try to access bit beyond allocated memory.
790 pr_info("out-of-bounds in test_and_set_bit\n");
791 test_and_set_bit(BITS_PER_LONG + BITS_PER_BYTE, bits);
793 pr_info("out-of-bounds in __test_and_set_bit\n");
794 __test_and_set_bit(BITS_PER_LONG + BITS_PER_BYTE, bits);
796 pr_info("out-of-bounds in test_and_set_bit_lock\n");
797 test_and_set_bit_lock(BITS_PER_LONG + BITS_PER_BYTE, bits);
799 pr_info("out-of-bounds in test_and_clear_bit\n");
800 test_and_clear_bit(BITS_PER_LONG + BITS_PER_BYTE, bits);
802 pr_info("out-of-bounds in __test_and_clear_bit\n");
803 __test_and_clear_bit(BITS_PER_LONG + BITS_PER_BYTE, bits);
805 pr_info("out-of-bounds in test_and_change_bit\n");
806 test_and_change_bit(BITS_PER_LONG + BITS_PER_BYTE, bits);
808 pr_info("out-of-bounds in __test_and_change_bit\n");
809 __test_and_change_bit(BITS_PER_LONG + BITS_PER_BYTE, bits);
811 pr_info("out-of-bounds in test_bit\n");
812 kasan_int_result = test_bit(BITS_PER_LONG + BITS_PER_BYTE, bits);
814 #if defined(clear_bit_unlock_is_negative_byte)
815 pr_info("out-of-bounds in clear_bit_unlock_is_negative_byte\n");
816 kasan_int_result = clear_bit_unlock_is_negative_byte(BITS_PER_LONG +
817 BITS_PER_BYTE, bits);
822 static noinline void __init kmalloc_double_kzfree(void)
827 pr_info("double-free (kfree_sensitive)\n");
828 ptr = kmalloc(size, GFP_KERNEL);
830 pr_err("Allocation failed\n");
834 kfree_sensitive(ptr);
835 kfree_sensitive(ptr);
838 #ifdef CONFIG_KASAN_VMALLOC
839 static noinline void __init vmalloc_oob(void)
843 pr_info("vmalloc out-of-bounds\n");
846 * We have to be careful not to hit the guard page.
847 * The MMU will catch that and crash us.
849 area = vmalloc(3000);
851 pr_err("Allocation failed\n");
855 ((volatile char *)area)[3100];
859 static void __init vmalloc_oob(void) {}
862 static struct kasan_rcu_info {
867 static noinline void __init kasan_rcu_reclaim(struct rcu_head *rp)
869 struct kasan_rcu_info *fp = container_of(rp,
870 struct kasan_rcu_info, rcu);
876 static noinline void __init kasan_rcu_uaf(void)
878 struct kasan_rcu_info *ptr;
880 pr_info("use-after-free in kasan_rcu_reclaim\n");
881 ptr = kmalloc(sizeof(struct kasan_rcu_info), GFP_KERNEL);
883 pr_err("Allocation failed\n");
887 global_rcu_ptr = rcu_dereference_protected(ptr, NULL);
888 call_rcu(&global_rcu_ptr->rcu, kasan_rcu_reclaim);
891 static int __init kmalloc_tests_init(void)
894 * Temporarily enable multi-shot mode. Otherwise, we'd only get a
895 * report for the first case.
897 bool multishot = kasan_save_enable_multi_shot();
901 kmalloc_node_oob_right();
903 kmalloc_pagealloc_oob_right();
904 kmalloc_pagealloc_uaf();
905 kmalloc_pagealloc_invalid_free();
907 kmalloc_large_oob_right();
908 kmalloc_oob_krealloc_more();
909 kmalloc_oob_krealloc_less();
911 kmalloc_oob_in_memset();
912 kmalloc_oob_memset_2();
913 kmalloc_oob_memset_4();
914 kmalloc_oob_memset_8();
915 kmalloc_oob_memset_16();
916 kmalloc_memmove_invalid_size();
918 kmalloc_uaf_memset();
923 memcg_accounted_kmem_cache();
926 kasan_alloca_oob_left();
927 kasan_alloca_oob_right();
928 ksize_unpoisons_memory();
930 kmem_cache_double_free();
931 kmem_cache_invalid_free();
936 kmalloc_double_kzfree();
940 kasan_restore_multi_shot(multishot);
945 module_init(kmalloc_tests_init);
946 MODULE_LICENSE("GPL");