From 9f7d4109593def89cdbd78627781dd101aabc8e0 Mon Sep 17 00:00:00 2001 From: vvb2060 Date: Sun, 2 Apr 2023 22:42:25 +0800 Subject: [PATCH] Use pathname local socket --- native/src/core/daemon.cpp | 35 +++++++++++++++++++++++------------ native/src/include/magisk.hpp | 5 ++--- native/src/init/mount.cpp | 2 +- native/src/init/rootdir.cpp | 10 ---------- 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/native/src/core/daemon.cpp b/native/src/core/daemon.cpp index 4dee0fb13..ba0f28ee9 100644 --- a/native/src/core/daemon.cpp +++ b/native/src/core/daemon.cpp @@ -88,7 +88,7 @@ static void poll_ctrl_handler(pollfd *pfd) { int code = read_int(pfd->fd); switch (code) { case POLL_CTRL_NEW: { - pollfd new_fd; + pollfd new_fd{}; poll_callback cb; xxread(pfd->fd, &new_fd, sizeof(new_fd)); xxread(pfd->fd, &cb, sizeof(cb)); @@ -101,6 +101,8 @@ static void poll_ctrl_handler(pollfd *pfd) { unregister_poll(fd, auto_close); break; } + default: + __builtin_unreachable(); } } @@ -372,7 +374,7 @@ static void daemon_entry() { rm_rf((MAGISKTMP + "/" ROOTOVL).data()); // Load config status - auto config = MAGISKTMP + "/" INTLROOT "/config"; + auto config = MAGISKTMP + "/" MAIN_CONFIG; parse_prop_file(config.data(), [](auto key, auto val) -> bool { if (key == "RECOVERYMODE" && val == "true") RECOVERY_MODE = true; @@ -394,11 +396,14 @@ 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)) + sockaddr_un addr = {.sun_family = AF_LOCAL}; + strcpy(addr.sun_path, (MAGISKTMP + "/" MAIN_SOCKET).data()); + unlink(addr.sun_path); + if (xbind(fd, (sockaddr *) &addr, sizeof(addr))) exit(1); + chmod(addr.sun_path, 0666); + setfilecon(addr.sun_path, MAGISK_FILE_CON); xlisten(fd, 10); default_new(poll_map); @@ -414,10 +419,16 @@ static void daemon_entry() { } int connect_daemon(int req, bool create) { - 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)) { + int fd = xsocket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0); + sockaddr_un addr = {.sun_family = AF_LOCAL}; + string tmp; + for (const auto &info: parse_mount_info("self")) { + if (info.source == "magisk" && info.root == "/") { + tmp = info.target; + } + } + strcpy(addr.sun_path, (tmp + "/" MAIN_SOCKET).data()); + if (connect(fd, (sockaddr *) &addr, sizeof(addr))) { if (!create || getuid() != AID_ROOT) { LOGE("No daemon is currently running!\n"); close(fd); @@ -426,8 +437,8 @@ int connect_daemon(int req, bool create) { char buf[64]; xreadlink("/proc/self/exe", buf, sizeof(buf)); - if (str_starts(buf, "/system/bin/")) { - LOGE("Start daemon on /dev or /sbin\n"); + if (tmp.empty() || !str_starts(buf, tmp)) { + LOGE("Start daemon on magisk tmpfs\n"); close(fd); return -1; } @@ -437,7 +448,7 @@ int connect_daemon(int req, bool create) { daemon_entry(); } - while (connect(fd, (struct sockaddr *) &sun, len)) + while (connect(fd, (sockaddr *) &addr, sizeof(addr))) usleep(10000); } write_int(fd, req); diff --git a/native/src/include/magisk.hpp b/native/src/include/magisk.hpp index 30aac3170..3b7fcb3b1 100644 --- a/native/src/include/magisk.hpp +++ b/native/src/include/magisk.hpp @@ -2,9 +2,6 @@ #include -// magiskinit will hex patch this constant, -// appending \0 to prevent the compiler from reusing the string for "1" -#define MAIN_SOCKET "d30138f2310a9fb9c54a3e0c21f58591\0" #define JAVA_PACKAGE_NAME "com.topjohnwu.magisk" #define LOGFILE "/cache/magisk.log" #define SECURE_DIR "/data/adb" @@ -28,6 +25,8 @@ extern std::string MAGISKTMP; #define ROOTMNT ROOTOVL "/.mount_list" #define ZYGISKBIN INTLROOT "/zygisk" #define SELINUXMOCK INTLROOT "/selinux" +#define MAIN_CONFIG INTLROOT "/config" +#define MAIN_SOCKET INTLROOT "/socket" constexpr const char *applet_names[] = { "su", "resetprop", nullptr }; diff --git a/native/src/init/mount.cpp b/native/src/init/mount.cpp index 251a49405..8f774fb88 100644 --- a/native/src/init/mount.cpp +++ b/native/src/init/mount.cpp @@ -273,7 +273,7 @@ void MagiskInit::setup_tmp(const char *path) { mount_preinit_dir(path, preinit_dev); - cp_afc(".backup/.magisk", INTLROOT "/config"); + cp_afc(".backup/.magisk", MAIN_CONFIG); rm_rf(".backup"); // Create applet symlinks diff --git a/native/src/init/rootdir.cpp b/native/src/init/rootdir.cpp index 2b69f41a5..7bb1d7338 100644 --- a/native/src/init/rootdir.cpp +++ b/native/src/init/rootdir.cpp @@ -158,14 +158,6 @@ static void magic_mount(const string &sdir, const string &ddir = "") { } } -static void patch_socket_name(const char *path) { - static char rstr[16] = { 0 }; - if (rstr[0] == '\0') - gen_rand_str(rstr, sizeof(rstr)); - auto bin = mmap_data(path, true); - bin.patch({ make_pair(MAIN_SOCKET, rstr) }); -} - static void extract_files(bool sbin) { const char *m32 = sbin ? "/sbin/magisk32.xz" : "magisk32.xz"; const char *m64 = sbin ? "/sbin/magisk64.xz" : "magisk64.xz"; @@ -177,7 +169,6 @@ static void extract_files(bool sbin) { int fd = xopen("magisk32", O_WRONLY | O_CREAT, 0755); unxz(fd, magisk.buf, magisk.sz); close(fd); - patch_socket_name("magisk32"); } if (access(m64, F_OK) == 0) { auto magisk = mmap_data(m64); @@ -185,7 +176,6 @@ static void extract_files(bool sbin) { int fd = xopen("magisk64", O_WRONLY | O_CREAT, 0755); unxz(fd, magisk.buf, magisk.sz); close(fd); - patch_socket_name("magisk64"); xsymlink("./magisk64", "magisk"); } else { xsymlink("./magisk32", "magisk");