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>
27 * We assign some test results to these globals to make sure the tests
28 * are not eliminated as dead code.
32 void *kasan_ptr_result;
35 * Note: test functions are marked noinline so that their names appear in
39 static noinline void __init kmalloc_oob_right(void)
44 pr_info("out-of-bounds to right\n");
45 ptr = kmalloc(size, GFP_KERNEL);
47 pr_err("Allocation failed\n");
55 static noinline void __init kmalloc_oob_left(void)
60 pr_info("out-of-bounds to left\n");
61 ptr = kmalloc(size, GFP_KERNEL);
63 pr_err("Allocation failed\n");
71 static noinline void __init kmalloc_node_oob_right(void)
76 pr_info("kmalloc_node(): out-of-bounds to right\n");
77 ptr = kmalloc_node(size, GFP_KERNEL, 0);
79 pr_err("Allocation failed\n");
88 static noinline void __init kmalloc_pagealloc_oob_right(void)
91 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
93 /* Allocate a chunk that does not fit into a SLUB cache to trigger
94 * the page allocator fallback.
96 pr_info("kmalloc pagealloc allocation: out-of-bounds to right\n");
97 ptr = kmalloc(size, GFP_KERNEL);
99 pr_err("Allocation failed\n");
107 static noinline void __init kmalloc_pagealloc_uaf(void)
110 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
112 pr_info("kmalloc pagealloc allocation: use-after-free\n");
113 ptr = kmalloc(size, GFP_KERNEL);
115 pr_err("Allocation failed\n");
123 static noinline void __init kmalloc_pagealloc_invalid_free(void)
126 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
128 pr_info("kmalloc pagealloc allocation: invalid-free\n");
129 ptr = kmalloc(size, GFP_KERNEL);
131 pr_err("Allocation failed\n");
139 static noinline void __init kmalloc_large_oob_right(void)
142 size_t size = KMALLOC_MAX_CACHE_SIZE - 256;
143 /* Allocate a chunk that is large enough, but still fits into a slab
144 * and does not trigger the page allocator fallback in SLUB.
146 pr_info("kmalloc large allocation: out-of-bounds to right\n");
147 ptr = kmalloc(size, GFP_KERNEL);
149 pr_err("Allocation failed\n");
157 static noinline void __init kmalloc_oob_krealloc_more(void)
163 pr_info("out-of-bounds after krealloc more\n");
164 ptr1 = kmalloc(size1, GFP_KERNEL);
165 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
166 if (!ptr1 || !ptr2) {
167 pr_err("Allocation failed\n");
177 static noinline void __init kmalloc_oob_krealloc_less(void)
183 pr_info("out-of-bounds after krealloc less\n");
184 ptr1 = kmalloc(size1, GFP_KERNEL);
185 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
186 if (!ptr1 || !ptr2) {
187 pr_err("Allocation failed\n");
195 static noinline void __init kmalloc_oob_16(void)
201 pr_info("kmalloc out-of-bounds for 16-bytes access\n");
202 ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
203 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
204 if (!ptr1 || !ptr2) {
205 pr_err("Allocation failed\n");
215 static noinline void __init kmalloc_oob_memset_2(void)
220 pr_info("out-of-bounds in memset2\n");
221 ptr = kmalloc(size, GFP_KERNEL);
223 pr_err("Allocation failed\n");
231 static noinline void __init kmalloc_oob_memset_4(void)
236 pr_info("out-of-bounds in memset4\n");
237 ptr = kmalloc(size, GFP_KERNEL);
239 pr_err("Allocation failed\n");
248 static noinline void __init kmalloc_oob_memset_8(void)
253 pr_info("out-of-bounds in memset8\n");
254 ptr = kmalloc(size, GFP_KERNEL);
256 pr_err("Allocation failed\n");
264 static noinline void __init kmalloc_oob_memset_16(void)
269 pr_info("out-of-bounds in memset16\n");
270 ptr = kmalloc(size, GFP_KERNEL);
272 pr_err("Allocation failed\n");
276 memset(ptr+1, 0, 16);
280 static noinline void __init kmalloc_oob_in_memset(void)
285 pr_info("out-of-bounds in memset\n");
286 ptr = kmalloc(size, GFP_KERNEL);
288 pr_err("Allocation failed\n");
292 memset(ptr, 0, size+5);
296 static noinline void __init kmalloc_memmove_invalid_size(void)
300 volatile size_t invalid_size = -2;
302 pr_info("invalid size in memmove\n");
303 ptr = kmalloc(size, GFP_KERNEL);
305 pr_err("Allocation failed\n");
309 memset((char *)ptr, 0, 64);
310 memmove((char *)ptr, (char *)ptr + 4, invalid_size);
314 static noinline void __init kmalloc_uaf(void)
319 pr_info("use-after-free\n");
320 ptr = kmalloc(size, GFP_KERNEL);
322 pr_err("Allocation failed\n");
330 static noinline void __init kmalloc_uaf_memset(void)
335 pr_info("use-after-free in memset\n");
336 ptr = kmalloc(size, GFP_KERNEL);
338 pr_err("Allocation failed\n");
343 memset(ptr, 0, size);
346 static noinline void __init kmalloc_uaf2(void)
351 pr_info("use-after-free after another kmalloc\n");
352 ptr1 = kmalloc(size, GFP_KERNEL);
354 pr_err("Allocation failed\n");
359 ptr2 = kmalloc(size, GFP_KERNEL);
361 pr_err("Allocation failed\n");
367 pr_err("Could not detect use-after-free: ptr1 == ptr2\n");
371 static noinline void __init kfree_via_page(void)
376 unsigned long offset;
378 pr_info("invalid-free false positive (via page)\n");
379 ptr = kmalloc(size, GFP_KERNEL);
381 pr_err("Allocation failed\n");
385 page = virt_to_page(ptr);
386 offset = offset_in_page(ptr);
387 kfree(page_address(page) + offset);
390 static noinline void __init kfree_via_phys(void)
396 pr_info("invalid-free false positive (via phys)\n");
397 ptr = kmalloc(size, GFP_KERNEL);
399 pr_err("Allocation failed\n");
403 phys = virt_to_phys(ptr);
404 kfree(phys_to_virt(phys));
407 static noinline void __init kmem_cache_oob(void)
411 struct kmem_cache *cache = kmem_cache_create("test_cache",
415 pr_err("Cache allocation failed\n");
418 pr_info("out-of-bounds in kmem_cache_alloc\n");
419 p = kmem_cache_alloc(cache, GFP_KERNEL);
421 pr_err("Allocation failed\n");
422 kmem_cache_destroy(cache);
427 kmem_cache_free(cache, p);
428 kmem_cache_destroy(cache);
431 static noinline void __init memcg_accounted_kmem_cache(void)
436 struct kmem_cache *cache;
438 cache = kmem_cache_create("test_cache", size, 0, SLAB_ACCOUNT, NULL);
440 pr_err("Cache allocation failed\n");
444 pr_info("allocate memcg accounted object\n");
446 * Several allocations with a delay to allow for lazy per memcg kmem
449 for (i = 0; i < 5; i++) {
450 p = kmem_cache_alloc(cache, GFP_KERNEL);
454 kmem_cache_free(cache, p);
459 kmem_cache_destroy(cache);
462 static char global_array[10];
464 static noinline void __init kasan_global_oob(void)
467 char *p = &global_array[ARRAY_SIZE(global_array) + i];
469 pr_info("out-of-bounds global variable\n");
473 static noinline void __init kasan_stack_oob(void)
475 char stack_array[10];
477 char *p = &stack_array[ARRAY_SIZE(stack_array) + i];
479 pr_info("out-of-bounds on stack\n");
483 static noinline void __init ksize_unpoisons_memory(void)
486 size_t size = 123, real_size;
488 pr_info("ksize() unpoisons the whole allocated chunk\n");
489 ptr = kmalloc(size, GFP_KERNEL);
491 pr_err("Allocation failed\n");
494 real_size = ksize(ptr);
495 /* This access doesn't trigger an error. */
498 ptr[real_size] = 'y';
502 static noinline void __init copy_user_test(void)
505 char __user *usermem;
509 kmem = kmalloc(size, GFP_KERNEL);
513 usermem = (char __user *)vm_mmap(NULL, 0, PAGE_SIZE,
514 PROT_READ | PROT_WRITE | PROT_EXEC,
515 MAP_ANONYMOUS | MAP_PRIVATE, 0);
516 if (IS_ERR(usermem)) {
517 pr_err("Failed to allocate user memory\n");
522 pr_info("out-of-bounds in copy_from_user()\n");
523 unused = copy_from_user(kmem, usermem, size + 1);
525 pr_info("out-of-bounds in copy_to_user()\n");
526 unused = copy_to_user(usermem, kmem, size + 1);
528 pr_info("out-of-bounds in __copy_from_user()\n");
529 unused = __copy_from_user(kmem, usermem, size + 1);
531 pr_info("out-of-bounds in __copy_to_user()\n");
532 unused = __copy_to_user(usermem, kmem, size + 1);
534 pr_info("out-of-bounds in __copy_from_user_inatomic()\n");
535 unused = __copy_from_user_inatomic(kmem, usermem, size + 1);
537 pr_info("out-of-bounds in __copy_to_user_inatomic()\n");
538 unused = __copy_to_user_inatomic(usermem, kmem, size + 1);
540 pr_info("out-of-bounds in strncpy_from_user()\n");
541 unused = strncpy_from_user(kmem, usermem, size + 1);
543 vm_munmap((unsigned long)usermem, PAGE_SIZE);
547 static noinline void __init kasan_alloca_oob_left(void)
550 char alloca_array[i];
551 char *p = alloca_array - 1;
553 pr_info("out-of-bounds to left on alloca\n");
557 static noinline void __init kasan_alloca_oob_right(void)
560 char alloca_array[i];
561 char *p = alloca_array + i;
563 pr_info("out-of-bounds to right on alloca\n");
567 static noinline void __init kmem_cache_double_free(void)
571 struct kmem_cache *cache;
573 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
575 pr_err("Cache allocation failed\n");
578 pr_info("double-free on heap object\n");
579 p = kmem_cache_alloc(cache, GFP_KERNEL);
581 pr_err("Allocation failed\n");
582 kmem_cache_destroy(cache);
586 kmem_cache_free(cache, p);
587 kmem_cache_free(cache, p);
588 kmem_cache_destroy(cache);
591 static noinline void __init kmem_cache_invalid_free(void)
595 struct kmem_cache *cache;
597 cache = kmem_cache_create("test_cache", size, 0, SLAB_TYPESAFE_BY_RCU,
600 pr_err("Cache allocation failed\n");
603 pr_info("invalid-free of heap object\n");
604 p = kmem_cache_alloc(cache, GFP_KERNEL);
606 pr_err("Allocation failed\n");
607 kmem_cache_destroy(cache);
611 /* Trigger invalid free, the object doesn't get freed */
612 kmem_cache_free(cache, p + 1);
615 * Properly free the object to prevent the "Objects remaining in
616 * test_cache on __kmem_cache_shutdown" BUG failure.
618 kmem_cache_free(cache, p);
620 kmem_cache_destroy(cache);
623 static noinline void __init kasan_memchr(void)
628 pr_info("out-of-bounds in memchr\n");
629 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
633 kasan_ptr_result = memchr(ptr, '1', size + 1);
637 static noinline void __init kasan_memcmp(void)
643 pr_info("out-of-bounds in memcmp\n");
644 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
648 memset(arr, 0, sizeof(arr));
649 kasan_int_result = memcmp(ptr, arr, size + 1);
653 static noinline void __init kasan_strings(void)
658 pr_info("use-after-free in strchr\n");
659 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
666 * Try to cause only 1 invalid access (less spam in dmesg).
667 * For that we need ptr to point to zeroed byte.
668 * Skip metadata that could be stored in freed object so ptr
669 * will likely point to zeroed byte.
672 kasan_ptr_result = strchr(ptr, '1');
674 pr_info("use-after-free in strrchr\n");
675 kasan_ptr_result = strrchr(ptr, '1');
677 pr_info("use-after-free in strcmp\n");
678 kasan_int_result = strcmp(ptr, "2");
680 pr_info("use-after-free in strncmp\n");
681 kasan_int_result = strncmp(ptr, "2", 1);
683 pr_info("use-after-free in strlen\n");
684 kasan_int_result = strlen(ptr);
686 pr_info("use-after-free in strnlen\n");
687 kasan_int_result = strnlen(ptr, 1);
690 static noinline void __init kasan_bitops(void)
693 * Allocate 1 more byte, which causes kzalloc to round up to 16-bytes;
694 * this way we do not actually corrupt other memory.
696 long *bits = kzalloc(sizeof(*bits) + 1, GFP_KERNEL);
701 * Below calls try to access bit within allocated memory; however, the
702 * below accesses are still out-of-bounds, since bitops are defined to
703 * operate on the whole long the bit is in.
705 pr_info("out-of-bounds in set_bit\n");
706 set_bit(BITS_PER_LONG, bits);
708 pr_info("out-of-bounds in __set_bit\n");
709 __set_bit(BITS_PER_LONG, bits);
711 pr_info("out-of-bounds in clear_bit\n");
712 clear_bit(BITS_PER_LONG, bits);
714 pr_info("out-of-bounds in __clear_bit\n");
715 __clear_bit(BITS_PER_LONG, bits);
717 pr_info("out-of-bounds in clear_bit_unlock\n");
718 clear_bit_unlock(BITS_PER_LONG, bits);
720 pr_info("out-of-bounds in __clear_bit_unlock\n");
721 __clear_bit_unlock(BITS_PER_LONG, bits);
723 pr_info("out-of-bounds in change_bit\n");
724 change_bit(BITS_PER_LONG, bits);
726 pr_info("out-of-bounds in __change_bit\n");
727 __change_bit(BITS_PER_LONG, bits);
730 * Below calls try to access bit beyond allocated memory.
732 pr_info("out-of-bounds in test_and_set_bit\n");
733 test_and_set_bit(BITS_PER_LONG + BITS_PER_BYTE, bits);
735 pr_info("out-of-bounds in __test_and_set_bit\n");
736 __test_and_set_bit(BITS_PER_LONG + BITS_PER_BYTE, bits);
738 pr_info("out-of-bounds in test_and_set_bit_lock\n");
739 test_and_set_bit_lock(BITS_PER_LONG + BITS_PER_BYTE, bits);
741 pr_info("out-of-bounds in test_and_clear_bit\n");
742 test_and_clear_bit(BITS_PER_LONG + BITS_PER_BYTE, bits);
744 pr_info("out-of-bounds in __test_and_clear_bit\n");
745 __test_and_clear_bit(BITS_PER_LONG + BITS_PER_BYTE, bits);
747 pr_info("out-of-bounds in test_and_change_bit\n");
748 test_and_change_bit(BITS_PER_LONG + BITS_PER_BYTE, bits);
750 pr_info("out-of-bounds in __test_and_change_bit\n");
751 __test_and_change_bit(BITS_PER_LONG + BITS_PER_BYTE, bits);
753 pr_info("out-of-bounds in test_bit\n");
754 kasan_int_result = test_bit(BITS_PER_LONG + BITS_PER_BYTE, bits);
756 #if defined(clear_bit_unlock_is_negative_byte)
757 pr_info("out-of-bounds in clear_bit_unlock_is_negative_byte\n");
758 kasan_int_result = clear_bit_unlock_is_negative_byte(BITS_PER_LONG +
759 BITS_PER_BYTE, bits);
764 static noinline void __init kmalloc_double_kzfree(void)
769 pr_info("double-free (kzfree)\n");
770 ptr = kmalloc(size, GFP_KERNEL);
772 pr_err("Allocation failed\n");
780 #ifdef CONFIG_KASAN_VMALLOC
781 static noinline void __init vmalloc_oob(void)
785 pr_info("vmalloc out-of-bounds\n");
788 * We have to be careful not to hit the guard page.
789 * The MMU will catch that and crash us.
791 area = vmalloc(3000);
793 pr_err("Allocation failed\n");
797 ((volatile char *)area)[3100];
801 static void __init vmalloc_oob(void) {}
804 static int __init kmalloc_tests_init(void)
807 * Temporarily enable multi-shot mode. Otherwise, we'd only get a
808 * report for the first case.
810 bool multishot = kasan_save_enable_multi_shot();
814 kmalloc_node_oob_right();
816 kmalloc_pagealloc_oob_right();
817 kmalloc_pagealloc_uaf();
818 kmalloc_pagealloc_invalid_free();
820 kmalloc_large_oob_right();
821 kmalloc_oob_krealloc_more();
822 kmalloc_oob_krealloc_less();
824 kmalloc_oob_in_memset();
825 kmalloc_oob_memset_2();
826 kmalloc_oob_memset_4();
827 kmalloc_oob_memset_8();
828 kmalloc_oob_memset_16();
829 kmalloc_memmove_invalid_size();
831 kmalloc_uaf_memset();
836 memcg_accounted_kmem_cache();
839 kasan_alloca_oob_left();
840 kasan_alloca_oob_right();
841 ksize_unpoisons_memory();
843 kmem_cache_double_free();
844 kmem_cache_invalid_free();
849 kmalloc_double_kzfree();
852 kasan_restore_multi_shot(multishot);
857 module_init(kmalloc_tests_init);
858 MODULE_LICENSE("GPL");