Allow ADB shell to remove modules and reboot

This commit is contained in:
topjohnwu 2019-09-13 03:14:21 -04:00
parent 86bfb22d4c
commit e31e687602
4 changed files with 60 additions and 45 deletions

View File

@ -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 <sys/mount.h> #include <sys/mount.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <stdlib.h> #include <stdlib.h>
@ -718,10 +711,7 @@ void late_start(int client) {
if (access(SECURE_DIR, F_OK) != 0) if (access(SECURE_DIR, F_OK) != 0)
xmkdir(SECURE_DIR, 0700); xmkdir(SECURE_DIR, 0700);
// And reboot to make proper setup possible // And reboot to make proper setup possible
if (RECOVERY_MODE) reboot();
exec_command_sync("/system/bin/reboot", "recovery");
else
exec_command_sync("/system/bin/reboot");
} }
if (access(BBPATH, F_OK) != 0){ if (access(BBPATH, F_OK) != 0){

View File

@ -1,9 +1,3 @@
/* daemon.c - Magisk Daemon
*
* Start the daemon and wait for requests
* Connect the daemon and send requests through sockets
*/
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
@ -30,14 +24,20 @@ static void verify_client(int client, pid_t pid) {
// Verify caller is the same as server // Verify caller is the same as server
char path[32]; char path[32];
sprintf(path, "/proc/%d/exe", pid); sprintf(path, "/proc/%d/exe", pid);
struct stat st{}; struct stat st;
stat(path, &st); if (stat(path, &st) || st.st_dev != SERVER_STAT.st_dev || st.st_ino != SERVER_STAT.st_ino) {
if (st.st_dev != SERVER_STAT.st_dev || st.st_ino != SERVER_STAT.st_ino) {
close(client); close(client);
pthread_exit(nullptr); 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) { static void *request_handler(void *args) {
int client = *((int *) args); int client = *((int *) args);
delete (int *) args; delete (int *) args;
@ -95,6 +95,16 @@ static void *request_handler(void *args) {
LOGD("* Use broadcasts for su logging and notify\n"); LOGD("* Use broadcasts for su logging and notify\n");
CONNECT_BROADCAST = true; CONNECT_BROADCAST = true;
close(client); 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: default:
close(client); close(client);
break; 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) { int switch_mnt_ns(int pid) {
char mnt[32]; char mnt[32];
snprintf(mnt, sizeof(mnt), "/proc/%d/ns/mnt", pid); snprintf(mnt, sizeof(mnt), "/proc/%d/ns/mnt", pid);

View File

@ -16,31 +16,33 @@ using namespace std::literals;
[[noreturn]] static void usage() { [[noreturn]] static void usage() {
fprintf(stderr, fprintf(stderr,
FULL_VER(Magisk) " multi-call binary\n" FULL_VER(Magisk) R"EOF( multi-call binary
"\n"
"Usage: magisk [applet [arguments]...]\n" Usage: magisk [applet [arguments]...]
" or: magisk [options]...\n" or: magisk [options]...
"\n"
"Options:\n" Options:
" -c print current binary version\n" -c print current binary version
" -v print running daemon version\n" -v print running daemon version
" -V print running daemon version code\n" -V print running daemon version code
" --list list all available applets\n" --list list all available applets
" --daemon manually start magisk daemon\n" --daemon manually start magisk daemon
" --[init trigger] start service for init trigger\n" --remove-modules remove all modules and reboot
"\n" --[init trigger] start service for init trigger
"Advanced Options (Internal APIs):\n"
" --unlock-blocks set BLKROSET flag to OFF for all block devices\n" Advanced Options (Internal APIs):
" --restorecon restore selinux context on Magisk files\n" --unlock-blocks set BLKROSET flag to OFF for all block devices
" --clone-attr SRC DEST clone permission, owner, and selinux context\n" --restorecon restore selinux context on Magisk files
" --clone SRC DEST clone SRC to DEST\n" --clone-attr SRC DEST clone permission, owner, and selinux context
" --sqlite SQL exec SQL to Magisk database\n" --clone SRC DEST clone SRC to DEST
" --use-broadcast use broadcast for su logging and notify\n" --sqlite SQL exec SQL commands to Magisk database
"\n" --use-broadcast use broadcast for su logging and notify
"Supported init triggers:\n"
" post-fs-data, service, boot-complete\n" Supported init triggers:
"\n" post-fs-data, service, boot-complete
"Supported applets:\n");
Supported applets:
)EOF");
for (int i = 0; applet_names[i]; ++i) for (int i = 0; applet_names[i]; ++i)
fprintf(stderr, i ? ", %s" : " %s", applet_names[i]); fprintf(stderr, i ? ", %s" : " %s", applet_names[i]);
@ -117,6 +119,10 @@ int magisk_main(int argc, char *argv[]) {
int fd = connect_daemon(); int fd = connect_daemon();
write_int(fd, BROADCAST_ACK); write_int(fd, BROADCAST_ACK);
return 0; return 0;
} else if (argv[1] == "--remove-modules"sv) {
int fd = connect_daemon();
write_int(fd, REMOVE_MODULES);
return read_int(fd);
} }
#if 0 #if 0
/* Entry point for testing stuffs */ /* Entry point for testing stuffs */

View File

@ -18,6 +18,7 @@ enum {
MAGISKHIDE, MAGISKHIDE,
SQLITE_CMD, SQLITE_CMD,
BROADCAST_ACK, BROADCAST_ACK,
REMOVE_MODULES,
}; };
// Return codes for daemon // Return codes for daemon
@ -32,6 +33,7 @@ enum {
int connect_daemon(bool create = false); int connect_daemon(bool create = false);
int switch_mnt_ns(int pid); int switch_mnt_ns(int pid);
void reboot();
// socket.c // socket.c