2017-04-06 00:12:29 +02:00
|
|
|
/* magiskhide.c - initialize the environment for Magiskhide
|
|
|
|
*/
|
2016-12-30 19:44:24 +01:00
|
|
|
|
2017-04-06 00:12:29 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <pthread.h>
|
2017-04-07 01:50:02 +02:00
|
|
|
#include <signal.h>
|
2017-04-09 01:25:10 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/wait.h>
|
2017-04-07 01:50:02 +02:00
|
|
|
#include <sys/types.h>
|
2016-12-30 19:44:24 +01:00
|
|
|
|
2017-04-06 00:12:29 +02:00
|
|
|
#include "magisk.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "magiskhide.h"
|
2017-04-09 01:25:10 +02:00
|
|
|
#include "daemon.h"
|
2017-04-20 16:45:56 +02:00
|
|
|
#include "resetprop.h"
|
2016-12-30 19:44:24 +01:00
|
|
|
|
2017-04-20 16:45:56 +02:00
|
|
|
struct vector *hide_list = NULL;
|
2016-12-30 19:44:24 +01:00
|
|
|
|
2017-04-20 16:45:56 +02:00
|
|
|
int hideEnabled = 0;
|
2017-04-06 00:12:29 +02:00
|
|
|
static pthread_t proc_monitor_thread;
|
2017-05-03 20:58:37 +02:00
|
|
|
pthread_mutex_t hide_lock, file_lock;
|
2017-01-01 11:54:13 +01:00
|
|
|
|
2017-04-09 01:25:10 +02:00
|
|
|
void kill_proc(int pid) {
|
2017-04-07 01:50:02 +02:00
|
|
|
kill(pid, SIGTERM);
|
|
|
|
}
|
|
|
|
|
2017-04-09 01:25:10 +02:00
|
|
|
static void usage(char *arg0) {
|
|
|
|
fprintf(stderr,
|
2017-07-08 17:51:58 +02:00
|
|
|
"MagiskHide v" xstr(MAGISK_VERSION) "(" xstr(MAGISK_VER_CODE) ") (by topjohnwu) - Hide Magisk!\n\n"
|
2017-08-14 19:25:54 +02:00
|
|
|
"Usage: %s [--options [arguments...] ]\n\n"
|
2017-04-09 01:25:10 +02:00
|
|
|
"Options:\n"
|
2017-08-14 19:25:54 +02: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"
|
2017-04-09 01:25:10 +02:00
|
|
|
, arg0);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void launch_magiskhide(int client) {
|
2017-04-21 18:54:08 +02:00
|
|
|
if (hideEnabled) {
|
2017-07-02 17:04:57 +02:00
|
|
|
if (client > 0) {
|
|
|
|
write_int(client, HIDE_IS_ENABLED);
|
|
|
|
close(client);
|
|
|
|
}
|
2017-04-21 18:54:08 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-12-30 19:44:24 +01:00
|
|
|
|
2018-02-11 19:48:15 +01:00
|
|
|
if (!logd) {
|
|
|
|
if (client > 0) {
|
|
|
|
write_int(client, LOGD_DISABLED);
|
|
|
|
close(client);
|
|
|
|
}
|
2018-03-18 05:02:56 +01:00
|
|
|
setprop(MAGISKHIDE_PROP, "0");
|
|
|
|
// Remove without actually removing persist props
|
|
|
|
deleteprop2(MAGISKHIDE_PROP, 0);
|
2018-02-11 19:48:15 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:45:56 +02:00
|
|
|
hideEnabled = 1;
|
2017-07-02 17:04:57 +02:00
|
|
|
LOGI("* Starting MagiskHide\n");
|
2017-04-24 15:43:30 +02:00
|
|
|
|
2017-10-09 20:04:50 +02:00
|
|
|
deleteprop2(MAGISKHIDE_PROP, 1);
|
2017-04-20 16:45:56 +02:00
|
|
|
|
2017-04-17 10:36:49 +02:00
|
|
|
hide_sensitive_props();
|
|
|
|
|
2017-05-12 09:28:15 +02:00
|
|
|
// Initialize the mutex lock
|
|
|
|
pthread_mutex_init(&hide_lock, NULL);
|
|
|
|
pthread_mutex_init(&file_lock, NULL);
|
|
|
|
|
2017-04-06 00:12:29 +02:00
|
|
|
// Initialize the hide list
|
2017-04-20 16:45:56 +02:00
|
|
|
if (init_list())
|
2017-04-09 01:25:10 +02:00
|
|
|
goto error;
|
2017-04-20 16:45:56 +02:00
|
|
|
|
|
|
|
// Add SafetyNet by default
|
|
|
|
add_list(strdup("com.google.android.gms.unstable"));
|
2016-12-30 19:44:24 +01:00
|
|
|
|
2017-07-08 17:51:58 +02:00
|
|
|
if (client > 0) {
|
|
|
|
write_int(client, DAEMON_SUCCESS);
|
|
|
|
close(client);
|
|
|
|
}
|
2017-04-21 18:54:08 +02:00
|
|
|
|
|
|
|
// Get thread reference
|
|
|
|
proc_monitor_thread = pthread_self();
|
|
|
|
// Start monitoring
|
|
|
|
proc_monitor();
|
2017-04-09 01:25:10 +02:00
|
|
|
return;
|
2017-04-21 18:54:08 +02:00
|
|
|
|
2017-04-09 01:25:10 +02:00
|
|
|
error:
|
2017-04-20 16:45:56 +02:00
|
|
|
hideEnabled = 0;
|
2017-07-08 17:51:58 +02:00
|
|
|
if (client > 0) {
|
|
|
|
write_int(client, DAEMON_ERROR);
|
|
|
|
close(client);
|
2017-04-15 20:43:19 +02:00
|
|
|
}
|
2017-04-09 01:25:10 +02:00
|
|
|
return;
|
2017-04-06 00:12:29 +02:00
|
|
|
}
|
2016-12-30 19:44:24 +01:00
|
|
|
|
2017-04-09 01:25:10 +02:00
|
|
|
void stop_magiskhide(int client) {
|
2017-04-21 18:54:08 +02:00
|
|
|
if (!hideEnabled) {
|
2017-04-21 19:40:07 +02:00
|
|
|
write_int(client, HIDE_NOT_ENABLED);
|
2017-04-21 18:54:08 +02:00
|
|
|
close(client);
|
2017-04-09 01:25:10 +02:00
|
|
|
return;
|
2017-04-21 18:54:08 +02:00
|
|
|
}
|
2017-04-09 01:25:10 +02:00
|
|
|
|
|
|
|
LOGI("* Stopping MagiskHide\n");
|
|
|
|
|
2017-04-20 16:45:56 +02:00
|
|
|
hideEnabled = 0;
|
2017-05-26 16:05:04 +02:00
|
|
|
setprop(MAGISKHIDE_PROP, "0");
|
2017-07-18 06:26:23 +02:00
|
|
|
// Remove without actually removing persist props
|
2017-10-09 20:04:50 +02:00
|
|
|
deleteprop2(MAGISKHIDE_PROP, 0);
|
2017-11-08 20:05:01 +01:00
|
|
|
pthread_kill(proc_monitor_thread, TERM_THREAD);
|
2017-04-21 18:54:08 +02:00
|
|
|
|
2017-05-05 10:13:26 +02:00
|
|
|
write_int(client, DAEMON_SUCCESS);
|
2017-04-14 21:23:09 +02:00
|
|
|
close(client);
|
2017-04-06 00:12:29 +02:00
|
|
|
}
|
2016-12-30 19:44:24 +01:00
|
|
|
|
2017-04-06 00:12:29 +02:00
|
|
|
int magiskhide_main(int argc, char *argv[]) {
|
2017-04-09 01:25:10 +02:00
|
|
|
if (argc < 2) {
|
|
|
|
usage(argv[0]);
|
|
|
|
}
|
2018-02-11 10:23:36 +01:00
|
|
|
int req = DO_NOTHING;
|
2017-04-09 01:25:10 +02:00
|
|
|
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) {
|
2017-09-05 20:25:40 +02:00
|
|
|
req = LS_HIDELIST;
|
2017-10-10 13:49:15 +02:00
|
|
|
} else {
|
|
|
|
usage(argv[0]);
|
2017-04-09 01:25:10 +02:00
|
|
|
}
|
|
|
|
int fd = connect_daemon();
|
|
|
|
write_int(fd, req);
|
|
|
|
if (req == ADD_HIDELIST || req == RM_HIDELIST) {
|
|
|
|
write_string(fd, argv[2]);
|
|
|
|
}
|
2018-02-11 10:23:36 +01:00
|
|
|
int code = read_int(fd);
|
2017-04-21 19:40:07 +02:00
|
|
|
switch (code) {
|
2017-05-05 10:13:26 +02:00
|
|
|
case DAEMON_SUCCESS:
|
|
|
|
break;
|
|
|
|
case ROOT_REQUIRED:
|
|
|
|
fprintf(stderr, "Root is required for this operation\n");
|
2017-09-05 20:25:40 +02:00
|
|
|
return code;
|
2018-02-11 19:48:15 +01:00
|
|
|
case LOGD_DISABLED:
|
|
|
|
fprintf(stderr, "Logd is not running, cannot run logcat\n");
|
|
|
|
return (code);
|
2017-04-21 19:40:07 +02:00
|
|
|
case HIDE_NOT_ENABLED:
|
|
|
|
fprintf(stderr, "Magisk hide is not enabled yet\n");
|
2017-09-05 20:25:40 +02:00
|
|
|
return code;
|
2017-04-21 19:40:07 +02:00
|
|
|
case HIDE_IS_ENABLED:
|
|
|
|
fprintf(stderr, "Magisk hide is already enabled\n");
|
2017-09-05 20:25:40 +02:00
|
|
|
return code;
|
2017-04-21 19:40:07 +02:00
|
|
|
case HIDE_ITEM_EXIST:
|
|
|
|
fprintf(stderr, "Process [%s] already exists in hide list\n", argv[2]);
|
2017-09-05 20:25:40 +02:00
|
|
|
return code;
|
2017-04-21 19:40:07 +02:00
|
|
|
case HIDE_ITEM_NOT_EXIST:
|
|
|
|
fprintf(stderr, "Process [%s] does not exist in hide list\n", argv[2]);
|
2017-09-05 20:25:40 +02:00
|
|
|
return code;
|
2018-02-11 10:23:36 +01:00
|
|
|
case DAEMON_ERROR:
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "Error occured in daemon...\n");
|
|
|
|
return code;
|
2017-09-05 20:25:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2017-04-09 01:25:10 +02:00
|
|
|
}
|
2017-09-05 20:25:40 +02:00
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return 0;
|
2017-04-06 00:12:29 +02:00
|
|
|
}
|