From 332d99784e220e5b99d4998ea0715fc79bb870e0 Mon Sep 17 00:00:00 2001 From: JingMatrix Date: Fri, 13 Sep 2024 10:20:01 +0200 Subject: [PATCH] Fix log formatting of __openat hook --- .../src/main/jni/src/jni/bypass_sig.cpp | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) 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 8b371c9e..06d993b2 100644 --- a/patch-loader/src/main/jni/src/jni/bypass_sig.cpp +++ b/patch-loader/src/main/jni/src/jni/bypass_sig.cpp @@ -2,8 +2,9 @@ // Created by VIP on 2021/4/25. // -#include "../src/native_api.h" #include "bypass_sig.h" + +#include "../src/native_api.h" #include "elf_util.h" #include "logging.h" #include "native_util.h" @@ -16,10 +17,22 @@ namespace lspd { std::string apkPath; std::string redirectPath; -inline static lsplant::Hooker<"__openat", int(int, const char*, int flag, int)> __openat_ = +inline static constexpr auto kLibCName = "libc.so"; + +std::unique_ptr& GetC(bool release = false) { + static std::unique_ptr kImg = nullptr; + if (release) { + kImg.reset(); + } else if (!kImg) { + kImg = std::make_unique(kLibCName); + } + return kImg; +} + +inline static lsplant::Hooker<"__openat", int(int, const char*, int, int)> __openat_ = +[](int fd, const char* pathname, int flag, int mode) { if (pathname == apkPath) { - LOGD("redirect openat"); + LOGD("Redirect openat from {} to {}", pathname, redirectPath); return __openat_(fd, redirectPath.c_str(), flag, mode); } return __openat_(fd, pathname, flag, mode); @@ -35,8 +48,7 @@ LSP_DEF_NATIVE_METHOD(void, SigBypass, enableOpenatHook, jstring origApkPath, void* bk = nullptr; return HookInline(t, r, &bk) == 0 ? bk : nullptr; }, - .art_symbol_resolver = - [](auto symbol) { return SandHook::ElfImg("libc.so").getSymbAddress(symbol); }, + .art_symbol_resolver = [](auto symbol) { return GetC()->getSymbAddress(symbol); }, }); if (!r) { LOGE("Hook __openat fail"); @@ -46,8 +58,9 @@ LSP_DEF_NATIVE_METHOD(void, SigBypass, enableOpenatHook, jstring origApkPath, lsplant::JUTFString str2(env, cacheApkPath); apkPath = str1.get(); redirectPath = str2.get(); - LOGD("apkPath %s", apkPath.c_str()); - LOGD("redirectPath %s", redirectPath.c_str()); + LOGD("apkPath {}", apkPath.c_str()); + LOGD("redirectPath {}", redirectPath.c_str()); + GetC(true); } static JNINativeMethod gMethods[] = {