Revert "tty: hvc: Fix data abort due to race in hvc_open"
[linux/fpc-iii.git] / lib / test_kasan.c
blobdc2c6a51d11a0d38e54c8c760477d559048148f3
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
4 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
5 * Author: Andrey Ryabinin <a.ryabinin@samsung.com>
6 */
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>
14 #include <linux/mm.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>
21 #include <linux/io.h>
22 #include <linux/vmalloc.h>
24 #include <asm/page.h>
27 * We assign some test results to these globals to make sure the tests
28 * are not eliminated as dead code.
31 int kasan_int_result;
32 void *kasan_ptr_result;
35 * Note: test functions are marked noinline so that their names appear in
36 * reports.
39 static noinline void __init kmalloc_oob_right(void)
41 char *ptr;
42 size_t size = 123;
44 pr_info("out-of-bounds to right\n");
45 ptr = kmalloc(size, GFP_KERNEL);
46 if (!ptr) {
47 pr_err("Allocation failed\n");
48 return;
51 ptr[size] = 'x';
52 kfree(ptr);
55 static noinline void __init kmalloc_oob_left(void)
57 char *ptr;
58 size_t size = 15;
60 pr_info("out-of-bounds to left\n");
61 ptr = kmalloc(size, GFP_KERNEL);
62 if (!ptr) {
63 pr_err("Allocation failed\n");
64 return;
67 *ptr = *(ptr - 1);
68 kfree(ptr);
71 static noinline void __init kmalloc_node_oob_right(void)
73 char *ptr;
74 size_t size = 4096;
76 pr_info("kmalloc_node(): out-of-bounds to right\n");
77 ptr = kmalloc_node(size, GFP_KERNEL, 0);
78 if (!ptr) {
79 pr_err("Allocation failed\n");
80 return;
83 ptr[size] = 0;
84 kfree(ptr);
87 #ifdef CONFIG_SLUB
88 static noinline void __init kmalloc_pagealloc_oob_right(void)
90 char *ptr;
91 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
93 /* Allocate a chunk that does not fit into a SLUB cache to trigger
94 * the page allocator fallback.
96 pr_info("kmalloc pagealloc allocation: out-of-bounds to right\n");
97 ptr = kmalloc(size, GFP_KERNEL);
98 if (!ptr) {
99 pr_err("Allocation failed\n");
100 return;
103 ptr[size] = 0;
104 kfree(ptr);
107 static noinline void __init kmalloc_pagealloc_uaf(void)
109 char *ptr;
110 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
112 pr_info("kmalloc pagealloc allocation: use-after-free\n");
113 ptr = kmalloc(size, GFP_KERNEL);
114 if (!ptr) {
115 pr_err("Allocation failed\n");
116 return;
119 kfree(ptr);
120 ptr[0] = 0;
123 static noinline void __init kmalloc_pagealloc_invalid_free(void)
125 char *ptr;
126 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
128 pr_info("kmalloc pagealloc allocation: invalid-free\n");
129 ptr = kmalloc(size, GFP_KERNEL);
130 if (!ptr) {
131 pr_err("Allocation failed\n");
132 return;
135 kfree(ptr + 1);
137 #endif
139 static noinline void __init kmalloc_large_oob_right(void)
141 char *ptr;
142 size_t size = KMALLOC_MAX_CACHE_SIZE - 256;
143 /* Allocate a chunk that is large enough, but still fits into a slab
144 * and does not trigger the page allocator fallback in SLUB.
146 pr_info("kmalloc large allocation: out-of-bounds to right\n");
147 ptr = kmalloc(size, GFP_KERNEL);
148 if (!ptr) {
149 pr_err("Allocation failed\n");
150 return;
153 ptr[size] = 0;
154 kfree(ptr);
157 static noinline void __init kmalloc_oob_krealloc_more(void)
159 char *ptr1, *ptr2;
160 size_t size1 = 17;
161 size_t size2 = 19;
163 pr_info("out-of-bounds after krealloc more\n");
164 ptr1 = kmalloc(size1, GFP_KERNEL);
165 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
166 if (!ptr1 || !ptr2) {
167 pr_err("Allocation failed\n");
168 kfree(ptr1);
169 kfree(ptr2);
170 return;
173 ptr2[size2] = 'x';
174 kfree(ptr2);
177 static noinline void __init kmalloc_oob_krealloc_less(void)
179 char *ptr1, *ptr2;
180 size_t size1 = 17;
181 size_t size2 = 15;
183 pr_info("out-of-bounds after krealloc less\n");
184 ptr1 = kmalloc(size1, GFP_KERNEL);
185 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
186 if (!ptr1 || !ptr2) {
187 pr_err("Allocation failed\n");
188 kfree(ptr1);
189 return;
191 ptr2[size2] = 'x';
192 kfree(ptr2);
195 static noinline void __init kmalloc_oob_16(void)
197 struct {
198 u64 words[2];
199 } *ptr1, *ptr2;
201 pr_info("kmalloc out-of-bounds for 16-bytes access\n");
202 ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
203 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
204 if (!ptr1 || !ptr2) {
205 pr_err("Allocation failed\n");
206 kfree(ptr1);
207 kfree(ptr2);
208 return;
210 *ptr1 = *ptr2;
211 kfree(ptr1);
212 kfree(ptr2);
215 static noinline void __init kmalloc_oob_memset_2(void)
217 char *ptr;
218 size_t size = 8;
220 pr_info("out-of-bounds in memset2\n");
221 ptr = kmalloc(size, GFP_KERNEL);
222 if (!ptr) {
223 pr_err("Allocation failed\n");
224 return;
227 memset(ptr+7, 0, 2);
228 kfree(ptr);
231 static noinline void __init kmalloc_oob_memset_4(void)
233 char *ptr;
234 size_t size = 8;
236 pr_info("out-of-bounds in memset4\n");
237 ptr = kmalloc(size, GFP_KERNEL);
238 if (!ptr) {
239 pr_err("Allocation failed\n");
240 return;
243 memset(ptr+5, 0, 4);
244 kfree(ptr);
248 static noinline void __init kmalloc_oob_memset_8(void)
250 char *ptr;
251 size_t size = 8;
253 pr_info("out-of-bounds in memset8\n");
254 ptr = kmalloc(size, GFP_KERNEL);
255 if (!ptr) {
256 pr_err("Allocation failed\n");
257 return;
260 memset(ptr+1, 0, 8);
261 kfree(ptr);
264 static noinline void __init kmalloc_oob_memset_16(void)
266 char *ptr;
267 size_t size = 16;
269 pr_info("out-of-bounds in memset16\n");
270 ptr = kmalloc(size, GFP_KERNEL);
271 if (!ptr) {
272 pr_err("Allocation failed\n");
273 return;
276 memset(ptr+1, 0, 16);
277 kfree(ptr);
280 static noinline void __init kmalloc_oob_in_memset(void)
282 char *ptr;
283 size_t size = 666;
285 pr_info("out-of-bounds in memset\n");
286 ptr = kmalloc(size, GFP_KERNEL);
287 if (!ptr) {
288 pr_err("Allocation failed\n");
289 return;
292 memset(ptr, 0, size+5);
293 kfree(ptr);
296 static noinline void __init kmalloc_memmove_invalid_size(void)
298 char *ptr;
299 size_t size = 64;
300 volatile size_t invalid_size = -2;
302 pr_info("invalid size in memmove\n");
303 ptr = kmalloc(size, GFP_KERNEL);
304 if (!ptr) {
305 pr_err("Allocation failed\n");
306 return;
309 memset((char *)ptr, 0, 64);
310 memmove((char *)ptr, (char *)ptr + 4, invalid_size);
311 kfree(ptr);
314 static noinline void __init kmalloc_uaf(void)
316 char *ptr;
317 size_t size = 10;
319 pr_info("use-after-free\n");
320 ptr = kmalloc(size, GFP_KERNEL);
321 if (!ptr) {
322 pr_err("Allocation failed\n");
323 return;
326 kfree(ptr);
327 *(ptr + 8) = 'x';
330 static noinline void __init kmalloc_uaf_memset(void)
332 char *ptr;
333 size_t size = 33;
335 pr_info("use-after-free in memset\n");
336 ptr = kmalloc(size, GFP_KERNEL);
337 if (!ptr) {
338 pr_err("Allocation failed\n");
339 return;
342 kfree(ptr);
343 memset(ptr, 0, size);
346 static noinline void __init kmalloc_uaf2(void)
348 char *ptr1, *ptr2;
349 size_t size = 43;
351 pr_info("use-after-free after another kmalloc\n");
352 ptr1 = kmalloc(size, GFP_KERNEL);
353 if (!ptr1) {
354 pr_err("Allocation failed\n");
355 return;
358 kfree(ptr1);
359 ptr2 = kmalloc(size, GFP_KERNEL);
360 if (!ptr2) {
361 pr_err("Allocation failed\n");
362 return;
365 ptr1[40] = 'x';
366 if (ptr1 == ptr2)
367 pr_err("Could not detect use-after-free: ptr1 == ptr2\n");
368 kfree(ptr2);
371 static noinline void __init kfree_via_page(void)
373 char *ptr;
374 size_t size = 8;
375 struct page *page;
376 unsigned long offset;
378 pr_info("invalid-free false positive (via page)\n");
379 ptr = kmalloc(size, GFP_KERNEL);
380 if (!ptr) {
381 pr_err("Allocation failed\n");
382 return;
385 page = virt_to_page(ptr);
386 offset = offset_in_page(ptr);
387 kfree(page_address(page) + offset);
390 static noinline void __init kfree_via_phys(void)
392 char *ptr;
393 size_t size = 8;
394 phys_addr_t phys;
396 pr_info("invalid-free false positive (via phys)\n");
397 ptr = kmalloc(size, GFP_KERNEL);
398 if (!ptr) {
399 pr_err("Allocation failed\n");
400 return;
403 phys = virt_to_phys(ptr);
404 kfree(phys_to_virt(phys));
407 static noinline void __init kmem_cache_oob(void)
409 char *p;
410 size_t size = 200;
411 struct kmem_cache *cache = kmem_cache_create("test_cache",
412 size, 0,
413 0, NULL);
414 if (!cache) {
415 pr_err("Cache allocation failed\n");
416 return;
418 pr_info("out-of-bounds in kmem_cache_alloc\n");
419 p = kmem_cache_alloc(cache, GFP_KERNEL);
420 if (!p) {
421 pr_err("Allocation failed\n");
422 kmem_cache_destroy(cache);
423 return;
426 *p = p[size];
427 kmem_cache_free(cache, p);
428 kmem_cache_destroy(cache);
431 static noinline void __init memcg_accounted_kmem_cache(void)
433 int i;
434 char *p;
435 size_t size = 200;
436 struct kmem_cache *cache;
438 cache = kmem_cache_create("test_cache", size, 0, SLAB_ACCOUNT, NULL);
439 if (!cache) {
440 pr_err("Cache allocation failed\n");
441 return;
444 pr_info("allocate memcg accounted object\n");
446 * Several allocations with a delay to allow for lazy per memcg kmem
447 * cache creation.
449 for (i = 0; i < 5; i++) {
450 p = kmem_cache_alloc(cache, GFP_KERNEL);
451 if (!p)
452 goto free_cache;
454 kmem_cache_free(cache, p);
455 msleep(100);
458 free_cache:
459 kmem_cache_destroy(cache);
462 static char global_array[10];
464 static noinline void __init kasan_global_oob(void)
466 volatile int i = 3;
467 char *p = &global_array[ARRAY_SIZE(global_array) + i];
469 pr_info("out-of-bounds global variable\n");
470 *(volatile char *)p;
473 static noinline void __init kasan_stack_oob(void)
475 char stack_array[10];
476 volatile int i = 0;
477 char *p = &stack_array[ARRAY_SIZE(stack_array) + i];
479 pr_info("out-of-bounds on stack\n");
480 *(volatile char *)p;
483 static noinline void __init ksize_unpoisons_memory(void)
485 char *ptr;
486 size_t size = 123, real_size;
488 pr_info("ksize() unpoisons the whole allocated chunk\n");
489 ptr = kmalloc(size, GFP_KERNEL);
490 if (!ptr) {
491 pr_err("Allocation failed\n");
492 return;
494 real_size = ksize(ptr);
495 /* This access doesn't trigger an error. */
496 ptr[size] = 'x';
497 /* This one does. */
498 ptr[real_size] = 'y';
499 kfree(ptr);
502 static noinline void __init copy_user_test(void)
504 char *kmem;
505 char __user *usermem;
506 size_t size = 10;
507 int unused;
509 kmem = kmalloc(size, GFP_KERNEL);
510 if (!kmem)
511 return;
513 usermem = (char __user *)vm_mmap(NULL, 0, PAGE_SIZE,
514 PROT_READ | PROT_WRITE | PROT_EXEC,
515 MAP_ANONYMOUS | MAP_PRIVATE, 0);
516 if (IS_ERR(usermem)) {
517 pr_err("Failed to allocate user memory\n");
518 kfree(kmem);
519 return;
522 pr_info("out-of-bounds in copy_from_user()\n");
523 unused = copy_from_user(kmem, usermem, size + 1);
525 pr_info("out-of-bounds in copy_to_user()\n");
526 unused = copy_to_user(usermem, kmem, size + 1);
528 pr_info("out-of-bounds in __copy_from_user()\n");
529 unused = __copy_from_user(kmem, usermem, size + 1);
531 pr_info("out-of-bounds in __copy_to_user()\n");
532 unused = __copy_to_user(usermem, kmem, size + 1);
534 pr_info("out-of-bounds in __copy_from_user_inatomic()\n");
535 unused = __copy_from_user_inatomic(kmem, usermem, size + 1);
537 pr_info("out-of-bounds in __copy_to_user_inatomic()\n");
538 unused = __copy_to_user_inatomic(usermem, kmem, size + 1);
540 pr_info("out-of-bounds in strncpy_from_user()\n");
541 unused = strncpy_from_user(kmem, usermem, size + 1);
543 vm_munmap((unsigned long)usermem, PAGE_SIZE);
544 kfree(kmem);
547 static noinline void __init kasan_alloca_oob_left(void)
549 volatile int i = 10;
550 char alloca_array[i];
551 char *p = alloca_array - 1;
553 pr_info("out-of-bounds to left on alloca\n");
554 *(volatile char *)p;
557 static noinline void __init kasan_alloca_oob_right(void)
559 volatile int i = 10;
560 char alloca_array[i];
561 char *p = alloca_array + i;
563 pr_info("out-of-bounds to right on alloca\n");
564 *(volatile char *)p;
567 static noinline void __init kmem_cache_double_free(void)
569 char *p;
570 size_t size = 200;
571 struct kmem_cache *cache;
573 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
574 if (!cache) {
575 pr_err("Cache allocation failed\n");
576 return;
578 pr_info("double-free on heap object\n");
579 p = kmem_cache_alloc(cache, GFP_KERNEL);
580 if (!p) {
581 pr_err("Allocation failed\n");
582 kmem_cache_destroy(cache);
583 return;
586 kmem_cache_free(cache, p);
587 kmem_cache_free(cache, p);
588 kmem_cache_destroy(cache);
591 static noinline void __init kmem_cache_invalid_free(void)
593 char *p;
594 size_t size = 200;
595 struct kmem_cache *cache;
597 cache = kmem_cache_create("test_cache", size, 0, SLAB_TYPESAFE_BY_RCU,
598 NULL);
599 if (!cache) {
600 pr_err("Cache allocation failed\n");
601 return;
603 pr_info("invalid-free of heap object\n");
604 p = kmem_cache_alloc(cache, GFP_KERNEL);
605 if (!p) {
606 pr_err("Allocation failed\n");
607 kmem_cache_destroy(cache);
608 return;
611 /* Trigger invalid free, the object doesn't get freed */
612 kmem_cache_free(cache, p + 1);
615 * Properly free the object to prevent the "Objects remaining in
616 * test_cache on __kmem_cache_shutdown" BUG failure.
618 kmem_cache_free(cache, p);
620 kmem_cache_destroy(cache);
623 static noinline void __init kasan_memchr(void)
625 char *ptr;
626 size_t size = 24;
628 pr_info("out-of-bounds in memchr\n");
629 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
630 if (!ptr)
631 return;
633 kasan_ptr_result = memchr(ptr, '1', size + 1);
634 kfree(ptr);
637 static noinline void __init kasan_memcmp(void)
639 char *ptr;
640 size_t size = 24;
641 int arr[9];
643 pr_info("out-of-bounds in memcmp\n");
644 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
645 if (!ptr)
646 return;
648 memset(arr, 0, sizeof(arr));
649 kasan_int_result = memcmp(ptr, arr, size + 1);
650 kfree(ptr);
653 static noinline void __init kasan_strings(void)
655 char *ptr;
656 size_t size = 24;
658 pr_info("use-after-free in strchr\n");
659 ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
660 if (!ptr)
661 return;
663 kfree(ptr);
666 * Try to cause only 1 invalid access (less spam in dmesg).
667 * For that we need ptr to point to zeroed byte.
668 * Skip metadata that could be stored in freed object so ptr
669 * will likely point to zeroed byte.
671 ptr += 16;
672 kasan_ptr_result = strchr(ptr, '1');
674 pr_info("use-after-free in strrchr\n");
675 kasan_ptr_result = strrchr(ptr, '1');
677 pr_info("use-after-free in strcmp\n");
678 kasan_int_result = strcmp(ptr, "2");
680 pr_info("use-after-free in strncmp\n");
681 kasan_int_result = strncmp(ptr, "2", 1);
683 pr_info("use-after-free in strlen\n");
684 kasan_int_result = strlen(ptr);
686 pr_info("use-after-free in strnlen\n");
687 kasan_int_result = strnlen(ptr, 1);
690 static noinline void __init kasan_bitops(void)
693 * Allocate 1 more byte, which causes kzalloc to round up to 16-bytes;
694 * this way we do not actually corrupt other memory.
696 long *bits = kzalloc(sizeof(*bits) + 1, GFP_KERNEL);
697 if (!bits)
698 return;
701 * Below calls try to access bit within allocated memory; however, the
702 * below accesses are still out-of-bounds, since bitops are defined to
703 * operate on the whole long the bit is in.
705 pr_info("out-of-bounds in set_bit\n");
706 set_bit(BITS_PER_LONG, bits);
708 pr_info("out-of-bounds in __set_bit\n");
709 __set_bit(BITS_PER_LONG, bits);
711 pr_info("out-of-bounds in clear_bit\n");
712 clear_bit(BITS_PER_LONG, bits);
714 pr_info("out-of-bounds in __clear_bit\n");
715 __clear_bit(BITS_PER_LONG, bits);
717 pr_info("out-of-bounds in clear_bit_unlock\n");
718 clear_bit_unlock(BITS_PER_LONG, bits);
720 pr_info("out-of-bounds in __clear_bit_unlock\n");
721 __clear_bit_unlock(BITS_PER_LONG, bits);
723 pr_info("out-of-bounds in change_bit\n");
724 change_bit(BITS_PER_LONG, bits);
726 pr_info("out-of-bounds in __change_bit\n");
727 __change_bit(BITS_PER_LONG, bits);
730 * Below calls try to access bit beyond allocated memory.
732 pr_info("out-of-bounds in test_and_set_bit\n");
733 test_and_set_bit(BITS_PER_LONG + BITS_PER_BYTE, bits);
735 pr_info("out-of-bounds in __test_and_set_bit\n");
736 __test_and_set_bit(BITS_PER_LONG + BITS_PER_BYTE, bits);
738 pr_info("out-of-bounds in test_and_set_bit_lock\n");
739 test_and_set_bit_lock(BITS_PER_LONG + BITS_PER_BYTE, bits);
741 pr_info("out-of-bounds in test_and_clear_bit\n");
742 test_and_clear_bit(BITS_PER_LONG + BITS_PER_BYTE, bits);
744 pr_info("out-of-bounds in __test_and_clear_bit\n");
745 __test_and_clear_bit(BITS_PER_LONG + BITS_PER_BYTE, bits);
747 pr_info("out-of-bounds in test_and_change_bit\n");
748 test_and_change_bit(BITS_PER_LONG + BITS_PER_BYTE, bits);
750 pr_info("out-of-bounds in __test_and_change_bit\n");
751 __test_and_change_bit(BITS_PER_LONG + BITS_PER_BYTE, bits);
753 pr_info("out-of-bounds in test_bit\n");
754 kasan_int_result = test_bit(BITS_PER_LONG + BITS_PER_BYTE, bits);
756 #if defined(clear_bit_unlock_is_negative_byte)
757 pr_info("out-of-bounds in clear_bit_unlock_is_negative_byte\n");
758 kasan_int_result = clear_bit_unlock_is_negative_byte(BITS_PER_LONG +
759 BITS_PER_BYTE, bits);
760 #endif
761 kfree(bits);
764 static noinline void __init kmalloc_double_kzfree(void)
766 char *ptr;
767 size_t size = 16;
769 pr_info("double-free (kzfree)\n");
770 ptr = kmalloc(size, GFP_KERNEL);
771 if (!ptr) {
772 pr_err("Allocation failed\n");
773 return;
776 kzfree(ptr);
777 kzfree(ptr);
780 #ifdef CONFIG_KASAN_VMALLOC
781 static noinline void __init vmalloc_oob(void)
783 void *area;
785 pr_info("vmalloc out-of-bounds\n");
788 * We have to be careful not to hit the guard page.
789 * The MMU will catch that and crash us.
791 area = vmalloc(3000);
792 if (!area) {
793 pr_err("Allocation failed\n");
794 return;
797 ((volatile char *)area)[3100];
798 vfree(area);
800 #else
801 static void __init vmalloc_oob(void) {}
802 #endif
804 static int __init kmalloc_tests_init(void)
807 * Temporarily enable multi-shot mode. Otherwise, we'd only get a
808 * report for the first case.
810 bool multishot = kasan_save_enable_multi_shot();
812 kmalloc_oob_right();
813 kmalloc_oob_left();
814 kmalloc_node_oob_right();
815 #ifdef CONFIG_SLUB
816 kmalloc_pagealloc_oob_right();
817 kmalloc_pagealloc_uaf();
818 kmalloc_pagealloc_invalid_free();
819 #endif
820 kmalloc_large_oob_right();
821 kmalloc_oob_krealloc_more();
822 kmalloc_oob_krealloc_less();
823 kmalloc_oob_16();
824 kmalloc_oob_in_memset();
825 kmalloc_oob_memset_2();
826 kmalloc_oob_memset_4();
827 kmalloc_oob_memset_8();
828 kmalloc_oob_memset_16();
829 kmalloc_memmove_invalid_size();
830 kmalloc_uaf();
831 kmalloc_uaf_memset();
832 kmalloc_uaf2();
833 kfree_via_page();
834 kfree_via_phys();
835 kmem_cache_oob();
836 memcg_accounted_kmem_cache();
837 kasan_stack_oob();
838 kasan_global_oob();
839 kasan_alloca_oob_left();
840 kasan_alloca_oob_right();
841 ksize_unpoisons_memory();
842 copy_user_test();
843 kmem_cache_double_free();
844 kmem_cache_invalid_free();
845 kasan_memchr();
846 kasan_memcmp();
847 kasan_strings();
848 kasan_bitops();
849 kmalloc_double_kzfree();
850 vmalloc_oob();
852 kasan_restore_multi_shot(multishot);
854 return -EAGAIN;
857 module_init(kmalloc_tests_init);
858 MODULE_LICENSE("GPL");