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");
169 static noinline
void __init
kmalloc_oob_krealloc_less(void)
175 pr_info("out-of-bounds after krealloc less\n");
176 ptr1
= kmalloc(size1
, GFP_KERNEL
);
177 ptr2
= krealloc(ptr1
, size2
, GFP_KERNEL
);
178 if (!ptr1
|| !ptr2
) {
179 pr_err("Allocation failed\n");
187 static noinline
void __init
kmalloc_oob_16(void)
193 pr_info("kmalloc out-of-bounds for 16-bytes access\n");
194 ptr1
= kmalloc(sizeof(*ptr1
) - 3, GFP_KERNEL
);
195 ptr2
= kmalloc(sizeof(*ptr2
), GFP_KERNEL
);
196 if (!ptr1
|| !ptr2
) {
197 pr_err("Allocation failed\n");
207 static noinline
void __init
kmalloc_oob_memset_2(void)
212 pr_info("out-of-bounds in memset2\n");
213 ptr
= kmalloc(size
, GFP_KERNEL
);
215 pr_err("Allocation failed\n");
223 static noinline
void __init
kmalloc_oob_memset_4(void)
228 pr_info("out-of-bounds in memset4\n");
229 ptr
= kmalloc(size
, GFP_KERNEL
);
231 pr_err("Allocation failed\n");
240 static noinline
void __init
kmalloc_oob_memset_8(void)
245 pr_info("out-of-bounds in memset8\n");
246 ptr
= kmalloc(size
, GFP_KERNEL
);
248 pr_err("Allocation failed\n");
256 static noinline
void __init
kmalloc_oob_memset_16(void)
261 pr_info("out-of-bounds in memset16\n");
262 ptr
= kmalloc(size
, GFP_KERNEL
);
264 pr_err("Allocation failed\n");
268 memset(ptr
+1, 0, 16);
272 static noinline
void __init
kmalloc_oob_in_memset(void)
277 pr_info("out-of-bounds in memset\n");
278 ptr
= kmalloc(size
, GFP_KERNEL
);
280 pr_err("Allocation failed\n");
284 memset(ptr
, 0, size
+5);
288 static noinline
void __init
kmalloc_uaf(void)
293 pr_info("use-after-free\n");
294 ptr
= kmalloc(size
, GFP_KERNEL
);
296 pr_err("Allocation failed\n");
304 static noinline
void __init
kmalloc_uaf_memset(void)
309 pr_info("use-after-free in memset\n");
310 ptr
= kmalloc(size
, GFP_KERNEL
);
312 pr_err("Allocation failed\n");
317 memset(ptr
, 0, size
);
320 static noinline
void __init
kmalloc_uaf2(void)
325 pr_info("use-after-free after another kmalloc\n");
326 ptr1
= kmalloc(size
, GFP_KERNEL
);
328 pr_err("Allocation failed\n");
333 ptr2
= kmalloc(size
, GFP_KERNEL
);
335 pr_err("Allocation failed\n");
341 pr_err("Could not detect use-after-free: ptr1 == ptr2\n");
345 static noinline
void __init
kfree_via_page(void)
350 unsigned long offset
;
352 pr_info("invalid-free false positive (via page)\n");
353 ptr
= kmalloc(size
, GFP_KERNEL
);
355 pr_err("Allocation failed\n");
359 page
= virt_to_page(ptr
);
360 offset
= offset_in_page(ptr
);
361 kfree(page_address(page
) + offset
);
364 static noinline
void __init
kfree_via_phys(void)
370 pr_info("invalid-free false positive (via phys)\n");
371 ptr
= kmalloc(size
, GFP_KERNEL
);
373 pr_err("Allocation failed\n");
377 phys
= virt_to_phys(ptr
);
378 kfree(phys_to_virt(phys
));
381 static noinline
void __init
kmem_cache_oob(void)
385 struct kmem_cache
*cache
= kmem_cache_create("test_cache",
389 pr_err("Cache allocation failed\n");
392 pr_info("out-of-bounds in kmem_cache_alloc\n");
393 p
= kmem_cache_alloc(cache
, GFP_KERNEL
);
395 pr_err("Allocation failed\n");
396 kmem_cache_destroy(cache
);
401 kmem_cache_free(cache
, p
);
402 kmem_cache_destroy(cache
);
405 static noinline
void __init
memcg_accounted_kmem_cache(void)
410 struct kmem_cache
*cache
;
412 cache
= kmem_cache_create("test_cache", size
, 0, SLAB_ACCOUNT
, NULL
);
414 pr_err("Cache allocation failed\n");
418 pr_info("allocate memcg accounted object\n");
420 * Several allocations with a delay to allow for lazy per memcg kmem
423 for (i
= 0; i
< 5; i
++) {
424 p
= kmem_cache_alloc(cache
, GFP_KERNEL
);
428 kmem_cache_free(cache
, p
);
433 kmem_cache_destroy(cache
);
436 static char global_array
[10];
438 static noinline
void __init
kasan_global_oob(void)
441 char *p
= &global_array
[ARRAY_SIZE(global_array
) + i
];
443 pr_info("out-of-bounds global variable\n");
447 static noinline
void __init
kasan_stack_oob(void)
449 char stack_array
[10];
451 char *p
= &stack_array
[ARRAY_SIZE(stack_array
) + i
];
453 pr_info("out-of-bounds on stack\n");
457 static noinline
void __init
ksize_unpoisons_memory(void)
460 size_t size
= 123, real_size
;
462 pr_info("ksize() unpoisons the whole allocated chunk\n");
463 ptr
= kmalloc(size
, GFP_KERNEL
);
465 pr_err("Allocation failed\n");
468 real_size
= ksize(ptr
);
469 /* This access doesn't trigger an error. */
472 ptr
[real_size
] = 'y';
476 static noinline
void __init
copy_user_test(void)
479 char __user
*usermem
;
483 kmem
= kmalloc(size
, GFP_KERNEL
);
487 usermem
= (char __user
*)vm_mmap(NULL
, 0, PAGE_SIZE
,
488 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
489 MAP_ANONYMOUS
| MAP_PRIVATE
, 0);
490 if (IS_ERR(usermem
)) {
491 pr_err("Failed to allocate user memory\n");
496 pr_info("out-of-bounds in copy_from_user()\n");
497 unused
= copy_from_user(kmem
, usermem
, size
+ 1);
499 pr_info("out-of-bounds in copy_to_user()\n");
500 unused
= copy_to_user(usermem
, kmem
, size
+ 1);
502 pr_info("out-of-bounds in __copy_from_user()\n");
503 unused
= __copy_from_user(kmem
, usermem
, size
+ 1);
505 pr_info("out-of-bounds in __copy_to_user()\n");
506 unused
= __copy_to_user(usermem
, kmem
, size
+ 1);
508 pr_info("out-of-bounds in __copy_from_user_inatomic()\n");
509 unused
= __copy_from_user_inatomic(kmem
, usermem
, size
+ 1);
511 pr_info("out-of-bounds in __copy_to_user_inatomic()\n");
512 unused
= __copy_to_user_inatomic(usermem
, kmem
, size
+ 1);
514 pr_info("out-of-bounds in strncpy_from_user()\n");
515 unused
= strncpy_from_user(kmem
, usermem
, size
+ 1);
517 vm_munmap((unsigned long)usermem
, PAGE_SIZE
);
521 static noinline
void __init
kasan_alloca_oob_left(void)
524 char alloca_array
[i
];
525 char *p
= alloca_array
- 1;
527 pr_info("out-of-bounds to left on alloca\n");
531 static noinline
void __init
kasan_alloca_oob_right(void)
534 char alloca_array
[i
];
535 char *p
= alloca_array
+ i
;
537 pr_info("out-of-bounds to right on alloca\n");
541 static noinline
void __init
kmem_cache_double_free(void)
545 struct kmem_cache
*cache
;
547 cache
= kmem_cache_create("test_cache", size
, 0, 0, NULL
);
549 pr_err("Cache allocation failed\n");
552 pr_info("double-free on heap object\n");
553 p
= kmem_cache_alloc(cache
, GFP_KERNEL
);
555 pr_err("Allocation failed\n");
556 kmem_cache_destroy(cache
);
560 kmem_cache_free(cache
, p
);
561 kmem_cache_free(cache
, p
);
562 kmem_cache_destroy(cache
);
565 static noinline
void __init
kmem_cache_invalid_free(void)
569 struct kmem_cache
*cache
;
571 cache
= kmem_cache_create("test_cache", size
, 0, SLAB_TYPESAFE_BY_RCU
,
574 pr_err("Cache allocation failed\n");
577 pr_info("invalid-free of heap object\n");
578 p
= kmem_cache_alloc(cache
, GFP_KERNEL
);
580 pr_err("Allocation failed\n");
581 kmem_cache_destroy(cache
);
585 /* Trigger invalid free, the object doesn't get freed */
586 kmem_cache_free(cache
, p
+ 1);
589 * Properly free the object to prevent the "Objects remaining in
590 * test_cache on __kmem_cache_shutdown" BUG failure.
592 kmem_cache_free(cache
, p
);
594 kmem_cache_destroy(cache
);
597 static noinline
void __init
kasan_memchr(void)
602 pr_info("out-of-bounds in memchr\n");
603 ptr
= kmalloc(size
, GFP_KERNEL
| __GFP_ZERO
);
607 memchr(ptr
, '1', size
+ 1);
611 static noinline
void __init
kasan_memcmp(void)
617 pr_info("out-of-bounds in memcmp\n");
618 ptr
= kmalloc(size
, GFP_KERNEL
| __GFP_ZERO
);
622 memset(arr
, 0, sizeof(arr
));
623 memcmp(ptr
, arr
, size
+1);
627 static noinline
void __init
kasan_strings(void)
632 pr_info("use-after-free in strchr\n");
633 ptr
= kmalloc(size
, GFP_KERNEL
| __GFP_ZERO
);
640 * Try to cause only 1 invalid access (less spam in dmesg).
641 * For that we need ptr to point to zeroed byte.
642 * Skip metadata that could be stored in freed object so ptr
643 * will likely point to zeroed byte.
648 pr_info("use-after-free in strrchr\n");
651 pr_info("use-after-free in strcmp\n");
654 pr_info("use-after-free in strncmp\n");
655 strncmp(ptr
, "2", 1);
657 pr_info("use-after-free in strlen\n");
660 pr_info("use-after-free in strnlen\n");
664 static noinline
void __init
kasan_bitops(void)
667 * Allocate 1 more byte, which causes kzalloc to round up to 16-bytes;
668 * this way we do not actually corrupt other memory.
670 long *bits
= kzalloc(sizeof(*bits
) + 1, GFP_KERNEL
);
675 * Below calls try to access bit within allocated memory; however, the
676 * below accesses are still out-of-bounds, since bitops are defined to
677 * operate on the whole long the bit is in.
679 pr_info("out-of-bounds in set_bit\n");
680 set_bit(BITS_PER_LONG
, bits
);
682 pr_info("out-of-bounds in __set_bit\n");
683 __set_bit(BITS_PER_LONG
, bits
);
685 pr_info("out-of-bounds in clear_bit\n");
686 clear_bit(BITS_PER_LONG
, bits
);
688 pr_info("out-of-bounds in __clear_bit\n");
689 __clear_bit(BITS_PER_LONG
, bits
);
691 pr_info("out-of-bounds in clear_bit_unlock\n");
692 clear_bit_unlock(BITS_PER_LONG
, bits
);
694 pr_info("out-of-bounds in __clear_bit_unlock\n");
695 __clear_bit_unlock(BITS_PER_LONG
, bits
);
697 pr_info("out-of-bounds in change_bit\n");
698 change_bit(BITS_PER_LONG
, bits
);
700 pr_info("out-of-bounds in __change_bit\n");
701 __change_bit(BITS_PER_LONG
, bits
);
704 * Below calls try to access bit beyond allocated memory.
706 pr_info("out-of-bounds in test_and_set_bit\n");
707 test_and_set_bit(BITS_PER_LONG
+ BITS_PER_BYTE
, bits
);
709 pr_info("out-of-bounds in __test_and_set_bit\n");
710 __test_and_set_bit(BITS_PER_LONG
+ BITS_PER_BYTE
, bits
);
712 pr_info("out-of-bounds in test_and_set_bit_lock\n");
713 test_and_set_bit_lock(BITS_PER_LONG
+ BITS_PER_BYTE
, bits
);
715 pr_info("out-of-bounds in test_and_clear_bit\n");
716 test_and_clear_bit(BITS_PER_LONG
+ BITS_PER_BYTE
, bits
);
718 pr_info("out-of-bounds in __test_and_clear_bit\n");
719 __test_and_clear_bit(BITS_PER_LONG
+ BITS_PER_BYTE
, bits
);
721 pr_info("out-of-bounds in test_and_change_bit\n");
722 test_and_change_bit(BITS_PER_LONG
+ BITS_PER_BYTE
, bits
);
724 pr_info("out-of-bounds in __test_and_change_bit\n");
725 __test_and_change_bit(BITS_PER_LONG
+ BITS_PER_BYTE
, bits
);
727 pr_info("out-of-bounds in test_bit\n");
728 (void)test_bit(BITS_PER_LONG
+ BITS_PER_BYTE
, bits
);
730 #if defined(clear_bit_unlock_is_negative_byte)
731 pr_info("out-of-bounds in clear_bit_unlock_is_negative_byte\n");
732 clear_bit_unlock_is_negative_byte(BITS_PER_LONG
+ BITS_PER_BYTE
, bits
);
737 static noinline
void __init
kmalloc_double_kzfree(void)
742 pr_info("double-free (kzfree)\n");
743 ptr
= kmalloc(size
, GFP_KERNEL
);
745 pr_err("Allocation failed\n");
753 #ifdef CONFIG_KASAN_VMALLOC
754 static noinline
void __init
vmalloc_oob(void)
758 pr_info("vmalloc out-of-bounds\n");
761 * We have to be careful not to hit the guard page.
762 * The MMU will catch that and crash us.
764 area
= vmalloc(3000);
766 pr_err("Allocation failed\n");
770 ((volatile char *)area
)[3100];
774 static void __init
vmalloc_oob(void) {}
777 static int __init
kmalloc_tests_init(void)
780 * Temporarily enable multi-shot mode. Otherwise, we'd only get a
781 * report for the first case.
783 bool multishot
= kasan_save_enable_multi_shot();
787 kmalloc_node_oob_right();
789 kmalloc_pagealloc_oob_right();
790 kmalloc_pagealloc_uaf();
791 kmalloc_pagealloc_invalid_free();
793 kmalloc_large_oob_right();
794 kmalloc_oob_krealloc_more();
795 kmalloc_oob_krealloc_less();
797 kmalloc_oob_in_memset();
798 kmalloc_oob_memset_2();
799 kmalloc_oob_memset_4();
800 kmalloc_oob_memset_8();
801 kmalloc_oob_memset_16();
803 kmalloc_uaf_memset();
808 memcg_accounted_kmem_cache();
811 kasan_alloca_oob_left();
812 kasan_alloca_oob_right();
813 ksize_unpoisons_memory();
815 kmem_cache_double_free();
816 kmem_cache_invalid_free();
821 kmalloc_double_kzfree();
824 kasan_restore_multi_shot(multishot
);
829 module_init(kmalloc_tests_init
);
830 MODULE_LICENSE("GPL");