-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.c
432 lines (330 loc) · 10.5 KB
/
main.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*
* .ed"""" """$$$$be.
* -" ^""**$$$e.
* ." '$$$c
* / C S A W "4$$b
* d 3 2 0 1 3 $$$$
* $ * .$$$$$$
* .$ ^c $$$$$e$$$$$$$$.
* d$L 4. 4$$$$$$$$$$$$$$b
* $$$$b ^ceeeee. 4$$ECL.F*$$$$$$$
* e$""=. $$$$P d$$$$F $ $$$$$$$$$- $$$$$$
* z$$b. ^c 3$$$F "$$$$b $"$$$$$$$ $$$$*" .=""$c
* 4$$$$L $$P" "$$b .$ $$$$$...e$$ .= e$$$.
* ^*$$$$$c %.. *c .. $$ 3$$$$$$$$$$eF zP d$$$$$
* "**$$$ec " %ce"" $$$ $$$$$$$$$$* .r" =$$$$P""
* "*$b. "c *$e. *** d$$$$$"L$$ .d" e$$***"
* ^*$$c ^$c $$$ 4J$$$$$% $$$ .e*".eeP"
* "$$$$$$"'$=e....$*$$**$cz$$" "..d$*"
* "*$$$ *=%4.$ L L$ P3$$$F $$$P"
* "$ "%*ebJLzb$e$$$$$b $P"
* %.. 4$$$$$$$$$$ "
* $$$e z$$$$$$$$$$%
* "*$c "$$$$$$$P"
* ."""*$$$$$$$$bc
* .-" .$***$$$"""*e.
* .-" .e$" "*$c ^*b.
* .=*"""" .e$*" "*bc "*$e..
* .$" .z*" ^*$e. "*****e.
* $$ee$c .d" "*$. 3.
* ^*$E")$..$" * .ee==d%
* $.d$$$* * J$$$e*
* """"" "$$$" Gilo95'
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/miscdevice.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/random.h>
#include <linux/list.h>
#include <linux/sched.h>
#include <asm/uaccess.h>
#define DRIVER_VERSION "CSAW SUCKiT v1.3.37"
#define CSAW_IOCTL_BASE 0x77617363
#define CSAW_ALLOC_HANDLE CSAW_IOCTL_BASE+1
#define CSAW_READ_HANDLE CSAW_IOCTL_BASE+2
#define CSAW_WRITE_HANDLE CSAW_IOCTL_BASE+3
#define CSAW_GET_CONSUMER CSAW_IOCTL_BASE+4
#define CSAW_SET_CONSUMER CSAW_IOCTL_BASE+5
#define CSAW_FREE_HANDLE CSAW_IOCTL_BASE+6
#define CSAW_GET_STATS CSAW_IOCTL_BASE+7
#define MAX_CONSUMERS 255
struct csaw_buf {
unsigned long consumers[MAX_CONSUMERS];
char *buf;
unsigned long size;
unsigned long seed;
struct list_head list;
};
LIST_HEAD(csaw_bufs);
struct alloc_args {
unsigned long size;
unsigned long handle;
};
struct free_args {
unsigned long handle;
};
struct read_args {
unsigned long handle;
unsigned long size;
void *out;
};
struct write_args {
unsigned long handle;
unsigned long size;
void *in;
};
struct consumer_args {
unsigned long handle;
unsigned long pid;
unsigned char offset;
};
struct csaw_stats {
unsigned long clients;
unsigned long handles;
unsigned long bytes_read;
unsigned long bytes_written;
char version[40];
};
unsigned long clients = 0;
unsigned long handles = 0;
unsigned long bytes_read = 0;
unsigned long bytes_written = 0;
static int csaw_open ( struct inode *inode, struct file *file )
{
clients++;
return 0;
}
static int csaw_release ( struct inode *inode, struct file *file )
{
clients--;
return 0;
}
int alloc_buf ( struct alloc_args *alloc_args )
{
struct csaw_buf *cbuf;
char *buf;
unsigned long size, seed, handle;
size = alloc_args->size;
if ( ! size )
return -EINVAL;
cbuf = kmalloc(sizeof(*cbuf), GFP_KERNEL);
if ( ! cbuf )
return -ENOMEM;
buf = kzalloc(size, GFP_KERNEL);
if ( ! buf )
{
kfree(cbuf);
return -ENOMEM;
}
cbuf->buf = buf;
cbuf->size = size;
memset(&cbuf->consumers, 0, sizeof(cbuf->consumers));
cbuf->consumers[0] = current->pid;
get_random_bytes(&seed, sizeof(seed));
cbuf->seed = seed;
handle = (unsigned long)buf ^ seed;
list_add(&cbuf->list, &csaw_bufs);
alloc_args->handle = handle;
return 0;
}
void free_buf ( struct csaw_buf *cbuf )
{
list_del(&cbuf->list);
kfree(cbuf->buf);
kfree(cbuf);
}
struct csaw_buf *find_cbuf ( unsigned long handle )
{
struct csaw_buf *cbuf;
list_for_each_entry ( cbuf, &csaw_bufs, list )
if ( handle == ((unsigned long)cbuf->buf ^ cbuf->seed) )
return cbuf;
return NULL;
}
static long csaw_ioctl ( struct file *file, unsigned int cmd, unsigned long arg )
{
int ret = 0;
unsigned long *argp = (unsigned long *)arg;
switch ( cmd )
{
case CSAW_ALLOC_HANDLE:
{
int ret;
struct alloc_args alloc_args;
if ( copy_from_user(&alloc_args, argp, sizeof(alloc_args)) )
return -EFAULT;
if ( (ret = alloc_buf(&alloc_args)) < 0 )
return ret;
if ( copy_to_user(argp, &alloc_args, sizeof(alloc_args)) )
return -EFAULT;
handles++;
break;
}
case CSAW_READ_HANDLE:
{
struct read_args read_args;
struct csaw_buf *cbuf;
unsigned int i, authorized = 0;
unsigned long to_read;
if ( copy_from_user(&read_args, argp, sizeof(read_args)) )
return -EFAULT;
cbuf = find_cbuf(read_args.handle);
if ( ! cbuf )
return -EINVAL;
for ( i = 0; i < MAX_CONSUMERS; i++ )
if ( current->pid == cbuf->consumers[i] )
authorized = 1;
if ( ! authorized )
return -EPERM;
to_read = min(read_args.size, cbuf->size);
if ( copy_to_user(read_args.out, cbuf->buf, to_read) )
return -EFAULT;
bytes_read += to_read;
break;
}
case CSAW_WRITE_HANDLE:
{
struct write_args write_args;
struct csaw_buf *cbuf;
unsigned int i, authorized = 0;
unsigned long to_write;
if ( copy_from_user(&write_args, argp, sizeof(write_args)) )
return -EFAULT;
cbuf = find_cbuf(write_args.handle);
if ( ! cbuf )
return -EINVAL;
for ( i = 0; i < MAX_CONSUMERS; i++ )
if ( current->pid == cbuf->consumers[i] )
authorized = 1;
if ( ! authorized )
return -EPERM;
to_write = min(write_args.size, cbuf->size);
if ( copy_from_user(cbuf->buf, write_args.in, to_write) )
return -EFAULT;
bytes_written += to_write;
break;
}
case CSAW_GET_CONSUMER:
{
struct consumer_args consumer_args;
struct csaw_buf *cbuf;
unsigned int i, authorized = 0;
if ( copy_from_user(&consumer_args, argp, sizeof(consumer_args)) )
return -EFAULT;
cbuf = find_cbuf(consumer_args.handle);
if ( ! cbuf )
return -EINVAL;
for ( i = 0; i < MAX_CONSUMERS; i++ )
if ( current->pid == cbuf->consumers[i] )
authorized = 1;
if ( ! authorized )
return -EPERM;
consumer_args.pid = cbuf->consumers[consumer_args.offset];
if ( copy_to_user(argp, &consumer_args, sizeof(consumer_args)) )
return -EFAULT;
break;
}
case CSAW_SET_CONSUMER:
{
struct consumer_args consumer_args;
struct csaw_buf *cbuf;
unsigned int i, authorized = 0;
if ( copy_from_user(&consumer_args, argp, sizeof(consumer_args)) )
return -EFAULT;
cbuf = find_cbuf(consumer_args.handle);
if ( ! cbuf )
return -EINVAL;
for ( i = 0; i < MAX_CONSUMERS; i++ )
if ( current->pid == cbuf->consumers[i] )
authorized = 1;
if ( ! authorized )
return -EPERM;
cbuf->consumers[consumer_args.offset] = consumer_args.pid;
break;
}
case CSAW_FREE_HANDLE:
{
struct free_args free_args;
struct csaw_buf *cbuf;
unsigned int i, authorized = 0;
if ( copy_from_user(&free_args, argp, sizeof(free_args)) )
return -EFAULT;
cbuf = find_cbuf(free_args.handle);
if ( ! cbuf )
return -EINVAL;
for ( i = 0; i < MAX_CONSUMERS; i++ )
if ( current->pid == cbuf->consumers[i] )
authorized = 1;
if ( ! authorized )
return -EPERM;
free_buf(cbuf);
handles--;
break;
}
case CSAW_GET_STATS:
{
struct csaw_stats csaw_stats;
csaw_stats.clients = clients;
csaw_stats.handles = handles;
csaw_stats.bytes_read = bytes_read;
csaw_stats.bytes_written = bytes_written;
strcpy(csaw_stats.version, DRIVER_VERSION);
if ( copy_to_user(argp, &csaw_stats, sizeof(csaw_stats)) )
return -EFAULT;
break;
}
default:
ret = -EINVAL;
break;
}
return ret;
}
static ssize_t csaw_read ( struct file *file, char *buf, size_t count, loff_t *pos )
{
char *stats;
unsigned int to_read;
unsigned int ret;
stats = kmalloc(1024, GFP_KERNEL);
if ( ! buf )
return -ENOMEM;
ret = snprintf(stats, 1024, "Active clients: %lu\nHandles allocated: %lu\nBytes read: %lu\nBytes written: %lu\n",
clients, handles, bytes_read, bytes_written);
if ( count < ret )
to_read = count;
else
to_read = ret;
if ( copy_to_user(buf, stats, to_read) )
{
kfree(stats);
return -EFAULT;
}
kfree(stats);
return 0;
}
static const struct file_operations csaw_fops = {
owner: THIS_MODULE,
open: csaw_open,
release: csaw_release,
unlocked_ioctl: csaw_ioctl,
read: csaw_read,
};
static struct miscdevice csaw_miscdev = {
name: "csaw",
fops: &csaw_fops
};
static int __init lezzdoit ( void )
{
misc_register(&csaw_miscdev);
return 0;
}
static void __exit wereouttahurr ( void )
{
misc_deregister(&csaw_miscdev);
}
module_init(lezzdoit);
module_exit(wereouttahurr);
MODULE_LICENSE("GPL");