From 82c864d57e737c5b56eca60c6325219e33c665e2 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Wed, 6 Mar 2019 18:22:04 -0500 Subject: [PATCH] Make zygote notifier more reliable --- native/jni/core/bootstages.cpp | 2 +- native/jni/core/magisk.cpp | 1 + native/jni/include/daemon.h | 1 - native/jni/magiskhide/hide_utils.cpp | 20 +------------------- native/jni/magiskhide/magiskhide.h | 6 ++---- native/jni/magiskhide/proc_monitor.cpp | 26 ++++++++++++++++++++++++++ 6 files changed, 31 insertions(+), 25 deletions(-) diff --git a/native/jni/core/bootstages.cpp b/native/jni/core/bootstages.cpp index eefcf863f..5c6bc0412 100644 --- a/native/jni/core/bootstages.cpp +++ b/native/jni/core/bootstages.cpp @@ -583,7 +583,7 @@ static void dump_logs() { sprintf(buf, "/system/bin/%s", app); if (lstat(buf, &st) == 0 && S_ISREG(st.st_mode)) { clone_attr(buf, MAGISKTMP "/app_process"); - bind_mount(MAGISKTMP "/app_process", buf); + bind_mount(MAGISKTMP "/app_process", buf, false); } } unblock_boot_process(); diff --git a/native/jni/core/magisk.cpp b/native/jni/core/magisk.cpp index d97f22231..4c01776ab 100644 --- a/native/jni/core/magisk.cpp +++ b/native/jni/core/magisk.cpp @@ -129,6 +129,7 @@ int app_process_main(int argc, char *argv[]) { int fd = connect_daemon(); write_int(fd, ZYGOTE_NOTIFY); write_string(fd, path); + read_int(fd); close(fd); } else { // Redirect to system mirror diff --git a/native/jni/include/daemon.h b/native/jni/include/daemon.h index 7ae9eecac..77b1a7166 100644 --- a/native/jni/include/daemon.h +++ b/native/jni/include/daemon.h @@ -80,7 +80,6 @@ void install_apk(const char *apk); void magiskhide_handler(int client); void zygote_notify(int client, struct ucred *cred); -void zygote_notify(int pid); /************* * Superuser * diff --git a/native/jni/magiskhide/hide_utils.cpp b/native/jni/magiskhide/hide_utils.cpp index 38b608643..1bab2a6e5 100644 --- a/native/jni/magiskhide/hide_utils.cpp +++ b/native/jni/magiskhide/hide_utils.cpp @@ -18,7 +18,7 @@ using namespace std; -static pthread_t proc_monitor_thread; +pthread_t proc_monitor_thread; static const char *prop_key[] = { "ro.boot.vbmeta.device_state", "ro.boot.verifiedbootstate", "ro.boot.flash.locked", @@ -328,21 +328,3 @@ void auto_start_magiskhide() { } } -int next_zygote = -1; - -void zygote_notify(int pid) { - if (hide_enabled) { - MutexGuard lock(monitor_lock); - next_zygote = pid; - pthread_kill(proc_monitor_thread, SIGZYGOTE); - } -} - -void zygote_notify(int client, struct ucred *cred) { - char *path = read_string(client); - close(client); - zygote_notify(cred->pid); - usleep(100000); - xmount(MAGISKTMP "/app_process", path, nullptr, MS_BIND, nullptr); - free(path); -} diff --git a/native/jni/magiskhide/magiskhide.h b/native/jni/magiskhide/magiskhide.h index 0358ffc87..d8984484a 100644 --- a/native/jni/magiskhide/magiskhide.h +++ b/native/jni/magiskhide/magiskhide.h @@ -1,5 +1,4 @@ -#ifndef MAGISK_HIDE_H -#define MAGISK_HIDE_H +#pragma once #include #include @@ -54,6 +53,7 @@ static inline int parse_int(const char *s) { return val; } +extern pthread_t proc_monitor_thread; extern bool hide_enabled; extern pthread_mutex_t monitor_lock; extern std::set> hide_set; @@ -75,5 +75,3 @@ enum { HIDE_ITEM_NOT_EXIST, HIDE_NO_NS }; - -#endif diff --git a/native/jni/magiskhide/proc_monitor.cpp b/native/jni/magiskhide/proc_monitor.cpp index 7bd9b7678..e8133c76e 100644 --- a/native/jni/magiskhide/proc_monitor.cpp +++ b/native/jni/magiskhide/proc_monitor.cpp @@ -241,6 +241,32 @@ static void term_thread(int) { //#define PTRACE_LOG(fmt, args...) LOGD("PID=[%d] " fmt, pid, ##args) #define PTRACE_LOG(...) +int next_zygote = -1; + +void zygote_notify(int client, struct ucred *cred) { + char *path = read_string(client); + + xptrace(PTRACE_ATTACH, cred->pid); + // Wait for attach + waitpid(cred->pid, nullptr, __WALL | __WNOTHREAD); + xptrace(PTRACE_CONT, cred->pid); + write_int(client, 0); + close(client); + // Wait for exec + waitpid(cred->pid, nullptr, __WALL | __WNOTHREAD); + xptrace(PTRACE_DETACH, cred->pid); + + if (hide_enabled) { + MutexGuard lock(monitor_lock); + next_zygote = cred->pid; + pthread_kill(proc_monitor_thread, SIGZYGOTE); + } + + // Remount zygote notifier ASAP + xmount(MAGISKTMP "/app_process", path, nullptr, MS_BIND, nullptr); + free(path); +} + static bool check_pid(int pid) { char path[128]; char cmdline[1024];