Magisk/native/jni/magiskhide/magiskhide.cpp

190 lines
4.1 KiB
C++
Raw Normal View History

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"
#include "flags.h"
2016-12-30 18:44:24 +00:00
2018-11-01 17:23:12 +00:00
int hide_enabled = 0;
2017-04-05 22:12:29 +00:00
static pthread_t proc_monitor_thread;
2018-11-01 17:23:12 +00:00
pthread_mutex_t list_lock;
2017-01-01 10:54:13 +00:00
[[noreturn]] static void usage(char *arg0) {
fprintf(stderr,
"MagiskHide v" xstr(MAGISK_VERSION) "(" xstr(MAGISK_VER_CODE) ") (by topjohnwu) - Hide Magisk!\n\n"
2017-08-14 17:25:54 +00:00
"Usage: %s [--options [arguments...] ]\n\n"
"Options:\n"
2017-08-14 17:25:54 +00:00
" --enable Start magiskhide\n"
" --disable Stop magiskhide\n"
" --add PROCESS Add PROCESS to the hide list\n"
" --rm PROCESS Remove PROCESS from the hide list\n"
" --ls Print out the current hide list\n"
, arg0);
exit(1);
}
int launch_magiskhide() {
if (hide_enabled)
return HIDE_IS_ENABLED;
2016-12-30 18:44:24 +00:00
2018-10-12 04:50:47 +00:00
if (!log_daemon_started) {
setprop(MAGISKHIDE_PROP, "0");
// Remove without actually removing persist props
2018-11-04 08:38:06 +00:00
deleteprop(MAGISKHIDE_PROP);
return LOGCAT_DISABLED;
2018-02-11 18:48:15 +00:00
}
2018-11-01 17:23:12 +00:00
hide_enabled = 1;
2017-07-02 15:04:57 +00:00
LOGI("* Starting MagiskHide\n");
2017-04-24 13:43:30 +00:00
2018-11-04 08:38:06 +00:00
deleteprop(MAGISKHIDE_PROP, true);
2017-04-20 14:45:56 +00:00
2017-04-17 08:36:49 +00:00
hide_sensitive_props();
2017-05-12 07:28:15 +00:00
// Initialize the mutex lock
2018-11-01 17:23:12 +00:00
pthread_mutex_init(&list_lock, nullptr);
2017-05-12 07:28:15 +00:00
2017-04-05 22:12:29 +00:00
// Initialize the hide list
2018-11-07 07:10:38 +00:00
if (!init_list())
goto error;
2017-04-20 14:45:56 +00:00
// Add SafetyNet by default
2018-11-07 07:10:38 +00:00
add_list("com.google.android.gms.unstable");
2016-12-30 18:44:24 +00:00
2017-04-21 16:54:08 +00:00
// Get thread reference
proc_monitor_thread = pthread_self();
// Start monitoring
proc_monitor();
return DAEMON_SUCCESS;
2017-04-21 16:54:08 +00:00
error:
2018-11-01 17:23:12 +00:00
hide_enabled = 0;
return DAEMON_ERROR;
2017-04-05 22:12:29 +00:00
}
2016-12-30 18:44:24 +00:00
int stop_magiskhide() {
LOGI("* Stopping MagiskHide\n");
2018-11-01 17:23:12 +00:00
hide_enabled = 0;
2017-05-26 14:05:04 +00:00
setprop(MAGISKHIDE_PROP, "0");
2017-07-18 04:26:23 +00:00
// Remove without actually removing persist props
2018-11-04 08:38:06 +00:00
deleteprop(MAGISKHIDE_PROP);
pthread_kill(proc_monitor_thread, TERM_THREAD);
2017-04-21 16:54:08 +00:00
return DAEMON_SUCCESS;
}
void magiskhide_handler(int client) {
int req = read_int(client);
int res = DAEMON_ERROR;
switch (req) {
case STOP_MAGISKHIDE:
case ADD_HIDELIST:
case RM_HIDELIST:
case LS_HIDELIST:
if (!hide_enabled) {
write_int(client, HIDE_NOT_ENABLED);
close(client);
return;
}
}
switch (req) {
case LAUNCH_MAGISKHIDE:
res = launch_magiskhide();
break;
case STOP_MAGISKHIDE:
res = stop_magiskhide();
break;
case ADD_HIDELIST:
res = add_list(client);
break;
case RM_HIDELIST:
res = rm_list(client);
break;
case LS_HIDELIST:
ls_list(client);
client = -1;
break;
}
write_int(client, res);
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]);
}
int 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) {
req = LS_HIDELIST;
2017-10-10 11:49:15 +00:00
} else {
usage(argv[0]);
}
2018-06-16 17:28:29 +00:00
int fd = connect_daemon();
write_int(fd, MAGISKHIDE);
write_int(fd, req);
if (req == ADD_HIDELIST || req == RM_HIDELIST) {
write_string(fd, argv[2]);
}
2018-02-11 09:23:36 +00:00
int code = read_int(fd);
2017-04-21 17:40:07 +00:00
switch (code) {
2017-05-05 08:13:26 +00:00
case DAEMON_SUCCESS:
break;
case ROOT_REQUIRED:
fprintf(stderr, "Root is required for this operation\n");
return code;
2018-04-07 18:12:40 +00:00
case LOGCAT_DISABLED:
fprintf(stderr, "Logcat is disabled, cannot start MagiskHide\n");
return code;
2017-04-21 17:40:07 +00:00
case HIDE_NOT_ENABLED:
2018-04-07 18:12:40 +00:00
fprintf(stderr, "MagiskHide is not enabled yet\n");
return code;
2017-04-21 17:40:07 +00:00
case HIDE_IS_ENABLED:
2018-04-07 18:12:40 +00:00
fprintf(stderr, "MagiskHide is already enabled\n");
return code;
2017-04-21 17:40:07 +00:00
case HIDE_ITEM_EXIST:
fprintf(stderr, "Process [%s] already exists in hide list\n", argv[2]);
return code;
2017-04-21 17:40:07 +00:00
case HIDE_ITEM_NOT_EXIST:
fprintf(stderr, "Process [%s] does not exist in hide list\n", argv[2]);
return code;
2018-02-11 09:23:36 +00:00
case DAEMON_ERROR:
default:
fprintf(stderr, "Error occured in daemon...\n");
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;
2017-04-05 22:12:29 +00:00
}