From eca21686851efdac4e1735eef60b66367d240935 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 16 May 2020 23:31:30 -0700 Subject: [PATCH] Guard magiskhide state with mutexes --- native/jni/magiskhide/hide_utils.cpp | 39 +++++++++++++++++----------- native/jni/magiskhide/magiskhide.cpp | 6 ++--- native/jni/magiskhide/magiskhide.hpp | 2 +- 3 files changed, 27 insertions(+), 20 deletions(-) diff --git a/native/jni/magiskhide/hide_utils.cpp b/native/jni/magiskhide/hide_utils.cpp index 16f3349f6..9b009ee25 100644 --- a/native/jni/magiskhide/hide_utils.cpp +++ b/native/jni/magiskhide/hide_utils.cpp @@ -34,6 +34,14 @@ void crawl_procfs(DIR *dir, const function &fn) { } } +static bool hide_state = false; +static pthread_mutex_t hide_state_lock = PTHREAD_MUTEX_INITIALIZER; + +bool hide_enabled() { + mutex_guard g(hide_state_lock); + return hide_state; +} + static bool proc_name_match(int pid, const char *name) { char buf[4019]; sprintf(buf, "/proc/%d/cmdline", pid); @@ -206,39 +214,40 @@ void ls_list(int client) { static void set_hide_config() { char sql[64]; sprintf(sql, "REPLACE INTO settings (key,value) VALUES('%s',%d)", - DB_SETTING_KEYS[HIDE_CONFIG], hide_enabled); + DB_SETTING_KEYS[HIDE_CONFIG], hide_state); char *err = db_exec(sql); db_err(err); } [[noreturn]] static void launch_err(int client, int code = DAEMON_ERROR) { if (code != HIDE_IS_ENABLED) - hide_enabled = false; + hide_state = false; if (client >= 0) { write_int(client, code); close(client); } + pthread_mutex_unlock(&hide_state_lock); pthread_exit(nullptr); } -#define LAUNCH_ERR launch_err(client) - void launch_magiskhide(int client) { - if (SDK_INT < 19) - LAUNCH_ERR; + pthread_mutex_lock(&hide_state_lock); - if (hide_enabled) + if (SDK_INT < 19) + launch_err(client); + + if (hide_state) launch_err(client, HIDE_IS_ENABLED); if (access("/proc/1/ns/mnt", F_OK) != 0) launch_err(client, HIDE_NO_NS); - hide_enabled = true; + hide_state = true; set_hide_config(); LOGI("* Starting MagiskHide\n"); if (procfp == nullptr && (procfp = opendir("/proc")) == nullptr) - LAUNCH_ERR; + launch_err(client); hide_sensitive_props(); @@ -247,7 +256,7 @@ void launch_magiskhide(int client) { // Initialize the hide list if (!init_list()) - LAUNCH_ERR; + launch_err(client); // Get thread reference proc_monitor_thread = pthread_self(); @@ -260,23 +269,23 @@ void launch_magiskhide(int client) { proc_monitor(); // proc_monitor should not return - LAUNCH_ERR; + launch_err(client); } int stop_magiskhide() { LOGI("* Stopping MagiskHide\n"); - if (hide_enabled) + mutex_guard g(hide_state_lock); + if (hide_state) pthread_kill(proc_monitor_thread, SIGTERMTHRD); - - hide_enabled = false; + hide_state = false; set_hide_config(); return DAEMON_SUCCESS; } void auto_start_magiskhide() { - if (hide_enabled) { + if (hide_enabled()) { pthread_kill(proc_monitor_thread, SIGZYGOTE); } else if (SDK_INT >= 19) { db_settings dbs; diff --git a/native/jni/magiskhide/magiskhide.cpp b/native/jni/magiskhide/magiskhide.cpp index cceab78ed..2c721b207 100644 --- a/native/jni/magiskhide/magiskhide.cpp +++ b/native/jni/magiskhide/magiskhide.cpp @@ -16,8 +16,6 @@ using namespace std::literals; -bool hide_enabled = false; - [[noreturn]] static void usage(char *arg0) { fprintf(stderr, NAME_WITH_VER(MagiskHide) "\n\n" @@ -47,7 +45,7 @@ void magiskhide_handler(int client) { case ADD_HIDELIST: case RM_HIDELIST: case LS_HIDELIST: - if (!hide_enabled) { + if (!hide_enabled()) { write_int(client, HIDE_NOT_ENABLED); close(client); return; @@ -72,7 +70,7 @@ void magiskhide_handler(int client) { client = -1; break; case HIDE_STATUS: - res = hide_enabled ? HIDE_IS_ENABLED : HIDE_NOT_ENABLED; + res = hide_enabled() ? HIDE_IS_ENABLED : HIDE_NOT_ENABLED; break; } diff --git a/native/jni/magiskhide/magiskhide.hpp b/native/jni/magiskhide/magiskhide.hpp index fc8a82fb4..99fcda9a2 100644 --- a/native/jni/magiskhide/magiskhide.hpp +++ b/native/jni/magiskhide/magiskhide.hpp @@ -30,13 +30,13 @@ void update_uid_map(); // Utility functions void crawl_procfs(const std::function &fn); void crawl_procfs(DIR *dir, const std::function &fn); +bool hide_enabled(); // Hide policies void hide_daemon(int pid); void hide_unmount(int pid = getpid()); void hide_sensitive_props(); -extern bool hide_enabled; extern pthread_mutex_t monitor_lock; extern std::set> hide_set;