]>
Commit | Line | Data |
---|---|---|
73228c7e PA |
1 | // SPDX-License-Identifier: GPL-2.0-only |
2 | /* | |
3 | * | |
4 | * Copyright (c) 2014 Samsung Electronics Co., Ltd. | |
5 | * Author: Andrey Ryabinin <a.ryabinin@samsung.com> | |
6 | */ | |
7 | ||
8 | #define pr_fmt(fmt) "kasan test: %s " fmt, __func__ | |
9 | ||
10 | #include <linux/mman.h> | |
11 | #include <linux/module.h> | |
12 | #include <linux/printk.h> | |
13 | #include <linux/slab.h> | |
14 | #include <linux/uaccess.h> | |
15 | ||
16 | #include "../mm/kasan/kasan.h" | |
17 | ||
73228c7e PA |
18 | static noinline void __init copy_user_test(void) |
19 | { | |
20 | char *kmem; | |
21 | char __user *usermem; | |
756e5a47 | 22 | size_t size = 128 - KASAN_GRANULE_SIZE; |
e1566567 | 23 | int __maybe_unused unused; |
73228c7e PA |
24 | |
25 | kmem = kmalloc(size, GFP_KERNEL); | |
26 | if (!kmem) | |
27 | return; | |
28 | ||
29 | usermem = (char __user *)vm_mmap(NULL, 0, PAGE_SIZE, | |
30 | PROT_READ | PROT_WRITE | PROT_EXEC, | |
31 | MAP_ANONYMOUS | MAP_PRIVATE, 0); | |
32 | if (IS_ERR(usermem)) { | |
33 | pr_err("Failed to allocate user memory\n"); | |
34 | kfree(kmem); | |
35 | return; | |
36 | } | |
37 | ||
38 | pr_info("out-of-bounds in copy_from_user()\n"); | |
756e5a47 | 39 | unused = copy_from_user(kmem, usermem, size + 1); |
73228c7e PA |
40 | |
41 | pr_info("out-of-bounds in copy_to_user()\n"); | |
756e5a47 | 42 | unused = copy_to_user(usermem, kmem, size + 1); |
73228c7e PA |
43 | |
44 | pr_info("out-of-bounds in __copy_from_user()\n"); | |
756e5a47 | 45 | unused = __copy_from_user(kmem, usermem, size + 1); |
73228c7e PA |
46 | |
47 | pr_info("out-of-bounds in __copy_to_user()\n"); | |
756e5a47 | 48 | unused = __copy_to_user(usermem, kmem, size + 1); |
73228c7e PA |
49 | |
50 | pr_info("out-of-bounds in __copy_from_user_inatomic()\n"); | |
756e5a47 | 51 | unused = __copy_from_user_inatomic(kmem, usermem, size + 1); |
73228c7e PA |
52 | |
53 | pr_info("out-of-bounds in __copy_to_user_inatomic()\n"); | |
756e5a47 | 54 | unused = __copy_to_user_inatomic(usermem, kmem, size + 1); |
73228c7e PA |
55 | |
56 | pr_info("out-of-bounds in strncpy_from_user()\n"); | |
756e5a47 | 57 | unused = strncpy_from_user(kmem, usermem, size + 1); |
73228c7e PA |
58 | |
59 | vm_munmap((unsigned long)usermem, PAGE_SIZE); | |
60 | kfree(kmem); | |
61 | } | |
62 | ||
63 | static struct kasan_rcu_info { | |
64 | int i; | |
65 | struct rcu_head rcu; | |
66 | } *global_rcu_ptr; | |
67 | ||
68 | static noinline void __init kasan_rcu_reclaim(struct rcu_head *rp) | |
69 | { | |
70 | struct kasan_rcu_info *fp = container_of(rp, | |
71 | struct kasan_rcu_info, rcu); | |
72 | ||
73 | kfree(fp); | |
f16de0bc | 74 | ((volatile struct kasan_rcu_info *)fp)->i; |
73228c7e PA |
75 | } |
76 | ||
77 | static noinline void __init kasan_rcu_uaf(void) | |
78 | { | |
79 | struct kasan_rcu_info *ptr; | |
80 | ||
81 | pr_info("use-after-free in kasan_rcu_reclaim\n"); | |
82 | ptr = kmalloc(sizeof(struct kasan_rcu_info), GFP_KERNEL); | |
83 | if (!ptr) { | |
84 | pr_err("Allocation failed\n"); | |
85 | return; | |
86 | } | |
87 | ||
88 | global_rcu_ptr = rcu_dereference_protected(ptr, NULL); | |
89 | call_rcu(&global_rcu_ptr->rcu, kasan_rcu_reclaim); | |
90 | } | |
91 | ||
214c783d WW |
92 | static noinline void __init kasan_workqueue_work(struct work_struct *work) |
93 | { | |
94 | kfree(work); | |
95 | } | |
96 | ||
97 | static noinline void __init kasan_workqueue_uaf(void) | |
98 | { | |
99 | struct workqueue_struct *workqueue; | |
100 | struct work_struct *work; | |
101 | ||
102 | workqueue = create_workqueue("kasan_wq_test"); | |
103 | if (!workqueue) { | |
104 | pr_err("Allocation failed\n"); | |
105 | return; | |
106 | } | |
107 | work = kmalloc(sizeof(struct work_struct), GFP_KERNEL); | |
108 | if (!work) { | |
109 | pr_err("Allocation failed\n"); | |
110 | return; | |
111 | } | |
112 | ||
113 | INIT_WORK(work, kasan_workqueue_work); | |
114 | queue_work(workqueue, work); | |
115 | destroy_workqueue(workqueue); | |
116 | ||
117 | pr_info("use-after-free on workqueue\n"); | |
118 | ((volatile struct work_struct *)work)->data; | |
119 | } | |
73228c7e PA |
120 | |
121 | static int __init test_kasan_module_init(void) | |
122 | { | |
123 | /* | |
0fd37925 AK |
124 | * Temporarily enable multi-shot mode. Otherwise, KASAN would only |
125 | * report the first detected bug and panic the kernel if panic_on_warn | |
126 | * is enabled. | |
73228c7e PA |
127 | */ |
128 | bool multishot = kasan_save_enable_multi_shot(); | |
129 | ||
130 | copy_user_test(); | |
131 | kasan_rcu_uaf(); | |
214c783d | 132 | kasan_workqueue_uaf(); |
73228c7e PA |
133 | |
134 | kasan_restore_multi_shot(multishot); | |
135 | return -EAGAIN; | |
136 | } | |
137 | ||
138 | module_init(test_kasan_module_init); | |
139 | MODULE_LICENSE("GPL"); |