From e31e68760205427f1683a04353faea5724bdae32 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Fri, 13 Sep 2019 03:14:21 -0400 Subject: [PATCH] Allow ADB shell to remove modules and reboot --- native/jni/core/bootstages.cpp | 12 +------- native/jni/core/daemon.cpp | 35 +++++++++++++++------ native/jni/core/magisk.cpp | 56 +++++++++++++++++++--------------- native/jni/include/daemon.h | 2 ++ 4 files changed, 60 insertions(+), 45 deletions(-) diff --git a/native/jni/core/bootstages.cpp b/native/jni/core/bootstages.cpp index b31d81408..a848f967d 100644 --- a/native/jni/core/bootstages.cpp +++ b/native/jni/core/bootstages.cpp @@ -1,10 +1,3 @@ -/* bootstages.c - Core bootstage operations - * - * All bootstage operations, including simple mount in post-fs, - * magisk mount in post-fs-data, various image handling, script - * execution, load modules, install Magisk Manager etc. - */ - #include #include #include @@ -718,10 +711,7 @@ void late_start(int client) { if (access(SECURE_DIR, F_OK) != 0) xmkdir(SECURE_DIR, 0700); // And reboot to make proper setup possible - if (RECOVERY_MODE) - exec_command_sync("/system/bin/reboot", "recovery"); - else - exec_command_sync("/system/bin/reboot"); + reboot(); } if (access(BBPATH, F_OK) != 0){ diff --git a/native/jni/core/daemon.cpp b/native/jni/core/daemon.cpp index 0a72d82ef..274c41675 100644 --- a/native/jni/core/daemon.cpp +++ b/native/jni/core/daemon.cpp @@ -1,9 +1,3 @@ -/* daemon.c - Magisk Daemon - * - * Start the daemon and wait for requests - * Connect the daemon and send requests through sockets - */ - #include #include #include @@ -30,14 +24,20 @@ static void verify_client(int client, pid_t pid) { // Verify caller is the same as server char path[32]; sprintf(path, "/proc/%d/exe", pid); - struct stat st{}; - stat(path, &st); - if (st.st_dev != SERVER_STAT.st_dev || st.st_ino != SERVER_STAT.st_ino) { + struct stat st; + if (stat(path, &st) || st.st_dev != SERVER_STAT.st_dev || st.st_ino != SERVER_STAT.st_ino) { close(client); pthread_exit(nullptr); } } +static void remove_modules() { + LOGI("* Remove all modules and reboot"); + rm_rf(MODULEROOT); + rm_rf(MODULEUPGRADE); + reboot(); +} + static void *request_handler(void *args) { int client = *((int *) args); delete (int *) args; @@ -95,6 +95,16 @@ static void *request_handler(void *args) { LOGD("* Use broadcasts for su logging and notify\n"); CONNECT_BROADCAST = true; close(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); + } + close(client); + break; default: close(client); break; @@ -169,6 +179,13 @@ static void main_daemon() { } } +void reboot() { + if (RECOVERY_MODE) + exec_command_sync("/system/bin/reboot", "recovery"); + else + exec_command_sync("/system/bin/reboot"); +} + int switch_mnt_ns(int pid) { char mnt[32]; snprintf(mnt, sizeof(mnt), "/proc/%d/ns/mnt", pid); diff --git a/native/jni/core/magisk.cpp b/native/jni/core/magisk.cpp index 7d95b2018..a93ea1eaa 100644 --- a/native/jni/core/magisk.cpp +++ b/native/jni/core/magisk.cpp @@ -16,31 +16,33 @@ using namespace std::literals; [[noreturn]] static void usage() { fprintf(stderr, - FULL_VER(Magisk) " multi-call binary\n" - "\n" - "Usage: magisk [applet [arguments]...]\n" - " or: magisk [options]...\n" - "\n" - "Options:\n" - " -c print current binary version\n" - " -v print running daemon version\n" - " -V print running daemon version code\n" - " --list list all available applets\n" - " --daemon manually start magisk daemon\n" - " --[init trigger] start service for init trigger\n" - "\n" - "Advanced Options (Internal APIs):\n" - " --unlock-blocks set BLKROSET flag to OFF for all block devices\n" - " --restorecon restore selinux context on Magisk files\n" - " --clone-attr SRC DEST clone permission, owner, and selinux context\n" - " --clone SRC DEST clone SRC to DEST\n" - " --sqlite SQL exec SQL to Magisk database\n" - " --use-broadcast use broadcast for su logging and notify\n" - "\n" - "Supported init triggers:\n" - " post-fs-data, service, boot-complete\n" - "\n" - "Supported applets:\n"); +FULL_VER(Magisk) R"EOF( multi-call binary + +Usage: magisk [applet [arguments]...] + or: magisk [options]... + +Options: + -c print current binary version + -v print running daemon version + -V print running daemon version code + --list list all available applets + --daemon manually start magisk daemon + --remove-modules remove all modules and reboot + --[init trigger] start service for init trigger + +Advanced Options (Internal APIs): + --unlock-blocks set BLKROSET flag to OFF for all block devices + --restorecon restore selinux context on Magisk files + --clone-attr SRC DEST clone permission, owner, and selinux context + --clone SRC DEST clone SRC to DEST + --sqlite SQL exec SQL commands to Magisk database + --use-broadcast use broadcast for su logging and notify + +Supported init triggers: + post-fs-data, service, boot-complete + +Supported applets: +)EOF"); for (int i = 0; applet_names[i]; ++i) fprintf(stderr, i ? ", %s" : " %s", applet_names[i]); @@ -117,6 +119,10 @@ int magisk_main(int argc, char *argv[]) { int fd = connect_daemon(); write_int(fd, BROADCAST_ACK); return 0; + } else if (argv[1] == "--remove-modules"sv) { + int fd = connect_daemon(); + write_int(fd, REMOVE_MODULES); + return read_int(fd); } #if 0 /* Entry point for testing stuffs */ diff --git a/native/jni/include/daemon.h b/native/jni/include/daemon.h index 98751c91f..92e1b5b62 100644 --- a/native/jni/include/daemon.h +++ b/native/jni/include/daemon.h @@ -18,6 +18,7 @@ enum { MAGISKHIDE, SQLITE_CMD, BROADCAST_ACK, + REMOVE_MODULES, }; // Return codes for daemon @@ -32,6 +33,7 @@ enum { int connect_daemon(bool create = false); int switch_mnt_ns(int pid); +void reboot(); // socket.c