2021-01-08 08:53:24 +00:00
|
|
|
#include <xhook.h>
|
|
|
|
#include <utils.hpp>
|
|
|
|
#include <flags.hpp>
|
2021-01-12 11:28:00 +00:00
|
|
|
#include <daemon.hpp>
|
2021-01-08 08:53:24 +00:00
|
|
|
|
|
|
|
#include "inject.hpp"
|
2021-08-01 21:35:16 +00:00
|
|
|
#include "memory.hpp"
|
2021-01-08 08:53:24 +00:00
|
|
|
|
|
|
|
using namespace std;
|
2021-08-01 21:35:16 +00:00
|
|
|
using jni_hook::hash_map;
|
|
|
|
using jni_hook::tree_map;
|
|
|
|
using xstring = jni_hook::string;
|
|
|
|
|
2021-08-02 10:20:19 +00:00
|
|
|
struct SpecializeAppProcessArgs {
|
|
|
|
jint &uid;
|
|
|
|
jint &gid;
|
|
|
|
jintArray &gids;
|
|
|
|
jint &runtime_flags;
|
|
|
|
jint &mount_external;
|
|
|
|
jstring &se_info;
|
|
|
|
jstring &nice_name;
|
|
|
|
jstring &instruction_set;
|
|
|
|
jstring &app_data_dir;
|
|
|
|
|
|
|
|
/* Optional */
|
|
|
|
jboolean *is_child_zygote = nullptr;
|
|
|
|
jboolean *is_top_app = nullptr;
|
|
|
|
jobjectArray *pkg_data_info_list = nullptr;
|
|
|
|
jobjectArray *whitelisted_data_info_list = nullptr;
|
|
|
|
jboolean *mount_data_dirs = nullptr;
|
|
|
|
jboolean *mount_storage_dirs = nullptr;
|
|
|
|
|
|
|
|
SpecializeAppProcessArgs(
|
|
|
|
jint &uid, jint &gid, jintArray &gids, jint &runtime_flags,
|
|
|
|
jint &mount_external, jstring &se_info, jstring &nice_name,
|
|
|
|
jstring &instruction_set, jstring &app_data_dir) :
|
|
|
|
uid(uid), gid(gid), gids(gids), runtime_flags(runtime_flags),
|
|
|
|
mount_external(mount_external), se_info(se_info), nice_name(nice_name),
|
|
|
|
instruction_set(instruction_set), app_data_dir(app_data_dir) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ForkSystemServerArgs {
|
|
|
|
jint &uid;
|
|
|
|
jint &gid;
|
|
|
|
jintArray &gids;
|
|
|
|
jint &runtime_flags;
|
|
|
|
jlong &permitted_capabilities;
|
|
|
|
jlong &effective_capabilities;
|
|
|
|
|
|
|
|
ForkSystemServerArgs(
|
|
|
|
jint &uid, jint &gid, jintArray &gids, jint &runtime_flags,
|
|
|
|
jlong &permitted_capabilities, jlong &effective_capabilities) :
|
|
|
|
uid(uid), gid(gid), gids(gids), runtime_flags(runtime_flags),
|
|
|
|
permitted_capabilities(permitted_capabilities),
|
|
|
|
effective_capabilities(effective_capabilities) {}
|
|
|
|
};
|
|
|
|
|
2021-08-01 21:35:16 +00:00
|
|
|
struct HookContext {
|
|
|
|
int pid;
|
|
|
|
bool do_hide;
|
2021-08-02 10:20:19 +00:00
|
|
|
union {
|
|
|
|
SpecializeAppProcessArgs *args;
|
|
|
|
ForkSystemServerArgs *server_args;
|
|
|
|
void *raw_args;
|
|
|
|
};
|
2021-08-01 21:35:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static vector<tuple<const char *, const char *, void **>> *xhook_list;
|
|
|
|
static vector<JNINativeMethod> *jni_hook_list;
|
|
|
|
static hash_map<xstring, tree_map<xstring, tree_map<xstring, void *>>> *jni_method_map;
|
|
|
|
|
|
|
|
static JavaVM *g_jvm;
|
|
|
|
static HookContext *current_ctx;
|
2021-01-08 08:53:24 +00:00
|
|
|
|
2021-01-10 04:39:59 +00:00
|
|
|
#define DCL_HOOK_FUNC(ret, func, ...) \
|
|
|
|
static ret (*old_##func)(__VA_ARGS__); \
|
|
|
|
static ret new_##func(__VA_ARGS__)
|
|
|
|
|
|
|
|
#define DCL_JNI_FUNC(name) \
|
2021-08-02 10:20:19 +00:00
|
|
|
static void *name##_orig; \
|
2021-08-01 21:35:16 +00:00
|
|
|
extern const JNINativeMethod name##_methods[]; \
|
2021-01-10 04:39:59 +00:00
|
|
|
extern const int name##_methods_num;
|
2021-01-08 13:25:44 +00:00
|
|
|
|
2021-01-10 01:41:25 +00:00
|
|
|
namespace {
|
|
|
|
// JNI method declarations
|
2021-01-10 04:39:59 +00:00
|
|
|
DCL_JNI_FUNC(nativeForkAndSpecialize)
|
|
|
|
DCL_JNI_FUNC(nativeSpecializeAppProcess)
|
|
|
|
DCL_JNI_FUNC(nativeForkSystemServer)
|
2021-01-10 01:41:25 +00:00
|
|
|
}
|
|
|
|
|
2021-01-10 04:39:59 +00:00
|
|
|
#define HOOK_JNI(method) \
|
2021-08-02 10:20:19 +00:00
|
|
|
if (methods[i].name == #method##sv) { \
|
2021-08-01 21:35:16 +00:00
|
|
|
jni_hook_list->push_back(methods[i]); \
|
2021-08-02 10:20:19 +00:00
|
|
|
method##_orig = methods[i].fnPtr; \
|
2021-08-01 21:35:16 +00:00
|
|
|
for (int j = 0; j < method##_methods_num; ++j) { \
|
|
|
|
if (strcmp(methods[i].signature, method##_methods[j].signature) == 0) { \
|
|
|
|
newMethods[i] = method##_methods[j]; \
|
|
|
|
LOGI("hook: replaced #" #method "\n"); \
|
|
|
|
++hooked; \
|
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
continue; \
|
2021-01-08 13:25:44 +00:00
|
|
|
}
|
|
|
|
|
2021-01-10 04:39:59 +00:00
|
|
|
DCL_HOOK_FUNC(int, jniRegisterNativeMethods,
|
2021-01-08 08:53:24 +00:00
|
|
|
JNIEnv *env, const char *className, const JNINativeMethod *methods, int numMethods) {
|
|
|
|
LOGD("hook: jniRegisterNativeMethods %s", className);
|
|
|
|
|
2021-01-08 13:25:44 +00:00
|
|
|
if (g_jvm == nullptr) {
|
|
|
|
// Save for later unhooking
|
|
|
|
env->GetJavaVM(&g_jvm);
|
|
|
|
}
|
2021-01-08 08:53:24 +00:00
|
|
|
|
2021-08-01 21:35:16 +00:00
|
|
|
unique_ptr<JNINativeMethod[]> newMethods;
|
|
|
|
int hooked = numeric_limits<int>::max();
|
2021-01-08 13:25:44 +00:00
|
|
|
if (className == "com/android/internal/os/Zygote"sv) {
|
2021-08-01 21:35:16 +00:00
|
|
|
hooked = 0;
|
2021-01-10 04:39:59 +00:00
|
|
|
newMethods = make_unique<JNINativeMethod[]>(numMethods);
|
|
|
|
memcpy(newMethods.get(), methods, sizeof(JNINativeMethod) * numMethods);
|
2021-08-01 21:35:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto &class_map = (*jni_method_map)[className];
|
|
|
|
for (int i = 0; i < numMethods; ++i) {
|
|
|
|
class_map[methods[i].name][methods[i].signature] = methods[i].fnPtr;
|
2021-08-02 10:20:19 +00:00
|
|
|
if (hooked < 3) {
|
|
|
|
HOOK_JNI(nativeForkAndSpecialize);
|
|
|
|
HOOK_JNI(nativeSpecializeAppProcess);
|
|
|
|
HOOK_JNI(nativeForkSystemServer);
|
|
|
|
}
|
2021-01-08 13:25:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return old_jniRegisterNativeMethods(env, className, newMethods.get() ?: methods, numMethods);
|
2021-01-08 08:53:24 +00:00
|
|
|
}
|
|
|
|
|
2021-01-10 09:25:30 +00:00
|
|
|
DCL_HOOK_FUNC(int, fork) {
|
2021-08-02 10:20:19 +00:00
|
|
|
return current_ctx ? current_ctx->pid : old_fork();
|
2021-01-10 09:25:30 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 11:28:00 +00:00
|
|
|
DCL_HOOK_FUNC(int, selinux_android_setcontext,
|
|
|
|
uid_t uid, int isSystemServer, const char *seinfo, const char *pkgname) {
|
|
|
|
if (current_ctx && current_ctx->do_hide) {
|
|
|
|
// Ask magiskd to hide ourselves before switching context
|
|
|
|
// because magiskd socket is not accessible on Android 8.0+
|
|
|
|
remote_request_hide();
|
|
|
|
LOGD("hook: process successfully hidden\n");
|
|
|
|
}
|
|
|
|
return old_selinux_android_setcontext(uid, isSystemServer, seinfo, pkgname);
|
|
|
|
}
|
|
|
|
|
2021-01-10 09:25:30 +00:00
|
|
|
static int sigmask(int how, int signum) {
|
|
|
|
sigset_t set;
|
|
|
|
sigemptyset(&set);
|
|
|
|
sigaddset(&set, signum);
|
|
|
|
return sigprocmask(how, &set, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------
|
|
|
|
|
2021-08-02 10:20:19 +00:00
|
|
|
static void nativeSpecializeAppProcess_pre(HookContext *ctx, JNIEnv *env, jclass clazz) {
|
2021-01-12 11:28:00 +00:00
|
|
|
current_ctx = ctx;
|
2021-08-02 10:20:19 +00:00
|
|
|
const char *process = env->GetStringUTFChars(ctx->args->nice_name, nullptr);
|
2021-01-12 11:28:00 +00:00
|
|
|
LOGD("hook: %s %s\n", __FUNCTION__, process);
|
2021-01-10 01:41:25 +00:00
|
|
|
|
2021-08-02 10:20:19 +00:00
|
|
|
if (ctx->args->mount_external != 0 /* TODO: Handle MOUNT_EXTERNAL_NONE cases */
|
|
|
|
&& remote_check_hide(ctx->args->uid, process)) {
|
2021-01-12 11:28:00 +00:00
|
|
|
ctx->do_hide = true;
|
|
|
|
LOGI("hook: [%s] should be hidden\n", process);
|
|
|
|
}
|
2021-01-10 09:25:30 +00:00
|
|
|
|
2021-08-02 10:20:19 +00:00
|
|
|
env->ReleaseStringUTFChars(ctx->args->nice_name, process);
|
2021-01-12 11:28:00 +00:00
|
|
|
}
|
2021-01-10 09:25:30 +00:00
|
|
|
|
2021-01-12 11:28:00 +00:00
|
|
|
static void nativeSpecializeAppProcess_post(HookContext *ctx, JNIEnv *env, jclass clazz) {
|
2021-08-02 10:20:19 +00:00
|
|
|
current_ctx = nullptr;
|
2021-01-10 01:41:25 +00:00
|
|
|
LOGD("hook: %s\n", __FUNCTION__);
|
2021-01-10 09:25:30 +00:00
|
|
|
|
2021-01-12 11:28:00 +00:00
|
|
|
if (ctx->do_hide)
|
2021-01-10 01:41:25 +00:00
|
|
|
self_unload();
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------
|
|
|
|
|
2021-08-02 10:20:19 +00:00
|
|
|
// Do our own fork before loading any 3rd party code
|
|
|
|
// First block SIGCHLD, unblock after original fork is done
|
|
|
|
#define PRE_FORK() \
|
|
|
|
current_ctx = ctx; \
|
|
|
|
sigmask(SIG_BLOCK, SIGCHLD); \
|
|
|
|
ctx->pid = old_fork(); \
|
|
|
|
if (ctx->pid != 0) \
|
2021-01-12 11:28:00 +00:00
|
|
|
return;
|
|
|
|
|
2021-08-02 10:20:19 +00:00
|
|
|
// Unblock SIGCHLD in case the original method didn't
|
|
|
|
#define POST_FORK() \
|
|
|
|
current_ctx = nullptr; \
|
|
|
|
sigmask(SIG_UNBLOCK, SIGCHLD); \
|
|
|
|
if (ctx->pid != 0)\
|
|
|
|
return;
|
|
|
|
|
|
|
|
static void nativeForkAndSpecialize_pre(HookContext *ctx, JNIEnv *env, jclass clazz) {
|
|
|
|
PRE_FORK();
|
|
|
|
nativeSpecializeAppProcess_pre(ctx, env, clazz);
|
2021-01-10 01:41:25 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 11:28:00 +00:00
|
|
|
static void nativeForkAndSpecialize_post(HookContext *ctx, JNIEnv *env, jclass clazz) {
|
2021-08-02 10:20:19 +00:00
|
|
|
POST_FORK();
|
2021-01-12 11:28:00 +00:00
|
|
|
nativeSpecializeAppProcess_post(ctx, env, clazz);
|
2021-01-10 01:41:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------
|
|
|
|
|
2021-08-02 10:20:19 +00:00
|
|
|
static void nativeForkSystemServer_pre(HookContext *ctx, JNIEnv *env, jclass clazz) {
|
|
|
|
PRE_FORK();
|
2021-01-10 01:41:25 +00:00
|
|
|
LOGD("hook: %s\n", __FUNCTION__);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void nativeForkSystemServer_post(HookContext *ctx, JNIEnv *env, jclass clazz) {
|
2021-08-02 10:20:19 +00:00
|
|
|
POST_FORK();
|
2021-01-10 01:41:25 +00:00
|
|
|
LOGD("hook: %s\n", __FUNCTION__);
|
|
|
|
}
|
|
|
|
|
2021-08-02 10:20:19 +00:00
|
|
|
#undef PRE_FORK
|
|
|
|
#undef POST_FORK
|
|
|
|
|
2021-01-10 04:39:59 +00:00
|
|
|
// -----------------------------------------------------------------
|
|
|
|
|
2021-01-08 08:53:24 +00: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 13:07:17 +00:00
|
|
|
xhook_list->emplace_back(path, symbol, old_func);
|
2021-01-08 08:53:24 +00: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 13:07:17 +00:00
|
|
|
xhook_list = new remove_pointer_t<decltype(xhook_list)>();
|
2021-08-01 21:35:16 +00:00
|
|
|
jni_hook_list = new remove_pointer_t<decltype(jni_hook_list)>();
|
|
|
|
jni_method_map = new remove_pointer_t<decltype(jni_method_map)>();
|
2021-01-10 13:07:17 +00:00
|
|
|
|
2021-01-08 08:53:24 +00:00
|
|
|
XHOOK_REGISTER(".*\\libandroid_runtime.so$", jniRegisterNativeMethods);
|
2021-01-10 09:25:30 +00:00
|
|
|
XHOOK_REGISTER(".*\\libandroid_runtime.so$", fork);
|
2021-01-12 11:28:00 +00:00
|
|
|
XHOOK_REGISTER(".*\\libandroid_runtime.so$", selinux_android_setcontext);
|
2021-01-08 08:53:24 +00:00
|
|
|
hook_refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool unhook_functions() {
|
2021-01-08 13:25:44 +00:00
|
|
|
JNIEnv* env;
|
|
|
|
if (g_jvm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK)
|
|
|
|
return false;
|
|
|
|
|
2021-08-01 21:35:16 +00:00
|
|
|
// Do NOT call any destructors
|
|
|
|
operator delete(jni_method_map);
|
|
|
|
// Directly unmap the whole memory block
|
|
|
|
jni_hook::memory_block::release();
|
|
|
|
|
2021-01-10 01:41:25 +00:00
|
|
|
// Unhook JNI methods
|
2021-08-01 21:35:16 +00:00
|
|
|
if (!jni_hook_list->empty() && old_jniRegisterNativeMethods(env,
|
2021-01-08 13:25:44 +00:00
|
|
|
"com/android/internal/os/Zygote",
|
2021-08-01 21:35:16 +00:00
|
|
|
jni_hook_list->data(), jni_hook_list->size()) != 0) {
|
2021-01-10 13:07:17 +00:00
|
|
|
LOGE("hook: Failed to register JNI hook\n");
|
2021-01-08 13:25:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-08-01 21:35:16 +00:00
|
|
|
delete jni_hook_list;
|
2021-01-08 13:25:44 +00:00
|
|
|
|
2021-01-10 01:41:25 +00:00
|
|
|
// Unhook xhook
|
2021-01-10 13:07:17 +00:00
|
|
|
for (auto &[path, sym, old_func] : *xhook_list) {
|
2021-01-08 08:53:24 +00:00
|
|
|
if (xhook_register(path, sym, *old_func, nullptr) != 0) {
|
2021-01-08 13:25:44 +00:00
|
|
|
LOGE("hook: Failed to register hook \"%s\"\n", sym);
|
2021-01-08 08:53:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2021-01-10 13:07:17 +00:00
|
|
|
delete xhook_list;
|
2021-01-08 08:53:24 +00:00
|
|
|
return hook_refresh();
|
|
|
|
}
|
2021-01-10 01:41:25 +00:00
|
|
|
|
2021-08-02 10:20:19 +00:00
|
|
|
// JNI method definitions, include all method signatures of past Android versions
|
2021-01-10 01:41:25 +00:00
|
|
|
#include "jni_hooks.hpp"
|