From 933047e130c0ec84425ca356cc357a70038f991d Mon Sep 17 00:00:00 2001 From: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> Date: Sun, 8 Nov 2026 15:10:43 -0500 Subject: [PATCH] fix(native): address review findings in the hook and the ctl fallback --- app/src/main/AndroidManifest.xml | 8 ++ .../launcher/agent/AgentBridgeProvider.kt | 1 - .../launcher/agent/AgentCallReceiver.kt | 8 -- tools/nativeguest/ddnatv-hook.c | 2 +- tools/nativeguest/gosys/gosys.c | 2 +- tools/nativeguest/hook/compression.c | 114 ------------------ tools/nativeguest/hook/exec.c | 6 +- tools/nativeguest/hook/fileops.c | 7 +- tools/nativeguest/hook/identity.c | 9 +- tools/nativeguest/hook/net.c | 89 -------------- tools/nativeguest/hook/sysv_shm.c | 1 + 11 files changed, 28 insertions(+), 219 deletions(-) delete mode 100644 tools/nativeguest/hook/compression.c diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 5f010b48d3..968aed7d04 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -352,6 +352,14 @@ + + + + + #include #include +#include #ifndef RENAME_EXCHANGE #define RENAME_EXCHANGE (1 << 1) @@ -71,7 +72,6 @@ #include "hook/fileops.c" #include "hook/identity.c" #include "hook/privileged.c" -#include "hook/compression.c" #include "hook/env.c" #include "hook/exec_probe.c" #include "hook/static_programs.c" diff --git a/tools/nativeguest/gosys/gosys.c b/tools/nativeguest/gosys/gosys.c index 15f20f8190..fb8230ce4a 100644 --- a/tools/nativeguest/gosys/gosys.c +++ b/tools/nativeguest/gosys/gosys.c @@ -386,7 +386,7 @@ static i64 with_execve(const struct gosys_hdr *h, u32 *held, i64 a0, i64 a1, i64 int has_root = 0; while (envp && envp[envc]) { const char *e = envp[envc]; - if (e[0] == 'C' && e[1] == 'O' && e[2] == 'R' && e[3] == 'T' && e[4] == 'E' && e[5] == 'X' && + if (e[0] == 'D' && e[1] == 'D' && e[2] == 'N' && e[3] == 'A' && e[4] == 'T' && e[5] == 'V' && e[6] == '_' && e[7] == 'R' && e[8] == 'O' && e[9] == 'O' && e[10] == 'T' && e[11] == '=') has_root = 1; envc++; diff --git a/tools/nativeguest/hook/compression.c b/tools/nativeguest/hook/compression.c deleted file mode 100644 index 51bfea6fd9..0000000000 --- a/tools/nativeguest/hook/compression.c +++ /dev/null @@ -1,114 +0,0 @@ -// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the -// parts share their static functions and are built with the same command as before). -// -// xz and zstd limited to one thread (threads of forked dpkg-deb children fail on Android). - -#ifndef DDNATV_HOST_TEST - -// Hook lzma multi-threaded routines to enforce single-threaded execution -// This prevents clone/pthread_create failures in forked dpkg-deb child processes on Android -typedef struct { - const uint8_t *next_in; - size_t avail_in; - uint64_t total_in; - uint8_t *next_out; - size_t avail_out; - uint64_t total_out; - const void *allocator; - void *internal; - void *reserved_ptr1; - void *reserved_ptr2; - void *reserved_ptr3; - void *reserved_ptr4; - uint64_t reserved_int1; - uint64_t reserved_int2; - size_t reserved_int3; - size_t reserved_int4; - uint32_t reserved_enum1; - uint32_t reserved_enum2; -} ddnatv_lzma_stream; - -typedef struct { - uint32_t flags; - uint32_t threads; - uint64_t block_size; - uint32_t timeout; - uint32_t preset; - const void *filters; - int check; - uint64_t memlimit_threading; - uint64_t memlimit_stop; -} ddnatv_lzma_mt; - -int lzma_stream_decoder_mt(ddnatv_lzma_stream *strm, const ddnatv_lzma_mt *options) { - static int (*orig_decoder)(ddnatv_lzma_stream *, uint64_t, uint32_t) = NULL; - if (!orig_decoder) { - orig_decoder = (int (*)(ddnatv_lzma_stream *, uint64_t, uint32_t))dlsym(RTLD_DEFAULT, "lzma_stream_decoder"); - if (!orig_decoder) { - orig_decoder = (int (*)(ddnatv_lzma_stream *, uint64_t, uint32_t))dlsym(RTLD_NEXT, "lzma_stream_decoder"); - } - } - uint64_t memlimit = (options && options->memlimit_threading > 0) ? options->memlimit_threading : UINT64_MAX; - uint32_t flags = options ? options->flags : 0; - if (orig_decoder) { - return orig_decoder(strm, memlimit, flags); - } - static int (*orig_mt)(ddnatv_lzma_stream *, const ddnatv_lzma_mt *) = NULL; - if (!orig_mt) { - orig_mt = (int (*)(ddnatv_lzma_stream *, const ddnatv_lzma_mt *))dlsym(RTLD_NEXT, "lzma_stream_decoder_mt"); - } - return orig_mt ? orig_mt(strm, options) : -1; -} - -int lzma_stream_encoder_mt(ddnatv_lzma_stream *strm, const ddnatv_lzma_mt *options) { - // The native userland encodes on one thread. The single-thread encoders take the same settings: xz passes its - // compression level as a filter chain (its preset field is unused), other programs a preset. - // Honouring only the preset made `xz -T4 -9` (and -T0) compress like `xz -0`. - if (options && options->filters) { - static int (*orig_stream)(ddnatv_lzma_stream *, const void *, int) = NULL; - if (!orig_stream) { - orig_stream = (int (*)(ddnatv_lzma_stream *, const void *, int))dlsym(RTLD_DEFAULT, "lzma_stream_encoder"); - if (!orig_stream) { - orig_stream = (int (*)(ddnatv_lzma_stream *, const void *, int))dlsym(RTLD_NEXT, "lzma_stream_encoder"); - } - } - if (orig_stream) return orig_stream(strm, options->filters, options->check); - } - static int (*orig_easy)(ddnatv_lzma_stream *, uint32_t, int) = NULL; - if (!orig_easy) { - orig_easy = (int (*)(ddnatv_lzma_stream *, uint32_t, int))dlsym(RTLD_DEFAULT, "lzma_easy_encoder"); - if (!orig_easy) { - orig_easy = (int (*)(ddnatv_lzma_stream *, uint32_t, int))dlsym(RTLD_NEXT, "lzma_easy_encoder"); - } - } - if (orig_easy) { - uint32_t preset = options ? options->preset : 6; - int check = options ? options->check : 4; // LZMA_CHECK_CRC64 - return orig_easy(strm, preset, check); - } - static int (*orig_mt)(ddnatv_lzma_stream *, const ddnatv_lzma_mt *) = NULL; - if (!orig_mt) { - orig_mt = (int (*)(ddnatv_lzma_stream *, const ddnatv_lzma_mt *))dlsym(RTLD_NEXT, "lzma_stream_encoder_mt"); - } - return orig_mt ? orig_mt(strm, options) : -1; -} - -uint32_t lzma_cputhreads(void) { - return 1; -} - -int ZSTD_CCtx_setParameter(void *cctx, int param, int value) { - static int (*orig_param)(void *, int, int) = NULL; - if (!orig_param) { - orig_param = (int (*)(void *, int, int))dlsym(RTLD_DEFAULT, "ZSTD_CCtx_setParameter"); - if (!orig_param) { - orig_param = (int (*)(void *, int, int))dlsym(RTLD_NEXT, "ZSTD_CCtx_setParameter"); - } - } - if (param == 400 /* ZSTD_c_nbWorkers */ && value > 1) { - value = 1; - } - return orig_param ? orig_param(cctx, param, value) : -1; -} - -#endif /* !DDNATV_HOST_TEST */ diff --git a/tools/nativeguest/hook/exec.c b/tools/nativeguest/hook/exec.c index 95f5cc4a35..d973ac134b 100644 --- a/tools/nativeguest/hook/exec.c +++ b/tools/nativeguest/hook/exec.c @@ -841,7 +841,9 @@ static int ddnatv_posix_spawn(pid_t *pid, const char *path, char ld_lib_path[PATH_MAX * 6]; build_ld_library_path(orig_ep, probe.runpath, probe.runpath_is_rpath, elf_target, ld_lib_path, sizeof(ld_lib_path)); char **new_argv = build_ldso_argv(ld_so, hook_so, ld_lib_path, elf_target, prog_name, argv); - if (new_argv) { + if (!new_argv) { + ret = ENOMEM; + } else { if (orig_posix_spawn) { ret = orig_posix_spawn(pid, ld_so, file_actions, attrp, new_argv, new_envp); if (ret == EACCES && chmod(ld_so, 0755) == 0) { @@ -858,7 +860,7 @@ static int ddnatv_posix_spawn(pid_t *pid, const char *path, } } - if (ret != 0) { + if (!orig_posix_spawn) { pid_t child = fork(); if (child < 0) { free_modified_env(new_envp, orig_ep); diff --git a/tools/nativeguest/hook/fileops.c b/tools/nativeguest/hook/fileops.c index 9e94d460d8..fdad4e0248 100644 --- a/tools/nativeguest/hook/fileops.c +++ b/tools/nativeguest/hook/fileops.c @@ -209,10 +209,13 @@ int inotify_add_watch(int fd, const char *pathname, uint32_t mask) { if (!pathname) return -1; char buf[PATH_MAX]; const char *target = rewrite_path(pathname, buf, sizeof(buf)); - if (target && access(target, F_OK) != 0) { + if (!orig_inotify_add_watch) return -1; + int wd = orig_inotify_add_watch(fd, target, mask); + if (wd < 0 && errno == ENOENT && (mask & IN_ONLYDIR)) { ensure_dir_exists(target); + wd = orig_inotify_add_watch(fd, target, mask); } - return orig_inotify_add_watch ? orig_inotify_add_watch(fd, target, mask) : -1; + return wd; } // Hook rename diff --git a/tools/nativeguest/hook/identity.c b/tools/nativeguest/hook/identity.c index 21064a9cab..83a5d5b40e 100644 --- a/tools/nativeguest/hook/identity.c +++ b/tools/nativeguest/hook/identity.c @@ -139,7 +139,14 @@ int DDNATV_SHADOW_SYM(lckpwdf)(void) { memset(&fl, 0, sizeof(fl)); fl.l_type = F_WRLCK; fl.l_whence = SEEK_SET; - fcntl(g_pwd_lock_fd, F_SETLK, &fl); + if (fcntl(g_pwd_lock_fd, F_SETLK, &fl) != 0) { + int saved = errno; + close(g_pwd_lock_fd); + g_pwd_lock_fd = -1; + pthread_mutex_unlock(&g_pwd_lock_mutex); + errno = saved; + return -1; + } } } if (g_pwd_lock_fd < 0) { diff --git a/tools/nativeguest/hook/net.c b/tools/nativeguest/hook/net.c index 478b7328e3..25bc9f6609 100644 --- a/tools/nativeguest/hook/net.c +++ b/tools/nativeguest/hook/net.c @@ -695,95 +695,6 @@ static int synthesize_fallback_addrinfo(const char *node, const char *service, return 0; } - // The built-in addresses below are IPv4 ones. - if (family == AF_INET6) return EAI_NONAME; - - if (domain_matches(node, "ubuntu.com")) { - struct addrinfo *ai1 = alloc_one_addrinfo(node, "91.189.91.103", port, socktype, protocol); - if (!ai1) return EAI_MEMORY; - struct addrinfo *ai2 = alloc_one_addrinfo(node, "91.189.92.21", port, socktype, protocol); - if (ai2) { - ai1->ai_next = ai2; - } - *res = ai1; - return 0; - } - - if (domain_matches(node, "debian.org")) { - struct addrinfo *ai1 = alloc_one_addrinfo(node, "151.101.130.132", port, socktype, protocol); - if (!ai1) return EAI_MEMORY; - struct addrinfo *ai2 = alloc_one_addrinfo(node, "151.101.2.132", port, socktype, protocol); - if (ai2) { - ai1->ai_next = ai2; - } - *res = ai1; - return 0; - } - - if (domain_matches(node, "opencode.ai")) { - struct addrinfo *ai1 = alloc_one_addrinfo(node, "172.65.90.22", port, socktype, protocol); - if (!ai1) return EAI_MEMORY; - struct addrinfo *ai2 = alloc_one_addrinfo(node, "172.65.90.23", port, socktype, protocol); - if (ai2) { - ai1->ai_next = ai2; - } - *res = ai1; - return 0; - } - - if (domain_matches(node, "github.com")) { - struct addrinfo *ai1 = alloc_one_addrinfo(node, "140.82.121.6", port, socktype, protocol); - if (!ai1) return EAI_MEMORY; - struct addrinfo *ai2 = alloc_one_addrinfo(node, "140.82.121.4", port, socktype, protocol); - if (ai2) { - ai1->ai_next = ai2; - } - *res = ai1; - return 0; - } - - if (domain_matches(node, "githubusercontent.com")) { - struct addrinfo *ai1 = alloc_one_addrinfo(node, "185.199.110.133", port, socktype, protocol); - if (!ai1) return EAI_MEMORY; - struct addrinfo *ai2 = alloc_one_addrinfo(node, "185.199.108.133", port, socktype, protocol); - if (ai2) { - ai1->ai_next = ai2; - } - *res = ai1; - return 0; - } - - if (domain_matches(node, "npmjs.org") || domain_matches(node, "npmjs.com")) { - struct addrinfo *ai1 = alloc_one_addrinfo(node, "104.16.2.34", port, socktype, protocol); - if (!ai1) return EAI_MEMORY; - struct addrinfo *ai2 = alloc_one_addrinfo(node, "104.16.3.34", port, socktype, protocol); - if (ai2) { - ai1->ai_next = ai2; - } - *res = ai1; - return 0; - } - - if (domain_matches(node, "googleapis.com") || domain_matches(node, "accounts.google.com")) { - struct addrinfo *ai1 = alloc_one_addrinfo(node, "142.251.127.95", port, socktype, protocol); - if (!ai1) return EAI_MEMORY; - struct addrinfo *ai2 = alloc_one_addrinfo(node, "142.251.127.84", port, socktype, protocol); - if (ai2) { - ai1->ai_next = ai2; - } - *res = ai1; - return 0; - } - - if (domain_matches(node, "meta.ai") || domain_matches(node, "meta.com") || domain_matches(node, "facebook.com")) { - const char *ip = domain_matches(node, "lookaside.facebook.com") ? "57.144.36.128" : "57.144.36.141"; - struct addrinfo *ai = alloc_one_addrinfo(node, ip, port, socktype, protocol); - if (ai) { - *res = ai; - return 0; - } - } - return EAI_NONAME; } diff --git a/tools/nativeguest/hook/sysv_shm.c b/tools/nativeguest/hook/sysv_shm.c index 811f76a90c..1fad51e93e 100644 --- a/tools/nativeguest/hook/sysv_shm.c +++ b/tools/nativeguest/hook/sysv_shm.c @@ -182,6 +182,7 @@ static int shm_create(long key, size_t size, unsigned mode) { if (page <= 0) page = 4096; if (size > (size_t)INTPTR_MAX - (size_t)page) { // no file (and no mapping) can be that large + if (seg_fd >= 0) syscall(SYS_close, seg_fd); syscall(SYS_close, meta_fd); shm_remove(id, IPC_PRIVATE); errno = EINVAL;