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 "../mm/kasan/kasan.h"
28 #define OOB_TAG_OFF (IS_ENABLED(CONFIG_KASAN_GENERIC) ? 0 : KASAN_SHADOW_SCALE_SIZE)
31 * We assign some test results to these globals to make sure the tests
32 * are not eliminated as dead code.
36 void *kasan_ptr_result
;
39 * Note: test functions are marked noinline so that their names appear in
43 static noinline
void __init
kmalloc_oob_right(void)
48 pr_info("out-of-bounds to right\n");
49 ptr
= kmalloc(size
, GFP_KERNEL
);
51 pr_err("Allocation failed\n");
55 ptr
[size
+ OOB_TAG_OFF
] = 'x';
60 static noinline
void __init
kmalloc_oob_left(void)
65 pr_info("out-of-bounds to left\n");
66 ptr
= kmalloc(size
, GFP_KERNEL
);
68 pr_err("Allocation failed\n");
76 static noinline
void __init
kmalloc_node_oob_right(void)
81 pr_info("kmalloc_node(): out-of-bounds to right\n");
82 ptr
= kmalloc_node(size
, GFP_KERNEL
, 0);
84 pr_err("Allocation failed\n");
93 static noinline
void __init
kmalloc_pagealloc_oob_right(void)
96 size_t size
= KMALLOC_MAX_CACHE_SIZE
+ 10;
98 /* Allocate a chunk that does not fit into a SLUB cache to trigger
99 * the page allocator fallback.
101 pr_info("kmalloc pagealloc allocation: out-of-bounds to right\n");
102 ptr
= kmalloc(size
, GFP_KERNEL
);
104 pr_err("Allocation failed\n");
108 ptr
[size
+ OOB_TAG_OFF
] = 0;
113 static noinline
void __init
kmalloc_pagealloc_uaf(void)
116 size_t size
= KMALLOC_MAX_CACHE_SIZE
+ 10;
118 pr_info("kmalloc pagealloc allocation: use-after-free\n");
119 ptr
= kmalloc(size
, GFP_KERNEL
);
121 pr_err("Allocation failed\n");
129 static noinline
void __init
kmalloc_pagealloc_invalid_free(void)
132 size_t size
= KMALLOC_MAX_CACHE_SIZE
+ 10;
134 pr_info("kmalloc pagealloc allocation: invalid-free\n");
135 ptr
= kmalloc(size
, GFP_KERNEL
);
137 pr_err("Allocation failed\n");
145 static noinline
void __init
kmalloc_large_oob_right(void)
148 size_t size
= KMALLOC_MAX_CACHE_SIZE
- 256;
149 /* Allocate a chunk that is large enough, but still fits into a slab
150 * and does not trigger the page allocator fallback in SLUB.
152 pr_info("kmalloc large allocation: out-of-bounds to right\n");
153 ptr
= kmalloc(size
, GFP_KERNEL
);
155 pr_err("Allocation failed\n");
163 static noinline
void __init
kmalloc_oob_krealloc_more(void)
169 pr_info("out-of-bounds after krealloc more\n");
170 ptr1
= kmalloc(size1
, GFP_KERNEL
);
171 ptr2
= krealloc(ptr1
, size2
, GFP_KERNEL
);
172 if (!ptr1
|| !ptr2
) {
173 pr_err("Allocation failed\n");
179 ptr2
[size2
+ OOB_TAG_OFF
] = 'x';
184 static noinline
void __init
kmalloc_oob_krealloc_less(void)
190 pr_info("out-of-bounds after krealloc less\n");
191 ptr1
= kmalloc(size1
, GFP_KERNEL
);
192 ptr2
= krealloc(ptr1
, size2
, GFP_KERNEL
);
193 if (!ptr1
|| !ptr2
) {
194 pr_err("Allocation failed\n");
199 ptr2
[size2
+ OOB_TAG_OFF
] = 'x';
204 static noinline
void __init
kmalloc_oob_16(void)
210 pr_info("kmalloc out-of-bounds for 16-bytes access\n");
211 ptr1
= kmalloc(sizeof(*ptr1
) - 3, GFP_KERNEL
);
212 ptr2
= kmalloc(sizeof(*ptr2
), GFP_KERNEL
);
213 if (!ptr1
|| !ptr2
) {
214 pr_err("Allocation failed\n");
224 static noinline
void __init
kmalloc_oob_memset_2(void)
229 pr_info("out-of-bounds in memset2\n");
230 ptr
= kmalloc(size
, GFP_KERNEL
);
232 pr_err("Allocation failed\n");
236 memset(ptr
+ 7 + OOB_TAG_OFF
, 0, 2);
241 static noinline
void __init
kmalloc_oob_memset_4(void)
246 pr_info("out-of-bounds in memset4\n");
247 ptr
= kmalloc(size
, GFP_KERNEL
);
249 pr_err("Allocation failed\n");
253 memset(ptr
+ 5 + OOB_TAG_OFF
, 0, 4);
259 static noinline
void __init
kmalloc_oob_memset_8(void)
264 pr_info("out-of-bounds in memset8\n");
265 ptr
= kmalloc(size
, GFP_KERNEL
);
267 pr_err("Allocation failed\n");
271 memset(ptr
+ 1 + OOB_TAG_OFF
, 0, 8);
276 static noinline
void __init
kmalloc_oob_memset_16(void)
281 pr_info("out-of-bounds in memset16\n");
282 ptr
= kmalloc(size
, GFP_KERNEL
);
284 pr_err("Allocation failed\n");
288 memset(ptr
+ 1 + OOB_TAG_OFF
, 0, 16);
293 static noinline
void __init
kmalloc_oob_in_memset(void)
298 pr_info("out-of-bounds in memset\n");
299 ptr
= kmalloc(size
, GFP_KERNEL
);
301 pr_err("Allocation failed\n");
305 memset(ptr
, 0, size
+ 5 + OOB_TAG_OFF
);
310 static noinline
void __init
kmalloc_memmove_invalid_size(void)
314 volatile size_t invalid_size
= -2;
316 pr_info("invalid size in memmove\n");
317 ptr
= kmalloc(size
, GFP_KERNEL
);
319 pr_err("Allocation failed\n");
323 memset((char *)ptr
, 0, 64);
324 memmove((char *)ptr
, (char *)ptr
+ 4, invalid_size
);
328 static noinline
void __init
kmalloc_uaf(void)
333 pr_info("use-after-free\n");
334 ptr
= kmalloc(size
, GFP_KERNEL
);
336 pr_err("Allocation failed\n");
344 static noinline
void __init
kmalloc_uaf_memset(void)
349 pr_info("use-after-free in memset\n");
350 ptr
= kmalloc(size
, GFP_KERNEL
);
352 pr_err("Allocation failed\n");
357 memset(ptr
, 0, size
);
360 static noinline
void __init
kmalloc_uaf2(void)
365 pr_info("use-after-free after another kmalloc\n");
366 ptr1
= kmalloc(size
, GFP_KERNEL
);
368 pr_err("Allocation failed\n");
373 ptr2
= kmalloc(size
, GFP_KERNEL
);
375 pr_err("Allocation failed\n");
381 pr_err("Could not detect use-after-free: ptr1 == ptr2\n");
385 static noinline
void __init
kfree_via_page(void)
390 unsigned long offset
;
392 pr_info("invalid-free false positive (via page)\n");
393 ptr
= kmalloc(size
, GFP_KERNEL
);
395 pr_err("Allocation failed\n");
399 page
= virt_to_page(ptr
);
400 offset
= offset_in_page(ptr
);
401 kfree(page_address(page
) + offset
);
404 static noinline
void __init
kfree_via_phys(void)
410 pr_info("invalid-free false positive (via phys)\n");
411 ptr
= kmalloc(size
, GFP_KERNEL
);
413 pr_err("Allocation failed\n");
417 phys
= virt_to_phys(ptr
);
418 kfree(phys_to_virt(phys
));
421 static noinline
void __init
kmem_cache_oob(void)
425 struct kmem_cache
*cache
= kmem_cache_create("test_cache",
429 pr_err("Cache allocation failed\n");
432 pr_info("out-of-bounds in kmem_cache_alloc\n");
433 p
= kmem_cache_alloc(cache
, GFP_KERNEL
);
435 pr_err("Allocation failed\n");
436 kmem_cache_destroy(cache
);
440 *p
= p
[size
+ OOB_TAG_OFF
];
442 kmem_cache_free(cache
, p
);
443 kmem_cache_destroy(cache
);
446 static noinline
void __init
memcg_accounted_kmem_cache(void)
451 struct kmem_cache
*cache
;
453 cache
= kmem_cache_create("test_cache", size
, 0, SLAB_ACCOUNT
, NULL
);
455 pr_err("Cache allocation failed\n");
459 pr_info("allocate memcg accounted object\n");
461 * Several allocations with a delay to allow for lazy per memcg kmem
464 for (i
= 0; i
< 5; i
++) {
465 p
= kmem_cache_alloc(cache
, GFP_KERNEL
);
469 kmem_cache_free(cache
, p
);
474 kmem_cache_destroy(cache
);
477 static char global_array
[10];
479 static noinline
void __init
kasan_global_oob(void)
482 char *p
= &global_array
[ARRAY_SIZE(global_array
) + i
];
484 pr_info("out-of-bounds global variable\n");
488 static noinline
void __init
kasan_stack_oob(void)
490 char stack_array
[10];
491 volatile int i
= OOB_TAG_OFF
;
492 char *p
= &stack_array
[ARRAY_SIZE(stack_array
) + i
];
494 pr_info("out-of-bounds on stack\n");
498 static noinline
void __init
ksize_unpoisons_memory(void)
501 size_t size
= 123, real_size
;
503 pr_info("ksize() unpoisons the whole allocated chunk\n");
504 ptr
= kmalloc(size
, GFP_KERNEL
);
506 pr_err("Allocation failed\n");
509 real_size
= ksize(ptr
);
510 /* This access doesn't trigger an error. */
513 ptr
[real_size
] = 'y';
517 static noinline
void __init
copy_user_test(void)
520 char __user
*usermem
;
524 kmem
= kmalloc(size
, GFP_KERNEL
);
528 usermem
= (char __user
*)vm_mmap(NULL
, 0, PAGE_SIZE
,
529 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
530 MAP_ANONYMOUS
| MAP_PRIVATE
, 0);
531 if (IS_ERR(usermem
)) {
532 pr_err("Failed to allocate user memory\n");
537 pr_info("out-of-bounds in copy_from_user()\n");
538 unused
= copy_from_user(kmem
, usermem
, size
+ 1 + OOB_TAG_OFF
);
540 pr_info("out-of-bounds in copy_to_user()\n");
541 unused
= copy_to_user(usermem
, kmem
, size
+ 1 + OOB_TAG_OFF
);
543 pr_info("out-of-bounds in __copy_from_user()\n");
544 unused
= __copy_from_user(kmem
, usermem
, size
+ 1 + OOB_TAG_OFF
);
546 pr_info("out-of-bounds in __copy_to_user()\n");
547 unused
= __copy_to_user(usermem
, kmem
, size
+ 1 + OOB_TAG_OFF
);
549 pr_info("out-of-bounds in __copy_from_user_inatomic()\n");
550 unused
= __copy_from_user_inatomic(kmem
, usermem
, size
+ 1 + OOB_TAG_OFF
);
552 pr_info("out-of-bounds in __copy_to_user_inatomic()\n");
553 unused
= __copy_to_user_inatomic(usermem
, kmem
, size
+ 1 + OOB_TAG_OFF
);
555 pr_info("out-of-bounds in strncpy_from_user()\n");
556 unused
= strncpy_from_user(kmem
, usermem
, size
+ 1 + OOB_TAG_OFF
);
558 vm_munmap((unsigned long)usermem
, PAGE_SIZE
);
562 static noinline
void __init
kasan_alloca_oob_left(void)
565 char alloca_array
[i
];
566 char *p
= alloca_array
- 1;
568 pr_info("out-of-bounds to left on alloca\n");
572 static noinline
void __init
kasan_alloca_oob_right(void)
575 char alloca_array
[i
];
576 char *p
= alloca_array
+ i
;
578 pr_info("out-of-bounds to right on alloca\n");
582 static noinline
void __init
kmem_cache_double_free(void)
586 struct kmem_cache
*cache
;
588 cache
= kmem_cache_create("test_cache", size
, 0, 0, NULL
);
590 pr_err("Cache allocation failed\n");
593 pr_info("double-free on heap object\n");
594 p
= kmem_cache_alloc(cache
, GFP_KERNEL
);
596 pr_err("Allocation failed\n");
597 kmem_cache_destroy(cache
);
601 kmem_cache_free(cache
, p
);
602 kmem_cache_free(cache
, p
);
603 kmem_cache_destroy(cache
);
606 static noinline
void __init
kmem_cache_invalid_free(void)
610 struct kmem_cache
*cache
;
612 cache
= kmem_cache_create("test_cache", size
, 0, SLAB_TYPESAFE_BY_RCU
,
615 pr_err("Cache allocation failed\n");
618 pr_info("invalid-free of heap object\n");
619 p
= kmem_cache_alloc(cache
, GFP_KERNEL
);
621 pr_err("Allocation failed\n");
622 kmem_cache_destroy(cache
);
626 /* Trigger invalid free, the object doesn't get freed */
627 kmem_cache_free(cache
, p
+ 1);
630 * Properly free the object to prevent the "Objects remaining in
631 * test_cache on __kmem_cache_shutdown" BUG failure.
633 kmem_cache_free(cache
, p
);
635 kmem_cache_destroy(cache
);
638 static noinline
void __init
kasan_memchr(void)
643 pr_info("out-of-bounds in memchr\n");
644 ptr
= kmalloc(size
, GFP_KERNEL
| __GFP_ZERO
);
648 kasan_ptr_result
= memchr(ptr
, '1', size
+ 1);
652 static noinline
void __init
kasan_memcmp(void)
658 pr_info("out-of-bounds in memcmp\n");
659 ptr
= kmalloc(size
, GFP_KERNEL
| __GFP_ZERO
);
663 memset(arr
, 0, sizeof(arr
));
664 kasan_int_result
= memcmp(ptr
, arr
, size
+ 1);
668 static noinline
void __init
kasan_strings(void)
673 pr_info("use-after-free in strchr\n");
674 ptr
= kmalloc(size
, GFP_KERNEL
| __GFP_ZERO
);
681 * Try to cause only 1 invalid access (less spam in dmesg).
682 * For that we need ptr to point to zeroed byte.
683 * Skip metadata that could be stored in freed object so ptr
684 * will likely point to zeroed byte.
687 kasan_ptr_result
= strchr(ptr
, '1');
689 pr_info("use-after-free in strrchr\n");
690 kasan_ptr_result
= strrchr(ptr
, '1');
692 pr_info("use-after-free in strcmp\n");
693 kasan_int_result
= strcmp(ptr
, "2");
695 pr_info("use-after-free in strncmp\n");
696 kasan_int_result
= strncmp(ptr
, "2", 1);
698 pr_info("use-after-free in strlen\n");
699 kasan_int_result
= strlen(ptr
);
701 pr_info("use-after-free in strnlen\n");
702 kasan_int_result
= strnlen(ptr
, 1);
705 static noinline
void __init
kasan_bitops(void)
708 * Allocate 1 more byte, which causes kzalloc to round up to 16-bytes;
709 * this way we do not actually corrupt other memory.
711 long *bits
= kzalloc(sizeof(*bits
) + 1, GFP_KERNEL
);
716 * Below calls try to access bit within allocated memory; however, the
717 * below accesses are still out-of-bounds, since bitops are defined to
718 * operate on the whole long the bit is in.
720 pr_info("out-of-bounds in set_bit\n");
721 set_bit(BITS_PER_LONG
, bits
);
723 pr_info("out-of-bounds in __set_bit\n");
724 __set_bit(BITS_PER_LONG
, bits
);
726 pr_info("out-of-bounds in clear_bit\n");
727 clear_bit(BITS_PER_LONG
, bits
);
729 pr_info("out-of-bounds in __clear_bit\n");
730 __clear_bit(BITS_PER_LONG
, bits
);
732 pr_info("out-of-bounds in clear_bit_unlock\n");
733 clear_bit_unlock(BITS_PER_LONG
, bits
);
735 pr_info("out-of-bounds in __clear_bit_unlock\n");
736 __clear_bit_unlock(BITS_PER_LONG
, bits
);
738 pr_info("out-of-bounds in change_bit\n");
739 change_bit(BITS_PER_LONG
, bits
);
741 pr_info("out-of-bounds in __change_bit\n");
742 __change_bit(BITS_PER_LONG
, bits
);
745 * Below calls try to access bit beyond allocated memory.
747 pr_info("out-of-bounds in test_and_set_bit\n");
748 test_and_set_bit(BITS_PER_LONG
+ BITS_PER_BYTE
, bits
);
750 pr_info("out-of-bounds in __test_and_set_bit\n");
751 __test_and_set_bit(BITS_PER_LONG
+ BITS_PER_BYTE
, bits
);
753 pr_info("out-of-bounds in test_and_set_bit_lock\n");
754 test_and_set_bit_lock(BITS_PER_LONG
+ BITS_PER_BYTE
, bits
);
756 pr_info("out-of-bounds in test_and_clear_bit\n");
757 test_and_clear_bit(BITS_PER_LONG
+ BITS_PER_BYTE
, bits
);
759 pr_info("out-of-bounds in __test_and_clear_bit\n");
760 __test_and_clear_bit(BITS_PER_LONG
+ BITS_PER_BYTE
, bits
);
762 pr_info("out-of-bounds in test_and_change_bit\n");
763 test_and_change_bit(BITS_PER_LONG
+ BITS_PER_BYTE
, bits
);
765 pr_info("out-of-bounds in __test_and_change_bit\n");
766 __test_and_change_bit(BITS_PER_LONG
+ BITS_PER_BYTE
, bits
);
768 pr_info("out-of-bounds in test_bit\n");
769 kasan_int_result
= test_bit(BITS_PER_LONG
+ BITS_PER_BYTE
, bits
);
771 #if defined(clear_bit_unlock_is_negative_byte)
772 pr_info("out-of-bounds in clear_bit_unlock_is_negative_byte\n");
773 kasan_int_result
= clear_bit_unlock_is_negative_byte(BITS_PER_LONG
+
774 BITS_PER_BYTE
, bits
);
779 static noinline
void __init
kmalloc_double_kzfree(void)
784 pr_info("double-free (kfree_sensitive)\n");
785 ptr
= kmalloc(size
, GFP_KERNEL
);
787 pr_err("Allocation failed\n");
791 kfree_sensitive(ptr
);
792 kfree_sensitive(ptr
);
795 #ifdef CONFIG_KASAN_VMALLOC
796 static noinline
void __init
vmalloc_oob(void)
800 pr_info("vmalloc out-of-bounds\n");
803 * We have to be careful not to hit the guard page.
804 * The MMU will catch that and crash us.
806 area
= vmalloc(3000);
808 pr_err("Allocation failed\n");
812 ((volatile char *)area
)[3100];
816 static void __init
vmalloc_oob(void) {}
819 static struct kasan_rcu_info
{
824 static noinline
void __init
kasan_rcu_reclaim(struct rcu_head
*rp
)
826 struct kasan_rcu_info
*fp
= container_of(rp
,
827 struct kasan_rcu_info
, rcu
);
833 static noinline
void __init
kasan_rcu_uaf(void)
835 struct kasan_rcu_info
*ptr
;
837 pr_info("use-after-free in kasan_rcu_reclaim\n");
838 ptr
= kmalloc(sizeof(struct kasan_rcu_info
), GFP_KERNEL
);
840 pr_err("Allocation failed\n");
844 global_rcu_ptr
= rcu_dereference_protected(ptr
, NULL
);
845 call_rcu(&global_rcu_ptr
->rcu
, kasan_rcu_reclaim
);
848 static int __init
kmalloc_tests_init(void)
851 * Temporarily enable multi-shot mode. Otherwise, we'd only get a
852 * report for the first case.
854 bool multishot
= kasan_save_enable_multi_shot();
858 kmalloc_node_oob_right();
860 kmalloc_pagealloc_oob_right();
861 kmalloc_pagealloc_uaf();
862 kmalloc_pagealloc_invalid_free();
864 kmalloc_large_oob_right();
865 kmalloc_oob_krealloc_more();
866 kmalloc_oob_krealloc_less();
868 kmalloc_oob_in_memset();
869 kmalloc_oob_memset_2();
870 kmalloc_oob_memset_4();
871 kmalloc_oob_memset_8();
872 kmalloc_oob_memset_16();
873 kmalloc_memmove_invalid_size();
875 kmalloc_uaf_memset();
880 memcg_accounted_kmem_cache();
883 kasan_alloca_oob_left();
884 kasan_alloca_oob_right();
885 ksize_unpoisons_memory();
887 kmem_cache_double_free();
888 kmem_cache_invalid_free();
893 kmalloc_double_kzfree();
897 kasan_restore_multi_shot(multishot
);
902 module_init(kmalloc_tests_init
);
903 MODULE_LICENSE("GPL");