From b409cd39e40de0bd5bb881e499016de57fcf24b3 Mon Sep 17 00:00:00 2001 From: JingMatrix Date: Mon, 29 Jul 2024 12:07:21 +0200 Subject: [PATCH] Update JingMatrix/LSPosed Fix proguard rules for release build --- core | 2 +- manager/proguard-rules.pro | 1 + .../include/art/runtime/jit/profile_saver.h | 79 ++++++++++--------- .../include/art/runtime/oat_file_manager.h | 44 ++++++----- .../src/main/jni/src/jni/bypass_sig.cpp | 72 +++++++++-------- .../src/main/jni/src/patch_loader.cpp | 10 +-- 6 files changed, 111 insertions(+), 97 deletions(-) diff --git a/core b/core index bdba029f..2965a301 160000 --- a/core +++ b/core @@ -1 +1 @@ -Subproject commit bdba029fe9ebde961a4e7f9047226d4d7258c233 +Subproject commit 2965a301eaee87c7932c3b3f3286b6726cabd70b diff --git a/manager/proguard-rules.pro b/manager/proguard-rules.pro index 4ceeb1c9..edd5d4f3 100644 --- a/manager/proguard-rules.pro +++ b/manager/proguard-rules.pro @@ -10,6 +10,7 @@ } -keep class com.beust.jcommander.** { *; } +-keep class org.lsposed.lspatch.database.** { *; } -keep class org.lsposed.lspatch.Patcher$Options { *; } -keep class org.lsposed.lspatch.share.LSPConfig { *; } -keep class org.lsposed.lspatch.share.PatchConfig { *; } diff --git a/patch-loader/src/main/jni/include/art/runtime/jit/profile_saver.h b/patch-loader/src/main/jni/include/art/runtime/jit/profile_saver.h index ca543507..bb573878 100644 --- a/patch-loader/src/main/jni/include/art/runtime/jit/profile_saver.h +++ b/patch-loader/src/main/jni/include/art/runtime/jit/profile_saver.h @@ -10,47 +10,48 @@ using namespace lsplant; namespace art { - CREATE_MEM_HOOK_STUB_ENTRY( - "_ZN3art12ProfileSaver20ProcessProfilingInfoEbPt", - bool, ProcessProfilingInfo, (void * thiz, bool, uint16_t *), { - LOGD("skipped profile saving"); - return true; - }); - - CREATE_MEM_HOOK_STUB_ENTRY( - "_ZN3art12ProfileSaver20ProcessProfilingInfoEbbPt", - bool, ProcessProfilingInfoWithBool, (void * thiz, bool, bool, uint16_t *), { - LOGD("skipped profile saving"); - return true; - }); - - CREATE_HOOK_STUB_ENTRY( - "execve", - int, execve, (const char *pathname, const char *argv[], char *const envp[]), { - if (strstr(pathname, "dex2oat")) { - size_t count = 0; - while (argv[count++] != nullptr); - std::unique_ptr new_args = std::make_unique( - count + 1); - for (size_t i = 0; i < count - 1; ++i) - new_args[i] = argv[i]; - new_args[count - 1] = "--inline-max-code-units=0"; - new_args[count] = nullptr; - - LOGD("dex2oat by disable inline!"); - int ret = backup(pathname, new_args.get(), envp); - return ret; - } - int ret = backup(pathname, argv, envp); +class ProfileSaver { +private: + inline static MemberHooker<"_ZN3art12ProfileSaver20ProcessProfilingInfoEbPt", ProfileSaver, + bool(bool, uint16_t *)> + ProcessProfilingInfo_ = +[](ProfileSaver *thiz, bool a, uint16_t *b) { + LOGD("skipped profile saving"); + return true; + }; + + inline static MemberHooker<"_ZN3art12ProfileSaver20ProcessProfilingInfoEbbPt", ProfileSaver, + bool(bool, bool, uint16_t *)> + ProcessProfilingInfoWithBool_ = +[](ProfileSaver *thiz, bool, bool, uint16_t *) { + LOGD("skipped profile saving"); + return true; + }; + + inline static Hooker<"execve", + int(const char *pathname, const char *argv[], char *const envp[])> + execve_ = +[](const char *pathname, const char *argv[], char *const envp[]) { + if (strstr(pathname, "dex2oat")) { + size_t count = 0; + while (argv[count++] != nullptr); + std::unique_ptr new_args = + std::make_unique(count + 1); + for (size_t i = 0; i < count - 1; ++i) new_args[i] = argv[i]; + new_args[count - 1] = "--inline-max-code-units=0"; + new_args[count] = nullptr; + + LOGD("dex2oat by disable inline!"); + int ret = execve_(pathname, new_args.get(), envp); return ret; - }); - + } + int ret = execve_(pathname, argv, envp); + return ret; + }; +public: static void DisableInline(const HookHandler &handler) { - HookSyms(handler, ProcessProfilingInfo, ProcessProfilingInfoWithBool); - HookSymNoHandle(handler, reinterpret_cast(&::execve), execve); + handler.hook(ProcessProfilingInfo_, ProcessProfilingInfoWithBool_); + handler.hook(execve_); } -} - +}; +} // namespace art -#endif //LSPATCH_PROFILE_SAVER_H +#endif // LSPATCH_PROFILE_SAVER_H diff --git a/patch-loader/src/main/jni/include/art/runtime/oat_file_manager.h b/patch-loader/src/main/jni/include/art/runtime/oat_file_manager.h index d72be48b..c01f20ab 100644 --- a/patch-loader/src/main/jni/include/art/runtime/oat_file_manager.h +++ b/patch-loader/src/main/jni/include/art/runtime/oat_file_manager.h @@ -26,39 +26,43 @@ using namespace lsplant; namespace art { - CREATE_MEM_HOOK_STUB_ENTRY( - "_ZN3art14OatFileManager25RunBackgroundVerificationERKNSt3__16vectorIPKNS_7DexFileENS1_9allocatorIS5_EEEEP8_jobjectPKc", - void, RunBackgroundVerificationWithContext, - (void * thiz, const std::vector &dex_files, - jobject class_loader, - const char *class_loader_context), { +class FileManager { +public: + inline static MemberHooker< + "_ZN3art14OatFileManager25RunBackgroundVerificationERKNSt3__16vectorIPKNS_7DexFileENS1_9allocatorIS5_EEEEP8_jobjectPKc", + FileManager, void(const std::vector &, jobject, const char *)> + RunBackgroundVerificationWithContext_ = + +[](FileManager *thiz, const std::vector &dex_files, jobject class_loader, + const char *class_loader_context) { if (lspd::Context::GetInstance()->GetCurrentClassLoader() == nullptr) { LOGD("Disabled background verification"); return; } - backup(thiz, dex_files, class_loader, class_loader_context); - }); + RunBackgroundVerificationWithContext_(thiz, dex_files, class_loader, + class_loader_context); + }; - CREATE_MEM_HOOK_STUB_ENTRY( - "_ZN3art14OatFileManager25RunBackgroundVerificationERKNSt3__16vectorIPKNS_7DexFileENS1_9allocatorIS5_EEEEP8_jobject", - void, RunBackgroundVerification, - (void * thiz, const std::vector &dex_files, - jobject class_loader), { + inline static MemberHooker< + "_ZN3art14OatFileManager25RunBackgroundVerificationERKNSt3__16vectorIPKNS_7DexFileENS1_9allocatorIS5_EEEEP8_jobject", + FileManager, void(const std::vector &, jobject)> + RunBackgroundVerification_ = + +[](FileManager *thiz, const std::vector &dex_files, + jobject class_loader) { if (lspd::Context::GetInstance()->GetCurrentClassLoader() == nullptr) { LOGD("Disabled background verification"); return; } - backup(thiz, dex_files, class_loader); - }); - + RunBackgroundVerification_(thiz, dex_files, class_loader); + }; +public: static void DisableBackgroundVerification(const lsplant::HookHandler &handler) { const int api_level = lspd::GetAndroidApiLevel(); if (api_level >= __ANDROID_API_Q__) { - HookSyms(handler, RunBackgroundVerificationWithContext, RunBackgroundVerification); + handler.hook(RunBackgroundVerificationWithContext_, RunBackgroundVerification_); } } -} - +}; +} // namespace art -#endif //LSPATCH_OAT_FILE_MANAGER_H +#endif // LSPATCH_OAT_FILE_MANAGER_H diff --git a/patch-loader/src/main/jni/src/jni/bypass_sig.cpp b/patch-loader/src/main/jni/src/jni/bypass_sig.cpp index c546fbc4..d0846d12 100644 --- a/patch-loader/src/main/jni/src/jni/bypass_sig.cpp +++ b/patch-loader/src/main/jni/src/jni/bypass_sig.cpp @@ -3,6 +3,8 @@ // #include "bypass_sig.h" + +#include "../src/native_api.h" #include "elf_util.h" #include "logging.h" #include "native_util.h" @@ -12,40 +14,46 @@ namespace lspd { - std::string apkPath; - std::string redirectPath; - - CREATE_HOOK_STUB_ENTRY( - "__openat", - int, __openat, - (int fd, const char* pathname, int flag, int mode), { - if (pathname == apkPath) { - LOGD("redirect openat"); - return backup(fd, redirectPath.c_str(), flag, mode); - } - return backup(fd, pathname, flag, mode); - }); - - LSP_DEF_NATIVE_METHOD(void, SigBypass, enableOpenatHook, jstring origApkPath, jstring cacheApkPath) { - auto sym_openat = SandHook::ElfImg("libc.so").getSymbAddress("__openat"); - auto r = HookSymNoHandle(handler, sym_openat, __openat); - if (!r) { - LOGE("Hook __openat fail"); - return; - } - lsplant::JUTFString str1(env, origApkPath); - lsplant::JUTFString str2(env, cacheApkPath); - apkPath = str1.get(); - redirectPath = str2.get(); - LOGD("apkPath %s", apkPath.c_str()); - LOGD("redirectPath %s", redirectPath.c_str()); - } +std::string apkPath; +std::string redirectPath; - static JNINativeMethod gMethods[] = { - LSP_NATIVE_METHOD(SigBypass, enableOpenatHook, "(Ljava/lang/String;Ljava/lang/String;)V") +inline static lsplant::Hooker<"__openat", int(int, const char*, int flag, int)> __openat_ = + +[](int fd, const char* pathname, int flag, int mode) { + if (pathname == apkPath) { + LOGD("redirect openat"); + return __openat_(fd, redirectPath.c_str(), flag, mode); + } + return __openat_(fd, pathname, flag, mode); }; - void RegisterBypass(JNIEnv* env) { - REGISTER_LSP_NATIVE_METHODS(SigBypass); +bool HookOpenat(const lsplant::HookHandler& handler) { return handler.hook(__openat_, true); } + +LSP_DEF_NATIVE_METHOD(void, SigBypass, enableOpenatHook, jstring origApkPath, + jstring cacheApkPath) { + auto r = HookOpenat(lsplant::InitInfo{ + .inline_hooker = + [](auto t, auto r) { + void* bk = nullptr; + return HookFunction(t, r, &bk) == 0 ? bk : nullptr; + }, + .art_symbol_resolver = + [](auto symbol) { return SandHook::ElfImg("libc.so").getSymbAddress(symbol); }, + }); + if (!r) { + LOGE("Hook __openat fail"); + return; } + lsplant::JUTFString str1(env, origApkPath); + lsplant::JUTFString str2(env, cacheApkPath); + apkPath = str1.get(); + redirectPath = str2.get(); + LOGD("apkPath %s", apkPath.c_str()); + LOGD("redirectPath %s", redirectPath.c_str()); } + +static JNINativeMethod gMethods[] = { + LSP_NATIVE_METHOD(SigBypass, enableOpenatHook, "(Ljava/lang/String;Ljava/lang/String;)V")}; + +void RegisterBypass(JNIEnv* env) { REGISTER_LSP_NATIVE_METHODS(SigBypass); } + +} // namespace lspd diff --git a/patch-loader/src/main/jni/src/patch_loader.cpp b/patch-loader/src/main/jni/src/patch_loader.cpp index fd962815..f05bb15d 100644 --- a/patch-loader/src/main/jni/src/patch_loader.cpp +++ b/patch-loader/src/main/jni/src/patch_loader.cpp @@ -74,8 +74,8 @@ namespace lspd { void PatchLoader::InitArtHooker(JNIEnv* env, const InitInfo& initInfo) { Context::InitArtHooker(env, initInfo); handler = initInfo; - art::DisableInline(initInfo); - art::DisableBackgroundVerification(initInfo); + art::ProfileSaver::DisableInline(initInfo); + art::FileManager::DisableBackgroundVerification(initInfo); } void PatchLoader::InitHooks(JNIEnv* env) { @@ -95,13 +95,13 @@ namespace lspd { lsplant::InitInfo initInfo { .inline_hooker = [](auto t, auto r) { void* bk = nullptr; - return HookFunction(t, r, &bk) == RS_SUCCESS ? bk : nullptr; + return HookArtFunction(t, r, &bk) == 0 ? bk : nullptr; }, .inline_unhooker = [](auto t) { - return UnhookFunction(t) == RT_SUCCESS; + return UnhookArtFunction(t) == 0; }, .art_symbol_resolver = [](auto symbol) { - return GetArt()->getSymbAddress(symbol); + return GetArt()->getSymbAddress(symbol); }, .art_symbol_prefix_resolver = [](auto symbol) { return GetArt()->getSymbPrefixFirstAddress(symbol);