regulator: axp20x: Fix axp22x ldo_io registration error on cold boot
[linux-2.6/btrfs-unstable.git] / lib / test_kasan.c
blob82169fbf24538fdc70ff98fce1f324cb13f241da
1 /*
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)
22 char *ptr;
23 size_t size = 123;
25 pr_info("out-of-bounds to right\n");
26 ptr = kmalloc(size, GFP_KERNEL);
27 if (!ptr) {
28 pr_err("Allocation failed\n");
29 return;
32 ptr[size] = 'x';
33 kfree(ptr);
36 static noinline void __init kmalloc_oob_left(void)
38 char *ptr;
39 size_t size = 15;
41 pr_info("out-of-bounds to left\n");
42 ptr = kmalloc(size, GFP_KERNEL);
43 if (!ptr) {
44 pr_err("Allocation failed\n");
45 return;
48 *ptr = *(ptr - 1);
49 kfree(ptr);
52 static noinline void __init kmalloc_node_oob_right(void)
54 char *ptr;
55 size_t size = 4096;
57 pr_info("kmalloc_node(): out-of-bounds to right\n");
58 ptr = kmalloc_node(size, GFP_KERNEL, 0);
59 if (!ptr) {
60 pr_err("Allocation failed\n");
61 return;
64 ptr[size] = 0;
65 kfree(ptr);
68 #ifdef CONFIG_SLUB
69 static noinline void __init kmalloc_pagealloc_oob_right(void)
71 char *ptr;
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);
79 if (!ptr) {
80 pr_err("Allocation failed\n");
81 return;
84 ptr[size] = 0;
85 kfree(ptr);
87 #endif
89 static noinline void __init kmalloc_large_oob_right(void)
91 char *ptr;
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);
98 if (!ptr) {
99 pr_err("Allocation failed\n");
100 return;
103 ptr[size] = 0;
104 kfree(ptr);
107 static noinline void __init kmalloc_oob_krealloc_more(void)
109 char *ptr1, *ptr2;
110 size_t size1 = 17;
111 size_t size2 = 19;
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");
118 kfree(ptr1);
119 return;
122 ptr2[size2] = 'x';
123 kfree(ptr2);
126 static noinline void __init kmalloc_oob_krealloc_less(void)
128 char *ptr1, *ptr2;
129 size_t size1 = 17;
130 size_t size2 = 15;
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");
137 kfree(ptr1);
138 return;
140 ptr2[size2] = 'x';
141 kfree(ptr2);
144 static noinline void __init kmalloc_oob_16(void)
146 struct {
147 u64 words[2];
148 } *ptr1, *ptr2;
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");
155 kfree(ptr1);
156 kfree(ptr2);
157 return;
159 *ptr1 = *ptr2;
160 kfree(ptr1);
161 kfree(ptr2);
164 static noinline void __init kmalloc_oob_memset_2(void)
166 char *ptr;
167 size_t size = 8;
169 pr_info("out-of-bounds in memset2\n");
170 ptr = kmalloc(size, GFP_KERNEL);
171 if (!ptr) {
172 pr_err("Allocation failed\n");
173 return;
176 memset(ptr+7, 0, 2);
177 kfree(ptr);
180 static noinline void __init kmalloc_oob_memset_4(void)
182 char *ptr;
183 size_t size = 8;
185 pr_info("out-of-bounds in memset4\n");
186 ptr = kmalloc(size, GFP_KERNEL);
187 if (!ptr) {
188 pr_err("Allocation failed\n");
189 return;
192 memset(ptr+5, 0, 4);
193 kfree(ptr);
197 static noinline void __init kmalloc_oob_memset_8(void)
199 char *ptr;
200 size_t size = 8;
202 pr_info("out-of-bounds in memset8\n");
203 ptr = kmalloc(size, GFP_KERNEL);
204 if (!ptr) {
205 pr_err("Allocation failed\n");
206 return;
209 memset(ptr+1, 0, 8);
210 kfree(ptr);
213 static noinline void __init kmalloc_oob_memset_16(void)
215 char *ptr;
216 size_t size = 16;
218 pr_info("out-of-bounds in memset16\n");
219 ptr = kmalloc(size, GFP_KERNEL);
220 if (!ptr) {
221 pr_err("Allocation failed\n");
222 return;
225 memset(ptr+1, 0, 16);
226 kfree(ptr);
229 static noinline void __init kmalloc_oob_in_memset(void)
231 char *ptr;
232 size_t size = 666;
234 pr_info("out-of-bounds in memset\n");
235 ptr = kmalloc(size, GFP_KERNEL);
236 if (!ptr) {
237 pr_err("Allocation failed\n");
238 return;
241 memset(ptr, 0, size+5);
242 kfree(ptr);
245 static noinline void __init kmalloc_uaf(void)
247 char *ptr;
248 size_t size = 10;
250 pr_info("use-after-free\n");
251 ptr = kmalloc(size, GFP_KERNEL);
252 if (!ptr) {
253 pr_err("Allocation failed\n");
254 return;
257 kfree(ptr);
258 *(ptr + 8) = 'x';
261 static noinline void __init kmalloc_uaf_memset(void)
263 char *ptr;
264 size_t size = 33;
266 pr_info("use-after-free in memset\n");
267 ptr = kmalloc(size, GFP_KERNEL);
268 if (!ptr) {
269 pr_err("Allocation failed\n");
270 return;
273 kfree(ptr);
274 memset(ptr, 0, size);
277 static noinline void __init kmalloc_uaf2(void)
279 char *ptr1, *ptr2;
280 size_t size = 43;
282 pr_info("use-after-free after another kmalloc\n");
283 ptr1 = kmalloc(size, GFP_KERNEL);
284 if (!ptr1) {
285 pr_err("Allocation failed\n");
286 return;
289 kfree(ptr1);
290 ptr2 = kmalloc(size, GFP_KERNEL);
291 if (!ptr2) {
292 pr_err("Allocation failed\n");
293 return;
296 ptr1[40] = 'x';
297 if (ptr1 == ptr2)
298 pr_err("Could not detect use-after-free: ptr1 == ptr2\n");
299 kfree(ptr2);
302 static noinline void __init kmem_cache_oob(void)
304 char *p;
305 size_t size = 200;
306 struct kmem_cache *cache = kmem_cache_create("test_cache",
307 size, 0,
308 0, NULL);
309 if (!cache) {
310 pr_err("Cache allocation failed\n");
311 return;
313 pr_info("out-of-bounds in kmem_cache_alloc\n");
314 p = kmem_cache_alloc(cache, GFP_KERNEL);
315 if (!p) {
316 pr_err("Allocation failed\n");
317 kmem_cache_destroy(cache);
318 return;
321 *p = p[size];
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)
330 volatile int i = 3;
331 char *p = &global_array[ARRAY_SIZE(global_array) + i];
333 pr_info("out-of-bounds global variable\n");
334 *(volatile char *)p;
337 static noinline void __init kasan_stack_oob(void)
339 char stack_array[10];
340 volatile int i = 0;
341 char *p = &stack_array[ARRAY_SIZE(stack_array) + i];
343 pr_info("out-of-bounds on stack\n");
344 *(volatile char *)p;
347 static int __init kmalloc_tests_init(void)
349 kmalloc_oob_right();
350 kmalloc_oob_left();
351 kmalloc_node_oob_right();
352 #ifdef CONFIG_SLUB
353 kmalloc_pagealloc_oob_right();
354 #endif
355 kmalloc_large_oob_right();
356 kmalloc_oob_krealloc_more();
357 kmalloc_oob_krealloc_less();
358 kmalloc_oob_16();
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();
364 kmalloc_uaf();
365 kmalloc_uaf_memset();
366 kmalloc_uaf2();
367 kmem_cache_oob();
368 kasan_stack_oob();
369 kasan_global_oob();
370 return -EAGAIN;
373 module_init(kmalloc_tests_init);
374 MODULE_LICENSE("GPL");