Fork once only
Right now there are three threads (process) 1. Main thread in parent: monitor logcat 2. Second thread in parent: monitor hidelist 3. Children: switch namespace and unmounts
This commit is contained in:
parent
70e332b9e8
commit
8d6d619eed
@ -18,8 +18,8 @@
|
|||||||
#define HIDELIST "/magisk/.core/magiskhide/hidelist"
|
#define HIDELIST "/magisk/.core/magiskhide/hidelist"
|
||||||
|
|
||||||
FILE *logfile;
|
FILE *logfile;
|
||||||
int i, list_size;
|
int i, list_size, pipefd[2];
|
||||||
char **hide_list = NULL, cache_block[256];
|
char **hide_list = NULL;
|
||||||
pthread_mutex_t mutex;
|
pthread_mutex_t mutex;
|
||||||
|
|
||||||
char **file_to_str_arr(FILE *fp, int *size) {
|
char **file_to_str_arr(FILE *fp, int *size) {
|
||||||
@ -55,34 +55,28 @@ void lazy_unmount(const char* mountpoint) {
|
|||||||
fprintf(logfile, "MagiskHide: Unmount Failed (%s)\n", mountpoint);
|
fprintf(logfile, "MagiskHide: Unmount Failed (%s)\n", mountpoint);
|
||||||
}
|
}
|
||||||
|
|
||||||
//WARNING: Calling this will change our current namespace
|
int hideMagisk() {
|
||||||
//We don't care because we don't want to run from here anyway
|
int pid;
|
||||||
int hideMagisk(int pid, int uid) {
|
char path[256], cache_block[256];
|
||||||
// struct stat info;
|
cache_block[0] = 0;
|
||||||
char path[256];
|
|
||||||
// snprintf(path, 256, "/proc/%d", pid);
|
|
||||||
// if (stat(path, &info) == -1) {
|
|
||||||
// fprintf(logfile, "MagiskHide: Unable to get info for pid=%d\n", pid);
|
|
||||||
// return 1;
|
|
||||||
// }
|
|
||||||
// if (info.st_uid != uid) {
|
|
||||||
// fprintf(logfile, "MagiskHide: Incorrect uid=%d, expect uid=%d\n", info.st_uid, uid);
|
|
||||||
// return 1;
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
close(pipefd[1]);
|
||||||
|
while(1) {
|
||||||
|
read(pipefd[0], &pid, sizeof(pid));
|
||||||
|
if(pid == -1) break;
|
||||||
snprintf(path, 256, "/proc/%d/ns/mnt", pid);
|
snprintf(path, 256, "/proc/%d/ns/mnt", pid);
|
||||||
int fd = open(path, O_RDONLY);
|
int fd = open(path, O_RDONLY);
|
||||||
if(fd == -1) return 2; // Maybe process died..
|
if(fd == -1) continue; // Maybe process died..
|
||||||
if(setns(fd, 0) == -1) {
|
if(setns(fd, 0) == -1) {
|
||||||
fprintf(logfile, "MagiskHide: Unable to change namespace for pid=%d\n", pid);
|
fprintf(logfile, "MagiskHide: Unable to change namespace for pid=%d\n", pid);
|
||||||
return 3;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
snprintf(path, 256, "/proc/%d/mounts", pid);
|
snprintf(path, 256, "/proc/%d/mounts", pid);
|
||||||
FILE *mount_fp = fopen(path, "r");
|
FILE *mount_fp = fopen(path, "r");
|
||||||
if (mount_fp == NULL) {
|
if (mount_fp == NULL) {
|
||||||
fprintf(logfile, "MagiskHide: Error opening mount list!\n");
|
fprintf(logfile, "MagiskHide: Error opening mount list!\n");
|
||||||
return 4;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
int mount_size;
|
int mount_size;
|
||||||
@ -102,7 +96,7 @@ int hideMagisk(int pid, int uid) {
|
|||||||
for(i = mount_size - 1; i >= 0; --i) {
|
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")) ) {
|
|| (strstr(mount_list[i], cache_block) && strstr(mount_list[i], "/system")) ) {
|
||||||
sscanf(mount_list[i], "%256s %256s", mountpoint, mountpoint);
|
sscanf(mount_list[i], "%*s %256s", mountpoint);
|
||||||
lazy_unmount(mountpoint);
|
lazy_unmount(mountpoint);
|
||||||
}
|
}
|
||||||
free(mount_list[i]);
|
free(mount_list[i]);
|
||||||
@ -118,15 +112,31 @@ int hideMagisk(int pid, int uid) {
|
|||||||
// Unmount loop mounts
|
// Unmount loop mounts
|
||||||
for(i = mount_size - 1; i >= 0; --i) {
|
for(i = mount_size - 1; i >= 0; --i) {
|
||||||
if (strstr(mount_list[i], "/dev/block/loop")) {
|
if (strstr(mount_list[i], "/dev/block/loop")) {
|
||||||
sscanf(mount_list[i], "%256s %256s", mountpoint, mountpoint);
|
sscanf(mount_list[i], "%*s %256s", mountpoint);
|
||||||
lazy_unmount(mountpoint);
|
lazy_unmount(mountpoint);
|
||||||
}
|
}
|
||||||
free(mount_list[i]);
|
free(mount_list[i]);
|
||||||
}
|
}
|
||||||
// Free memory
|
// Free memory
|
||||||
free(mount_list);
|
free(mount_list);
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
// Should never go here
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
// Below are UID checks, not used now but I'll leave it here
|
||||||
|
|
||||||
|
// struct stat info;
|
||||||
|
|
||||||
|
// snprintf(path, 256, "/proc/%d", pid);
|
||||||
|
// if (stat(path, &info) == -1) {
|
||||||
|
// fprintf(logfile, "MagiskHide: Unable to get info for pid=%d\n", pid);
|
||||||
|
// return 1;
|
||||||
|
// }
|
||||||
|
// if (info.st_uid != uid) {
|
||||||
|
// fprintf(logfile, "MagiskHide: Incorrect uid=%d, expect uid=%d\n", info.st_uid, uid);
|
||||||
|
// return 1;
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
void update_list(const char *listpath) {
|
void update_list(const char *listpath) {
|
||||||
@ -202,11 +212,18 @@ int main(int argc, char **argv, char **envp) {
|
|||||||
logfile = fopen(LOGFILE, "a+");
|
logfile = fopen(LOGFILE, "a+");
|
||||||
setbuf(logfile, NULL);
|
setbuf(logfile, NULL);
|
||||||
|
|
||||||
// Set cache block to null
|
// Fork a child to handle namespace switches and unmounts
|
||||||
cache_block[0] = 0;
|
pipe(pipefd);
|
||||||
|
forkpid = fork();
|
||||||
|
if (forkpid < 0)
|
||||||
|
return 1;
|
||||||
|
if (forkpid == 0)
|
||||||
|
return hideMagisk();
|
||||||
|
|
||||||
|
close(pipefd[0]);
|
||||||
|
|
||||||
|
// Start a thread to constantly check the hide list
|
||||||
pthread_t list_monitor;
|
pthread_t list_monitor;
|
||||||
|
|
||||||
pthread_mutex_init(&mutex, NULL);
|
pthread_mutex_init(&mutex, NULL);
|
||||||
pthread_create(&list_monitor, NULL, monitor_list, HIDELIST);
|
pthread_create(&list_monitor, NULL, monitor_list, HIDELIST);
|
||||||
|
|
||||||
@ -238,25 +255,22 @@ int main(int argc, char **argv, char **envp) {
|
|||||||
for (i = 0; i < list_size; ++i) {
|
for (i = 0; i < list_size; ++i) {
|
||||||
if(strstr(processName, hide_list[i])) {
|
if(strstr(processName, hide_list[i])) {
|
||||||
fprintf(logfile, "MagiskHide: Disabling for process=%s, PID=%d, UID=%d\n", processName, pid, uid);
|
fprintf(logfile, "MagiskHide: Disabling for process=%s, PID=%d, UID=%d\n", processName, pid, uid);
|
||||||
forkpid = fork();
|
write(pipefd[1], &pid, sizeof(pid));
|
||||||
if (forkpid < 0)
|
|
||||||
break;
|
|
||||||
if (forkpid == 0) {
|
|
||||||
hideMagisk(pid, uid);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
waitpid(forkpid, NULL, 0);
|
|
||||||
kill(forkpid, SIGTERM);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close the logcat monitor
|
||||||
pclose(p);
|
pclose(p);
|
||||||
|
|
||||||
|
// Close the config list monitor
|
||||||
pthread_kill(list_monitor, SIGQUIT);
|
pthread_kill(list_monitor, SIGQUIT);
|
||||||
pthread_mutex_destroy(&mutex);
|
pthread_mutex_destroy(&mutex);
|
||||||
|
|
||||||
|
// Terminate our children
|
||||||
|
i = -1;
|
||||||
|
write(pipefd[1], &i, sizeof(i));
|
||||||
|
|
||||||
fprintf(logfile, "MagiskHide: Cannot read from logcat, abort...\n");
|
fprintf(logfile, "MagiskHide: Cannot read from logcat, abort...\n");
|
||||||
fclose(logfile);
|
fclose(logfile);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user