Use pathname local socket

This commit is contained in:
vvb2060 2023-04-02 22:42:25 +08:00 committed by John Wu
parent bc94ea4334
commit 9f7d410959
4 changed files with 26 additions and 26 deletions

View File

@ -88,7 +88,7 @@ static void poll_ctrl_handler(pollfd *pfd) {
int code = read_int(pfd->fd); int code = read_int(pfd->fd);
switch (code) { switch (code) {
case POLL_CTRL_NEW: { case POLL_CTRL_NEW: {
pollfd new_fd; pollfd new_fd{};
poll_callback cb; poll_callback cb;
xxread(pfd->fd, &new_fd, sizeof(new_fd)); xxread(pfd->fd, &new_fd, sizeof(new_fd));
xxread(pfd->fd, &cb, sizeof(cb)); xxread(pfd->fd, &cb, sizeof(cb));
@ -101,6 +101,8 @@ static void poll_ctrl_handler(pollfd *pfd) {
unregister_poll(fd, auto_close); unregister_poll(fd, auto_close);
break; break;
} }
default:
__builtin_unreachable();
} }
} }
@ -372,7 +374,7 @@ static void daemon_entry() {
rm_rf((MAGISKTMP + "/" ROOTOVL).data()); rm_rf((MAGISKTMP + "/" ROOTOVL).data());
// Load config status // Load config status
auto config = MAGISKTMP + "/" INTLROOT "/config"; auto config = MAGISKTMP + "/" MAIN_CONFIG;
parse_prop_file(config.data(), [](auto key, auto val) -> bool { parse_prop_file(config.data(), [](auto key, auto val) -> bool {
if (key == "RECOVERYMODE" && val == "true") if (key == "RECOVERYMODE" && val == "true")
RECOVERY_MODE = 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); 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); exit(1);
chmod(addr.sun_path, 0666);
setfilecon(addr.sun_path, MAGISK_FILE_CON);
xlisten(fd, 10); xlisten(fd, 10);
default_new(poll_map); default_new(poll_map);
@ -414,10 +419,16 @@ static void daemon_entry() {
} }
int connect_daemon(int req, bool create) { int connect_daemon(int req, bool create) {
sockaddr_un sun{}; int fd = xsocket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
socklen_t len = setup_sockaddr(&sun, MAIN_SOCKET); sockaddr_un addr = {.sun_family = AF_LOCAL};
int fd = xsocket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); string tmp;
if (connect(fd, (sockaddr *) &sun, len)) { 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) { if (!create || getuid() != AID_ROOT) {
LOGE("No daemon is currently running!\n"); LOGE("No daemon is currently running!\n");
close(fd); close(fd);
@ -426,8 +437,8 @@ int connect_daemon(int req, bool create) {
char buf[64]; char buf[64];
xreadlink("/proc/self/exe", buf, sizeof(buf)); xreadlink("/proc/self/exe", buf, sizeof(buf));
if (str_starts(buf, "/system/bin/")) { if (tmp.empty() || !str_starts(buf, tmp)) {
LOGE("Start daemon on /dev or /sbin\n"); LOGE("Start daemon on magisk tmpfs\n");
close(fd); close(fd);
return -1; return -1;
} }
@ -437,7 +448,7 @@ int connect_daemon(int req, bool create) {
daemon_entry(); daemon_entry();
} }
while (connect(fd, (struct sockaddr *) &sun, len)) while (connect(fd, (sockaddr *) &addr, sizeof(addr)))
usleep(10000); usleep(10000);
} }
write_int(fd, req); write_int(fd, req);

View File

@ -2,9 +2,6 @@
#include <string> #include <string>
// 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 JAVA_PACKAGE_NAME "com.topjohnwu.magisk"
#define LOGFILE "/cache/magisk.log" #define LOGFILE "/cache/magisk.log"
#define SECURE_DIR "/data/adb" #define SECURE_DIR "/data/adb"
@ -28,6 +25,8 @@ extern std::string MAGISKTMP;
#define ROOTMNT ROOTOVL "/.mount_list" #define ROOTMNT ROOTOVL "/.mount_list"
#define ZYGISKBIN INTLROOT "/zygisk" #define ZYGISKBIN INTLROOT "/zygisk"
#define SELINUXMOCK INTLROOT "/selinux" #define SELINUXMOCK INTLROOT "/selinux"
#define MAIN_CONFIG INTLROOT "/config"
#define MAIN_SOCKET INTLROOT "/socket"
constexpr const char *applet_names[] = { "su", "resetprop", nullptr }; constexpr const char *applet_names[] = { "su", "resetprop", nullptr };

View File

@ -273,7 +273,7 @@ void MagiskInit::setup_tmp(const char *path) {
mount_preinit_dir(path, preinit_dev); mount_preinit_dir(path, preinit_dev);
cp_afc(".backup/.magisk", INTLROOT "/config"); cp_afc(".backup/.magisk", MAIN_CONFIG);
rm_rf(".backup"); rm_rf(".backup");
// Create applet symlinks // Create applet symlinks

View File

@ -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) { static void extract_files(bool sbin) {
const char *m32 = sbin ? "/sbin/magisk32.xz" : "magisk32.xz"; const char *m32 = sbin ? "/sbin/magisk32.xz" : "magisk32.xz";
const char *m64 = sbin ? "/sbin/magisk64.xz" : "magisk64.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); int fd = xopen("magisk32", O_WRONLY | O_CREAT, 0755);
unxz(fd, magisk.buf, magisk.sz); unxz(fd, magisk.buf, magisk.sz);
close(fd); close(fd);
patch_socket_name("magisk32");
} }
if (access(m64, F_OK) == 0) { if (access(m64, F_OK) == 0) {
auto magisk = mmap_data(m64); auto magisk = mmap_data(m64);
@ -185,7 +176,6 @@ static void extract_files(bool sbin) {
int fd = xopen("magisk64", O_WRONLY | O_CREAT, 0755); int fd = xopen("magisk64", O_WRONLY | O_CREAT, 0755);
unxz(fd, magisk.buf, magisk.sz); unxz(fd, magisk.buf, magisk.sz);
close(fd); close(fd);
patch_socket_name("magisk64");
xsymlink("./magisk64", "magisk"); xsymlink("./magisk64", "magisk");
} else { } else {
xsymlink("./magisk32", "magisk"); xsymlink("./magisk32", "magisk");