|
| 1 | +/* |
| 2 | + * statx-shim.c — SIGSYS handler for Android/Termux statx seccomp compatibility |
| 3 | + * |
| 4 | + * Problem: |
| 5 | + * Android's seccomp filter blocks the statx() syscall (aarch64 #291). |
| 6 | + * glibc's stat()/fstatat() internally use INLINE_SYSCALL_CALL(statx, ...) |
| 7 | + * which is a direct 'svc #0' instruction, NOT a call to the public statx() |
| 8 | + * symbol. When seccomp blocks it, the kernel sends SIGSYS. The default |
| 9 | + * SIGSYS handler terminates the process → crash. |
| 10 | + * |
| 11 | + * LD_PRELOAD symbol interception CANNOT fix this because there is no symbol |
| 12 | + * to intercept — it's a direct syscall instruction. |
| 13 | + * |
| 14 | + * Solution: |
| 15 | + * Install a SIGSYS handler that catches seccomp-blocked statx syscalls. |
| 16 | + * The handler sets the return register (x0) to -ENOSYS and advances the |
| 17 | + * program counter past the svc #0 instruction. glibc sees -ENOSYS, sets |
| 18 | + * errno=ENOSYS, caches the result, and falls back to fstatat/newfstatat. |
| 19 | + * |
| 20 | + * This is the same pattern used by: |
| 21 | + * - libuv (src/unix/fs.c): static _Atomic int no_statx; on Android |
| 22 | + * - OpenJDK 17/21/25: #if defined(__linux__) && !defined(__ANDROID__) |
| 23 | + * - util-linux: ac_cv_func_statx=no |
| 24 | + * |
| 25 | + * Compile (any C compiler — self-contained, no libc calls beyond signal API): |
| 26 | + * gcc -shared -fPIC -o libstatx-shim.so statx-shim.c |
| 27 | + * |
| 28 | + * Usage: |
| 29 | + * LD_PRELOAD=/path/to/libstatx-shim.so ./your-binary |
| 30 | + */ |
| 31 | + |
| 32 | +#define _GNU_SOURCE |
| 33 | +#include <errno.h> |
| 34 | +#include <signal.h> |
| 35 | +#include <string.h> |
| 36 | +#include <sys/syscall.h> |
| 37 | +#include <ucontext.h> |
| 38 | + |
| 39 | +/* aarch64 statx syscall number */ |
| 40 | +#ifndef __NR_statx |
| 41 | +#define __NR_statx 291 |
| 42 | +#endif |
| 43 | + |
| 44 | +/* ENOSYS — "Function not implemented" */ |
| 45 | +#ifndef ENOSYS |
| 46 | +#define ENOSYS 38 |
| 47 | +#endif |
| 48 | + |
| 49 | +/* |
| 50 | + * SIGSYS handler — intercept seccomp-blocked statx syscalls. |
| 51 | + * |
| 52 | + * When seccomp blocks a syscall, the kernel delivers SIGSYS with: |
| 53 | + * si_code = SYS_SECCOMP |
| 54 | + * si_sysno = the blocked syscall number |
| 55 | + * |
| 56 | + * We check if it's statx. If so: |
| 57 | + * 1. Set x0 (return register) to -ENOSYS |
| 58 | + * 2. Advance PC past the svc #0 instruction (4 bytes on aarch64) |
| 59 | + * |
| 60 | + * glibc's INLINE_SYSCALL sees x0 = -ENOSYS, recognizes it as an error |
| 61 | + * (aarch64 uses negative errno in x0 for errors), sets errno = ENOSYS, |
| 62 | + * and returns -1. glibc's statx() wrapper then caches this and falls back |
| 63 | + * to fstatat/newfstatat syscalls. |
| 64 | + */ |
| 65 | +static void statx_sigsys_handler(int sig, siginfo_t *info, void *ucontext) |
| 66 | +{ |
| 67 | + (void)sig; |
| 68 | + |
| 69 | + if (info->si_code != SYS_SECCOMP) |
| 70 | + return; |
| 71 | + |
| 72 | + if (info->si_syscall != __NR_statx) |
| 73 | + return; |
| 74 | + |
| 75 | + ucontext_t *ctx = (ucontext_t *)ucontext; |
| 76 | + |
| 77 | + /* Set return value to -ENOSYS (glibc expects negative errno in x0) */ |
| 78 | + ctx->uc_mcontext.regs[0] = (unsigned long long)(-(long long)ENOSYS); |
| 79 | + |
| 80 | + /* Advance past the svc #0 instruction (4 bytes) */ |
| 81 | + ctx->uc_mcontext.pc += 4; |
| 82 | +} |
| 83 | + |
| 84 | +/* |
| 85 | + * Install the SIGSYS handler when the shared library is loaded. |
| 86 | + * Uses __attribute__((constructor)) so it runs before main(). |
| 87 | + */ |
| 88 | +static void __attribute__((constructor)) statx_shim_init(void) |
| 89 | +{ |
| 90 | + struct sigaction sa; |
| 91 | + memset(&sa, 0, sizeof(sa)); |
| 92 | + sa.sa_sigaction = statx_sigsys_handler; |
| 93 | + sa.sa_flags = SA_SIGINFO; |
| 94 | + sigemptyset(&sa.sa_mask); |
| 95 | + |
| 96 | + /* |
| 97 | + * Only install if we're on aarch64 (the only architecture we support |
| 98 | + * for this shim). On other architectures, statx may not be blocked |
| 99 | + * by seccomp or the syscall number differs. |
| 100 | + */ |
| 101 | +#if defined(__aarch64__) |
| 102 | + sigaction(SIGSYS, &sa, NULL); |
| 103 | +#endif |
| 104 | +} |
0 commit comments