Skip to content

Commit 55101ce

Browse files
committed
fix: add statx seccomp shim to prevent SIGSYS crash on Android
Android's seccomp filter blocks the statx() syscall (aarch64 #291). glibc's stat()/fstatat() internally use direct svc #0 instructions, not the public statx() symbol, so LD_PRELOAD symbol interception cannot help. When seccomp blocks the syscall, SIGSYS → SIGSEGV crash. Solution: tools/statx-shim.c installs a SIGSYS handler that catches seccomp-blocked statx syscalls, returns -ENOSYS, and advances PC. glibc sees ENOSYS and falls back to fstatat/newfstatat syscalls. - tools/statx-shim.c: new SIGSYS handler shim (constructor attribute) - scripts/build.sh: compile shim during staging to lib/libstatx-shim.so - scripts/launcher.sh: LD_PRELOAD shim before launching runtime - README.md, docs/README.md: document shim and OPENCODE_DISABLE_STATX_SHIM=1 Same pattern used by libuv, OpenJDK 17/21/25, util-linux, Qt6 on Android.
1 parent a101d5b commit 55101ce

5 files changed

Lines changed: 144 additions & 0 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ Installed launcher includes:
134134
- stale lock cleanup
135135
- broken plugin cache cleanup
136136
- `OPENCODE_DISABLE_DEFAULT_PLUGINS=1` default
137+
- statx seccomp shim (`libstatx-shim.so`): installs a SIGSYS handler that catches seccomp-blocked `statx()` syscalls and returns `-ENOSYS`, so glibc falls back to `stat`/`fstatat` (Android seccomp blocks `statx` → `SIGSYS` → `SIGSEGV`). Disable with `OPENCODE_DISABLE_STATX_SHIM=1`.
137138

138139
## Metadata policy
139140

@@ -162,6 +163,7 @@ Maintainer/packager identity defaults to:
162163
- CI armv7 handoff: `docs/ci-prebuild-armv7.md`
163164
- Execution checklist: `docs/execution-checklist.md`
164165
- Incident RCA (`.so` restart snowball): `docs/incidents/2026-02-23-opencode-web-termux-so-avalanche.md`
166+
- Statx seccomp shim: `tools/statx-shim.c` (compiled during staging, preloaded via launcher)
165167

166168
## License / upstream
167169

‎docs/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ This directory is the single source of truth for the current Termux packaging/ru
5151
- Old generated `artifacts/staged`, `packaging/deb/work`, `packaging/pacman/src` can contain stale runtime versions
5252
- `sv status opencode-web` in Termux should use full service path (`$PREFIX/var/service/opencode-web`)
5353
- `opencode web` under runit may restart-loop and accumulate `.*-0000*.so` files if startup crashes
54+
- **statx seccomp crash**: Android's seccomp filter blocks `statx()` syscall → SIGSYS → SIGSEGV. The statx shim (`libstatx-shim.so`) is automatically compiled during staging and preloaded via launcher. Disable with `OPENCODE_DISABLE_STATX_SHIM=1`.
5455

5556
## Install policy summary
5657

‎scripts/build.sh‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,27 @@ if [[ -f "$DOCS_LIST" ]]; then
7171
done <"$DOCS_LIST"
7272
fi
7373

74+
# Compile statx-seccomp shim for Android/Termux compatibility
75+
# See: tools/statx-shim.c — returns ENOSYS so glibc falls back to stat/fstatat
76+
# The shim is self-contained (no libc calls), so any C compiler works.
77+
STATX_SHIM_SRC="$ROOT_DIR/tools/statx-shim.c"
78+
STATX_SHIM_DST="$PREFIX_DIR/lib/opencode/lib/libstatx-shim.so"
79+
if [[ -f "$STATX_SHIM_SRC" ]]; then
80+
log "compiling statx seccomp shim"
81+
ensure_dir "$PREFIX_DIR/lib/opencode/lib"
82+
if command -v gcc >/dev/null 2>&1; then
83+
gcc -shared -fPIC -o "$STATX_SHIM_DST" "$STATX_SHIM_SRC" || {
84+
log "warning: statx shim compilation failed, skipping"
85+
}
86+
elif command -v cc >/dev/null 2>&1; then
87+
cc -shared -fPIC -o "$STATX_SHIM_DST" "$STATX_SHIM_SRC" || {
88+
log "warning: statx shim compilation failed, skipping"
89+
}
90+
else
91+
log "warning: no C compiler found, statx shim not built"
92+
fi
93+
fi
94+
7495
write_build_meta "$ROOT_DIR/artifacts/opencode/build.meta" \
7596
"component=opencode" \
7697
"prefix=$PREFIX_DIR" \

‎scripts/launcher.sh‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@ set -euo pipefail
44
SELF_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
55
OPENCODE_CLI="$SELF_DIR/../lib/opencode/packages/opencode/bin/opencode"
66
OPENCODE_RUNTIME="$SELF_DIR/../lib/opencode/runtime/opencode"
7+
STATX_SHIM="$SELF_DIR/../lib/opencode/lib/libstatx-shim.so"
8+
9+
# Preload statx shim to avoid seccomp-blocked statx() syscall on Android.
10+
# glibc's stat()/fstatat() internally use direct syscall(statx) instructions;
11+
# Android seccomp blocks this (SIGSYS → SIGSEGV). The shim installs a SIGSYS
12+
# handler that returns -ENOSYS, triggering glibc's fallback to fstatat.
13+
# Can be disabled with OPENCODE_DISABLE_STATX_SHIM=1.
14+
apply_statx_shim() {
15+
if [[ "${OPENCODE_DISABLE_STATX_SHIM:-0}" == "1" ]]; then
16+
return
17+
fi
18+
if [[ -f "$STATX_SHIM" ]]; then
19+
export LD_PRELOAD="${STATX_SHIM}${LD_PRELOAD:+:$LD_PRELOAD}"
20+
fi
21+
}
722

823
cleanup_tty_full() {
924
if [ -t 1 ]; then
@@ -47,6 +62,7 @@ cleanup_state_locks
4762
cleanup_broken_cached_modules
4863
: "${OPENCODE_DISABLE_DEFAULT_PLUGINS:=1}"
4964
export OPENCODE_DISABLE_DEFAULT_PLUGINS
65+
apply_statx_shim
5066

5167
if [[ -x "$OPENCODE_RUNTIME" ]]; then
5268
"$OPENCODE_RUNTIME" "$@"

‎tools/statx-shim.c‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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

Comments
 (0)