Add zygote namespace detection

This commit is contained in:
topjohnwu 2017-04-07 07:50:02 +08:00
parent a1fd7704e0
commit a223f6056e
4 changed files with 28 additions and 4 deletions

View File

@ -9,10 +9,10 @@
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include <sys/prctl.h>
#include "magisk.h"
#include "utils.h"

View File

@ -7,6 +7,8 @@
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <signal.h>
#include <sys/types.h>
#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() {

View File

@ -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:

View File

@ -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);