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
540
541
542
543
544
545
546
547
548
| #include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/printk.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include <linux/thread_info.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/uaccess.h>
#include <linux/spinlock.h>
#include <linux/debugfs.h>
#include <linux/seq_file.h>
#define CLASS_NAME "processmemtest"
#define DEV_FNAME "PROCESSMEMTESTMOD"
#define DEV_COUNT 1
/*
* Objective:
* 1) Given a process number traverse the mm_struct list
* and dump the pgd address.
*
* Bonus
* -----
* 2) Create a debugfs file and dump the neccessary debug information to it.
* 3) Extract the pgd for each task.
*/
/*
* Design:
* -------
* Create an attribute file and use it to write the process
* number to it. Use it to traverse the mm_struct list and
* display the pgd address either in the attribute file or
* in dmesg.
*/
/*
*
*/
static ssize_t process_mem_test_read(
struct file *file,
char __user *buff,
size_t count,
loff_t *offset
)
{
printk(KERN_ALERT "Process mem test read\n");
return 0; //Make the user space process exit read.
}
static ssize_t process_mem_test_write(
struct file *file,
const char __user *buff,
size_t count,
loff_t *offset)
{
printk(KERN_ALERT "Process mem test write\n");
return count;
}
static int process_mem_test_open(
struct inode *inode,
struct file *file
)
{
printk(KERN_ALERT "Process mem test open\n");
return 0;
}
static int process_mem_test_release(
struct inode *inode,
struct file *file
)
{
printk(KERN_ALERT "Process mem test release\n");
return 0;
}
struct file_operations process_mem_test_fops = {
.owner = THIS_MODULE,
.read = process_mem_test_read,
.write = process_mem_test_write,
.open = process_mem_test_open,
.release = process_mem_test_release,
};
dev_t process_mem_test_dev_no;
struct process_mem_test_device {
struct cdev cdev;
} process_mem_test_dev;
struct class *process_mem_test_class;
struct device *process_mem_test_dev_info;
ssize_t process_mem_test_attr_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct task_struct *current_task;
struct vm_area_struct *vmarea_struct_addr;
current_task = current;
if(current_task != NULL) {
printk(KERN_ALERT "Current task : %d",
current_task->pid);
/*printk(KERN_ALERT "Current task : %d, On CPU %d\n",
current_task->pid,current_task->on_cpu);*/
printk(KERN_ALERT "mm: 0x%lx, active_mm 0x%lx\n",
(unsigned long)current_task->mm,
(unsigned long)current_task->active_mm);
printk(KERN_ALERT "Page global directory : 0x%lx\n",
(unsigned long)current_task->mm->pgd);
printk(KERN_ALERT "mmap base : 0x%lx",
(unsigned long)current_task->mm->mmap_base);
vmarea_struct_addr = current_task->mm->mmap;
while(vmarea_struct_addr != NULL) {
printk(KERN_ALERT "vm_start : 0x%lx, vm_end : 0x%lx\n",
(unsigned long)vmarea_struct_addr->vm_start,
(unsigned long)vmarea_struct_addr->vm_end);
vmarea_struct_addr = vmarea_struct_addr->vm_next;
}
} else {
printk(KERN_ALERT "Current task NULL\n");
}
return sprintf(buf,"attrib show\n");
}
void my_follow_page(struct mm_struct *mm,
unsigned long addr_res)
{
pgd_t *pgd;
pmd_t *pmd;
pud_t *pud;
pte_t *ptep, pte;
unsigned long pfn;
struct page *page;
// char byte_val = 0;
down_read(&(mm->mmap_sem));
pgd = pgd_offset(mm,addr_res);
/* copy_from_user(&byte_val,addr_res,1);
printk(KERN_ALERT "virt addr: 0x%lx, byte val : 0x%lx\n",
addr_res,
byte_val);*/
if(pgd_none(*pgd) || pgd_bad(*pgd)) {
printk(KERN_ALERT "pgd bad\n");
return;
} else {
printk(KERN_ALERT "pgd 0x%lx\n",(unsigned long)pgd);
}
pud = pud_offset(pgd,addr_res);
if(pud_none(*pud) || pud_bad(*pud)) {
printk(KERN_ALERT "pud bad\n");
return;
} else {
printk(KERN_ALERT "pud 0x%lx\n",(unsigned long)pud);
}
pmd = pmd_offset(pud,addr_res);
if(pmd_none(*pmd) || pmd_bad(*pmd)) {
printk(KERN_ALERT "pmd bad\n");
return;
} else {
printk(KERN_ALERT "pmd 0x%lx\n",(unsigned long)pmd);
}
ptep = pte_offset_map(pmd,addr_res);
if(!ptep) {
printk(KERN_ALERT "ptep bad\n");
} else {
printk(KERN_ALERT "ptep 0x%lx\n",(unsigned long)ptep);
}
pte = *ptep;
if(pte_present(pte)) {
printk(KERN_ALERT "pte : 0x%lx\n",(unsigned long)pte);
page = pte_page(pte);
} else {
printk(KERN_ALERT "pte not present\n");
}
printk(KERN_ALERT "pte with offset 0x%lx offset : 0x%lx\n",
pte+((addr_res) & ((1<<PAGE_SHIFT)-1)),
addr_res & ((1<<PAGE_SHIFT)-1));
printk(KERN_ALERT "pfn from pte : 0x%lx\n",pfn = pte_pfn(pte));
printk(KERN_ALERT "pfn to addr : 0x%lx, addr_res : 0x%lx\n",(pfn<<PAGE_SHIFT),
(unsigned long)PAGE_MASK);
printk(KERN_ALERT "phys_addr : 0x%lx\n",(pfn<<PAGE_SHIFT) + (addr_res & ~PAGE_MASK));
up_read(&(mm->mmap_sem));
}
//#define LOCK_TEST
#undef LOCK_TEST
#define MAP_REGION 0x100000
ssize_t process_mem_test_attr_store(struct device *dev,
struct device_attribute *attr,
const char *buf,
size_t count)
{
struct task_struct *current_task;
unsigned long addr_res;
struct vm_area_struct *vmarea_struct_addr;
unsigned int i = 0;
#ifdef LOCK_TEST
DEFINE_SPINLOCK(test_lock);
spin_lock_irq(&test_lock);
#endif
current_task = current;
if(current_task != NULL) {
printk(KERN_ALERT "\nCurrent task pid: %d\n",
current_task->pid);
printk(KERN_ALERT "mm: 0x%lx, active_mm 0x%lx\n",
(unsigned long)current_task->mm,
(unsigned long)current_task->active_mm);
printk(KERN_ALERT "Page global directory : 0x%lx\n",
(unsigned long)current_task->mm->pgd);
printk(KERN_ALERT "mmap base : 0x%lx",
(unsigned long)current_task->mm->mmap_base);
vmarea_struct_addr = current_task->mm->mmap;
while(vmarea_struct_addr != NULL) {
printk(KERN_ALERT "vm_start : 0x%lx, vm_end : 0x%lx\n",
(unsigned long)vmarea_struct_addr->vm_start,
(unsigned long)vmarea_struct_addr->vm_end);
vmarea_struct_addr = vmarea_struct_addr->vm_next;
}
} else {
printk(KERN_ALERT "Current task NULL\n");
}
if(kstrtol(buf,10,&addr_res) != 0) {
printk(KERN_ALERT "Error converting to long\n");
return count;
}
// copy_from_user(&kval,(unsigned int *)addr_res,4);
// printk(KERN_ALERT "kval : %x\n",kval);
//addr_res = MAP_REGION;
printk(KERN_ALERT "addr: 0x%lx\n",addr_res);
my_follow_page(current_task->mm,
addr_res+i);
#ifdef LOCK_TEST
while(1)
;
printk(KERN_ALERT "After lock\n");
#endif
return count;
}
#define TEST_SIZE 4LL
int test_arr[TEST_SIZE] = {5,4,3,2};
/*
* Explanation for the below code
*
* Initially "start" is called. Do all init's,holding locks in this
* function. After this "show" is called. This is right after "start"
* and is the first iteration. After this "next" is called where
* we increment the iterator. After this "show" is called. If in
* "next" we reach the end we return NULL. This triggers the call to
* "stop". A call will be done to start again where *pos is checked
* and return NULL which exits the sequence.
*
* General Sequence:
* -----------------
* Start -> Show -> Next -> Show -> Next ->.. Next-> Stop-> Start-> Stop.
*/
unsigned char STOP_FLAG = 0;
void * mem_dbg_start(struct seq_file *m, loff_t *pos)
{
struct vm_area_struct *vmarea_struct_addr;
printk(KERN_ALERT "[GAUN] In %s\n",__FUNCTION__);
if(STOP_FLAG) {
STOP_FLAG = 0;
return NULL;
}
if(current == NULL)
return NULL;
vmarea_struct_addr = current->mm->mmap;
if(vmarea_struct_addr == NULL)
return NULL;
seq_printf(m,"Current task : %d\n",
current->pid);
seq_printf(m,"mm: 0x%lx, active_mm 0x%lx\n",
(unsigned long)current->mm,
(unsigned long)current->active_mm);
seq_printf(m,"Page global directory : 0x%lx\n",
(unsigned long)current->mm->pgd);
seq_printf(m,"mmap base : 0x%lx\n",
(unsigned long)current->mm->mmap_base);
return vmarea_struct_addr;
/*printk(KERN_ALERT "In %s pos : %lld\n",__FUNCTION__,*pos);
if((*pos) == TEST_SIZE)
return NULL;
return test_arr;*/
}
void * mem_dbg_next(struct seq_file *m, void *v, loff_t *pos)
{
struct vm_area_struct *vmarea_struct_addr;
vmarea_struct_addr = (struct vm_area_struct *)v;
vmarea_struct_addr = vmarea_struct_addr->vm_next;
if(vmarea_struct_addr == NULL) {
STOP_FLAG = 1;
return NULL;
}
return vmarea_struct_addr;
/* (*pos)++;
if((*pos) == TEST_SIZE) {
printk(KERN_ALERT "[GAUN] Match\n");
return NULL;
}
printk(KERN_ALERT "In %s pos : %lld\n",__FUNCTION__,*pos);
return test_arr+(*pos);*/
}
int mem_dbg_show(struct seq_file *m, void *v)
{
struct vm_area_struct *vmarea_struct_addr;
vmarea_struct_addr = (struct vm_area_struct *) v;
printk(KERN_ALERT "[GAUN] In %s\n",__FUNCTION__);
if(vmarea_struct_addr != NULL)
seq_printf(m,"vm_start : 0x%lx, vm_end : 0x%lx\n",
(unsigned long)vmarea_struct_addr->vm_start,
(unsigned long)vmarea_struct_addr->vm_end);
/* if(v != NULL)
seq_printf(m,"%x ",*((int *)v));*/
return 0;
}
void mem_dbg_stop(struct seq_file *m, void *v)
{
printk(KERN_ALERT "[GAUN] In %s\n",__FUNCTION__);
}
static const struct seq_operations seq_dbg_ops = {
.start = mem_dbg_start,
.next = mem_dbg_next,
.stop = mem_dbg_stop,
.show = mem_dbg_show,
};
int process_mem_dbg_open(struct inode *inode,
struct file *file)
{
return seq_open(file,&seq_dbg_ops);
}
static const struct file_operations process_mem_dbg_fops = {
.open = process_mem_dbg_open,
.read = seq_read,
.llseek = seq_lseek,
.release = seq_release,
};
DEVICE_ATTR(
process_mem_test,
S_IRWXU|S_IRWXG|S_IRWXO,
process_mem_test_attr_show,
process_mem_test_attr_store
);
struct dentry *process_mem_dir;
struct dentry *process_mem_dbgfile;
static int __init process_mem_test_init(void)
{
int retval = 0;
printk(KERN_ALERT "Process mem test init\n");
retval = alloc_chrdev_region(
&process_mem_test_dev_no,
0,
DEV_COUNT,
DEV_FNAME
);
if(retval == 0) {
printk(KERN_ALERT "Allocated char region with Major number : %d, Minor number : %d\n",
MAJOR(process_mem_test_dev_no),
MINOR(process_mem_test_dev_no));
} else {
printk(KERN_ALERT "Could not allocate char region\n");
return retval;
}
cdev_init(&(process_mem_test_dev.cdev),
&process_mem_test_fops);
process_mem_test_dev.cdev.owner = THIS_MODULE;
retval = cdev_add(&process_mem_test_dev.cdev,
process_mem_test_dev_no,DEV_COUNT);
if(retval != 0) {
printk(KERN_ALERT "Could not add character device\n");
return retval;
}
process_mem_test_class = class_create(THIS_MODULE,CLASS_NAME);
process_mem_test_dev_info = device_create(
process_mem_test_class,
NULL,
process_mem_test_dev_no,
NULL,
"process_mem_test_dev_0"
);
retval = device_create_file(
process_mem_test_dev_info,
&dev_attr_process_mem_test
);
if(retval != 0) {
printk(KERN_ALERT "Could not create device file\n");
return retval;
}
process_mem_dir = debugfs_create_dir(
"process_mem_dbg",
NULL);
if(process_mem_dir == NULL) {
printk(KERN_ALERT
"Could not create directory in debugfs\n");
} else {
process_mem_dbgfile = debugfs_create_file(
"process_mem_test",
0600,
process_mem_dir,
NULL,
&process_mem_dbg_fops);
}
return 0;
}
static void __exit process_mem_test_exit(void)
{
printk(KERN_ALERT "Process mem test exit\n");
unregister_chrdev_region(process_mem_test_dev_no,
DEV_COUNT);
device_destroy(process_mem_test_class,
process_mem_test_dev_no);
class_destroy(process_mem_test_class);
cdev_del(&process_mem_test_dev.cdev);
debugfs_remove(process_mem_dbgfile);
debugfs_remove(process_mem_dir);
}
module_init(process_mem_test_init);
module_exit(process_mem_test_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("TEST");
MODULE_DESCRIPTION("Process mem test");
|