[FEATURE] first working version
[kernel/swap-modules.git] / us_manager / helper.c
1 #include <dbi_kprobes.h>
2 #include <dbi_kprobes_deps.h>
3 #include <ksyms.h>
4 #include "us_proc_inst.h"
5 #include "us_slot_manager.h"
6 #include "storage.h"
7 #include "sspt/sspt.h"
8 #include "helper.h"
9
10 struct task_struct;
11
12 struct task_struct *check_task(struct task_struct *task);
13
14 /*
15  ******************************************************************************
16  *                               do_page_fault()                              *
17  ******************************************************************************
18  */
19
20 struct pf_data {
21         unsigned long addr;
22 };
23
24 static int entry_handler_pf(struct kretprobe_instance *ri, struct pt_regs *regs)
25 {
26         struct pf_data *data = (struct pf_data *)ri->data;
27
28 #if defined(CONFIG_X86)
29         data->addr = read_cr2();
30 #elif defined(CONFIG_ARM)
31         data->addr = regs->ARM_r0;
32 #else
33 #error this architecture is not supported
34 #endif
35
36         return 0;
37 }
38
39 /* Detects when IPs are really loaded into phy mem and installs probes. */
40 static int ret_handler_pf(struct kretprobe_instance *ri, struct pt_regs *regs)
41 {
42         struct task_struct *task;
43         struct sspt_proc *proc;
44
45         install_page(((struct pf_data *)ri->data)->addr);
46         return 0;
47
48         /*
49          * Because process threads have same address space
50          * we instrument only group_leader of all this threads
51          */
52         task = current->group_leader;
53         if (is_kthread(task))
54                 return 0;
55
56         proc = sspt_proc_get_by_task(task);
57         if (proc)
58                 goto install_proc;
59
60         task = check_task(task);
61         if (task) {
62                 proc = sspt_proc_get_new(task);
63                 goto install_proc;
64         }
65
66         return 0;
67
68 install_proc:
69         if (proc->first_install) {
70                 unsigned long page;
71                 page = ((struct pf_data *)ri->data)->addr & PAGE_MASK;
72                 sspt_proc_install_page(proc, page);
73         } else {
74                 sspt_proc_install(proc);
75         }
76
77         return 0;
78 }
79
80 static struct kretprobe pf_kretprobe = {
81         .entry_handler = entry_handler_pf,
82         .handler = ret_handler_pf,
83         .data_size = sizeof(struct pf_data)
84 };
85
86
87
88 /*
89  ******************************************************************************
90  *                              copy_process()                                *
91  ******************************************************************************
92  */
93
94 static void recover_child(struct task_struct *child_task, struct sspt_proc *proc)
95 {
96         sspt_proc_uninstall(proc, child_task, US_DISARM);
97         dbi_disarm_urp_inst_for_task(current, child_task);
98 }
99
100 static void rm_uprobes_child(struct task_struct *task)
101 {
102         struct sspt_proc *proc = sspt_proc_get_by_task(current);
103         if(proc) {
104                 recover_child(task, proc);
105         }
106 }
107
108 /* Delete uprobs in children at fork */
109 static int ret_handler_cp(struct kretprobe_instance *ri, struct pt_regs *regs)
110 {
111         struct task_struct *task = (struct task_struct *)regs_return_value(regs);
112
113         if(!task || IS_ERR(task))
114                 goto out;
115
116         if(task->mm != current->mm) {   /* check flags CLONE_VM */
117                 rm_uprobes_child(task);
118
119                 if (check_task(current)) {
120                         struct sspt_proc *proc;
121
122                         proc = sspt_proc_get_new(task);
123                         sspt_proc_install(proc);
124                 }
125         }
126 out:
127         return 0;
128 }
129
130 static struct kretprobe cp_kretprobe = {
131         .handler = ret_handler_cp,
132 };
133
134
135
136 /*
137  ******************************************************************************
138  *                                mm_release()                                *
139  ******************************************************************************
140  */
141
142 /* Detects when target process removes IPs. */
143 static int mr_pre_handler(struct kprobe *p, struct pt_regs *regs)
144 {
145         struct sspt_proc *proc = NULL;
146         struct task_struct *task;
147
148 #if defined(CONFIG_X86)
149         task = (struct task_struct *)regs->EREG(ax);
150 #elif defined(CONFIG_ARM)
151         task = (struct task_struct *)regs->ARM_r0;
152 #else
153 #error this architecture is not supported
154 #endif
155
156         if (is_kthread(task))
157                 goto out;
158
159         if (task->tgid != task->pid) {
160                 goto out;
161         }
162
163         proc = sspt_proc_get_by_task(task);
164         if (proc) {
165                 int ret = sspt_proc_uninstall(proc, task, US_UNREGS_PROBE);
166                 if (ret != 0) {
167                         printk("failed to uninstall IPs (%d)!\n", ret);
168                 }
169
170                 dbi_unregister_all_uprobes(task);
171         }
172
173 out:
174         return 0;
175 }
176
177 static struct kprobe mr_kprobe = {
178         .pre_handler = mr_pre_handler
179 };
180
181
182
183 /*
184  ******************************************************************************
185  *                                 do_munmap()                                *
186  ******************************************************************************
187  */
188
189 static int remove_unmap_probes(struct task_struct *task, struct sspt_proc *proc, unsigned long start, size_t len)
190 {
191         struct mm_struct *mm = task->mm;
192         struct vm_area_struct *vma;
193
194         if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE - start) {
195                 return -EINVAL;
196         }
197
198         if ((len = PAGE_ALIGN(len)) == 0) {
199                 return -EINVAL;
200         }
201
202         vma = find_vma(mm, start);
203         if (vma && check_vma(vma)) {
204                 struct sspt_file *file;
205                 unsigned long end = start + len;
206                 struct dentry *dentry = vma->vm_file->f_dentry;
207
208                 file = sspt_proc_find_file(proc, dentry);
209                 if (file) {
210                         if (vma->vm_start == start || vma->vm_end == end) {
211                                 sspt_file_uninstall(file, task, US_UNREGS_PROBE);
212                                 file->loaded = 0;
213                         } else {
214                                 unsigned long page_addr;
215                                 struct sspt_page *page;
216
217                                 for (page_addr = vma->vm_start; page_addr < vma->vm_end; page_addr += PAGE_SIZE) {
218                                         page = sspt_find_page_mapped(file, page_addr);
219                                         if (page) {
220                                                 sspt_unregister_page(page, US_UNREGS_PROBE, task);
221                                         }
222                                 }
223
224                                 if (sspt_file_check_install_pages(file)) {
225                                         file->loaded = 0;
226                                 }
227                         }
228                 }
229         }
230
231         return 0;
232 }
233
234 /* Detects when target removes IPs. */
235 static int unmap_pre_handler(struct kprobe *p, struct pt_regs *regs)
236 {
237         /* for ARM */
238         struct mm_struct *mm;
239         unsigned long start;
240         size_t len;
241
242 #if defined(CONFIG_X86)
243         mm = (struct mm_struct *)regs->EREG(ax);
244         start = regs->EREG(dx);
245         len = (size_t)regs->EREG(cx);
246 #elif defined(CONFIG_ARM)
247         mm = (struct mm_struct *)regs->ARM_r0;
248         start = regs->ARM_r1;
249         len = (size_t)regs->ARM_r2;
250 #else
251 #error this architecture is not supported
252 #endif
253
254         struct sspt_proc *proc = NULL;
255         struct task_struct *task = current;
256
257         if (is_kthread(task))
258                 goto out;
259
260         proc = sspt_proc_get_by_task(task);
261         if (proc) {
262                 if (remove_unmap_probes(task, proc, start, len)) {
263                         printk("ERROR do_munmap: start=%lx, len=%x\n", start, len);
264                 }
265         }
266
267 out:
268         return 0;
269 }
270
271 static struct kprobe unmap_kprobe = {
272         .pre_handler = unmap_pre_handler
273 };
274
275
276
277 int register_helper(void)
278 {
279         int ret = 0;
280
281         /* install kprobe on 'do_munmap' to detect when for remove user space probes */
282         ret = dbi_register_kprobe(&unmap_kprobe);
283         if (ret) {
284                 printk("dbi_register_kprobe(do_munmap) result=%d!\n", ret);
285                 return ret;
286         }
287
288         /* install kprobe on 'mm_release' to detect when for remove user space probes */
289         ret = dbi_register_kprobe(&mr_kprobe);
290         if (ret != 0) {
291                 printk("dbi_register_kprobe(mm_release) result=%d!\n", ret);
292                 goto unregister_unmap;
293         }
294
295
296         /* install kretprobe on 'copy_process' */
297         ret = dbi_register_kretprobe(&cp_kretprobe);
298         if (ret) {
299                 printk("dbi_register_kretprobe(copy_process) result=%d!\n", ret);
300                 goto unregister_mr;
301         }
302
303         /* install kretprobe on 'do_page_fault' to detect when they will be loaded */
304         ret = dbi_register_kretprobe(&pf_kretprobe);
305         if (ret) {
306                 printk("dbi_register_kretprobe(do_page_fault) result=%d!\n", ret);
307                 goto unregister_cp;
308         }
309
310         return ret;
311
312 unregister_cp:
313         dbi_unregister_kretprobe(&cp_kretprobe);
314
315 unregister_mr:
316         dbi_unregister_kprobe(&mr_kprobe, NULL);
317
318 unregister_unmap:
319         dbi_unregister_kprobe(&unmap_kprobe, NULL);
320
321         return ret;
322 }
323
324 void unregister_helper(void)
325 {
326         /* uninstall kretprobe with 'do_page_fault' */
327         dbi_unregister_kretprobe(&pf_kretprobe);
328
329         /* uninstall kretprobe with 'copy_process' */
330         dbi_unregister_kretprobe(&cp_kretprobe);
331
332         /* uninstall kprobe with 'mm_release' */
333         dbi_unregister_kprobe(&mr_kprobe, NULL);
334
335         /* uninstall kprobe with 'do_munmap' */
336         dbi_unregister_kprobe(&unmap_kprobe, NULL);
337 }
338
339 int init_helper(void)
340 {
341         unsigned long addr;
342         addr = swap_ksyms("do_page_fault");
343         if (addr == 0) {
344                 printk("Cannot find address for page fault function!\n");
345                 return -EINVAL;
346         }
347         pf_kretprobe.kp.addr = (kprobe_opcode_t *)addr;
348
349         addr = swap_ksyms("copy_process");
350         if (addr == 0) {
351                 printk("Cannot find address for copy_process function!\n");
352                 return -EINVAL;
353         }
354         cp_kretprobe.kp.addr = (kprobe_opcode_t *)addr;
355
356         addr = swap_ksyms("mm_release");
357         if (addr == 0) {
358                 printk("Cannot find address for mm_release function!\n");
359                 return -EINVAL;
360         }
361         mr_kprobe.addr = (kprobe_opcode_t *)addr;
362
363         addr = swap_ksyms("do_munmap");
364         if (addr == 0) {
365                 printk("Cannot find address for do_munmap function!\n");
366                 return -EINVAL;
367         }
368         unmap_kprobe.addr = (kprobe_opcode_t *)addr;
369
370         return 0;
371 }
372
373 void uninit_helper(void)
374 {
375 }