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_rigth(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");
103 static noinline
void __init
kmalloc_oob_krealloc_less(void)
109 pr_info("out-of-bounds after krealloc less\n");
110 ptr1
= kmalloc(size1
, GFP_KERNEL
);
111 ptr2
= krealloc(ptr1
, size2
, GFP_KERNEL
);
112 if (!ptr1
|| !ptr2
) {
113 pr_err("Allocation failed\n");
121 static noinline
void __init
kmalloc_oob_16(void)
127 pr_info("kmalloc out-of-bounds for 16-bytes access\n");
128 ptr1
= kmalloc(sizeof(*ptr1
) - 3, GFP_KERNEL
);
129 ptr2
= kmalloc(sizeof(*ptr2
), GFP_KERNEL
);
130 if (!ptr1
|| !ptr2
) {
131 pr_err("Allocation failed\n");
141 static noinline
void __init
kmalloc_oob_in_memset(void)
146 pr_info("out-of-bounds in memset\n");
147 ptr
= kmalloc(size
, GFP_KERNEL
);
149 pr_err("Allocation failed\n");
153 memset(ptr
, 0, size
+5);
157 static noinline
void __init
kmalloc_uaf(void)
162 pr_info("use-after-free\n");
163 ptr
= kmalloc(size
, GFP_KERNEL
);
165 pr_err("Allocation failed\n");
173 static noinline
void __init
kmalloc_uaf_memset(void)
178 pr_info("use-after-free in memset\n");
179 ptr
= kmalloc(size
, GFP_KERNEL
);
181 pr_err("Allocation failed\n");
186 memset(ptr
, 0, size
);
189 static noinline
void __init
kmalloc_uaf2(void)
194 pr_info("use-after-free after another kmalloc\n");
195 ptr1
= kmalloc(size
, GFP_KERNEL
);
197 pr_err("Allocation failed\n");
202 ptr2
= kmalloc(size
, GFP_KERNEL
);
204 pr_err("Allocation failed\n");
212 static noinline
void __init
kmem_cache_oob(void)
216 struct kmem_cache
*cache
= kmem_cache_create("test_cache",
220 pr_err("Cache allocation failed\n");
223 pr_info("out-of-bounds in kmem_cache_alloc\n");
224 p
= kmem_cache_alloc(cache
, GFP_KERNEL
);
226 pr_err("Allocation failed\n");
227 kmem_cache_destroy(cache
);
232 kmem_cache_free(cache
, p
);
233 kmem_cache_destroy(cache
);
236 static char global_array
[10];
238 static noinline
void __init
kasan_global_oob(void)
241 char *p
= &global_array
[ARRAY_SIZE(global_array
) + i
];
243 pr_info("out-of-bounds global variable\n");
247 static noinline
void __init
kasan_stack_oob(void)
249 char stack_array
[10];
251 char *p
= &stack_array
[ARRAY_SIZE(stack_array
) + i
];
253 pr_info("out-of-bounds on stack\n");
257 static int __init
kmalloc_tests_init(void)
261 kmalloc_node_oob_right();
262 kmalloc_large_oob_rigth();
263 kmalloc_oob_krealloc_more();
264 kmalloc_oob_krealloc_less();
266 kmalloc_oob_in_memset();
268 kmalloc_uaf_memset();
276 module_init(kmalloc_tests_init
);
277 MODULE_LICENSE("GPL");