mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-01-04 16:17:39 +00:00
MagiskHide is coming back strong
This commit is contained in:
parent
98f60216ac
commit
1e94517a72
@ -30,7 +30,7 @@ db_settings::db_settings() {
|
|||||||
data[ROOT_ACCESS] = ROOT_ACCESS_APPS_AND_ADB;
|
data[ROOT_ACCESS] = ROOT_ACCESS_APPS_AND_ADB;
|
||||||
data[SU_MULTIUSER_MODE] = MULTIUSER_MODE_OWNER_ONLY;
|
data[SU_MULTIUSER_MODE] = MULTIUSER_MODE_OWNER_ONLY;
|
||||||
data[SU_MNT_NS] = NAMESPACE_MODE_REQUESTER;
|
data[SU_MNT_NS] = NAMESPACE_MODE_REQUESTER;
|
||||||
data[HIDE_CONFIG] = false;
|
data[HIDE_CONFIG] = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
int db_settings::getKeyIdx(string_view key) const {
|
int db_settings::getKeyIdx(string_view key) const {
|
||||||
|
@ -38,7 +38,7 @@ static inline void lazy_unmount(const char* mountpoint) {
|
|||||||
void hide_daemon(int pid) {
|
void hide_daemon(int pid) {
|
||||||
RunFinally fin([=]() -> void {
|
RunFinally fin([=]() -> void {
|
||||||
// Send resume signal
|
// Send resume signal
|
||||||
tgkill(pid, pid, SIGCONT);
|
kill(pid, SIGCONT);
|
||||||
_exit(0);
|
_exit(0);
|
||||||
});
|
});
|
||||||
hide_unmount(pid);
|
hide_unmount(pid);
|
||||||
|
@ -37,7 +37,6 @@ pthread_mutex_t monitor_lock;
|
|||||||
|
|
||||||
#define PID_MAX 32768
|
#define PID_MAX 32768
|
||||||
static bitset<PID_MAX> attaches; /* true if pid is monitored */
|
static bitset<PID_MAX> attaches; /* true if pid is monitored */
|
||||||
static bitset<PID_MAX> detaches; /* true if tid should be detached */
|
|
||||||
|
|
||||||
/********
|
/********
|
||||||
* Utils
|
* Utils
|
||||||
@ -163,7 +162,6 @@ static void term_thread(int) {
|
|||||||
zygote_map.clear();
|
zygote_map.clear();
|
||||||
hide_set.clear();
|
hide_set.clear();
|
||||||
attaches.reset();
|
attaches.reset();
|
||||||
detaches.reset();
|
|
||||||
// Misc
|
// Misc
|
||||||
hide_enabled = false;
|
hide_enabled = false;
|
||||||
pthread_mutex_destroy(&monitor_lock);
|
pthread_mutex_destroy(&monitor_lock);
|
||||||
@ -185,44 +183,41 @@ static void term_thread(int) {
|
|||||||
#define PTRACE_LOG(...)
|
#define PTRACE_LOG(...)
|
||||||
|
|
||||||
static void detach_pid(int pid, int signal = 0) {
|
static void detach_pid(int pid, int signal = 0) {
|
||||||
char path[128];
|
|
||||||
attaches[pid] = false;
|
attaches[pid] = false;
|
||||||
xptrace(PTRACE_DETACH, pid, nullptr, signal);
|
ptrace(PTRACE_DETACH, pid, 0, signal);
|
||||||
|
PTRACE_LOG("detach\n");
|
||||||
// Detach all child threads too
|
|
||||||
sprintf(path, "/proc/%d/task", pid);
|
|
||||||
DIR *dir = opendir(path);
|
|
||||||
crawl_procfs(dir, [&](int tid) -> bool {
|
|
||||||
if (tid != pid) {
|
|
||||||
// Check if we should force a SIGSTOP
|
|
||||||
if (waitpid(tid, nullptr, __WALL | __WNOTHREAD | WNOHANG) == tid) {
|
|
||||||
PTRACE_LOG("detach thread [%d]\n", tid);
|
|
||||||
xptrace(PTRACE_DETACH, tid);
|
|
||||||
} else {
|
|
||||||
detaches[tid] = true;
|
|
||||||
tgkill(pid, tid, SIGSTOP);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
});
|
|
||||||
closedir(dir);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool check_pid(int pid) {
|
static bool check_pid(int pid) {
|
||||||
char path[128];
|
char path[128];
|
||||||
char cmdline[1024];
|
char cmdline[1024];
|
||||||
sprintf(path, "/proc/%d/cmdline", pid);
|
struct stat st;
|
||||||
FILE *f = fopen(path, "re");
|
|
||||||
// Process killed unexpectedly, ignore
|
|
||||||
if (!f) return true;
|
|
||||||
fgets(cmdline, sizeof(cmdline), f);
|
|
||||||
fclose(f);
|
|
||||||
if (strncmp(cmdline, "zygote", 6) == 0)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
sprintf(path, "/proc/%d", pid);
|
sprintf(path, "/proc/%d", pid);
|
||||||
struct stat st;
|
if (stat(path, &st)) {
|
||||||
lstat(path, &st);
|
// Process killed unexpectedly, ignore
|
||||||
|
detach_pid(pid);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// UID hasn't changed
|
||||||
|
if (st.st_uid == 0)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
sprintf(path, "/proc/%d/cmdline", pid);
|
||||||
|
if (FILE *f; (f = fopen(path, "re"))) {
|
||||||
|
fgets(cmdline, sizeof(cmdline), f);
|
||||||
|
fclose(f);
|
||||||
|
} else {
|
||||||
|
// Process killed unexpectedly, ignore
|
||||||
|
detach_pid(pid);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Still zygote/usap
|
||||||
|
if (strncmp(cmdline, "zygote", 6) == 0 || strncmp(cmdline, "usap", 4) == 0)
|
||||||
|
return false;
|
||||||
|
|
||||||
int uid = st.st_uid % 100000;
|
int uid = st.st_uid % 100000;
|
||||||
auto it = uid_proc_map.find(uid);
|
auto it = uid_proc_map.find(uid);
|
||||||
if (it != uid_proc_map.end()) {
|
if (it != uid_proc_map.end()) {
|
||||||
@ -259,6 +254,25 @@ static bool check_pid(int pid) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool is_process(int pid) {
|
||||||
|
char buf[128];
|
||||||
|
char key[32];
|
||||||
|
int tgid;
|
||||||
|
sprintf(buf, "/proc/%d/status", pid);
|
||||||
|
unique_ptr<FILE, decltype(&fclose)> fp(fopen(buf, "re"), &fclose);
|
||||||
|
// PID is dead
|
||||||
|
if (!fp)
|
||||||
|
return false;
|
||||||
|
while (fgets(buf, sizeof(buf), fp.get())) {
|
||||||
|
sscanf(buf, "%s", key);
|
||||||
|
if (key == "Tgid:"sv) {
|
||||||
|
sscanf(buf, "%*s %d", &tgid);
|
||||||
|
return tgid == pid;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
static void new_zygote(int pid) {
|
static void new_zygote(int pid) {
|
||||||
struct stat st;
|
struct stat st;
|
||||||
if (read_ns(pid, &st))
|
if (read_ns(pid, &st))
|
||||||
@ -322,17 +336,13 @@ void proc_monitor() {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
bool detach = false;
|
bool detach = false;
|
||||||
RunFinally detach_task([&]() -> void {
|
RunFinally detach_task([&] {
|
||||||
if (detach) {
|
if (detach)
|
||||||
// Non of our business now
|
// Non of our business now
|
||||||
attaches[pid] = false;
|
detach_pid(pid);
|
||||||
detaches[pid] = false;
|
|
||||||
ptrace(PTRACE_DETACH, pid, 0, 0);
|
|
||||||
PTRACE_LOG("detach\n");
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!WIFSTOPPED(status) /* Ignore if not ptrace-stop */ || detaches[pid])
|
if (!WIFSTOPPED(status) /* Ignore if not ptrace-stop */)
|
||||||
DETACH_AND_CONT;
|
DETACH_AND_CONT;
|
||||||
|
|
||||||
if (WSTOPSIG(status) == SIGTRAP && WEVENT(status)) {
|
if (WSTOPSIG(status) == SIGTRAP && WEVENT(status)) {
|
||||||
@ -370,10 +380,21 @@ void proc_monitor() {
|
|||||||
}
|
}
|
||||||
xptrace(PTRACE_CONT, pid);
|
xptrace(PTRACE_CONT, pid);
|
||||||
} else if (WSTOPSIG(status) == SIGSTOP) {
|
} else if (WSTOPSIG(status) == SIGSTOP) {
|
||||||
PTRACE_LOG("SIGSTOP from child\n");
|
if (!attaches[pid]) {
|
||||||
xptrace(PTRACE_SETOPTIONS, pid, nullptr,
|
// Double check if this is actually a process
|
||||||
PTRACE_O_TRACECLONE | PTRACE_O_TRACEEXEC | PTRACE_O_TRACEEXIT);
|
attaches[pid] = is_process(pid);
|
||||||
xptrace(PTRACE_CONT, pid);
|
}
|
||||||
|
if (attaches[pid]) {
|
||||||
|
// This is a process, continue monitoring
|
||||||
|
PTRACE_LOG("SIGSTOP from child\n");
|
||||||
|
xptrace(PTRACE_SETOPTIONS, pid, nullptr,
|
||||||
|
PTRACE_O_TRACECLONE | PTRACE_O_TRACEEXEC | PTRACE_O_TRACEEXIT);
|
||||||
|
xptrace(PTRACE_CONT, pid);
|
||||||
|
} else {
|
||||||
|
// This is a thread, do NOT monitor
|
||||||
|
PTRACE_LOG("SIGSTOP from thread\n");
|
||||||
|
DETACH_AND_CONT;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// Not caused by us, resend signal
|
// Not caused by us, resend signal
|
||||||
xptrace(PTRACE_CONT, pid, nullptr, WSTOPSIG(status));
|
xptrace(PTRACE_CONT, pid, nullptr, WSTOPSIG(status));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user