1
2
3
4
5
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
18static noinline void __init copy_user_test(void)
19{
20 char *kmem;
21 char __user *usermem;
22 size_t size = 128 - KASAN_GRANULE_SIZE;
23 int __maybe_unused unused;
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");
39 unused = copy_from_user(kmem, usermem, size + 1);
40
41 pr_info("out-of-bounds in copy_to_user()\n");
42 unused = copy_to_user(usermem, kmem, size + 1);
43
44 pr_info("out-of-bounds in __copy_from_user()\n");
45 unused = __copy_from_user(kmem, usermem, size + 1);
46
47 pr_info("out-of-bounds in __copy_to_user()\n");
48 unused = __copy_to_user(usermem, kmem, size + 1);
49
50 pr_info("out-of-bounds in __copy_from_user_inatomic()\n");
51 unused = __copy_from_user_inatomic(kmem, usermem, size + 1);
52
53 pr_info("out-of-bounds in __copy_to_user_inatomic()\n");
54 unused = __copy_to_user_inatomic(usermem, kmem, size + 1);
55
56 pr_info("out-of-bounds in strncpy_from_user()\n");
57 unused = strncpy_from_user(kmem, usermem, size + 1);
58
59 vm_munmap((unsigned long)usermem, PAGE_SIZE);
60 kfree(kmem);
61}
62
63static struct kasan_rcu_info {
64 int i;
65 struct rcu_head rcu;
66} *global_rcu_ptr;
67
68static 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);
74 ((volatile struct kasan_rcu_info *)fp)->i;
75}
76
77static 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
92static noinline void __init kasan_workqueue_work(struct work_struct *work)
93{
94 kfree(work);
95}
96
97static 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}
120
121static int __init test_kasan_module_init(void)
122{
123
124
125
126
127
128 bool multishot = kasan_save_enable_multi_shot();
129
130 copy_user_test();
131 kasan_rcu_uaf();
132 kasan_workqueue_uaf();
133
134 kasan_restore_multi_shot(multishot);
135 return -EAGAIN;
136}
137
138module_init(test_kasan_module_init);
139MODULE_LICENSE("GPL");
140