/* magiskhide.c - initialize the environment for Magiskhide */ // TODO: Functions to modify hiding list #include #include #include #include #include #include #include #include #include "magisk.h" #include "utils.h" #include "magiskhide.h" #include "daemon.h" #include "resetprop.h" int sv[2], hide_pid = -1; struct vector *hide_list = NULL; int hideEnabled = 0; static pthread_t proc_monitor_thread; pthread_mutex_t lock; void kill_proc(int pid) { kill(pid, SIGTERM); } static void usage(char *arg0) { fprintf(stderr, "%s [--options [arguments...] ]\n\n" "Options:\n" " --enable: Start the magiskhide daemon\n" " --disable: Stop the magiskhide daemon\n" " --add : Add to the list\n" " --rm : Remove from the list\n" " --ls: Print out the current hide list\n" , arg0); exit(1); } void launch_magiskhide(int client) { if (hideEnabled) goto success; /* * The setns system call do not support multithread processes * We have to fork a new process, and communicate with pipe */ LOGI("* Starting MagiskHide\n"); hideEnabled = 1; setprop("persist.magisk.hide", "1"); hide_sensitive_props(); if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sv) == -1) goto error; // Launch the hide daemon if (hide_daemon()) goto error; close(sv[1]); // Initialize the hide list if (init_list()) goto error; // Add SafetyNet by default add_list(strdup("com.google.android.gms.unstable")); // Start a new thread to monitor processes pthread_mutex_init(&lock, NULL); if (xpthread_create(&proc_monitor_thread, NULL, proc_monitor, NULL)) goto error; success: write_int(client, 0); close(client); return; error: hideEnabled = 0; write_int(client, 1); close(client); if (hide_pid != -1) { int kill = -1; // Kill hide daemon write(sv[0], &kill, sizeof(kill)); close(sv[0]); waitpid(hide_pid, NULL, 0); hide_pid = -1; } return; } void stop_magiskhide(int client) { if (!hideEnabled) return; LOGI("* Stopping MagiskHide\n"); pthread_kill(proc_monitor_thread, SIGUSR1); pthread_join(proc_monitor_thread, NULL); hideEnabled = 0; setprop("persist.magisk.hide", "0"); write_int(client, 0); close(client); } int magiskhide_main(int argc, char *argv[]) { if (argc < 2) { usage(argv[0]); } client_request req; if (strcmp(argv[1], "--enable") == 0) { req = LAUNCH_MAGISKHIDE; } else if (strcmp(argv[1], "--disable") == 0) { req = STOP_MAGISKHIDE; } else if (strcmp(argv[1], "--add") == 0 && argc > 2) { req = ADD_HIDELIST; } else if (strcmp(argv[1], "--rm") == 0 && argc > 2) { req = RM_HIDELIST; } else if (strcmp(argv[1], "--ls") == 0) { FILE *fp = xfopen(HIDELIST, "r"); char buffer[512]; while (fgets(buffer, sizeof(buffer), fp)) { printf("%s", buffer); } fclose(fp); return 0; } int fd = connect_daemon(); write_int(fd, req); if (req == ADD_HIDELIST || req == RM_HIDELIST) { write_string(fd, argv[2]); } int code = read_int(fd); close(fd); if (code) { fprintf(stderr, "Error occured in MagiskHide daemon\n"); } return code; }