1 #include <dbi_kprobes.h>
2 #include <dbi_kprobes_deps.h>
4 #include "us_proc_inst.h"
5 #include "us_slot_manager.h"
12 struct task_struct *check_task(struct task_struct *task);
15 ******************************************************************************
17 ******************************************************************************
24 static int entry_handler_pf(struct kretprobe_instance *ri, struct pt_regs *regs)
26 struct pf_data *data = (struct pf_data *)ri->data;
28 #if defined(CONFIG_X86)
29 data->addr = read_cr2();
30 #elif defined(CONFIG_ARM)
31 data->addr = regs->ARM_r0;
33 #error this architecture is not supported
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)
42 struct task_struct *task;
43 struct sspt_proc *proc;
45 call_page_fault(((struct pf_data *)ri->data)->addr);
49 * Because process threads have same address space
50 * we instrument only group_leader of all this threads
52 task = current->group_leader;
56 proc = sspt_proc_get_by_task(task);
60 task = check_task(task);
62 proc = sspt_proc_get_new(task);
69 if (proc->first_install) {
71 page = ((struct pf_data *)ri->data)->addr & PAGE_MASK;
72 sspt_proc_install_page(proc, page);
74 sspt_proc_install(proc);
80 static struct kretprobe pf_kretprobe = {
81 .entry_handler = entry_handler_pf,
82 .handler = ret_handler_pf,
83 .data_size = sizeof(struct pf_data)
89 ******************************************************************************
91 ******************************************************************************
94 static void recover_child(struct task_struct *child_task, struct sspt_proc *proc)
96 sspt_proc_uninstall(proc, child_task, US_DISARM);
97 dbi_disarm_urp_inst_for_task(current, child_task);
100 static void rm_uprobes_child(struct task_struct *task)
102 struct sspt_proc *proc = sspt_proc_get_by_task(current);
104 recover_child(task, proc);
108 /* Delete uprobs in children at fork */
109 static int ret_handler_cp(struct kretprobe_instance *ri, struct pt_regs *regs)
111 struct task_struct *task = (struct task_struct *)regs_return_value(regs);
115 if(!task || IS_ERR(task))
118 if(task->mm != current->mm) { /* check flags CLONE_VM */
119 rm_uprobes_child(task);
121 if (check_task(current)) {
122 struct sspt_proc *proc;
124 proc = sspt_proc_get_new(task);
125 sspt_proc_install(proc);
132 static struct kretprobe cp_kretprobe = {
133 .handler = ret_handler_cp,
139 ******************************************************************************
141 ******************************************************************************
144 /* Detects when target process removes IPs. */
145 static int mr_pre_handler(struct kprobe *p, struct pt_regs *regs)
147 struct sspt_proc *proc = NULL;
148 struct task_struct *task;
150 #if defined(CONFIG_X86)
151 task = (struct task_struct *)regs->EREG(ax);
152 #elif defined(CONFIG_ARM)
153 task = (struct task_struct *)regs->ARM_r0;
155 #error this architecture is not supported
158 if (is_kthread(task))
161 if (task->tgid != task->pid) {
165 call_mm_release(task);
168 proc = sspt_proc_get_by_task(task);
170 int ret = sspt_proc_uninstall(proc, task, US_UNREGS_PROBE);
172 printk("failed to uninstall IPs (%d)!\n", ret);
175 dbi_unregister_all_uprobes(task);
182 static struct kprobe mr_kprobe = {
183 .pre_handler = mr_pre_handler
189 ******************************************************************************
191 ******************************************************************************
194 static int remove_unmap_probes(struct task_struct *task, struct sspt_proc *proc, unsigned long start, size_t len)
196 struct mm_struct *mm = task->mm;
197 struct vm_area_struct *vma;
201 if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE - start) {
205 if ((len = PAGE_ALIGN(len)) == 0) {
209 vma = find_vma(mm, start);
210 if (vma && check_vma(vma)) {
211 struct sspt_file *file;
212 unsigned long end = start + len;
213 struct dentry *dentry = vma->vm_file->f_dentry;
215 file = sspt_proc_find_file(proc, dentry);
217 if (vma->vm_start == start || vma->vm_end == end) {
218 sspt_file_uninstall(file, task, US_UNREGS_PROBE);
221 unsigned long page_addr;
222 struct sspt_page *page;
224 for (page_addr = vma->vm_start; page_addr < vma->vm_end; page_addr += PAGE_SIZE) {
225 page = sspt_find_page_mapped(file, page_addr);
227 sspt_unregister_page(page, US_UNREGS_PROBE, task);
231 if (sspt_file_check_install_pages(file)) {
241 /* Detects when target removes IPs. */
242 static int unmap_pre_handler(struct kprobe *p, struct pt_regs *regs)
245 struct mm_struct *mm;
249 #if defined(CONFIG_X86)
250 mm = (struct mm_struct *)regs->EREG(ax);
251 start = regs->EREG(dx);
252 len = (size_t)regs->EREG(cx);
253 #elif defined(CONFIG_ARM)
254 mm = (struct mm_struct *)regs->ARM_r0;
255 start = regs->ARM_r1;
256 len = (size_t)regs->ARM_r2;
258 #error this architecture is not supported
261 struct sspt_proc *proc = NULL;
262 struct task_struct *task = current;
264 if (is_kthread(task))
267 proc = sspt_proc_get_by_task(task);
269 if (remove_unmap_probes(task, proc, start, len)) {
270 printk("ERROR do_munmap: start=%lx, len=%x\n", start, len);
278 static struct kprobe unmap_kprobe = {
279 .pre_handler = unmap_pre_handler
284 int register_helper(void)
288 /* install kprobe on 'do_munmap' to detect when for remove user space probes */
289 ret = dbi_register_kprobe(&unmap_kprobe);
291 printk("dbi_register_kprobe(do_munmap) result=%d!\n", ret);
295 /* install kprobe on 'mm_release' to detect when for remove user space probes */
296 ret = dbi_register_kprobe(&mr_kprobe);
298 printk("dbi_register_kprobe(mm_release) result=%d!\n", ret);
299 goto unregister_unmap;
303 /* install kretprobe on 'copy_process' */
304 ret = dbi_register_kretprobe(&cp_kretprobe);
306 printk("dbi_register_kretprobe(copy_process) result=%d!\n", ret);
310 /* install kretprobe on 'do_page_fault' to detect when they will be loaded */
311 ret = dbi_register_kretprobe(&pf_kretprobe);
313 printk("dbi_register_kretprobe(do_page_fault) result=%d!\n", ret);
320 dbi_unregister_kretprobe(&cp_kretprobe);
323 dbi_unregister_kprobe(&mr_kprobe, NULL);
326 dbi_unregister_kprobe(&unmap_kprobe, NULL);
331 void unregister_helper(void)
333 /* uninstall kretprobe with 'do_page_fault' */
334 dbi_unregister_kretprobe(&pf_kretprobe);
336 /* uninstall kretprobe with 'copy_process' */
337 dbi_unregister_kretprobe(&cp_kretprobe);
339 /* uninstall kprobe with 'mm_release' */
340 dbi_unregister_kprobe(&mr_kprobe, NULL);
342 /* uninstall kprobe with 'do_munmap' */
343 dbi_unregister_kprobe(&unmap_kprobe, NULL);
346 int init_helper(void)
349 addr = swap_ksyms("do_page_fault");
351 printk("Cannot find address for page fault function!\n");
354 pf_kretprobe.kp.addr = (kprobe_opcode_t *)addr;
356 addr = swap_ksyms("copy_process");
358 printk("Cannot find address for copy_process function!\n");
361 cp_kretprobe.kp.addr = (kprobe_opcode_t *)addr;
363 addr = swap_ksyms("mm_release");
365 printk("Cannot find address for mm_release function!\n");
368 mr_kprobe.addr = (kprobe_opcode_t *)addr;
370 addr = swap_ksyms("do_munmap");
372 printk("Cannot find address for do_munmap function!\n");
375 unmap_kprobe.addr = (kprobe_opcode_t *)addr;
380 void uninit_helper(void)