1 #include <dbi_kprobes.h>
2 #include <dbi_kprobes_deps.h>
4 #include "us_proc_inst.h"
5 #include "us_slot_manager.h"
8 #include "filters/filters_core.h"
11 ******************************************************************************
13 ******************************************************************************
20 static int entry_handler_pf(struct kretprobe_instance *ri, struct pt_regs *regs)
22 struct pf_data *data = (struct pf_data *)ri->data;
24 #if defined(CONFIG_X86)
25 data->addr = read_cr2();
26 #elif defined(CONFIG_ARM)
27 data->addr = regs->ARM_r0;
29 #error this architecture is not supported
35 /* Detects when IPs are really loaded into phy mem and installs probes. */
36 static int ret_handler_pf(struct kretprobe_instance *ri, struct pt_regs *regs)
38 struct task_struct *task;
39 struct sspt_proc *proc;
42 * Because process threads have same address space
43 * we instrument only group_leader of all this threads
45 task = current->group_leader;
46 if (task->flags & PF_KTHREAD)
49 if (!is_us_instrumentation())
52 proc = sspt_proc_get_by_task(task);
56 task = check_task(task);
58 proc = sspt_proc_get_new(task);
65 if (proc->first_install) {
67 page = ((struct pf_data *)ri->data)->addr & PAGE_MASK;
68 sspt_proc_install_page(proc, page);
70 sspt_proc_install(proc);
76 static struct kretprobe pf_kretprobe = {
77 .entry_handler = entry_handler_pf,
78 .handler = ret_handler_pf,
79 .data_size = sizeof(struct pf_data)
85 ******************************************************************************
87 ******************************************************************************
90 static void recover_child(struct task_struct *child_task, struct sspt_proc *proc)
92 sspt_proc_uninstall(proc, child_task, US_DISARM);
93 dbi_disarm_urp_inst_for_task(current, child_task);
96 static void rm_uprobes_child(struct task_struct *task)
98 struct sspt_proc *proc = sspt_proc_get_by_task(current);
100 recover_child(task, proc);
104 /* Delete uprobs in children at fork */
105 static int ret_handler_cp(struct kretprobe_instance *ri, struct pt_regs *regs)
107 struct task_struct *task = (struct task_struct *)regs_return_value(regs);
109 if(!task || IS_ERR(task))
112 if(task->mm != current->mm) { /* check flags CLONE_VM */
113 rm_uprobes_child(task);
115 if (check_task(current)) {
116 struct sspt_proc *proc;
118 proc = sspt_proc_get_new(task);
119 sspt_proc_install(proc);
126 static struct kretprobe cp_kretprobe = {
127 .handler = ret_handler_cp,
133 ******************************************************************************
135 ******************************************************************************
138 /* Detects when target process removes IPs. */
139 static int mr_pre_handler(struct kprobe *p, struct pt_regs *regs)
141 struct sspt_proc *proc = NULL;
142 struct task_struct *task;
144 #if defined(CONFIG_X86)
145 task = (struct task_struct *)regs->EREG(ax);
146 #elif defined(CONFIG_ARM)
147 task = (struct task_struct *)regs->ARM_r0;
149 #error this architecture is not supported
152 if (!is_us_instrumentation() || task->tgid != task->pid) {
156 proc = sspt_proc_get_by_task(task);
158 int ret = sspt_proc_uninstall(proc, task, US_UNREGS_PROBE);
160 printk("failed to uninstall IPs (%d)!\n", ret);
163 dbi_unregister_all_uprobes(task);
170 static struct kprobe mr_kprobe = {
171 .pre_handler = mr_pre_handler
177 ******************************************************************************
179 ******************************************************************************
182 static int remove_unmap_probes(struct task_struct *task, struct sspt_proc *proc, unsigned long start, size_t len)
184 struct mm_struct *mm = task->mm;
185 struct vm_area_struct *vma;
187 if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE - start) {
191 if ((len = PAGE_ALIGN(len)) == 0) {
195 vma = find_vma(mm, start);
196 if (vma && check_vma(vma)) {
197 struct sspt_file *file;
198 unsigned long end = start + len;
199 struct dentry *dentry = vma->vm_file->f_dentry;
201 file = sspt_proc_find_file(proc, dentry);
203 if (vma->vm_start == start || vma->vm_end == end) {
204 sspt_file_uninstall(file, task, US_UNREGS_PROBE);
207 unsigned long page_addr;
208 struct sspt_page *page;
210 for (page_addr = vma->vm_start; page_addr < vma->vm_end; page_addr += PAGE_SIZE) {
211 page = sspt_find_page_mapped(file, page_addr);
213 sspt_unregister_page(page, US_UNREGS_PROBE, task);
217 if (sspt_file_check_install_pages(file)) {
227 /* Detects when target removes IPs. */
228 static int unmap_pre_handler(struct kprobe *p, struct pt_regs *regs)
231 struct mm_struct *mm;
235 #if defined(CONFIG_X86)
236 mm = (struct mm_struct *)regs->EREG(ax);
237 start = regs->EREG(dx);
238 len = (size_t)regs->EREG(cx);
239 #elif defined(CONFIG_ARM)
240 mm = (struct mm_struct *)regs->ARM_r0;
241 start = regs->ARM_r1;
242 len = (size_t)regs->ARM_r2;
244 #error this architecture is not supported
247 struct sspt_proc *proc = NULL;
248 struct task_struct *task = current;
250 //if user-space instrumentation is not set
251 if (!is_us_instrumentation()) {
255 proc = sspt_proc_get_by_task(task);
257 if (remove_unmap_probes(task, proc, start, len)) {
258 printk("ERROR do_munmap: start=%lx, len=%x\n", start, len);
266 static struct kprobe unmap_kprobe = {
267 .pre_handler = unmap_pre_handler
272 int register_helper(void)
276 /* install kprobe on 'do_munmap' to detect when for remove user space probes */
277 ret = dbi_register_kprobe(&unmap_kprobe);
279 printk("dbi_register_kprobe(do_munmap) result=%d!\n", ret);
283 /* install kprobe on 'mm_release' to detect when for remove user space probes */
284 ret = dbi_register_kprobe(&mr_kprobe);
286 printk("dbi_register_kprobe(mm_release) result=%d!\n", ret);
287 goto unregister_unmap;
291 /* install kretprobe on 'copy_process' */
292 ret = dbi_register_kretprobe(&cp_kretprobe);
294 printk("dbi_register_kretprobe(copy_process) result=%d!\n", ret);
298 /* install kretprobe on 'do_page_fault' to detect when they will be loaded */
299 ret = dbi_register_kretprobe(&pf_kretprobe);
301 printk("dbi_register_kretprobe(do_page_fault) result=%d!\n", ret);
308 dbi_unregister_kretprobe(&cp_kretprobe);
311 dbi_unregister_kprobe(&mr_kprobe, NULL);
314 dbi_unregister_kprobe(&unmap_kprobe, NULL);
319 void unregister_helper(void)
321 /* uninstall kretprobe with 'do_page_fault' */
322 dbi_unregister_kretprobe(&pf_kretprobe);
324 /* uninstall kretprobe with 'copy_process' */
325 dbi_unregister_kretprobe(&cp_kretprobe);
327 /* uninstall kprobe with 'mm_release' */
328 dbi_unregister_kprobe(&mr_kprobe, NULL);
330 /* uninstall kprobe with 'do_munmap' */
331 dbi_unregister_kprobe(&unmap_kprobe, NULL);
334 int init_helper(void)
337 addr = swap_ksyms("do_page_fault");
339 printk("Cannot find address for page fault function!\n");
342 pf_kretprobe.kp.addr = (kprobe_opcode_t *)addr;
344 addr = swap_ksyms("copy_process");
346 printk("Cannot find address for copy_process function!\n");
349 cp_kretprobe.kp.addr = (kprobe_opcode_t *)addr;
351 addr = swap_ksyms("mm_release");
353 printk("Cannot find address for mm_release function!\n");
356 mr_kprobe.addr = (kprobe_opcode_t *)addr;
358 addr = swap_ksyms("do_munmap");
360 printk("Cannot find address for do_munmap function!\n");
363 unmap_kprobe.addr = (kprobe_opcode_t *)addr;
368 void uninit_helper(void)