diff --git a/native/jni/core/db.cpp b/native/jni/core/db.cpp index b2770f241..fe76eaad4 100644 --- a/native/jni/core/db.cpp +++ b/native/jni/core/db.cpp @@ -9,7 +9,7 @@ #include #include -#define DB_VERSION 8 +#define DB_VERSION 9 static sqlite3 *mDB = nullptr; @@ -99,23 +99,23 @@ static char *open_and_init_db(sqlite3 *&db) { if (ver < 3) { // Policies sqlite3_exec(db, - "CREATE TABLE IF NOT EXISTS policies " - "(uid INT, package_name TEXT, policy INT, until INT, " - "logging INT, notification INT, PRIMARY KEY(uid))", - nullptr, nullptr, &err); + "CREATE TABLE IF NOT EXISTS policies " + "(uid INT, package_name TEXT, policy INT, until INT, " + "logging INT, notification INT, PRIMARY KEY(uid))", + nullptr, nullptr, &err); err_ret(err); // Logs sqlite3_exec(db, - "CREATE TABLE IF NOT EXISTS logs " - "(from_uid INT, package_name TEXT, app_name TEXT, from_pid INT, " - "to_uid INT, action INT, time INT, command TEXT)", - nullptr, nullptr, &err); + "CREATE TABLE IF NOT EXISTS logs " + "(from_uid INT, package_name TEXT, app_name TEXT, from_pid INT, " + "to_uid INT, action INT, time INT, command TEXT)", + nullptr, nullptr, &err); err_ret(err); // Settings sqlite3_exec(db, - "CREATE TABLE IF NOT EXISTS settings " - "(key TEXT, value INT, PRIMARY KEY(key))", - nullptr, nullptr, &err); + "CREATE TABLE IF NOT EXISTS settings " + "(key TEXT, value INT, PRIMARY KEY(key))", + nullptr, nullptr, &err); err_ret(err); ver = 3; upgrade = true; @@ -123,9 +123,9 @@ static char *open_and_init_db(sqlite3 *&db) { if (ver < 4) { // Strings sqlite3_exec(db, - "CREATE TABLE IF NOT EXISTS strings " - "(key TEXT, value TEXT, PRIMARY KEY(key))", - nullptr, nullptr, &err); + "CREATE TABLE IF NOT EXISTS strings " + "(key TEXT, value TEXT, PRIMARY KEY(key))", + nullptr, nullptr, &err); err_ret(err); ver = 4; upgrade = true; @@ -133,28 +133,47 @@ static char *open_and_init_db(sqlite3 *&db) { if (ver < 5) { sqlite3_exec(db, "UPDATE policies SET uid=uid%100000", nullptr, nullptr, &err); err_ret(err); - /* Skip version 5 */ + /* Directly jump to version 6 */ ver = 6; upgrade = true; } if (ver < 7) { - // Hide list sqlite3_exec(db, - "CREATE TABLE IF NOT EXISTS hidelist " - "(process TEXT, PRIMARY KEY(process))", - nullptr, nullptr, &err); + "CREATE TABLE IF NOT EXISTS hidelist " + "(package_name TEXT, process TEXT, PRIMARY KEY(package_name, process));", + nullptr, nullptr, &err); err_ret(err); - ver = 7; + /* Directly jump to version 9 */ + ver = 9; upgrade = true; } if (ver < 8) { sqlite3_exec(db, - "ALTER TABLE hidelist ADD COLUMN package_name TEXT;" - "SELECT process FROM hidelist;" - "UPDATE hidelist SET package_name=process;", - nullptr, nullptr, &err); + "BEGIN TRANSACTION;" + "ALTER TABLE hidelist RENAME TO hidelist_tmp;" + "CREATE TABLE IF NOT EXISTS hidelist " + "(package_name TEXT, process TEXT, PRIMARY KEY(package_name, process));" + "INSERT INTO hidelist SELECT process as package_name, process FROM hidelist_tmp;" + "DROP TABLE hidelist_tmp;" + "COMMIT;", + nullptr, nullptr, &err); err_ret(err); - ver = 8; + /* Directly jump to version 9 */ + ver = 9; + upgrade = true; + } + if (ver < 9) { + sqlite3_exec(db, + "BEGIN TRANSACTION;" + "ALTER TABLE hidelist RENAME TO hidelist_tmp;" + "CREATE TABLE IF NOT EXISTS hidelist " + "(package_name TEXT, process TEXT, PRIMARY KEY(package_name, process));" + "INSERT INTO hidelist SELECT * FROM hidelist_tmp;" + "DROP TABLE hidelist_tmp;" + "COMMIT;", + nullptr, nullptr, &err); + err_ret(err); + ver = 9; upgrade = true; } diff --git a/native/jni/magiskhide/hide_utils.cpp b/native/jni/magiskhide/hide_utils.cpp index 485d483e5..4331f26d3 100644 --- a/native/jni/magiskhide/hide_utils.cpp +++ b/native/jni/magiskhide/hide_utils.cpp @@ -123,8 +123,9 @@ static int add_list(const char *pkg, const char *proc = "") { if (proc[0] == '\0') proc = pkg; - if (hide_map.count(proc)) - return HIDE_ITEM_EXIST; + for (auto &hide : hide_set) + if (hide.first == pkg && hide.second == proc) + return HIDE_ITEM_EXIST; // Add to database char sql[4096]; @@ -133,12 +134,12 @@ static int add_list(const char *pkg, const char *proc = "") { char *err = db_exec(sql); db_err_cmd(err, return DAEMON_ERROR); - LOGI("hide_list add: [%s]\n", proc); + LOGI("hide_list add: [%s/%s]\n", pkg, proc); // Critical region { MutexGuard lock(monitor_lock); - hide_map[proc] = pkg; + hide_set.emplace(pkg, proc); } kill_process(proc); @@ -160,24 +161,15 @@ static int rm_list(const char *pkg, const char *proc = "") { // Critical region MutexGuard lock(monitor_lock); bool remove = false; - if (proc[0] == '\0') { - auto next = hide_map.begin(); - decltype(next) cur; - while (next != hide_map.end()) { - cur = next; - ++next; - if (cur->second == pkg) { - remove = true; - LOGI("hide_list rm: [%s]\n", cur->first.data()); - hide_map.erase(cur); - } - } - } else { - auto it = hide_map.find(proc); - if (it != hide_map.end()) { + auto next = hide_set.begin(); + decltype(next) cur; + while (next != hide_set.end()) { + cur = next; + ++next; + if (cur->first == pkg && (proc[0] == '\0' || cur->second == proc)) { remove = true; - hide_map.erase(it); - LOGI("hide_list rm: [%s]\n", proc); + LOGI("hide_list rm: [%s]\n", cur->second.data()); + hide_set.erase(cur); } } if (!remove) @@ -188,7 +180,8 @@ static int rm_list(const char *pkg, const char *proc = "") { if (proc[0] == '\0') snprintf(sql, sizeof(sql), "DELETE FROM hidelist WHERE package_name='%s'", pkg); else - snprintf(sql, sizeof(sql), "DELETE FROM hidelist WHERE process='%s'", proc); + snprintf(sql, sizeof(sql), + "DELETE FROM hidelist WHERE package_name='%s' AND process='%s'", pkg, proc); char *err = db_exec(sql); db_err(err); return DAEMON_SUCCESS; @@ -206,8 +199,8 @@ int rm_list(int client) { } static void init_list(const char *pkg, const char *proc) { - LOGI("hide_list init: [%s]\n", proc); - hide_map[proc] = pkg; + LOGI("hide_list init: [%s/%s]\n", pkg, proc); + hide_set.emplace(pkg, proc); kill_process(proc); } @@ -242,6 +235,7 @@ bool init_list() { // Add SafetyNet by default rm_list(SAFETYNET_COMPONENT); + rm_list(SAFETYNET_PROCESS); init_list(SAFETYNET_PKG, SAFETYNET_PROCESS); update_uid_map(); @@ -250,8 +244,8 @@ bool init_list() { void ls_list(int client) { FILE *out = fdopen(recv_fd(client), "a"); - for (auto &s : hide_map) - fprintf(out, "%s|%s\n", s.second.data(), s.first.data()); + for (auto &hide : hide_set) + fprintf(out, "%s|%s\n", hide.first.data(), hide.second.data()); fclose(out); write_int(client, DAEMON_SUCCESS); close(client); diff --git a/native/jni/magiskhide/magiskhide.h b/native/jni/magiskhide/magiskhide.h index cb67ca00e..5d5853735 100644 --- a/native/jni/magiskhide/magiskhide.h +++ b/native/jni/magiskhide/magiskhide.h @@ -8,6 +8,7 @@ #include #include #include +#include #include "daemon.h" @@ -54,7 +55,7 @@ static inline int parse_int(const char *s) { extern bool hide_enabled; extern pthread_mutex_t monitor_lock; -extern std::map hide_map; +extern std::set> hide_set; extern int next_zygote; enum { diff --git a/native/jni/magiskhide/proc_monitor.cpp b/native/jni/magiskhide/proc_monitor.cpp index cfd488711..7bd9b7678 100644 --- a/native/jni/magiskhide/proc_monitor.cpp +++ b/native/jni/magiskhide/proc_monitor.cpp @@ -19,7 +19,6 @@ #include #include #include -#include #include #include @@ -39,7 +38,7 @@ static void new_zygote(int pid); * All the maps and sets ************************/ -map hide_map; /* process -> package_name */ +set> hide_set; /* set of pair */ static map zygote_map; /* zygote pid -> mnt ns */ static map> uid_proc_map; /* uid -> list of process */ @@ -88,7 +87,7 @@ static bool parse_packages_xml(string_view s) { start += 9; /* Skip '