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/mman.h>
17 #include <linux/printk.h>
18 #include <linux/slab.h>
19 #include <linux/string.h>
20 #include <linux/uaccess.h>
21 #include <linux/module.h>
23 static noinline
void __init
kmalloc_oob_right(void)
28 pr_info("out-of-bounds to right\n");
29 ptr
= kmalloc(size
, GFP_KERNEL
);
31 pr_err("Allocation failed\n");
39 static noinline
void __init
kmalloc_oob_left(void)
44 pr_info("out-of-bounds to left\n");
45 ptr
= kmalloc(size
, GFP_KERNEL
);
47 pr_err("Allocation failed\n");
55 static noinline
void __init
kmalloc_node_oob_right(void)
60 pr_info("kmalloc_node(): out-of-bounds to right\n");
61 ptr
= kmalloc_node(size
, GFP_KERNEL
, 0);
63 pr_err("Allocation failed\n");
72 static noinline
void __init
kmalloc_pagealloc_oob_right(void)
75 size_t size
= KMALLOC_MAX_CACHE_SIZE
+ 10;
77 /* Allocate a chunk that does not fit into a SLUB cache to trigger
78 * the page allocator fallback.
80 pr_info("kmalloc pagealloc allocation: out-of-bounds to right\n");
81 ptr
= kmalloc(size
, GFP_KERNEL
);
83 pr_err("Allocation failed\n");
92 static noinline
void __init
kmalloc_large_oob_right(void)
95 size_t size
= KMALLOC_MAX_CACHE_SIZE
- 256;
96 /* Allocate a chunk that is large enough, but still fits into a slab
97 * and does not trigger the page allocator fallback in SLUB.
99 pr_info("kmalloc large allocation: out-of-bounds to right\n");
100 ptr
= kmalloc(size
, GFP_KERNEL
);
102 pr_err("Allocation failed\n");
110 static noinline
void __init
kmalloc_oob_krealloc_more(void)
116 pr_info("out-of-bounds after krealloc more\n");
117 ptr1
= kmalloc(size1
, GFP_KERNEL
);
118 ptr2
= krealloc(ptr1
, size2
, GFP_KERNEL
);
119 if (!ptr1
|| !ptr2
) {
120 pr_err("Allocation failed\n");
129 static noinline
void __init
kmalloc_oob_krealloc_less(void)
135 pr_info("out-of-bounds after krealloc less\n");
136 ptr1
= kmalloc(size1
, GFP_KERNEL
);
137 ptr2
= krealloc(ptr1
, size2
, GFP_KERNEL
);
138 if (!ptr1
|| !ptr2
) {
139 pr_err("Allocation failed\n");
147 static noinline
void __init
kmalloc_oob_16(void)
153 pr_info("kmalloc out-of-bounds for 16-bytes access\n");
154 ptr1
= kmalloc(sizeof(*ptr1
) - 3, GFP_KERNEL
);
155 ptr2
= kmalloc(sizeof(*ptr2
), GFP_KERNEL
);
156 if (!ptr1
|| !ptr2
) {
157 pr_err("Allocation failed\n");
167 static noinline
void __init
kmalloc_oob_memset_2(void)
172 pr_info("out-of-bounds in memset2\n");
173 ptr
= kmalloc(size
, GFP_KERNEL
);
175 pr_err("Allocation failed\n");
183 static noinline
void __init
kmalloc_oob_memset_4(void)
188 pr_info("out-of-bounds in memset4\n");
189 ptr
= kmalloc(size
, GFP_KERNEL
);
191 pr_err("Allocation failed\n");
200 static noinline
void __init
kmalloc_oob_memset_8(void)
205 pr_info("out-of-bounds in memset8\n");
206 ptr
= kmalloc(size
, GFP_KERNEL
);
208 pr_err("Allocation failed\n");
216 static noinline
void __init
kmalloc_oob_memset_16(void)
221 pr_info("out-of-bounds in memset16\n");
222 ptr
= kmalloc(size
, GFP_KERNEL
);
224 pr_err("Allocation failed\n");
228 memset(ptr
+1, 0, 16);
232 static noinline
void __init
kmalloc_oob_in_memset(void)
237 pr_info("out-of-bounds in memset\n");
238 ptr
= kmalloc(size
, GFP_KERNEL
);
240 pr_err("Allocation failed\n");
244 memset(ptr
, 0, size
+5);
248 static noinline
void __init
kmalloc_uaf(void)
253 pr_info("use-after-free\n");
254 ptr
= kmalloc(size
, GFP_KERNEL
);
256 pr_err("Allocation failed\n");
264 static noinline
void __init
kmalloc_uaf_memset(void)
269 pr_info("use-after-free in memset\n");
270 ptr
= kmalloc(size
, GFP_KERNEL
);
272 pr_err("Allocation failed\n");
277 memset(ptr
, 0, size
);
280 static noinline
void __init
kmalloc_uaf2(void)
285 pr_info("use-after-free after another kmalloc\n");
286 ptr1
= kmalloc(size
, GFP_KERNEL
);
288 pr_err("Allocation failed\n");
293 ptr2
= kmalloc(size
, GFP_KERNEL
);
295 pr_err("Allocation failed\n");
301 pr_err("Could not detect use-after-free: ptr1 == ptr2\n");
305 static noinline
void __init
kmem_cache_oob(void)
309 struct kmem_cache
*cache
= kmem_cache_create("test_cache",
313 pr_err("Cache allocation failed\n");
316 pr_info("out-of-bounds in kmem_cache_alloc\n");
317 p
= kmem_cache_alloc(cache
, GFP_KERNEL
);
319 pr_err("Allocation failed\n");
320 kmem_cache_destroy(cache
);
325 kmem_cache_free(cache
, p
);
326 kmem_cache_destroy(cache
);
329 static char global_array
[10];
331 static noinline
void __init
kasan_global_oob(void)
334 char *p
= &global_array
[ARRAY_SIZE(global_array
) + i
];
336 pr_info("out-of-bounds global variable\n");
340 static noinline
void __init
kasan_stack_oob(void)
342 char stack_array
[10];
344 char *p
= &stack_array
[ARRAY_SIZE(stack_array
) + i
];
346 pr_info("out-of-bounds on stack\n");
350 static noinline
void __init
ksize_unpoisons_memory(void)
353 size_t size
= 123, real_size
= size
;
355 pr_info("ksize() unpoisons the whole allocated chunk\n");
356 ptr
= kmalloc(size
, GFP_KERNEL
);
358 pr_err("Allocation failed\n");
361 real_size
= ksize(ptr
);
362 /* This access doesn't trigger an error. */
365 ptr
[real_size
] = 'y';
369 static noinline
void __init
copy_user_test(void)
372 char __user
*usermem
;
376 kmem
= kmalloc(size
, GFP_KERNEL
);
380 usermem
= (char __user
*)vm_mmap(NULL
, 0, PAGE_SIZE
,
381 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
382 MAP_ANONYMOUS
| MAP_PRIVATE
, 0);
383 if (IS_ERR(usermem
)) {
384 pr_err("Failed to allocate user memory\n");
389 pr_info("out-of-bounds in copy_from_user()\n");
390 unused
= copy_from_user(kmem
, usermem
, size
+ 1);
392 pr_info("out-of-bounds in copy_to_user()\n");
393 unused
= copy_to_user(usermem
, kmem
, size
+ 1);
395 pr_info("out-of-bounds in __copy_from_user()\n");
396 unused
= __copy_from_user(kmem
, usermem
, size
+ 1);
398 pr_info("out-of-bounds in __copy_to_user()\n");
399 unused
= __copy_to_user(usermem
, kmem
, size
+ 1);
401 pr_info("out-of-bounds in __copy_from_user_inatomic()\n");
402 unused
= __copy_from_user_inatomic(kmem
, usermem
, size
+ 1);
404 pr_info("out-of-bounds in __copy_to_user_inatomic()\n");
405 unused
= __copy_to_user_inatomic(usermem
, kmem
, size
+ 1);
407 pr_info("out-of-bounds in strncpy_from_user()\n");
408 unused
= strncpy_from_user(kmem
, usermem
, size
+ 1);
410 vm_munmap((unsigned long)usermem
, PAGE_SIZE
);
414 static int __init
kmalloc_tests_init(void)
418 kmalloc_node_oob_right();
420 kmalloc_pagealloc_oob_right();
422 kmalloc_large_oob_right();
423 kmalloc_oob_krealloc_more();
424 kmalloc_oob_krealloc_less();
426 kmalloc_oob_in_memset();
427 kmalloc_oob_memset_2();
428 kmalloc_oob_memset_4();
429 kmalloc_oob_memset_8();
430 kmalloc_oob_memset_16();
432 kmalloc_uaf_memset();
437 ksize_unpoisons_memory();
442 module_init(kmalloc_tests_init
);
443 MODULE_LICENSE("GPL");