forked from abcz316/rwProcMem33
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsys.c
More file actions
540 lines (391 loc) · 11.2 KB
/
sys.c
File metadata and controls
540 lines (391 loc) · 11.2 KB
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
#include <linux/module.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/mm.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#include <linux/uaccess.h>
#include <linux/kernel.h>
#include <linux/version.h>
#include <linux/slab.h>
#include <linux/device.h>
#include "phy_mem.h"
#include "remote_proc_maps.h"
#include "remote_proc_cmdline.h"
#include "remote_proc_rss.h"
#include "version_control.h"
#define MAJOR_NUM 100
#define IOCTL_OPEN_PROCESS _IOR(MAJOR_NUM, 1, char*)
#define IOCTL_CLOSE_HANDLE _IOR(MAJOR_NUM, 2, char*)
#define IOCTL_GET_PROCESS_MAPS_COUNT _IOR(MAJOR_NUM, 3, char*)
#define IOCTL_GET_PROCESS_MAPS_LIST _IOR(MAJOR_NUM, 4, char*)
#define IOCTL_CHECK_PROCESS_ADDR_PHY _IOR(MAJOR_NUM, 5, char*)
#define IOCTL_GET_PROCESS_CMDLINE_ADDR _IOR(MAJOR_NUM, 6, char*)
#define IOCTL_GET_PROCESS_RSS _IOR(MAJOR_NUM, 7, char*)
static int g_rwProcMem_major = 0;
static dev_t g_rwProcMem_devno;
struct rwProcMemDev
{
struct cdev cdev;
ssize_t mm_struct_arg_start_offset;
};
struct rwProcMemDev *g_rwProcMem_devp;
struct class *g_Class_devp;
int rwProcMem_open(struct inode *inode, struct file *filp)
{
filp->private_data = g_rwProcMem_devp;
return 0;
}
static ssize_t rwProcMem_read(struct file* filp, char __user* buf, size_t size, loff_t* ppos)
{
char data[16];
memset(data, 0, sizeof(data));
if (copy_from_user(data, buf, 16))
{
return -EFAULT;
}
struct pid * proc_pid_struct = NULL;
memcpy(&proc_pid_struct, data, sizeof(proc_pid_struct));
printk_debug(KERN_INFO "read proc_pid_struct *:%ld\n", proc_pid_struct);
size_t proc_virt_addr = 0;
memcpy(&proc_virt_addr, (void*)((size_t)data + (size_t)8), sizeof(proc_virt_addr));
printk_debug(KERN_INFO "proc_virt_addr :0x%p\n", proc_virt_addr);
if (!check_proc_map_can_read(proc_pid_struct, proc_virt_addr, size))
{
return -EFAULT;
}
if (clear_user(buf, size))
{
return -EFAULT;
}
size_t read_size = 0;
while (read_size < size)
{
size_t phy_addr = get_proc_phy_addr(proc_pid_struct, proc_virt_addr + read_size);
printk_debug(KERN_INFO "phy_addr:0x%p\n", phy_addr);
if (phy_addr == 0)
{
break;
}
size_t pfn_sz = size_inside_page(phy_addr, ((size - read_size) > PAGE_SIZE) ? PAGE_SIZE : (size - read_size));
printk_debug(KERN_INFO "pfn_sz:%ld\n", pfn_sz);
read_ram_physical_addr_to_user(phy_addr, buf + read_size, pfn_sz);
read_size += pfn_sz;
}
return read_size;
}
static ssize_t rwProcMem_write(struct file* filp, const char __user* buf, size_t size, loff_t *ppos)
{
char data[16];
memset(data, 0, sizeof(data));
if (copy_from_user(data, buf, 16))
{
return -EFAULT;
}
struct pid * proc_pid_struct = NULL;
memcpy(&proc_pid_struct, data, sizeof(proc_pid_struct));
printk_debug(KERN_INFO "read proc_pid_struct *:%ld\n", proc_pid_struct);
size_t proc_virt_addr = 0;
memcpy(&proc_virt_addr, (void*)((size_t)data + (size_t)8), sizeof(proc_virt_addr));
printk_debug(KERN_INFO "proc_virt_addr :0x%p\n", proc_virt_addr);
if (!check_proc_map_can_write(proc_pid_struct, proc_virt_addr, size))
{
return -EFAULT;
}
size_t write_size = 0;
while (write_size < size)
{
size_t phy_addr = get_proc_phy_addr(proc_pid_struct, proc_virt_addr + write_size);
printk_debug(KERN_INFO "phy_addr:0x%p\n", phy_addr);
if (phy_addr == 0)
{
break;
}
size_t pfn_sz = size_inside_page(phy_addr, ((size - write_size) > PAGE_SIZE) ? PAGE_SIZE : (size - write_size));
printk_debug(KERN_INFO "pfn_sz:%ld\n", pfn_sz);
write_ram_physical_addr_from_user(phy_addr, (void*)((size_t)buf + (size_t)16 + write_size), pfn_sz);
write_size += pfn_sz;
}
return write_size;
}
static loff_t rwProcMem_llseek(struct file* filp, loff_t offset, int orig)
{
loff_t ret = 0;
switch (orig)
{
case SEEK_SET:
if (offset < 0)
{
ret = -EINVAL;
break;
}
filp->f_pos = offset;
ret = filp->f_pos;
break;
case SEEK_CUR:
if ((filp->f_pos + offset) < 0)
{
ret = -EINVAL;
break;
}
filp->f_pos += offset;
ret = filp->f_pos;
break;
default:
ret = -EINVAL;
break;
}
return ret;
}
static long rwProcMem_ioctl(
struct file *filp,
unsigned int cmd,
unsigned long arg)
{
struct inode *inode = inode = filp->f_inode;
struct rwProcMemDev* devp = filp->private_data;
switch (cmd)
{
case IOCTL_OPEN_PROCESS:
{
printk_debug(KERN_INFO "IOCTL_OPEN_PROCESS\n");
char buf[8];
memset(&buf, 0, sizeof(buf));
if (copy_from_user(buf, arg, 8))
{
return -EINVAL;
}
int64_t pid = 0;
memcpy(&pid, buf, sizeof(pid));
printk_debug(KERN_INFO "pid:%ld\n", pid);
struct pid * proc_pid_struct = get_proc_pid_struct(pid);
printk_debug(KERN_INFO "proc_pid_struct *:%ld\n", proc_pid_struct);
if (!proc_pid_struct)
{
return -EINVAL;
}
memset(&buf, 0, sizeof(buf));
memcpy(&buf, &proc_pid_struct, sizeof(proc_pid_struct));
if (copy_to_user(arg, buf, 8))
{
return -EINVAL;
}
return 0;
break;
}
case IOCTL_CLOSE_HANDLE:
{
printk_debug(KERN_INFO "IOCTL_CLOSE_HANDLE\n");
char buf[8];
memset(&buf, 0, sizeof(buf));
if (copy_from_user(buf, arg, 8))
{
return -EFAULT;
}
struct pid * proc_pid_struct = NULL;
memcpy(&proc_pid_struct, buf, sizeof(proc_pid_struct));
printk_debug(KERN_INFO "proc_pid_struct *:%ld\n", proc_pid_struct);
release_proc_pid_struct(proc_pid_struct);
return 0;
break;
}
case IOCTL_GET_PROCESS_MAPS_COUNT:
{
printk_debug(KERN_INFO "IOCTL_GET_PROCESS_MAPS_COUNT\n");
char buf[8];
memset(&buf, 0, 8);
if (copy_from_user(buf, arg, 8))
{
return -EINVAL;
}
struct pid * proc_pid_struct = NULL;
memcpy(&proc_pid_struct, buf, sizeof(proc_pid_struct));
printk_debug(KERN_INFO "proc_pid_struct *:%ld\n", proc_pid_struct);
return get_proc_map_count(proc_pid_struct);
break;
}
case IOCTL_GET_PROCESS_MAPS_LIST:
{
printk_debug(KERN_INFO "IOCTL_GET_PROCESS_MAPS_LIST\n");
char buf[24];
memset(&buf, 0, 24);
if (copy_from_user(buf, arg, 24))
{
return -EINVAL;
}
struct pid * proc_pid_struct = NULL;
memcpy(&proc_pid_struct, buf, sizeof(proc_pid_struct));
printk_debug(KERN_INFO "proc_pid_struct *:%ld\n", proc_pid_struct);
size_t name_len = 0;
memcpy(&name_len, (void*)((size_t)buf + (size_t)8), sizeof(name_len));
printk_debug(KERN_INFO "name_len:%ld\n", name_len);
size_t buf_size = 0;
memcpy(&buf_size, (void*)((size_t)buf + (size_t)16), sizeof(buf_size));
printk_debug(KERN_INFO "buf_size:%ld\n", buf_size);
int have_pass;
int count = get_proc_maps_list_to_user(proc_pid_struct, name_len, (void*)((size_t)arg + (size_t)1), buf_size-1, &have_pass);
unsigned char ch = have_pass == 1 ? '\x01' : '\x00';
if (copy_to_user(arg, &ch, 1))
{
return -EFAULT;
}
return count;
break;
}
case IOCTL_CHECK_PROCESS_ADDR_PHY:
{
printk_debug(KERN_INFO "IOCTL_CHECK_PROC_ADDR_PHY\n");
char buf[16];
memset(&buf, 0, sizeof(buf));
if (copy_from_user(buf, arg, 16))
{
return -EFAULT;
}
struct pid * proc_pid_struct = NULL;
memcpy(&proc_pid_struct, buf, sizeof(proc_pid_struct));
printk_debug(KERN_INFO "proc_pid_struct *:%ld\n", proc_pid_struct);
size_t proc_virt_addr = 0;
memcpy(&proc_virt_addr, (void*)((size_t)buf + (size_t)8), sizeof(proc_virt_addr));
printk_debug(KERN_INFO "proc_virt_addr :0x%p\n", proc_virt_addr);
if (get_proc_phy_addr(proc_pid_struct, proc_virt_addr))
{
return 1;
}
return 0;
break;
}
case IOCTL_GET_PROCESS_CMDLINE_ADDR:
{
printk_debug(KERN_INFO "IOCTL_GET_PROCESS_CMDLINE_ADDR\n");
char buf[16];
memset(&buf, 0, 16);
if (copy_from_user(buf, arg, 16))
{
return -EINVAL;
}
struct pid * proc_pid_struct = NULL;
memcpy(&proc_pid_struct, buf, sizeof(proc_pid_struct));
printk_debug(KERN_INFO "proc_pid_struct *:%ld\n", proc_pid_struct);
ssize_t relative_offset = 0;
memcpy(&relative_offset, (void*)((size_t)buf + (size_t)8), sizeof(relative_offset));
printk_debug(KERN_INFO "relative_offset:%ld\n", relative_offset);
if (relative_offset!=-1)
{
devp->mm_struct_arg_start_offset = relative_offset;
}
memset(&buf, 0, 16);
size_t arg_start = 0, arg_end = 0;
int res = get_proc_cmdline_addr(proc_pid_struct, devp->mm_struct_arg_start_offset, &arg_start, &arg_end);
memcpy((void*)buf, &arg_start, 8);
memcpy((void*)((size_t)buf + (size_t)8), &arg_end, 8);
if (copy_to_user(arg, &buf, 16))
{
return -EINVAL;
}
return res;
break;
}
case IOCTL_GET_PROCESS_RSS:
{
printk_debug(KERN_INFO "IOCTL_GET_PROCESS_RSS\n");
char buf[8];
memset(&buf, 0, 8);
if (copy_from_user(buf, arg, 8))
{
return -EINVAL;
}
struct pid * proc_pid_struct = NULL;
memcpy(&proc_pid_struct, buf, sizeof(proc_pid_struct));
printk_debug(KERN_INFO "proc_pid_struct *:%ld\n", proc_pid_struct);
memset(&buf, 0, 8);
size_t rss = read_proc_rss_size(proc_pid_struct, devp->mm_struct_arg_start_offset);
memcpy((void*)buf, &rss, 8);
if (copy_to_user(arg, &buf, 8))
{
return -EINVAL;
}
return 0;
break;
}
default:
return -EINVAL;
}
return 0;
}
int rwProcMem_release(struct inode *inode, struct file *filp)
{
return 0;
}
static const struct file_operations rwProcMem_fops =
{
.owner = THIS_MODULE,
.open = rwProcMem_open,
.release = rwProcMem_release,
.read = rwProcMem_read,
.write = rwProcMem_write,
.llseek = rwProcMem_llseek,
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
.ioctl = rwProcMem_ioctl,
#else
.compat_ioctl = rwProcMem_ioctl,
.unlocked_ioctl = rwProcMem_ioctl,
#endif
};
static int rwProcMem_dev_init(void)
{
int result;
result = alloc_chrdev_region(&g_rwProcMem_devno, 0, 1, DEV_FILENAME);
g_rwProcMem_major = MAJOR(g_rwProcMem_devno);
if (result < 0)
{
printk(KERN_EMERG "rwProcMem alloc_chrdev_region failed %d\n", result);
return result;
}
g_rwProcMem_devp = kmalloc(sizeof(struct rwProcMemDev), GFP_KERNEL);
if (!g_rwProcMem_devp)
{
result = -ENOMEM;
goto _fail;
}
memset(g_rwProcMem_devp, 0, sizeof(struct rwProcMemDev));
int err;
cdev_init(&g_rwProcMem_devp->cdev, &rwProcMem_fops);
g_rwProcMem_devp->cdev.owner = THIS_MODULE;
g_rwProcMem_devp->cdev.ops = &rwProcMem_fops;
err = cdev_add(&g_rwProcMem_devp->cdev, g_rwProcMem_devno, 1);
if (err)
{
printk(KERN_NOTICE "Error in cdev_add()\n");
result = -EFAULT;
goto _fail;
}
g_Class_devp = class_create(THIS_MODULE, DEV_FILENAME);
device_create(g_Class_devp, NULL, g_rwProcMem_devno, NULL, "%s", DEV_FILENAME);
#ifdef DEBUG_PRINTK
printk(KERN_EMERG "Hello, rwProcMem %d debug\n", SYS_VERSION);
#else
printk(KERN_EMERG "Hello, rwProcMem %d\n", SYS_VERSION);
#endif
return 0;
_fail:
unregister_chrdev_region(g_rwProcMem_devno, 1);
return result;
}
static void rwProcMem_dev_exit(void)
{
device_destroy(g_Class_devp, g_rwProcMem_devno);
class_destroy(g_Class_devp);
cdev_del(&g_rwProcMem_devp->cdev);
kfree(g_rwProcMem_devp);
unregister_chrdev_region(g_rwProcMem_devno, 1);
printk(KERN_EMERG "Goodbye, rwProcMem %d\n", SYS_VERSION);
}
module_init(rwProcMem_dev_init);
module_exit(rwProcMem_dev_exit);
MODULE_AUTHOR("abcz316");
MODULE_DESCRIPTION("Linux read & write process memory module. Coded by abcz316 2020-03-20");
MODULE_LICENSE("GPL");