From 57930ac32565007a496cceb1443e730016b0369e Mon Sep 17 00:00:00 2001 From: Anatolii Nikulin Date: Thu, 17 Sep 2015 23:43:22 +0300 Subject: [PATCH] [FIX] do not call dlopen in libpthread and libsmack Change-Id: I4e739c2c9224e7e65dd2d7af4d4429868e941efb Signed-off-by: Anatolii Nikulin --- preload/preload_module.c | 67 ++++++++++++++++++++++++++++++++++++++++++++--- preload/preload_storage.c | 54 ++++++++++++++++++++++++++++++++++++++ preload/preload_storage.h | 6 +++++ 3 files changed, 123 insertions(+), 4 deletions(-) diff --git a/preload/preload_module.c b/preload/preload_module.c index 759d46f..c105fd0 100644 --- a/preload/preload_module.c +++ b/preload/preload_module.c @@ -276,6 +276,56 @@ static struct vm_area_struct *__get_libc_vma(struct task_struct *task) return NULL; } +static struct vm_area_struct *__get_libpthread_vma(struct task_struct *task) +{ + struct vm_area_struct *vma = NULL; + struct bin_info *libpthread_info; + + libpthread_info = preload_storage_get_libpthread_info(); + + if (!libpthread_info) { + printk(PRELOAD_PREFIX "Cannot get libpthread info [%u %u %s]!\n", + task->tgid, task->pid, task->comm); + return NULL; + } + + for (vma = task->mm->mmap; vma; vma = vma->vm_next) { + if (vma->vm_file && vma->vm_flags & VM_EXEC + && vma->vm_file->f_dentry == libpthread_info->dentry) { + preload_storage_put_libpthread_info(libpthread_info); + return vma; + } + } + + preload_storage_put_libpthread_info(libpthread_info); + return NULL; +} + +static struct vm_area_struct *__get_libsmack_vma(struct task_struct *task) +{ + struct vm_area_struct *vma = NULL; + struct bin_info *libsmack_info; + + libsmack_info = preload_storage_get_libsmack_info(); + + if (!libsmack_info) { + printk(PRELOAD_PREFIX "Cannot get libsmack info [%u %u %s]!\n", + task->tgid, task->pid, task->comm); + return NULL; + } + + for (vma = task->mm->mmap; vma; vma = vma->vm_next) { + if (vma->vm_file && vma->vm_flags & VM_EXEC + && vma->vm_file->f_dentry == libsmack_info->dentry) { + preload_storage_put_libsmack_info(libsmack_info); + return vma; + } + } + + preload_storage_put_libsmack_info(libsmack_info); + return NULL; +} + static inline struct vm_area_struct *__get_vma_by_addr(struct task_struct *task, unsigned long caller_addr) { @@ -397,9 +447,18 @@ static bool __not_system_caller(struct task_struct *task, { struct vm_area_struct *linker_vma = __get_linker_vma(task); struct vm_area_struct *libc_vma = __get_libc_vma(task); - - if (linker_vma == NULL || libc_vma == NULL || caller == NULL || - caller == linker_vma || caller == libc_vma) + struct vm_area_struct *libpthread_vma = __get_libpthread_vma(task); + struct vm_area_struct *libsmack_vma = __get_libsmack_vma(task); + + if (linker_vma == NULL || + libc_vma == NULL || + libpthread_vma == NULL || + libsmack_vma == NULL || + caller == NULL || + caller == linker_vma || + caller == libc_vma || + caller == libpthread_vma || + caller == libsmack_vma) return false; return true; @@ -409,7 +468,7 @@ static bool __should_we_preload_handlers(struct task_struct *task, struct pt_regs *regs) { unsigned long caller_addr = get_regs_ret_func(regs); - struct vm_area_struct *cvma = __get_vma_by_addr(current, caller_addr); + struct vm_area_struct *cvma = __get_vma_by_addr(current, caller_addr); if (!__is_proc_mmap_mappable(task) || !__not_system_caller(task, cvma)) diff --git a/preload/preload_storage.c b/preload/preload_storage.c index c721491..e6d1c5d 100644 --- a/preload/preload_storage.c +++ b/preload/preload_storage.c @@ -10,6 +10,8 @@ static struct bin_info __handlers_info = { NULL, NULL }; static struct bin_info __linker_info = { NULL, NULL }; static struct bin_info __libc_info; +static struct bin_info __libpthread_info; +static struct bin_info __libsmack_info; static inline struct bin_info *__get_handlers_info(void) { @@ -161,6 +163,24 @@ static inline void __drop_libc_info(void) __libc_info.dentry = NULL; } +static inline void __drop_libpthread_info(void) +{ + if (__libpthread_info.dentry) + put_dentry(__libpthread_info.dentry); + + __libpthread_info.path = NULL; + __libpthread_info.dentry = NULL; +} + +static inline void __drop_libsmack_info(void) +{ + if (__libsmack_info.dentry) + put_dentry(__libsmack_info.dentry); + + __libsmack_info.path = NULL; + __libsmack_info.dentry = NULL; +} + void preload_storage_put_linker_info(struct bin_info *info) { } @@ -170,10 +190,28 @@ struct bin_info *preload_storage_get_libc_info(void) return &__libc_info; } +struct bin_info *preload_storage_get_libpthread_info(void) +{ + return &__libpthread_info; +} + +struct bin_info *preload_storage_get_libsmack_info(void) +{ + return &__libsmack_info; +} + void preload_storage_put_libc_info(struct bin_info *info) { } +void preload_storage_put_libpthread_info(struct bin_info *info) +{ +} + +void preload_storage_put_libsmack_info(struct bin_info *info) +{ +} + int preload_storage_init(void) { __libc_info.path = "/lib/libc.so.6"; @@ -182,11 +220,27 @@ int preload_storage_init(void) if (!__libc_info.dentry) return -ENOENT; + /* TODO check if we have not library */ + __libpthread_info.path = "/lib/libpthread.so.0"; + __libpthread_info.dentry = get_dentry(__libpthread_info.path); + + if (!__libpthread_info.dentry) + return -ENOENT; + + /* TODO check if we have not library */ + __libsmack_info.path = "/usr/lib/libsmack.so.1.0.0"; + __libsmack_info.dentry = get_dentry(__libsmack_info.path); + + if (!__libsmack_info.dentry) + return -ENOENT; + return 0; } void preload_storage_exit(void) { + __drop_libsmack_info(); + __drop_libpthread_info(); __drop_libc_info(); __drop_handlers_info(); __drop_linker_info(); diff --git a/preload/preload_storage.h b/preload/preload_storage.h index faa9ff6..2ae1675 100644 --- a/preload/preload_storage.h +++ b/preload/preload_storage.h @@ -18,6 +18,12 @@ void preload_storage_put_linker_info(struct bin_info *info); struct bin_info *preload_storage_get_libc_info(void); void preload_storage_put_libc_info(struct bin_info *info); +struct bin_info *preload_storage_get_libpthread_info(void); +void preload_storage_put_libpthread_info(struct bin_info *info); + +struct bin_info *preload_storage_get_libsmack_info(void); +void preload_storage_put_libsmack_info(struct bin_info *info); + int preload_storage_init(void); void preload_storage_exit(void); -- 2.7.4