1 /*
2  *
3  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
4  * Author: Andrey Ryabinin <a.ryabinin@samsung.com>
5  *
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.
9  *
10  */
11 
12 #define pr_fmt(fmt) "kasan test: %s " fmt, __func__
13 
14 #include <linux/delay.h>
15 #include <linux/kernel.h>
16 #include <linux/mman.h>
17 #include <linux/mm.h>
18 #include <linux/printk.h>
19 #include <linux/slab.h>
20 #include <linux/string.h>
21 #include <linux/uaccess.h>
22 #include <linux/module.h>
23 #include <linux/kasan.h>
24 
25 /*
26  * Note: test functions are marked noinline so that their names appear in
27  * reports.
28  */
29 
kmalloc_oob_right(void)30 static noinline void __init kmalloc_oob_right(void)
31 {
32 	char *ptr;
33 	size_t size = 123;
34 
35 	pr_info("out-of-bounds to right\n");
36 	ptr = kmalloc(size, GFP_KERNEL);
37 	if (!ptr) {
38 		pr_err("Allocation failed\n");
39 		return;
40 	}
41 
42 	ptr[size] = 'x';
43 	kfree(ptr);
44 }
45 
kmalloc_oob_left(void)46 static noinline void __init kmalloc_oob_left(void)
47 {
48 	char *ptr;
49 	size_t size = 15;
50 
51 	pr_info("out-of-bounds to left\n");
52 	ptr = kmalloc(size, GFP_KERNEL);
53 	if (!ptr) {
54 		pr_err("Allocation failed\n");
55 		return;
56 	}
57 
58 	*ptr = *(ptr - 1);
59 	kfree(ptr);
60 }
61 
kmalloc_node_oob_right(void)62 static noinline void __init kmalloc_node_oob_right(void)
63 {
64 	char *ptr;
65 	size_t size = 4096;
66 
67 	pr_info("kmalloc_node(): out-of-bounds to right\n");
68 	ptr = kmalloc_node(size, GFP_KERNEL, 0);
69 	if (!ptr) {
70 		pr_err("Allocation failed\n");
71 		return;
72 	}
73 
74 	ptr[size] = 0;
75 	kfree(ptr);
76 }
77 
78 #ifdef CONFIG_SLUB
kmalloc_pagealloc_oob_right(void)79 static noinline void __init kmalloc_pagealloc_oob_right(void)
80 {
81 	char *ptr;
82 	size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
83 
84 	/* Allocate a chunk that does not fit into a SLUB cache to trigger
85 	 * the page allocator fallback.
86 	 */
87 	pr_info("kmalloc pagealloc allocation: out-of-bounds to right\n");
88 	ptr = kmalloc(size, GFP_KERNEL);
89 	if (!ptr) {
90 		pr_err("Allocation failed\n");
91 		return;
92 	}
93 
94 	ptr[size] = 0;
95 	kfree(ptr);
96 }
97 
kmalloc_pagealloc_uaf(void)98 static noinline void __init kmalloc_pagealloc_uaf(void)
99 {
100 	char *ptr;
101 	size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
102 
103 	pr_info("kmalloc pagealloc allocation: use-after-free\n");
104 	ptr = kmalloc(size, GFP_KERNEL);
105 	if (!ptr) {
106 		pr_err("Allocation failed\n");
107 		return;
108 	}
109 
110 	kfree(ptr);
111 	ptr[0] = 0;
112 }
113 
kmalloc_pagealloc_invalid_free(void)114 static noinline void __init kmalloc_pagealloc_invalid_free(void)
115 {
116 	char *ptr;
117 	size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
118 
119 	pr_info("kmalloc pagealloc allocation: invalid-free\n");
120 	ptr = kmalloc(size, GFP_KERNEL);
121 	if (!ptr) {
122 		pr_err("Allocation failed\n");
123 		return;
124 	}
125 
126 	kfree(ptr + 1);
127 }
128 #endif
129 
kmalloc_large_oob_right(void)130 static noinline void __init kmalloc_large_oob_right(void)
131 {
132 	char *ptr;
133 	size_t size = KMALLOC_MAX_CACHE_SIZE - 256;
134 	/* Allocate a chunk that is large enough, but still fits into a slab
135 	 * and does not trigger the page allocator fallback in SLUB.
136 	 */
137 	pr_info("kmalloc large allocation: out-of-bounds to right\n");
138 	ptr = kmalloc(size, GFP_KERNEL);
139 	if (!ptr) {
140 		pr_err("Allocation failed\n");
141 		return;
142 	}
143 
144 	ptr[size] = 0;
145 	kfree(ptr);
146 }
147 
kmalloc_oob_krealloc_more(void)148 static noinline void __init kmalloc_oob_krealloc_more(void)
149 {
150 	char *ptr1, *ptr2;
151 	size_t size1 = 17;
152 	size_t size2 = 19;
153 
154 	pr_info("out-of-bounds after krealloc more\n");
155 	ptr1 = kmalloc(size1, GFP_KERNEL);
156 	ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
157 	if (!ptr1 || !ptr2) {
158 		pr_err("Allocation failed\n");
159 		kfree(ptr1);
160 		kfree(ptr2);
161 		return;
162 	}
163 
164 	ptr2[size2] = 'x';
165 	kfree(ptr2);
166 }
167 
kmalloc_oob_krealloc_less(void)168 static noinline void __init kmalloc_oob_krealloc_less(void)
169 {
170 	char *ptr1, *ptr2;
171 	size_t size1 = 17;
172 	size_t size2 = 15;
173 
174 	pr_info("out-of-bounds after krealloc less\n");
175 	ptr1 = kmalloc(size1, GFP_KERNEL);
176 	ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
177 	if (!ptr1 || !ptr2) {
178 		pr_err("Allocation failed\n");
179 		kfree(ptr1);
180 		return;
181 	}
182 	ptr2[size2] = 'x';
183 	kfree(ptr2);
184 }
185 
kmalloc_oob_16(void)186 static noinline void __init kmalloc_oob_16(void)
187 {
188 	struct {
189 		u64 words[2];
190 	} *ptr1, *ptr2;
191 
192 	pr_info("kmalloc out-of-bounds for 16-bytes access\n");
193 	ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
194 	ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
195 	if (!ptr1 || !ptr2) {
196 		pr_err("Allocation failed\n");
197 		kfree(ptr1);
198 		kfree(ptr2);
199 		return;
200 	}
201 	*ptr1 = *ptr2;
202 	kfree(ptr1);
203 	kfree(ptr2);
204 }
205 
kmalloc_oob_memset_2(void)206 static noinline void __init kmalloc_oob_memset_2(void)
207 {
208 	char *ptr;
209 	size_t size = 8;
210 
211 	pr_info("out-of-bounds in memset2\n");
212 	ptr = kmalloc(size, GFP_KERNEL);
213 	if (!ptr) {
214 		pr_err("Allocation failed\n");
215 		return;
216 	}
217 
218 	memset(ptr+7, 0, 2);
219 	kfree(ptr);
220 }
221 
kmalloc_oob_memset_4(void)222 static noinline void __init kmalloc_oob_memset_4(void)
223 {
224 	char *ptr;
225 	size_t size = 8;
226 
227 	pr_info("out-of-bounds in memset4\n");
228 	ptr = kmalloc(size, GFP_KERNEL);
229 	if (!ptr) {
230 		pr_err("Allocation failed\n");
231 		return;
232 	}
233 
234 	memset(ptr+5, 0, 4);
235 	kfree(ptr);
236 }
237 
238 
kmalloc_oob_memset_8(void)239 static noinline void __init kmalloc_oob_memset_8(void)
240 {
241 	char *ptr;
242 	size_t size = 8;
243 
244 	pr_info("out-of-bounds in memset8\n");
245 	ptr = kmalloc(size, GFP_KERNEL);
246 	if (!ptr) {
247 		pr_err("Allocation failed\n");
248 		return;
249 	}
250 
251 	memset(ptr+1, 0, 8);
252 	kfree(ptr);
253 }
254 
kmalloc_oob_memset_16(void)255 static noinline void __init kmalloc_oob_memset_16(void)
256 {
257 	char *ptr;
258 	size_t size = 16;
259 
260 	pr_info("out-of-bounds in memset16\n");
261 	ptr = kmalloc(size, GFP_KERNEL);
262 	if (!ptr) {
263 		pr_err("Allocation failed\n");
264 		return;
265 	}
266 
267 	memset(ptr+1, 0, 16);
268 	kfree(ptr);
269 }
270 
kmalloc_oob_in_memset(void)271 static noinline void __init kmalloc_oob_in_memset(void)
272 {
273 	char *ptr;
274 	size_t size = 666;
275 
276 	pr_info("out-of-bounds in memset\n");
277 	ptr = kmalloc(size, GFP_KERNEL);
278 	if (!ptr) {
279 		pr_err("Allocation failed\n");
280 		return;
281 	}
282 
283 	memset(ptr, 0, size+5);
284 	kfree(ptr);
285 }
286 
kmalloc_uaf(void)287 static noinline void __init kmalloc_uaf(void)
288 {
289 	char *ptr;
290 	size_t size = 10;
291 
292 	pr_info("use-after-free\n");
293 	ptr = kmalloc(size, GFP_KERNEL);
294 	if (!ptr) {
295 		pr_err("Allocation failed\n");
296 		return;
297 	}
298 
299 	kfree(ptr);
300 	*(ptr + 8) = 'x';
301 }
302 
kmalloc_uaf_memset(void)303 static noinline void __init kmalloc_uaf_memset(void)
304 {
305 	char *ptr;
306 	size_t size = 33;
307 
308 	pr_info("use-after-free in memset\n");
309 	ptr = kmalloc(size, GFP_KERNEL);
310 	if (!ptr) {
311 		pr_err("Allocation failed\n");
312 		return;
313 	}
314 
315 	kfree(ptr);
316 	memset(ptr, 0, size);
317 }
318 
kmalloc_uaf2(void)319 static noinline void __init kmalloc_uaf2(void)
320 {
321 	char *ptr1, *ptr2;
322 	size_t size = 43;
323 
324 	pr_info("use-after-free after another kmalloc\n");
325 	ptr1 = kmalloc(size, GFP_KERNEL);
326 	if (!ptr1) {
327 		pr_err("Allocation failed\n");
328 		return;
329 	}
330 
331 	kfree(ptr1);
332 	ptr2 = kmalloc(size, GFP_KERNEL);
333 	if (!ptr2) {
334 		pr_err("Allocation failed\n");
335 		return;
336 	}
337 
338 	ptr1[40] = 'x';
339 	if (ptr1 == ptr2)
340 		pr_err("Could not detect use-after-free: ptr1 == ptr2\n");
341 	kfree(ptr2);
342 }
343 
kmem_cache_oob(void)344 static noinline void __init kmem_cache_oob(void)
345 {
346 	char *p;
347 	size_t size = 200;
348 	struct kmem_cache *cache = kmem_cache_create("test_cache",
349 						size, 0,
350 						0, NULL);
351 	if (!cache) {
352 		pr_err("Cache allocation failed\n");
353 		return;
354 	}
355 	pr_info("out-of-bounds in kmem_cache_alloc\n");
356 	p = kmem_cache_alloc(cache, GFP_KERNEL);
357 	if (!p) {
358 		pr_err("Allocation failed\n");
359 		kmem_cache_destroy(cache);
360 		return;
361 	}
362 
363 	*p = p[size];
364 	kmem_cache_free(cache, p);
365 	kmem_cache_destroy(cache);
366 }
367 
memcg_accounted_kmem_cache(void)368 static noinline void __init memcg_accounted_kmem_cache(void)
369 {
370 	int i;
371 	char *p;
372 	size_t size = 200;
373 	struct kmem_cache *cache;
374 
375 	cache = kmem_cache_create("test_cache", size, 0, SLAB_ACCOUNT, NULL);
376 	if (!cache) {
377 		pr_err("Cache allocation failed\n");
378 		return;
379 	}
380 
381 	pr_info("allocate memcg accounted object\n");
382 	/*
383 	 * Several allocations with a delay to allow for lazy per memcg kmem
384 	 * cache creation.
385 	 */
386 	for (i = 0; i < 5; i++) {
387 		p = kmem_cache_alloc(cache, GFP_KERNEL);
388 		if (!p)
389 			goto free_cache;
390 
391 		kmem_cache_free(cache, p);
392 		msleep(100);
393 	}
394 
395 free_cache:
396 	kmem_cache_destroy(cache);
397 }
398 
399 static char global_array[10];
400 
kasan_global_oob(void)401 static noinline void __init kasan_global_oob(void)
402 {
403 	volatile int i = 3;
404 	char *p = &global_array[ARRAY_SIZE(global_array) + i];
405 
406 	pr_info("out-of-bounds global variable\n");
407 	*(volatile char *)p;
408 }
409 
kasan_stack_oob(void)410 static noinline void __init kasan_stack_oob(void)
411 {
412 	char stack_array[10];
413 	volatile int i = 0;
414 	char *p = &stack_array[ARRAY_SIZE(stack_array) + i];
415 
416 	pr_info("out-of-bounds on stack\n");
417 	*(volatile char *)p;
418 }
419 
ksize_unpoisons_memory(void)420 static noinline void __init ksize_unpoisons_memory(void)
421 {
422 	char *ptr;
423 	size_t size = 123, real_size;
424 
425 	pr_info("ksize() unpoisons the whole allocated chunk\n");
426 	ptr = kmalloc(size, GFP_KERNEL);
427 	if (!ptr) {
428 		pr_err("Allocation failed\n");
429 		return;
430 	}
431 	real_size = ksize(ptr);
432 	/* This access doesn't trigger an error. */
433 	ptr[size] = 'x';
434 	/* This one does. */
435 	ptr[real_size] = 'y';
436 	kfree(ptr);
437 }
438 
copy_user_test(void)439 static noinline void __init copy_user_test(void)
440 {
441 	char *kmem;
442 	char __user *usermem;
443 	size_t size = 10;
444 	int unused;
445 
446 	kmem = kmalloc(size, GFP_KERNEL);
447 	if (!kmem)
448 		return;
449 
450 	usermem = (char __user *)vm_mmap(NULL, 0, PAGE_SIZE,
451 			    PROT_READ | PROT_WRITE | PROT_EXEC,
452 			    MAP_ANONYMOUS | MAP_PRIVATE, 0);
453 	if (IS_ERR(usermem)) {
454 		pr_err("Failed to allocate user memory\n");
455 		kfree(kmem);
456 		return;
457 	}
458 
459 	pr_info("out-of-bounds in copy_from_user()\n");
460 	unused = copy_from_user(kmem, usermem, size + 1);
461 
462 	pr_info("out-of-bounds in copy_to_user()\n");
463 	unused = copy_to_user(usermem, kmem, size + 1);
464 
465 	pr_info("out-of-bounds in __copy_from_user()\n");
466 	unused = __copy_from_user(kmem, usermem, size + 1);
467 
468 	pr_info("out-of-bounds in __copy_to_user()\n");
469 	unused = __copy_to_user(usermem, kmem, size + 1);
470 
471 	pr_info("out-of-bounds in __copy_from_user_inatomic()\n");
472 	unused = __copy_from_user_inatomic(kmem, usermem, size + 1);
473 
474 	pr_info("out-of-bounds in __copy_to_user_inatomic()\n");
475 	unused = __copy_to_user_inatomic(usermem, kmem, size + 1);
476 
477 	pr_info("out-of-bounds in strncpy_from_user()\n");
478 	unused = strncpy_from_user(kmem, usermem, size + 1);
479 
480 	vm_munmap((unsigned long)usermem, PAGE_SIZE);
481 	kfree(kmem);
482 }
483 
use_after_scope_test(void)484 static noinline void __init use_after_scope_test(void)
485 {
486 	volatile char *volatile p;
487 
488 	pr_info("use-after-scope on int\n");
489 	{
490 		int local = 0;
491 
492 		p = (char *)&local;
493 	}
494 	p[0] = 1;
495 	p[3] = 1;
496 
497 	pr_info("use-after-scope on array\n");
498 	{
499 		char local[1024] = {0};
500 
501 		p = local;
502 	}
503 	p[0] = 1;
504 	p[1023] = 1;
505 }
506 
kasan_alloca_oob_left(void)507 static noinline void __init kasan_alloca_oob_left(void)
508 {
509 	volatile int i = 10;
510 	char alloca_array[i];
511 	char *p = alloca_array - 1;
512 
513 	pr_info("out-of-bounds to left on alloca\n");
514 	*(volatile char *)p;
515 }
516 
kasan_alloca_oob_right(void)517 static noinline void __init kasan_alloca_oob_right(void)
518 {
519 	volatile int i = 10;
520 	char alloca_array[i];
521 	char *p = alloca_array + i;
522 
523 	pr_info("out-of-bounds to right on alloca\n");
524 	*(volatile char *)p;
525 }
526 
kmem_cache_double_free(void)527 static noinline void __init kmem_cache_double_free(void)
528 {
529 	char *p;
530 	size_t size = 200;
531 	struct kmem_cache *cache;
532 
533 	cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
534 	if (!cache) {
535 		pr_err("Cache allocation failed\n");
536 		return;
537 	}
538 	pr_info("double-free on heap object\n");
539 	p = kmem_cache_alloc(cache, GFP_KERNEL);
540 	if (!p) {
541 		pr_err("Allocation failed\n");
542 		kmem_cache_destroy(cache);
543 		return;
544 	}
545 
546 	kmem_cache_free(cache, p);
547 	kmem_cache_free(cache, p);
548 	kmem_cache_destroy(cache);
549 }
550 
kmem_cache_invalid_free(void)551 static noinline void __init kmem_cache_invalid_free(void)
552 {
553 	char *p;
554 	size_t size = 200;
555 	struct kmem_cache *cache;
556 
557 	cache = kmem_cache_create("test_cache", size, 0, SLAB_TYPESAFE_BY_RCU,
558 				  NULL);
559 	if (!cache) {
560 		pr_err("Cache allocation failed\n");
561 		return;
562 	}
563 	pr_info("invalid-free of heap object\n");
564 	p = kmem_cache_alloc(cache, GFP_KERNEL);
565 	if (!p) {
566 		pr_err("Allocation failed\n");
567 		kmem_cache_destroy(cache);
568 		return;
569 	}
570 
571 	/* Trigger invalid free, the object doesn't get freed */
572 	kmem_cache_free(cache, p + 1);
573 
574 	/*
575 	 * Properly free the object to prevent the "Objects remaining in
576 	 * test_cache on __kmem_cache_shutdown" BUG failure.
577 	 */
578 	kmem_cache_free(cache, p);
579 
580 	kmem_cache_destroy(cache);
581 }
582 
kmalloc_tests_init(void)583 static int __init kmalloc_tests_init(void)
584 {
585 	/*
586 	 * Temporarily enable multi-shot mode. Otherwise, we'd only get a
587 	 * report for the first case.
588 	 */
589 	bool multishot = kasan_save_enable_multi_shot();
590 
591 	kmalloc_oob_right();
592 	kmalloc_oob_left();
593 	kmalloc_node_oob_right();
594 #ifdef CONFIG_SLUB
595 	kmalloc_pagealloc_oob_right();
596 	kmalloc_pagealloc_uaf();
597 	kmalloc_pagealloc_invalid_free();
598 #endif
599 	kmalloc_large_oob_right();
600 	kmalloc_oob_krealloc_more();
601 	kmalloc_oob_krealloc_less();
602 	kmalloc_oob_16();
603 	kmalloc_oob_in_memset();
604 	kmalloc_oob_memset_2();
605 	kmalloc_oob_memset_4();
606 	kmalloc_oob_memset_8();
607 	kmalloc_oob_memset_16();
608 	kmalloc_uaf();
609 	kmalloc_uaf_memset();
610 	kmalloc_uaf2();
611 	kmem_cache_oob();
612 	memcg_accounted_kmem_cache();
613 	kasan_stack_oob();
614 	kasan_global_oob();
615 	kasan_alloca_oob_left();
616 	kasan_alloca_oob_right();
617 	ksize_unpoisons_memory();
618 	copy_user_test();
619 	use_after_scope_test();
620 	kmem_cache_double_free();
621 	kmem_cache_invalid_free();
622 
623 	kasan_restore_multi_shot(multishot);
624 
625 	return -EAGAIN;
626 }
627 
628 module_init(kmalloc_tests_init);
629 MODULE_LICENSE("GPL");
630