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/kernel.h>
15 #include <linux/printk.h>
16 #include <linux/slab.h>
17 #include <linux/string.h>
18 #include <linux/module.h>
20 static noinline void __init kmalloc_oob_right(void)
25 pr_info("out-of-bounds to right\n");
26 ptr = kmalloc(size, GFP_KERNEL);
28 pr_err("Allocation failed\n");
36 static noinline void __init kmalloc_oob_left(void)
41 pr_info("out-of-bounds to left\n");
42 ptr = kmalloc(size, GFP_KERNEL);
44 pr_err("Allocation failed\n");
52 static noinline void __init kmalloc_node_oob_right(void)
57 pr_info("kmalloc_node(): out-of-bounds to right\n");
58 ptr = kmalloc_node(size, GFP_KERNEL, 0);
60 pr_err("Allocation failed\n");
68 static noinline void __init kmalloc_large_oob_right(void)
71 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
73 pr_info("kmalloc large allocation: out-of-bounds to right\n");
74 ptr = kmalloc(size, GFP_KERNEL);
76 pr_err("Allocation failed\n");
84 static noinline void __init kmalloc_oob_krealloc_more(void)
90 pr_info("out-of-bounds after krealloc more\n");
91 ptr1 = kmalloc(size1, GFP_KERNEL);
92 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
94 pr_err("Allocation failed\n");
104 static noinline void __init kmalloc_oob_krealloc_less(void)
110 pr_info("out-of-bounds after krealloc less\n");
111 ptr1 = kmalloc(size1, GFP_KERNEL);
112 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
113 if (!ptr1 || !ptr2) {
114 pr_err("Allocation failed\n");
122 static noinline void __init kmalloc_oob_16(void)
128 pr_info("kmalloc out-of-bounds for 16-bytes access\n");
129 ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
130 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
131 if (!ptr1 || !ptr2) {
132 pr_err("Allocation failed\n");
142 static noinline void __init kmalloc_oob_memset_2(void)
147 pr_info("out-of-bounds in memset2\n");
148 ptr = kmalloc(size, GFP_KERNEL);
150 pr_err("Allocation failed\n");
158 static noinline void __init kmalloc_oob_memset_4(void)
163 pr_info("out-of-bounds in memset4\n");
164 ptr = kmalloc(size, GFP_KERNEL);
166 pr_err("Allocation failed\n");
175 static noinline void __init kmalloc_oob_memset_8(void)
180 pr_info("out-of-bounds in memset8\n");
181 ptr = kmalloc(size, GFP_KERNEL);
183 pr_err("Allocation failed\n");
191 static noinline void __init kmalloc_oob_memset_16(void)
196 pr_info("out-of-bounds in memset16\n");
197 ptr = kmalloc(size, GFP_KERNEL);
199 pr_err("Allocation failed\n");
203 memset(ptr+1, 0, 16);
207 static noinline void __init kmalloc_oob_in_memset(void)
212 pr_info("out-of-bounds in memset\n");
213 ptr = kmalloc(size, GFP_KERNEL);
215 pr_err("Allocation failed\n");
219 memset(ptr, 0, size+5);
223 static noinline void __init kmalloc_uaf(void)
228 pr_info("use-after-free\n");
229 ptr = kmalloc(size, GFP_KERNEL);
231 pr_err("Allocation failed\n");
239 static noinline void __init kmalloc_uaf_memset(void)
244 pr_info("use-after-free in memset\n");
245 ptr = kmalloc(size, GFP_KERNEL);
247 pr_err("Allocation failed\n");
252 memset(ptr, 0, size);
255 static noinline void __init kmalloc_uaf2(void)
260 pr_info("use-after-free after another kmalloc\n");
261 ptr1 = kmalloc(size, GFP_KERNEL);
263 pr_err("Allocation failed\n");
268 ptr2 = kmalloc(size, GFP_KERNEL);
270 pr_err("Allocation failed\n");
278 static noinline void __init kmem_cache_oob(void)
282 struct kmem_cache *cache = kmem_cache_create("test_cache",
286 pr_err("Cache allocation failed\n");
289 pr_info("out-of-bounds in kmem_cache_alloc\n");
290 p = kmem_cache_alloc(cache, GFP_KERNEL);
292 pr_err("Allocation failed\n");
293 kmem_cache_destroy(cache);
298 kmem_cache_free(cache, p);
299 kmem_cache_destroy(cache);
302 static char global_array[10];
304 static noinline void __init kasan_global_oob(void)
307 char *p = &global_array[ARRAY_SIZE(global_array) + i];
309 pr_info("out-of-bounds global variable\n");
313 static noinline void __init kasan_stack_oob(void)
315 char stack_array[10];
317 char *p = &stack_array[ARRAY_SIZE(stack_array) + i];
319 pr_info("out-of-bounds on stack\n");
323 static int __init kmalloc_tests_init(void)
327 kmalloc_node_oob_right();
328 kmalloc_large_oob_right();
329 kmalloc_oob_krealloc_more();
330 kmalloc_oob_krealloc_less();
332 kmalloc_oob_in_memset();
333 kmalloc_oob_memset_2();
334 kmalloc_oob_memset_4();
335 kmalloc_oob_memset_8();
336 kmalloc_oob_memset_16();
338 kmalloc_uaf_memset();
346 module_init(kmalloc_tests_init);
347 MODULE_LICENSE("GPL");