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>
23 * Note: test functions are marked noinline so that their names appear in
27 static noinline void __init kmalloc_oob_right(void)
32 pr_info("out-of-bounds to right\n");
33 ptr = kmalloc(size, GFP_KERNEL);
35 pr_err("Allocation failed\n");
43 static noinline void __init kmalloc_oob_left(void)
48 pr_info("out-of-bounds to left\n");
49 ptr = kmalloc(size, GFP_KERNEL);
51 pr_err("Allocation failed\n");
59 static noinline void __init kmalloc_node_oob_right(void)
64 pr_info("kmalloc_node(): out-of-bounds to right\n");
65 ptr = kmalloc_node(size, GFP_KERNEL, 0);
67 pr_err("Allocation failed\n");
76 static noinline void __init kmalloc_pagealloc_oob_right(void)
79 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
81 /* Allocate a chunk that does not fit into a SLUB cache to trigger
82 * the page allocator fallback.
84 pr_info("kmalloc pagealloc allocation: out-of-bounds to right\n");
85 ptr = kmalloc(size, GFP_KERNEL);
87 pr_err("Allocation failed\n");
95 static noinline void __init kmalloc_pagealloc_uaf(void)
98 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
100 pr_info("kmalloc pagealloc allocation: use-after-free\n");
101 ptr = kmalloc(size, GFP_KERNEL);
103 pr_err("Allocation failed\n");
111 static noinline void __init kmalloc_pagealloc_invalid_free(void)
114 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
116 pr_info("kmalloc pagealloc allocation: invalid-free\n");
117 ptr = kmalloc(size, GFP_KERNEL);
119 pr_err("Allocation failed\n");
127 static noinline void __init kmalloc_large_oob_right(void)
130 size_t size = KMALLOC_MAX_CACHE_SIZE - 256;
131 /* Allocate a chunk that is large enough, but still fits into a slab
132 * and does not trigger the page allocator fallback in SLUB.
134 pr_info("kmalloc large allocation: out-of-bounds to right\n");
135 ptr = kmalloc(size, GFP_KERNEL);
137 pr_err("Allocation failed\n");
145 static noinline void __init kmalloc_oob_krealloc_more(void)
151 pr_info("out-of-bounds after krealloc more\n");
152 ptr1 = kmalloc(size1, GFP_KERNEL);
153 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
154 if (!ptr1 || !ptr2) {
155 pr_err("Allocation failed\n");
164 static noinline void __init kmalloc_oob_krealloc_less(void)
170 pr_info("out-of-bounds after krealloc less\n");
171 ptr1 = kmalloc(size1, GFP_KERNEL);
172 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
173 if (!ptr1 || !ptr2) {
174 pr_err("Allocation failed\n");
182 static noinline void __init kmalloc_oob_16(void)
188 pr_info("kmalloc out-of-bounds for 16-bytes access\n");
189 ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
190 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
191 if (!ptr1 || !ptr2) {
192 pr_err("Allocation failed\n");
202 static noinline void __init kmalloc_oob_memset_2(void)
207 pr_info("out-of-bounds in memset2\n");
208 ptr = kmalloc(size, GFP_KERNEL);
210 pr_err("Allocation failed\n");
218 static noinline void __init kmalloc_oob_memset_4(void)
223 pr_info("out-of-bounds in memset4\n");
224 ptr = kmalloc(size, GFP_KERNEL);
226 pr_err("Allocation failed\n");
235 static noinline void __init kmalloc_oob_memset_8(void)
240 pr_info("out-of-bounds in memset8\n");
241 ptr = kmalloc(size, GFP_KERNEL);
243 pr_err("Allocation failed\n");
251 static noinline void __init kmalloc_oob_memset_16(void)
256 pr_info("out-of-bounds in memset16\n");
257 ptr = kmalloc(size, GFP_KERNEL);
259 pr_err("Allocation failed\n");
263 memset(ptr+1, 0, 16);
267 static noinline void __init kmalloc_oob_in_memset(void)
272 pr_info("out-of-bounds in memset\n");
273 ptr = kmalloc(size, GFP_KERNEL);
275 pr_err("Allocation failed\n");
279 memset(ptr, 0, size+5);
283 static noinline void __init kmalloc_uaf(void)
288 pr_info("use-after-free\n");
289 ptr = kmalloc(size, GFP_KERNEL);
291 pr_err("Allocation failed\n");
299 static noinline void __init kmalloc_uaf_memset(void)
304 pr_info("use-after-free in memset\n");
305 ptr = kmalloc(size, GFP_KERNEL);
307 pr_err("Allocation failed\n");
312 memset(ptr, 0, size);
315 static noinline void __init kmalloc_uaf2(void)
320 pr_info("use-after-free after another kmalloc\n");
321 ptr1 = kmalloc(size, GFP_KERNEL);
323 pr_err("Allocation failed\n");
328 ptr2 = kmalloc(size, GFP_KERNEL);
330 pr_err("Allocation failed\n");
336 pr_err("Could not detect use-after-free: ptr1 == ptr2\n");
340 static noinline void __init kmem_cache_oob(void)
344 struct kmem_cache *cache = kmem_cache_create("test_cache",
348 pr_err("Cache allocation failed\n");
351 pr_info("out-of-bounds in kmem_cache_alloc\n");
352 p = kmem_cache_alloc(cache, GFP_KERNEL);
354 pr_err("Allocation failed\n");
355 kmem_cache_destroy(cache);
360 kmem_cache_free(cache, p);
361 kmem_cache_destroy(cache);
364 static noinline void __init memcg_accounted_kmem_cache(void)
369 struct kmem_cache *cache;
371 cache = kmem_cache_create("test_cache", size, 0, SLAB_ACCOUNT, NULL);
373 pr_err("Cache allocation failed\n");
377 pr_info("allocate memcg accounted object\n");
379 * Several allocations with a delay to allow for lazy per memcg kmem
382 for (i = 0; i < 5; i++) {
383 p = kmem_cache_alloc(cache, GFP_KERNEL);
387 kmem_cache_free(cache, p);
392 kmem_cache_destroy(cache);
395 static char global_array[10];
397 static noinline void __init kasan_global_oob(void)
400 char *p = &global_array[ARRAY_SIZE(global_array) + i];
402 pr_info("out-of-bounds global variable\n");
406 static noinline void __init kasan_stack_oob(void)
408 char stack_array[10];
410 char *p = &stack_array[ARRAY_SIZE(stack_array) + i];
412 pr_info("out-of-bounds on stack\n");
416 static noinline void __init ksize_unpoisons_memory(void)
419 size_t size = 123, real_size;
421 pr_info("ksize() unpoisons the whole allocated chunk\n");
422 ptr = kmalloc(size, GFP_KERNEL);
424 pr_err("Allocation failed\n");
427 real_size = ksize(ptr);
428 /* This access doesn't trigger an error. */
431 ptr[real_size] = 'y';
435 static noinline void __init copy_user_test(void)
438 char __user *usermem;
442 kmem = kmalloc(size, GFP_KERNEL);
446 usermem = (char __user *)vm_mmap(NULL, 0, PAGE_SIZE,
447 PROT_READ | PROT_WRITE | PROT_EXEC,
448 MAP_ANONYMOUS | MAP_PRIVATE, 0);
449 if (IS_ERR(usermem)) {
450 pr_err("Failed to allocate user memory\n");
455 pr_info("out-of-bounds in copy_from_user()\n");
456 unused = copy_from_user(kmem, usermem, size + 1);
458 pr_info("out-of-bounds in copy_to_user()\n");
459 unused = copy_to_user(usermem, kmem, size + 1);
461 pr_info("out-of-bounds in __copy_from_user()\n");
462 unused = __copy_from_user(kmem, usermem, size + 1);
464 pr_info("out-of-bounds in __copy_to_user()\n");
465 unused = __copy_to_user(usermem, kmem, size + 1);
467 pr_info("out-of-bounds in __copy_from_user_inatomic()\n");
468 unused = __copy_from_user_inatomic(kmem, usermem, size + 1);
470 pr_info("out-of-bounds in __copy_to_user_inatomic()\n");
471 unused = __copy_to_user_inatomic(usermem, kmem, size + 1);
473 pr_info("out-of-bounds in strncpy_from_user()\n");
474 unused = strncpy_from_user(kmem, usermem, size + 1);
476 vm_munmap((unsigned long)usermem, PAGE_SIZE);
480 static noinline void __init kasan_alloca_oob_left(void)
483 char alloca_array[i];
484 char *p = alloca_array - 1;
486 pr_info("out-of-bounds to left on alloca\n");
490 static noinline void __init kasan_alloca_oob_right(void)
493 char alloca_array[i];
494 char *p = alloca_array + i;
496 pr_info("out-of-bounds to right on alloca\n");
500 static noinline void __init kmem_cache_double_free(void)
504 struct kmem_cache *cache;
506 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
508 pr_err("Cache allocation failed\n");
511 pr_info("double-free on heap object\n");
512 p = kmem_cache_alloc(cache, GFP_KERNEL);
514 pr_err("Allocation failed\n");
515 kmem_cache_destroy(cache);
519 kmem_cache_free(cache, p);
520 kmem_cache_free(cache, p);
521 kmem_cache_destroy(cache);
524 static noinline void __init kmem_cache_invalid_free(void)
528 struct kmem_cache *cache;
530 cache = kmem_cache_create("test_cache", size, 0, SLAB_TYPESAFE_BY_RCU,
533 pr_err("Cache allocation failed\n");
536 pr_info("invalid-free of heap object\n");
537 p = kmem_cache_alloc(cache, GFP_KERNEL);
539 pr_err("Allocation failed\n");
540 kmem_cache_destroy(cache);
544 /* Trigger invalid free, the object doesn't get freed */
545 kmem_cache_free(cache, p + 1);
548 * Properly free the object to prevent the "Objects remaining in
549 * test_cache on __kmem_cache_shutdown" BUG failure.
551 kmem_cache_free(cache, p);
553 kmem_cache_destroy(cache);
556 static noinline void __init kasan_memchr(void)
561 pr_info("out-of-bounds in memchr\n");
562 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
566 memchr(ptr, '1', size + 1);
570 static noinline void __init kasan_memcmp(void)
576 pr_info("out-of-bounds in memcmp\n");
577 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
581 memset(arr, 0, sizeof(arr));
582 memcmp(ptr, arr, size+1);
586 static noinline void __init kasan_strings(void)
591 pr_info("use-after-free in strchr\n");
592 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
599 * Try to cause only 1 invalid access (less spam in dmesg).
600 * For that we need ptr to point to zeroed byte.
601 * Skip metadata that could be stored in freed object so ptr
602 * will likely point to zeroed byte.
607 pr_info("use-after-free in strrchr\n");
610 pr_info("use-after-free in strcmp\n");
613 pr_info("use-after-free in strncmp\n");
614 strncmp(ptr, "2", 1);
616 pr_info("use-after-free in strlen\n");
619 pr_info("use-after-free in strnlen\n");
623 static noinline void __init kasan_bitops(void)
626 * Allocate 1 more byte, which causes kzalloc to round up to 16-bytes;
627 * this way we do not actually corrupt other memory.
629 long *bits = kzalloc(sizeof(*bits) + 1, GFP_KERNEL);
634 * Below calls try to access bit within allocated memory; however, the
635 * below accesses are still out-of-bounds, since bitops are defined to
636 * operate on the whole long the bit is in.
638 pr_info("out-of-bounds in set_bit\n");
639 set_bit(BITS_PER_LONG, bits);
641 pr_info("out-of-bounds in __set_bit\n");
642 __set_bit(BITS_PER_LONG, bits);
644 pr_info("out-of-bounds in clear_bit\n");
645 clear_bit(BITS_PER_LONG, bits);
647 pr_info("out-of-bounds in __clear_bit\n");
648 __clear_bit(BITS_PER_LONG, bits);
650 pr_info("out-of-bounds in clear_bit_unlock\n");
651 clear_bit_unlock(BITS_PER_LONG, bits);
653 pr_info("out-of-bounds in __clear_bit_unlock\n");
654 __clear_bit_unlock(BITS_PER_LONG, bits);
656 pr_info("out-of-bounds in change_bit\n");
657 change_bit(BITS_PER_LONG, bits);
659 pr_info("out-of-bounds in __change_bit\n");
660 __change_bit(BITS_PER_LONG, bits);
663 * Below calls try to access bit beyond allocated memory.
665 pr_info("out-of-bounds in test_and_set_bit\n");
666 test_and_set_bit(BITS_PER_LONG + BITS_PER_BYTE, bits);
668 pr_info("out-of-bounds in __test_and_set_bit\n");
669 __test_and_set_bit(BITS_PER_LONG + BITS_PER_BYTE, bits);
671 pr_info("out-of-bounds in test_and_set_bit_lock\n");
672 test_and_set_bit_lock(BITS_PER_LONG + BITS_PER_BYTE, bits);
674 pr_info("out-of-bounds in test_and_clear_bit\n");
675 test_and_clear_bit(BITS_PER_LONG + BITS_PER_BYTE, bits);
677 pr_info("out-of-bounds in __test_and_clear_bit\n");
678 __test_and_clear_bit(BITS_PER_LONG + BITS_PER_BYTE, bits);
680 pr_info("out-of-bounds in test_and_change_bit\n");
681 test_and_change_bit(BITS_PER_LONG + BITS_PER_BYTE, bits);
683 pr_info("out-of-bounds in __test_and_change_bit\n");
684 __test_and_change_bit(BITS_PER_LONG + BITS_PER_BYTE, bits);
686 pr_info("out-of-bounds in test_bit\n");
687 (void)test_bit(BITS_PER_LONG + BITS_PER_BYTE, bits);
689 #if defined(clear_bit_unlock_is_negative_byte)
690 pr_info("out-of-bounds in clear_bit_unlock_is_negative_byte\n");
691 clear_bit_unlock_is_negative_byte(BITS_PER_LONG + BITS_PER_BYTE, bits);
696 static noinline void __init kmalloc_double_kzfree(void)
701 pr_info("double-free (kzfree)\n");
702 ptr = kmalloc(size, GFP_KERNEL);
704 pr_err("Allocation failed\n");
712 static int __init kmalloc_tests_init(void)
715 * Temporarily enable multi-shot mode. Otherwise, we'd only get a
716 * report for the first case.
718 bool multishot = kasan_save_enable_multi_shot();
722 kmalloc_node_oob_right();
724 kmalloc_pagealloc_oob_right();
725 kmalloc_pagealloc_uaf();
726 kmalloc_pagealloc_invalid_free();
728 kmalloc_large_oob_right();
729 kmalloc_oob_krealloc_more();
730 kmalloc_oob_krealloc_less();
732 kmalloc_oob_in_memset();
733 kmalloc_oob_memset_2();
734 kmalloc_oob_memset_4();
735 kmalloc_oob_memset_8();
736 kmalloc_oob_memset_16();
738 kmalloc_uaf_memset();
741 memcg_accounted_kmem_cache();
744 kasan_alloca_oob_left();
745 kasan_alloca_oob_right();
746 ksize_unpoisons_memory();
748 kmem_cache_double_free();
749 kmem_cache_invalid_free();
754 kmalloc_double_kzfree();
756 kasan_restore_multi_shot(multishot);
761 module_init(kmalloc_tests_init);
762 MODULE_LICENSE("GPL");