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/delay.h>
11 #include <linux/kernel.h>
12 #include <linux/mman.h>
14 #include <linux/printk.h>
15 #include <linux/slab.h>
16 #include <linux/string.h>
17 #include <linux/uaccess.h>
18 #include <linux/module.h>
19 #include <linux/kasan.h>
22 * Note: test functions are marked noinline so that their names appear in
26 static noinline
void __init
kmalloc_oob_right(void)
31 pr_info("out-of-bounds to right\n");
32 ptr
= kmalloc(size
, GFP_KERNEL
);
34 pr_err("Allocation failed\n");
42 static noinline
void __init
kmalloc_oob_left(void)
47 pr_info("out-of-bounds to left\n");
48 ptr
= kmalloc(size
, GFP_KERNEL
);
50 pr_err("Allocation failed\n");
58 static noinline
void __init
kmalloc_node_oob_right(void)
63 pr_info("kmalloc_node(): out-of-bounds to right\n");
64 ptr
= kmalloc_node(size
, GFP_KERNEL
, 0);
66 pr_err("Allocation failed\n");
75 static noinline
void __init
kmalloc_pagealloc_oob_right(void)
78 size_t size
= KMALLOC_MAX_CACHE_SIZE
+ 10;
80 /* Allocate a chunk that does not fit into a SLUB cache to trigger
81 * the page allocator fallback.
83 pr_info("kmalloc pagealloc allocation: out-of-bounds to right\n");
84 ptr
= kmalloc(size
, GFP_KERNEL
);
86 pr_err("Allocation failed\n");
94 static noinline
void __init
kmalloc_pagealloc_uaf(void)
97 size_t size
= KMALLOC_MAX_CACHE_SIZE
+ 10;
99 pr_info("kmalloc pagealloc allocation: use-after-free\n");
100 ptr
= kmalloc(size
, GFP_KERNEL
);
102 pr_err("Allocation failed\n");
110 static noinline
void __init
kmalloc_pagealloc_invalid_free(void)
113 size_t size
= KMALLOC_MAX_CACHE_SIZE
+ 10;
115 pr_info("kmalloc pagealloc allocation: invalid-free\n");
116 ptr
= kmalloc(size
, GFP_KERNEL
);
118 pr_err("Allocation failed\n");
126 static noinline
void __init
kmalloc_large_oob_right(void)
129 size_t size
= KMALLOC_MAX_CACHE_SIZE
- 256;
130 /* Allocate a chunk that is large enough, but still fits into a slab
131 * and does not trigger the page allocator fallback in SLUB.
133 pr_info("kmalloc large allocation: out-of-bounds to right\n");
134 ptr
= kmalloc(size
, GFP_KERNEL
);
136 pr_err("Allocation failed\n");
144 static noinline
void __init
kmalloc_oob_krealloc_more(void)
150 pr_info("out-of-bounds after krealloc more\n");
151 ptr1
= kmalloc(size1
, GFP_KERNEL
);
152 ptr2
= krealloc(ptr1
, size2
, GFP_KERNEL
);
153 if (!ptr1
|| !ptr2
) {
154 pr_err("Allocation failed\n");
163 static noinline
void __init
kmalloc_oob_krealloc_less(void)
169 pr_info("out-of-bounds after krealloc less\n");
170 ptr1
= kmalloc(size1
, GFP_KERNEL
);
171 ptr2
= krealloc(ptr1
, size2
, GFP_KERNEL
);
172 if (!ptr1
|| !ptr2
) {
173 pr_err("Allocation failed\n");
181 static noinline
void __init
kmalloc_oob_16(void)
187 pr_info("kmalloc out-of-bounds for 16-bytes access\n");
188 ptr1
= kmalloc(sizeof(*ptr1
) - 3, GFP_KERNEL
);
189 ptr2
= kmalloc(sizeof(*ptr2
), GFP_KERNEL
);
190 if (!ptr1
|| !ptr2
) {
191 pr_err("Allocation failed\n");
201 static noinline
void __init
kmalloc_oob_memset_2(void)
206 pr_info("out-of-bounds in memset2\n");
207 ptr
= kmalloc(size
, GFP_KERNEL
);
209 pr_err("Allocation failed\n");
217 static noinline
void __init
kmalloc_oob_memset_4(void)
222 pr_info("out-of-bounds in memset4\n");
223 ptr
= kmalloc(size
, GFP_KERNEL
);
225 pr_err("Allocation failed\n");
234 static noinline
void __init
kmalloc_oob_memset_8(void)
239 pr_info("out-of-bounds in memset8\n");
240 ptr
= kmalloc(size
, GFP_KERNEL
);
242 pr_err("Allocation failed\n");
250 static noinline
void __init
kmalloc_oob_memset_16(void)
255 pr_info("out-of-bounds in memset16\n");
256 ptr
= kmalloc(size
, GFP_KERNEL
);
258 pr_err("Allocation failed\n");
262 memset(ptr
+1, 0, 16);
266 static noinline
void __init
kmalloc_oob_in_memset(void)
271 pr_info("out-of-bounds in memset\n");
272 ptr
= kmalloc(size
, GFP_KERNEL
);
274 pr_err("Allocation failed\n");
278 memset(ptr
, 0, size
+5);
282 static noinline
void __init
kmalloc_uaf(void)
287 pr_info("use-after-free\n");
288 ptr
= kmalloc(size
, GFP_KERNEL
);
290 pr_err("Allocation failed\n");
298 static noinline
void __init
kmalloc_uaf_memset(void)
303 pr_info("use-after-free in memset\n");
304 ptr
= kmalloc(size
, GFP_KERNEL
);
306 pr_err("Allocation failed\n");
311 memset(ptr
, 0, size
);
314 static noinline
void __init
kmalloc_uaf2(void)
319 pr_info("use-after-free after another kmalloc\n");
320 ptr1
= kmalloc(size
, GFP_KERNEL
);
322 pr_err("Allocation failed\n");
327 ptr2
= kmalloc(size
, GFP_KERNEL
);
329 pr_err("Allocation failed\n");
335 pr_err("Could not detect use-after-free: ptr1 == ptr2\n");
339 static noinline
void __init
kmem_cache_oob(void)
343 struct kmem_cache
*cache
= kmem_cache_create("test_cache",
347 pr_err("Cache allocation failed\n");
350 pr_info("out-of-bounds in kmem_cache_alloc\n");
351 p
= kmem_cache_alloc(cache
, GFP_KERNEL
);
353 pr_err("Allocation failed\n");
354 kmem_cache_destroy(cache
);
359 kmem_cache_free(cache
, p
);
360 kmem_cache_destroy(cache
);
363 static noinline
void __init
memcg_accounted_kmem_cache(void)
368 struct kmem_cache
*cache
;
370 cache
= kmem_cache_create("test_cache", size
, 0, SLAB_ACCOUNT
, NULL
);
372 pr_err("Cache allocation failed\n");
376 pr_info("allocate memcg accounted object\n");
378 * Several allocations with a delay to allow for lazy per memcg kmem
381 for (i
= 0; i
< 5; i
++) {
382 p
= kmem_cache_alloc(cache
, GFP_KERNEL
);
386 kmem_cache_free(cache
, p
);
391 kmem_cache_destroy(cache
);
394 static char global_array
[10];
396 static noinline
void __init
kasan_global_oob(void)
399 char *p
= &global_array
[ARRAY_SIZE(global_array
) + i
];
401 pr_info("out-of-bounds global variable\n");
405 static noinline
void __init
kasan_stack_oob(void)
407 char stack_array
[10];
409 char *p
= &stack_array
[ARRAY_SIZE(stack_array
) + i
];
411 pr_info("out-of-bounds on stack\n");
415 static noinline
void __init
ksize_unpoisons_memory(void)
418 size_t size
= 123, real_size
;
420 pr_info("ksize() unpoisons the whole allocated chunk\n");
421 ptr
= kmalloc(size
, GFP_KERNEL
);
423 pr_err("Allocation failed\n");
426 real_size
= ksize(ptr
);
427 /* This access doesn't trigger an error. */
430 ptr
[real_size
] = 'y';
434 static noinline
void __init
copy_user_test(void)
437 char __user
*usermem
;
441 kmem
= kmalloc(size
, GFP_KERNEL
);
445 usermem
= (char __user
*)vm_mmap(NULL
, 0, PAGE_SIZE
,
446 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
447 MAP_ANONYMOUS
| MAP_PRIVATE
, 0);
448 if (IS_ERR(usermem
)) {
449 pr_err("Failed to allocate user memory\n");
454 pr_info("out-of-bounds in copy_from_user()\n");
455 unused
= copy_from_user(kmem
, usermem
, size
+ 1);
457 pr_info("out-of-bounds in copy_to_user()\n");
458 unused
= copy_to_user(usermem
, kmem
, size
+ 1);
460 pr_info("out-of-bounds in __copy_from_user()\n");
461 unused
= __copy_from_user(kmem
, usermem
, size
+ 1);
463 pr_info("out-of-bounds in __copy_to_user()\n");
464 unused
= __copy_to_user(usermem
, kmem
, size
+ 1);
466 pr_info("out-of-bounds in __copy_from_user_inatomic()\n");
467 unused
= __copy_from_user_inatomic(kmem
, usermem
, size
+ 1);
469 pr_info("out-of-bounds in __copy_to_user_inatomic()\n");
470 unused
= __copy_to_user_inatomic(usermem
, kmem
, size
+ 1);
472 pr_info("out-of-bounds in strncpy_from_user()\n");
473 unused
= strncpy_from_user(kmem
, usermem
, size
+ 1);
475 vm_munmap((unsigned long)usermem
, PAGE_SIZE
);
479 static noinline
void __init
kasan_alloca_oob_left(void)
482 char alloca_array
[i
];
483 char *p
= alloca_array
- 1;
485 pr_info("out-of-bounds to left on alloca\n");
489 static noinline
void __init
kasan_alloca_oob_right(void)
492 char alloca_array
[i
];
493 char *p
= alloca_array
+ i
;
495 pr_info("out-of-bounds to right on alloca\n");
499 static noinline
void __init
kmem_cache_double_free(void)
503 struct kmem_cache
*cache
;
505 cache
= kmem_cache_create("test_cache", size
, 0, 0, NULL
);
507 pr_err("Cache allocation failed\n");
510 pr_info("double-free on heap object\n");
511 p
= kmem_cache_alloc(cache
, GFP_KERNEL
);
513 pr_err("Allocation failed\n");
514 kmem_cache_destroy(cache
);
518 kmem_cache_free(cache
, p
);
519 kmem_cache_free(cache
, p
);
520 kmem_cache_destroy(cache
);
523 static noinline
void __init
kmem_cache_invalid_free(void)
527 struct kmem_cache
*cache
;
529 cache
= kmem_cache_create("test_cache", size
, 0, SLAB_TYPESAFE_BY_RCU
,
532 pr_err("Cache allocation failed\n");
535 pr_info("invalid-free of heap object\n");
536 p
= kmem_cache_alloc(cache
, GFP_KERNEL
);
538 pr_err("Allocation failed\n");
539 kmem_cache_destroy(cache
);
543 /* Trigger invalid free, the object doesn't get freed */
544 kmem_cache_free(cache
, p
+ 1);
547 * Properly free the object to prevent the "Objects remaining in
548 * test_cache on __kmem_cache_shutdown" BUG failure.
550 kmem_cache_free(cache
, p
);
552 kmem_cache_destroy(cache
);
555 static noinline
void __init
kasan_memchr(void)
560 pr_info("out-of-bounds in memchr\n");
561 ptr
= kmalloc(size
, GFP_KERNEL
| __GFP_ZERO
);
565 memchr(ptr
, '1', size
+ 1);
569 static noinline
void __init
kasan_memcmp(void)
575 pr_info("out-of-bounds in memcmp\n");
576 ptr
= kmalloc(size
, GFP_KERNEL
| __GFP_ZERO
);
580 memset(arr
, 0, sizeof(arr
));
581 memcmp(ptr
, arr
, size
+1);
585 static noinline
void __init
kasan_strings(void)
590 pr_info("use-after-free in strchr\n");
591 ptr
= kmalloc(size
, GFP_KERNEL
| __GFP_ZERO
);
598 * Try to cause only 1 invalid access (less spam in dmesg).
599 * For that we need ptr to point to zeroed byte.
600 * Skip metadata that could be stored in freed object so ptr
601 * will likely point to zeroed byte.
606 pr_info("use-after-free in strrchr\n");
609 pr_info("use-after-free in strcmp\n");
612 pr_info("use-after-free in strncmp\n");
613 strncmp(ptr
, "2", 1);
615 pr_info("use-after-free in strlen\n");
618 pr_info("use-after-free in strnlen\n");
622 static int __init
kmalloc_tests_init(void)
625 * Temporarily enable multi-shot mode. Otherwise, we'd only get a
626 * report for the first case.
628 bool multishot
= kasan_save_enable_multi_shot();
632 kmalloc_node_oob_right();
634 kmalloc_pagealloc_oob_right();
635 kmalloc_pagealloc_uaf();
636 kmalloc_pagealloc_invalid_free();
638 kmalloc_large_oob_right();
639 kmalloc_oob_krealloc_more();
640 kmalloc_oob_krealloc_less();
642 kmalloc_oob_in_memset();
643 kmalloc_oob_memset_2();
644 kmalloc_oob_memset_4();
645 kmalloc_oob_memset_8();
646 kmalloc_oob_memset_16();
648 kmalloc_uaf_memset();
651 memcg_accounted_kmem_cache();
654 kasan_alloca_oob_left();
655 kasan_alloca_oob_right();
656 ksize_unpoisons_memory();
658 kmem_cache_double_free();
659 kmem_cache_invalid_free();
664 kasan_restore_multi_shot(multishot
);
669 module_init(kmalloc_tests_init
);
670 MODULE_LICENSE("GPL");