Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions tools/proot-builder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,19 @@ runs the bundle on both ABIs (armeabi-v7a through the 32-bit compat mode of the
kernel, as on a phone). `guest-init.sh` is the test list. Each test names the
`build_static.sh` customization it covers: static start, the tar/gzip/xz/rsync round-trips,
the proot launch, fork, `fchmodat2`, `futex_time64` and EXITKILL.
`clock_gettime64-app-filter` (K2GO-597) runs proot under a filter like the Android 8 to 10
app filter (SIGSYS on `clock_gettime64` and the proot avoider): the probe must get ENOSYS and
a real time from its fallback, not a swallowed SIGSYS. The fault showed on kernels older than
4.8 (4.4 here); arm64 3.16 has no seccomp filters, so the test is a SKIP there.
The `*-app-filter` tests run proot under a filter like the Android 8 to 10 app filter (SIGSYS
on `clock_gettime64`, `futex_time64` and the proot avoider). `clock_gettime64-app-filter`
(K2GO-597): the probe must get ENOSYS and a real time from its fallback, not a swallowed SIGSYS;
the fault showed on kernels older than 4.8 (4.4 here). `futex_time64-app-filter`: proot must run
futex in place of the trapped call (32-bit Go programs crashed). `futex_time64-timeout-*`: a
wait must last its full timeout (3 x 0.9 s), so proot must copy the timeout in the 32-bit layout.
arm64 3.16 has no seccomp filters, so the filter tests are a SKIP there.
The `arm64-lib32` pass runs the 32-bit programs with the arm64 proot and its 32-bit loader (a
32-bit library on the arm64 app, K2GO-571), once with seccomp and once with
`PROOT_NO_SECCOMP=1` (`arm64-lib32-nosec`).
Not covered here: an arm64 kernel older than 3.19 with seccomp filters (a vendor 3.18 on a phone).
On such a kernel proot cannot change the syscall number of a 32-bit program, so it runs the call
with arguments that fail and chains the translated call. Check that case on a real phone.

proot runs with the options of the app (`PRootEngine`) and with its seccomp fallback
(ADFA-5362): when proot aborts with one of the markers of `SeccompFailure.java`, the test
Expand Down
92 changes: 88 additions & 4 deletions tools/proot-builder/build_static.sh
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,20 @@ STUB
# K2GO-411: translate aarch32 futex_time64 (#422) -> classic futex so 32-bit Go
# binaries run on pre-5.1 kernels (e.g. Android 4.14, where futex_time64 is
# ENOSYS). PRoot ships utimensat_time64 (v5.3.0); this follows the same path.
# Safe: PRoot has no futex arg handling and FUTEX_WAKE carries no timespec;
# on a 64-bit host PR_futex resolves to the native 64-bit-timespec futex.
# The wait operations carry a timeout with 64-bit fields, and futex reads 32-bit
# fields: translate_futex_time64() (step 5) copies the timeout in the 32-bit layout.
# A plain sysnum swap made a 500 ms wait return at once (kernel matrix,
# futex_time64-timeout).
SYSD="$TERMUX_PKG_SRCDIR/src/syscall"
TRD="$TERMUX_PKG_SRCDIR/src/tracee"
# 1) abstract sysnum
grep -q 'SYSNUM(futex_time64)' "$SYSD/sysnums.list" \
|| sed -i '/^SYSNUM(futex)$/a SYSNUM(futex_time64)' "$SYSD/sysnums.list"
# 2) enter.c: rewrite PR_futex_time64 -> PR_futex at sysenter
# 2) enter.c: translate PR_futex_time64 -> PR_futex at sysenter
grep -q 'PR_futex_time64' "$SYSD/enter.c" \
|| { awk '/^\tcase PR_getcwd:/ && !p { print "\tcase PR_futex_time64:"; print "\t\tset_sysnum(tracee, PR_futex);"; print "\t\tstatus = 0;"; print "\t\tbreak;"; print ""; p=1 } { print }' "$SYSD/enter.c" > "$SYSD/enter.c.tmp" && mv "$SYSD/enter.c.tmp" "$SYSD/enter.c"; }
|| { awk '/^\tcase PR_getcwd:/ && !p { print "\tcase PR_futex_time64:"; print "\t\tstatus = translate_futex_time64(tracee);"; print "\t\tbreak;"; print ""; p=1 } { print }' "$SYSD/enter.c" > "$SYSD/enter.c.tmp" && mv "$SYSD/enter.c.tmp" "$SYSD/enter.c"; }
grep -q '#include "tracee/seccomp.h"' "$SYSD/enter.c" \
|| sed -i 's|^#include "tracee/mem.h"$|&\n#include "tracee/seccomp.h" /* translate_futex_time64 */|' "$SYSD/enter.c"
# 3) ABI table: map the raw aarch32 number 422 -> PR_futex_time64 (i386 uses 422
# too). WITHOUT THIS the sysnum never resolves to PR_futex_time64 and the enter.c
# case never fires (verified on device: patch present but still ENOSYS). Anchor on
Expand All @@ -232,11 +237,90 @@ STUB
# so without this futex_time64 passes through untranslated unless PROOT_NO_SECCOMP).
grep -q '{ PR_futex_time64,' "$SYSD/seccomp.c" \
|| sed -i 's/^\([[:space:]]*\){ PR_fchmodat,[[:space:]]*0 },$/&\n\1{ PR_futex_time64, 0 },/' "$SYSD/seccomp.c"
# 5) One translation for both paths: the sysenter of step 2, and the SIGSYS of the
# Android 8 to 10 app filter, which can trap futex_time64 before any sysenter stop
# (the box Go programs crashed). The timeout copy goes below the stack pointer;
# both paths restore the stack pointer at the end of the syscall.
if ! grep -q 'int translate_futex_time64' "$TRD/seccomp.c"; then
sed -i 's|^void fix_and_restart_enosys_syscall(Tracee\* tracee);$|&\nint translate_futex_time64(Tracee *tracee);|' "$TRD/seccomp.h"
cat << 'FUTEX64' >> "$TRD/seccomp.c"

#include <stdint.h> /* int32_t, INT32_MAX */

/* K2GO-411: futex_time64 -> futex for the 32-bit ABIs. The wait
* operations read SYSARG_4 as a timeout with 64-bit fields; futex(2)
* reads 32-bit fields. Copy the timeout in the 32-bit layout below the
* stack pointer, which is restored at the end of the syscall. */
int translate_futex_time64(Tracee *tracee)
{
struct { int64_t sec; int64_t nsec; } t64;
struct { int32_t sec; int32_t nsec; } t32;
word_t op = peek_reg(tracee, CURRENT, SYSARG_2) & 0x7f; /* FUTEX_CMD_MASK */
word_t timeout = peek_reg(tracee, CURRENT, SYSARG_4);
word_t address;
int status;

set_sysnum(tracee, PR_futex);

/* FUTEX_WAIT, LOCK_PI, WAIT_BITSET, WAIT_REQUEUE_PI, LOCK_PI2. For
* the other operations SYSARG_4 is a number, not a pointer. */
if (timeout == 0 || (op != 0 && op != 6 && op != 9 && op != 11 && op != 13))
return 0;

status = read_data(tracee, &t64, timeout, sizeof(t64));
if (status < 0)
return status;

/* A time after 2038 waits until 2038. A value that futex(2) rejects
* stays invalid, so the kernel still returns EINVAL. */
t32.sec = t64.sec > INT32_MAX ? INT32_MAX : (t64.sec < INT32_MIN ? -1 : (int32_t) t64.sec);
t32.nsec = (t64.nsec > INT32_MAX || t64.nsec < INT32_MIN) ? -1 : (int32_t) t64.nsec;

address = alloc_mem(tracee, sizeof(t32));
if (address == 0)
return -EFAULT;
status = write_data(tracee, address, &t32, sizeof(t32));
if (status < 0)
return status;
poke_reg(tracee, SYSARG_4, address);
return 0;
}
FUTEX64
awk '/^\tcase PR_accept:$/ && !p { print "\tcase PR_futex_time64:"; print "\t\t/* K2GO-411: the app filter of Android 8 to 10 traps it (SIGSYS). */"; print "\t\tret = translate_futex_time64(tracee);"; print "\t\tif (ret < 0) {"; print "\t\t\tset_result_after_seccomp(tracee, ret);"; print "\t\t\tbreak;"; print "\t\t}"; print "\t\trestart_syscall_after_seccomp(tracee);"; print "\t\tbreak;"; print ""; p=1 } { print }' "$TRD/seccomp.c" > "$TRD/seccomp.c.tmp" && mv "$TRD/seccomp.c.tmp" "$TRD/seccomp.c"
fi
# 6) Without the NT_ARM_SYSTEM_CALL regset (arm64 Linux older than 3.19, a phone with
# 3.18 and a 32-bit library) proot cannot change the syscall number. It runs the original
# call with arguments that fail, then chains futex. The app filter traps that original
# futex_time64, and the SIGSYS handler took it as a new call: it wrote ENOSYS over the first
# argument of the chained futex (EINVAL; Forgejo crashed). The SIGSYS of that faulty call
# is its failure: proot swallows it, as it does for the avoider syscall.
if ! grep -q 'K2GO-411: the SIGSYS of the faulty call' "$TRD/event.c"; then
awk '
/VERBOSE\(tracee, 1, "Handling syscall exit from SIGSYS"\);/ && !d {
print "\t\t\t\t\t/* K2GO-411: the SIGSYS of the faulty call of the sysnum"
print "\t\t\t\t\t * workaround (chain.c) is its failure, not a new call. */"
print "\t\t\t\t\tconst bool faulty_call = tracee->chain.sysnum_workaround_state"
print "\t\t\t\t\t\t== SYSNUM_WORKAROUND_PROCESS_FAULTY_CALL;"
d = 1; w = 1
}
{ print }
w && /tracee->restore_sysarg1_after_sigsys = true;/ {
print "\t\t\t\t\tif (faulty_call)"
print "\t\t\t\t\t\ttracee->skip_next_seccomp_signal = true;"
w = 0
}' "$TRD/event.c" > "$TRD/event.c.tmp" && mv "$TRD/event.c.tmp" "$TRD/event.c"
fi
# guards
grep -q 'SYSNUM(futex_time64)' "$SYSD/sysnums.list" || { echo "!! [IIAB] sysnums.list anchor 'SYSNUM(futex)' not found"; exit 1; }
grep -q 'case PR_futex_time64:' "$SYSD/enter.c" || { echo "!! [IIAB] enter.c anchor 'case PR_getcwd:' not found"; exit 1; }
grep -q '\[ 422 \] = PR_futex_time64,' "$SYSD/sysnums-arm.h" || { echo "!! [IIAB] sysnums-arm.h anchor '[ 240 ] = PR_futex,' not found"; exit 1; }
grep -q '{ PR_futex_time64,' "$SYSD/seccomp.c" || { echo "!! [IIAB] seccomp.c anchor '{ PR_fchmodat, 0 }' not found"; exit 1; }
grep -q 'translate_futex_time64(tracee)' "$SYSD/enter.c" || { echo "!! [IIAB] enter.c futex_time64 case not translated"; exit 1; }
grep -q '#include "tracee/seccomp.h"' "$SYSD/enter.c" || { echo "!! [IIAB] enter.c anchor '#include \"tracee/mem.h\"' not found"; exit 1; }
grep -q 'int translate_futex_time64(Tracee \*tracee);' "$TRD/seccomp.h" || { echo "!! [IIAB] seccomp.h anchor 'fix_and_restart_enosys_syscall' not found"; exit 1; }
grep -q -F 'case PR_futex_time64:' "$TRD/seccomp.c" || { echo "!! [IIAB] tracee/seccomp.c anchor 'case PR_accept:' not found"; exit 1; }
grep -q 'K2GO-411: the SIGSYS of the faulty call' "$TRD/event.c" || { echo "!! [IIAB] tracee/event.c anchor 'Handling syscall exit from SIGSYS' not found"; exit 1; }
grep -q 'tracee->skip_next_seccomp_signal = true;' "$TRD/event.c" || { echo "!! [IIAB] tracee/event.c anchor 'restore_sysarg1_after_sigsys = true' not found"; exit 1; }

# --- ADFA-5334 (ADR-5334): translate fchmodat2 (452) like fchmodat, mirroring faccessat2. ---
# glibc>=2.39 restores directory modes via fchmodat2; proot passes it through untranslated, so
Expand Down
53 changes: 48 additions & 5 deletions tools/proot-builder/kernel-matrix/guest-init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,26 @@ for abi in arm64-v8a armeabi-v7a; do
[ "$rc" -eq "$want" ] && say "$abi" futex_time64-native PASS "kernel result $rc as expected" \
|| say "$abi" futex_time64-native FAIL "probe exit $rc, expected $want: $(cat /tmp/out)"
ptest "$abi" futex_time64-in-proot "probe in proot" $PROOT -r "$R" -w / /probe futex_time64
# The wait operations of futex_time64 carry a timeout with 64-bit fields; futex reads
# 32-bit fields, so proot must copy the timeout (a wrong copy waits 0 s).
ptest "$abi" futex_time64-timeout-in-proot "timeout in proot" $PROOT -r "$R" -w / /probe futex_time64-timeout
# The Android 8 to 10 app filter (K2GO-597): proot and the box inherit a filter that traps
# clock_gettime64 (403) and the proot avoider (222). proot must answer ENOSYS, so glibc
# falls back to clock_gettime; it must not swallow the SIGSYS as a syscall that it voided.
# The fault showed on kernels older than 4.8 (3.18 on a phone, 4.4 here); the old proot
# passes this test on 4.9 and later.
# clock_gettime64 (403), futex_time64 (422) and the proot avoider (222). For 403, proot
# must answer ENOSYS, so glibc falls back to clock_gettime; it must not swallow the SIGSYS
# as a syscall that it voided (the fault showed on kernels older than 4.8: 3.18 on a phone,
# 4.4 here). For 422, proot must run futex in its place (32-bit Go programs crashed).
if "/r/$abi/probe" seccomp-trap 403 /bin/busybox true >/tmp/out 2>&1; then
ptest "$abi" clock_gettime64-app-filter "clock_gettime64 under the app filter" \
"/r/$abi/probe" seccomp-trap 222,403 $PROOT -r "$R" -w / /probe clock_gettime64
ptest "$abi" futex_time64-app-filter "futex_time64 under the app filter" \
"/r/$abi/probe" seccomp-trap 222,403,422 $PROOT -r "$R" -w / /probe futex_time64
ptest "$abi" futex_time64-timeout-app-filter "futex_time64 timeout under the app filter" \
"/r/$abi/probe" seccomp-trap 222,403,422 $PROOT -r "$R" -w / /probe futex_time64-timeout
elif [ "$KV" -lt 3019 ]; then
# arm64 has seccomp filters since Linux 3.19; an Android 8 phone always has them.
say "$abi" clock_gettime64-app-filter SKIP "this kernel cannot install a seccomp filter: $(head -c 80 /tmp/out | tr '\n|' ' ')"
for t in clock_gettime64-app-filter futex_time64-app-filter futex_time64-timeout-app-filter; do
say "$abi" "$t" SKIP "this kernel cannot install a seccomp filter: $(head -c 80 /tmp/out | tr '\n|' ' ')"
done
else
say "$abi" clock_gettime64-app-filter FAIL "the probe could not install the filter: $(head -c 80 /tmp/out | tr '\n|' ' ')"
fi
Expand Down Expand Up @@ -157,5 +166,39 @@ for abi in arm64-v8a armeabi-v7a; do
cd /
done

# A 32-bit library on the arm64 app (K2GO-571): the arm64 proot runs the 32-bit programs with its
# 32-bit loader. The app filter of Android 8 to 10 has a 32-bit part, so the same futex_time64
# tests apply. Before Linux 4.8, seccomp acts before the sysenter stop, so here only the SIGSYS
# path of proot can translate futex_time64.
B=/k2go/arm64-v8a
R=/r/armeabi-v7a
# The app keeps PROOT_NO_SECCOMP=1 for all programs once the arm64 proot aborts on a kernel (the
# Nokia 6, Linux 3.18), so the pass runs in both modes.
for mode in seccomp no-seccomp; do
[ -f "$B/libproot-loader32.so" ] && [ -d "$R" ] || break
abi=arm64-lib32
mkdir -p "/tmp/pt-$abi"
export PROOT_LOADER="$B/libproot-loader.so" PROOT_LOADER_32="$B/libproot-loader32.so" PROOT_TMP_DIR="/tmp/pt-$abi"
if [ "$mode" = seccomp ]; then unset PROOT_NO_SECCOMP; else export PROOT_NO_SECCOMP=1; abi=arm64-lib32-nosec; fi
PROOT="$B/libproot.so --sysvipc -0 --link2symlink --kill-on-exit -b /dev -b /proc -b /sys"
ptest "$abi" proot-launch "launch" sh -c "$PROOT -r $R -w / /prog --version | grep -q tar"
ptest "$abi" futex_time64-in-proot "probe in proot" $PROOT -r "$R" -w / /probe futex_time64
ptest "$abi" futex_time64-timeout-in-proot "timeout in proot" $PROOT -r "$R" -w / /probe futex_time64-timeout
if /r/arm64-v8a/probe seccomp-trap 403 /bin/busybox true >/tmp/out 2>&1; then
ptest "$abi" clock_gettime64-app-filter "clock_gettime64 under the app filter" \
/r/arm64-v8a/probe seccomp-trap 222,403 $PROOT -r "$R" -w / /probe clock_gettime64
ptest "$abi" futex_time64-app-filter "futex_time64 under the app filter" \
/r/arm64-v8a/probe seccomp-trap 222,403,422 $PROOT -r "$R" -w / /probe futex_time64
ptest "$abi" futex_time64-timeout-app-filter "futex_time64 timeout under the app filter" \
/r/arm64-v8a/probe seccomp-trap 222,403,422 $PROOT -r "$R" -w / /probe futex_time64-timeout
elif [ "$KV" -lt 3019 ]; then
for t in clock_gettime64-app-filter futex_time64-app-filter futex_time64-timeout-app-filter; do
say "$abi" "$t" SKIP "this kernel cannot install a seccomp filter: $(head -c 80 /tmp/out | tr '\n|' ' ')"
done
else
say "$abi" clock_gettime64-app-filter FAIL "the probe could not install the filter: $(head -c 80 /tmp/out | tr '\n|' ' ')"
fi
done

echo "K2GO-END|$KNAME"
poweroff -f
31 changes: 30 additions & 1 deletion tools/proot-builder/kernel-matrix/probe.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* probe fchmodat2 <path> syscall 452, mode 0640
* probe clock_gettime64 syscall 403 (32-bit ARM only) as glibc calls it: on ENOSYS, fall
* back to clock_gettime. Exit 0 only for a real time.
* probe futex_time64-timeout syscall 422 FUTEX_WAIT, 3 times with a 0.9 s timeout; exit 0 only
* when the 3 waits take 2 to 5 s (a wrong timespec waits 0 s)
* probe seccomp-trap <n,n> <command...>
* install a filter that traps (SIGSYS) these 32-bit ARM syscalls,
* as the Android app filter does, then run the command in its place
Expand Down Expand Up @@ -111,11 +113,38 @@ int main(int argc, char** argv) {
#else
printf("clock_gettime64 exists only on 32-bit ABIs\n");
return 2;
#endif
}
if (argc == 2 && strcmp(argv[1], "futex_time64-timeout") == 0) {
#if defined(__arm__)
/* FUTEX_WAIT with a 0.9 s timeout (64-bit timespec), 3 times, timed with the 32-bit
* clock_gettime, so the result does not depend on clock_gettime64. The clock of a test VM can
* step by 1 s, so one short wait cannot be timed. A wrong timespec conversion waits 0 s. */
int word = 0;
struct timespec t0, t1;
long r = 0;
int err = 0;
if (syscall(NR_CLOCK_GETTIME_ARM, CLOCK_MONOTONIC, &t0) != 0) return result(-1);
for (int i = 0; i < 3; i++) {
struct { int64_t sec; int64_t nsec; } timeout = {0, 900000000};
r = syscall(NR_FUTEX_TIME64, &word, 0 /* FUTEX_WAIT */, 0, &timeout, NULL, 0);
err = errno;
if (r != -1 || err != ETIMEDOUT) break;
}
if (syscall(NR_CLOCK_GETTIME_ARM, CLOCK_MONOTONIC, &t1) != 0) return result(-1);
long ms = (t1.tv_sec - t0.tv_sec) * 1000L + (t1.tv_nsec - t0.tv_nsec) / 1000000L;
printf("futex_time64 wait 3 x 900 ms: result %ld errno %d, elapsed %ld ms\n", r, r == -1 ? err : 0, ms);
if (r == -1 && err == ENOSYS) return 38;
if (r != -1 || err != ETIMEDOUT) return 1;
return (ms >= 2000 && ms <= 5000) ? 0 : 1;
#else
printf("futex_time64 exists only on 32-bit ABIs\n");
return 2;
#endif
}
if (argc >= 4 && strcmp(argv[1], "seccomp-trap") == 0) {
return seccomp_trap(argv[2], argv + 3);
}
fprintf(stderr, "usage: probe futex_time64 | fchmodat2 <path> | clock_gettime64 | seccomp-trap <n,n> <command...>\n");
fprintf(stderr, "usage: probe futex_time64 | futex_time64-timeout | fchmodat2 <path> | clock_gettime64 | seccomp-trap <n,n> <command...>\n");
return 2;
}
2 changes: 2 additions & 0 deletions tools/proot-builder/kernel-matrix/run-kernel-matrix.sh
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ for pair in arm64-v8a:arm64 armeabi-v7a:armhf; do
for b in tar gzip xz proot proot-loader rsync nano less; do
cp "$JNI/$abi/lib$b.so" "$ROOT/k2go/$abi/"
done
# arm64 only: the loader of 32-bit programs (a 32-bit library on the arm64 app, K2GO-571).
[ -f "$JNI/$abi/libproot-loader32.so" ] && cp "$JNI/$abi/libproot-loader32.so" "$ROOT/k2go/$abi/"
cp "$WORK/bb-$deb/bin/busybox" "$ROOT/r/$abi/bin/busybox"
cp "$WORK/probe-$deb" "$ROOT/r/$abi/probe"
cp "$JNI/$abi/libtar.so" "$ROOT/r/$abi/prog"
Expand Down
Loading