fix(adrenotools): resolve linker internals on first use so Android 17 Pixels can start a session

linkernsbypass ran its libdl walk from a load-time constructor, so every
System.loadLibrary of the compositor library executed it. On Android 17
Pixel builds the branch target sits on a guarded page that was never
unprotected (the second mprotect received the local variable's address),
and the indirect call raised SIGILL before any session could start.

Resolution now happens once from linkernsbypass_load_status(), the scan
is bounded, the target page itself is unprotected, and a page that
cannot be unprotected makes the bypass report failure instead of being
called. libhook_impl checks the status before using the namespace calls.
This commit is contained in:
The412Banner committed 2026-11-27 11:08:44 +00:00
1 parent f65e7a3d43
commit 98c8ee318d
4 files changed
+226 -5

No files matched your search

@@ -21,8 +21,14 @@ static loader_android_create_namespace_t loader_android_create_namespace;
static bool lib_loaded;
static void resolve_linker_symbols();
bool linkernsbypass_load_status() {
static const bool resolved{[]() {
resolve_linker_symbols();
return true;
}()};
(void)resolved;
return lib_loaded;
}
@@ -118,7 +124,13 @@ static void *align_ptr(void *ptr) {
return reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(ptr) & ~(getpagesize() - 1));
}
__attribute__((constructor)) static void resolve_linker_symbols() {
static bool unguard_page(const void *code) {
auto page{align_ptr(const_cast<void *>(code))};
return mprotect(page, getpagesize(), PROT_WRITE | PROT_READ | PROT_EXEC) == 0 ||
mprotect(page, getpagesize(), PROT_READ | PROT_EXEC) == 0;
}
static void resolve_linker_symbols() {
using loader_dlopen_t = void *(*)(const char *, int, const void *);
if (android_get_device_api_level() < 28)
@@ -139,16 +151,21 @@ __attribute__((constructor)) static void resolve_linker_symbols() {
};
static_assert(sizeof(BranchLinked) == 4, "BranchLinked is wrong size");
mprotect(align_ptr(reinterpret_cast<void *>(&dlopen)), getpagesize(), PROT_WRITE | PROT_READ | PROT_EXEC);
constexpr int maxWrapperInstrs{32};
if (!unguard_page(reinterpret_cast<const void *>(&dlopen)))
return static_cast<loader_dlopen_t>(nullptr);
auto blInstr{reinterpret_cast<BranchLinked *>(&dlopen)};
while (!blInstr->Verify())
blInstr++;
for (int i{}; !blInstr->Verify(); i++, blInstr++)
if (i == maxWrapperInstrs)
return static_cast<loader_dlopen_t>(nullptr);
return reinterpret_cast<loader_dlopen_t>(blInstr + blInstr->offset);
}()};
mprotect(align_ptr(reinterpret_cast<void *>(&loader_dlopen)), getpagesize(), PROT_WRITE | PROT_READ | PROT_EXEC);
if (!loader_dlopen || !unguard_page(reinterpret_cast<const void *>(loader_dlopen)))
return;
auto ldHandle{loader_dlopen("ld-android.so", RTLD_LAZY, reinterpret_cast<void *>(&dlopen))};
if (!ldHandle)
@@ -51,6 +51,11 @@ __attribute__((visibility("default"))) void *hook_android_dlopen_ext(const char
return fallback();
}
if (!linkernsbypass_load_status()) {
LOGI("hook_android_dlopen_ext: hook failed: linkernsbypass failed to load!");
return fallback();
}
auto driverNs{android_create_namespace(filename, hook_params->customDriverDir.c_str(),
hook_params->hookLibDir.c_str(), ANDROID_NAMESPACE_TYPE_SHARED,
nullptr, extinfo->library_namespace)};
@@ -133,6 +138,9 @@ __attribute__((visibility("default"))) void *hook_android_dlopen_ext(const char
__attribute__((visibility("default"))) void *hook_android_load_sphal_library(const char *filename, int flags) {
LOGI("hook_android_load_sphal_library: filename: %s", filename);
if (!linkernsbypass_load_status())
return nullptr;
for (const char *name : {"sphal", "vendor", "default"}) {
if (auto vendorNs{android_get_exported_namespace(name)}) {
android_dlextinfo dlextinfo{