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");
69 static noinline
void __init
kmalloc_pagealloc_oob_right(void)
72 size_t size
= KMALLOC_MAX_CACHE_SIZE
+ 10;
74 /* Allocate a chunk that does not fit into a SLUB cache to trigger
75 * the page allocator fallback.
77 pr_info("kmalloc pagealloc allocation: out-of-bounds to right\n");
78 ptr
= kmalloc(size
, GFP_KERNEL
);
80 pr_err("Allocation failed\n");
89 static noinline
void __init
kmalloc_large_oob_right(void)
92 size_t size
= KMALLOC_MAX_CACHE_SIZE
- 256;
93 /* Allocate a chunk that is large enough, but still fits into a slab
94 * and does not trigger the page allocator fallback in SLUB.
96 pr_info("kmalloc large allocation: out-of-bounds to right\n");
97 ptr
= kmalloc(size
, GFP_KERNEL
);
99 pr_err("Allocation failed\n");
107 static noinline
void __init
kmalloc_oob_krealloc_more(void)
113 pr_info("out-of-bounds after krealloc more\n");
114 ptr1
= kmalloc(size1
, GFP_KERNEL
);
115 ptr2
= krealloc(ptr1
, size2
, GFP_KERNEL
);
116 if (!ptr1
|| !ptr2
) {
117 pr_err("Allocation failed\n");
126 static noinline
void __init
kmalloc_oob_krealloc_less(void)
132 pr_info("out-of-bounds after krealloc less\n");
133 ptr1
= kmalloc(size1
, GFP_KERNEL
);
134 ptr2
= krealloc(ptr1
, size2
, GFP_KERNEL
);
135 if (!ptr1
|| !ptr2
) {
136 pr_err("Allocation failed\n");
144 static noinline
void __init
kmalloc_oob_16(void)
150 pr_info("kmalloc out-of-bounds for 16-bytes access\n");
151 ptr1
= kmalloc(sizeof(*ptr1
) - 3, GFP_KERNEL
);
152 ptr2
= kmalloc(sizeof(*ptr2
), GFP_KERNEL
);
153 if (!ptr1
|| !ptr2
) {
154 pr_err("Allocation failed\n");
164 static noinline
void __init
kmalloc_oob_memset_2(void)
169 pr_info("out-of-bounds in memset2\n");
170 ptr
= kmalloc(size
, GFP_KERNEL
);
172 pr_err("Allocation failed\n");
180 static noinline
void __init
kmalloc_oob_memset_4(void)
185 pr_info("out-of-bounds in memset4\n");
186 ptr
= kmalloc(size
, GFP_KERNEL
);
188 pr_err("Allocation failed\n");
197 static noinline
void __init
kmalloc_oob_memset_8(void)
202 pr_info("out-of-bounds in memset8\n");
203 ptr
= kmalloc(size
, GFP_KERNEL
);
205 pr_err("Allocation failed\n");
213 static noinline
void __init
kmalloc_oob_memset_16(void)
218 pr_info("out-of-bounds in memset16\n");
219 ptr
= kmalloc(size
, GFP_KERNEL
);
221 pr_err("Allocation failed\n");
225 memset(ptr
+1, 0, 16);
229 static noinline
void __init
kmalloc_oob_in_memset(void)
234 pr_info("out-of-bounds in memset\n");
235 ptr
= kmalloc(size
, GFP_KERNEL
);
237 pr_err("Allocation failed\n");
241 memset(ptr
, 0, size
+5);
245 static noinline
void __init
kmalloc_uaf(void)
250 pr_info("use-after-free\n");
251 ptr
= kmalloc(size
, GFP_KERNEL
);
253 pr_err("Allocation failed\n");
261 static noinline
void __init
kmalloc_uaf_memset(void)
266 pr_info("use-after-free in memset\n");
267 ptr
= kmalloc(size
, GFP_KERNEL
);
269 pr_err("Allocation failed\n");
274 memset(ptr
, 0, size
);
277 static noinline
void __init
kmalloc_uaf2(void)
282 pr_info("use-after-free after another kmalloc\n");
283 ptr1
= kmalloc(size
, GFP_KERNEL
);
285 pr_err("Allocation failed\n");
290 ptr2
= kmalloc(size
, GFP_KERNEL
);
292 pr_err("Allocation failed\n");
298 pr_err("Could not detect use-after-free: ptr1 == ptr2\n");
302 static noinline
void __init
kmem_cache_oob(void)
306 struct kmem_cache
*cache
= kmem_cache_create("test_cache",
310 pr_err("Cache allocation failed\n");
313 pr_info("out-of-bounds in kmem_cache_alloc\n");
314 p
= kmem_cache_alloc(cache
, GFP_KERNEL
);
316 pr_err("Allocation failed\n");
317 kmem_cache_destroy(cache
);
322 kmem_cache_free(cache
, p
);
323 kmem_cache_destroy(cache
);
326 static char global_array
[10];
328 static noinline
void __init
kasan_global_oob(void)
331 char *p
= &global_array
[ARRAY_SIZE(global_array
) + i
];
333 pr_info("out-of-bounds global variable\n");
337 static noinline
void __init
kasan_stack_oob(void)
339 char stack_array
[10];
341 char *p
= &stack_array
[ARRAY_SIZE(stack_array
) + i
];
343 pr_info("out-of-bounds on stack\n");
347 static int __init
kmalloc_tests_init(void)
351 kmalloc_node_oob_right();
353 kmalloc_pagealloc_oob_right();
355 kmalloc_large_oob_right();
356 kmalloc_oob_krealloc_more();
357 kmalloc_oob_krealloc_less();
359 kmalloc_oob_in_memset();
360 kmalloc_oob_memset_2();
361 kmalloc_oob_memset_4();
362 kmalloc_oob_memset_8();
363 kmalloc_oob_memset_16();
365 kmalloc_uaf_memset();
373 module_init(kmalloc_tests_init
);
374 MODULE_LICENSE("GPL");