From a223f6056e068a62d98f52d4dc4529f932262ae3 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Fri, 7 Apr 2017 07:50:02 +0800 Subject: [PATCH] Add zygote namespace detection --- jni/magiskhide/hide_daemon.c | 2 +- jni/magiskhide/magiskhide.c | 12 ++++++++++++ jni/magiskhide/proc_monitor.c | 17 ++++++++++++++--- jni/utils/misc.c | 1 + 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/jni/magiskhide/hide_daemon.c b/jni/magiskhide/hide_daemon.c index 6283e4ce9..d2b67fd88 100644 --- a/jni/magiskhide/hide_daemon.c +++ b/jni/magiskhide/hide_daemon.c @@ -9,10 +9,10 @@ #include #include #include +#include #include #include #include -#include #include "magisk.h" #include "utils.h" diff --git a/jni/magiskhide/magiskhide.c b/jni/magiskhide/magiskhide.c index e79136171..e15f8cb10 100644 --- a/jni/magiskhide/magiskhide.c +++ b/jni/magiskhide/magiskhide.c @@ -7,6 +7,8 @@ #include #include #include +#include +#include #include "magisk.h" #include "utils.h" @@ -17,6 +19,10 @@ struct vector *hide_list, *new_list; static pthread_t proc_monitor_thread; +static void kill_proc(int pid) { + kill(pid, SIGTERM); +} + void launch_magiskhide() { /* * The setns system call do not support multithread processes @@ -36,9 +42,15 @@ void launch_magiskhide() { FILE *fp = xfopen(HIDELIST, "r"); file_to_vector(hide_list, fp); fclose(fp); + char *line; + vec_for_each(hide_list, line) { + LOGI("hide_list: [%s]\n", line); + ps_filter_proc_name(line, kill_proc); + } // Start a new thread to monitor processes pthread_create(&proc_monitor_thread, NULL, proc_monitor, NULL); + pthread_join(proc_monitor_thread, NULL); } void stop_magiskhide() { diff --git a/jni/magiskhide/proc_monitor.c b/jni/magiskhide/proc_monitor.c index 0ad50ccfe..0659c93a8 100644 --- a/jni/magiskhide/proc_monitor.c +++ b/jni/magiskhide/proc_monitor.c @@ -20,6 +20,9 @@ #define pthread_cleanup_push_fix(routine, arg) \ pthread_cleanup_push(routine, arg) } while(0); +static int zygote_num = 0; +static char init_ns[32], zygote_ns[2][32]; + static void read_namespace(const int pid, char* target, const size_t size) { char path[32]; snprintf(path, sizeof(path), "/proc/%d/ns/mnt", pid); @@ -40,13 +43,21 @@ static void cleanup_handler(void *arg) { hide_list = new_list = NULL; } +static void store_zygote_ns(int pid) { + do { + usleep(500); + read_namespace(pid, zygote_ns[zygote_num], 32); + } while (strcmp(zygote_ns[zygote_num], init_ns) == 0); + ++zygote_num; +} + void *proc_monitor(void *args) { // Register the cancel signal signal(SIGUSR1, quit_pthread); pthread_cleanup_push_fix(cleanup_handler, NULL); - int pid, zygote_num = 0; - char init_ns[32], zygote_ns[2][32], buffer[512]; + int pid; + char buffer[512]; FILE *p; // Get the mount namespace of init @@ -54,7 +65,7 @@ void *proc_monitor(void *args) { LOGI("proc_monitor: init ns=%s\n", init_ns); // Get the mount namespace of zygote - // TODO: Crawl through /proc to get process names + ps_filter_proc_name("zygote", store_zygote_ns); switch(zygote_num) { case 1: diff --git a/jni/utils/misc.c b/jni/utils/misc.c index 5643c3dbe..cf5e70e5f 100644 --- a/jni/utils/misc.c +++ b/jni/utils/misc.c @@ -97,6 +97,7 @@ static void proc_name_filter(int pid) { fdreadline(fd, buf, sizeof(buf)); } if (strstr(buf, ps_filter_pattern)) { + // printf("%d: %s\n", pid, buf); ps_filter_cb(pid); } close(fd);