Refactor daemon connection

This commit is contained in:
LoveSy
2022-02-12 23:43:36 +08:00
committed by John Wu
parent c82a46c1ee
commit 7999b66c3c
14 changed files with 312 additions and 237 deletions

View File

@@ -14,6 +14,7 @@
#include <selinux.hpp>
#include "core.hpp"
#include "zygisk/deny/deny.hpp"
using namespace std;

View File

@@ -86,20 +86,20 @@ void clear_poll() {
static void poll_ctrl_handler(pollfd *pfd) {
int code = read_int(pfd->fd);
switch (code) {
case POLL_CTRL_NEW: {
pollfd new_fd;
poll_callback cb;
xxread(pfd->fd, &new_fd, sizeof(new_fd));
xxread(pfd->fd, &cb, sizeof(cb));
register_poll(&new_fd, cb);
break;
}
case POLL_CTRL_RM: {
int fd = read_int(pfd->fd);
bool auto_close = read_int(pfd->fd);
unregister_poll(fd, auto_close);
break;
}
case POLL_CTRL_NEW: {
pollfd new_fd;
poll_callback cb;
xxread(pfd->fd, &new_fd, sizeof(new_fd));
xxread(pfd->fd, &cb, sizeof(cb));
register_poll(&new_fd, cb);
break;
}
case POLL_CTRL_RM: {
int fd = read_int(pfd->fd);
bool auto_close = read_int(pfd->fd);
unregister_poll(fd, auto_close);
break;
}
}
}
@@ -133,61 +133,63 @@ static void poll_ctrl_handler(pollfd *pfd) {
}
}
static void handle_request_async(int client, int code, const sock_cred &cred) {
static void handle_request_async(int client, DaemonRequest code, const sock_cred &cred) {
// using enum DAEMON_REQUEST;
switch (code) {
case DENYLIST:
case DaemonRequest::DENYLIST:
denylist_handler(client, &cred);
break;
case SUPERUSER:
case DaemonRequest::SUPERUSER:
su_daemon_handler(client, &cred);
break;
case POST_FS_DATA:
case DaemonRequest::POST_FS_DATA:
post_fs_data(client);
break;
case LATE_START:
case DaemonRequest::LATE_START:
late_start(client);
break;
case BOOT_COMPLETE:
case DaemonRequest::BOOT_COMPLETE:
boot_complete(client);
break;
case SQLITE_CMD:
case DaemonRequest::SQLITE_CMD:
exec_sql(client);
break;
case REMOVE_MODULES:
case DaemonRequest::REMOVE_MODULES:
remove_modules();
write_int(client, 0);
close(client);
reboot();
break;
case ZYGISK_REQUEST:
case ZYGISK_PASSTHROUGH:
case DaemonRequest::ZYGISK_REQUEST:
zygisk_handler(client, &cred);
break;
default:
close(client);
break;
__builtin_unreachable();
}
}
static void handle_request_sync(int client, int code) {
static void handle_request_sync(int client, DaemonRequest code) {
// using enum DAEMON_REQUEST;
switch (code) {
case CHECK_VERSION:
case DaemonRequest::CHECK_VERSION:
write_string(client, MAGISK_VERSION ":MAGISK");
break;
case CHECK_VERSION_CODE:
case DaemonRequest::CHECK_VERSION_CODE:
write_int(client, MAGISK_VER_CODE);
break;
case GET_PATH:
case DaemonRequest::GET_PATH:
write_string(client, MAGISKTMP.data());
break;
case START_DAEMON:
case DaemonRequest::START_DAEMON:
setup_logfile(true);
break;
case STOP_DAEMON:
case DaemonRequest::STOP_DAEMON:
denylist_handler(-1, nullptr);
write_int(client, 0);
// Terminate the daemon!
exit(0);
default:
__builtin_unreachable();
}
}
@@ -195,11 +197,17 @@ static bool is_client(pid_t pid) {
// Verify caller is the same as server
char path[32];
sprintf(path, "/proc/%d/exe", pid);
struct stat st;
struct stat st{};
return !(stat(path, &st) || st.st_dev != self_st.st_dev || st.st_ino != self_st.st_ino);
}
inline static void write_response(int client, DaemonResponse res) {
write_int(client, static_cast<std::underlying_type_t<DaemonResponse>>(res));
}
static void handle_request(pollfd *pfd) {
// using enum DAEMON_REQUEST;
// using enum DAEMON_RESPONSE;
int client = xaccept4(pfd->fd, nullptr, nullptr, SOCK_CLOEXEC);
// Verify client credentials
@@ -207,6 +215,7 @@ static void handle_request(pollfd *pfd) {
bool is_root;
bool is_zygote;
int code;
DaemonRequest req;
if (!get_client_cred(client, &cred))
goto done;
@@ -217,44 +226,46 @@ static void handle_request(pollfd *pfd) {
goto done;
code = read_int(client);
if (code < 0 || (code & DAEMON_CODE_MASK) >= DAEMON_CODE_END)
static_assert(is_scoped_enum_v<DaemonRequest>);
if (code < 0 || code >= DaemonRequest::END || code == DaemonRequest::_SYNC_BARRIER_)
goto done;
req = static_cast<DaemonRequest>(code);
// Check client permissions
switch (code) {
case POST_FS_DATA:
case LATE_START:
case BOOT_COMPLETE:
case SQLITE_CMD:
case GET_PATH:
case DENYLIST:
case STOP_DAEMON:
switch (req) {
case DaemonRequest::POST_FS_DATA:
case DaemonRequest::LATE_START:
case DaemonRequest::BOOT_COMPLETE:
case DaemonRequest::SQLITE_CMD:
case DaemonRequest::GET_PATH:
case DaemonRequest::DENYLIST:
case DaemonRequest::STOP_DAEMON:
if (!is_root) {
write_int(client, ROOT_REQUIRED);
write_response(client, DaemonResponse::ROOT_REQUIRED);
goto done;
}
break;
case REMOVE_MODULES:
case DaemonRequest::REMOVE_MODULES:
if (!is_root && cred.uid != UID_SHELL) {
write_int(client, 1);
write_response(client, DaemonResponse::ROOT_REQUIRED);
goto done;
}
break;
case ZYGISK_REQUEST:
if (!is_zygote) {
write_int(client, DAEMON_ERROR);
goto done;
}
default:
break;
}
if (code & SYNC_FLAG) {
handle_request_sync(client, code);
write_response(client, DaemonResponse::OK);
if (req < DaemonRequest::_SYNC_BARRIER_) {
handle_request_sync(client, req);
goto done;
}
// Handle complex requests in another thread
exec_task([=] { handle_request_async(client, code, cred); });
exec_task([=] { handle_request_async(client, req, cred); });
return;
done:
@@ -376,7 +387,7 @@ static void daemon_entry() {
sockaddr_un sun{};
socklen_t len = setup_sockaddr(&sun, MAIN_SOCKET);
fd = xsocket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
if (xbind(fd, (sockaddr*) &sun, len))
if (xbind(fd, (sockaddr *) &sun, len))
exit(1);
xlisten(fd, 10);
@@ -391,11 +402,12 @@ static void daemon_entry() {
poll_loop();
}
int connect_daemon(bool create) {
int connect_daemon(DaemonRequest req, bool create) {
// using enum DAEMON_RESPONSE;
sockaddr_un sun{};
socklen_t len = setup_sockaddr(&sun, MAIN_SOCKET);
int fd = xsocket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
if (connect(fd, (sockaddr*) &sun, len)) {
if (connect(fd, (sockaddr *) &sun, len)) {
if (!create || getuid() != UID_ROOT) {
LOGE("No daemon is currently running!\n");
close(fd);
@@ -407,8 +419,27 @@ int connect_daemon(bool create) {
daemon_entry();
}
while (connect(fd, (struct sockaddr*) &sun, len))
while (connect(fd, (struct sockaddr *) &sun, len))
usleep(10000);
}
write_int(fd, static_cast<int>(req));
int ret = read_int(fd);
auto res = (ret < DaemonResponse::ERROR || ret >= DaemonResponse::END) ? DaemonResponse::ERROR
: static_cast<DaemonResponse>(ret);
switch (res) {
case DaemonResponse::OK:
break;
case DaemonResponse::ERROR:
LOGE("Daemon error\n");
exit(-1);
case DaemonResponse::ROOT_REQUIRED:
LOGE("Root is required for this operation\n");
exit(-1);
case DaemonResponse::INVALID_REQUEST:
LOGE("Invalid request\n");
exit(-1);
case DaemonResponse::END:
__builtin_unreachable();
}
return fd;
}

View File

@@ -50,20 +50,19 @@ Available applets:
}
int magisk_main(int argc, char *argv[]) {
// using enum DAEMON_REQUEST;
if (argc < 2)
usage();
if (argv[1] == "-c"sv) {
printf(MAGISK_VERSION ":MAGISK (" str(MAGISK_VER_CODE) ")\n");
return 0;
} else if (argv[1] == "-v"sv) {
int fd = connect_daemon();
write_int(fd, CHECK_VERSION);
int fd = connect_daemon(DaemonRequest::CHECK_VERSION);
string v = read_string(fd);
printf("%s\n", v.data());
return 0;
} else if (argv[1] == "-V"sv) {
int fd = connect_daemon();
write_int(fd, CHECK_VERSION_CODE);
int fd = connect_daemon(DaemonRequest::CHECK_VERSION_CODE);
printf("%d\n", read_int(fd));
return 0;
} else if (argv[1] == "--list"sv) {
@@ -76,37 +75,32 @@ int magisk_main(int argc, char *argv[]) {
} else if (argv[1] == "--restorecon"sv) {
restorecon();
return 0;
} else if (argc >= 4 && argv[1] == "--clone-attr"sv) {;
} else if (argc >= 4 && argv[1] == "--clone-attr"sv) {
clone_attr(argv[2], argv[3]);
return 0;
} else if (argc >= 4 && argv[1] == "--clone"sv) {
cp_afc(argv[2], argv[3]);
return 0;
} else if (argv[1] == "--daemon"sv) {
int fd = connect_daemon(true);
write_int(fd, START_DAEMON);
int fd = connect_daemon(DaemonRequest::START_DAEMON, true);
close(fd);
return 0;
} else if (argv[1] == "--stop"sv) {
int fd = connect_daemon();
write_int(fd, STOP_DAEMON);
int fd = connect_daemon(DaemonRequest::STOP_DAEMON);
return read_int(fd);
} else if (argv[1] == "--post-fs-data"sv) {
int fd = connect_daemon(true);
write_int(fd, POST_FS_DATA);
int fd = connect_daemon(DaemonRequest::POST_FS_DATA, true);
return read_int(fd);
} else if (argv[1] == "--service"sv) {
int fd = connect_daemon(true);
write_int(fd, LATE_START);
int fd = connect_daemon(DaemonRequest::LATE_START, true);
return read_int(fd);
} else if (argv[1] == "--boot-complete"sv) {
int fd = connect_daemon(true);
write_int(fd, BOOT_COMPLETE);
int fd = connect_daemon(DaemonRequest::BOOT_COMPLETE, true);
return read_int(fd);
} else if (argv[1] == "--denylist"sv) {
return denylist_cli(argc - 1, argv + 1);
}else if (argc >= 3 && argv[1] == "--sqlite"sv) {
int fd = connect_daemon();
write_int(fd, SQLITE_CMD);
} else if (argc >= 3 && argv[1] == "--sqlite"sv) {
int fd = connect_daemon(DaemonRequest::SQLITE_CMD);
write_string(fd, argv[2]);
string res;
for (;;) {
@@ -116,12 +110,10 @@ int magisk_main(int argc, char *argv[]) {
printf("%s\n", res.data());
}
} else if (argv[1] == "--remove-modules"sv) {
int fd = connect_daemon();
write_int(fd, REMOVE_MODULES);
int fd = connect_daemon(DaemonRequest::REMOVE_MODULES);
return read_int(fd);
} else if (argv[1] == "--path"sv) {
int fd = connect_daemon();
write_int(fd, GET_PATH);
int fd = connect_daemon(DaemonRequest::GET_PATH);
string path = read_string(fd);
printf("%s\n", path.data());
return 0;