Optimize magiskhide to work with the log daemon

This commit is contained in:
topjohnwu 2018-07-04 01:52:23 +08:00
parent 52a2c6958b
commit e7a2144def
5 changed files with 100 additions and 90 deletions

View File

@ -26,21 +26,15 @@ static char *prop_value[] =
"0", "0", "0", "1", "0", "0", "0", "1",
"user", "release-keys", "0", NULL }; "user", "release-keys", "0", NULL };
static int mocked = 0;
void manage_selinux() { void manage_selinux() {
if (mocked) return; char val;
char val[1];
int fd = xopen(SELINUX_ENFORCE, O_RDONLY); int fd = xopen(SELINUX_ENFORCE, O_RDONLY);
xxread(fd, val, 1); xxread(fd, &val, sizeof(val));
close(fd); close(fd);
// Permissive // Permissive
if (val[0] == '0') { if (val == '0') {
LOGI("hide_daemon: Permissive detected, hide the state\n");
chmod(SELINUX_ENFORCE, 0640); chmod(SELINUX_ENFORCE, 0640);
chmod(SELINUX_POLICY, 0440); chmod(SELINUX_POLICY, 0440);
mocked = 1;
} }
} }
@ -65,6 +59,10 @@ static void rm_magisk_prop(const char *name, const char *value, void *v) {
} }
} }
static void kill_proc(int pid) {
kill(pid, SIGTERM);
}
void clean_magisk_props() { void clean_magisk_props() {
LOGD("hide_utils: Cleaning magisk props\n"); LOGD("hide_utils: Cleaning magisk props\n");
getprop_all(rm_magisk_prop, NULL); getprop_all(rm_magisk_prop, NULL);

View File

@ -22,10 +22,6 @@ int hideEnabled = 0;
static pthread_t proc_monitor_thread; static pthread_t proc_monitor_thread;
pthread_mutex_t hide_lock, file_lock; pthread_mutex_t hide_lock, file_lock;
void kill_proc(int pid) {
kill(pid, SIGTERM);
}
static void usage(char *arg0) { static void usage(char *arg0) {
fprintf(stderr, fprintf(stderr,
"MagiskHide v" xstr(MAGISK_VERSION) "(" xstr(MAGISK_VER_CODE) ") (by topjohnwu) - Hide Magisk!\n\n" "MagiskHide v" xstr(MAGISK_VERSION) "(" xstr(MAGISK_VER_CODE) ") (by topjohnwu) - Hide Magisk!\n\n"

View File

@ -5,9 +5,6 @@
#define TERM_THREAD SIGUSR1 #define TERM_THREAD SIGUSR1
// Kill process
void kill_proc(int pid);
// Process monitor // Process monitor
void proc_monitor(); void proc_monitor();

View File

@ -126,18 +126,16 @@ void proc_monitor() {
term_thread(TERM_THREAD); term_thread(TERM_THREAD);
} }
while(1) {
// Connect to the log daemon // Connect to the log daemon
connect_daemon2(LOG_DAEMON, &sockfd); connect_daemon2(LOG_DAEMON, &sockfd);
write_int(sockfd, HIDE_CONNECT); write_int(sockfd, HIDE_CONNECT);
FILE *logs = fdopen(sockfd, "r"); FILE *log_in = fdopen(sockfd, "r");
char log[4096], *line; char buf[4096];
while (1) { while (fgets(buf, sizeof(buf), log_in)) {
/* It might be interrupted */ char *ss = strchr(buf, '[');
if (fgets(log, sizeof(log), logs) == NULL) int pid, ppid, num = 0;
continue;
char *ss = strchr(log, '[');
int pid, ppid, ret, comma = 0;
char *pos = ss, proc[256], ns[32], pns[32]; char *pos = ss, proc[256], ns[32], pns[32];
while(1) { while(1) {
@ -145,28 +143,35 @@ void proc_monitor() {
if(pos == NULL) if(pos == NULL)
break; break;
pos[0] = ' '; pos[0] = ' ';
++comma; ++num;
} }
if (comma == 6) if(sscanf(ss, num == 6 ? "[%*d %d %*d %*d %256s" : "[%*d %d %*d %256s", &pid, proc) != 2)
ret = sscanf(ss, "[%*d %d %*d %*d %256s", &pid, proc);
else
ret = sscanf(ss, "[%*d %d %*d %256s", &pid, proc);
if(ret != 2)
continue; continue;
ppid = parse_ppid(pid); // Make sure our target is alive
if (kill(pid, 0))
continue;
// Allow hiding sub-services of applications // Allow hiding sub-services of applications
char *colon = strchr(proc, ':'); char *colon = strchr(proc, ':');
if (colon) if (colon)
*colon = '\0'; *colon = '\0';
// Critical region int hide = 0;
pthread_mutex_lock(&hide_lock); pthread_mutex_lock(&hide_lock);
char *line;
vec_for_each(hide_list, line) { vec_for_each(hide_list, line) {
if (strcmp(proc, line) == 0) { if (strcmp(proc, line) == 0) {
hide = 1;
break;
}
}
pthread_mutex_unlock(&hide_lock);
if (!hide)
continue;
ppid = parse_ppid(pid);
read_namespace(ppid, pns, sizeof(pns)); read_namespace(ppid, pns, sizeof(pns));
do { do {
read_namespace(pid, ns, sizeof(ns)); read_namespace(pid, ns, sizeof(ns));
@ -184,7 +189,8 @@ void proc_monitor() {
if (colon) if (colon)
*colon = ':'; *colon = ':';
#ifdef MAGISK_DEBUG #ifdef MAGISK_DEBUG
LOGI("proc_monitor: %s (PID=[%d] ns=%s)(PPID=[%d] ns=%s)\n", proc, pid, ns + 4, ppid, pns + 4); LOGI("proc_monitor: %s (PID=[%d] ns=%s)(PPID=[%d] ns=%s)\n",
proc, pid, ns + 4, ppid, pns + 4);
#else #else
LOGI("proc_monitor: %s\n", proc); LOGI("proc_monitor: %s\n", proc);
#endif #endif
@ -195,10 +201,7 @@ void proc_monitor() {
*/ */
if (fork_dont_care() == 0) if (fork_dont_care() == 0)
hide_daemon(pid); hide_daemon(pid);
}
break; // The other end EOF, restart the connection
}
}
pthread_mutex_unlock(&hide_lock);
} }
} }

View File

@ -170,22 +170,38 @@ void ps(void (*func)(int)) {
static void (*ps_filter_cb)(int); static void (*ps_filter_cb)(int);
static const char *ps_filter_pattern; static const char *ps_filter_pattern;
static void proc_name_filter(int pid) { static void proc_name_filter(int pid) {
char buf[64]; char buf[128];
int fd; FILE *f;
snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid); sprintf(buf, "/proc/%d/comm", pid);
if (access(buf, R_OK) == -1 || (fd = xopen(buf, O_RDONLY)) == -1) if ((f = fopen(buf, "r"))) {
fgets(buf, sizeof(buf), f);
if (strcmp(buf, ps_filter_pattern) == 0)
goto run_cb;
} else {
// The PID is already killed
return; return;
if (fdgets(buf, sizeof(buf), fd) == 0) {
snprintf(buf, sizeof(buf), "/proc/%d/comm", pid);
close(fd);
if (access(buf, R_OK) == -1 || (fd = xopen(buf, O_RDONLY)) == -1)
return;
fdgets(buf, sizeof(buf), fd);
} }
if (strcmp(buf, ps_filter_pattern) == 0) { fclose(f);
sprintf(buf, "/proc/%d/cmdline", pid);
f = fopen(buf, "r");
fgets(buf, sizeof(buf), f);
if (strcmp(basename(buf), ps_filter_pattern) == 0)
goto run_cb;
fclose(f);
sprintf(buf, "/proc/%d/exe", pid);
if (access(buf, F_OK) != 0)
return;
xreadlink(buf, buf, sizeof(buf));
if (strcmp(basename(buf), ps_filter_pattern) == 0)
goto run_cb;
return;
run_cb:
ps_filter_cb(pid); ps_filter_cb(pid);
} fclose(f);
close(fd); return;
} }
/* Call func with process name filtered with pattern */ /* Call func with process name filtered with pattern */