Preload modules

This commit is contained in:
Nullptr
2023-01-31 18:40:49 +08:00
parent d7877ea959
commit 335a1c3437
8 changed files with 33 additions and 20 deletions
+1 -6
View File
@@ -61,12 +61,7 @@ namespace zygiskd {
for (size_t i = 0; i < len; i++) { for (size_t i = 0; i < len; i++) {
std::string name = socket_utils::read_string(fd); std::string name = socket_utils::read_string(fd);
int module_fd = socket_utils::recv_fd(fd); int module_fd = socket_utils::recv_fd(fd);
auto handle = DlopenMem(module_fd, RTLD_NOW); modules.emplace_back(name, module_fd);
if (handle == nullptr) {
LOGW("Failed to dlopen module %s: %s", name.data(), dlerror());
continue;
}
modules.emplace_back(i, name, handle);
} }
return modules; return modules;
} }
+3 -4
View File
@@ -2,6 +2,7 @@
#include <string_view> #include <string_view>
#include <string> #include <string>
#include <unistd.h>
#include <vector> #include <vector>
#if defined(__LP64__) #if defined(__LP64__)
@@ -44,11 +45,9 @@ namespace zygiskd {
struct Module { struct Module {
std::string name; std::string name;
void* handle; UniqueFd memfd;
int id;
inline explicit Module(int id, std::string name, void* handle) inline explicit Module(std::string name, int memfd) : name(name), memfd(memfd) {}
: name(name), handle(handle), id(id) {}
}; };
enum class SocketAction { enum class SocketAction {
+11 -1
View File
@@ -5,6 +5,7 @@
using namespace std; using namespace std;
void *self_handle = nullptr; void *self_handle = nullptr;
vector<zygiskd::Module> preloaded_modules;
[[gnu::destructor]] [[maybe_unused]] [[gnu::destructor]] [[maybe_unused]]
static void zygisk_cleanup_wait() { static void zygisk_cleanup_wait() {
@@ -15,9 +16,18 @@ static void zygisk_cleanup_wait() {
} }
} }
void preload_modules() {
LOGI("Preload modules");
preloaded_modules = zygiskd::ReadModules();
for (auto& module : preloaded_modules) {
LOGD(" Preloaded `%s`", module.name.data());
}
}
extern "C" __used void entry(void *handle) { extern "C" __used void entry(void *handle) {
LOGD("load success"); LOGD("Load injector successful");
self_handle = handle; self_handle = handle;
preload_modules();
hook_functions(); hook_functions();
} }
+10 -6
View File
@@ -12,6 +12,7 @@
#include <daemon.h> #include <daemon.h>
#include <dirent.h> #include <dirent.h>
#include "dl.h"
#include "zygisk.hpp" #include "zygisk.hpp"
#include "memory.hpp" #include "memory.hpp"
#include "module.hpp" #include "module.hpp"
@@ -535,14 +536,17 @@ void HookContext::fork_post() {
} }
void HookContext::run_modules_pre() { void HookContext::run_modules_pre() {
auto ms = zygiskd::ReadModules(); size_t size = preloaded_modules.size();
modules.reserve(ms.size()); modules.reserve(size);
for (auto &m: ms) { for (size_t i = 0; i < size; i++) {
auto h = m.handle; auto& module = preloaded_modules[i];
if (void *e = dlsym(h, "zygisk_module_entry")) { if (void* handle = DlopenMem(module.memfd, RTLD_NOW);
modules.emplace_back(m.id, h, e); void* entry = handle ? dlsym(handle, "zygisk_module_entry") : nullptr) {
modules.emplace_back(i, handle, entry);
} }
} }
// memfds will be closed by RTTI
preloaded_modules.clear();
for (auto &m : modules) { for (auto &m : modules) {
m.onLoad(env); m.onLoad(env);
+3 -1
View File
@@ -4,7 +4,9 @@
#include <jni.h> #include <jni.h>
#include <vector> #include <vector>
#include "daemon.h"
extern void *self_handle; extern void *self_handle;
extern std::vector<zygiskd::Module> preloaded_modules;
void hook_functions(); void hook_functions();
+1
View File
@@ -49,6 +49,7 @@ ui_print "- Extracting module files"
extract "$ZIPFILE" 'daemon.sh' "$MODPATH" extract "$ZIPFILE" 'daemon.sh' "$MODPATH"
extract "$ZIPFILE" 'module.prop' "$MODPATH" extract "$ZIPFILE" 'module.prop' "$MODPATH"
extract "$ZIPFILE" 'post-fs-data.sh' "$MODPATH" extract "$ZIPFILE" 'post-fs-data.sh' "$MODPATH"
extract "$ZIPFILE" 'sepolicy.rule' "$MODPATH"
extract "$ZIPFILE" 'service.sh' "$MODPATH" extract "$ZIPFILE" 'service.sh' "$MODPATH"
HAS32BIT=false && [ -d "/system/lib" ] && HAS32BIT=true HAS32BIT=false && [ -d "/system/lib" ] && HAS32BIT=true
+2
View File
@@ -0,0 +1,2 @@
allow * tmpfs * *
allow system_server system_server process execmem
+2 -2
View File
@@ -99,8 +99,8 @@ fn load_modules(arch: &str) -> Result<Vec<Module>> {
for entry_result in dir.into_iter() { for entry_result in dir.into_iter() {
let entry = entry_result?; let entry = entry_result?;
let name = entry.file_name().into_string().unwrap(); let name = entry.file_name().into_string().unwrap();
let so_path = entry.path().join(format!("zygisksu/{arch}.so")); let so_path = entry.path().join(format!("zygisk/{arch}.so"));
let disabled = entry.path().join("disabled"); let disabled = entry.path().join("disable");
if !so_path.exists() || disabled.exists() { if !so_path.exists() || disabled.exists() {
continue; continue;
} }