diff --git a/jni/Android.mk b/jni/Android.mk index 63c185b75..6721f51f3 100644 --- a/jni/Android.mk +++ b/jni/Android.mk @@ -6,6 +6,7 @@ include $(CLEAR_VARS) LOCAL_MODULE := magiskhide LOCAL_MODULE_TAGS := optional LOCAL_SRC_FILES := magiskhide.c +LOCAL_CFLAGS += -std=gnu11 include $(BUILD_EXECUTABLE) include $(CLEAR_VARS) diff --git a/jni/magiskhide.c b/jni/magiskhide.c index 13db9191f..c78ea80e1 100644 --- a/jni/magiskhide.c +++ b/jni/magiskhide.c @@ -1,24 +1,21 @@ -typedef unsigned short int sa_family_t; -//Linux includes -#define _LINUX_TIME_H #define _GNU_SOURCE -#include #include -#include #include -#include -#include -#include -#include -#include #include #include -#include -#include +#include +#include #include -#include -#include -#include +#include + +#include +#include +#include + +FILE *logfile; +int i, list_size; +char **hide_list = NULL; +pthread_mutex_t mutex; char **file_to_str_arr(FILE *fp, int *size) { int allocated = 16; @@ -46,6 +43,13 @@ char **file_to_str_arr(FILE *fp, int *size) { return array; } +void lazy_unmount(const char* mountpoint) { + if (umount2(mountpoint, MNT_DETACH) != -1) + fprintf(logfile, "MagiskHide: Unmounted (%s)\n", mountpoint); + else + fprintf(logfile, "MagiskHide: Unmount Failed (%s)\n", mountpoint); +} + //WARNING: Calling this will change our current namespace //We don't care because we don't want to run from here anyway int hideMagisk(int pid) { @@ -53,15 +57,14 @@ int hideMagisk(int pid) { asprintf(&path, "/proc/%d/ns/mnt", pid); int fd = open(path, O_RDONLY); if(fd == -1) return 2; - int res = syscall(SYS_setns, fd, 0); - if(res == -1) return 3; + if(setns(fd, 0) == -1) return 3; free(path); path = NULL; asprintf(&path, "/proc/%d/mounts", pid); FILE *mount_fp = fopen(path, "r"); if (mount_fp == NULL) { - fprintf(stderr, "Error opening mount list!\n"); + fprintf(logfile, "Error opening mount list!\n"); return 1; } free(path); @@ -70,23 +73,14 @@ int hideMagisk(int pid) { char **mount_list = file_to_str_arr(mount_fp, &mount_size), mountpoint[256], *sbstr; fclose(mount_fp); - int i, unmount = 0; for(i = mount_size - 1; i >= 0; --i) { - if((strstr(mount_list[i], "/dev/block/loop") != NULL)) { + if(strstr(mount_list[i], "/dev/block/loop")) { sscanf(mount_list[i], "%256s %256s", mountpoint, mountpoint); - if (strstr(mountpoint, "/.core/dummy") != NULL) - unmount = 0; - else - unmount = 1; - } else if ((sbstr = strstr(mount_list[i], "/.core/dummy")) != NULL) { - sscanf(sbstr, "/.core/dummy%256s", mountpoint); - unmount = 1; - } - if(unmount) { - unmount = 0; - res = umount2(mountpoint, MNT_DETACH); - if (res != -1) printf("Unmounted: %s\n", mountpoint); - else printf("Failed: %s\n", mountpoint); + if (!strstr(mountpoint, "/dev/magisk/dummy")) + lazy_unmount(mountpoint); + } else if ((sbstr = strstr(mount_list[i], "/dev/magisk/dummy"))) { + sscanf(sbstr, "/dev/magisk/dummy%256s", mountpoint); + lazy_unmount(mountpoint); } free(mount_list[i]); } @@ -96,36 +90,89 @@ int hideMagisk(int pid) { return 0; } -int main(int argc, char **argv, char **envp) { - if (argc != 2) { - fprintf(stderr, "%s \n", argv[0]); - return 1; - } - int i, hide_size; - char **hide_list; - - FILE *hide_fp = fopen(argv[1], "r"); +void update_list(const char *listpath) { + FILE *hide_fp = fopen((char*) listpath, "r"); if (hide_fp == NULL) { - fprintf(stderr, "Error opening hide list\n"); - return 1; + fprintf(logfile, "MagiskHide: Error opening hide list\n"); + exit(1); + } + pthread_mutex_lock(&mutex); + if (hide_list) { + // Free memory + for(i = 0; i < list_size; ++i) + free(hide_list[i]); + free(hide_list); + } + hide_list = file_to_str_arr(hide_fp, &list_size); + pthread_mutex_unlock(&mutex); + fclose(hide_fp); + fprintf(logfile, "MagiskHide: Update process/package list:\n"); + for(i = 0; i < list_size; i++) + fprintf(logfile, "MagiskHide: %s\n", hide_list[i]); +} + +void quit_pthread(int sig) { + // Free memory + for(i = 0; i < list_size; ++i) + free(hide_list[i]); + free(hide_list); + pthread_exit(NULL); +} + +void *monitor_list(void *listpath) { + signal(SIGQUIT, quit_pthread); + // Initial load + update_list((char*) listpath); + + int inotifyFd; + char buffer[512]; + + inotifyFd = inotify_init(); + if (inotifyFd == -1) + exit(1); + if (inotify_add_watch(inotifyFd, (char*) listpath, IN_ATTRIB | IN_MODIFY) == -1) + exit(1); + + while(1) { + if (read(inotifyFd, buffer, 512) == -1) + exit(1); + update_list((char*) listpath); } - hide_list = file_to_str_arr(hide_fp, &hide_size); - fclose(hide_fp); + return NULL; +} - printf("Get process / package name from config:\n"); - for(i = 0; i < hide_size; i++) - printf("%s\n", hide_list[i]); - printf("\n"); +int main(int argc, char **argv, char **envp) { - char buffer[512]; - 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 - fgets(buffer, sizeof(buffer), p); + pid_t forkpid = fork(); + + if (forkpid < 0) + return 1; + + if (forkpid == 0) { + if (setsid() < 0) + return 1; + + close(STDIN_FILENO); + close(STDOUT_FILENO); + close(STDERR_FILENO); + + logfile = fopen("/cache/magisk.log", "a+"); + setbuf(logfile, NULL); + + pthread_t list_monitor, unmount_thread; + + pthread_mutex_init(&mutex, NULL); + pthread_create(&list_monitor, NULL, monitor_list, "/magisk/.core/magiskhide/hidelist"); + + char buffer[512]; + 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 + fgets(buffer, sizeof(buffer), p); - { char *pos = buffer; while(1) { pos = strchr(pos, ','); @@ -133,33 +180,44 @@ int main(int argc, char **argv, char **envp) { break; pos[0] = ' '; } - } - int user, pid, uid; - char processName[256], hostingType[16], hostingName[256]; - int ret = sscanf(buffer, "[%d %d %d %256s %16s %256s]", - &user, &pid, &uid, - processName, hostingType, hostingName); + int user, pid, uid; + char processName[256], hostingType[16], hostingName[256]; + int ret = sscanf(buffer, "[%d %d %d %256s %16s %256s]", + &user, &pid, &uid, + processName, hostingType, hostingName); + if(ret != 6) + continue; - if(ret != 6) { - continue; - } - for (i = 0; i < hide_size; ++i) { - if(strstr(processName, hide_list[i]) != NULL) { - printf("Disabling for process = %s, PID = %d, UID = %d\n", processName, pid, uid); - hideMagisk(pid); - break; + for (i = 0; i < list_size; ++i) { + if(strstr(processName, hide_list[i])) { + fprintf(logfile, "MagiskHide: Disabling for process = %s, PID = %d, UID = %d\n", processName, pid, uid); + forkpid = fork(); + if (forkpid < 0) + break; + if (forkpid == 0) { + hideMagisk(pid); + return 0; + } + wait(&i); + kill(forkpid, SIGTERM); + break; + } } } + + pclose(p); + + pthread_kill(list_monitor, SIGQUIT); + pthread_mutex_destroy(&mutex); + + fprintf(logfile, "MagiskHide: Error occurred...\n"); + + fclose(logfile); + + return 1; } - pclose(p); - - // Free memory - for(i = 0; i < hide_size; ++i) - free(hide_list[i]); - free(hide_list); - return 0; } diff --git a/scripts/magic_mask.sh b/scripts/magic_mask.sh index b1d08e874..3a92ed5b8 100644 --- a/scripts/magic_mask.sh +++ b/scripts/magic_mask.sh @@ -21,8 +21,8 @@ export OLDPATH=$PATH log_print() { - echo $1 - echo $1 >> $LOGFILE + echo "$1" + echo "$1" >> $LOGFILE log -p i -t Magisk "$1" } @@ -224,7 +224,7 @@ case $1 in touch $LOGFILE chmod 644 $LOGFILE - log_print "Magisk post-fs mode running..." + log_print "** Magisk post-fs mode running..." # No more cache mods! # Only for multirom! @@ -246,7 +246,7 @@ case $1 in # Don't run twice if [ "$(getprop magisk.restart_pfsd)" != "1" ]; then - log_print "Magisk post-fs-data mode running..." + log_print "** Magisk post-fs-data mode running..." # Live patch sepolicy /data/magisk/sepolicy-inject --live -s su @@ -397,24 +397,18 @@ case $1 in service ) # Version info MAGISK_VERSION_STUB - log_print "Magisk late_start service mode running..." + log_print "** Magisk late_start service mode running..." run_scripts service - [ -f "$COREDIR/magiskhide/enable" ] && setprop magisk.hide 1 - ;; - hide ) - # Enable magiskhide - [ ! -f "$COREDIR/magiskhide/hidelist" ] && mktouch $COREDIR/magiskhide/hidelist - # Add preset for Safety Net - if [ $(grep -c "com.google.android.gms.unstable" $COREDIR/magiskhide/hidelist) -eq "0" ]; then - mv $COREDIR/magiskhide/hidelist $COREDIR/magiskhide/hidelist.tmp - echo "com.google.android.gms.unstable" > $COREDIR/magiskhide/hidelist - cat $COREDIR/magiskhide/hidelist.tmp >> $COREDIR/magiskhide/hidelist - rm -f $COREDIR/magiskhide/hidelist.tmp + # MagiskHide + if [ -f "$COREDIR/magiskhide/enable" ]; then + [ ! -f "$COREDIR/magiskhide/hidelist" ] && mktouch $COREDIR/magiskhide/hidelist + chmod -R 755 $COREDIR/magiskhide + # Add Safety Net preset + $COREDIR/magiskhide/add com.google.android.gms.unstable + log_print "** Starting Magisk Hide" + /data/magisk/magiskhide fi - chmod 755 $COREDIR/magiskhide $COREDIR/magiskhide/* - log_print "Starting Magisk Hide" - exec /data/magisk/magiskhide $COREDIR/magiskhide/hidelist ;; esac diff --git a/zip_static/common/magiskhide/add b/zip_static/common/magiskhide/add index 1b87dde52..0a7677ff2 100644 --- a/zip_static/common/magiskhide/add +++ b/zip_static/common/magiskhide/add @@ -1,12 +1,9 @@ #!/system/bin/sh -HIDELIST=$MOUNTPOINT/.core/magiskhide/hidelist +HIDELIST=/magisk/.core/magiskhide/hidelist if [ ! -z "$1" ]; then if [ $(grep -c "^$1$" $HIDELIST) -eq "0" ]; then echo "$1" >> $HIDELIST fi fi - -# Reload the list -setprop magisk.hide 1 diff --git a/zip_static/common/magiskhide/list b/zip_static/common/magiskhide/list index 10a8f5cf2..a1dcbb7c2 100644 --- a/zip_static/common/magiskhide/list +++ b/zip_static/common/magiskhide/list @@ -1,4 +1,5 @@ #!/system/bin/sh -HIDELIST=$MOUNTPOINT/.core/magiskhide/hidelist +HIDELIST=/magisk/.core/magiskhide/hidelist + cat $HIDELIST diff --git a/zip_static/common/magiskhide/rm b/zip_static/common/magiskhide/rm index 2eb8b3b4b..5dfc84d07 100644 --- a/zip_static/common/magiskhide/rm +++ b/zip_static/common/magiskhide/rm @@ -1,12 +1,9 @@ #!/system/bin/sh -HIDELIST=$MOUNTPOINT/.core/magiskhide/hidelist +HIDELIST=/magisk/.core/magiskhide/hidelist if [ ! -z "$1" ]; then mv $HIDELIST $HIDELIST.tmp cat $HIDELIST.tmp | grep -v "^$1$" > $HIDELIST rm -f $HIDELIST.tmp fi - -# Reload the list -setprop magisk.hide 1