xref: /openbmc/linux/mm/kasan/kasan_test_module.c (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
1*f7e01ab8SAndrey Konovalov // SPDX-License-Identifier: GPL-2.0-only
2*f7e01ab8SAndrey Konovalov /*
3*f7e01ab8SAndrey Konovalov  *
4*f7e01ab8SAndrey Konovalov  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
5*f7e01ab8SAndrey Konovalov  * Author: Andrey Ryabinin <a.ryabinin@samsung.com>
6*f7e01ab8SAndrey Konovalov  */
7*f7e01ab8SAndrey Konovalov 
8*f7e01ab8SAndrey Konovalov #define pr_fmt(fmt) "kasan test: %s " fmt, __func__
9*f7e01ab8SAndrey Konovalov 
10*f7e01ab8SAndrey Konovalov #include <linux/mman.h>
11*f7e01ab8SAndrey Konovalov #include <linux/module.h>
12*f7e01ab8SAndrey Konovalov #include <linux/printk.h>
13*f7e01ab8SAndrey Konovalov #include <linux/slab.h>
14*f7e01ab8SAndrey Konovalov #include <linux/uaccess.h>
15*f7e01ab8SAndrey Konovalov 
16*f7e01ab8SAndrey Konovalov #include "kasan.h"
17*f7e01ab8SAndrey Konovalov 
copy_user_test(void)18*f7e01ab8SAndrey Konovalov static noinline void __init copy_user_test(void)
19*f7e01ab8SAndrey Konovalov {
20*f7e01ab8SAndrey Konovalov 	char *kmem;
21*f7e01ab8SAndrey Konovalov 	char __user *usermem;
22*f7e01ab8SAndrey Konovalov 	size_t size = 128 - KASAN_GRANULE_SIZE;
23*f7e01ab8SAndrey Konovalov 	int __maybe_unused unused;
24*f7e01ab8SAndrey Konovalov 
25*f7e01ab8SAndrey Konovalov 	kmem = kmalloc(size, GFP_KERNEL);
26*f7e01ab8SAndrey Konovalov 	if (!kmem)
27*f7e01ab8SAndrey Konovalov 		return;
28*f7e01ab8SAndrey Konovalov 
29*f7e01ab8SAndrey Konovalov 	usermem = (char __user *)vm_mmap(NULL, 0, PAGE_SIZE,
30*f7e01ab8SAndrey Konovalov 			    PROT_READ | PROT_WRITE | PROT_EXEC,
31*f7e01ab8SAndrey Konovalov 			    MAP_ANONYMOUS | MAP_PRIVATE, 0);
32*f7e01ab8SAndrey Konovalov 	if (IS_ERR(usermem)) {
33*f7e01ab8SAndrey Konovalov 		pr_err("Failed to allocate user memory\n");
34*f7e01ab8SAndrey Konovalov 		kfree(kmem);
35*f7e01ab8SAndrey Konovalov 		return;
36*f7e01ab8SAndrey Konovalov 	}
37*f7e01ab8SAndrey Konovalov 
38*f7e01ab8SAndrey Konovalov 	OPTIMIZER_HIDE_VAR(size);
39*f7e01ab8SAndrey Konovalov 
40*f7e01ab8SAndrey Konovalov 	pr_info("out-of-bounds in copy_from_user()\n");
41*f7e01ab8SAndrey Konovalov 	unused = copy_from_user(kmem, usermem, size + 1);
42*f7e01ab8SAndrey Konovalov 
43*f7e01ab8SAndrey Konovalov 	pr_info("out-of-bounds in copy_to_user()\n");
44*f7e01ab8SAndrey Konovalov 	unused = copy_to_user(usermem, kmem, size + 1);
45*f7e01ab8SAndrey Konovalov 
46*f7e01ab8SAndrey Konovalov 	pr_info("out-of-bounds in __copy_from_user()\n");
47*f7e01ab8SAndrey Konovalov 	unused = __copy_from_user(kmem, usermem, size + 1);
48*f7e01ab8SAndrey Konovalov 
49*f7e01ab8SAndrey Konovalov 	pr_info("out-of-bounds in __copy_to_user()\n");
50*f7e01ab8SAndrey Konovalov 	unused = __copy_to_user(usermem, kmem, size + 1);
51*f7e01ab8SAndrey Konovalov 
52*f7e01ab8SAndrey Konovalov 	pr_info("out-of-bounds in __copy_from_user_inatomic()\n");
53*f7e01ab8SAndrey Konovalov 	unused = __copy_from_user_inatomic(kmem, usermem, size + 1);
54*f7e01ab8SAndrey Konovalov 
55*f7e01ab8SAndrey Konovalov 	pr_info("out-of-bounds in __copy_to_user_inatomic()\n");
56*f7e01ab8SAndrey Konovalov 	unused = __copy_to_user_inatomic(usermem, kmem, size + 1);
57*f7e01ab8SAndrey Konovalov 
58*f7e01ab8SAndrey Konovalov 	pr_info("out-of-bounds in strncpy_from_user()\n");
59*f7e01ab8SAndrey Konovalov 	unused = strncpy_from_user(kmem, usermem, size + 1);
60*f7e01ab8SAndrey Konovalov 
61*f7e01ab8SAndrey Konovalov 	vm_munmap((unsigned long)usermem, PAGE_SIZE);
62*f7e01ab8SAndrey Konovalov 	kfree(kmem);
63*f7e01ab8SAndrey Konovalov }
64*f7e01ab8SAndrey Konovalov 
test_kasan_module_init(void)65*f7e01ab8SAndrey Konovalov static int __init test_kasan_module_init(void)
66*f7e01ab8SAndrey Konovalov {
67*f7e01ab8SAndrey Konovalov 	/*
68*f7e01ab8SAndrey Konovalov 	 * Temporarily enable multi-shot mode. Otherwise, KASAN would only
69*f7e01ab8SAndrey Konovalov 	 * report the first detected bug and panic the kernel if panic_on_warn
70*f7e01ab8SAndrey Konovalov 	 * is enabled.
71*f7e01ab8SAndrey Konovalov 	 */
72*f7e01ab8SAndrey Konovalov 	bool multishot = kasan_save_enable_multi_shot();
73*f7e01ab8SAndrey Konovalov 
74*f7e01ab8SAndrey Konovalov 	copy_user_test();
75*f7e01ab8SAndrey Konovalov 
76*f7e01ab8SAndrey Konovalov 	kasan_restore_multi_shot(multishot);
77*f7e01ab8SAndrey Konovalov 	return -EAGAIN;
78*f7e01ab8SAndrey Konovalov }
79*f7e01ab8SAndrey Konovalov 
80*f7e01ab8SAndrey Konovalov module_init(test_kasan_module_init);
81*f7e01ab8SAndrey Konovalov MODULE_LICENSE("GPL");
82