-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbob_over.c
182 lines (140 loc) · 3.78 KB
/
bob_over.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
#include "drv.h"
static int device_open(struct inode *, struct file *);
static long device_ioctl(struct file *, unsigned int, unsigned long);
static int device_release(struct inode *, struct file *f);
static int major_no;
struct vuln_struct {
unsigned long array[10];
};
#define SLUB_MAXLIST 4096
struct vuln_struct * objlist[SLUB_MAXLIST];
static struct file_operations fops = {
.open = device_open,
.release = device_release,
.unlocked_ioctl = device_ioctl
};
static int device_release(struct inode *i, struct file *f) {
printk(KERN_INFO "device_release() called\n");
return 0;
}
static int device_open(struct inode *i, struct file *f) {
return 0;
}
static DEFINE_MUTEX(objlist_mutex);
static long vuln_alloc(unsigned int index) {
if(index >= SLUB_MAXLIST)
return -EFAULT;
if(objlist[index] != NULL)
return -EFAULT;
mutex_lock(&objlist_mutex);
objlist[index] = kmalloc(sizeof(struct vuln_struct), GFP_KERNEL);
mutex_unlock(&objlist_mutex);
return index;
}
static long vuln_free(unsigned int index) {
if(index >= SLUB_MAXLIST)
return -EFAULT;
if(objlist[index] == NULL)
return -EFAULT;
mutex_lock(&objlist_mutex);
kfree(objlist[index]);
objlist[index] = NULL;
mutex_unlock(&objlist_mutex);
return 0;
}
static long vuln_read64(struct vuln_input * arg) {
if(arg->index >= SLUB_MAXLIST)
return -EFAULT;
// over
if(arg->pos >= 20)
return -EFAULT;
if(objlist[arg->index] == NULL)
return -EFAULT;
mutex_lock(&objlist_mutex);
arg->value = objlist[arg->index]->array[arg->pos];
mutex_unlock(&objlist_mutex);
return 0;
}
static long vuln_write64(struct vuln_input * arg) {
if(arg->index >= SLUB_MAXLIST)
return -EFAULT;
// over
if(arg->pos >= 20)
return -EFAULT;
if(objlist[arg->index] == NULL)
return -EFAULT;
mutex_lock(&objlist_mutex);
objlist[arg->index]->array[arg->pos] = arg->value;
mutex_unlock(&objlist_mutex);
return 0;
}
static long vuln_freeall(void) {
unsigned long i;
for(i=0; i<SLUB_MAXLIST; i++)
{
if(objlist[i] != NULL)
{
mutex_lock(&objlist_mutex);
kfree(objlist[i]);
objlist[i] = NULL;
mutex_unlock(&objlist_mutex);
}
}
return 0;
}
static long device_ioctl(struct file *file, unsigned int cmd, unsigned long args) {
struct vuln_input input;
long res;
switch(cmd) {
case IOCTL_ALLOC:
return vuln_alloc((unsigned int)args);
case IOCTL_FREE:
return vuln_free((unsigned int)args);
case IOCTL_FREEALL:
return vuln_freeall();
case IOCTL_READ64:
if (copy_from_user(&input, (struct vuln_input __user *)args, sizeof(input)))
return -EFAULT;
res = vuln_read64(&input);
if(res == 0){
if (copy_to_user((struct vuln_input __user *)args, &input,sizeof(input)))
return -EFAULT;
return 0;
}
return res;
case IOCTL_WRITE64:
if (copy_from_user(&input, (struct vuln_input __user *)args, sizeof(input)))
return -EFAULT;
return vuln_write64(&input);
default:
break;
}
return 0;
}
static struct class *class;
static int __init load(void) {
memset((void *)objlist, 0, sizeof(objlist));
printk(KERN_INFO "Driver loaded\n");
major_no = register_chrdev(0, DEVICE_NAME, &fops);
printk(KERN_INFO "major_no = %d\n", major_no);
class = class_create(THIS_MODULE, DEVICE_NAME);
device_create(class, NULL, MKDEV(major_no, 0), NULL, DEVICE_NAME);
return 0;
}
static void __exit unload(void) {
vuln_freeall();
device_destroy(class, MKDEV(major_no, 0));
class_unregister(class);
class_destroy(class);
unregister_chrdev(major_no, DEVICE_NAME);
printk(KERN_INFO "Driver unloaded\n");
}
module_init(load);
module_exit(unload);
MODULE_LICENSE("GPL");