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>
26 * Note: test functions are marked noinline so that their names appear in
30 static noinline void __init kmalloc_oob_right(void)
35 pr_info("out-of-bounds to right\n");
36 ptr = kmalloc(size, GFP_KERNEL);
38 pr_err("Allocation failed\n");
46 static noinline void __init kmalloc_oob_left(void)
51 pr_info("out-of-bounds to left\n");
52 ptr = kmalloc(size, GFP_KERNEL);
54 pr_err("Allocation failed\n");
62 static noinline void __init kmalloc_node_oob_right(void)
67 pr_info("kmalloc_node(): out-of-bounds to right\n");
68 ptr = kmalloc_node(size, GFP_KERNEL, 0);
70 pr_err("Allocation failed\n");
79 static noinline void __init kmalloc_pagealloc_oob_right(void)
82 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
84 /* Allocate a chunk that does not fit into a SLUB cache to trigger
85 * the page allocator fallback.
87 pr_info("kmalloc pagealloc allocation: out-of-bounds to right\n");
88 ptr = kmalloc(size, GFP_KERNEL);
90 pr_err("Allocation failed\n");
98 static noinline void __init kmalloc_pagealloc_uaf(void)
101 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
103 pr_info("kmalloc pagealloc allocation: use-after-free\n");
104 ptr = kmalloc(size, GFP_KERNEL);
106 pr_err("Allocation failed\n");
114 static noinline void __init kmalloc_pagealloc_invalid_free(void)
117 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
119 pr_info("kmalloc pagealloc allocation: invalid-free\n");
120 ptr = kmalloc(size, GFP_KERNEL);
122 pr_err("Allocation failed\n");
130 static noinline void __init kmalloc_large_oob_right(void)
133 size_t size = KMALLOC_MAX_CACHE_SIZE - 256;
134 /* Allocate a chunk that is large enough, but still fits into a slab
135 * and does not trigger the page allocator fallback in SLUB.
137 pr_info("kmalloc large allocation: out-of-bounds to right\n");
138 ptr = kmalloc(size, GFP_KERNEL);
140 pr_err("Allocation failed\n");
148 static noinline void __init kmalloc_oob_krealloc_more(void)
154 pr_info("out-of-bounds after krealloc more\n");
155 ptr1 = kmalloc(size1, GFP_KERNEL);
156 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
157 if (!ptr1 || !ptr2) {
158 pr_err("Allocation failed\n");
167 static noinline void __init kmalloc_oob_krealloc_less(void)
173 pr_info("out-of-bounds after krealloc less\n");
174 ptr1 = kmalloc(size1, GFP_KERNEL);
175 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
176 if (!ptr1 || !ptr2) {
177 pr_err("Allocation failed\n");
185 static noinline void __init kmalloc_oob_16(void)
191 pr_info("kmalloc out-of-bounds for 16-bytes access\n");
192 ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
193 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
194 if (!ptr1 || !ptr2) {
195 pr_err("Allocation failed\n");
205 static noinline void __init kmalloc_oob_memset_2(void)
210 pr_info("out-of-bounds in memset2\n");
211 ptr = kmalloc(size, GFP_KERNEL);
213 pr_err("Allocation failed\n");
221 static noinline void __init kmalloc_oob_memset_4(void)
226 pr_info("out-of-bounds in memset4\n");
227 ptr = kmalloc(size, GFP_KERNEL);
229 pr_err("Allocation failed\n");
238 static noinline void __init kmalloc_oob_memset_8(void)
243 pr_info("out-of-bounds in memset8\n");
244 ptr = kmalloc(size, GFP_KERNEL);
246 pr_err("Allocation failed\n");
254 static noinline void __init kmalloc_oob_memset_16(void)
259 pr_info("out-of-bounds in memset16\n");
260 ptr = kmalloc(size, GFP_KERNEL);
262 pr_err("Allocation failed\n");
266 memset(ptr+1, 0, 16);
270 static noinline void __init kmalloc_oob_in_memset(void)
275 pr_info("out-of-bounds in memset\n");
276 ptr = kmalloc(size, GFP_KERNEL);
278 pr_err("Allocation failed\n");
282 memset(ptr, 0, size+5);
286 static noinline void __init kmalloc_uaf(void)
291 pr_info("use-after-free\n");
292 ptr = kmalloc(size, GFP_KERNEL);
294 pr_err("Allocation failed\n");
302 static noinline void __init kmalloc_uaf_memset(void)
307 pr_info("use-after-free in memset\n");
308 ptr = kmalloc(size, GFP_KERNEL);
310 pr_err("Allocation failed\n");
315 memset(ptr, 0, size);
318 static noinline void __init kmalloc_uaf2(void)
323 pr_info("use-after-free after another kmalloc\n");
324 ptr1 = kmalloc(size, GFP_KERNEL);
326 pr_err("Allocation failed\n");
331 ptr2 = kmalloc(size, GFP_KERNEL);
333 pr_err("Allocation failed\n");
339 pr_err("Could not detect use-after-free: ptr1 == ptr2\n");
343 static noinline void __init kfree_via_page(void)
348 unsigned long offset;
350 pr_info("invalid-free false positive (via page)\n");
351 ptr = kmalloc(size, GFP_KERNEL);
353 pr_err("Allocation failed\n");
357 page = virt_to_page(ptr);
358 offset = offset_in_page(ptr);
359 kfree(page_address(page) + offset);
362 static noinline void __init kfree_via_phys(void)
368 pr_info("invalid-free false positive (via phys)\n");
369 ptr = kmalloc(size, GFP_KERNEL);
371 pr_err("Allocation failed\n");
375 phys = virt_to_phys(ptr);
376 kfree(phys_to_virt(phys));
379 static noinline void __init kmem_cache_oob(void)
383 struct kmem_cache *cache = kmem_cache_create("test_cache",
387 pr_err("Cache allocation failed\n");
390 pr_info("out-of-bounds in kmem_cache_alloc\n");
391 p = kmem_cache_alloc(cache, GFP_KERNEL);
393 pr_err("Allocation failed\n");
394 kmem_cache_destroy(cache);
399 kmem_cache_free(cache, p);
400 kmem_cache_destroy(cache);
403 static noinline void __init memcg_accounted_kmem_cache(void)
408 struct kmem_cache *cache;
410 cache = kmem_cache_create("test_cache", size, 0, SLAB_ACCOUNT, NULL);
412 pr_err("Cache allocation failed\n");
416 pr_info("allocate memcg accounted object\n");
418 * Several allocations with a delay to allow for lazy per memcg kmem
421 for (i = 0; i < 5; i++) {
422 p = kmem_cache_alloc(cache, GFP_KERNEL);
426 kmem_cache_free(cache, p);
431 kmem_cache_destroy(cache);
434 static char global_array[10];
436 static noinline void __init kasan_global_oob(void)
439 char *p = &global_array[ARRAY_SIZE(global_array) + i];
441 pr_info("out-of-bounds global variable\n");
445 static noinline void __init kasan_stack_oob(void)
447 char stack_array[10];
449 char *p = &stack_array[ARRAY_SIZE(stack_array) + i];
451 pr_info("out-of-bounds on stack\n");
455 static noinline void __init ksize_unpoisons_memory(void)
458 size_t size = 123, real_size;
460 pr_info("ksize() unpoisons the whole allocated chunk\n");
461 ptr = kmalloc(size, GFP_KERNEL);
463 pr_err("Allocation failed\n");
466 real_size = ksize(ptr);
467 /* This access doesn't trigger an error. */
470 ptr[real_size] = 'y';
474 static noinline void __init copy_user_test(void)
477 char __user *usermem;
481 kmem = kmalloc(size, GFP_KERNEL);
485 usermem = (char __user *)vm_mmap(NULL, 0, PAGE_SIZE,
486 PROT_READ | PROT_WRITE | PROT_EXEC,
487 MAP_ANONYMOUS | MAP_PRIVATE, 0);
488 if (IS_ERR(usermem)) {
489 pr_err("Failed to allocate user memory\n");
494 pr_info("out-of-bounds in copy_from_user()\n");
495 unused = copy_from_user(kmem, usermem, size + 1);
497 pr_info("out-of-bounds in copy_to_user()\n");
498 unused = copy_to_user(usermem, kmem, size + 1);
500 pr_info("out-of-bounds in __copy_from_user()\n");
501 unused = __copy_from_user(kmem, usermem, size + 1);
503 pr_info("out-of-bounds in __copy_to_user()\n");
504 unused = __copy_to_user(usermem, kmem, size + 1);
506 pr_info("out-of-bounds in __copy_from_user_inatomic()\n");
507 unused = __copy_from_user_inatomic(kmem, usermem, size + 1);
509 pr_info("out-of-bounds in __copy_to_user_inatomic()\n");
510 unused = __copy_to_user_inatomic(usermem, kmem, size + 1);
512 pr_info("out-of-bounds in strncpy_from_user()\n");
513 unused = strncpy_from_user(kmem, usermem, size + 1);
515 vm_munmap((unsigned long)usermem, PAGE_SIZE);
519 static noinline void __init kasan_alloca_oob_left(void)
522 char alloca_array[i];
523 char *p = alloca_array - 1;
525 pr_info("out-of-bounds to left on alloca\n");
529 static noinline void __init kasan_alloca_oob_right(void)
532 char alloca_array[i];
533 char *p = alloca_array + i;
535 pr_info("out-of-bounds to right on alloca\n");
539 static noinline void __init kmem_cache_double_free(void)
543 struct kmem_cache *cache;
545 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
547 pr_err("Cache allocation failed\n");
550 pr_info("double-free on heap object\n");
551 p = kmem_cache_alloc(cache, GFP_KERNEL);
553 pr_err("Allocation failed\n");
554 kmem_cache_destroy(cache);
558 kmem_cache_free(cache, p);
559 kmem_cache_free(cache, p);
560 kmem_cache_destroy(cache);
563 static noinline void __init kmem_cache_invalid_free(void)
567 struct kmem_cache *cache;
569 cache = kmem_cache_create("test_cache", size, 0, SLAB_TYPESAFE_BY_RCU,
572 pr_err("Cache allocation failed\n");
575 pr_info("invalid-free of heap object\n");
576 p = kmem_cache_alloc(cache, GFP_KERNEL);
578 pr_err("Allocation failed\n");
579 kmem_cache_destroy(cache);
583 /* Trigger invalid free, the object doesn't get freed */
584 kmem_cache_free(cache, p + 1);
587 * Properly free the object to prevent the "Objects remaining in
588 * test_cache on __kmem_cache_shutdown" BUG failure.
590 kmem_cache_free(cache, p);
592 kmem_cache_destroy(cache);
595 static noinline void __init kasan_memchr(void)
600 pr_info("out-of-bounds in memchr\n");
601 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
605 memchr(ptr, '1', size + 1);
609 static noinline void __init kasan_memcmp(void)
615 pr_info("out-of-bounds in memcmp\n");
616 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
620 memset(arr, 0, sizeof(arr));
621 memcmp(ptr, arr, size+1);
625 static noinline void __init kasan_strings(void)
630 pr_info("use-after-free in strchr\n");
631 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
638 * Try to cause only 1 invalid access (less spam in dmesg).
639 * For that we need ptr to point to zeroed byte.
640 * Skip metadata that could be stored in freed object so ptr
641 * will likely point to zeroed byte.
646 pr_info("use-after-free in strrchr\n");
649 pr_info("use-after-free in strcmp\n");
652 pr_info("use-after-free in strncmp\n");
653 strncmp(ptr, "2", 1);
655 pr_info("use-after-free in strlen\n");
658 pr_info("use-after-free in strnlen\n");
662 static noinline void __init kasan_bitops(void)
665 * Allocate 1 more byte, which causes kzalloc to round up to 16-bytes;
666 * this way we do not actually corrupt other memory.
668 long *bits = kzalloc(sizeof(*bits) + 1, GFP_KERNEL);
673 * Below calls try to access bit within allocated memory; however, the
674 * below accesses are still out-of-bounds, since bitops are defined to
675 * operate on the whole long the bit is in.
677 pr_info("out-of-bounds in set_bit\n");
678 set_bit(BITS_PER_LONG, bits);
680 pr_info("out-of-bounds in __set_bit\n");
681 __set_bit(BITS_PER_LONG, bits);
683 pr_info("out-of-bounds in clear_bit\n");
684 clear_bit(BITS_PER_LONG, bits);
686 pr_info("out-of-bounds in __clear_bit\n");
687 __clear_bit(BITS_PER_LONG, bits);
689 pr_info("out-of-bounds in clear_bit_unlock\n");
690 clear_bit_unlock(BITS_PER_LONG, bits);
692 pr_info("out-of-bounds in __clear_bit_unlock\n");
693 __clear_bit_unlock(BITS_PER_LONG, bits);
695 pr_info("out-of-bounds in change_bit\n");
696 change_bit(BITS_PER_LONG, bits);
698 pr_info("out-of-bounds in __change_bit\n");
699 __change_bit(BITS_PER_LONG, bits);
702 * Below calls try to access bit beyond allocated memory.
704 pr_info("out-of-bounds in test_and_set_bit\n");
705 test_and_set_bit(BITS_PER_LONG + BITS_PER_BYTE, bits);
707 pr_info("out-of-bounds in __test_and_set_bit\n");
708 __test_and_set_bit(BITS_PER_LONG + BITS_PER_BYTE, bits);
710 pr_info("out-of-bounds in test_and_set_bit_lock\n");
711 test_and_set_bit_lock(BITS_PER_LONG + BITS_PER_BYTE, bits);
713 pr_info("out-of-bounds in test_and_clear_bit\n");
714 test_and_clear_bit(BITS_PER_LONG + BITS_PER_BYTE, bits);
716 pr_info("out-of-bounds in __test_and_clear_bit\n");
717 __test_and_clear_bit(BITS_PER_LONG + BITS_PER_BYTE, bits);
719 pr_info("out-of-bounds in test_and_change_bit\n");
720 test_and_change_bit(BITS_PER_LONG + BITS_PER_BYTE, bits);
722 pr_info("out-of-bounds in __test_and_change_bit\n");
723 __test_and_change_bit(BITS_PER_LONG + BITS_PER_BYTE, bits);
725 pr_info("out-of-bounds in test_bit\n");
726 (void)test_bit(BITS_PER_LONG + BITS_PER_BYTE, bits);
728 #if defined(clear_bit_unlock_is_negative_byte)
729 pr_info("out-of-bounds in clear_bit_unlock_is_negative_byte\n");
730 clear_bit_unlock_is_negative_byte(BITS_PER_LONG + BITS_PER_BYTE, bits);
735 static noinline void __init kmalloc_double_kzfree(void)
740 pr_info("double-free (kzfree)\n");
741 ptr = kmalloc(size, GFP_KERNEL);
743 pr_err("Allocation failed\n");
751 static int __init kmalloc_tests_init(void)
754 * Temporarily enable multi-shot mode. Otherwise, we'd only get a
755 * report for the first case.
757 bool multishot = kasan_save_enable_multi_shot();
761 kmalloc_node_oob_right();
763 kmalloc_pagealloc_oob_right();
764 kmalloc_pagealloc_uaf();
765 kmalloc_pagealloc_invalid_free();
767 kmalloc_large_oob_right();
768 kmalloc_oob_krealloc_more();
769 kmalloc_oob_krealloc_less();
771 kmalloc_oob_in_memset();
772 kmalloc_oob_memset_2();
773 kmalloc_oob_memset_4();
774 kmalloc_oob_memset_8();
775 kmalloc_oob_memset_16();
777 kmalloc_uaf_memset();
782 memcg_accounted_kmem_cache();
785 kasan_alloca_oob_left();
786 kasan_alloca_oob_right();
787 ksize_unpoisons_memory();
789 kmem_cache_double_free();
790 kmem_cache_invalid_free();
795 kmalloc_double_kzfree();
797 kasan_restore_multi_shot(multishot);
802 module_init(kmalloc_tests_init);
803 MODULE_LICENSE("GPL");