From 1a0f08ae850a15646ebebeacb4d130f967c28aba Mon Sep 17 00:00:00 2001 From: Vyacheslav Cherkashin Date: Wed, 15 May 2013 16:46:44 +0400 Subject: [PATCH] [FEATURE] add filter_by_pach --- driver/Kbuild | 2 +- driver/filters/filter_by_pach.c | 82 +++++++++++++++++++++++++++++++++++++++++ driver/filters/filter_by_pach.h | 32 ++++++++++++++++ driver/us_proc_inst.c | 38 ++++++++++--------- driver/us_proc_inst.h | 1 - 5 files changed, 135 insertions(+), 20 deletions(-) create mode 100644 driver/filters/filter_by_pach.c create mode 100644 driver/filters/filter_by_pach.h diff --git a/driver/Kbuild b/driver/Kbuild index 983f23a..b274062 100644 --- a/driver/Kbuild +++ b/driver/Kbuild @@ -4,4 +4,4 @@ obj-m := swap_driver.o swap_driver-y := error_storage.o device_driver.o ec.o legacy.o module.o probes.o \ probes_manager.o storage.o us_proc_inst.o helper.o us_slot_manager.o \ sspt/ip.o sspt/sspt_page.o sspt/sspt_file.o sspt/sspt_procs.o \ - filters/filters_core.o + filters/filters_core.o filters/filter_by_pach.o diff --git a/driver/filters/filter_by_pach.c b/driver/filters/filter_by_pach.c new file mode 100644 index 0000000..5914b01 --- /dev/null +++ b/driver/filters/filter_by_pach.c @@ -0,0 +1,82 @@ +/* + * Dynamic Binary Instrumentation Module based on KProbes + * modules/driver/filters/filter_by_pach.c + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * Copyright (C) Samsung Electronics, 2013 + * + * 2013 Vyacheslav Cherkashin + * + */ + +#include +#include +#include "filters_core.h" + +static struct dentry *dentry = NULL; + +static int check_dentry(struct task_struct *task, struct dentry *dentry) +{ + struct vm_area_struct *vma; + struct mm_struct *mm = task->mm; + + if (mm == NULL) { + return 0; + } + + for (vma = mm->mmap; vma; vma = vma->vm_next) { + if (check_vma(vma) && vma->vm_file->f_dentry == dentry) { + return 1; + } + } + + return 0; +} + +static int init_by_pach(void *data, size_t size) +{ + if (dentry) { + return -EPERM; + } + + dentry = (struct dentry *)data; + + return 0; +} + +static void uninit_by_pach(void) +{ + dentry = NULL; +} + +static struct task_struct *call_by_pach(struct task_struct *task) +{ + if (dentry && check_dentry(task, dentry)) + return task; + + return NULL; +} + +static struct task_filter ts_filter = { + .init = init_by_pach, + .uninit = uninit_by_pach, + .call = call_by_pach +}; + +struct task_filter *get_filter_by_pach(void) +{ + return &ts_filter; +} diff --git a/driver/filters/filter_by_pach.h b/driver/filters/filter_by_pach.h new file mode 100644 index 0000000..99169a9 --- /dev/null +++ b/driver/filters/filter_by_pach.h @@ -0,0 +1,32 @@ +#ifndef _FILTER_BY_PACH_H +#define _FILTER_BY_PACH_H + +/* + * Dynamic Binary Instrumentation Module based on KProbes + * modules/driver/filters/filter_by_pach.h + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * Copyright (C) Samsung Electronics, 2013 + * + * 2013 Vyacheslav Cherkashin + * + */ + +struct task_filter; + +struct task_filter *get_filter_by_pach(void); + +#endif /* _FILTER_BY_PACH_H */ diff --git a/driver/us_proc_inst.c b/driver/us_proc_inst.c index ff3ed84..64f7fe1 100644 --- a/driver/us_proc_inst.c +++ b/driver/us_proc_inst.c @@ -23,9 +23,12 @@ #include "sspt/sspt.h" #include "filters/filters_core.h" +#include "filters/filter_by_pach.h" #include "helper.h" #include "us_slot_manager.h" +static const char *app_filter = "app"; + unsigned long ujprobe_event_pre_handler (struct us_ip *ip, struct pt_regs *regs); void ujprobe_event_handler (unsigned long arg1, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5, unsigned long arg6); int uretprobe_event_handler(struct uretprobe_instance *probe, struct pt_regs *regs, struct us_ip *ip); @@ -225,6 +228,9 @@ int deinst_usr_space_proc (void) } } + uninit_filter(); + unregister_filter(app_filter); + return iRet; } @@ -240,6 +246,20 @@ int inst_usr_space_proc (void) DPRINTF("User space instr"); + ret = register_filter(app_filter, get_filter_by_pach()); + if (ret) + return ret; + + if (strcmp(us_proc_info.path, "*")) { + ret = set_filter(app_filter); + if (ret) + return ret; + + ret = init_filter(us_proc_info.m_f_dentry, 0); + if (ret) + return ret; + } + ret = register_helper(); if (ret) { return ret; @@ -301,24 +321,6 @@ int uninstall_us_proc_probes(struct task_struct *task, struct sspt_procs *procs, return err; } -int check_dentry(struct task_struct *task, struct dentry *dentry) -{ - struct vm_area_struct *vma; - struct mm_struct *mm = task->active_mm; - - if (mm == NULL) { - return 0; - } - - for (vma = mm->mmap; vma; vma = vma->vm_next) { - if (check_vma(vma) && vma->vm_file->f_dentry == dentry) { - return 1; - } - } - - return 0; -} - void print_vma(struct mm_struct *mm) { struct vm_area_struct *vma; diff --git a/driver/us_proc_inst.h b/driver/us_proc_inst.h index 1d71420..9c53dbe 100644 --- a/driver/us_proc_inst.h +++ b/driver/us_proc_inst.h @@ -51,7 +51,6 @@ int install_otg_ip(unsigned long addr, unsigned long jp_handler, uretprobe_handler_t rp_handler); -int check_dentry(struct task_struct *task, struct dentry *dentry); int uninstall_us_proc_probes(struct task_struct *task, struct sspt_procs *procs, enum US_FLAGS flag); int check_vma(struct vm_area_struct *vma); int unregister_us_file_probes(struct task_struct *task, struct sspt_file *file, enum US_FLAGS flag); -- 2.7.4