From ab74290fe3f6fe10fcc98a3f4703679fa5b5a83a Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Fri, 16 Nov 2018 01:15:34 -0500 Subject: [PATCH] Move magiskhide config into database --- native/jni/daemon/bootstages.cpp | 17 +------ native/jni/daemon/db.cpp | 7 +-- native/jni/include/db.h | 12 +++-- native/jni/magiskhide/hide_utils.cpp | 76 ++++++++++++++++++++++++++++ native/jni/magiskhide/magiskhide.cpp | 58 --------------------- 5 files changed, 88 insertions(+), 82 deletions(-) diff --git a/native/jni/daemon/bootstages.cpp b/native/jni/daemon/bootstages.cpp index 4b00c8be8..aca133ccd 100644 --- a/native/jni/daemon/bootstages.cpp +++ b/native/jni/daemon/bootstages.cpp @@ -30,6 +30,7 @@ static bool seperate_vendor; char *system_block, *vendor_block, *magiskloop; static int bind_mount(const char *from, const char *to); +extern void auto_start_magiskhide(); /*************** * Magic Mount * @@ -599,22 +600,6 @@ static bool check_data() { return data; } -extern int launch_magiskhide(int client = -1); - -static void auto_start_magiskhide() { - if (!start_log_daemon()) - return; - CharArray hide_prop = getprop(MAGISKHIDE_PROP, true); - if (hide_prop != "0") { - pthread_t thread; - xpthread_create(&thread, nullptr, [](void*) -> void* { - launch_magiskhide(); - return nullptr; - }, nullptr); - pthread_detach(thread); - } -} - void unlock_blocks() { DIR *dir; struct dirent *entry; diff --git a/native/jni/daemon/db.cpp b/native/jni/daemon/db.cpp index eb6ea9823..379d2ec84 100644 --- a/native/jni/daemon/db.cpp +++ b/native/jni/daemon/db.cpp @@ -42,7 +42,8 @@ int db_strings::getKeyIdx(const char *key) const { db_settings::db_settings() : data { ROOT_ACCESS_APPS_AND_ADB, MULTIUSER_MODE_OWNER_ONLY, - NAMESPACE_MODE_REQUESTER + NAMESPACE_MODE_REQUESTER, + 1 } {} int &db_settings::operator[](const int idx) { @@ -188,7 +189,7 @@ static int settings_cb(void *v, int col_num, char **data, char **col_name) { return 0; } -int get_db_settings(sqlite3 *db, struct db_settings *dbs, int key) { +int get_db_settings(sqlite3 *db, db_settings *dbs, int key) { if (db == nullptr) return 1; char *err; @@ -224,7 +225,7 @@ static int strings_cb(void *v, int col_num, char **data, char **col_name) { return 0; } -int get_db_strings(sqlite3 *db, struct db_strings *str, int key) { +int get_db_strings(sqlite3 *db, db_strings *str, int key) { if (db == nullptr) return 1; char *err; diff --git a/native/jni/include/db.h b/native/jni/include/db.h index a5e6fa952..fe6fdd9d6 100644 --- a/native/jni/include/db.h +++ b/native/jni/include/db.h @@ -12,16 +12,18 @@ ((const char *[]) { \ "root_access", \ "multiuser_mode", \ -"mnt_ns" \ +"mnt_ns", \ +"magiskhide", \ }) -#define DB_SETTINGS_NUM 3 +#define DB_SETTINGS_NUM 4 // Settings keys enum { ROOT_ACCESS = 0, SU_MULTIUSER_MODE, - SU_MNT_NS + SU_MNT_NS, + HIDE_CONFIG }; // Values for root_access @@ -127,8 +129,8 @@ struct su_access { ********************/ sqlite3 *get_magiskdb(); -int get_db_settings(sqlite3 *db, struct db_settings *dbs, int key = -1); -int get_db_strings(sqlite3 *db, struct db_strings *str, int key = -1); +int get_db_settings(sqlite3 *db, db_settings *dbs, int key = -1); +int get_db_strings(sqlite3 *db, db_strings *str, int key = -1); int get_uid_policy(sqlite3 *db, int uid, struct su_access *su); int validate_manager(char *alt_pkg, int userid, struct stat *st); int exec_sql(const char *sql); diff --git a/native/jni/magiskhide/hide_utils.cpp b/native/jni/magiskhide/hide_utils.cpp index 1879177c1..0ab7d3993 100644 --- a/native/jni/magiskhide/hide_utils.cpp +++ b/native/jni/magiskhide/hide_utils.cpp @@ -16,6 +16,9 @@ #include "db.h" Vector hide_list; +pthread_mutex_t list_lock; + +static pthread_t proc_monitor_thread; static const char *prop_key[] = { "ro.boot.vbmeta.device_state", "ro.boot.verifiedbootstate", "ro.boot.flash.locked", @@ -250,3 +253,76 @@ void ls_list(int client) { write_string(client, s); close(client); } + +static void set_hide_config() { + sqlite3 *db = get_magiskdb(); + char sql[64]; + sprintf(sql, "REPLACE INTO settings (key,value) VALUES('%s',%d)", + DB_SETTING_KEYS[HIDE_CONFIG], hide_enabled); + sqlite3_exec(db, sql, nullptr, nullptr, nullptr); + sqlite3_close_v2(db); +} + +int launch_magiskhide(int client) { + if (hide_enabled) + return HIDE_IS_ENABLED; + + if (!log_daemon_started) + return LOGCAT_DISABLED; + + hide_enabled = true; + set_hide_config(); + LOGI("* Starting MagiskHide\n"); + + hide_sensitive_props(); + + // Initialize the mutex lock + pthread_mutex_init(&list_lock, nullptr); + + // Initialize the hide list + if (!init_list()) + goto error; + + // Add SafetyNet by default + add_list("com.google.android.gms.unstable"); + + // Get thread reference + proc_monitor_thread = pthread_self(); + if (client >= 0) { + write_int(client, DAEMON_SUCCESS); + close(client); + } + // Start monitoring + proc_monitor(); + + error: + hide_enabled = false; + return DAEMON_ERROR; +} + +int stop_magiskhide() { + LOGI("* Stopping MagiskHide\n"); + + hide_enabled = false; + set_hide_config(); + pthread_kill(proc_monitor_thread, TERM_THREAD); + + return DAEMON_SUCCESS; +} + +void auto_start_magiskhide() { + if (!start_log_daemon()) + return; + sqlite3 *db = get_magiskdb(); + db_settings dbs; + get_db_settings(db, &dbs, HIDE_CONFIG); + sqlite3_close_v2(db); + if (dbs[HIDE_CONFIG]) { + pthread_t thread; + xpthread_create(&thread, nullptr, [](void*) -> void* { + launch_magiskhide(-1); + return nullptr; + }, nullptr); + pthread_detach(thread); + } +} diff --git a/native/jni/magiskhide/magiskhide.cpp b/native/jni/magiskhide/magiskhide.cpp index 4a1d09a0f..8edf514e9 100644 --- a/native/jni/magiskhide/magiskhide.cpp +++ b/native/jni/magiskhide/magiskhide.cpp @@ -8,15 +8,11 @@ #include #include "magisk.h" -#include "utils.h" #include "magiskhide.h" #include "daemon.h" -#include "resetprop.h" #include "flags.h" bool hide_enabled = false; -static pthread_t proc_monitor_thread; -pthread_mutex_t list_lock; [[noreturn]] static void usage(char *arg0) { fprintf(stderr, @@ -33,60 +29,6 @@ pthread_mutex_t list_lock; exit(1); } -int launch_magiskhide(int client) { - if (hide_enabled) - return HIDE_IS_ENABLED; - - if (!log_daemon_started) { - setprop(MAGISKHIDE_PROP, "0"); - // Remove without actually removing persist props - deleteprop(MAGISKHIDE_PROP); - return LOGCAT_DISABLED; - } - - hide_enabled = true; - LOGI("* Starting MagiskHide\n"); - - deleteprop(MAGISKHIDE_PROP, true); - - hide_sensitive_props(); - - // Initialize the mutex lock - pthread_mutex_init(&list_lock, nullptr); - - // Initialize the hide list - if (!init_list()) - goto error; - - // Add SafetyNet by default - add_list("com.google.android.gms.unstable"); - - // Get thread reference - proc_monitor_thread = pthread_self(); - if (client >= 0) { - write_int(client, DAEMON_SUCCESS); - close(client); - } - // Start monitoring - proc_monitor(); - -error: - hide_enabled = false; - return DAEMON_ERROR; -} - -int stop_magiskhide() { - LOGI("* Stopping MagiskHide\n"); - - hide_enabled = false; - setprop(MAGISKHIDE_PROP, "0"); - // Remove without actually removing persist props - deleteprop(MAGISKHIDE_PROP); - pthread_kill(proc_monitor_thread, TERM_THREAD); - - return DAEMON_SUCCESS; -} - void magiskhide_handler(int client) { int req = read_int(client); int res = DAEMON_ERROR;