1 #include <dbi_kprobes.h>
2 #include <dbi_kprobes_deps.h>
4 #include "us_proc_inst.h"
5 #include "us_slot_manager.h"
10 ******************************************************************************
12 ******************************************************************************
19 static int entry_handler_pf(struct kretprobe_instance *ri, struct pt_regs *regs)
21 struct pf_data *data = (struct pf_data *)ri->data;
24 data->addr = read_cr2();
26 data->addr = regs->ARM_r0;
28 #error this architecture is not supported
34 /* Detects when IPs are really loaded into phy mem and installs probes. */
35 static int ret_handler_pf(struct kretprobe_instance *ri, struct pt_regs *regs)
37 struct task_struct *task = current->group_leader;
38 struct mm_struct *mm = task->mm;
39 struct sspt_procs *procs = NULL;
41 * Because process threads have same address space
42 * we instrument only group_leader of all this threads
45 unsigned long addr = 0;
48 if (task->flags & PF_KTHREAD) {
52 if (!is_us_instrumentation()) {
56 data = (struct pf_data *)ri->data;
59 valid_addr = mm && page_present(mm, addr);
65 procs = sspt_procs_get_by_task_or_new(task);
68 if (us_proc_info.tgid == 0) {
69 pid_t tgid = find_proc_by_task(task, us_proc_info.m_f_dentry);
71 us_proc_info.tgid = gl_nNotifyTgid = tgid;
72 procs = sspt_procs_get_by_task_or_new(task);
74 /* install probes in already mapped memory */
75 install_proc_probes(task, procs);
79 if (us_proc_info.tgid == task->tgid) {
80 procs = sspt_procs_get_by_task_or_new(task);
85 unsigned long page = addr & PAGE_MASK;
86 install_page_probes(page, task, procs);
93 static struct kretprobe pf_kretprobe = {
94 .entry_handler = entry_handler_pf,
95 .handler = ret_handler_pf,
96 .data_size = sizeof(struct pf_data)
102 ******************************************************************************
104 ******************************************************************************
107 static void recover_child(struct task_struct *child_task, struct sspt_procs *procs)
109 uninstall_us_proc_probes(child_task, procs, US_DISARM);
110 dbi_disarm_urp_inst_for_task(current, child_task);
113 static void rm_uprobes_child(struct task_struct *task)
115 struct sspt_procs *procs = sspt_procs_get_by_task(current);
117 recover_child(task, procs);
121 /* Delete uprobs in children at fork */
122 static int ret_handler_cp(struct kretprobe_instance *ri, struct pt_regs *regs)
124 struct task_struct* task = (struct task_struct *)regs_return_value(regs);
126 if(!task || IS_ERR(task))
129 if(task->mm != current->mm) /* check flags CLONE_VM */
130 rm_uprobes_child(task);
136 static struct kretprobe cp_kretprobe = {
137 .handler = ret_handler_cp,
143 ******************************************************************************
145 ******************************************************************************
148 /* Detects when target process removes IPs. */
149 static int mr_pre_handler(struct kprobe *p, struct pt_regs *regs)
151 struct sspt_procs *procs = NULL;
152 struct task_struct *task = (struct task_struct *)regs->ARM_r0; /* for ARM */
154 if (!is_us_instrumentation() || task->tgid != task->pid) {
159 procs = sspt_procs_get_by_task(task);
161 if (task->tgid == us_proc_info.tgid) {
162 procs = sspt_procs_get_by_task(task);
163 us_proc_info.tgid = 0;
168 int ret = uninstall_us_proc_probes(task, procs, US_UNREGS_PROBE);
170 printk("failed to uninstall IPs (%d)!\n", ret);
173 dbi_unregister_all_uprobes(task);
180 static struct kprobe mr_kprobe = {
181 .pre_handler = mr_pre_handler
187 ******************************************************************************
189 ******************************************************************************
192 static int remove_unmap_probes(struct task_struct *task, struct sspt_procs *procs, unsigned long start, size_t len)
194 struct mm_struct *mm = task->mm;
195 struct vm_area_struct *vma;
197 if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE - start) {
201 if ((len = PAGE_ALIGN(len)) == 0) {
205 vma = find_vma(mm, start);
206 if (vma && check_vma(vma)) {
207 struct sspt_file *file;
208 unsigned long end = start + len;
209 struct dentry *dentry = vma->vm_file->f_dentry;
211 file = sspt_procs_find_file(procs, dentry);
213 if (vma->vm_start == start || vma->vm_end == end) {
214 unregister_us_file_probes(task, file, US_UNREGS_PROBE);
217 unsigned long page_addr;
218 struct sspt_page *page;
220 for (page_addr = vma->vm_start; page_addr < vma->vm_end; page_addr += PAGE_SIZE) {
221 page = sspt_find_page_mapped(file, page_addr);
223 sspt_unregister_page(page, US_UNREGS_PROBE, task);
227 if (check_install_pages_in_file(task, file)) {
237 /* Detects when target removes IPs. */
238 static int unmap_pre_handler(struct kprobe *p, struct pt_regs *regs)
241 struct mm_struct *mm = (struct mm_struct *)regs->ARM_r0;
242 unsigned long start = regs->ARM_r1;
243 size_t len = (size_t)regs->ARM_r2;
245 struct sspt_procs *procs = NULL;
246 struct task_struct *task = current;
248 //if user-space instrumentation is not set
249 if (!is_us_instrumentation()) {
253 procs = sspt_procs_get_by_task(task);
255 if (remove_unmap_probes(task, procs, start, len)) {
256 printk("ERROR do_munmap: start=%lx, len=%x\n", start, len);
264 static struct kprobe unmap_kprobe = {
265 .pre_handler = unmap_pre_handler
270 int register_helper(void)
274 /* install kprobe on 'do_munmap' to detect when for remove user space probes */
275 ret = dbi_register_kprobe(&unmap_kprobe);
277 printk("dbi_register_kprobe(do_munmap) result=%d!\n", ret);
281 /* install kprobe on 'mm_release' to detect when for remove user space probes */
282 ret = dbi_register_kprobe(&mr_kprobe);
284 printk("dbi_register_kprobe(mm_release) result=%d!\n", ret);
285 goto unregister_unmap;
289 /* install kretprobe on 'copy_process' */
290 ret = dbi_register_kretprobe(&cp_kretprobe);
292 printk("dbi_register_kretprobe(copy_process) result=%d!\n", ret);
296 /* install kretprobe on 'do_page_fault' to detect when they will be loaded */
297 ret = dbi_register_kretprobe(&pf_kretprobe);
299 printk("dbi_register_kretprobe(do_page_fault) result=%d!\n", ret);
306 dbi_unregister_kretprobe(&cp_kretprobe);
309 dbi_unregister_kprobe(&mr_kprobe, NULL);
312 dbi_unregister_kprobe(&unmap_kprobe, NULL);
317 void unregister_helper(void)
319 /* uninstall kretprobe with 'do_page_fault' */
320 dbi_unregister_kretprobe(&pf_kretprobe);
322 /* uninstall kretprobe with 'copy_process' */
323 dbi_unregister_kretprobe(&cp_kretprobe);
325 /* uninstall kprobe with 'mm_release' */
326 dbi_unregister_kprobe(&mr_kprobe, NULL);
328 /* uninstall kprobe with 'do_munmap' */
329 dbi_unregister_kprobe(&unmap_kprobe, NULL);
332 int init_helper(void)
335 addr = swap_ksyms("do_page_fault");
337 printk("Cannot find address for page fault function!\n");
340 pf_kretprobe.kp.addr = (kprobe_opcode_t *)addr;
342 addr = swap_ksyms("copy_process");
344 printk("Cannot find address for copy_process function!\n");
347 cp_kretprobe.kp.addr = (kprobe_opcode_t *)addr;
349 addr = swap_ksyms("mm_release");
351 printk("Cannot find address for mm_release function!\n");
354 mr_kprobe.addr = (kprobe_opcode_t *)addr;
356 addr = swap_ksyms("do_munmap");
358 printk("Cannot find address for do_munmap function!\n");
361 unmap_kprobe.addr = (kprobe_opcode_t *)addr;
366 void uninit_helper(void)