From eceba2689433d5ec44c0d613e0a2fafd04b43260 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Wed, 6 Sep 2017 02:25:40 +0800 Subject: [PATCH] Make MagiskHide work without magisk.img --- jni/daemon/bootstages.c | 2 +- jni/daemon/daemon.c | 4 ++++ jni/include/daemon.h | 2 ++ jni/magiskhide/hide_utils.c | 29 ++++++++++++++++++++--------- jni/magiskhide/magiskhide.c | 36 +++++++++++++++++++----------------- jni/utils/misc.c | 2 +- 6 files changed, 47 insertions(+), 28 deletions(-) diff --git a/jni/daemon/bootstages.c b/jni/daemon/bootstages.c index 289e30d70..06b783a90 100644 --- a/jni/daemon/bootstages.c +++ b/jni/daemon/bootstages.c @@ -589,7 +589,7 @@ void post_fs_data(int client) { // Trim, mount magisk.img, which will also travel through the modules // After this, it will create the module list if (prepare_img()) - goto unblock; + goto core_only; // Mounting fails, we can only do core only stuffs // Run common scripts LOGI("* Running post-fs-data.d scripts\n"); diff --git a/jni/daemon/daemon.c b/jni/daemon/daemon.c index 51bcca0db..531fbbaee 100644 --- a/jni/daemon/daemon.c +++ b/jni/daemon/daemon.c @@ -40,6 +40,7 @@ static void *request_handler(void *args) { case STOP_MAGISKHIDE: case ADD_HIDELIST: case RM_HIDELIST: + case LS_HIDELIST: case POST_FS: case POST_FS_DATA: case LATE_START: @@ -65,6 +66,9 @@ static void *request_handler(void *args) { case RM_HIDELIST: rm_hide_list(client); break; + case LS_HIDELIST: + ls_hide_list(client); + break; case SUPERUSER: su_daemon_receiver(client); break; diff --git a/jni/include/daemon.h b/jni/include/daemon.h index c2f1cce96..0a72b1c29 100644 --- a/jni/include/daemon.h +++ b/jni/include/daemon.h @@ -15,6 +15,7 @@ typedef enum { STOP_MAGISKHIDE, ADD_HIDELIST, RM_HIDELIST, + LS_HIDELIST, SUPERUSER, CHECK_VERSION, CHECK_VERSION_CODE, @@ -69,6 +70,7 @@ void launch_magiskhide(int client); void stop_magiskhide(int client); void add_hide_list(int client); void rm_hide_list(int client); +void ls_hide_list(int client); /************* * Superuser * diff --git a/jni/magiskhide/hide_utils.c b/jni/magiskhide/hide_utils.c index 0a169e0d0..e01896aad 100644 --- a/jni/magiskhide/hide_utils.c +++ b/jni/magiskhide/hide_utils.c @@ -19,7 +19,7 @@ #include "daemon.h" static char *prop_key[] = - { "ro.boot.verifiedbootstate", "ro.boot.flash.locked", "ro.boot.veritymode", "ro.boot.warranty_bit", "ro.warranty_bit", + { "ro.boot.verifiedbootstate", "ro.boot.flash.locked", "ro.boot.veritymode", "ro.boot.warranty_bit", "ro.warranty_bit", "ro.debuggable", "ro.secure", "ro.build.type", "ro.build.tags", "ro.build.selinux", NULL }; static char *prop_value[] = @@ -72,7 +72,7 @@ void clean_magisk_props() { void relink_sbin() { struct stat st; if (stat("/sbin_orig", &st) == -1 && errno == ENOENT) { - // Re-link all binaries and bind mount + // Re-link all binaries and bind mount DIR *dir; struct dirent *entry; char from[PATH_MAX], to[PATH_MAX]; @@ -98,7 +98,7 @@ void relink_sbin() { symlink(from, to); lsetfilecon(to, "u:object_r:rootfs:s0"); } - + closedir(dir); xmount("/dev/sbin_bind", "/sbin", NULL, MS_BIND, NULL); @@ -140,10 +140,7 @@ int add_list(char *proc) { pthread_mutex_unlock(&hide_lock); pthread_mutex_lock(&file_lock); - if (vector_to_file(HIDELIST, hide_list)) { - pthread_mutex_unlock(&file_lock); - return DAEMON_ERROR; - } + vector_to_file(HIDELIST, hide_list); // Do not complain if file not found pthread_mutex_unlock(&file_lock); return DAEMON_SUCCESS; } @@ -184,8 +181,7 @@ int rm_list(char *proc) { ret = DAEMON_SUCCESS; pthread_mutex_lock(&file_lock); - if (vector_to_file(HIDELIST, hide_list)) - ret = DAEMON_ERROR; + vector_to_file(HIDELIST, hide_list); // Do not complain if file not found pthread_mutex_unlock(&file_lock); } else { ret = HIDE_ITEM_NOT_EXIST; @@ -241,3 +237,18 @@ void rm_hide_list(int client) { write_int(client, rm_list(proc)); close(client); } + +void ls_hide_list(int client) { + err_handler = do_nothing; + if (!hideEnabled) { + write_int(client, HIDE_NOT_ENABLED); + return; + } + write_int(client, DAEMON_SUCCESS); + write_int(client, vec_size(hide_list)); + char *s; + vec_for_each(hide_list, s) { + write_string(client, s); + } + close(client); +} diff --git a/jni/magiskhide/magiskhide.c b/jni/magiskhide/magiskhide.c index d93b29819..272e4daec 100644 --- a/jni/magiskhide/magiskhide.c +++ b/jni/magiskhide/magiskhide.c @@ -123,15 +123,7 @@ int magiskhide_main(int argc, char *argv[]) { } else if (strcmp(argv[1], "--rm") == 0 && argc > 2) { req = RM_HIDELIST; } else if (strcmp(argv[1], "--ls") == 0) { - FILE *fp = fopen(HIDELIST, "r"); - if (fp == NULL) - return 1; - char buffer[512]; - while (fgets(buffer, sizeof(buffer), fp)) { - printf("%s", buffer); - } - fclose(fp); - return 0; + req = LS_HIDELIST; } int fd = connect_daemon(); write_int(fd, req); @@ -139,28 +131,38 @@ int magiskhide_main(int argc, char *argv[]) { write_string(fd, argv[2]); } daemon_response code = read_int(fd); - close(fd); switch (code) { case DAEMON_ERROR: fprintf(stderr, "Error occured in daemon...\n"); - break; + return code; case DAEMON_SUCCESS: break; case ROOT_REQUIRED: fprintf(stderr, "Root is required for this operation\n"); - break; + return code; case HIDE_NOT_ENABLED: fprintf(stderr, "Magisk hide is not enabled yet\n"); - break; + return code; case HIDE_IS_ENABLED: fprintf(stderr, "Magisk hide is already enabled\n"); - break; + return code; case HIDE_ITEM_EXIST: fprintf(stderr, "Process [%s] already exists in hide list\n", argv[2]); - break; + return code; case HIDE_ITEM_NOT_EXIST: fprintf(stderr, "Process [%s] does not exist in hide list\n", argv[2]); - break; + return code; } - return code; + + if (req == LS_HIDELIST) { + int argc = read_int(fd); + for (int i = 0; i < argc; ++i) { + char *s = read_string(fd); + printf("%s\n", s); + free(s); + } + } + close(fd); + + return 0; } diff --git a/jni/utils/misc.c b/jni/utils/misc.c index 9721f5eab..22ff80138 100644 --- a/jni/utils/misc.c +++ b/jni/utils/misc.c @@ -70,7 +70,7 @@ int file_to_vector(const char* filename, struct vector *v) { size_t len = 0; ssize_t read; - FILE *fp = fopen(filename, "r"); + FILE *fp = xfopen(filename, "r"); if (fp == NULL) return 1;