Compare commits

...
Author SHA1 Message Date
The412Banner 98c8ee318d 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.
2026-11-27 11:08:44 +00:00
4 changed files with 226 additions and 5 deletions

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{
+125
View File
@@ -0,0 +1,125 @@
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <sys/mman.h>
#include <unistd.h>
#include <vector>
#include <android/dlext.h>
#include <android_linker_ns.h>
extern "C" {
uint32_t __wrap_dlopen[64];
}
static std::vector<uintptr_t> protected_pages;
static std::vector<int> protected_flags;
static std::vector<std::string> 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<uintptr_t>(p) & ~static_cast<uintptr_t>(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<void *>(0x1000);
}
static void fake_linker_entry() {}
extern "C" int __wrap_mprotect(void *addr, size_t, int prot) {
protected_pages.push_back(reinterpret_cast<uintptr_t>(addr));
protected_flags.push_back(prot);
if (reinterpret_cast<uintptr_t>(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<void *>(&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<intptr_t>(&__wrap_dlopen[index])};
auto to{reinterpret_cast<intptr_t>(&fake_loader_dlopen)};
__wrap_dlopen[index] = 0x94000000u | (static_cast<uint32_t>((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<const void *>(&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<const void *>(&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;
}
+71
View File
@@ -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 <stddef.h>\n'
'#include <stdint.h>\n'
'#include <sys/types.h>\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()