1
mirror of https://github.com/topjohnwu/Magisk synced 2024-11-14 22:28:37 +01:00

Small updates for MagiskHide

This commit is contained in:
topjohnwu 2017-01-01 18:54:13 +08:00
parent 668601ca23
commit f9fea265cf
4 changed files with 68 additions and 51 deletions

View File

@ -4,5 +4,5 @@ include $(CLEAR_VARS)
LOCAL_MODULE := magiskhide
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := main.c hide.c list_monitor.c proc_monitor.c util.c
LOCAL_CFLAGS += -std=gnu11
LOCAL_CFLAGS += -std=gnu11 -O3
include $(BUILD_EXECUTABLE)

View File

@ -2,53 +2,16 @@
int hideMagisk() {
close(pipefd[1]);
int pid, zygote_num = 0;
char cache_block[256], zygote_ns[2][32];
int pid, fd;
char cache_block[256];
cache_block[0] = '\0';
// Set to the top priority
setpriority(PRIO_PROCESS, 0, -20);
// Get the mount namespace of zygote
FILE *p = popen("/data/busybox/ps | grep zygote | grep -v grep", "r");
while(fgets(buffer, sizeof(buffer), p)) {
if (zygote_num == 2) break;
sscanf(buffer, "%d", &pid);
read_namespace(pid, zygote_ns[zygote_num], 32);
++zygote_num;
}
pclose(p);
for (i = 0; i < zygote_num; ++i)
fprintf(logfile, "Zygote(%d) ns=%s ", i, zygote_ns[i]);
fprintf(logfile, "\n");
while(1) {
read(pipefd[0], &pid, sizeof(pid));
// Termination called
if(pid == -1) break;
int badns, fd;
while(1) {
badns = 0;
read_namespace(pid, buffer, 32);
printf("%s\n", buffer);
for (i = 0; i < zygote_num; ++i) {
if (strcmp(buffer, zygote_ns[i]) == 0) {
usleep(500);
badns = 1;
break;
}
}
if (!badns) break;
}
// Send pause signal ASAP
if (kill(pid, SIGSTOP) == -1) continue;
fprintf(logfile, "ns=%s)\n", buffer);
snprintf(buffer, sizeof(buffer), "/proc/%d/ns/mnt", pid);
if((fd = open(buffer, O_RDONLY)) == -1) continue; // Maybe process died..
if(setns(fd, 0) == -1) {
@ -79,7 +42,7 @@ int hideMagisk() {
// First unmount the dummy skeletons and the cache mounts
for(i = mount_size - 1; i >= 0; --i) {
if (strstr(mount_list[i], "tmpfs /system/") || strstr(mount_list[i], "tmpfs /vendor/")
if (strstr(mount_list[i], "tmpfs /system") || strstr(mount_list[i], "tmpfs /vendor/")
|| (strstr(mount_list[i], cache_block) && strstr(mount_list[i], "/system")) ) {
sscanf(mount_list[i], "%*s %512s", buffer);
lazy_unmount(buffer);

View File

@ -14,15 +14,25 @@ static void terminate(int sig) {
// Terminate our children
i = -1;
write(pipefd[1], &i, sizeof(i));
exit(0);
}
int main(int argc, char **argv, char **envp) {
int main(int argc, char *argv[]) {
if (argc > 0) {
if (strcmp(argv[1], "--daemon") == 0)
run_as_daemon();
else {
fprintf(stderr, "%s (with no options)\n\tRun magiskhide and output to stdout\n", argv[0]);
fprintf(stderr, "%s --daemon\n\tRun magiskhide as daemon, output to magisk.log\n", argv[0]);
return 1;
}
} else
logfile = stdout;
run_as_daemon();
// Handle all killing signals
signal(SIGINT, terminate);
signal(SIGKILL, terminate);
signal(SIGTERM, terminate);
// Fork a child to handle namespace switches and unmounts
@ -41,6 +51,9 @@ int main(int argc, char **argv, char **envp) {
pthread_mutex_init(&mutex, NULL);
pthread_create(&list_monitor, NULL, monitor_list, HIDELIST);
// Set main process to the top priority
setpriority(PRIO_PROCESS, 0, -20);
monitor_proc();
terminate(0);

View File

@ -1,8 +1,31 @@
#include "magiskhide.h"
void monitor_proc() {
// Monitor am_proc_start in main thread
FILE *p = popen("while true; do logcat -b events -v raw -s am_proc_start; sleep 1; done", "r");
int pid, badns, zygote_num = 0;
char init_ns[32], zygote_ns[2][32];
// Get the mount namespace of init
read_namespace(1, init_ns, 32);
// Get the mount namespace of zygote
FILE *p = popen("/data/busybox/ps | grep zygote | grep -v grep", "r");
while(fgets(buffer, sizeof(buffer), p)) {
if (zygote_num == 2) break;
sscanf(buffer, "%d", &pid);
do {
usleep(500);
read_namespace(pid, zygote_ns[zygote_num], 32);
} while (strcmp(zygote_ns[zygote_num], init_ns) == 0);
++zygote_num;
}
pclose(p);
for (i = 0; i < zygote_num; ++i)
fprintf(logfile, "Zygote(%d) ns=%s ", i, zygote_ns[i]);
fprintf(logfile, "\n");
// Monitor am_proc_start
p = popen("while true; do logcat -b events -c; logcat -b events -v raw -s am_proc_start; sleep 1; done", "r");
while(!feof(p)) {
//Format of am_proc_start is (as of Android 5.1 and 6.0)
@ -17,21 +40,39 @@ void monitor_proc() {
pos[0] = ' ';
}
int pid;
char processName[256];
int ret = sscanf(buffer, "[%*d %d %*d %256s", &pid, processName);
if(ret != 2)
continue;
pthread_mutex_lock(&mutex);
for (i = 0; i < list_size; ++i) {
if(strcmp(processName, hide_list[i]) == 0) {
// Check PID exist
if (kill(pid, 0) == -1) continue;
fprintf(logfile, "MagiskHide: %s(PID=%d ", processName, pid);
while(1) {
badns = 0;
read_namespace(pid, buffer, 32);
for (i = 0; i < zygote_num; ++i) {
if (strcmp(buffer, zygote_ns[i]) == 0) {
usleep(500);
badns = 1;
break;
}
}
if (!badns) break;
}
// Send pause signal ASAP
if (kill(pid, SIGSTOP) == -1) continue;
fprintf(logfile, "MagiskHide: %s(PID=%d ns=%s)\n", processName, pid, buffer);
// Unmount start
write(pipefd[1], &pid, sizeof(pid));
break;
}
}
pthread_mutex_unlock(&mutex);
}
// Close the logcat monitor