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 install_page(((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);
113 if(!task || IS_ERR(task))
116 if(task->mm != current->mm) { /* check flags CLONE_VM */
117 rm_uprobes_child(task);
119 if (check_task(current)) {
120 struct sspt_proc *proc;
122 proc = sspt_proc_get_new(task);
123 sspt_proc_install(proc);
130 static struct kretprobe cp_kretprobe = {
131 .handler = ret_handler_cp,
137 ******************************************************************************
139 ******************************************************************************
142 /* Detects when target process removes IPs. */
143 static int mr_pre_handler(struct kprobe *p, struct pt_regs *regs)
145 struct sspt_proc *proc = NULL;
146 struct task_struct *task;
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;
153 #error this architecture is not supported
156 if (is_kthread(task))
159 if (task->tgid != task->pid) {
163 proc = sspt_proc_get_by_task(task);
165 int ret = sspt_proc_uninstall(proc, task, US_UNREGS_PROBE);
167 printk("failed to uninstall IPs (%d)!\n", ret);
170 dbi_unregister_all_uprobes(task);
177 static struct kprobe mr_kprobe = {
178 .pre_handler = mr_pre_handler
184 ******************************************************************************
186 ******************************************************************************
189 static int remove_unmap_probes(struct task_struct *task, struct sspt_proc *proc, unsigned long start, size_t len)
191 struct mm_struct *mm = task->mm;
192 struct vm_area_struct *vma;
194 if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE - start) {
198 if ((len = PAGE_ALIGN(len)) == 0) {
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;
208 file = sspt_proc_find_file(proc, dentry);
210 if (vma->vm_start == start || vma->vm_end == end) {
211 sspt_file_uninstall(file, task, US_UNREGS_PROBE);
214 unsigned long page_addr;
215 struct sspt_page *page;
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);
220 sspt_unregister_page(page, US_UNREGS_PROBE, task);
224 if (sspt_file_check_install_pages(file)) {
234 /* Detects when target removes IPs. */
235 static int unmap_pre_handler(struct kprobe *p, struct pt_regs *regs)
238 struct mm_struct *mm;
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;
251 #error this architecture is not supported
254 struct sspt_proc *proc = NULL;
255 struct task_struct *task = current;
257 if (is_kthread(task))
260 proc = sspt_proc_get_by_task(task);
262 if (remove_unmap_probes(task, proc, start, len)) {
263 printk("ERROR do_munmap: start=%lx, len=%x\n", start, len);
271 static struct kprobe unmap_kprobe = {
272 .pre_handler = unmap_pre_handler
277 int register_helper(void)
281 /* install kprobe on 'do_munmap' to detect when for remove user space probes */
282 ret = dbi_register_kprobe(&unmap_kprobe);
284 printk("dbi_register_kprobe(do_munmap) result=%d!\n", ret);
288 /* install kprobe on 'mm_release' to detect when for remove user space probes */
289 ret = dbi_register_kprobe(&mr_kprobe);
291 printk("dbi_register_kprobe(mm_release) result=%d!\n", ret);
292 goto unregister_unmap;
296 /* install kretprobe on 'copy_process' */
297 ret = dbi_register_kretprobe(&cp_kretprobe);
299 printk("dbi_register_kretprobe(copy_process) result=%d!\n", ret);
303 /* install kretprobe on 'do_page_fault' to detect when they will be loaded */
304 ret = dbi_register_kretprobe(&pf_kretprobe);
306 printk("dbi_register_kretprobe(do_page_fault) result=%d!\n", ret);
313 dbi_unregister_kretprobe(&cp_kretprobe);
316 dbi_unregister_kprobe(&mr_kprobe, NULL);
319 dbi_unregister_kprobe(&unmap_kprobe, NULL);
324 void unregister_helper(void)
326 /* uninstall kretprobe with 'do_page_fault' */
327 dbi_unregister_kretprobe(&pf_kretprobe);
329 /* uninstall kretprobe with 'copy_process' */
330 dbi_unregister_kretprobe(&cp_kretprobe);
332 /* uninstall kprobe with 'mm_release' */
333 dbi_unregister_kprobe(&mr_kprobe, NULL);
335 /* uninstall kprobe with 'do_munmap' */
336 dbi_unregister_kprobe(&unmap_kprobe, NULL);
339 int init_helper(void)
342 addr = swap_ksyms("do_page_fault");
344 printk("Cannot find address for page fault function!\n");
347 pf_kretprobe.kp.addr = (kprobe_opcode_t *)addr;
349 addr = swap_ksyms("copy_process");
351 printk("Cannot find address for copy_process function!\n");
354 cp_kretprobe.kp.addr = (kprobe_opcode_t *)addr;
356 addr = swap_ksyms("mm_release");
358 printk("Cannot find address for mm_release function!\n");
361 mr_kprobe.addr = (kprobe_opcode_t *)addr;
363 addr = swap_ksyms("do_munmap");
365 printk("Cannot find address for do_munmap function!\n");
368 unmap_kprobe.addr = (kprobe_opcode_t *)addr;
373 void uninit_helper(void)