From 12f1061a3c420ab92869be7c444c3a06c982d0e0 Mon Sep 17 00:00:00 2001 From: hihumanzone Date: Sun, 25 Oct 2026 17:18:10 +0530 Subject: [PATCH 1/3] fix(remote-control): resolve daemon spawning and Android seccomp SIGSYS crashes (fixes #51) - lib/agy_compat.c: add auxiliary compatibility library intercepting syscall() to bypass Android seccomp SIGSYS trap on syscall 788533211 (0x2f000fdb) - lib/agy_helper.c: attempt direct kernel execv() of agy.va39 when running natively on aarch64 to maintain correct /proc/self/exe resolution for Go daemonization - build.sh: compile libagy_compat.so with -nostdlib, set PT_INTERP and RUNPATH on agy.va39 with patchelf, and add libagy_compat.so to DT_NEEDED - install.sh: extract and install libagy_compat.so alongside binaries with full backup and rollback support - workflows: package and verify libagy_compat.so in release archive, add patchelf to build dependencies --- .github/workflows/auto-sync-release.yml | 2 +- .github/workflows/pr-check.yml | 5 +- build.sh | 27 +++++++++- install.sh | 18 ++++++- lib/agy_compat.c | 69 +++++++++++++++++++++++++ lib/agy_helper.c | 10 ++++ 6 files changed, 126 insertions(+), 5 deletions(-) create mode 100644 lib/agy_compat.c diff --git a/.github/workflows/auto-sync-release.yml b/.github/workflows/auto-sync-release.yml index bcda4598b..ecc9b2bb8 100644 --- a/.github/workflows/auto-sync-release.yml +++ b/.github/workflows/auto-sync-release.yml @@ -118,7 +118,7 @@ jobs: if: steps.check_version.outputs.build_needed == 'true' run: | echo "Packaging standalone release archive..." - tar -czf antigravity-termux-standalone.tar.gz -C bin agy agy.va39 + tar -czf antigravity-termux-standalone.tar.gz -C bin agy agy.va39 libagy_compat.so - name: Upload Standalone Archive to Run Artifacts if: steps.check_version.outputs.build_needed == 'true' diff --git a/.github/workflows/pr-check.yml b/.github/workflows/pr-check.yml index 4e4004e29..d96109bed 100644 --- a/.github/workflows/pr-check.yml +++ b/.github/workflows/pr-check.yml @@ -174,6 +174,7 @@ jobs: glibc glibc-runner resolv-conf + patchelf exclude-paths: | .git ./.git @@ -191,7 +192,7 @@ jobs: ./build.sh echo "Packaging standalone release archive..." - tar -czf antigravity-termux-standalone.tar.gz -C bin agy agy.va39 + tar -czf antigravity-termux-standalone.tar.gz -C bin agy agy.va39 libagy_compat.so if [[ ! -f antigravity-termux-standalone.tar.gz ]]; then echo "Error: Release archive antigravity-termux-standalone.tar.gz was not generated." >&2 @@ -203,6 +204,7 @@ jobs: tar -tzf antigravity-termux-standalone.tar.gz | grep -qE '^agy$' || (echo "Error: Missing agy in release archive!" && exit 1) tar -tzf antigravity-termux-standalone.tar.gz | grep -qE '^agy\.va39$' || (echo "Error: Missing agy.va39 in release archive!" && exit 1) + tar -tzf antigravity-termux-standalone.tar.gz | grep -qE '^libagy_compat\.so$' || (echo "Error: Missing libagy_compat.so in release archive!" && exit 1) echo "Archive validation passed successfully." @@ -219,6 +221,7 @@ jobs: glibc glibc-runner resolv-conf + patchelf exclude-paths: | .git ./.git diff --git a/build.sh b/build.sh index 4af4b3e6b..ebd0e9428 100755 --- a/build.sh +++ b/build.sh @@ -42,7 +42,7 @@ Environment: Required if that binary cannot run on this host. Requirements: - - curl, jq, tar, python3 + - curl, jq, tar, python3, patchelf - native aarch64 Termux clang, or \$ANDROID_NDK_HOME with an aarch64 Android clang EOF } @@ -66,7 +66,7 @@ trap cleanup EXIT check_prereqs() { info "Checking build prerequisites..." local missing=() - for cmd in curl jq tar python3; do + for cmd in curl jq tar python3 patchelf; do if ! command -v "$cmd" &>/dev/null; then missing+=("$cmd") fi @@ -239,6 +239,29 @@ fi chmod +x bin/agy ok "Native Termux bootstrapper compiled successfully." +# Compile auxiliary compatibility library if present. +if [[ -f "lib/agy_compat.c" ]]; then + info "Compiling auxiliary compatibility library..." + if ! "$local_cc" -O2 -fPIC -shared -nostdlib -o bin/libagy_compat.so lib/agy_compat.c; then + die "Compilation of lib/agy_compat.c failed." + fi + chmod 0755 bin/libagy_compat.so + ok "Auxiliary compatibility library compiled successfully." +fi + +# Patch ELF interpreter and RPATH on agy.va39. +termux_prefix="${PREFIX:-/data/data/com.termux/files/usr}" +info "Patching ELF interpreter and RPATH on bin/agy.va39..." +patchelf --set-interpreter "${termux_prefix}/glibc/lib/ld-linux-aarch64.so.1" \ + --set-rpath "\$ORIGIN:\$ORIGIN/../glibc/lib:${termux_prefix}/glibc/lib" \ + bin/agy.va39 + +if [[ -f "bin/libagy_compat.so" ]]; then + info "Adding libagy_compat.so DT_NEEDED dependency to bin/agy.va39..." + patchelf --add-needed "libagy_compat.so" bin/agy.va39 +fi +ok "ELF patching complete on bin/agy.va39." + if [[ -n "${TERMUX_VERSION:-}" ]]; then info "Validating built Termux binary with --help..." if ! bin/agy --help >/dev/null; then diff --git a/install.sh b/install.sh index 6c9e7906b..2a52f80ff 100755 --- a/install.sh +++ b/install.sh @@ -43,6 +43,9 @@ cleanup() { if [[ -n "${AGY_VA39_BAK:-}" && -f "$AGY_VA39_BAK" ]]; then mv -f "$AGY_VA39_BAK" "$INSTALL_BIN_DIR/agy.va39" || true fi + if [[ -n "${AGY_COMPAT_BAK:-}" && -f "$AGY_COMPAT_BAK" ]]; then + mv -f "$AGY_COMPAT_BAK" "$INSTALL_BIN_DIR/libagy_compat.so" || true + fi else if [[ -n "${AGY_BAK:-}" && -f "$AGY_BAK" ]]; then rm -f "$AGY_BAK" || true @@ -50,6 +53,9 @@ cleanup() { if [[ -n "${AGY_VA39_BAK:-}" && -f "$AGY_VA39_BAK" ]]; then rm -f "$AGY_VA39_BAK" || true fi + if [[ -n "${AGY_COMPAT_BAK:-}" && -f "$AGY_COMPAT_BAK" ]]; then + rm -f "$AGY_COMPAT_BAK" || true + fi fi } @@ -290,11 +296,12 @@ mkdir -p "$EXTRACT_DIR" download_with_progress "$URL" "$TMP" || die # ── Extraction ──────────────────────────────────────────────────────────────── -tar -xz -C "$EXTRACT_DIR" -f "$TMP" agy agy.va39 >/dev/null 2>&1 & +tar -xz -C "$EXTRACT_DIR" -f "$TMP" >/dev/null 2>&1 & spinner $! "Extracting binaries..." || die AGY_BAK="" AGY_VA39_BAK="" +AGY_COMPAT_BAK="" if [[ -f "$INSTALL_BIN_DIR/agy" ]]; then AGY_BAK="$INSTALL_BIN_DIR/agy.bak.$$" mv -f "$INSTALL_BIN_DIR/agy" "$AGY_BAK" || die "Failed to back up existing agy binary from $INSTALL_BIN_DIR" @@ -303,9 +310,18 @@ if [[ -f "$INSTALL_BIN_DIR/agy.va39" ]]; then AGY_VA39_BAK="$INSTALL_BIN_DIR/agy.va39.bak.$$" mv -f "$INSTALL_BIN_DIR/agy.va39" "$AGY_VA39_BAK" || die "Failed to back up existing agy.va39 binary from $INSTALL_BIN_DIR" fi +if [[ -f "$INSTALL_BIN_DIR/libagy_compat.so" ]]; then + AGY_COMPAT_BAK="$INSTALL_BIN_DIR/libagy_compat.so.bak.$$" + mv -f "$INSTALL_BIN_DIR/libagy_compat.so" "$AGY_COMPAT_BAK" || true +fi install -m 0755 "$EXTRACT_DIR/agy" "$INSTALL_BIN_DIR/agy" || die "Failed to install agy binary to $INSTALL_BIN_DIR" install -m 0755 "$EXTRACT_DIR/agy.va39" "$INSTALL_BIN_DIR/agy.va39" || die "Failed to install agy.va39 binary to $INSTALL_BIN_DIR" +if [[ -f "$EXTRACT_DIR/libagy_compat.so" ]]; then + install -m 0755 "$EXTRACT_DIR/libagy_compat.so" "$INSTALL_BIN_DIR/libagy_compat.so" || die "Failed to install libagy_compat.so to $INSTALL_BIN_DIR" + mkdir -p "${TERMUX_PREFIX}/glibc/lib" 2>/dev/null || true + cp -pf "$EXTRACT_DIR/libagy_compat.so" "${TERMUX_PREFIX}/glibc/lib/libagy_compat.so" 2>/dev/null || true +fi rm -rf "$EXTRACT_DIR" # ── Verify twin-binary ──────────────────────────────────────────────────────── diff --git a/lib/agy_compat.c b/lib/agy_compat.c new file mode 100644 index 000000000..82f791817 --- /dev/null +++ b/lib/agy_compat.c @@ -0,0 +1,69 @@ +#include +#include + +#ifndef ENOSYS +#define ENOSYS 38 +#endif + +#define AGY_SECCOMP_BLOCKED_SYSCALL_1 788533211L +#define AGY_SECCOMP_BLOCKED_SYSCALL_2 0x2f000fdbL +#define AGY_MAX_LINUX_ARM64_SYSCALL 500L +#define AGY_MAX_ERRNO_VALUE 4095UL + +extern int *__errno_location(void); + +static inline void set_errno(int err) { + int *loc = __errno_location(); + if (loc != NULL) { + *loc = err; + } +} + +long syscall(long number, ...) { + if (number == AGY_SECCOMP_BLOCKED_SYSCALL_1 || number == AGY_SECCOMP_BLOCKED_SYSCALL_2 || + number < 0 || number >= AGY_MAX_LINUX_ARM64_SYSCALL) { + set_errno(ENOSYS); + return -1; + } + + va_list args; + va_start(args, number); + long arg1 = va_arg(args, long); + long arg2 = va_arg(args, long); + long arg3 = va_arg(args, long); + long arg4 = va_arg(args, long); + long arg5 = va_arg(args, long); + long arg6 = va_arg(args, long); + va_end(args); + +#if defined(__aarch64__) && !defined(__CPPCHECK__) + register long reg_x8 __asm__("x8") = number; + register long reg_x0 __asm__("x0") = arg1; + register long reg_x1 __asm__("x1") = arg2; + register long reg_x2 __asm__("x2") = arg3; + register long reg_x3 __asm__("x3") = arg4; + register long reg_x4 __asm__("x4") = arg5; + register long reg_x5 __asm__("x5") = arg6; + + __asm__ volatile("svc #0" + : "=r"(reg_x0) + : "r"(reg_x8), "r"(reg_x0), "r"(reg_x1), "r"(reg_x2), "r"(reg_x3), + "r"(reg_x4), "r"(reg_x5) + : "memory"); + + if ((unsigned long)reg_x0 >= (unsigned long)-AGY_MAX_ERRNO_VALUE) { + set_errno(-(int)reg_x0); + return -1; + } + return reg_x0; +#else + (void)arg1; + (void)arg2; + (void)arg3; + (void)arg4; + (void)arg5; + (void)arg6; + set_errno(ENOSYS); + return -1; +#endif +} diff --git a/lib/agy_helper.c b/lib/agy_helper.c index 99dcb050a..ff9074261 100644 --- a/lib/agy_helper.c +++ b/lib/agy_helper.c @@ -640,6 +640,14 @@ static int resolve_qemu_for_cpu(const char *prefix, char *qemu_path, size_t qemu return 0; } +static void try_direct_exec(int has_qemu, const char *patched_bin, char **argv) { + if (!has_qemu) { + argv[0] = (char *)patched_bin; + // NOLINTNEXTLINE(clang-analyzer-optin.taint.GenericTaint) + (void)execv(patched_bin, argv); + } +} + int main(int argc, char **argv) { char exec_path[PATH_MAX]; char lib_path[PATH_MAX + 16]; @@ -734,6 +742,8 @@ int main(int argc, char **argv) { return 1; } + try_direct_exec(qemu != NULL, patched_bin, argv); + // We allocate enough space for: qemu + loader + "--library-path" + lib_path // + patched_bin + user args + NULL int new_argc = argc + 6; From 1f5c63129f8ff48f7805756c61cfcf444e9c31be Mon Sep 17 00:00:00 2001 From: hihumanzone Date: Tue, 27 Oct 2026 01:55:24 +0530 Subject: [PATCH 2/3] fix(compat): intercept Go faccessat2 seccomp SIGSYS via watchdog trap handler --- lib/agy_compat.c | 209 +++++++++++++++++++++++++++++++---------------- 1 file changed, 140 insertions(+), 69 deletions(-) diff --git a/lib/agy_compat.c b/lib/agy_compat.c index 82f791817..8134b160a 100644 --- a/lib/agy_compat.c +++ b/lib/agy_compat.c @@ -1,69 +1,140 @@ -#include -#include - -#ifndef ENOSYS -#define ENOSYS 38 -#endif - -#define AGY_SECCOMP_BLOCKED_SYSCALL_1 788533211L -#define AGY_SECCOMP_BLOCKED_SYSCALL_2 0x2f000fdbL -#define AGY_MAX_LINUX_ARM64_SYSCALL 500L -#define AGY_MAX_ERRNO_VALUE 4095UL - -extern int *__errno_location(void); - -static inline void set_errno(int err) { - int *loc = __errno_location(); - if (loc != NULL) { - *loc = err; - } -} - -long syscall(long number, ...) { - if (number == AGY_SECCOMP_BLOCKED_SYSCALL_1 || number == AGY_SECCOMP_BLOCKED_SYSCALL_2 || - number < 0 || number >= AGY_MAX_LINUX_ARM64_SYSCALL) { - set_errno(ENOSYS); - return -1; - } - - va_list args; - va_start(args, number); - long arg1 = va_arg(args, long); - long arg2 = va_arg(args, long); - long arg3 = va_arg(args, long); - long arg4 = va_arg(args, long); - long arg5 = va_arg(args, long); - long arg6 = va_arg(args, long); - va_end(args); - -#if defined(__aarch64__) && !defined(__CPPCHECK__) - register long reg_x8 __asm__("x8") = number; - register long reg_x0 __asm__("x0") = arg1; - register long reg_x1 __asm__("x1") = arg2; - register long reg_x2 __asm__("x2") = arg3; - register long reg_x3 __asm__("x3") = arg4; - register long reg_x4 __asm__("x4") = arg5; - register long reg_x5 __asm__("x5") = arg6; - - __asm__ volatile("svc #0" - : "=r"(reg_x0) - : "r"(reg_x8), "r"(reg_x0), "r"(reg_x1), "r"(reg_x2), "r"(reg_x3), - "r"(reg_x4), "r"(reg_x5) - : "memory"); - - if ((unsigned long)reg_x0 >= (unsigned long)-AGY_MAX_ERRNO_VALUE) { - set_errno(-(int)reg_x0); - return -1; - } - return reg_x0; -#else - (void)arg1; - (void)arg2; - (void)arg3; - (void)arg4; - (void)arg5; - (void)arg6; - set_errno(ENOSYS); - return -1; -#endif -} +// NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include + +#ifndef ENOSYS +#define ENOSYS 38 +#endif + +#define AGY_SECCOMP_BLOCKED_SYSCALL_1 788533211L +#define AGY_SECCOMP_BLOCKED_SYSCALL_2 0x2f000fdbL +#define AGY_SYS_FACCESSAT2 439L +#define AGY_MAX_LINUX_ARM64_SYSCALL 500L +#define AGY_MAX_ERRNO_VALUE 4095UL + +enum AgyWatchdogConfig { + AGY_WATCHDOG_STACK_SIZE = 65536U, + AGY_WATCHDOG_INITIAL_DELAY_US = 100000U, + AGY_WATCHDOG_CHECK_INTERVAL_SEC = 2U +}; + +// NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) +extern int *__errno_location(void); + +static inline void set_errno(int err) { + int *loc = __errno_location(); + if (loc != NULL) { + *loc = err; + } +} + +static void agy_sigsys_handler(int sig, siginfo_t *si, void *ucontext_ptr) { + if (si != NULL && si->si_code == 1 /* SYS_SECCOMP */) { +#ifdef __aarch64__ + ucontext_t *ctx = (ucontext_t *)ucontext_ptr; + if (ctx != NULL) { + ctx->uc_mcontext.regs[0] = (unsigned long)(-ENOSYS); + return; + } +#endif + } + _exit(128 + sig); +} + +static void install_sigsys_handler(void) { + struct sigaction sa; + sigemptyset(&sa.sa_mask); + sa.sa_sigaction = agy_sigsys_handler; + sa.sa_flags = SA_SIGINFO | SA_NODEFER | SA_ONSTACK; + sigaction(SIGSYS, &sa, NULL); +} + +static void *sigsys_watchdog_thread(void *arg) { + (void)arg; + usleep(AGY_WATCHDOG_INITIAL_DELAY_US); + install_sigsys_handler(); + + while (1) { + sleep(AGY_WATCHDOG_CHECK_INTERVAL_SEC); + struct sigaction current_sa; + sigemptyset(¤t_sa.sa_mask); + if (sigaction(SIGSYS, NULL, ¤t_sa) == 0) { + if (current_sa.sa_sigaction != agy_sigsys_handler) { + install_sigsys_handler(); + } + } + } + return NULL; +} + +__attribute__((constructor)) static void agy_compat_init(void) { + install_sigsys_handler(); + + pthread_t th = 0; + pthread_attr_t attr; + if (pthread_attr_init(&attr) == 0) { + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + pthread_attr_setstacksize(&attr, (size_t)AGY_WATCHDOG_STACK_SIZE); + pthread_create(&th, &attr, sigsys_watchdog_thread, NULL); + pthread_attr_destroy(&attr); + } +} + +long syscall(long number, ...) { + if (number == AGY_SECCOMP_BLOCKED_SYSCALL_1 || + number == AGY_SECCOMP_BLOCKED_SYSCALL_2 || number == AGY_SYS_FACCESSAT2 || + number < 0 || number >= AGY_MAX_LINUX_ARM64_SYSCALL) { + set_errno(ENOSYS); + return -1; + } + + va_list args; + va_start(args, number); + long a1 = va_arg(args, long); + long a2 = va_arg(args, long); + long a3 = va_arg(args, long); + long a4 = va_arg(args, long); + long a5 = va_arg(args, long); + long a6 = va_arg(args, long); + va_end(args); + +#ifdef __aarch64__ + long ret = a1; + long num = number; + __asm__ volatile( + "mov x8, %[num]\n" + "mov x0, %[arg1]\n" + "mov x1, %[arg2]\n" + "mov x2, %[arg3]\n" + "mov x3, %[arg4]\n" + "mov x4, %[arg5]\n" + "mov x5, %[arg6]\n" + "svc #0\n" + "mov %[ret], x0\n" + : [ret] "=r"(ret) + : [num] "r"(num), [arg1] "r"(a1), [arg2] "r"(a2), [arg3] "r"(a3), + [arg4] "r"(a4), [arg5] "r"(a5), [arg6] "r"(a6) + : "x0", "x1", "x2", "x3", "x4", "x5", "x8", "memory"); + + if ((unsigned long)ret >= (unsigned long)-AGY_MAX_ERRNO_VALUE) { + set_errno(-(int)ret); + return -1; + } + return ret; +#else + (void)a1; + (void)a2; + (void)a3; + (void)a4; + (void)a5; + (void)a6; + set_errno(ENOSYS); + return -1; +#endif +} From d126f2f6c7a0e3dd9444e50f37703e4cbaad3d30 Mon Sep 17 00:00:00 2001 From: hihumanzone Date: Wed, 25 Nov 2026 03:36:14 +0530 Subject: [PATCH 3/3] fix(pr-52): address review feedback with 4-byte gvisor patch and prefix handling --- .github/workflows/auto-sync-release.yml | 8 +- .github/workflows/pr-check.yml | 3 +- build.sh | 34 +++--- install.sh | 29 ++--- lib/agy_compat.c | 140 ------------------------ lib/agy_helper.c | 15 ++- 6 files changed, 47 insertions(+), 182 deletions(-) delete mode 100644 lib/agy_compat.c diff --git a/.github/workflows/auto-sync-release.yml b/.github/workflows/auto-sync-release.yml index ecc9b2bb8..0344babd6 100644 --- a/.github/workflows/auto-sync-release.yml +++ b/.github/workflows/auto-sync-release.yml @@ -107,6 +107,12 @@ jobs: run: | ./.github/scripts/check-version.sh + - name: Install Build Dependencies + if: steps.check_version.outputs.build_needed == 'true' + run: | + sudo apt-get update + sudo apt-get install -y patchelf + - name: Build Standalone Binaries if: steps.check_version.outputs.build_needed == 'true' env: @@ -118,7 +124,7 @@ jobs: if: steps.check_version.outputs.build_needed == 'true' run: | echo "Packaging standalone release archive..." - tar -czf antigravity-termux-standalone.tar.gz -C bin agy agy.va39 libagy_compat.so + tar -czf antigravity-termux-standalone.tar.gz -C bin agy agy.va39 - name: Upload Standalone Archive to Run Artifacts if: steps.check_version.outputs.build_needed == 'true' diff --git a/.github/workflows/pr-check.yml b/.github/workflows/pr-check.yml index d96109bed..9f7682d1c 100644 --- a/.github/workflows/pr-check.yml +++ b/.github/workflows/pr-check.yml @@ -192,7 +192,7 @@ jobs: ./build.sh echo "Packaging standalone release archive..." - tar -czf antigravity-termux-standalone.tar.gz -C bin agy agy.va39 libagy_compat.so + tar -czf antigravity-termux-standalone.tar.gz -C bin agy agy.va39 if [[ ! -f antigravity-termux-standalone.tar.gz ]]; then echo "Error: Release archive antigravity-termux-standalone.tar.gz was not generated." >&2 @@ -204,7 +204,6 @@ jobs: tar -tzf antigravity-termux-standalone.tar.gz | grep -qE '^agy$' || (echo "Error: Missing agy in release archive!" && exit 1) tar -tzf antigravity-termux-standalone.tar.gz | grep -qE '^agy\.va39$' || (echo "Error: Missing agy.va39 in release archive!" && exit 1) - tar -tzf antigravity-termux-standalone.tar.gz | grep -qE '^libagy_compat\.so$' || (echo "Error: Missing libagy_compat.so in release archive!" && exit 1) echo "Archive validation passed successfully." diff --git a/build.sh b/build.sh index ebd0e9428..6ab3467fa 100755 --- a/build.sh +++ b/build.sh @@ -221,12 +221,19 @@ word_rewrites = { for off in range(lo, hi, 4): w = get(off) if w in word_rewrites: put(off, word_rewrites[w]) +gvisor_probe_count = 0 for off in range(0, len(data) - 12, 4): if get(off) == 0xAA1F03E5 and get(off + 4) == 0xAA1F03E6 and get(off + 8) == 0xD28036E0 and (get(off + 12) & 0xFC000000) == 0x94000000: put(off + 8, 0xD2800600); faccessat2_count += 1 + elif get(off) == 0x5281FB60 and get(off + 4) == 0x72A5E000 and (get(off + 8) & 0xFC000000) == 0x94000000: + if (get(off + 12) & 0xFF00001F) == 0x34000000: + put(off + 8, 0x52800000) + else: + put(off + 8, 0x12800000) + gvisor_probe_count += 1 dst.write_bytes(data) dst.chmod(0o755) -print(f"Patched parameters: ubfx={ubfx_count}, lsl={lsl_count}, mask={mask_count}, mmap={mmap_count}, faccessat2={faccessat2_count}") +print(f"Patched parameters: ubfx={ubfx_count}, lsl={lsl_count}, mask={mask_count}, mmap={mmap_count}, faccessat2={faccessat2_count}, gvisor_probe={gvisor_probe_count}") PY ok "Patched binary generated: bin/agy.va39" @@ -239,27 +246,14 @@ fi chmod +x bin/agy ok "Native Termux bootstrapper compiled successfully." -# Compile auxiliary compatibility library if present. -if [[ -f "lib/agy_compat.c" ]]; then - info "Compiling auxiliary compatibility library..." - if ! "$local_cc" -O2 -fPIC -shared -nostdlib -o bin/libagy_compat.so lib/agy_compat.c; then - die "Compilation of lib/agy_compat.c failed." - fi - chmod 0755 bin/libagy_compat.so - ok "Auxiliary compatibility library compiled successfully." -fi - -# Patch ELF interpreter and RPATH on agy.va39. -termux_prefix="${PREFIX:-/data/data/com.termux/files/usr}" +# Patch ELF interpreter and RPATH on agy.va39 so direct kernel execution preserves +# /proc/self/exe for Go daemonization. Default to canonical Termux PREFIX so CI +# build hosts never bake host-specific paths into release archives. +target_prefix="${AGY_TARGET_PREFIX:-/data/data/com.termux/files/usr}" info "Patching ELF interpreter and RPATH on bin/agy.va39..." -patchelf --set-interpreter "${termux_prefix}/glibc/lib/ld-linux-aarch64.so.1" \ - --set-rpath "\$ORIGIN:\$ORIGIN/../glibc/lib:${termux_prefix}/glibc/lib" \ +patchelf --set-interpreter "${target_prefix}/glibc/lib/ld-linux-aarch64.so.1" \ + --set-rpath "\$ORIGIN:\$ORIGIN/../glibc/lib:${target_prefix}/glibc/lib" \ bin/agy.va39 - -if [[ -f "bin/libagy_compat.so" ]]; then - info "Adding libagy_compat.so DT_NEEDED dependency to bin/agy.va39..." - patchelf --add-needed "libagy_compat.so" bin/agy.va39 -fi ok "ELF patching complete on bin/agy.va39." if [[ -n "${TERMUX_VERSION:-}" ]]; then diff --git a/install.sh b/install.sh index 2a52f80ff..9bd6f7794 100755 --- a/install.sh +++ b/install.sh @@ -43,9 +43,6 @@ cleanup() { if [[ -n "${AGY_VA39_BAK:-}" && -f "$AGY_VA39_BAK" ]]; then mv -f "$AGY_VA39_BAK" "$INSTALL_BIN_DIR/agy.va39" || true fi - if [[ -n "${AGY_COMPAT_BAK:-}" && -f "$AGY_COMPAT_BAK" ]]; then - mv -f "$AGY_COMPAT_BAK" "$INSTALL_BIN_DIR/libagy_compat.so" || true - fi else if [[ -n "${AGY_BAK:-}" && -f "$AGY_BAK" ]]; then rm -f "$AGY_BAK" || true @@ -53,9 +50,6 @@ cleanup() { if [[ -n "${AGY_VA39_BAK:-}" && -f "$AGY_VA39_BAK" ]]; then rm -f "$AGY_VA39_BAK" || true fi - if [[ -n "${AGY_COMPAT_BAK:-}" && -f "$AGY_COMPAT_BAK" ]]; then - rm -f "$AGY_COMPAT_BAK" || true - fi fi } @@ -296,12 +290,11 @@ mkdir -p "$EXTRACT_DIR" download_with_progress "$URL" "$TMP" || die # ── Extraction ──────────────────────────────────────────────────────────────── -tar -xz -C "$EXTRACT_DIR" -f "$TMP" >/dev/null 2>&1 & +tar -xz -C "$EXTRACT_DIR" -f "$TMP" agy agy.va39 >/dev/null 2>&1 & spinner $! "Extracting binaries..." || die AGY_BAK="" AGY_VA39_BAK="" -AGY_COMPAT_BAK="" if [[ -f "$INSTALL_BIN_DIR/agy" ]]; then AGY_BAK="$INSTALL_BIN_DIR/agy.bak.$$" mv -f "$INSTALL_BIN_DIR/agy" "$AGY_BAK" || die "Failed to back up existing agy binary from $INSTALL_BIN_DIR" @@ -310,20 +303,22 @@ if [[ -f "$INSTALL_BIN_DIR/agy.va39" ]]; then AGY_VA39_BAK="$INSTALL_BIN_DIR/agy.va39.bak.$$" mv -f "$INSTALL_BIN_DIR/agy.va39" "$AGY_VA39_BAK" || die "Failed to back up existing agy.va39 binary from $INSTALL_BIN_DIR" fi -if [[ -f "$INSTALL_BIN_DIR/libagy_compat.so" ]]; then - AGY_COMPAT_BAK="$INSTALL_BIN_DIR/libagy_compat.so.bak.$$" - mv -f "$INSTALL_BIN_DIR/libagy_compat.so" "$AGY_COMPAT_BAK" || true -fi install -m 0755 "$EXTRACT_DIR/agy" "$INSTALL_BIN_DIR/agy" || die "Failed to install agy binary to $INSTALL_BIN_DIR" install -m 0755 "$EXTRACT_DIR/agy.va39" "$INSTALL_BIN_DIR/agy.va39" || die "Failed to install agy.va39 binary to $INSTALL_BIN_DIR" -if [[ -f "$EXTRACT_DIR/libagy_compat.so" ]]; then - install -m 0755 "$EXTRACT_DIR/libagy_compat.so" "$INSTALL_BIN_DIR/libagy_compat.so" || die "Failed to install libagy_compat.so to $INSTALL_BIN_DIR" - mkdir -p "${TERMUX_PREFIX}/glibc/lib" 2>/dev/null || true - cp -pf "$EXTRACT_DIR/libagy_compat.so" "${TERMUX_PREFIX}/glibc/lib/libagy_compat.so" 2>/dev/null || true -fi rm -rf "$EXTRACT_DIR" +DEFAULT_GLIBC_LOADER="/data/data/com.termux/files/usr/glibc/lib/ld-linux-aarch64.so.1" +if [[ "$GLIBC_LOADER" != "$DEFAULT_GLIBC_LOADER" ]]; then + if command -v patchelf >/dev/null 2>&1; then + patchelf --set-interpreter "$GLIBC_LOADER" \ + --set-rpath "\$ORIGIN:\$ORIGIN/../glibc/lib:${TERMUX_PREFIX}/glibc/lib" \ + "$INSTALL_BIN_DIR/agy.va39" || die "Failed to update ELF interpreter for custom PREFIX" + else + info "Non-default PREFIX detected without patchelf; direct execution will fall back to glibc loader." + fi +fi + # ── Verify twin-binary ──────────────────────────────────────────────────────── if [[ ! -f "$INSTALL_BIN_DIR/agy" || ! -f "$INSTALL_BIN_DIR/agy.va39" ]]; then rm -f "$INSTALL_BIN_DIR/agy" "$INSTALL_BIN_DIR/agy.va39" diff --git a/lib/agy_compat.c b/lib/agy_compat.c deleted file mode 100644 index 8134b160a..000000000 --- a/lib/agy_compat.c +++ /dev/null @@ -1,140 +0,0 @@ -// NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) -#define _GNU_SOURCE -#include -#include -#include -#include -#include -#include -#include - -#ifndef ENOSYS -#define ENOSYS 38 -#endif - -#define AGY_SECCOMP_BLOCKED_SYSCALL_1 788533211L -#define AGY_SECCOMP_BLOCKED_SYSCALL_2 0x2f000fdbL -#define AGY_SYS_FACCESSAT2 439L -#define AGY_MAX_LINUX_ARM64_SYSCALL 500L -#define AGY_MAX_ERRNO_VALUE 4095UL - -enum AgyWatchdogConfig { - AGY_WATCHDOG_STACK_SIZE = 65536U, - AGY_WATCHDOG_INITIAL_DELAY_US = 100000U, - AGY_WATCHDOG_CHECK_INTERVAL_SEC = 2U -}; - -// NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) -extern int *__errno_location(void); - -static inline void set_errno(int err) { - int *loc = __errno_location(); - if (loc != NULL) { - *loc = err; - } -} - -static void agy_sigsys_handler(int sig, siginfo_t *si, void *ucontext_ptr) { - if (si != NULL && si->si_code == 1 /* SYS_SECCOMP */) { -#ifdef __aarch64__ - ucontext_t *ctx = (ucontext_t *)ucontext_ptr; - if (ctx != NULL) { - ctx->uc_mcontext.regs[0] = (unsigned long)(-ENOSYS); - return; - } -#endif - } - _exit(128 + sig); -} - -static void install_sigsys_handler(void) { - struct sigaction sa; - sigemptyset(&sa.sa_mask); - sa.sa_sigaction = agy_sigsys_handler; - sa.sa_flags = SA_SIGINFO | SA_NODEFER | SA_ONSTACK; - sigaction(SIGSYS, &sa, NULL); -} - -static void *sigsys_watchdog_thread(void *arg) { - (void)arg; - usleep(AGY_WATCHDOG_INITIAL_DELAY_US); - install_sigsys_handler(); - - while (1) { - sleep(AGY_WATCHDOG_CHECK_INTERVAL_SEC); - struct sigaction current_sa; - sigemptyset(¤t_sa.sa_mask); - if (sigaction(SIGSYS, NULL, ¤t_sa) == 0) { - if (current_sa.sa_sigaction != agy_sigsys_handler) { - install_sigsys_handler(); - } - } - } - return NULL; -} - -__attribute__((constructor)) static void agy_compat_init(void) { - install_sigsys_handler(); - - pthread_t th = 0; - pthread_attr_t attr; - if (pthread_attr_init(&attr) == 0) { - pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); - pthread_attr_setstacksize(&attr, (size_t)AGY_WATCHDOG_STACK_SIZE); - pthread_create(&th, &attr, sigsys_watchdog_thread, NULL); - pthread_attr_destroy(&attr); - } -} - -long syscall(long number, ...) { - if (number == AGY_SECCOMP_BLOCKED_SYSCALL_1 || - number == AGY_SECCOMP_BLOCKED_SYSCALL_2 || number == AGY_SYS_FACCESSAT2 || - number < 0 || number >= AGY_MAX_LINUX_ARM64_SYSCALL) { - set_errno(ENOSYS); - return -1; - } - - va_list args; - va_start(args, number); - long a1 = va_arg(args, long); - long a2 = va_arg(args, long); - long a3 = va_arg(args, long); - long a4 = va_arg(args, long); - long a5 = va_arg(args, long); - long a6 = va_arg(args, long); - va_end(args); - -#ifdef __aarch64__ - long ret = a1; - long num = number; - __asm__ volatile( - "mov x8, %[num]\n" - "mov x0, %[arg1]\n" - "mov x1, %[arg2]\n" - "mov x2, %[arg3]\n" - "mov x3, %[arg4]\n" - "mov x4, %[arg5]\n" - "mov x5, %[arg6]\n" - "svc #0\n" - "mov %[ret], x0\n" - : [ret] "=r"(ret) - : [num] "r"(num), [arg1] "r"(a1), [arg2] "r"(a2), [arg3] "r"(a3), - [arg4] "r"(a4), [arg5] "r"(a5), [arg6] "r"(a6) - : "x0", "x1", "x2", "x3", "x4", "x5", "x8", "memory"); - - if ((unsigned long)ret >= (unsigned long)-AGY_MAX_ERRNO_VALUE) { - set_errno(-(int)ret); - return -1; - } - return ret; -#else - (void)a1; - (void)a2; - (void)a3; - (void)a4; - (void)a5; - (void)a6; - set_errno(ENOSYS); - return -1; -#endif -} diff --git a/lib/agy_helper.c b/lib/agy_helper.c index ff9074261..d081152c0 100644 --- a/lib/agy_helper.c +++ b/lib/agy_helper.c @@ -422,6 +422,15 @@ static int perform_transactional_update(const char *dir, const char *latest_tag) "agy agy.va39 && " "test -s \"$tmp/agy\" && test -x \"$tmp/agy\" && " "test -s \"$tmp/agy.va39\" && test -x \"$tmp/agy.va39\" && " + "termux_prefix=\"${PREFIX:-/data/data/com.termux/files/usr}\"; " + "loader_path=\"$termux_prefix/glibc/lib/ld-linux-aarch64.so.1\"; " + "if [ \"$loader_path\" != " + "\"/data/data/com.termux/files/usr/glibc/lib/ld-linux-aarch64.so.1\" ] " + "&& command -v patchelf >/dev/null 2>&1; then " + "patchelf --set-interpreter \"$loader_path\" " + "--set-rpath \"\\$ORIGIN:\\$ORIGIN/../glibc/lib:$termux_prefix/glibc/lib\" " + "\"$tmp/agy.va39\" || exit 1; " + "fi && " "\"$tmp/agy\" --help >/dev/null 2>&1 && " "install -m 0755 \"$tmp/agy\" \"$new_agy\" && " "install -m 0755 \"$tmp/agy.va39\" \"$new_payload\" && " @@ -640,11 +649,13 @@ static int resolve_qemu_for_cpu(const char *prefix, char *qemu_path, size_t qemu return 0; } -static void try_direct_exec(int has_qemu, const char *patched_bin, char **argv) { +static void try_direct_exec(int has_qemu, char *patched_bin, char **argv) { if (!has_qemu) { - argv[0] = (char *)patched_bin; + char *orig_argv0 = argv[0]; + argv[0] = patched_bin; // NOLINTNEXTLINE(clang-analyzer-optin.taint.GenericTaint) (void)execv(patched_bin, argv); + argv[0] = orig_argv0; } }