Magisk/jni/magiskhide/hide.c

79 lines
2.0 KiB
C
Raw Normal View History

2016-12-30 18:44:24 +00:00
#include "magiskhide.h"
int hideMagisk() {
close(pipefd[1]);
2017-01-01 10:54:13 +00:00
int pid, fd;
char cache_block[256];
2016-12-30 18:44:24 +00:00
cache_block[0] = '\0';
while(1) {
read(pipefd[0], &pid, sizeof(pid));
// Termination called
if(pid == -1) break;
manage_selinux();
2016-12-30 18:44:24 +00:00
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) {
fprintf(logfile, "MagiskHide: Unable to change namespace for pid=%d\n", pid);
continue;
}
close(fd);
snprintf(buffer, sizeof(buffer), "/proc/%d/mounts", pid);
FILE *mount_fp = fopen(buffer, "r");
if (mount_fp == NULL) {
fprintf(logfile, "MagiskHide: Error opening mount list!\n");
continue;
}
int mount_size;
char **mount_list = file_to_str_arr(mount_fp, &mount_size);
// Find the cache block name if not found yet
if (strlen(cache_block) == 0) {
for(i = 0; i < mount_size; ++i) {
if (strstr(mount_list[i], " /cache ")) {
sscanf(mount_list[i], "%256s", cache_block);
break;
}
}
}
// First unmount the dummy skeletons, cache mounts, and /sbin links
2016-12-30 18:44:24 +00:00
for(i = mount_size - 1; i >= 0; --i) {
2017-01-02 09:48:34 +00:00
if (strstr(mount_list[i], "tmpfs /system") || strstr(mount_list[i], "tmpfs /vendor")
|| strstr(mount_list[i], "tmpfs /sbin")
2017-03-18 08:57:29 +00:00
|| (strstr(mount_list[i], cache_block) && strstr(mount_list[i], "/system/")) ) {
2016-12-30 18:44:24 +00:00
sscanf(mount_list[i], "%*s %512s", buffer);
lazy_unmount(buffer);
}
free(mount_list[i]);
}
free(mount_list);
// Re-read mount infos
fseek(mount_fp, 0, SEEK_SET);
mount_list = file_to_str_arr(mount_fp, &mount_size);
fclose(mount_fp);
// Unmount loop mounts
for(i = mount_size - 1; i >= 0; --i) {
if (strstr(mount_list[i], "/dev/block/loop") && !strstr(mount_list[i], DUMMYPATH)) {
sscanf(mount_list[i], "%*s %512s", buffer);
lazy_unmount(buffer);
}
free(mount_list[i]);
}
free(mount_list);
// Send resume signal
kill(pid, SIGCONT);
}
// Should never go here
return 1;
}