You've already forked ReZygisk
mirror of
https://github.com/PerformanC/ReZygisk.git
synced 2025-09-06 06:37:01 +00:00
Fix hook
This commit is contained in:
@@ -158,10 +158,6 @@ string get_class_name(JNIEnv *env, jclass clazz) {
|
|||||||
return className;
|
return className;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define DCL_HOOK_FUNC(ret, func, ...) \
|
|
||||||
ret (*old_##func)(__VA_ARGS__); \
|
|
||||||
ret new_##func(__VA_ARGS__)
|
|
||||||
|
|
||||||
jint env_RegisterNatives(
|
jint env_RegisterNatives(
|
||||||
JNIEnv *env, jclass clazz, const JNINativeMethod *methods, jint numMethods) {
|
JNIEnv *env, jclass clazz, const JNINativeMethod *methods, jint numMethods) {
|
||||||
auto className = get_class_name(env, clazz);
|
auto className = get_class_name(env, clazz);
|
||||||
@@ -170,46 +166,50 @@ jint env_RegisterNatives(
|
|||||||
return old_functions->RegisterNatives(env, clazz, newMethods.get() ?: methods, numMethods);
|
return old_functions->RegisterNatives(env, clazz, newMethods.get() ?: methods, numMethods);
|
||||||
}
|
}
|
||||||
|
|
||||||
DCL_HOOK_FUNC(void, androidSetCreateThreadFunc, void* func) {
|
void replace_jni_methods() {
|
||||||
LOGD("androidSetCreateThreadFunc\n");
|
auto get_created_java_vms = reinterpret_cast<jint (*)(JavaVM **, jsize, jsize *)>(
|
||||||
do {
|
dlsym(RTLD_DEFAULT, "JNI_GetCreatedJavaVMs"));
|
||||||
auto get_created_java_vms = reinterpret_cast<jint (*)(JavaVM **, jsize, jsize *)>(
|
if (!get_created_java_vms) {
|
||||||
dlsym(RTLD_DEFAULT, "JNI_GetCreatedJavaVMs"));
|
for (auto &map: lsplt::MapInfo::Scan()) {
|
||||||
if (!get_created_java_vms) {
|
if (!map.path.ends_with("/libnativehelper.so")) continue;
|
||||||
for (auto &map: lsplt::MapInfo::Scan()) {
|
void *h = dlopen(map.path.data(), RTLD_LAZY);
|
||||||
if (!map.path.ends_with("/libnativehelper.so")) continue;
|
if (!h) {
|
||||||
void *h = dlopen(map.path.data(), RTLD_LAZY);
|
LOGW("cannot dlopen libnativehelper.so: %s\n", dlerror());
|
||||||
if (!h) {
|
|
||||||
LOGW("cannot dlopen libnativehelper.so: %s\n", 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\n");
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
get_created_java_vms = reinterpret_cast<decltype(get_created_java_vms)>(dlsym(h, "JNI_GetCreatedJavaVMs"));
|
||||||
|
dlclose(h);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
JavaVM *vm = nullptr;
|
if (!get_created_java_vms) {
|
||||||
jsize num = 0;
|
LOGW("JNI_GetCreatedJavaVMs not found\n");
|
||||||
jint res = get_created_java_vms(&vm, 1, &num);
|
return;
|
||||||
if (res != JNI_OK || vm == nullptr) break;
|
}
|
||||||
JNIEnv *env = nullptr;
|
}
|
||||||
res = vm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6);
|
JavaVM *vm = nullptr;
|
||||||
if (res != JNI_OK || env == nullptr) break;
|
jsize num = 0;
|
||||||
default_new(new_functions);
|
jint res = get_created_java_vms(&vm, 1, &num);
|
||||||
memcpy(new_functions, env->functions, sizeof(*new_functions));
|
if (res != JNI_OK || vm == nullptr) return;
|
||||||
new_functions->RegisterNatives = &env_RegisterNatives;
|
JNIEnv *env = nullptr;
|
||||||
|
res = vm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6);
|
||||||
|
if (res != JNI_OK || env == nullptr) return;
|
||||||
|
default_new(new_functions);
|
||||||
|
memcpy(new_functions, env->functions, sizeof(*new_functions));
|
||||||
|
new_functions->RegisterNatives = &env_RegisterNatives;
|
||||||
|
|
||||||
// Replace the function table in JNIEnv to hook RegisterNatives
|
// Replace the function table in JNIEnv to hook RegisterNatives
|
||||||
old_functions = env->functions;
|
old_functions = env->functions;
|
||||||
env->functions = new_functions;
|
env->functions = new_functions;
|
||||||
} while (false);
|
|
||||||
old_androidSetCreateThreadFunc(func);
|
// Re-run register_com_android_internal_os_Zygote to hook JNI methods
|
||||||
|
auto register_zygote = dlsym(RTLD_DEFAULT, "_ZN7android39register_com_android_internal_os_ZygoteEP7_JNIEnv");
|
||||||
|
reinterpret_cast<void (*)(JNIEnv *)>(register_zygote)(env);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#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
|
// Skip actual fork and return cached result if applicable
|
||||||
DCL_HOOK_FUNC(int, fork) {
|
DCL_HOOK_FUNC(int, fork) {
|
||||||
return (g_ctx && g_ctx->pid >= 0) ? g_ctx->pid : old_fork();
|
return (g_ctx && g_ctx->pid >= 0) ? g_ctx->pid : old_fork();
|
||||||
@@ -753,7 +753,6 @@ void hook_functions() {
|
|||||||
|
|
||||||
PLT_HOOK_REGISTER(android_runtime_dev, android_runtime_inode, fork);
|
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, unshare);
|
||||||
PLT_HOOK_REGISTER(android_runtime_dev, android_runtime_inode, androidSetCreateThreadFunc);
|
|
||||||
PLT_HOOK_REGISTER_SYM(android_runtime_dev, android_runtime_inode, "__android_log_close", android_log_close);
|
PLT_HOOK_REGISTER_SYM(android_runtime_dev, android_runtime_inode, "__android_log_close", android_log_close);
|
||||||
hook_commit();
|
hook_commit();
|
||||||
|
|
||||||
@@ -762,6 +761,8 @@ void hook_functions() {
|
|||||||
std::remove_if(plt_hook_list->begin(), plt_hook_list->end(),
|
std::remove_if(plt_hook_list->begin(), plt_hook_list->end(),
|
||||||
[](auto &t) { return *std::get<3>(t) == nullptr;}),
|
[](auto &t) { return *std::get<3>(t) == nullptr;}),
|
||||||
plt_hook_list->end());
|
plt_hook_list->end());
|
||||||
|
|
||||||
|
replace_jni_methods();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void hook_unloader() {
|
static void hook_unloader() {
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ using namespace std::string_view_literals;
|
|||||||
namespace {
|
namespace {
|
||||||
constexpr auto MODULE_DIR = "/data/adb/modules";
|
constexpr auto MODULE_DIR = "/data/adb/modules";
|
||||||
constexpr auto KSU_OVERLAY_SOURCE = "KSU";
|
constexpr auto KSU_OVERLAY_SOURCE = "KSU";
|
||||||
|
constexpr auto ZYGISK_FUSE_SOURCE = "zygisk";
|
||||||
const std::vector<std::string> KSU_PARTITIONS{"/system", "/vendor", "/product", "/system_ext", "/odm", "/oem"};
|
const std::vector<std::string> KSU_PARTITIONS{"/system", "/vendor", "/product", "/system_ext", "/odm", "/oem"};
|
||||||
|
|
||||||
void lazy_unmount(const char* mountpoint) {
|
void lazy_unmount(const char* mountpoint) {
|
||||||
@@ -44,6 +45,10 @@ void revert_unmount_ksu() {
|
|||||||
&& std::find(KSU_PARTITIONS.begin(), KSU_PARTITIONS.end(), info.target) != KSU_PARTITIONS.end()) {
|
&& std::find(KSU_PARTITIONS.begin(), KSU_PARTITIONS.end(), info.target) != KSU_PARTITIONS.end()) {
|
||||||
targets.emplace_back(info.target);
|
targets.emplace_back(info.target);
|
||||||
}
|
}
|
||||||
|
// Unmount fuse
|
||||||
|
if (info.type == "fuse" && info.source == ZYGISK_FUSE_SOURCE) {
|
||||||
|
targets.emplace_back(info.target);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
for (auto& info: parse_mount_info("self")) {
|
for (auto& info: parse_mount_info("self")) {
|
||||||
// Unmount everything from ksu loop except ksu module dir
|
// Unmount everything from ksu loop except ksu module dir
|
||||||
|
|||||||
Reference in New Issue
Block a user