From 3b8ce85092fe8a8082e60ff0ccd6de95b006dc9b Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Wed, 15 Sep 2021 01:59:43 -0700 Subject: [PATCH] Enable Zygisk --- native/jni/core/module.cpp | 21 ++++++++++++++++++++- native/jni/include/magisk.hpp | 1 + native/jni/utils/files.cpp | 4 ++-- native/jni/utils/files.hpp | 2 +- native/jni/utils/xwrap.cpp | 2 +- native/jni/zygisk/entry.cpp | 2 +- 6 files changed, 26 insertions(+), 6 deletions(-) diff --git a/native/jni/core/module.cpp b/native/jni/core/module.cpp index 913fc8a5e..f9e882799 100644 --- a/native/jni/core/module.cpp +++ b/native/jni/core/module.cpp @@ -542,6 +542,19 @@ static void inject_magisk_bins(root_node *system) { delete bin->extract(init_applet[i]); } +#define mount_zygisk(bit) \ +if (access("/system/bin/app_process" #bit, F_OK) == 0) { \ + string zbin = zygisk_bin + "/app_process" #bit; \ + string mbin = MAGISKTMP + "/magisk" #bit; \ + int src = xopen(mbin.data(), O_RDONLY); \ + int out = xopen(zbin.data(), O_CREAT | O_WRONLY, 0); \ + xsendfile(out, src, nullptr, INT_MAX); \ + close(src); \ + close(out); \ + clone_attr("/system/bin/app_process" #bit, zbin.data()); \ + bind_mount(zbin.data(), "/system/bin/app_process" #bit); \ +} + void magic_mount() { node_entry::mirror_dir = MAGISKTMP + "/" MIRRDIR; node_entry::module_mnt = MAGISKTMP + "/" MODULEMNT "/"; @@ -592,7 +605,7 @@ void magic_mount() { for (const char *part : { "/vendor", "/product", "/system_ext" }) { struct stat st; if (lstat(part, &st) == 0 && S_ISDIR(st.st_mode)) { - if (auto old = system->extract(part + 1); old) { + if (auto old = system->extract(part + 1)) { auto new_node = new root_node(old); root->insert(new_node); } @@ -601,6 +614,12 @@ void magic_mount() { root->prepare(); root->mount(); + + // Mount on top of modules to enable zygisk + string zygisk_bin = MAGISKTMP + "/" ZYGISKBIN; + mkdir(zygisk_bin.data(), 0); + mount_zygisk(32) + mount_zygisk(64) } static void prepare_modules() { diff --git a/native/jni/include/magisk.hpp b/native/jni/include/magisk.hpp index 51c5896d0..147ffbe92 100644 --- a/native/jni/include/magisk.hpp +++ b/native/jni/include/magisk.hpp @@ -26,6 +26,7 @@ extern std::string MAGISKTMP; #define ROOTOVL INTLROOT "/rootdir" #define SHELLPTS INTLROOT "/pts" #define ROOTMNT ROOTOVL "/.mount_list" +#define ZYGISKBIN INTLROOT "/zygisk" constexpr const char *applet_names[] = { "su", "resetprop", nullptr }; constexpr const char *init_applet[] = { "magiskpolicy", "supolicy", nullptr }; diff --git a/native/jni/utils/files.cpp b/native/jni/utils/files.cpp index 525b4aea2..bd3fafe8f 100644 --- a/native/jni/utils/files.cpp +++ b/native/jni/utils/files.cpp @@ -26,7 +26,7 @@ int fd_pathat(int dirfd, const char *name, char *path, size_t size) { return 0; } -int mkdirs(string path, mode_t mode) { +int mkdirs(string_view path, mode_t mode) { errno = 0; for (char *p = path.data() + 1; *p; ++p) { if (*p == '/') { @@ -420,7 +420,7 @@ void restore_folder(const char *dir, vector &files) { for (raw_file &file : files) { string path = base + "/" + file.path; if (S_ISDIR(file.attr.st.st_mode)) { - mkdirs(path.data(), 0); + mkdirs(path, 0); } else if (S_ISREG(file.attr.st.st_mode)) { auto fp = xopen_file(path.data(), "we"); if (fp) fwrite(file.buf, 1, file.sz, fp.get()); diff --git a/native/jni/utils/files.hpp b/native/jni/utils/files.hpp index e5e38f898..8948535ae 100644 --- a/native/jni/utils/files.hpp +++ b/native/jni/utils/files.hpp @@ -32,7 +32,7 @@ struct raw_file { ssize_t fd_path(int fd, char *path, size_t size); int fd_pathat(int dirfd, const char *name, char *path, size_t size); -int mkdirs(std::string path, mode_t mode); +int mkdirs(std::string_view path, mode_t mode); void rm_rf(const char *path); void mv_path(const char *src, const char *dest); void mv_dir(int src, int dest); diff --git a/native/jni/utils/xwrap.cpp b/native/jni/utils/xwrap.cpp index 875c1c74a..565ea7b89 100644 --- a/native/jni/utils/xwrap.cpp +++ b/native/jni/utils/xwrap.cpp @@ -466,7 +466,7 @@ void *xmmap(void *addr, size_t length, int prot, int flags, ssize_t xsendfile(int out_fd, int in_fd, off_t *offset, size_t count) { ssize_t ret = sendfile(out_fd, in_fd, offset, count); - if (count != ret) { + if (ret < 0) { PLOGE("sendfile"); } return ret; diff --git a/native/jni/zygisk/entry.cpp b/native/jni/zygisk/entry.cpp index 310c78ca1..c1a04bd5c 100644 --- a/native/jni/zygisk/entry.cpp +++ b/native/jni/zygisk/entry.cpp @@ -247,7 +247,7 @@ static void setup_files(int client, ucred *cred) { write_int(client, 0); - string path = MAGISKTMP + "/zygisk." + basename(buf); + string path = MAGISKTMP + "/" ZYGISKBIN "/zygisk." + basename(buf); cp_afc(buf, (path + ".1.so").data()); cp_afc(buf, (path + ".2.so").data());