From 98c8ee318d896d24b81dbf63d35febae06578c8f Mon Sep 17 00:00:00 2001 From: The412Banner <205237651+The412Banner@users.noreply.github.com> Date: Fri, 27 Nov 2026 11:08:44 +0000 Subject: [PATCH] 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. --- .../lib/linkernsbypass/android_linker_ns.cpp | 27 +++- .../cpp/adrenotools/src/hook/hook_impl.cpp | 8 ++ tools/tests/linkernsbypass_harness.cpp | 125 ++++++++++++++++++ tools/tests/test_linkernsbypass.py | 71 ++++++++++ 4 files changed, 226 insertions(+), 5 deletions(-) create mode 100644 tools/tests/linkernsbypass_harness.cpp create mode 100644 tools/tests/test_linkernsbypass.py diff --git a/app/src/main/cpp/adrenotools/lib/linkernsbypass/android_linker_ns.cpp b/app/src/main/cpp/adrenotools/lib/linkernsbypass/android_linker_ns.cpp index 8220824eb1..c14c06578e 100644 --- a/app/src/main/cpp/adrenotools/lib/linkernsbypass/android_linker_ns.cpp +++ b/app/src/main/cpp/adrenotools/lib/linkernsbypass/android_linker_ns.cpp @@ -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(reinterpret_cast(ptr) & ~(getpagesize() - 1)); } -__attribute__((constructor)) static void resolve_linker_symbols() { +static bool unguard_page(const void *code) { + auto page{align_ptr(const_cast(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(&dlopen)), getpagesize(), PROT_WRITE | PROT_READ | PROT_EXEC); + constexpr int maxWrapperInstrs{32}; + + if (!unguard_page(reinterpret_cast(&dlopen))) + return static_cast(nullptr); auto blInstr{reinterpret_cast(&dlopen)}; - while (!blInstr->Verify()) - blInstr++; + for (int i{}; !blInstr->Verify(); i++, blInstr++) + if (i == maxWrapperInstrs) + return static_cast(nullptr); return reinterpret_cast(blInstr + blInstr->offset); }()}; - mprotect(align_ptr(reinterpret_cast(&loader_dlopen)), getpagesize(), PROT_WRITE | PROT_READ | PROT_EXEC); + if (!loader_dlopen || !unguard_page(reinterpret_cast(loader_dlopen))) + return; auto ldHandle{loader_dlopen("ld-android.so", RTLD_LAZY, reinterpret_cast(&dlopen))}; if (!ldHandle) diff --git a/app/src/main/cpp/adrenotools/src/hook/hook_impl.cpp b/app/src/main/cpp/adrenotools/src/hook/hook_impl.cpp index 8fa80ee185..53ce4eb67c 100644 --- a/app/src/main/cpp/adrenotools/src/hook/hook_impl.cpp +++ b/app/src/main/cpp/adrenotools/src/hook/hook_impl.cpp @@ -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{ diff --git a/tools/tests/linkernsbypass_harness.cpp b/tools/tests/linkernsbypass_harness.cpp new file mode 100644 index 0000000000..7e36979df9 --- /dev/null +++ b/tools/tests/linkernsbypass_harness.cpp @@ -0,0 +1,125 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +extern "C" { +uint32_t __wrap_dlopen[64]; +} + +static std::vector protected_pages; +static std::vector protected_flags; +static std::vector loaded; +static const void *loader_caller; +static uintptr_t locked_page; +static bool deny_write_exec; +static int failures; + +static void check(bool ok, const char *what) { + if (!ok) { + std::printf("FAIL %s\n", what); + failures++; + } +} + +static uintptr_t page_of(const void *p) { + return reinterpret_cast(p) & ~static_cast(getpagesize() - 1); +} + +extern "C" __attribute__((aligned(16), noinline)) void *fake_loader_dlopen(const char *name, int, const void *caller) { + loaded.emplace_back(name); + loader_caller = caller; + return reinterpret_cast(0x1000); +} + +static void fake_linker_entry() {} + +extern "C" int __wrap_mprotect(void *addr, size_t, int prot) { + protected_pages.push_back(reinterpret_cast(addr)); + protected_flags.push_back(prot); + if (reinterpret_cast(addr) == locked_page) + return -1; + if (deny_write_exec && (prot & PROT_WRITE) && (prot & PROT_EXEC)) + return -1; + return 0; +} + +extern "C" void *__wrap_dlsym(void *, const char *name) { + return std::strncmp(name, "__loader_", 9) == 0 ? reinterpret_cast(&fake_linker_entry) : nullptr; +} + +extern "C" void *android_dlopen_ext(const char *, int, const android_dlextinfo *) { + return nullptr; +} + +extern "C" bool elf_soname_patch(const char *, int, const char *) { + return false; +} + +static void place_bl(int index) { + for (auto &word : __wrap_dlopen) + word = 0xd503201f; + auto from{reinterpret_cast(&__wrap_dlopen[index])}; + auto to{reinterpret_cast(&fake_loader_dlopen)}; + __wrap_dlopen[index] = 0x94000000u | (static_cast((to - from) / 4) & 0x3ffffffu); +} + +static bool page_protected(const void *p) { + for (auto page : protected_pages) + if (page == page_of(p)) + return true; + return false; +} + +int main(int argc, char **argv) { + if (argc != 2) + return 2; + std::string mode{argv[1]}; + + check(protected_pages.empty() && loaded.empty(), "linker internals touched while the library loaded"); + + if (mode == "lazy") { + place_bl(6); + check(linkernsbypass_load_status(), "status after resolving"); + check(loaded.size() == 2 && loaded[0] == "ld-android.so" && loaded[1] == "libdl_android.so", "loader calls"); + check(loader_caller == __wrap_dlopen, "caller address is dlopen"); + check(page_protected(__wrap_dlopen), "dlopen page unguarded"); + check(page_protected(reinterpret_cast(&fake_loader_dlopen)), "loader target page unguarded"); + check(android_link_namespaces && android_link_namespaces_all_libs && android_get_exported_namespace, "symbols resolved"); + auto calls{protected_pages.size()}; + check(linkernsbypass_load_status(), "status on second call"); + check(loaded.size() == 2 && protected_pages.size() == calls, "resolved only once"); + } else if (mode == "far") { + place_bl(40); + check(!linkernsbypass_load_status(), "status without a nearby branch"); + check(loaded.empty(), "loader not called without a nearby branch"); + } else if (mode == "locked") { + place_bl(6); + locked_page = page_of(reinterpret_cast(&fake_loader_dlopen)); + check(!linkernsbypass_load_status(), "status with a locked target page"); + check(loaded.empty(), "loader not called on a locked target page"); + int attempts{}; + for (auto page : protected_pages) + attempts += page == locked_page; + check(attempts == 2, "target page tried with and without write"); + } else if (mode == "no_write_exec") { + place_bl(6); + deny_write_exec = true; + check(linkernsbypass_load_status(), "status when write and exec together are refused"); + check(loaded.size() == 2, "loader calls when write and exec together are refused"); + check(protected_flags.back() == (PROT_READ | PROT_EXEC), "read and exec fallback"); + } else { + return 2; + } + + if (failures) + return 1; + std::printf("PASS %s\n", mode.c_str()); + return 0; +} diff --git a/tools/tests/test_linkernsbypass.py b/tools/tests/test_linkernsbypass.py new file mode 100644 index 0000000000..97f1ce708c --- /dev/null +++ b/tools/tests/test_linkernsbypass.py @@ -0,0 +1,71 @@ +import os +from pathlib import Path +import shutil +import subprocess +import tempfile +import unittest + +REPO = Path(__file__).resolve().parents[2] +LIB = REPO / 'app/src/main/cpp/adrenotools/lib/linkernsbypass' + +STUBS = { + 'android/api-level.h': 'static inline int android_get_device_api_level(void) { return 37; }\n', + 'android/log.h': 'int __android_log_print(int, const char *, const char *, ...);\n', + 'android/dlext.h': ( + '#pragma once\n' + '#include \n' + '#include \n' + '#include \n' + 'struct android_namespace_t;\n' + 'enum { ANDROID_DLEXT_USE_LIBRARY_FD = 0x10, ANDROID_DLEXT_USE_NAMESPACE = 0x200 };\n' + 'typedef struct {\n' + ' uint64_t flags; void *reserved_addr; size_t reserved_size; int relro_fd;\n' + ' int library_fd; off64_t library_fd_offset; struct android_namespace_t *library_namespace;\n' + '} android_dlextinfo;\n' + 'extern "C" void *android_dlopen_ext(const char *, int, const android_dlextinfo *);\n' + ), +} + + +class LinkerNsBypassTest(unittest.TestCase): + @classmethod + def setUpClass(cls): + compiler = shutil.which(os.environ.get('CXX', 'c++')) + if compiler is None: + raise unittest.SkipTest('A host C++ compiler is required') + cls.temp = tempfile.TemporaryDirectory(prefix='droiddeck-linkernsbypass-') + cls.addClassCleanup(cls.temp.cleanup) + work = Path(cls.temp.name) + for name, text in STUBS.items(): + (work / name).parent.mkdir(parents=True, exist_ok=True) + (work / name).write_text(text) + cls.binary = work / 'linkernsbypass-test' + subprocess.run([ + compiler, '-std=c++17', '-O1', '-Wall', '-Wextra', '-Wno-missing-field-initializers', + '-include', 'limits.h', '-include', 'sys/syscall.h', + '-I', str(work), '-I', str(LIB), + str(REPO / 'tools/tests/linkernsbypass_harness.cpp'), str(LIB / 'android_linker_ns.cpp'), + '-Wl,--wrap=dlopen,--wrap=dlsym,--wrap=mprotect', + '-o', str(cls.binary), + ], check=True, capture_output=True, text=True, timeout=120) + + def run_mode(self, mode): + result = subprocess.run([str(self.binary), mode], capture_output=True, text=True, timeout=10) + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) + self.assertIn(f'PASS {mode}', result.stdout) + + def test_loading_the_library_leaves_linker_internals_alone_until_first_use(self): + self.run_mode('lazy') + + def test_dlopen_without_a_nearby_branch_is_not_followed(self): + self.run_mode('far') + + def test_target_page_that_cannot_be_unguarded_is_never_called(self): + self.run_mode('locked') + + def test_devices_refusing_write_and_exec_together_still_resolve(self): + self.run_mode('no_write_exec') + + +if __name__ == '__main__': + unittest.main()