3 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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>
25 * Note: test functions are marked noinline so that their names appear in
29 static noinline void __init kmalloc_oob_right(void)
34 pr_info("out-of-bounds to right\n");
35 ptr = kmalloc(size, GFP_KERNEL);
37 pr_err("Allocation failed\n");
45 static noinline void __init kmalloc_oob_left(void)
50 pr_info("out-of-bounds to left\n");
51 ptr = kmalloc(size, GFP_KERNEL);
53 pr_err("Allocation failed\n");
61 static noinline void __init kmalloc_node_oob_right(void)
66 pr_info("kmalloc_node(): out-of-bounds to right\n");
67 ptr = kmalloc_node(size, GFP_KERNEL, 0);
69 pr_err("Allocation failed\n");
78 static noinline void __init kmalloc_pagealloc_oob_right(void)
81 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
83 /* Allocate a chunk that does not fit into a SLUB cache to trigger
84 * the page allocator fallback.
86 pr_info("kmalloc pagealloc allocation: out-of-bounds to right\n");
87 ptr = kmalloc(size, GFP_KERNEL);
89 pr_err("Allocation failed\n");
98 static noinline void __init kmalloc_large_oob_right(void)
101 size_t size = KMALLOC_MAX_CACHE_SIZE - 256;
102 /* Allocate a chunk that is large enough, but still fits into a slab
103 * and does not trigger the page allocator fallback in SLUB.
105 pr_info("kmalloc large allocation: out-of-bounds to right\n");
106 ptr = kmalloc(size, GFP_KERNEL);
108 pr_err("Allocation failed\n");
116 static noinline void __init kmalloc_oob_krealloc_more(void)
122 pr_info("out-of-bounds after krealloc more\n");
123 ptr1 = kmalloc(size1, GFP_KERNEL);
124 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
125 if (!ptr1 || !ptr2) {
126 pr_err("Allocation failed\n");
135 static noinline void __init kmalloc_oob_krealloc_less(void)
141 pr_info("out-of-bounds after krealloc less\n");
142 ptr1 = kmalloc(size1, GFP_KERNEL);
143 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
144 if (!ptr1 || !ptr2) {
145 pr_err("Allocation failed\n");
153 static noinline void __init kmalloc_oob_16(void)
159 pr_info("kmalloc out-of-bounds for 16-bytes access\n");
160 ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
161 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
162 if (!ptr1 || !ptr2) {
163 pr_err("Allocation failed\n");
173 static noinline void __init kmalloc_oob_memset_2(void)
178 pr_info("out-of-bounds in memset2\n");
179 ptr = kmalloc(size, GFP_KERNEL);
181 pr_err("Allocation failed\n");
189 static noinline void __init kmalloc_oob_memset_4(void)
194 pr_info("out-of-bounds in memset4\n");
195 ptr = kmalloc(size, GFP_KERNEL);
197 pr_err("Allocation failed\n");
206 static noinline void __init kmalloc_oob_memset_8(void)
211 pr_info("out-of-bounds in memset8\n");
212 ptr = kmalloc(size, GFP_KERNEL);
214 pr_err("Allocation failed\n");
222 static noinline void __init kmalloc_oob_memset_16(void)
227 pr_info("out-of-bounds in memset16\n");
228 ptr = kmalloc(size, GFP_KERNEL);
230 pr_err("Allocation failed\n");
234 memset(ptr+1, 0, 16);
238 static noinline void __init kmalloc_oob_in_memset(void)
243 pr_info("out-of-bounds in memset\n");
244 ptr = kmalloc(size, GFP_KERNEL);
246 pr_err("Allocation failed\n");
250 memset(ptr, 0, size+5);
254 static noinline void __init kmalloc_uaf(void)
259 pr_info("use-after-free\n");
260 ptr = kmalloc(size, GFP_KERNEL);
262 pr_err("Allocation failed\n");
270 static noinline void __init kmalloc_uaf_memset(void)
275 pr_info("use-after-free in memset\n");
276 ptr = kmalloc(size, GFP_KERNEL);
278 pr_err("Allocation failed\n");
283 memset(ptr, 0, size);
286 static noinline void __init kmalloc_uaf2(void)
291 pr_info("use-after-free after another kmalloc\n");
292 ptr1 = kmalloc(size, GFP_KERNEL);
294 pr_err("Allocation failed\n");
299 ptr2 = kmalloc(size, GFP_KERNEL);
301 pr_err("Allocation failed\n");
307 pr_err("Could not detect use-after-free: ptr1 == ptr2\n");
311 static noinline void __init kmem_cache_oob(void)
315 struct kmem_cache *cache = kmem_cache_create("test_cache",
319 pr_err("Cache allocation failed\n");
322 pr_info("out-of-bounds in kmem_cache_alloc\n");
323 p = kmem_cache_alloc(cache, GFP_KERNEL);
325 pr_err("Allocation failed\n");
326 kmem_cache_destroy(cache);
331 kmem_cache_free(cache, p);
332 kmem_cache_destroy(cache);
335 static noinline void __init memcg_accounted_kmem_cache(void)
340 struct kmem_cache *cache;
342 cache = kmem_cache_create("test_cache", size, 0, SLAB_ACCOUNT, NULL);
344 pr_err("Cache allocation failed\n");
348 pr_info("allocate memcg accounted object\n");
350 * Several allocations with a delay to allow for lazy per memcg kmem
353 for (i = 0; i < 5; i++) {
354 p = kmem_cache_alloc(cache, GFP_KERNEL);
356 pr_err("Allocation failed\n");
359 kmem_cache_free(cache, p);
364 kmem_cache_destroy(cache);
367 static char global_array[10];
369 static noinline void __init kasan_global_oob(void)
372 char *p = &global_array[ARRAY_SIZE(global_array) + i];
374 pr_info("out-of-bounds global variable\n");
378 static noinline void __init kasan_stack_oob(void)
380 char stack_array[10];
382 char *p = &stack_array[ARRAY_SIZE(stack_array) + i];
384 pr_info("out-of-bounds on stack\n");
388 static noinline void __init ksize_unpoisons_memory(void)
391 size_t size = 123, real_size = size;
393 pr_info("ksize() unpoisons the whole allocated chunk\n");
394 ptr = kmalloc(size, GFP_KERNEL);
396 pr_err("Allocation failed\n");
399 real_size = ksize(ptr);
400 /* This access doesn't trigger an error. */
403 ptr[real_size] = 'y';
407 static noinline void __init copy_user_test(void)
410 char __user *usermem;
414 kmem = kmalloc(size, GFP_KERNEL);
418 usermem = (char __user *)vm_mmap(NULL, 0, PAGE_SIZE,
419 PROT_READ | PROT_WRITE | PROT_EXEC,
420 MAP_ANONYMOUS | MAP_PRIVATE, 0);
421 if (IS_ERR(usermem)) {
422 pr_err("Failed to allocate user memory\n");
427 pr_info("out-of-bounds in copy_from_user()\n");
428 unused = copy_from_user(kmem, usermem, size + 1);
430 pr_info("out-of-bounds in copy_to_user()\n");
431 unused = copy_to_user(usermem, kmem, size + 1);
433 pr_info("out-of-bounds in __copy_from_user()\n");
434 unused = __copy_from_user(kmem, usermem, size + 1);
436 pr_info("out-of-bounds in __copy_to_user()\n");
437 unused = __copy_to_user(usermem, kmem, size + 1);
439 pr_info("out-of-bounds in __copy_from_user_inatomic()\n");
440 unused = __copy_from_user_inatomic(kmem, usermem, size + 1);
442 pr_info("out-of-bounds in __copy_to_user_inatomic()\n");
443 unused = __copy_to_user_inatomic(usermem, kmem, size + 1);
445 pr_info("out-of-bounds in strncpy_from_user()\n");
446 unused = strncpy_from_user(kmem, usermem, size + 1);
448 vm_munmap((unsigned long)usermem, PAGE_SIZE);
452 static noinline void __init use_after_scope_test(void)
454 volatile char *volatile p;
456 pr_info("use-after-scope on int\n");
465 pr_info("use-after-scope on array\n");
467 char local[1024] = {0};
475 static int __init kmalloc_tests_init(void)
479 kmalloc_node_oob_right();
481 kmalloc_pagealloc_oob_right();
483 kmalloc_large_oob_right();
484 kmalloc_oob_krealloc_more();
485 kmalloc_oob_krealloc_less();
487 kmalloc_oob_in_memset();
488 kmalloc_oob_memset_2();
489 kmalloc_oob_memset_4();
490 kmalloc_oob_memset_8();
491 kmalloc_oob_memset_16();
493 kmalloc_uaf_memset();
496 memcg_accounted_kmem_cache();
499 ksize_unpoisons_memory();
501 use_after_scope_test();
505 module_init(kmalloc_tests_init);
506 MODULE_LICENSE("GPL");