Magisk/jni/magiskhide/magiskhide.c

149 lines
3.1 KiB
C
Raw Normal View History

2017-04-05 22:12:29 +00:00
/* magiskhide.c - initialize the environment for Magiskhide
*/
2016-12-30 18:44:24 +00:00
2017-04-05 22:12:29 +00:00
// TODO: Functions to modify hiding list
2017-04-05 22:12:29 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
2017-04-06 23:50:02 +00:00
#include <signal.h>
#include <string.h>
#include <sys/wait.h>
2017-04-06 23:50:02 +00:00
#include <sys/types.h>
2016-12-30 18:44:24 +00:00
2017-04-05 22:12:29 +00:00
#include "magisk.h"
#include "utils.h"
#include "magiskhide.h"
#include "daemon.h"
2017-04-20 14:45:56 +00:00
#include "resetprop.h"
2016-12-30 18:44:24 +00:00
int sv[2], hide_pid = -1;
2017-04-20 14:45:56 +00:00
struct vector *hide_list = NULL;
2016-12-30 18:44:24 +00:00
2017-04-20 14:45:56 +00:00
int hideEnabled = 0;
2017-04-05 22:12:29 +00:00
static pthread_t proc_monitor_thread;
2017-04-20 14:45:56 +00:00
pthread_mutex_t lock;
2017-01-01 10:54:13 +00:00
void kill_proc(int pid) {
2017-04-06 23:50:02 +00:00
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 <process name>: Add <process name> to the list\n"
" --rm <process name>: Remove <process name> from the list\n"
" --ls: Print out the current hide list\n"
, arg0);
exit(1);
}
void launch_magiskhide(int client) {
2017-04-20 14:45:56 +00:00
if (hideEnabled)
2017-04-18 11:32:50 +00:00
goto success;
2017-04-05 22:12:29 +00:00
/*
* The setns system call do not support multithread processes
* We have to fork a new process, and communicate with pipe
*/
2016-12-30 18:44:24 +00:00
LOGI("* Starting MagiskHide\n");
2017-04-20 14:45:56 +00:00
hideEnabled = 1;
setprop("persist.magisk.hide", "1");
2017-04-17 08:36:49 +00:00
hide_sensitive_props();
if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sv) == -1)
goto error;
2016-12-30 18:44:24 +00:00
2017-04-05 22:12:29 +00:00
// Launch the hide daemon
if (hide_daemon())
goto error;
2016-12-30 18:44:24 +00:00
close(sv[1]);
2016-12-30 18:44:24 +00:00
2017-04-05 22:12:29 +00:00
// Initialize the hide list
2017-04-20 14:45:56 +00:00
if (init_list())
goto error;
2017-04-20 14:45:56 +00:00
// Add SafetyNet by default
add_list(strdup("com.google.android.gms.unstable"));
2016-12-30 18:44:24 +00:00
2017-04-05 22:12:29 +00:00
// Start a new thread to monitor processes
2017-04-20 14:45:56 +00:00
pthread_mutex_init(&lock, NULL);
if (xpthread_create(&proc_monitor_thread, NULL, proc_monitor, NULL))
goto error;
2017-04-18 11:32:50 +00:00
success:
write_int(client, 0);
2017-04-14 19:23:09 +00:00
close(client);
return;
error:
2017-04-20 14:45:56 +00:00
hideEnabled = 0;
write_int(client, 1);
2017-04-14 19:23:09 +00:00
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);
2017-04-17 08:36:49 +00:00
hide_pid = -1;
}
return;
2017-04-05 22:12:29 +00:00
}
2016-12-30 18:44:24 +00:00
void stop_magiskhide(int client) {
2017-04-20 14:45:56 +00:00
if (!hideEnabled)
return;
LOGI("* Stopping MagiskHide\n");
2017-04-05 22:12:29 +00:00
pthread_kill(proc_monitor_thread, SIGUSR1);
pthread_join(proc_monitor_thread, NULL);
2017-04-20 14:45:56 +00:00
hideEnabled = 0;
setprop("persist.magisk.hide", "0");
write_int(client, 0);
2017-04-14 19:23:09 +00:00
close(client);
2017-04-05 22:12:29 +00:00
}
2016-12-30 18:44:24 +00:00
2017-04-05 22:12:29 +00:00
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");
2017-04-14 19:23:09 +00:00
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;
2017-04-05 22:12:29 +00:00
}