2021-01-08 00:53:24 -08:00
|
|
|
#include <jni.h>
|
|
|
|
|
|
|
|
#include <xhook.h>
|
|
|
|
#include <utils.hpp>
|
|
|
|
#include <flags.hpp>
|
|
|
|
|
|
|
|
#include "inject.hpp"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2021-01-09 20:39:59 -08:00
|
|
|
#define DCL_HOOK_FUNC(ret, func, ...) \
|
|
|
|
static ret (*old_##func)(__VA_ARGS__); \
|
|
|
|
static ret new_##func(__VA_ARGS__)
|
|
|
|
|
|
|
|
#define DCL_JNI_FUNC(name) \
|
|
|
|
static const JNINativeMethod *name##_orig = nullptr; \
|
|
|
|
extern const JNINativeMethod name##_methods[]; \
|
|
|
|
extern const int name##_methods_num;
|
2021-01-08 05:25:44 -08:00
|
|
|
|
2021-01-10 05:07:17 -08:00
|
|
|
// For some reason static vectors won't work, use pointers instead
|
|
|
|
static vector<tuple<const char *, const char *, void **>> *xhook_list;
|
|
|
|
static vector<JNINativeMethod> *jni_list;
|
|
|
|
|
2021-01-09 20:39:59 -08:00
|
|
|
static JavaVM *g_jvm;
|
2021-01-10 01:25:30 -08:00
|
|
|
static int prev_fork_pid = -1;
|
2021-01-09 20:39:59 -08:00
|
|
|
|
2021-01-09 17:41:25 -08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct HookContext {
|
|
|
|
int pid;
|
2021-01-10 01:25:30 -08:00
|
|
|
bool unload;
|
2021-01-09 17:41:25 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
// JNI method declarations
|
2021-01-09 20:39:59 -08:00
|
|
|
DCL_JNI_FUNC(nativeForkAndSpecialize)
|
|
|
|
DCL_JNI_FUNC(nativeSpecializeAppProcess)
|
|
|
|
DCL_JNI_FUNC(nativeForkSystemServer)
|
2021-01-09 17:41:25 -08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-01-09 20:39:59 -08:00
|
|
|
#define HOOK_JNI(method) \
|
2021-01-08 05:25:44 -08:00
|
|
|
if (newMethods[i].name == #method##sv) { \
|
2021-01-09 17:41:25 -08:00
|
|
|
auto orig = new JNINativeMethod(); \
|
|
|
|
memcpy(orig, &newMethods[i], sizeof(JNINativeMethod)); \
|
2021-01-09 20:39:59 -08:00
|
|
|
method##_orig = orig; \
|
2021-01-10 05:07:17 -08:00
|
|
|
jni_list->push_back(newMethods[i]); \
|
2021-01-09 20:39:59 -08:00
|
|
|
for (int j = 0; j < method##_methods_num; ++j) { \
|
|
|
|
if (strcmp(newMethods[i].signature, method##_methods[j].signature) == 0) { \
|
|
|
|
newMethods[i] = method##_methods[j]; \
|
|
|
|
LOGI("hook: replaced #" #method "\n"); \
|
2021-01-08 05:25:44 -08:00
|
|
|
++hooked; \
|
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
continue; \
|
|
|
|
}
|
|
|
|
|
2021-01-09 20:39:59 -08:00
|
|
|
DCL_HOOK_FUNC(int, jniRegisterNativeMethods,
|
2021-01-08 00:53:24 -08:00
|
|
|
JNIEnv *env, const char *className, const JNINativeMethod *methods, int numMethods) {
|
|
|
|
LOGD("hook: jniRegisterNativeMethods %s", className);
|
|
|
|
|
2021-01-08 05:25:44 -08:00
|
|
|
unique_ptr<JNINativeMethod[]> newMethods;
|
|
|
|
int hooked = 0;
|
|
|
|
|
|
|
|
if (g_jvm == nullptr) {
|
|
|
|
// Save for later unhooking
|
|
|
|
env->GetJavaVM(&g_jvm);
|
|
|
|
}
|
2021-01-08 00:53:24 -08:00
|
|
|
|
2021-01-08 05:25:44 -08:00
|
|
|
if (className == "com/android/internal/os/Zygote"sv) {
|
2021-01-09 20:39:59 -08:00
|
|
|
newMethods = make_unique<JNINativeMethod[]>(numMethods);
|
|
|
|
memcpy(newMethods.get(), methods, sizeof(JNINativeMethod) * numMethods);
|
2021-01-08 05:25:44 -08:00
|
|
|
for (int i = 0; i < numMethods && hooked < 3; ++i) {
|
2021-01-09 20:39:59 -08:00
|
|
|
HOOK_JNI(nativeForkAndSpecialize);
|
|
|
|
HOOK_JNI(nativeSpecializeAppProcess);
|
|
|
|
HOOK_JNI(nativeForkSystemServer);
|
2021-01-08 05:25:44 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return old_jniRegisterNativeMethods(env, className, newMethods.get() ?: methods, numMethods);
|
2021-01-08 00:53:24 -08:00
|
|
|
}
|
|
|
|
|
2021-01-10 01:25:30 -08:00
|
|
|
DCL_HOOK_FUNC(int, fork) {
|
|
|
|
if (prev_fork_pid < 0)
|
|
|
|
return old_fork();
|
|
|
|
|
|
|
|
// Skip an actual fork and return the previous fork result
|
|
|
|
int pid = prev_fork_pid;
|
|
|
|
prev_fork_pid = -1;
|
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sigmask(int how, int signum) {
|
|
|
|
sigset_t set;
|
|
|
|
sigemptyset(&set);
|
|
|
|
sigaddset(&set, signum);
|
|
|
|
return sigprocmask(how, &set, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pre_specialize_fork() {
|
|
|
|
// First block SIGCHLD, unblock after original fork is done
|
|
|
|
sigmask(SIG_BLOCK, SIGCHLD);
|
|
|
|
prev_fork_pid = old_fork();
|
|
|
|
return prev_fork_pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------
|
|
|
|
|
2021-01-09 17:41:25 -08:00
|
|
|
static void nativeForkAndSpecialize_pre(HookContext *ctx,
|
|
|
|
JNIEnv *env, jclass clazz, jint &uid, jint &gid, jintArray &gids, jint &runtime_flags,
|
|
|
|
jobjectArray &rlimits, jint &mount_external, jstring &se_info, jstring &nice_name,
|
2021-01-09 20:39:59 -08:00
|
|
|
jintArray &fds_to_close, jintArray &fds_to_ignore, /* These 2 arguments are unique to fork */
|
|
|
|
jboolean &is_child_zygote, jstring &instruction_set, jstring &app_data_dir,
|
|
|
|
jboolean &is_top_app, jobjectArray &pkg_data_info_list,
|
|
|
|
jobjectArray &whitelisted_data_info_list, jboolean &mount_data_dirs,
|
|
|
|
jboolean &mount_storage_dirs) {
|
2021-01-10 01:25:30 -08:00
|
|
|
|
|
|
|
// Do our own fork before loading any 3rd party code
|
|
|
|
ctx->pid = pre_specialize_fork();
|
|
|
|
if (ctx->pid != 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// TODO: check if we need to do hiding
|
|
|
|
// Demonstrate self unload in child process
|
|
|
|
ctx->unload = true;
|
|
|
|
|
2021-01-09 17:41:25 -08:00
|
|
|
LOGD("hook: %s\n", __FUNCTION__);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void nativeForkAndSpecialize_post(HookContext *ctx, JNIEnv *env, jclass clazz) {
|
2021-01-10 01:25:30 -08:00
|
|
|
// Unblock SIGCHLD in case the original method didn't
|
|
|
|
sigmask(SIG_UNBLOCK, SIGCHLD);
|
|
|
|
|
|
|
|
if (ctx->pid != 0)
|
|
|
|
return;
|
|
|
|
|
2021-01-09 17:41:25 -08:00
|
|
|
LOGD("hook: %s\n", __FUNCTION__);
|
2021-01-10 01:25:30 -08:00
|
|
|
|
|
|
|
if (ctx->unload)
|
2021-01-09 17:41:25 -08:00
|
|
|
self_unload();
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------
|
|
|
|
|
|
|
|
static void nativeSpecializeAppProcess_pre(HookContext *ctx,
|
2021-01-09 20:39:59 -08:00
|
|
|
JNIEnv *env, jclass clazz, jint &uid, jint &gid, jintArray &gids, jint &runtime_flags,
|
|
|
|
jobjectArray &rlimits, jint &mount_external, jstring &se_info, jstring &nice_name,
|
|
|
|
jboolean &is_child_zygote, jstring &instruction_set, jstring &app_data_dir,
|
|
|
|
jboolean &is_top_app, jobjectArray &pkg_data_info_list,
|
|
|
|
jobjectArray &whitelisted_data_info_list, jboolean &mount_data_dirs,
|
|
|
|
jboolean &mount_storage_dirs) {
|
2021-01-09 17:41:25 -08:00
|
|
|
LOGD("hook: %s\n", __FUNCTION__);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void nativeSpecializeAppProcess_post(HookContext *ctx, JNIEnv *env, jclass clazz) {
|
|
|
|
LOGD("hook: %s\n", __FUNCTION__);
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------
|
|
|
|
|
|
|
|
static void nativeForkSystemServer_pre(HookContext *ctx,
|
|
|
|
JNIEnv *env, jclass clazz, uid_t &uid, gid_t &gid, jintArray &gids, jint &runtime_flags,
|
|
|
|
jobjectArray &rlimits, jlong &permitted_capabilities, jlong &effective_capabilities) {
|
2021-01-10 01:25:30 -08:00
|
|
|
|
|
|
|
// Do our own fork before loading any 3rd party code
|
|
|
|
ctx->pid = pre_specialize_fork();
|
|
|
|
if (ctx->pid != 0)
|
|
|
|
return;
|
|
|
|
|
2021-01-09 17:41:25 -08:00
|
|
|
LOGD("hook: %s\n", __FUNCTION__);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void nativeForkSystemServer_post(HookContext *ctx, JNIEnv *env, jclass clazz) {
|
2021-01-10 01:25:30 -08:00
|
|
|
// Unblock SIGCHLD in case the original method didn't
|
|
|
|
sigmask(SIG_UNBLOCK, SIGCHLD);
|
|
|
|
|
|
|
|
if (ctx->pid != 0)
|
|
|
|
return;
|
|
|
|
|
2021-01-09 17:41:25 -08:00
|
|
|
LOGD("hook: %s\n", __FUNCTION__);
|
|
|
|
}
|
|
|
|
|
2021-01-09 20:39:59 -08:00
|
|
|
// -----------------------------------------------------------------
|
|
|
|
|
2021-01-08 00:53:24 -08:00
|
|
|
static bool hook_refresh() {
|
|
|
|
if (xhook_refresh(0) == 0) {
|
|
|
|
xhook_clear();
|
|
|
|
LOGI("hook: xhook success\n");
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
LOGE("hook: xhook failed\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hook_register(const char *path, const char *symbol, void *new_func, void **old_func) {
|
|
|
|
int ret = xhook_register(path, symbol, new_func, old_func);
|
|
|
|
if (ret != 0) {
|
|
|
|
LOGE("hook: Failed to register hook \"%s\"\n", symbol);
|
|
|
|
return ret;
|
|
|
|
}
|
2021-01-10 05:07:17 -08:00
|
|
|
xhook_list->emplace_back(path, symbol, old_func);
|
2021-01-08 00:53:24 -08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define XHOOK_REGISTER(PATH_REGEX, NAME) \
|
|
|
|
hook_register(PATH_REGEX, #NAME, (void*) new_##NAME, (void **) &old_##NAME)
|
|
|
|
|
|
|
|
void hook_functions() {
|
|
|
|
#ifdef MAGISK_DEBUG
|
|
|
|
xhook_enable_debug(1);
|
|
|
|
xhook_enable_sigsegv_protection(0);
|
|
|
|
#endif
|
2021-01-10 05:07:17 -08:00
|
|
|
xhook_list = new remove_pointer_t<decltype(xhook_list)>();
|
|
|
|
jni_list = new remove_pointer_t<decltype(jni_list)>();
|
|
|
|
|
2021-01-08 00:53:24 -08:00
|
|
|
XHOOK_REGISTER(".*\\libandroid_runtime.so$", jniRegisterNativeMethods);
|
2021-01-10 01:25:30 -08:00
|
|
|
XHOOK_REGISTER(".*\\libandroid_runtime.so$", fork);
|
2021-01-08 00:53:24 -08:00
|
|
|
hook_refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool unhook_functions() {
|
2021-01-08 05:25:44 -08:00
|
|
|
JNIEnv* env;
|
|
|
|
if (g_jvm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK)
|
|
|
|
return false;
|
|
|
|
|
2021-01-09 17:41:25 -08:00
|
|
|
// Unhook JNI methods
|
2021-01-10 05:07:17 -08:00
|
|
|
if (!jni_list->empty() && old_jniRegisterNativeMethods(env,
|
2021-01-08 05:25:44 -08:00
|
|
|
"com/android/internal/os/Zygote",
|
2021-01-10 05:07:17 -08:00
|
|
|
jni_list->data(), jni_list->size()) != 0) {
|
|
|
|
LOGE("hook: Failed to register JNI hook\n");
|
2021-01-08 05:25:44 -08:00
|
|
|
return false;
|
|
|
|
}
|
2021-01-10 05:07:17 -08:00
|
|
|
delete jni_list;
|
2021-01-08 05:25:44 -08:00
|
|
|
|
2021-01-09 17:41:25 -08:00
|
|
|
// Unhook xhook
|
2021-01-10 05:07:17 -08:00
|
|
|
for (auto &[path, sym, old_func] : *xhook_list) {
|
2021-01-08 00:53:24 -08:00
|
|
|
if (xhook_register(path, sym, *old_func, nullptr) != 0) {
|
2021-01-08 05:25:44 -08:00
|
|
|
LOGE("hook: Failed to register hook \"%s\"\n", sym);
|
2021-01-08 00:53:24 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2021-01-10 05:07:17 -08:00
|
|
|
delete xhook_list;
|
2021-01-08 00:53:24 -08:00
|
|
|
return hook_refresh();
|
|
|
|
}
|
2021-01-09 17:41:25 -08:00
|
|
|
|
|
|
|
#include "jni_hooks.hpp"
|