mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-10-16 09:52:31 +00:00
Auto start magiskhide
This commit is contained in:
@@ -18,23 +18,7 @@
|
||||
#include "utils.h"
|
||||
#include "magiskhide.h"
|
||||
|
||||
static int isMocked = 0, pid;
|
||||
|
||||
static void manage_selinux() {
|
||||
if (isMocked) return;
|
||||
char val[1];
|
||||
int fd = xopen(ENFORCE_FILE, O_RDONLY);
|
||||
xxread(fd, val, 1);
|
||||
close(fd);
|
||||
// Permissive
|
||||
if (val[0] == '0') {
|
||||
LOGI("hide_daemon: Permissive detected, hide the state\n");
|
||||
|
||||
chmod(ENFORCE_FILE, 0640);
|
||||
chmod(POLICY_FILE, 0440);
|
||||
isMocked = 1;
|
||||
}
|
||||
}
|
||||
static int pid;
|
||||
|
||||
static void lazy_unmount(const char* mountpoint) {
|
||||
if (umount2(mountpoint, MNT_DETACH) != -1)
|
||||
@@ -87,6 +71,9 @@ int hide_daemon() {
|
||||
_exit(0);
|
||||
}
|
||||
|
||||
manage_selinux();
|
||||
relink_sbin();
|
||||
|
||||
snprintf(buffer, sizeof(buffer), "/proc/%d/ns/mnt", pid);
|
||||
if(access(buffer, F_OK) == -1) continue; // Maybe process died..
|
||||
|
||||
@@ -95,8 +82,6 @@ int hide_daemon() {
|
||||
xsetns(fd, 0);
|
||||
close(fd);
|
||||
|
||||
manage_selinux();
|
||||
|
||||
snprintf(buffer, sizeof(buffer), "/proc/%d/mounts", pid);
|
||||
fp = xfopen(buffer, "r");
|
||||
vec_init(&mount_list);
|
||||
|
@@ -52,6 +52,8 @@ void launch_magiskhide(int client) {
|
||||
|
||||
LOGI("* Starting MagiskHide\n");
|
||||
|
||||
hide_sensitive_props();
|
||||
|
||||
if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sv) == -1)
|
||||
goto error;
|
||||
|
||||
@@ -94,6 +96,7 @@ error:
|
||||
write(sv[0], &kill, sizeof(kill));
|
||||
close(sv[0]);
|
||||
waitpid(hide_pid, NULL, 0);
|
||||
hide_pid = -1;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
@@ -15,6 +15,11 @@ int hide_daemon();
|
||||
// Process monitor
|
||||
void *proc_monitor(void *args);
|
||||
|
||||
// Preprocess
|
||||
void manage_selinux();
|
||||
void hide_sensitive_props();
|
||||
void relink_sbin();
|
||||
|
||||
extern int sv[2], hide_pid, isEnabled;
|
||||
extern struct vector *hide_list, *new_list;
|
||||
|
||||
|
91
jni/magiskhide/pre_process.c
Normal file
91
jni/magiskhide/pre_process.c
Normal file
@@ -0,0 +1,91 @@
|
||||
/* pre_process.c - Some pre-processes for MagiskHide to hide properly
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <dirent.h>
|
||||
#include <string.h>
|
||||
#include <sys/mount.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <selinux/selinux.h>
|
||||
|
||||
#include "magisk.h"
|
||||
#include "utils.h"
|
||||
#include "resetprop.h"
|
||||
#include "magiskhide.h"
|
||||
|
||||
static char *prop_key[] =
|
||||
{ "ro.boot.verifiedbootstate", "ro.boot.flash.locked", "ro.boot.veritymode", "ro.boot.warranty_bit", "ro.warranty_bit",
|
||||
"ro.debuggable", "ro.secure", NULL };
|
||||
|
||||
static char *prop_value[] =
|
||||
{ "green", "1", "enforcing", "0", "0", "0", "1", NULL };
|
||||
|
||||
static int isMocked = 0;
|
||||
|
||||
void manage_selinux() {
|
||||
if (isMocked) return;
|
||||
char val[1];
|
||||
int fd = xopen(ENFORCE_FILE, O_RDONLY);
|
||||
xxread(fd, val, 1);
|
||||
close(fd);
|
||||
// Permissive
|
||||
if (val[0] == '0') {
|
||||
LOGI("hide_daemon: Permissive detected, hide the state\n");
|
||||
|
||||
chmod(ENFORCE_FILE, 0640);
|
||||
chmod(POLICY_FILE, 0440);
|
||||
isMocked = 1;
|
||||
}
|
||||
}
|
||||
|
||||
void hide_sensitive_props() {
|
||||
LOGI("hide_pre_proc: Hiding sensitive props\n");
|
||||
|
||||
// Hide all sensitive props
|
||||
init_resetprop();
|
||||
char *value;
|
||||
for (int i = 0; prop_key[i]; ++i) {
|
||||
value = getprop(prop_key[i]);
|
||||
if (value) {
|
||||
if (strcmp(value, prop_value[i]) != 0)
|
||||
setprop2(prop_key[i], prop_value[i], 0);
|
||||
free(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void relink_sbin() {
|
||||
struct stat st;
|
||||
if (stat("/sbin_orig", &st) == -1 && errno == ENOENT) {
|
||||
// Re-link all binaries and bind mount
|
||||
DIR *dir;
|
||||
struct dirent *entry;
|
||||
char from[PATH_MAX], to[PATH_MAX];
|
||||
|
||||
LOGI("hide_pre_proc: Re-linking /sbin\n");
|
||||
|
||||
xmount(NULL, "/", NULL, MS_REMOUNT, NULL);
|
||||
xrename("/sbin", "/sbin_orig");
|
||||
xmkdir("/sbin", 0755);
|
||||
xchmod("/sbin", 0755);
|
||||
xmount(NULL, "/", NULL, MS_REMOUNT | MS_RDONLY, NULL);
|
||||
xmkdir("/dev/sbin_bind", 0755);
|
||||
xchmod("/dev/sbin_bind", 0755);
|
||||
dir = xopendir("/sbin_orig");
|
||||
|
||||
while ((entry = xreaddir(dir))) {
|
||||
snprintf(from, sizeof(from), "%s/%s", "/sbin_orig", entry->d_name);
|
||||
snprintf(to, sizeof(to), "%s/%s", "/dev/sbin_bind", entry->d_name);
|
||||
symlink(from, to);
|
||||
lsetfilecon(to, "u:object_r:system_file:s0");
|
||||
}
|
||||
|
||||
closedir(dir);
|
||||
|
||||
xmount("/dev/sbin_bind", "/sbin", NULL, MS_BIND, NULL);
|
||||
}
|
||||
}
|
@@ -19,13 +19,11 @@
|
||||
|
||||
static int zygote_num = 0;
|
||||
static char init_ns[32], zygote_ns[2][32];
|
||||
static FILE *p;
|
||||
|
||||
static void read_namespace(const int pid, char* target, const size_t size) {
|
||||
char path[32];
|
||||
snprintf(path, sizeof(path), "/proc/%d/ns/mnt", pid);
|
||||
ssize_t len = readlink(path, target, size);
|
||||
target[len] = '\0';
|
||||
xreadlink(path, target, size);
|
||||
}
|
||||
|
||||
// Workaround for the lack of pthread_cancel
|
||||
@@ -44,7 +42,6 @@ static void quit_pthread(int sig) {
|
||||
hide_list = new_list = NULL;
|
||||
isEnabled = 0;
|
||||
LOGD("proc_monitor: terminating...\n");
|
||||
pclose(p);
|
||||
pthread_exit(NULL);
|
||||
}
|
||||
|
||||
@@ -80,7 +77,11 @@ void *proc_monitor(void *args) {
|
||||
LOGI("proc_monitor: init ns=%s\n", init_ns);
|
||||
|
||||
// Get the mount namespace of zygote
|
||||
ps_filter_proc_name("zygote", store_zygote_ns);
|
||||
while(!zygote_num) {
|
||||
// Check zygote every 2 secs
|
||||
sleep(2);
|
||||
ps_filter_proc_name("zygote", store_zygote_ns);
|
||||
}
|
||||
|
||||
switch(zygote_num) {
|
||||
case 1:
|
||||
@@ -92,7 +93,7 @@ void *proc_monitor(void *args) {
|
||||
}
|
||||
|
||||
// Monitor am_proc_start (the command shall never end)
|
||||
p = popen("while true; do logcat -b events -c; logcat -b events -v raw -s am_proc_start; sleep 1; done", "r");
|
||||
FILE *p = popen("while true; do logcat -b events -c; logcat -b events -v raw -s am_proc_start; sleep 1; done", "r");
|
||||
|
||||
while(fgets(buffer, sizeof(buffer), p)) {
|
||||
int ret, comma = 0;
|
||||
@@ -130,7 +131,7 @@ void *proc_monitor(void *args) {
|
||||
ret = 1;
|
||||
for (int i = 0; i < zygote_num; ++i) {
|
||||
if (strcmp(buffer, zygote_ns[i]) == 0) {
|
||||
usleep(500);
|
||||
usleep(50);
|
||||
ret = 0;
|
||||
break;
|
||||
}
|
||||
|
Reference in New Issue
Block a user