From 5fd574a14f6de5cc98b513c884a4269b001d6ba5 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Thu, 30 Apr 2020 01:26:50 -0700 Subject: [PATCH] Fix --remove-modules command --- native/jni/core/bootstages.cpp | 22 +------------ native/jni/core/daemon.cpp | 16 ++++++---- native/jni/core/module.cpp | 20 ++++++++++++ native/jni/include/daemon.hpp | 46 +++++++++------------------- native/jni/magiskhide/hide_utils.cpp | 1 + 5 files changed, 46 insertions(+), 59 deletions(-) diff --git a/native/jni/core/bootstages.cpp b/native/jni/core/bootstages.cpp index 4ad505520..9ae198a00 100644 --- a/native/jni/core/bootstages.cpp +++ b/native/jni/core/bootstages.cpp @@ -19,12 +19,9 @@ using namespace std; -extern vector module_list; static bool no_secure_dir = false; static bool pfs_done = false; -extern void auto_start_magiskhide(); - /********* * Setup * *********/ @@ -139,30 +136,13 @@ static bool magisk_env() { return true; } -static void reboot() { +void reboot() { if (RECOVERY_MODE) exec_command_sync("/system/bin/reboot", "recovery"); else exec_command_sync("/system/bin/reboot"); } -void remove_modules() { - LOGI("* Remove all modules and reboot"); - auto dir = xopen_dir(MODULEROOT); - int dfd = dirfd(dir.get()); - for (dirent *entry; (entry = xreaddir(dir.get()));) { - if (entry->d_type == DT_DIR) { - if (entry->d_name == ".core"sv) - continue; - - int modfd = xopenat(dfd, entry->d_name, O_RDONLY | O_CLOEXEC); - close(xopenat(modfd, "remove", O_RDONLY | O_CREAT | O_CLOEXEC)); - close(modfd); - } - } - reboot(); -} - static bool check_data() { bool mnt = false; bool data = false; diff --git a/native/jni/core/daemon.cpp b/native/jni/core/daemon.cpp index 729ab26f7..920cd010b 100644 --- a/native/jni/core/daemon.cpp +++ b/native/jni/core/daemon.cpp @@ -56,6 +56,14 @@ static void *request_handler(void *args) { close(client); return nullptr; } + break; + case REMOVE_MODULES: + if (credential.uid != UID_SHELL && credential.uid != UID_ROOT) { + write_int(client, 1); + close(client); + return nullptr; + } + break; default: break; } @@ -88,12 +96,8 @@ static void *request_handler(void *args) { exec_sql(client); break; case REMOVE_MODULES: - if (credential.uid == UID_SHELL || credential.uid == UID_ROOT) { - remove_modules(); - write_int(client, 0); - } else { - write_int(client, 1); - } + remove_modules(); + write_int(client, 0); close(client); break; case GET_PATH: diff --git a/native/jni/core/module.cpp b/native/jni/core/module.cpp index f374bd867..36f734dcc 100644 --- a/native/jni/core/module.cpp +++ b/native/jni/core/module.cpp @@ -686,3 +686,23 @@ void handle_modules() { mount_modules(); } + +void remove_modules() { + LOGI("* Remove all modules and reboot\n"); + auto dir = open_dir(MODULEROOT); + if (!dir) + return; + + int dfd = dirfd(dir.get()); + for (dirent *entry; (entry = xreaddir(dir.get()));) { + if (entry->d_type == DT_DIR) { + if (entry->d_name == ".core"sv) + continue; + + int modfd = xopenat(dfd, entry->d_name, O_RDONLY | O_CLOEXEC); + close(xopenat(modfd, "remove", O_RDONLY | O_CREAT | O_CLOEXEC, 0)); + close(modfd); + } + } + reboot(); +} diff --git a/native/jni/include/daemon.hpp b/native/jni/include/daemon.hpp index c9c03d5a9..a34c9e025 100644 --- a/native/jni/include/daemon.hpp +++ b/native/jni/include/daemon.hpp @@ -29,46 +29,28 @@ enum { DAEMON_LAST }; -// daemon.cpp +extern int SDK_INT; +extern bool RECOVERY_MODE; +extern std::vector module_list; +#define APP_DATA_DIR (SDK_INT >= 24 ? "/data/user_de" : "/data/user") -int connect_daemon(bool create = false); - -/*************** - * Boot Stages * - ***************/ - -void unlock_blocks(); +// Daemon handlers void post_fs_data(int client); void late_start(int client); void boot_complete(int client); -void handle_modules(); +void magiskhide_handler(int client); +void su_daemon_handler(int client, struct ucred *credential); void remove_modules(); -/************* - * Scripting * - *************/ +// Misc +int connect_daemon(bool create = false); +void auto_start_magiskhide(); +void unlock_blocks(); +void handle_modules(); +void reboot(); +// Scripting void exec_script(const char *script); void exec_common_script(const char *stage); void exec_module_script(const char *stage, const std::vector &module_list); void install_apk(const char *apk); - -/************** - * MagiskHide * - **************/ - -void magiskhide_handler(int client); - -/************* - * Superuser * - *************/ - -void su_daemon_handler(int client, struct ucred *credential); - -/********************* - * Daemon Global Vars - *********************/ - -extern int SDK_INT; -extern bool RECOVERY_MODE; -#define APP_DATA_DIR (SDK_INT >= 24 ? "/data/user_de" : "/data/user") diff --git a/native/jni/magiskhide/hide_utils.cpp b/native/jni/magiskhide/hide_utils.cpp index ed65cb788..3a804b379 100644 --- a/native/jni/magiskhide/hide_utils.cpp +++ b/native/jni/magiskhide/hide_utils.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include "magiskhide.hpp"