3 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 * Author: Andrey Ryabinin <a.ryabinin@samsung.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
12 #define pr_fmt(fmt) "kasan test: %s " fmt, __func__
14 #include <linux/delay.h>
15 #include <linux/kernel.h>
16 #include <linux/mman.h>
18 #include <linux/printk.h>
19 #include <linux/slab.h>
20 #include <linux/string.h>
21 #include <linux/uaccess.h>
22 #include <linux/module.h>
23 #include <linux/kasan.h>
26 * Note: test functions are marked noinline so that their names appear in
30 static noinline void __init kmalloc_oob_right(void)
35 pr_info("out-of-bounds to right\n");
36 ptr = kmalloc(size, GFP_KERNEL);
38 pr_err("Allocation failed\n");
46 static noinline void __init kmalloc_oob_left(void)
51 pr_info("out-of-bounds to left\n");
52 ptr = kmalloc(size, GFP_KERNEL);
54 pr_err("Allocation failed\n");
62 static noinline void __init kmalloc_node_oob_right(void)
67 pr_info("kmalloc_node(): out-of-bounds to right\n");
68 ptr = kmalloc_node(size, GFP_KERNEL, 0);
70 pr_err("Allocation failed\n");
79 static noinline void __init kmalloc_pagealloc_oob_right(void)
82 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
84 /* Allocate a chunk that does not fit into a SLUB cache to trigger
85 * the page allocator fallback.
87 pr_info("kmalloc pagealloc allocation: out-of-bounds to right\n");
88 ptr = kmalloc(size, GFP_KERNEL);
90 pr_err("Allocation failed\n");
98 static noinline void __init kmalloc_pagealloc_uaf(void)
101 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
103 pr_info("kmalloc pagealloc allocation: use-after-free\n");
104 ptr = kmalloc(size, GFP_KERNEL);
106 pr_err("Allocation failed\n");
114 static noinline void __init kmalloc_pagealloc_invalid_free(void)
117 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
119 pr_info("kmalloc pagealloc allocation: invalid-free\n");
120 ptr = kmalloc(size, GFP_KERNEL);
122 pr_err("Allocation failed\n");
130 static noinline void __init kmalloc_large_oob_right(void)
133 size_t size = KMALLOC_MAX_CACHE_SIZE - 256;
134 /* Allocate a chunk that is large enough, but still fits into a slab
135 * and does not trigger the page allocator fallback in SLUB.
137 pr_info("kmalloc large allocation: out-of-bounds to right\n");
138 ptr = kmalloc(size, GFP_KERNEL);
140 pr_err("Allocation failed\n");
148 static noinline void __init kmalloc_oob_krealloc_more(void)
154 pr_info("out-of-bounds after krealloc more\n");
155 ptr1 = kmalloc(size1, GFP_KERNEL);
156 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
157 if (!ptr1 || !ptr2) {
158 pr_err("Allocation failed\n");
168 static noinline void __init kmalloc_oob_krealloc_less(void)
174 pr_info("out-of-bounds after krealloc less\n");
175 ptr1 = kmalloc(size1, GFP_KERNEL);
176 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
177 if (!ptr1 || !ptr2) {
178 pr_err("Allocation failed\n");
186 static noinline void __init kmalloc_oob_16(void)
192 pr_info("kmalloc out-of-bounds for 16-bytes access\n");
193 ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
194 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
195 if (!ptr1 || !ptr2) {
196 pr_err("Allocation failed\n");
206 static noinline void __init kmalloc_oob_memset_2(void)
211 pr_info("out-of-bounds in memset2\n");
212 ptr = kmalloc(size, GFP_KERNEL);
214 pr_err("Allocation failed\n");
222 static noinline void __init kmalloc_oob_memset_4(void)
227 pr_info("out-of-bounds in memset4\n");
228 ptr = kmalloc(size, GFP_KERNEL);
230 pr_err("Allocation failed\n");
239 static noinline void __init kmalloc_oob_memset_8(void)
244 pr_info("out-of-bounds in memset8\n");
245 ptr = kmalloc(size, GFP_KERNEL);
247 pr_err("Allocation failed\n");
255 static noinline void __init kmalloc_oob_memset_16(void)
260 pr_info("out-of-bounds in memset16\n");
261 ptr = kmalloc(size, GFP_KERNEL);
263 pr_err("Allocation failed\n");
267 memset(ptr+1, 0, 16);
271 static noinline void __init kmalloc_oob_in_memset(void)
276 pr_info("out-of-bounds in memset\n");
277 ptr = kmalloc(size, GFP_KERNEL);
279 pr_err("Allocation failed\n");
283 memset(ptr, 0, size+5);
287 static noinline void __init kmalloc_uaf(void)
292 pr_info("use-after-free\n");
293 ptr = kmalloc(size, GFP_KERNEL);
295 pr_err("Allocation failed\n");
303 static noinline void __init kmalloc_uaf_memset(void)
308 pr_info("use-after-free in memset\n");
309 ptr = kmalloc(size, GFP_KERNEL);
311 pr_err("Allocation failed\n");
316 memset(ptr, 0, size);
319 static noinline void __init kmalloc_uaf2(void)
324 pr_info("use-after-free after another kmalloc\n");
325 ptr1 = kmalloc(size, GFP_KERNEL);
327 pr_err("Allocation failed\n");
332 ptr2 = kmalloc(size, GFP_KERNEL);
334 pr_err("Allocation failed\n");
340 pr_err("Could not detect use-after-free: ptr1 == ptr2\n");
344 static noinline void __init kmem_cache_oob(void)
348 struct kmem_cache *cache = kmem_cache_create("test_cache",
352 pr_err("Cache allocation failed\n");
355 pr_info("out-of-bounds in kmem_cache_alloc\n");
356 p = kmem_cache_alloc(cache, GFP_KERNEL);
358 pr_err("Allocation failed\n");
359 kmem_cache_destroy(cache);
364 kmem_cache_free(cache, p);
365 kmem_cache_destroy(cache);
368 static noinline void __init memcg_accounted_kmem_cache(void)
373 struct kmem_cache *cache;
375 cache = kmem_cache_create("test_cache", size, 0, SLAB_ACCOUNT, NULL);
377 pr_err("Cache allocation failed\n");
381 pr_info("allocate memcg accounted object\n");
383 * Several allocations with a delay to allow for lazy per memcg kmem
386 for (i = 0; i < 5; i++) {
387 p = kmem_cache_alloc(cache, GFP_KERNEL);
391 kmem_cache_free(cache, p);
396 kmem_cache_destroy(cache);
399 static char global_array[10];
401 static noinline void __init kasan_global_oob(void)
404 char *p = &global_array[ARRAY_SIZE(global_array) + i];
406 pr_info("out-of-bounds global variable\n");
410 static noinline void __init kasan_stack_oob(void)
412 char stack_array[10];
414 char *p = &stack_array[ARRAY_SIZE(stack_array) + i];
416 pr_info("out-of-bounds on stack\n");
420 static noinline void __init ksize_unpoisons_memory(void)
423 size_t size = 123, real_size;
425 pr_info("ksize() unpoisons the whole allocated chunk\n");
426 ptr = kmalloc(size, GFP_KERNEL);
428 pr_err("Allocation failed\n");
431 real_size = ksize(ptr);
432 /* This access doesn't trigger an error. */
435 ptr[real_size] = 'y';
439 static noinline void __init copy_user_test(void)
442 char __user *usermem;
446 kmem = kmalloc(size, GFP_KERNEL);
450 usermem = (char __user *)vm_mmap(NULL, 0, PAGE_SIZE,
451 PROT_READ | PROT_WRITE | PROT_EXEC,
452 MAP_ANONYMOUS | MAP_PRIVATE, 0);
453 if (IS_ERR(usermem)) {
454 pr_err("Failed to allocate user memory\n");
459 pr_info("out-of-bounds in copy_from_user()\n");
460 unused = copy_from_user(kmem, usermem, size + 1);
462 pr_info("out-of-bounds in copy_to_user()\n");
463 unused = copy_to_user(usermem, kmem, size + 1);
465 pr_info("out-of-bounds in __copy_from_user()\n");
466 unused = __copy_from_user(kmem, usermem, size + 1);
468 pr_info("out-of-bounds in __copy_to_user()\n");
469 unused = __copy_to_user(usermem, kmem, size + 1);
471 pr_info("out-of-bounds in __copy_from_user_inatomic()\n");
472 unused = __copy_from_user_inatomic(kmem, usermem, size + 1);
474 pr_info("out-of-bounds in __copy_to_user_inatomic()\n");
475 unused = __copy_to_user_inatomic(usermem, kmem, size + 1);
477 pr_info("out-of-bounds in strncpy_from_user()\n");
478 unused = strncpy_from_user(kmem, usermem, size + 1);
480 vm_munmap((unsigned long)usermem, PAGE_SIZE);
484 static noinline void __init use_after_scope_test(void)
486 volatile char *volatile p;
488 pr_info("use-after-scope on int\n");
497 pr_info("use-after-scope on array\n");
499 char local[1024] = {0};
507 static noinline void __init kasan_alloca_oob_left(void)
510 char alloca_array[i];
511 char *p = alloca_array - 1;
513 pr_info("out-of-bounds to left on alloca\n");
517 static noinline void __init kasan_alloca_oob_right(void)
520 char alloca_array[i];
521 char *p = alloca_array + i;
523 pr_info("out-of-bounds to right on alloca\n");
527 static noinline void __init kmem_cache_double_free(void)
531 struct kmem_cache *cache;
533 cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
535 pr_err("Cache allocation failed\n");
538 pr_info("double-free on heap object\n");
539 p = kmem_cache_alloc(cache, GFP_KERNEL);
541 pr_err("Allocation failed\n");
542 kmem_cache_destroy(cache);
546 kmem_cache_free(cache, p);
547 kmem_cache_free(cache, p);
548 kmem_cache_destroy(cache);
551 static noinline void __init kmem_cache_invalid_free(void)
555 struct kmem_cache *cache;
557 cache = kmem_cache_create("test_cache", size, 0, SLAB_TYPESAFE_BY_RCU,
560 pr_err("Cache allocation failed\n");
563 pr_info("invalid-free of heap object\n");
564 p = kmem_cache_alloc(cache, GFP_KERNEL);
566 pr_err("Allocation failed\n");
567 kmem_cache_destroy(cache);
571 /* Trigger invalid free, the object doesn't get freed */
572 kmem_cache_free(cache, p + 1);
575 * Properly free the object to prevent the "Objects remaining in
576 * test_cache on __kmem_cache_shutdown" BUG failure.
578 kmem_cache_free(cache, p);
580 kmem_cache_destroy(cache);
583 static int __init kmalloc_tests_init(void)
586 * Temporarily enable multi-shot mode. Otherwise, we'd only get a
587 * report for the first case.
589 bool multishot = kasan_save_enable_multi_shot();
593 kmalloc_node_oob_right();
595 kmalloc_pagealloc_oob_right();
596 kmalloc_pagealloc_uaf();
597 kmalloc_pagealloc_invalid_free();
599 kmalloc_large_oob_right();
600 kmalloc_oob_krealloc_more();
601 kmalloc_oob_krealloc_less();
603 kmalloc_oob_in_memset();
604 kmalloc_oob_memset_2();
605 kmalloc_oob_memset_4();
606 kmalloc_oob_memset_8();
607 kmalloc_oob_memset_16();
609 kmalloc_uaf_memset();
612 memcg_accounted_kmem_cache();
615 kasan_alloca_oob_left();
616 kasan_alloca_oob_right();
617 ksize_unpoisons_memory();
619 use_after_scope_test();
620 kmem_cache_double_free();
621 kmem_cache_invalid_free();
623 kasan_restore_multi_shot(multishot);
628 module_init(kmalloc_tests_init);
629 MODULE_LICENSE("GPL");