From aa2eed2c380c0627048d4de278d4f26d58f04243 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Thu, 6 Oct 2016 02:17:21 +0800 Subject: [PATCH] Make hidesu hide Magisk and read config file --- .gitignore | 1 + jni/hidesu.c | 68 +++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 58 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index ee40a85d1..3d315ee19 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ obj libs +*.zip diff --git a/jni/hidesu.c b/jni/hidesu.c index b1711f939..639512dea 100644 --- a/jni/hidesu.c +++ b/jni/hidesu.c @@ -16,10 +16,13 @@ typedef unsigned short int sa_family_t; #include #include #include +#include + +#define HIDE_LIST "/magisk/.core/hidelist" //WARNING: Calling this will change our current namespace //We don't care because we don't want to run from here anyway -int disableSu(int pid) { +int hideMagisk(int pid) { char *path = NULL; asprintf(&path, "/proc/%d/ns/mnt", pid); int fd = open(path, O_RDONLY); @@ -29,24 +32,61 @@ int disableSu(int pid) { int res = syscall(SYS_setns, fd, 0); if(res == -1) return 3; - //XXX: What to mount to /sbin...? - res = mount("/system", "/sbin", "bind", MS_BIND, ""); + res = umount2("/magisk", MNT_DETACH); + if(res == -1) return 4; + res = mount("/magisk/.core/mirror/system", "/system", "bind", MS_BIND, ""); if(res == -1) return 4; return 0; } int main(int argc, char **argv, char **envp) { + // Read config file + int allocated = 16, line = 0, i; + char buffer[512]; + + char **list = (char **) malloc(sizeof(char*) * allocated); + FILE *fp = fopen(HIDE_LIST, "r"); + if (fp == NULL){ + fprintf(stderr, "Error opening hide list\n"); + exit(1); + } + while (1) { + if (fgets(buffer, sizeof(buffer), fp) == NULL) { + --line; + break; + } + if (line >= allocated) { + // Double our allocation and re-allocate + allocated = allocated * 2; + list = (char **) realloc(list, sizeof(char*) * allocated); + } + list[line] = malloc(strlen(buffer)); + strcpy(list[line], buffer); + int j; + // Remove endline + for (j = strlen(list[line]) - 1; j >= 0 && (list[line][j] == '\n' || list[line][j] == '\r'); j--) + ; + list[line][j + 1] = '\0'; + ++line; + } + fclose(fp); + + printf("Get package name from config:\n"); + for(i = 0; i <= line; i++) + printf("%s\n", list[i]); + printf("\n"); + + FILE *p = popen("while true;do 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) //UserID, pid, unix uid, processName, hostingType, hostingName - char buffer[512]; - int size = fgets(buffer, sizeof(buffer), p); + fgets(buffer, sizeof(buffer), p); { char *pos = buffer; while(1) { - pos = index(pos, ','); + pos = strchr(pos, ','); if(pos == NULL) break; pos[0] = ' '; @@ -61,15 +101,21 @@ int main(int argc, char **argv, char **envp) { if(ret != 6) { - printf("sscanf returned %d on '%s'\n", ret, buffer); continue; } -#define GMS_PROC "com.google.android.gms.unstable" - if(strcmp(processName, GMS_PROC) == 0) { - printf("Disabling for PID = %d, UID = %d\n", pid, uid); - disableSu(pid); + + for (i = 0; i <= line; ++i) { + if(strstr(processName, list[i]) != NULL) { + printf("Disabling for process = %s, PID = %d, UID = %d\n", processName, pid, uid); + hideMagisk(pid); + } } } + pclose(p); + for (; line >= 0; line--) + free(list[line]); + free(list); + return 0; }