You've already forked ReZygisk
mirror of
https://github.com/PerformanC/ReZygisk.git
synced 2025-09-06 06:37:01 +00:00
876 lines
26 KiB
C++
876 lines
26 KiB
C++
#include <android/dlext.h>
|
|
#include <sys/mount.h>
|
|
#include <dlfcn.h>
|
|
#include <regex.h>
|
|
#include <bitset>
|
|
#include <list>
|
|
#include <map>
|
|
#include <array>
|
|
|
|
#include <lsplt.hpp>
|
|
|
|
#include <fcntl.h>
|
|
#include <sys/prctl.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/mman.h>
|
|
#include <unistd.h>
|
|
|
|
#include "daemon.h"
|
|
#include "zygisk.hpp"
|
|
#include "module.hpp"
|
|
#include "files.hpp"
|
|
#include "misc.hpp"
|
|
|
|
#include "solist.hpp"
|
|
|
|
#include "art_method.hpp"
|
|
|
|
using namespace std;
|
|
|
|
static void hook_unloader();
|
|
static void unhook_functions();
|
|
|
|
namespace {
|
|
|
|
enum {
|
|
POST_SPECIALIZE,
|
|
APP_FORK_AND_SPECIALIZE,
|
|
APP_SPECIALIZE,
|
|
SERVER_FORK_AND_SPECIALIZE,
|
|
DO_REVERT_UNMOUNT,
|
|
SKIP_FD_SANITIZATION,
|
|
|
|
FLAG_MAX
|
|
};
|
|
|
|
#define DCL_PRE_POST(name) \
|
|
void name##_pre(); \
|
|
void name##_post();
|
|
|
|
#define MAX_FD_SIZE 1024
|
|
|
|
struct ZygiskContext;
|
|
|
|
// Current context
|
|
ZygiskContext *g_ctx;
|
|
|
|
struct ZygiskContext {
|
|
JNIEnv *env;
|
|
union {
|
|
void *ptr;
|
|
AppSpecializeArgs_v5 *app;
|
|
ServerSpecializeArgs_v1 *server;
|
|
} args;
|
|
|
|
const char *process;
|
|
list<ZygiskModule> modules;
|
|
|
|
int pid;
|
|
bitset<FLAG_MAX> flags;
|
|
uint32_t info_flags;
|
|
bitset<MAX_FD_SIZE> allowed_fds;
|
|
vector<int> exempted_fds;
|
|
|
|
struct RegisterInfo {
|
|
regex_t regex;
|
|
string symbol;
|
|
void *callback;
|
|
void **backup;
|
|
};
|
|
|
|
struct IgnoreInfo {
|
|
regex_t regex;
|
|
string symbol;
|
|
};
|
|
|
|
pthread_mutex_t hook_info_lock;
|
|
vector<RegisterInfo> register_info;
|
|
vector<IgnoreInfo> ignore_info;
|
|
|
|
ZygiskContext(JNIEnv *env, void *args) :
|
|
env(env), args{args}, process(nullptr), pid(-1), info_flags(0),
|
|
hook_info_lock(PTHREAD_MUTEX_INITIALIZER) {
|
|
g_ctx = this;
|
|
}
|
|
~ZygiskContext();
|
|
|
|
/* Zygisksu changed: Load module fds */
|
|
void run_modules_pre();
|
|
void run_modules_post();
|
|
DCL_PRE_POST(fork)
|
|
DCL_PRE_POST(app_specialize)
|
|
DCL_PRE_POST(nativeForkAndSpecialize)
|
|
DCL_PRE_POST(nativeSpecializeAppProcess)
|
|
DCL_PRE_POST(nativeForkSystemServer)
|
|
|
|
void sanitize_fds();
|
|
bool exempt_fd(int fd);
|
|
bool is_child() const { return pid <= 0; }
|
|
|
|
// Compatibility shim
|
|
void plt_hook_register(const char *regex, const char *symbol, void *fn, void **backup);
|
|
void plt_hook_exclude(const char *regex, const char *symbol);
|
|
void plt_hook_process_regex();
|
|
|
|
bool plt_hook_commit();
|
|
};
|
|
|
|
#undef DCL_PRE_POST
|
|
|
|
// Global variables
|
|
vector<tuple<dev_t, ino_t, const char *, void **>> *plt_hook_list;
|
|
map<string, vector<JNINativeMethod>, StringCmp> *jni_hook_list;
|
|
bool should_unmap_zygisk = false;
|
|
std::vector<lsplt::MapInfo> cached_map_infos = {};
|
|
|
|
} // namespace
|
|
|
|
namespace {
|
|
|
|
#define DCL_HOOK_FUNC(ret, func, ...) \
|
|
ret (*old_##func)(__VA_ARGS__); \
|
|
ret new_##func(__VA_ARGS__)
|
|
|
|
// Skip actual fork and return cached result if applicable
|
|
DCL_HOOK_FUNC(int, fork) {
|
|
return (g_ctx && g_ctx->pid >= 0) ? g_ctx->pid : old_fork();
|
|
}
|
|
|
|
bool update_mnt_ns(enum mount_namespace_state mns_state, bool dry_run) {
|
|
std::string ns_path = zygiskd::UpdateMountNamespace(mns_state);
|
|
if (ns_path.empty()) {
|
|
PLOGE("Failed to update mount namespace");
|
|
|
|
return false;
|
|
}
|
|
|
|
if (dry_run) return true;
|
|
|
|
int updated_ns = open(ns_path.data(), O_RDONLY);
|
|
if (updated_ns == -1) {
|
|
PLOGE("Failed to open mount namespace [%s]", ns_path.data());
|
|
|
|
return false;
|
|
}
|
|
|
|
LOGD("set mount namespace to [%s] fd=[%d]\n", ns_path.data(), updated_ns);
|
|
if (setns(updated_ns, CLONE_NEWNS) == -1) {
|
|
PLOGE("Failed to set mount namespace [%s]", ns_path.data());
|
|
close(updated_ns);
|
|
|
|
return false;
|
|
}
|
|
|
|
close(updated_ns);
|
|
|
|
return true;
|
|
}
|
|
|
|
// Unmount stuffs in the process's private mount namespace
|
|
DCL_HOOK_FUNC(int, unshare, int flags) {
|
|
int res = old_unshare(flags);
|
|
if (g_ctx && (flags & CLONE_NEWNS) != 0 && res == 0 &&
|
|
// For some unknown reason, unmounting app_process in SysUI can break.
|
|
// This is reproducible on the official AVD running API 26 and 27.
|
|
// Simply avoid doing any unmounts for SysUI to avoid potential issues.
|
|
!g_ctx->flags[SERVER_FORK_AND_SPECIALIZE] && !(g_ctx->info_flags & PROCESS_IS_FIRST_STARTED)) {
|
|
if (g_ctx->info_flags & (PROCESS_IS_MANAGER | PROCESS_GRANTED_ROOT)) {
|
|
update_mnt_ns(Rooted, false);
|
|
} else if (!(g_ctx->flags[DO_REVERT_UNMOUNT])) {
|
|
update_mnt_ns(Module, false);
|
|
}
|
|
|
|
old_unshare(CLONE_NEWNS);
|
|
}
|
|
|
|
/* INFO: To spoof the errno value */
|
|
errno = 0;
|
|
|
|
return res;
|
|
}
|
|
|
|
// We cannot directly call `dlclose` to unload ourselves, otherwise when `dlclose` returns,
|
|
// it will return to our code which has been unmapped, causing segmentation fault.
|
|
// Instead, we hook `pthread_attr_setstacksize` which will be called when VM daemon threads start.
|
|
DCL_HOOK_FUNC(int, pthread_attr_setstacksize, void *target, size_t size) {
|
|
int res = old_pthread_attr_setstacksize((pthread_attr_t *)target, size);
|
|
LOGV("Call pthread_attr_setstacksize in [tid, pid]: %d, %d", gettid(), getpid());
|
|
|
|
// Only perform unloading on the main thread
|
|
if (gettid() != getpid())
|
|
return res;
|
|
|
|
if (should_unmap_zygisk) {
|
|
unhook_functions();
|
|
cached_map_infos.clear();
|
|
|
|
if (should_unmap_zygisk) {
|
|
// Because both `pthread_attr_setstacksize` and `dlclose` have the same function signature,
|
|
// we can use `musttail` to let the compiler reuse our stack frame and thus
|
|
// `dlclose` will directly return to the caller of `pthread_attr_setstacksize`.
|
|
LOGD("unmap libzygisk.so loaded at %p with size %zu", start_addr, block_size);
|
|
|
|
[[clang::musttail]] return munmap(start_addr, block_size);
|
|
}
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
void initialize_jni_hook();
|
|
|
|
DCL_HOOK_FUNC(char *, strdup, const char *s) {
|
|
if (strcmp(s, "com.android.internal.os.ZygoteInit") == 0) {
|
|
LOGV("strdup %s", s);
|
|
initialize_jni_hook();
|
|
cached_map_infos = lsplt::MapInfo::Scan();
|
|
LOGD("cached_map_infos updated");
|
|
}
|
|
|
|
return old_strdup(s);
|
|
}
|
|
|
|
#undef DCL_HOOK_FUNC
|
|
|
|
// -----------------------------------------------------------------
|
|
|
|
static bool can_hook_jni = false;
|
|
static jint MODIFIER_NATIVE = 0;
|
|
static jmethodID member_getModifiers = nullptr;
|
|
|
|
void hookJniNativeMethods(JNIEnv *env, const char *clz, JNINativeMethod *methods, int numMethods) {
|
|
if (!can_hook_jni) return;
|
|
auto clazz = env->FindClass(clz);
|
|
if (clazz == nullptr) {
|
|
env->ExceptionClear();
|
|
for (int i = 0; i < numMethods; i++) {
|
|
methods[i].fnPtr = nullptr;
|
|
}
|
|
return;
|
|
}
|
|
|
|
vector<JNINativeMethod> hooks;
|
|
for (int i = 0; i < numMethods; i++) {
|
|
auto &nm = methods[i];
|
|
auto mid = env->GetMethodID(clazz, nm.name, nm.signature);
|
|
bool is_static = false;
|
|
if (mid == nullptr) {
|
|
env->ExceptionClear();
|
|
mid = env->GetStaticMethodID(clazz, nm.name, nm.signature);
|
|
is_static = true;
|
|
}
|
|
if (mid == nullptr) {
|
|
env->ExceptionClear();
|
|
nm.fnPtr = nullptr;
|
|
continue;
|
|
}
|
|
auto method = lsplant::JNI_ToReflectedMethod(env, clazz, mid, is_static);
|
|
auto modifier = lsplant::JNI_CallIntMethod(env, method, member_getModifiers);
|
|
if ((modifier & MODIFIER_NATIVE) == 0) {
|
|
nm.fnPtr = nullptr;
|
|
continue;
|
|
}
|
|
auto artMethod = lsplant::art::ArtMethod::FromReflectedMethod(env, method);
|
|
hooks.push_back(nm);
|
|
auto orig = artMethod->GetData();
|
|
LOGV("replaced %s %s orig %p", clz, nm.name, orig);
|
|
nm.fnPtr = orig;
|
|
}
|
|
|
|
if (hooks.empty()) return;
|
|
env->RegisterNatives(clazz, hooks.data(), hooks.size());
|
|
}
|
|
|
|
// JNI method hook definitions, auto generated
|
|
#include "jni_hooks.hpp"
|
|
|
|
void initialize_jni_hook() {
|
|
auto get_created_java_vms = reinterpret_cast<jint (*)(JavaVM **, jsize, jsize *)>(
|
|
dlsym(RTLD_DEFAULT, "JNI_GetCreatedJavaVMs"));
|
|
if (!get_created_java_vms) {
|
|
for (auto &map: cached_map_infos) {
|
|
if (!map.path.ends_with("/libnativehelper.so")) continue;
|
|
void *h = dlopen(map.path.data(), RTLD_LAZY);
|
|
if (!h) {
|
|
LOGW("cannot dlopen libnativehelper.so: %s", dlerror());
|
|
break;
|
|
}
|
|
get_created_java_vms = reinterpret_cast<decltype(get_created_java_vms)>(dlsym(h, "JNI_GetCreatedJavaVMs"));
|
|
dlclose(h);
|
|
break;
|
|
}
|
|
if (!get_created_java_vms) {
|
|
LOGW("JNI_GetCreatedJavaVMs not found");
|
|
return;
|
|
}
|
|
}
|
|
JavaVM *vm = nullptr;
|
|
jsize num = 0;
|
|
jint res = get_created_java_vms(&vm, 1, &num);
|
|
if (res != JNI_OK || vm == nullptr) return;
|
|
JNIEnv *env = nullptr;
|
|
res = vm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6);
|
|
if (res != JNI_OK || env == nullptr) return;
|
|
|
|
auto classMember = lsplant::JNI_FindClass(env, "java/lang/reflect/Member");
|
|
if (classMember != nullptr) member_getModifiers = lsplant::JNI_GetMethodID(env, classMember, "getModifiers", "()I");
|
|
auto classModifier = lsplant::JNI_FindClass(env, "java/lang/reflect/Modifier");
|
|
if (classModifier != nullptr) {
|
|
auto fieldId = lsplant::JNI_GetStaticFieldID(env, classModifier, "NATIVE", "I");
|
|
if (fieldId != nullptr) MODIFIER_NATIVE = lsplant::JNI_GetStaticIntField(env, classModifier, fieldId);
|
|
}
|
|
if (member_getModifiers == nullptr || MODIFIER_NATIVE == 0) return;
|
|
if (!lsplant::art::ArtMethod::Init(env)) {
|
|
LOGE("failed to init ArtMethod");
|
|
return;
|
|
}
|
|
|
|
can_hook_jni = true;
|
|
do_hook_zygote(env);
|
|
}
|
|
|
|
// -----------------------------------------------------------------
|
|
|
|
ZygiskModule::ZygiskModule(int id, void *handle, void *entry)
|
|
: id(id), handle(handle), entry{entry}, api{}, mod{nullptr} {
|
|
// Make sure all pointers are null
|
|
memset(&api, 0, sizeof(api));
|
|
api.base.impl = this;
|
|
api.base.registerModule = &ZygiskModule::RegisterModuleImpl;
|
|
}
|
|
|
|
bool ZygiskModule::RegisterModuleImpl(ApiTable *api, long *module) {
|
|
if (api == nullptr || module == nullptr)
|
|
return false;
|
|
|
|
long api_version = *module;
|
|
// Unsupported version
|
|
if (api_version > ZYGISK_API_VERSION)
|
|
return false;
|
|
|
|
// Set the actual module_abi*
|
|
api->base.impl->mod = { module };
|
|
|
|
// Fill in API accordingly with module API version
|
|
if (api_version >= 1) {
|
|
api->v1.hookJniNativeMethods = hookJniNativeMethods;
|
|
api->v1.pltHookRegister = [](auto a, auto b, auto c, auto d) {
|
|
if (g_ctx) g_ctx->plt_hook_register(a, b, c, d);
|
|
};
|
|
api->v1.pltHookExclude = [](auto a, auto b) {
|
|
if (g_ctx) g_ctx->plt_hook_exclude(a, b);
|
|
};
|
|
api->v1.pltHookCommit = []() { return g_ctx && g_ctx->plt_hook_commit(); };
|
|
api->v1.connectCompanion = [](ZygiskModule *m) { return m->connectCompanion(); };
|
|
api->v1.setOption = [](ZygiskModule *m, auto opt) { m->setOption(opt); };
|
|
}
|
|
if (api_version >= 2) {
|
|
api->v2.getModuleDir = [](ZygiskModule *m) { return m->getModuleDir(); };
|
|
api->v2.getFlags = [](auto) { return ZygiskModule::getFlags(); };
|
|
}
|
|
if (api_version >= 4) {
|
|
api->v4.pltHookCommit = []() { return lsplt::CommitHook(cached_map_infos); };
|
|
api->v4.pltHookRegister = [](dev_t dev, ino_t inode, const char *symbol, void *fn, void **backup) {
|
|
if (dev == 0 || inode == 0 || symbol == nullptr || fn == nullptr)
|
|
return;
|
|
lsplt::RegisterHook(dev, inode, symbol, fn, backup);
|
|
};
|
|
api->v4.exemptFd = [](int fd) { return g_ctx && g_ctx->exempt_fd(fd); };
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
void ZygiskContext::plt_hook_register(const char *regex, const char *symbol, void *fn, void **backup) {
|
|
if (regex == nullptr || symbol == nullptr || fn == nullptr)
|
|
return;
|
|
regex_t re;
|
|
if (regcomp(&re, regex, REG_NOSUB) != 0)
|
|
return;
|
|
mutex_guard lock(hook_info_lock);
|
|
register_info.emplace_back(RegisterInfo{re, symbol, fn, backup});
|
|
}
|
|
|
|
void ZygiskContext::plt_hook_exclude(const char *regex, const char *symbol) {
|
|
if (!regex) return;
|
|
regex_t re;
|
|
if (regcomp(&re, regex, REG_NOSUB) != 0)
|
|
return;
|
|
mutex_guard lock(hook_info_lock);
|
|
ignore_info.emplace_back(IgnoreInfo{re, symbol ?: ""});
|
|
}
|
|
|
|
void ZygiskContext::plt_hook_process_regex() {
|
|
if (register_info.empty())
|
|
return;
|
|
for (auto &map : cached_map_infos) {
|
|
if (map.offset != 0 || !map.is_private || !(map.perms & PROT_READ)) continue;
|
|
for (auto ®: register_info) {
|
|
if (regexec(®.regex, map.path.data(), 0, nullptr, 0) != 0)
|
|
continue;
|
|
bool ignored = false;
|
|
for (auto &ign: ignore_info) {
|
|
if (regexec(&ign.regex, map.path.data(), 0, nullptr, 0) != 0)
|
|
continue;
|
|
if (ign.symbol.empty() || ign.symbol == reg.symbol) {
|
|
ignored = true;
|
|
break;
|
|
}
|
|
}
|
|
if (!ignored) {
|
|
lsplt::RegisterHook(map.dev, map.inode, reg.symbol, reg.callback, reg.backup);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
bool ZygiskContext::plt_hook_commit() {
|
|
{
|
|
mutex_guard lock(hook_info_lock);
|
|
plt_hook_process_regex();
|
|
register_info.clear();
|
|
ignore_info.clear();
|
|
}
|
|
return lsplt::CommitHook(cached_map_infos);
|
|
}
|
|
|
|
|
|
bool ZygiskModule::valid() const {
|
|
if (mod.api_version == nullptr)
|
|
return false;
|
|
switch (*mod.api_version) {
|
|
case 4:
|
|
case 3:
|
|
case 2:
|
|
case 1:
|
|
return mod.v1->impl && mod.v1->preAppSpecialize && mod.v1->postAppSpecialize &&
|
|
mod.v1->preServerSpecialize && mod.v1->postServerSpecialize;
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/* Zygisksu changed: Use own zygiskd */
|
|
int ZygiskModule::connectCompanion() const {
|
|
return zygiskd::ConnectCompanion(id);
|
|
}
|
|
|
|
/* Zygisksu changed: Use own zygiskd */
|
|
int ZygiskModule::getModuleDir() const {
|
|
return zygiskd::GetModuleDir(id);
|
|
}
|
|
|
|
void ZygiskModule::setOption(zygisk::Option opt) {
|
|
if (g_ctx == nullptr)
|
|
return;
|
|
switch (opt) {
|
|
case zygisk::FORCE_DENYLIST_UNMOUNT:
|
|
g_ctx->flags[DO_REVERT_UNMOUNT] = true;
|
|
break;
|
|
case zygisk::DLCLOSE_MODULE_LIBRARY:
|
|
unload = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
uint32_t ZygiskModule::getFlags() {
|
|
return g_ctx ? (g_ctx->info_flags & ~PRIVATE_MASK) : 0;
|
|
}
|
|
|
|
// -----------------------------------------------------------------
|
|
|
|
int sigmask(int how, int signum) {
|
|
sigset_t set;
|
|
sigemptyset(&set);
|
|
sigaddset(&set, signum);
|
|
return sigprocmask(how, &set, nullptr);
|
|
}
|
|
|
|
void ZygiskContext::fork_pre() {
|
|
// Do our own fork before loading any 3rd party code
|
|
// First block SIGCHLD, unblock after original fork is done
|
|
sigmask(SIG_BLOCK, SIGCHLD);
|
|
pid = old_fork();
|
|
if (pid != 0 || flags[SKIP_FD_SANITIZATION])
|
|
return;
|
|
|
|
// Record all open fds
|
|
auto dir = xopen_dir("/proc/self/fd");
|
|
for (dirent *entry; (entry = readdir(dir.get()));) {
|
|
int fd = parse_int(entry->d_name);
|
|
if (fd < 0 || fd >= MAX_FD_SIZE) {
|
|
close(fd);
|
|
continue;
|
|
}
|
|
allowed_fds[fd] = true;
|
|
}
|
|
// The dirfd should not be allowed
|
|
allowed_fds[dirfd(dir.get())] = false;
|
|
}
|
|
|
|
void ZygiskContext::sanitize_fds() {
|
|
if (flags[SKIP_FD_SANITIZATION])
|
|
return;
|
|
|
|
if (flags[APP_FORK_AND_SPECIALIZE]) {
|
|
auto update_fd_array = [&](int off) -> jintArray {
|
|
if (exempted_fds.empty())
|
|
return nullptr;
|
|
|
|
jintArray array = env->NewIntArray(static_cast<int>(off + exempted_fds.size()));
|
|
if (array == nullptr)
|
|
return nullptr;
|
|
|
|
env->SetIntArrayRegion(array, off, static_cast<int>(exempted_fds.size()), exempted_fds.data());
|
|
for (int fd : exempted_fds) {
|
|
if (fd >= 0 && fd < MAX_FD_SIZE) {
|
|
allowed_fds[fd] = true;
|
|
}
|
|
}
|
|
*args.app->fds_to_ignore = array;
|
|
flags[SKIP_FD_SANITIZATION] = true;
|
|
return array;
|
|
};
|
|
|
|
if (jintArray fdsToIgnore = *args.app->fds_to_ignore) {
|
|
int *arr = env->GetIntArrayElements(fdsToIgnore, nullptr);
|
|
int len = env->GetArrayLength(fdsToIgnore);
|
|
for (int i = 0; i < len; ++i) {
|
|
int fd = arr[i];
|
|
if (fd >= 0 && fd < MAX_FD_SIZE) {
|
|
allowed_fds[fd] = true;
|
|
}
|
|
}
|
|
if (jintArray newFdList = update_fd_array(len)) {
|
|
env->SetIntArrayRegion(newFdList, 0, len, arr);
|
|
}
|
|
env->ReleaseIntArrayElements(fdsToIgnore, arr, JNI_ABORT);
|
|
} else {
|
|
update_fd_array(0);
|
|
}
|
|
}
|
|
|
|
if (pid != 0)
|
|
return;
|
|
|
|
// Close all forbidden fds to prevent crashing
|
|
auto dir = open_dir("/proc/self/fd");
|
|
int dfd = dirfd(dir.get());
|
|
for (dirent *entry; (entry = readdir(dir.get()));) {
|
|
int fd = parse_int(entry->d_name);
|
|
if ((fd < 0 || fd >= MAX_FD_SIZE || !allowed_fds[fd]) && fd != dfd) {
|
|
close(fd);
|
|
}
|
|
}
|
|
}
|
|
|
|
void ZygiskContext::fork_post() {
|
|
// Unblock SIGCHLD in case the original method didn't
|
|
sigmask(SIG_UNBLOCK, SIGCHLD);
|
|
g_ctx = nullptr;
|
|
}
|
|
|
|
/* Zygisksu changed: Load module fds */
|
|
void ZygiskContext::run_modules_pre() {
|
|
auto ms = zygiskd::ReadModules();
|
|
auto size = ms.size();
|
|
for (size_t i = 0; i < size; i++) {
|
|
auto &m = ms[i];
|
|
|
|
void *handle = dlopen(m.path.c_str(), RTLD_NOW);
|
|
if (!handle) {
|
|
LOGE("Failed to load module [%s]: %s", m.path.c_str(), dlerror());
|
|
|
|
continue;
|
|
}
|
|
|
|
void *entry = dlsym(handle, "zygisk_module_entry");
|
|
if (!entry) {
|
|
LOGE("Failed to find entry point in module [%s]: %s", m.path.c_str(), dlerror());
|
|
|
|
dlclose(handle);
|
|
|
|
continue;
|
|
}
|
|
|
|
modules.emplace_back(i, handle, entry);
|
|
}
|
|
|
|
for (auto &m : modules) {
|
|
m.onLoad(env);
|
|
if (flags[APP_SPECIALIZE]) m.preAppSpecialize(args.app);
|
|
else if (flags[SERVER_FORK_AND_SPECIALIZE]) m.preServerSpecialize(args.server);
|
|
}
|
|
}
|
|
|
|
void ZygiskContext::run_modules_post() {
|
|
flags[POST_SPECIALIZE] = true;
|
|
|
|
size_t modules_unloaded = 0;
|
|
for (const auto &m : modules) {
|
|
if (flags[APP_SPECIALIZE]) m.postAppSpecialize(args.app);
|
|
else if (flags[SERVER_FORK_AND_SPECIALIZE]) m.postServerSpecialize(args.server);
|
|
|
|
if (m.tryUnload()) modules_unloaded++;
|
|
}
|
|
|
|
if (modules.size() > 0) {
|
|
LOGD("modules unloaded: %zu/%zu", modules_unloaded, modules.size());
|
|
clean_trace("/data/adb", modules.size(), modules_unloaded, true);
|
|
}
|
|
}
|
|
|
|
/* Zygisksu changed: Load module fds */
|
|
void ZygiskContext::app_specialize_pre() {
|
|
flags[APP_SPECIALIZE] = true;
|
|
|
|
info_flags = zygiskd::GetProcessFlags(g_ctx->args.app->uid);
|
|
if (info_flags & PROCESS_IS_FIRST_STARTED) {
|
|
update_mnt_ns(Clean, true);
|
|
}
|
|
|
|
if ((info_flags & PROCESS_ON_DENYLIST) == PROCESS_ON_DENYLIST) {
|
|
flags[DO_REVERT_UNMOUNT] = true;
|
|
}
|
|
|
|
if ((info_flags & (PROCESS_IS_MANAGER | PROCESS_ROOT_IS_MAGISK)) == (PROCESS_IS_MANAGER | PROCESS_ROOT_IS_MAGISK)) {
|
|
LOGD("Manager process detected. Notifying that Zygisk has been enabled.");
|
|
|
|
setenv("ZYGISK_ENABLED", "1", 1);
|
|
} else {
|
|
run_modules_pre();
|
|
}
|
|
}
|
|
|
|
|
|
void ZygiskContext::app_specialize_post() {
|
|
run_modules_post();
|
|
|
|
// Cleanups
|
|
env->ReleaseStringUTFChars(args.app->nice_name, process);
|
|
g_ctx = nullptr;
|
|
}
|
|
|
|
bool ZygiskContext::exempt_fd(int fd) {
|
|
if (flags[POST_SPECIALIZE] || flags[SKIP_FD_SANITIZATION])
|
|
return true;
|
|
if (!flags[APP_FORK_AND_SPECIALIZE])
|
|
return false;
|
|
exempted_fds.push_back(fd);
|
|
return true;
|
|
}
|
|
|
|
// -----------------------------------------------------------------
|
|
|
|
void ZygiskContext::nativeSpecializeAppProcess_pre() {
|
|
process = env->GetStringUTFChars(args.app->nice_name, nullptr);
|
|
LOGV("pre specialize [%s]", process);
|
|
// App specialize does not check FD
|
|
flags[SKIP_FD_SANITIZATION] = true;
|
|
app_specialize_pre();
|
|
}
|
|
|
|
void ZygiskContext::nativeSpecializeAppProcess_post() {
|
|
LOGV("post specialize [%s]", process);
|
|
app_specialize_post();
|
|
}
|
|
|
|
/* Zygisksu changed: No system_server status write back */
|
|
void ZygiskContext::nativeForkSystemServer_pre() {
|
|
LOGV("pre forkSystemServer");
|
|
flags[SERVER_FORK_AND_SPECIALIZE] = true;
|
|
|
|
fork_pre();
|
|
if (is_child()) {
|
|
run_modules_pre();
|
|
zygiskd::SystemServerStarted();
|
|
}
|
|
|
|
sanitize_fds();
|
|
}
|
|
|
|
void ZygiskContext::nativeForkSystemServer_post() {
|
|
if (pid == 0) {
|
|
LOGV("post forkSystemServer");
|
|
run_modules_post();
|
|
}
|
|
fork_post();
|
|
}
|
|
|
|
void ZygiskContext::nativeForkAndSpecialize_pre() {
|
|
process = env->GetStringUTFChars(args.app->nice_name, nullptr);
|
|
LOGV("pre forkAndSpecialize [%s]", process);
|
|
flags[APP_FORK_AND_SPECIALIZE] = true;
|
|
|
|
update_mnt_ns(Clean, false);
|
|
|
|
fork_pre();
|
|
if (pid == 0) {
|
|
app_specialize_pre();
|
|
}
|
|
sanitize_fds();
|
|
}
|
|
|
|
void ZygiskContext::nativeForkAndSpecialize_post() {
|
|
if (pid == 0) {
|
|
LOGV("post forkAndSpecialize [%s]", process);
|
|
app_specialize_post();
|
|
}
|
|
fork_post();
|
|
}
|
|
|
|
ZygiskContext::~ZygiskContext() {
|
|
// This global pointer points to a variable on the stack.
|
|
// Set this to nullptr to prevent leaking local variable.
|
|
// This also disables most plt hooked functions.
|
|
g_ctx = nullptr;
|
|
|
|
if (!is_child())
|
|
return;
|
|
|
|
should_unmap_zygisk = true;
|
|
|
|
// Unhook JNI methods
|
|
for (const auto &[clz, methods] : *jni_hook_list) {
|
|
if (!methods.empty() && env->RegisterNatives(
|
|
env->FindClass(clz.data()), methods.data(),
|
|
static_cast<int>(methods.size())) != 0) {
|
|
LOGE("Failed to restore JNI hook of class [%s]", clz.data());
|
|
should_unmap_zygisk = false;
|
|
}
|
|
}
|
|
delete jni_hook_list;
|
|
jni_hook_list = nullptr;
|
|
|
|
// Strip out all API function pointers
|
|
for (auto &m : modules) {
|
|
m.clearApi();
|
|
}
|
|
|
|
hook_unloader();
|
|
}
|
|
|
|
} // namespace
|
|
|
|
static bool hook_commit(std::vector<lsplt::MapInfo> &map_infos = cached_map_infos) {
|
|
if (lsplt::CommitHook(map_infos)) {
|
|
return true;
|
|
} else {
|
|
LOGE("plt_hook failed");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
static void hook_register(dev_t dev, ino_t inode, const char *symbol, void *new_func, void **old_func) {
|
|
if (!lsplt::RegisterHook(dev, inode, symbol, new_func, old_func)) {
|
|
LOGE("Failed to register plt_hook \"%s\"", symbol);
|
|
return;
|
|
}
|
|
plt_hook_list->emplace_back(dev, inode, symbol, old_func);
|
|
}
|
|
|
|
#define PLT_HOOK_REGISTER_SYM(DEV, INODE, SYM, NAME) \
|
|
hook_register(DEV, INODE, SYM, (void*) new_##NAME, (void **) &old_##NAME)
|
|
|
|
#define PLT_HOOK_REGISTER(DEV, INODE, NAME) \
|
|
PLT_HOOK_REGISTER_SYM(DEV, INODE, #NAME, NAME)
|
|
|
|
void clean_trace(const char* path, size_t load, size_t unload, bool spoof_maps) {
|
|
LOGD("cleaning trace for path %s", path);
|
|
|
|
if (load > 0 || unload >0) SoList::ResetCounters(load, unload);
|
|
bool path_found = SoList::DropSoPath(path);
|
|
if (!path_found || !spoof_maps) return;
|
|
|
|
LOGD("spoofing virtual maps for %s", path);
|
|
// spoofing map names is futile in Android, we do it simply
|
|
// to avoid Zygisk detections based on string comparison
|
|
for (auto &map : lsplt::MapInfo::Scan()) {
|
|
if (strstr(map.path.c_str(), path) && strstr(map.path.c_str(), "libzygisk") == 0)
|
|
{
|
|
void *addr = (void *)map.start;
|
|
size_t size = map.end - map.start;
|
|
void *copy = mmap(nullptr, size, PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
|
|
if (copy == MAP_FAILED) {
|
|
LOGE("failed to backup block %s [%p, %p]", map.path.c_str(), addr, (void*)map.end);
|
|
continue;
|
|
}
|
|
|
|
if ((map.perms & PROT_READ) == 0) {
|
|
mprotect(addr, size, PROT_READ);
|
|
}
|
|
memcpy(copy, addr, size);
|
|
mremap(copy, size, size, MREMAP_MAYMOVE | MREMAP_FIXED, addr);
|
|
mprotect(addr, size, map.perms);
|
|
}
|
|
}
|
|
}
|
|
|
|
void hook_functions() {
|
|
default_new(plt_hook_list);
|
|
default_new(jni_hook_list);
|
|
|
|
ino_t android_runtime_inode = 0;
|
|
dev_t android_runtime_dev = 0;
|
|
/* TODO by ThePedroo: Implement injection via native bridge */
|
|
// ino_t native_bridge_inode = 0;
|
|
// dev_t native_bridge_dev = 0;
|
|
|
|
cached_map_infos = lsplt::MapInfo::Scan();
|
|
for (auto &map : cached_map_infos) {
|
|
if (map.path.ends_with("libandroid_runtime.so")) {
|
|
android_runtime_inode = map.inode;
|
|
android_runtime_dev = map.dev;
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
PLT_HOOK_REGISTER(android_runtime_dev, android_runtime_inode, fork);
|
|
PLT_HOOK_REGISTER(android_runtime_dev, android_runtime_inode, unshare);
|
|
PLT_HOOK_REGISTER(android_runtime_dev, android_runtime_inode, strdup);
|
|
hook_commit();
|
|
|
|
// Remove unhooked methods
|
|
plt_hook_list->erase(
|
|
std::remove_if(plt_hook_list->begin(), plt_hook_list->end(),
|
|
[](auto &t) { return *std::get<3>(t) == nullptr;}),
|
|
plt_hook_list->end());
|
|
}
|
|
|
|
static void hook_unloader() {
|
|
ino_t art_inode = 0;
|
|
dev_t art_dev = 0;
|
|
|
|
for (auto &map : cached_map_infos) {
|
|
if (map.path.ends_with("/libart.so")) {
|
|
art_inode = map.inode;
|
|
art_dev = map.dev;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (art_dev == 0 || art_inode == 0) {
|
|
LOGE("virtual map for libart.so is not cached");
|
|
return;
|
|
} else {
|
|
LOGD("hook_unloader called with libart.so [%zu:%lu]", art_dev, art_inode);
|
|
}
|
|
PLT_HOOK_REGISTER(art_dev, art_inode, pthread_attr_setstacksize);
|
|
hook_commit();
|
|
}
|
|
|
|
static void unhook_functions() {
|
|
// Unhook plt_hook
|
|
for (const auto &[dev, inode, sym, old_func] : *plt_hook_list) {
|
|
if (!lsplt::RegisterHook(dev, inode, sym, *old_func, nullptr)) {
|
|
LOGE("Failed to register plt_hook [%s]", sym);
|
|
}
|
|
}
|
|
delete plt_hook_list;
|
|
if (!hook_commit()) {
|
|
LOGE("Failed to restore plt_hook");
|
|
should_unmap_zygisk = false;
|
|
}
|
|
}
|