From eaa017be86d844bef3698af591dc2bb156b89127 Mon Sep 17 00:00:00 2001 From: Dakkshesh Date: Mon, 11 Aug 2025 20:23:07 +0530 Subject: [PATCH] cpp: RefBase: Dont use incWeakRequireWeak() - Fixes the following on older android 10/11 platforms: 08-11 14:53:05.143 1388 1388 I TrickyStoreOSS: TrickyStore injector starting... 08-11 14:53:05.144 1388 1388 I TrickyStoreOSS: Starting injection of /data/adb/modules/tricky_store/libTrickyStoreOSS.so (entry: entry) into process 1024 08-11 14:53:05.151 587 587 I Zygote : Preloading shared libraries... 08-11 14:53:05.155 1026 1053 I ServiceManager: Waiting for service 'package_native' on '/dev/binder'... 08-11 14:53:05.155 1388 1388 W TrickyStoreOSS: Primary dlopen failed for library: /data/adb/modules/tricky_store/libTrickyStoreOSS.so, dlerror: dlopen failed: cannot locate symbol "_ZN7android7RefBase12weakref_type18incWeakRequireWeakEPKv" referenced by "/data/adb/modules/tricky_store/libTrickyStoreOSS.so"..., trying fallback 08-11 14:53:05.155 1388 1388 I TrickyStoreOSS: Using fallback dlopen method for compatibility 08-11 14:53:05.158 1388 1388 E TrickyStoreOSS: Fallback dlopen failed for library: /data/adb/modules/tricky_store/libTrickyStoreOSS.so, dlerror: dlopen failed: cannot locate symbol "_ZN7android7RefBase12weakref_type18incWeakRequireWeakEPKv" referenced by "/data/adb/modules/tricky_store/libTrickyStoreOSS.so"... 08-11 14:53:05.158 1388 1388 E TrickyStoreOSS: Failed to load library in remote process 08-11 14:53:05.158 1388 1388 E TrickyStoreOSS: Injection failed - Also improve error handling in injection process Signed-off-by: Dakkshesh --- .../cpp/external/AOSP/include/utils/RefBase.h | 4 +- app/src/main/cpp/inject/main.cpp | 42 +++++++++++++++++-- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/app/src/main/cpp/external/AOSP/include/utils/RefBase.h b/app/src/main/cpp/external/AOSP/include/utils/RefBase.h index 3170b82..8012c09 100644 --- a/app/src/main/cpp/external/AOSP/include/utils/RefBase.h +++ b/app/src/main/cpp/external/AOSP/include/utils/RefBase.h @@ -542,7 +542,9 @@ wp wp::fromExisting(T* other) { if (!other) return nullptr; auto refs = other->getWeakRefs(); - refs->incWeakRequireWeak(other); + //TrickyStoreOSS edit + //refs->incWeakRequireWeak(other); + refs->incWeak(other); wp ret; ret.m_ptr = other; diff --git a/app/src/main/cpp/inject/main.cpp b/app/src/main/cpp/inject/main.cpp index 6a5751c..1923b0a 100644 --- a/app/src/main/cpp/inject/main.cpp +++ b/app/src/main/cpp/inject/main.cpp @@ -225,12 +225,46 @@ static std::optional transfer_fd_to_remote(int pid, const char *lib_path, s return transferred_fd; } +static std::string get_remote_dlerror(int pid, struct user_regs_struct ®s, const std::vector &local_map, + const std::vector &remote_map, uintptr_t libc_return_addr) { + auto dlerror_addr = find_func_addr(local_map, remote_map, constants::kLibdlModule, "dlerror"); + if (!dlerror_addr) { + return "Failed to find dlerror function"; + } + + std::vector args; + auto dlerror_str_addr = remote_call(pid, regs, reinterpret_cast(dlerror_addr), libc_return_addr, args); + if (dlerror_str_addr == 0) { + return "dlerror returned null"; + } + + auto strlen_addr = find_func_addr(local_map, remote_map, constants::kLibcModule, "strlen"); + if (!strlen_addr) { + return "Failed to find strlen function"; + } + + args.clear(); + args.push_back(dlerror_str_addr); + auto dlerror_len = remote_call(pid, regs, reinterpret_cast(strlen_addr), libc_return_addr, args); + if (dlerror_len <= 0 || dlerror_len > 1024) { + return "Invalid dlerror string length"; + } + + std::string err; + err.resize(dlerror_len + 1, 0); + if (read_proc(pid, dlerror_str_addr, err.data(), dlerror_len) != dlerror_len) { + return "Failed to read dlerror string"; + } + err.resize(dlerror_len); + return err; +} + static std::optional remote_dlopen(int pid, struct user_regs_struct ®s, const std::vector &local_map, const std::vector &remote_map, int lib_fd, const char *lib_path, uintptr_t libc_return_addr) { auto dlopen_addr = find_func_addr(local_map, remote_map, constants::kLibdlModule, "android_dlopen_ext"); if (!dlopen_addr) { - LOGE("Failed to find android_dlopen_ext in %s", constants::kLibdlModule); + LOGW("Failed to find android_dlopen_ext in %s,", constants::kLibdlModule); return std::nullopt; } @@ -245,7 +279,8 @@ static std::optional remote_dlopen(int pid, struct user_regs_struct & uintptr_t remote_handle = remote_call(pid, regs, reinterpret_cast(dlopen_addr), libc_return_addr, args); if (remote_handle == 0) { - LOGE("Remote dlopen failed for library: %s", lib_path); + std::string error_msg = get_remote_dlerror(pid, regs, local_map, remote_map, libc_return_addr); + LOGW("Primary dlopen failed for library: %s, dlerror: %s", lib_path, error_msg.c_str()); return std::nullopt; } @@ -268,7 +303,8 @@ static std::optional remote_find_entry(int pid, struct user_regs_stru uintptr_t entry_addr = remote_call(pid, regs, reinterpret_cast(dlsym_addr), libc_return_addr, args); if (entry_addr == 0) { - LOGE("Failed to find entry symbol '%s' in remote library", constants::kEntrySymbol); + std::string error_msg = get_remote_dlerror(pid, regs, local_map, remote_map, libc_return_addr); + LOGE("Failed to find entry symbol '%s' in remote library, dlerror: %s", constants::kEntrySymbol, error_msg.c_str()); return std::nullopt; }