2016-09-27 00:08:18 +02:00
|
|
|
#define _GNU_SOURCE
|
2016-10-03 22:24:19 +02:00
|
|
|
#include <string.h>
|
2016-09-27 00:08:18 +02:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
2016-10-03 22:24:19 +02:00
|
|
|
#include <stdlib.h>
|
2016-11-01 04:21:43 +08:00
|
|
|
#include <sched.h>
|
|
|
|
#include <pthread.h>
|
2016-10-03 22:24:19 +02:00
|
|
|
#include <unistd.h>
|
2016-11-01 04:21:43 +08:00
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/inotify.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
2016-11-07 23:57:21 +08:00
|
|
|
#define LOGFILE "/cache/magisk.log"
|
|
|
|
#define HIDELIST "/magisk/.core/magiskhide/hidelist"
|
|
|
|
|
2016-11-01 04:21:43 +08:00
|
|
|
FILE *logfile;
|
|
|
|
int i, list_size;
|
|
|
|
char **hide_list = NULL;
|
|
|
|
pthread_mutex_t mutex;
|
2016-10-06 02:17:21 +08:00
|
|
|
|
2016-10-15 05:18:16 +08:00
|
|
|
char **file_to_str_arr(FILE *fp, int *size) {
|
|
|
|
int allocated = 16;
|
|
|
|
char *line = NULL, **array;
|
|
|
|
size_t len = 0;
|
|
|
|
ssize_t read;
|
|
|
|
|
|
|
|
array = (char **) malloc(sizeof(char*) * allocated);
|
2016-10-06 17:53:52 +08:00
|
|
|
|
2016-10-15 05:18:16 +08:00
|
|
|
*size = 0;
|
|
|
|
while ((read = getline(&line, &len, fp)) != -1) {
|
|
|
|
if (*size >= allocated) {
|
|
|
|
// Double our allocation and re-allocate
|
|
|
|
allocated *= 2;
|
|
|
|
array = (char **) realloc(array, sizeof(char*) * allocated);
|
|
|
|
}
|
|
|
|
// Remove end newline
|
|
|
|
if (line[read - 1] == '\n') {
|
|
|
|
line[read - 1] = '\0';
|
|
|
|
}
|
|
|
|
array[*size] = line;
|
|
|
|
line = NULL;
|
|
|
|
++(*size);
|
|
|
|
}
|
|
|
|
return array;
|
|
|
|
}
|
2016-09-27 00:08:18 +02:00
|
|
|
|
2016-11-01 04:21:43 +08:00
|
|
|
void lazy_unmount(const char* mountpoint) {
|
|
|
|
if (umount2(mountpoint, MNT_DETACH) != -1)
|
|
|
|
fprintf(logfile, "MagiskHide: Unmounted (%s)\n", mountpoint);
|
|
|
|
else
|
|
|
|
fprintf(logfile, "MagiskHide: Unmount Failed (%s)\n", mountpoint);
|
|
|
|
}
|
|
|
|
|
2016-10-03 22:24:19 +02:00
|
|
|
//WARNING: Calling this will change our current namespace
|
|
|
|
//We don't care because we don't want to run from here anyway
|
2016-10-06 02:17:21 +08:00
|
|
|
int hideMagisk(int pid) {
|
2016-10-03 22:24:19 +02:00
|
|
|
char *path = NULL;
|
|
|
|
asprintf(&path, "/proc/%d/ns/mnt", pid);
|
|
|
|
int fd = open(path, O_RDONLY);
|
2016-11-06 02:09:14 +08:00
|
|
|
if(fd == -1) return 2; // Maybe process died..
|
|
|
|
if(setns(fd, 0) == -1) {
|
|
|
|
fprintf(logfile, "Unable to change namespace for pid=%d\n", pid);
|
|
|
|
return 3;
|
|
|
|
}
|
2016-09-27 00:08:18 +02:00
|
|
|
|
2016-10-19 06:22:00 +08:00
|
|
|
free(path);
|
2016-10-15 05:18:16 +08:00
|
|
|
path = NULL;
|
|
|
|
asprintf(&path, "/proc/%d/mounts", pid);
|
|
|
|
FILE *mount_fp = fopen(path, "r");
|
|
|
|
if (mount_fp == NULL) {
|
2016-11-01 04:21:43 +08:00
|
|
|
fprintf(logfile, "Error opening mount list!\n");
|
2016-11-06 02:09:14 +08:00
|
|
|
return 4;
|
2016-10-06 17:53:52 +08:00
|
|
|
}
|
2016-10-19 06:22:00 +08:00
|
|
|
free(path);
|
2016-10-06 17:53:52 +08:00
|
|
|
|
2016-10-15 05:18:16 +08:00
|
|
|
int mount_size;
|
|
|
|
char **mount_list = file_to_str_arr(mount_fp, &mount_size), mountpoint[256], *sbstr;
|
|
|
|
fclose(mount_fp);
|
2016-10-03 22:24:19 +02:00
|
|
|
|
2016-11-06 02:09:14 +08:00
|
|
|
// Unmount in inverse order
|
2016-10-15 05:18:16 +08:00
|
|
|
for(i = mount_size - 1; i >= 0; --i) {
|
2016-11-06 02:09:14 +08:00
|
|
|
if (strstr(mount_list[i], "/dev/block/loop")) {
|
|
|
|
if (strstr(mount_list[i], "/dev/magisk")) continue;
|
|
|
|
// Everything from loop mount
|
2016-10-15 05:18:16 +08:00
|
|
|
sscanf(mount_list[i], "%256s %256s", mountpoint, mountpoint);
|
2016-11-06 02:09:14 +08:00
|
|
|
} else if (strstr(mount_list[i], "tmpfs /system/")) {
|
|
|
|
// Directly unmount skeletons
|
|
|
|
sscanf(mount_list[i], "%256s %256s", mountpoint, mountpoint);
|
|
|
|
} else continue;
|
|
|
|
lazy_unmount(mountpoint);
|
2016-10-15 05:18:16 +08:00
|
|
|
free(mount_list[i]);
|
2016-10-06 17:53:52 +08:00
|
|
|
}
|
|
|
|
// Free memory
|
2016-10-15 05:18:16 +08:00
|
|
|
free(mount_list);
|
2016-10-19 06:22:00 +08:00
|
|
|
|
2016-10-15 05:18:16 +08:00
|
|
|
return 0;
|
2016-10-06 04:31:59 +08:00
|
|
|
}
|
2016-10-06 02:17:21 +08:00
|
|
|
|
2016-11-01 04:21:43 +08:00
|
|
|
void update_list(const char *listpath) {
|
|
|
|
FILE *hide_fp = fopen((char*) listpath, "r");
|
2016-10-15 05:18:16 +08:00
|
|
|
if (hide_fp == NULL) {
|
2016-11-01 04:21:43 +08:00
|
|
|
fprintf(logfile, "MagiskHide: Error opening hide list\n");
|
|
|
|
exit(1);
|
2016-10-06 04:31:59 +08:00
|
|
|
}
|
2016-11-01 04:21:43 +08:00
|
|
|
pthread_mutex_lock(&mutex);
|
|
|
|
if (hide_list) {
|
|
|
|
// Free memory
|
|
|
|
for(i = 0; i < list_size; ++i)
|
|
|
|
free(hide_list[i]);
|
|
|
|
free(hide_list);
|
|
|
|
}
|
|
|
|
hide_list = file_to_str_arr(hide_fp, &list_size);
|
|
|
|
pthread_mutex_unlock(&mutex);
|
2016-10-15 05:18:16 +08:00
|
|
|
fclose(hide_fp);
|
2016-11-07 23:57:21 +08:00
|
|
|
if (list_size) fprintf(logfile, "MagiskHide: Update process/package list:\n");
|
2016-11-01 04:21:43 +08:00
|
|
|
for(i = 0; i < list_size; i++)
|
|
|
|
fprintf(logfile, "MagiskHide: %s\n", hide_list[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void quit_pthread(int sig) {
|
|
|
|
// Free memory
|
|
|
|
for(i = 0; i < list_size; ++i)
|
|
|
|
free(hide_list[i]);
|
|
|
|
free(hide_list);
|
|
|
|
pthread_exit(NULL);
|
|
|
|
}
|
2016-10-15 05:18:16 +08:00
|
|
|
|
2016-11-01 04:21:43 +08:00
|
|
|
void *monitor_list(void *listpath) {
|
|
|
|
signal(SIGQUIT, quit_pthread);
|
|
|
|
// Initial load
|
|
|
|
update_list((char*) listpath);
|
2016-10-06 17:53:52 +08:00
|
|
|
|
2016-11-07 23:57:21 +08:00
|
|
|
int inotifyFd = -1;
|
2016-10-06 04:31:59 +08:00
|
|
|
char buffer[512];
|
2016-10-03 22:24:19 +02:00
|
|
|
|
2016-11-01 04:21:43 +08:00
|
|
|
while(1) {
|
2016-11-07 23:57:21 +08:00
|
|
|
if (inotifyFd == -1 || read(inotifyFd, buffer, 512) == -1) {
|
|
|
|
close(inotifyFd);
|
|
|
|
inotifyFd = inotify_init();
|
|
|
|
if (inotifyFd == -1) {
|
|
|
|
fprintf(logfile, "MagiskHide: Unable to watch %s\n", listpath);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (inotify_add_watch(inotifyFd, (char*) listpath, IN_MODIFY) == -1) {
|
|
|
|
fprintf(logfile, "MagiskHide: Unable to watch %s\n", listpath);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2016-11-01 04:21:43 +08:00
|
|
|
update_list((char*) listpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv, char **envp) {
|
|
|
|
|
|
|
|
pid_t forkpid = fork();
|
|
|
|
|
|
|
|
if (forkpid < 0)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (forkpid == 0) {
|
|
|
|
if (setsid() < 0)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
close(STDIN_FILENO);
|
|
|
|
close(STDOUT_FILENO);
|
|
|
|
close(STDERR_FILENO);
|
|
|
|
|
2016-11-07 23:57:21 +08:00
|
|
|
logfile = fopen(LOGFILE, "a+");
|
2016-11-01 04:21:43 +08:00
|
|
|
setbuf(logfile, NULL);
|
|
|
|
|
2016-11-05 02:38:02 +08:00
|
|
|
pthread_t list_monitor;
|
2016-11-01 04:21:43 +08:00
|
|
|
|
|
|
|
pthread_mutex_init(&mutex, NULL);
|
2016-11-07 23:57:21 +08:00
|
|
|
pthread_create(&list_monitor, NULL, monitor_list, HIDELIST);
|
2016-11-01 04:21:43 +08:00
|
|
|
|
|
|
|
char buffer[512];
|
|
|
|
FILE *p = popen("while true; do logcat -b events -v raw -s am_proc_start; sleep 1; done", "r");
|
|
|
|
|
|
|
|
while(!feof(p)) {
|
|
|
|
//Format of am_proc_start is (as of Android 5.1 and 6.0)
|
|
|
|
//UserID, pid, unix uid, processName, hostingType, hostingName
|
|
|
|
fgets(buffer, sizeof(buffer), p);
|
|
|
|
|
2016-10-03 22:24:19 +02:00
|
|
|
char *pos = buffer;
|
|
|
|
while(1) {
|
2016-10-06 02:17:21 +08:00
|
|
|
pos = strchr(pos, ',');
|
2016-10-03 22:24:19 +02:00
|
|
|
if(pos == NULL)
|
|
|
|
break;
|
|
|
|
pos[0] = ' ';
|
|
|
|
}
|
2016-11-01 04:21:43 +08:00
|
|
|
|
|
|
|
int user, pid, uid;
|
|
|
|
char processName[256], hostingType[16], hostingName[256];
|
|
|
|
int ret = sscanf(buffer, "[%d %d %d %256s %16s %256s]",
|
|
|
|
&user, &pid, &uid,
|
|
|
|
processName, hostingType, hostingName);
|
|
|
|
|
|
|
|
if(ret != 6)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (i = 0; i < list_size; ++i) {
|
|
|
|
if(strstr(processName, hide_list[i])) {
|
2016-11-06 02:09:14 +08:00
|
|
|
fprintf(logfile, "MagiskHide: Disabling for process=%s, PID=%d, UID=%d\n", processName, pid, uid);
|
2016-11-01 04:21:43 +08:00
|
|
|
forkpid = fork();
|
|
|
|
if (forkpid < 0)
|
|
|
|
break;
|
|
|
|
if (forkpid == 0) {
|
|
|
|
hideMagisk(pid);
|
|
|
|
return 0;
|
|
|
|
}
|
2016-11-06 02:09:14 +08:00
|
|
|
waitpid(forkpid, NULL, 0);
|
2016-11-01 04:21:43 +08:00
|
|
|
kill(forkpid, SIGTERM);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-10-03 22:24:19 +02:00
|
|
|
}
|
|
|
|
|
2016-11-01 04:21:43 +08:00
|
|
|
pclose(p);
|
2016-10-03 22:24:19 +02:00
|
|
|
|
2016-11-01 04:21:43 +08:00
|
|
|
pthread_kill(list_monitor, SIGQUIT);
|
|
|
|
pthread_mutex_destroy(&mutex);
|
2016-10-03 22:24:19 +02:00
|
|
|
|
2016-11-01 04:21:43 +08:00
|
|
|
fprintf(logfile, "MagiskHide: Error occurred...\n");
|
2016-10-03 22:24:19 +02:00
|
|
|
|
2016-11-01 04:21:43 +08:00
|
|
|
fclose(logfile);
|
2016-10-06 17:53:52 +08:00
|
|
|
|
2016-11-01 04:21:43 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2016-10-06 17:53:52 +08:00
|
|
|
|
2016-10-03 22:24:19 +02:00
|
|
|
return 0;
|
2016-09-27 00:08:18 +02:00
|
|
|
}
|