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 * Note: test functions are marked noinline so that their names appear in
31 static noinline void __init kmalloc_oob_right(void)
36 pr_info("out-of-bounds to right\n");
37 ptr = kmalloc(size, GFP_KERNEL);
39 pr_err("Allocation failed\n");
47 static noinline void __init kmalloc_oob_left(void)
52 pr_info("out-of-bounds to left\n");
53 ptr = kmalloc(size, GFP_KERNEL);
55 pr_err("Allocation failed\n");
63 static noinline void __init kmalloc_node_oob_right(void)
68 pr_info("kmalloc_node(): out-of-bounds to right\n");
69 ptr = kmalloc_node(size, GFP_KERNEL, 0);
71 pr_err("Allocation failed\n");
80 static noinline void __init kmalloc_pagealloc_oob_right(void)
83 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
85 /* Allocate a chunk that does not fit into a SLUB cache to trigger
86 * the page allocator fallback.
88 pr_info("kmalloc pagealloc allocation: out-of-bounds to right\n");
89 ptr = kmalloc(size, GFP_KERNEL);
91 pr_err("Allocation failed\n");
99 static noinline void __init kmalloc_pagealloc_uaf(void)
102 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
104 pr_info("kmalloc pagealloc allocation: use-after-free\n");
105 ptr = kmalloc(size, GFP_KERNEL);
107 pr_err("Allocation failed\n");
115 static noinline void __init kmalloc_pagealloc_invalid_free(void)
118 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
120 pr_info("kmalloc pagealloc allocation: invalid-free\n");
121 ptr = kmalloc(size, GFP_KERNEL);
123 pr_err("Allocation failed\n");
131 static noinline void __init kmalloc_large_oob_right(void)
134 size_t size = KMALLOC_MAX_CACHE_SIZE - 256;
135 /* Allocate a chunk that is large enough, but still fits into a slab
136 * and does not trigger the page allocator fallback in SLUB.
138 pr_info("kmalloc large allocation: out-of-bounds to right\n");
139 ptr = kmalloc(size, GFP_KERNEL);
141 pr_err("Allocation failed\n");
149 static noinline void __init kmalloc_oob_krealloc_more(void)
155 pr_info("out-of-bounds after krealloc more\n");
156 ptr1 = kmalloc(size1, GFP_KERNEL);
157 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
158 if (!ptr1 || !ptr2) {
159 pr_err("Allocation failed\n");
168 static noinline void __init kmalloc_oob_krealloc_less(void)
174 pr_info("out-of-bounds after krealloc less\n");
175 ptr1 = kmalloc(size1, GFP_KERNEL);
176 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
177 if (!ptr1 || !ptr2) {
178 pr_err("Allocation failed\n");
186 static noinline void __init kmalloc_oob_16(void)
192 pr_info("kmalloc out-of-bounds for 16-bytes access\n");
193 ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
194 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
195 if (!ptr1 || !ptr2) {
196 pr_err("Allocation failed\n");
206 static noinline void __init kmalloc_oob_memset_2(void)
211 pr_info("out-of-bounds in memset2\n");
212 ptr = kmalloc(size, GFP_KERNEL);
214 pr_err("Allocation failed\n");
222 static noinline void __init kmalloc_oob_memset_4(void)
227 pr_info("out-of-bounds in memset4\n");
228 ptr = kmalloc(size, GFP_KERNEL);
230 pr_err("Allocation failed\n");
239 static noinline void __init kmalloc_oob_memset_8(void)
244 pr_info("out-of-bounds in memset8\n");
245 ptr = kmalloc(size, GFP_KERNEL);
247 pr_err("Allocation failed\n");
255 static noinline void __init kmalloc_oob_memset_16(void)
260 pr_info("out-of-bounds in memset16\n");
261 ptr = kmalloc(size, GFP_KERNEL);
263 pr_err("Allocation failed\n");
267 memset(ptr+1, 0, 16);
271 static noinline void __init kmalloc_oob_in_memset(void)
276 pr_info("out-of-bounds in memset\n");
277 ptr = kmalloc(size, GFP_KERNEL);
279 pr_err("Allocation failed\n");
283 memset(ptr, 0, size+5);
287 static noinline void __init kmalloc_uaf(void)
292 pr_info("use-after-free\n");
293 ptr = kmalloc(size, GFP_KERNEL);
295 pr_err("Allocation failed\n");
303 static noinline void __init kmalloc_uaf_memset(void)
308 pr_info("use-after-free in memset\n");
309 ptr = kmalloc(size, GFP_KERNEL);
311 pr_err("Allocation failed\n");
316 memset(ptr, 0, size);
319 static noinline void __init kmalloc_uaf2(void)
324 pr_info("use-after-free after another kmalloc\n");
325 ptr1 = kmalloc(size, GFP_KERNEL);
327 pr_err("Allocation failed\n");
332 ptr2 = kmalloc(size, GFP_KERNEL);
334 pr_err("Allocation failed\n");
340 pr_err("Could not detect use-after-free: ptr1 == ptr2\n");
344 static noinline void __init kfree_via_page(void)
349 unsigned long offset;
351 pr_info("invalid-free false positive (via page)\n");
352 ptr = kmalloc(size, GFP_KERNEL);
354 pr_err("Allocation failed\n");
358 page = virt_to_page(ptr);
359 offset = offset_in_page(ptr);
360 kfree(page_address(page) + offset);
363 static noinline void __init kfree_via_phys(void)
369 pr_info("invalid-free false positive (via phys)\n");
370 ptr = kmalloc(size, GFP_KERNEL);
372 pr_err("Allocation failed\n");
376 phys = virt_to_phys(ptr);
377 kfree(phys_to_virt(phys));
380 static noinline void __init kmem_cache_oob(void)
384 struct kmem_cache *cache = kmem_cache_create("test_cache",
388 pr_err("Cache allocation failed\n");
391 pr_info("out-of-bounds in kmem_cache_alloc\n");
392 p = kmem_cache_alloc(cache, GFP_KERNEL);
394 pr_err("Allocation failed\n");
395 kmem_cache_destroy(cache);
400 kmem_cache_free(cache, p);
401 kmem_cache_destroy(cache);
404 static noinline void __init memcg_accounted_kmem_cache(void)
409 struct kmem_cache *cache;
411 cache = kmem_cache_create("test_cache", size, 0, SLAB_ACCOUNT, NULL);
413 pr_err("Cache allocation failed\n");
417 pr_info("allocate memcg accounted object\n");
419 * Several allocations with a delay to allow for lazy per memcg kmem
422 for (i = 0; i < 5; i++) {
423 p = kmem_cache_alloc(cache, GFP_KERNEL);
427 kmem_cache_free(cache, p);
432 kmem_cache_destroy(cache);
435 static char global_array[10];
437 static noinline void __init kasan_global_oob(void)
440 char *p = &global_array[ARRAY_SIZE(global_array) + i];
442 pr_info("out-of-bounds global variable\n");
446 static noinline void __init kasan_stack_oob(void)
448 char stack_array[10];
450 char *p = &stack_array[ARRAY_SIZE(stack_array) + i];
452 pr_info("out-of-bounds on stack\n");
456 static noinline void __init ksize_unpoisons_memory(void)
459 size_t size = 123, real_size;
461 pr_info("ksize() unpoisons the whole allocated chunk\n");
462 ptr = kmalloc(size, GFP_KERNEL);
464 pr_err("Allocation failed\n");
467 real_size = ksize(ptr);
468 /* This access doesn't trigger an error. */
471 ptr[real_size] = 'y';
475 static noinline void __init copy_user_test(void)
478 char __user *usermem;
482 kmem = kmalloc(size, GFP_KERNEL);
486 usermem = (char __user *)vm_mmap(NULL, 0, PAGE_SIZE,
487 PROT_READ | PROT_WRITE | PROT_EXEC,
488 MAP_ANONYMOUS | MAP_PRIVATE, 0);
489 if (IS_ERR(usermem)) {
490 pr_err("Failed to allocate user memory\n");
495 pr_info("out-of-bounds in copy_from_user()\n");
496 unused = copy_from_user(kmem, usermem, size + 1);
498 pr_info("out-of-bounds in copy_to_user()\n");
499 unused = copy_to_user(usermem, kmem, size + 1);
501 pr_info("out-of-bounds in __copy_from_user()\n");
502 unused = __copy_from_user(kmem, usermem, size + 1);
504 pr_info("out-of-bounds in __copy_to_user()\n");
505 unused = __copy_to_user(usermem, kmem, size + 1);
507 pr_info("out-of-bounds in __copy_from_user_inatomic()\n");
508 unused = __copy_from_user_inatomic(kmem, usermem, size + 1);
510 pr_info("out-of-bounds in __copy_to_user_inatomic()\n");
511 unused = __copy_to_user_inatomic(usermem, kmem, size + 1);
513 pr_info("out-of-bounds in strncpy_from_user()\n");
514 unused = strncpy_from_user(kmem, usermem, size + 1);
516 vm_munmap((unsigned long)usermem, PAGE_SIZE);
520 static noinline void __init kasan_alloca_oob_left(void)
523 char alloca_array[i];
524 char *p = alloca_array - 1;
526 pr_info("out-of-bounds to left on alloca\n");
530 static noinline void __init kasan_alloca_oob_right(void)
533 char alloca_array[i];
534 char *p = alloca_array + i;
536 pr_info("out-of-bounds to right on alloca\n");
540 static noinline void __init kmem_cache_double_free(void)
544 struct kmem_cache *cache;
546 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
548 pr_err("Cache allocation failed\n");
551 pr_info("double-free on heap object\n");
552 p = kmem_cache_alloc(cache, GFP_KERNEL);
554 pr_err("Allocation failed\n");
555 kmem_cache_destroy(cache);
559 kmem_cache_free(cache, p);
560 kmem_cache_free(cache, p);
561 kmem_cache_destroy(cache);
564 static noinline void __init kmem_cache_invalid_free(void)
568 struct kmem_cache *cache;
570 cache = kmem_cache_create("test_cache", size, 0, SLAB_TYPESAFE_BY_RCU,
573 pr_err("Cache allocation failed\n");
576 pr_info("invalid-free of heap object\n");
577 p = kmem_cache_alloc(cache, GFP_KERNEL);
579 pr_err("Allocation failed\n");
580 kmem_cache_destroy(cache);
584 /* Trigger invalid free, the object doesn't get freed */
585 kmem_cache_free(cache, p + 1);
588 * Properly free the object to prevent the "Objects remaining in
589 * test_cache on __kmem_cache_shutdown" BUG failure.
591 kmem_cache_free(cache, p);
593 kmem_cache_destroy(cache);
596 static noinline void __init kasan_memchr(void)
601 pr_info("out-of-bounds in memchr\n");
602 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
606 memchr(ptr, '1', size + 1);
610 static noinline void __init kasan_memcmp(void)
616 pr_info("out-of-bounds in memcmp\n");
617 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
621 memset(arr, 0, sizeof(arr));
622 memcmp(ptr, arr, size+1);
626 static noinline void __init kasan_strings(void)
631 pr_info("use-after-free in strchr\n");
632 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
639 * Try to cause only 1 invalid access (less spam in dmesg).
640 * For that we need ptr to point to zeroed byte.
641 * Skip metadata that could be stored in freed object so ptr
642 * will likely point to zeroed byte.
647 pr_info("use-after-free in strrchr\n");
650 pr_info("use-after-free in strcmp\n");
653 pr_info("use-after-free in strncmp\n");
654 strncmp(ptr, "2", 1);
656 pr_info("use-after-free in strlen\n");
659 pr_info("use-after-free in strnlen\n");
663 static noinline void __init kasan_bitops(void)
666 * Allocate 1 more byte, which causes kzalloc to round up to 16-bytes;
667 * this way we do not actually corrupt other memory.
669 long *bits = kzalloc(sizeof(*bits) + 1, GFP_KERNEL);
674 * Below calls try to access bit within allocated memory; however, the
675 * below accesses are still out-of-bounds, since bitops are defined to
676 * operate on the whole long the bit is in.
678 pr_info("out-of-bounds in set_bit\n");
679 set_bit(BITS_PER_LONG, bits);
681 pr_info("out-of-bounds in __set_bit\n");
682 __set_bit(BITS_PER_LONG, bits);
684 pr_info("out-of-bounds in clear_bit\n");
685 clear_bit(BITS_PER_LONG, bits);
687 pr_info("out-of-bounds in __clear_bit\n");
688 __clear_bit(BITS_PER_LONG, bits);
690 pr_info("out-of-bounds in clear_bit_unlock\n");
691 clear_bit_unlock(BITS_PER_LONG, bits);
693 pr_info("out-of-bounds in __clear_bit_unlock\n");
694 __clear_bit_unlock(BITS_PER_LONG, bits);
696 pr_info("out-of-bounds in change_bit\n");
697 change_bit(BITS_PER_LONG, bits);
699 pr_info("out-of-bounds in __change_bit\n");
700 __change_bit(BITS_PER_LONG, bits);
703 * Below calls try to access bit beyond allocated memory.
705 pr_info("out-of-bounds in test_and_set_bit\n");
706 test_and_set_bit(BITS_PER_LONG + BITS_PER_BYTE, bits);
708 pr_info("out-of-bounds in __test_and_set_bit\n");
709 __test_and_set_bit(BITS_PER_LONG + BITS_PER_BYTE, bits);
711 pr_info("out-of-bounds in test_and_set_bit_lock\n");
712 test_and_set_bit_lock(BITS_PER_LONG + BITS_PER_BYTE, bits);
714 pr_info("out-of-bounds in test_and_clear_bit\n");
715 test_and_clear_bit(BITS_PER_LONG + BITS_PER_BYTE, bits);
717 pr_info("out-of-bounds in __test_and_clear_bit\n");
718 __test_and_clear_bit(BITS_PER_LONG + BITS_PER_BYTE, bits);
720 pr_info("out-of-bounds in test_and_change_bit\n");
721 test_and_change_bit(BITS_PER_LONG + BITS_PER_BYTE, bits);
723 pr_info("out-of-bounds in __test_and_change_bit\n");
724 __test_and_change_bit(BITS_PER_LONG + BITS_PER_BYTE, bits);
726 pr_info("out-of-bounds in test_bit\n");
727 (void)test_bit(BITS_PER_LONG + BITS_PER_BYTE, bits);
729 #if defined(clear_bit_unlock_is_negative_byte)
730 pr_info("out-of-bounds in clear_bit_unlock_is_negative_byte\n");
731 clear_bit_unlock_is_negative_byte(BITS_PER_LONG + BITS_PER_BYTE, bits);
736 static noinline void __init kmalloc_double_kzfree(void)
741 pr_info("double-free (kzfree)\n");
742 ptr = kmalloc(size, GFP_KERNEL);
744 pr_err("Allocation failed\n");
752 #ifdef CONFIG_KASAN_VMALLOC
753 static noinline void __init vmalloc_oob(void)
757 pr_info("vmalloc out-of-bounds\n");
760 * We have to be careful not to hit the guard page.
761 * The MMU will catch that and crash us.
763 area = vmalloc(3000);
765 pr_err("Allocation failed\n");
769 ((volatile char *)area)[3100];
773 static void __init vmalloc_oob(void) {}
776 static int __init kmalloc_tests_init(void)
779 * Temporarily enable multi-shot mode. Otherwise, we'd only get a
780 * report for the first case.
782 bool multishot = kasan_save_enable_multi_shot();
786 kmalloc_node_oob_right();
788 kmalloc_pagealloc_oob_right();
789 kmalloc_pagealloc_uaf();
790 kmalloc_pagealloc_invalid_free();
792 kmalloc_large_oob_right();
793 kmalloc_oob_krealloc_more();
794 kmalloc_oob_krealloc_less();
796 kmalloc_oob_in_memset();
797 kmalloc_oob_memset_2();
798 kmalloc_oob_memset_4();
799 kmalloc_oob_memset_8();
800 kmalloc_oob_memset_16();
802 kmalloc_uaf_memset();
807 memcg_accounted_kmem_cache();
810 kasan_alloca_oob_left();
811 kasan_alloca_oob_right();
812 ksize_unpoisons_memory();
814 kmem_cache_double_free();
815 kmem_cache_invalid_free();
820 kmalloc_double_kzfree();
823 kasan_restore_multi_shot(multishot);
828 module_init(kmalloc_tests_init);
829 MODULE_LICENSE("GPL");