feat: Add null checks for JNI method arguments

Adds a null pointer check for the origApkPath and cacheApkPath arguments in the enableOpenatHook native method.

Previously, passing nullptr to this method would lead to a potential crash. The added checks ensure the function handles invalid input gracefully by logging an error and returning early, thus preventing a potential crash.
This commit is contained in:
NkBe 2025-09-30 23:23:31 +08:00
parent 4b8da5c255
commit 5bbef84a43
No known key found for this signature in database
GPG Key ID: 75EF144ED8F4D7B8
2 changed files with 43 additions and 33 deletions

View File

@ -2,6 +2,8 @@ name: Build CI
on:
workflow_dispatch:
pull_request:
merge_group:
jobs:
build:
@ -41,7 +43,7 @@ jobs:
uses: actions/cache@v4
with:
path: ~/.ccache
key: ${{ runner.os }}-ccache-${{ github.sha }}
key: ${{ runner.os }}-ccache-${{ hashFiles('**/src/**/*.cpp', '**/src/**/*.h', '**/CMakeLists.txt') }}
restore-keys: |
${{ runner.os }}-ccache-

View File

@ -1,6 +1,6 @@
//
// Created by VIP on 2021/4/25.
// Update by HSSkyBoy on 2025/9/7
// Update by HSSkyBoy on 2025/9/11
//
#include "bypass_sig.h"
@ -17,57 +17,65 @@ using lsplant::operator""_sym;
namespace lspd {
std::string apkPath;
std::string redirectPath;
std::string apkPath;
std::string redirectPath;
inline static constexpr const char* kLibCName = "libc.so";
inline static constexpr const char* kLibCName = "libc.so";
// 修改回傳型別以匹配 kImg 的實際型別
std::unique_ptr<SandHook::ElfImg> &GetC(bool release = false) {
static auto kImg = std::make_unique<SandHook::ElfImg>(kLibCName);
if (release) {
kImg.reset();
kImg = nullptr;
std::unique_ptr<SandHook::ElfImg> &GetC(bool release = false) {
static auto kImg = std::make_unique<SandHook::ElfImg>(kLibCName);
if (release) {
kImg.reset();
kImg = nullptr;
}
return kImg;
}
return kImg;
}
inline static auto __openat_ =
"__openat"_sym.hook->*[]<lsplant::Backup auto backup>(int fd, const char *pathname, int flag,
int mode) static -> int {
if (pathname && strcmp(pathname, apkPath.c_str()) == 0) {
return backup(fd, redirectPath.c_str(), flag, mode);
}
return backup(fd, pathname, flag, mode);
};
inline static auto __openat_ =
"__openat"_sym.hook->*[]<lsplant::Backup auto backup>(int fd, const char *pathname, int flag,
int mode) static -> int {
if (pathname && strcmp(pathname, apkPath.c_str()) == 0) {
return backup(fd, redirectPath.c_str(), flag, mode);
}
return backup(fd, pathname, flag, mode);
};
static bool HookOpenat(const lsplant::HookHandler &handler) { return handler(__openat_); }
LSP_DEF_NATIVE_METHOD(void, SigBypass, enableOpenatHook, jstring origApkPath,
jstring cacheApkPath) {
if (origApkPath == nullptr || cacheApkPath == nullptr) {
LOGE("Invalid arguments: original or cache path is null.");
return;
}
lsplant::JUTFString str1(env, origApkPath);
lsplant::JUTFString str2(env, cacheApkPath);
apkPath = str1.get();
redirectPath = str2.get();
auto r = HookOpenat(lsplant::InitInfo{
.inline_hooker =
[](auto t, auto r) {
void *bk = nullptr;
return HookInline(t, r, &bk) == 0 ? bk : nullptr;
},
.art_symbol_resolver = [](auto symbol) { return GetC()->getSymbAddress(symbol); },
});
if (!r) {
LOGE("Hook __openat fail");
auto r = HookOpenat(lsplant::InitInfo{
.inline_hooker =
[](auto t, auto r) {
void *bk = nullptr;
return HookInline(t, r, &bk) == 0 ? bk : nullptr;
},
.art_symbol_resolver = [](auto symbol) {
return GetC()->getSymbAddress(symbol);
},
});
if (!r) {
LOGE("Hook __openat fail");
}
// 无论 Hook 成功与否,都确保清除 libc.so 的 ElfImg
GetC(true);
}
static JNINativeMethod gMethods[] = {
LSP_NATIVE_METHOD(SigBypass, enableOpenatHook, "(Ljava/lang/String;Ljava/lang/String;)V")};
static JNINativeMethod gMethods[] = {
LSP_NATIVE_METHOD(SigBypass, enableOpenatHook, "(Ljava/lang/String;Ljava/lang/String;)V")};
void RegisterBypass(JNIEnv *env) { REGISTER_LSP_NATIVE_METHODS(SigBypass); }
void RegisterBypass(JNIEnv *env) { REGISTER_LSP_NATIVE_METHODS(SigBypass); }
} // namespace lspd