This commit is contained in:
LZX committed 2026-09-13 16:41:19 +08:00
1 parent 87e03ce0d8
commit e932c60dbd
47 files changed
+4417 -923

No files matched your search

+86
View File
@@ -0,0 +1,86 @@
# 容器自检工具(container-selfcheck)
在 **Android app 进程内**的 PRoot 容器里做系统调用级自检,用于定位"某个 syscall 被平台策略
拦掉 / PRoot 语义不对"这类问题。本目录是 2026-09-13 排查 `apk temporary error` /
`Failed to preserve modification time` 时用的工具,已固化为可复用资产。
结论与证据见仓库根目录 `CONTAINER_FIX_SUMMARY.md`。
---
## 组件
| 文件 | 作用 |
|---|---|
| `probe3.c` | 系统调用策略自检:`poll/ppoll/select/pselect6/epoll_wait(epoll_pwait)/fcntl/ioctl/utimensat/fstat/statfs/clone/unshare/chroot/getrandom`。**支持 x86_64 与 arm64**(两者系统调用表不同,代码里已按架构区分)。 |
| `probe4.c` | 复现 apk 安装动作序列:`建目录 → openat 解包 → utimensat 保 mtime → linkat 硬链接 → renameat 改名`,用来定位 PRoot `--link2symlink` 的问题。 |
| `probe5.c` | 检查 PRoot 对空路径 `newfstatat(fd,"",st,AT_EMPTY_PATH)` 的处理(曾用于判定旧 workaround 是否还必要)。 |
| `selfcheck.sh` | 容器内一键验收:`apk update` / 硬链接包全新安装 / 带触发器安装 / broken 标记 / 三个 probe。 |
| `guest-run.sh` | **免重装 APK** 的 adb 侧跑手:用 `pm path` 推导原生库目录,自己拼 PRoot 命令行,在容器里执行任意命令。 |
## 编译探针
```bash
NDK=<ndk>/toolchains/llvm/prebuilt/<host>/bin
# arm64 真机
"$NDK/clang" --target=aarch64-linux-android21 -nostdlib -static -fno-stack-protector -O1 -o probe3 probe3.c
# x86_64 模拟器
"$NDK/clang" --target=x86_64-linux-android21 -nostdlib -static -fno-stack-protector -O1 -o probe3 probe3.c
```
无 libc、无依赖:所有系统调用直接发 `svc #0` / `syscall`,结果用 `write(2)` 打印。
## 用法(真机 / 模拟器都适用)
前提:app 为 **debug** 构建(`run-as` 需要),设备已连 adb。
```bash
PKG=com.example.code_editor
BASE=/data/data/$PKG/files/distros/alpine
R=$BASE/rootfs
# 1) 把探针与自检脚本放进容器
adb push probe3 /data/local/tmp/probe3
adb push selfcheck.sh /data/local/tmp/selfcheck.sh
adb shell "run-as $PKG sh -c 'cat /data/local/tmp/probe3 > $R/root/probe3; chmod 755 $R/root/probe3; \
cat /data/local/tmp/selfcheck.sh > $R/root/selfcheck.sh; chmod 755 $R/root/selfcheck.sh'"
# 2) 在容器里跑(adb 侧免重装)
adb shell "run-as $PKG sh /data/local/tmp/guest-run.sh /bin/sh /root/selfcheck.sh"
# 或者:push guest-run.sh 后
adb shell "run-as $PKG sh /data/local/tmp/guest-run.sh /root/probe3"
# 3) 读结果
adb shell "run-as $PKG cat $R/root/acc.log"
```
### 在「App 自己的终端会话」里跑(需要 app 上下文时)
App 上下文(有 zygote 装上的 seccomp filter)才是线上真实环境。两种方式:
- 直接在终端里敲:`sh /root/selfcheck.sh`
- **免打字**:把钩子挂到 `/etc/profile.d`,新会话自动执行(本目录不含该钩子文件,按需创建):
```sh
# <rootfs>/etc/profile.d/zz-selfcheck.sh
( sh /root/selfcheck.sh >/dev/null 2>&1 ) &
```
然后重启 App 并进入终端页,结果写入 `/root/acc.log`。
## 两条铁律
1. **不要在容器运行中原地覆盖 `/lib/libfix_seccomp.so`**:正在映射该文件的进程会 SIGSEGV
(实测:会话 `proot info: vpid 1: terminated with signal 11`)。必须
「写临时文件 → `mv` 重命名」。
2. **换 shim 后需要新起进程才生效**:已运行的进程仍映射旧库。用 `am force-stop` 重启 App,
或直接新开一个会话。
## 已定位(供参考,不必重跑)
| 现象 | 结论 |
|---|---|
| `apk` 报 `temporary error (try again later)` | **x86_64 专属**:arm64 无 `poll/select` 系统调用;x86_64 有且被 app seccomp 封为 ENOSYS,musl 的 `#ifdef SYS_poll` 命中 ⇒ 解析器超时 ⇒ `EAI_AGAIN`。 |
| `apk` 报 `IO ERROR` | 同上(`select`),libfetch 失败时不设错误码。 |
| `Failed to preserve modification time`(tzdata 等硬链接包) | **跨架构**:Android 禁止 `link(2)` ⇒ PRoot 用 symlink + `.proot-meta-file.*` 模拟 ⇒ apk 跟随语义的 `utimensat` 落到隐藏名 ⇒ ENOENT ⇒ 包被标 broken。 |
| `newfstatat(fd,"",st,AT_EMPTY_PATH)` | PRoot 处理正常(=0),旧的 workaround 已删除。 |
| `--sysvipc` | 本项目 `libproot.so` **不支持**(`unknown option`),不能加;Termux 的 proot 是带 libandroid-shmem 的 fork 版。 |
| `openat(dir, O_DIRECTORY)` | 在本项目 proot 下返回 **EINVAL**(`O_PATH|O_DIRECTORY` 正常)——已知怪癖,当前无功能影响。 |
+16
View File
@@ -0,0 +1,16 @@
#!/system/bin/sh
# run an arbitrary guest binary inside the PRoot container (adb harness)
PKG=com.example.code_editor
BASE=/data/data/$PKG/files/distros/alpine
ROOT=$BASE/rootfs
LIBDIR=$(pm path $PKG 2>/dev/null | head -1 | sed 's|package:||; s|/base.apk$||')/lib/arm64
export LD_LIBRARY_PATH=$LIBDIR
export PROOT_TMP_DIR=/data/data/$PKG/cache
export PROOT_L2S_DIR=$BASE/.l2s
P=$LIBDIR/libproot.so
cd $ROOT
exec $P --kill-on-exit --link2symlink -0 -r . -b /dev -b /dev/urandom:/dev/random \
-b $BASE/shm:/dev/shm -b /proc -b /sys -b /data/data/$PKG/cache:/tmp -w /root \
/usr/bin/env -i HOME=/root USER=root TERM=dumb LANG=C.UTF-8 \
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \
LD_PRELOAD=/lib/libfix_seccomp.so "$@"
+271
View File
@@ -0,0 +1,271 @@
/* probe3.c - real-device syscall self-check (freestanding, x86_64 + aarch64)
*
* Answers, on ANY device: which syscalls does the Android app seccomp policy
* deny to this container, and does the guest's path handling (utimensat,
* clone, unshare) work?
*
* Build:
* clang --target=aarch64-linux-android21 -nostdlib -static -O1 -o probe3 probe3.c
* clang --target=x86_64-linux-android21 -nostdlib -static -O1 -o probe3 probe3.c
*/
typedef unsigned long u64;
typedef long i64;
#if defined(__x86_64__)
static inline i64 sc6(i64 n, i64 a1, i64 a2, i64 a3, i64 a4, i64 a5, i64 a6)
{
i64 ret;
register i64 r10 __asm__("r10") = a4;
register i64 r8 __asm__("r8") = a5;
register i64 r9 __asm__("r9") = a6;
__asm__ __volatile__("syscall" : "=a"(ret)
: "a"(n), "D"(a1), "S"(a2), "d"(a3), "r"(r10), "r"(r8), "r"(r9)
: "rcx", "r11", "memory");
return ret;
}
#define SYS_read 0
#define SYS_write 1
#define SYS_close 3
#define SYS_fstat 5
#define SYS_poll 7
#define SYS_select 23
#define SYS_ioctl 16
#define SYS_socket 41
#define SYS_connect 42
#define SYS_sendto 44
#define SYS_recvfrom 45
#define SYS_bind 49
#define SYS_clone 56
#define SYS_uname 63
#define SYS_fcntl 72
#define SYS_statfs 137
#define SYS_epoll_wait 232
#define SYS_exit 60
#define SYS_wait4 61
#define SYS_openat 257
#define SYS_pselect6 270
#define SYS_ppoll 271
#define SYS_epoll_create1 291
#define SYS_utimensat 280
#define SYS_unshare 272
#define SYS_chroot 161
#define SYS_getrandom 318
#elif defined(__aarch64__)
static inline i64 sc6(i64 n, i64 a1, i64 a2, i64 a3, i64 a4, i64 a5, i64 a6)
{
register i64 x8 __asm__("x8") = n;
register i64 x0 __asm__("x0") = a1;
register i64 x1 __asm__("x1") = a2;
register i64 x2 __asm__("x2") = a3;
register i64 x3 __asm__("x3") = a4;
register i64 x4 __asm__("x4") = a5;
register i64 x5 __asm__("x5") = a6;
__asm__ __volatile__("svc #0" : "+r"(x0)
: "r"(x8), "r"(x1), "r"(x2), "r"(x3), "r"(x4), "r"(x5) : "memory");
return x0;
}
#define SYS_read 63
#define SYS_write 64
#define SYS_close 57
#define SYS_fstat 80
/* NOTE: arm64 (asm-generic table) has NO poll/select/epoll_wait syscalls at
* all - libc implements them on top of ppoll/pselect6/epoll_pwait. That is
* exactly why the x86_64-only "poll blocked" failure cannot happen here. */
#define SYS_poll (-1)
#define SYS_select (-1)
#define SYS_ioctl 29
#define SYS_epoll_wait (-1)
#define SYS_exit 93
#define SYS_wait4 260
#define SYS_socket 198
#define SYS_connect 203
#define SYS_sendto 206
#define SYS_recvfrom 207
#define SYS_bind 200
#define SYS_clone 220
#define SYS_uname 160
#define SYS_fcntl 25
#define SYS_statfs 43
#define SYS_openat 56
#define SYS_pselect6 72
#define SYS_ppoll 73
#define SYS_epoll_create1 20
#define SYS_utimensat 88
#define SYS_unshare 97
#define SYS_chroot 51
#define SYS_getrandom 278
#endif
static i64 sc0(i64 n) { return sc6(n, 0, 0, 0, 0, 0, 0); }
static i64 sc1(i64 n, i64 a) { return sc6(n, a, 0, 0, 0, 0, 0); }
static i64 sc2(i64 n, i64 a, i64 b) { return sc6(n, a, b, 0, 0, 0, 0); }
static i64 sc3(i64 n, i64 a, i64 b, i64 c) { return sc6(n, a, b, c, 0, 0, 0); }
static i64 sc4(i64 n, i64 a, i64 b, i64 c, i64 d) { return sc6(n, a, b, c, d, 0, 0); }
static void put(const char *s)
{
i64 n = 0;
while (s[n]) n++;
sc3(SYS_write, 1, (i64)s, n);
}
static void putnum(i64 v)
{
char b[24];
int i = 24, neg = v < 0;
unsigned long u = neg ? (unsigned long)(-v) : (unsigned long)v;
if (v == 0) { put("0"); return; }
b[--i] = 0;
while (u) { b[--i] = '0' + (u % 10); u /= 10; }
if (neg) b[--i] = '-';
put(&b[i]);
}
/* print a line, and for negative returns append a verdict */
static void show(const char *name, i64 ret)
{
put(name); put(" = "); putnum(ret);
if (ret == -38) put(" <-- ENOSYS (blocked by policy)");
else if (ret == -1) put(" <-- EPERM");
else if (ret == -9) put(" <-- EBADF");
else if (ret == -13) put(" <-- EACCES");
put("\n");
}
struct sockaddr_in { unsigned short f; unsigned short p; unsigned int a; unsigned char pad[8]; };
struct pollfd { int fd; short events; short revents; };
struct timespec { long s; long n; };
struct timeval { long s; long us; };
void prog_main(void);
#if defined(__x86_64__)
__asm__(
".globl _start\n"
"_start:\n"
" andq $-16, %rsp\n"
" call prog_main\n"
" movl $60, %eax\n"
" xorl %edi, %edi\n"
" syscall\n"
);
#else
__asm__(
".globl _start\n"
"_start:\n"
" bl prog_main\n"
" mov x0, #0\n"
" mov x8, #93\n"
" svc #0\n"
);
#endif
void prog_main(void)
{
i64 r, fd, sfd, dfd, ofd;
struct pollfd pfd;
struct timespec ts;
struct timeval tv;
struct sockaddr_in ns;
long ubuf[64];
put("=== probe3 (real device syscall self-check) ===\n");
/* --- wait primitives: the ones Android denies to app processes --- */
fd = sc3(SYS_socket, 2, 2, 0); /* AF_INET, SOCK_DGRAM */
show("socket(UDP)", fd);
if (fd >= 0) {
pfd.fd = (int)fd; pfd.events = 1; pfd.revents = 0;
ts.s = 0; ts.n = 200000000L; /* 200ms */
#if defined(__x86_64__)
show("poll(fd,POLLIN,200ms)", sc3(SYS_poll, (i64)&pfd, 1, 200));
#else
put("poll(...) = N/A on arm64 (syscall does not exist)\n");
#endif
pfd.revents = 0;
show("ppoll(fd,POLLIN,200ms)", sc6(SYS_ppoll, (i64)&pfd, 1, (i64)&ts, 0, 0, 0));
tv.s = 0; tv.us = 0;
#if defined(__x86_64__)
show("select(0,NULL,NULL,NULL,{0,0})", sc6(SYS_select, 0, 0, 0, 0, (i64)&tv, 0));
#else
put("select(...) = N/A on arm64 (syscall does not exist)\n");
#endif
show("pselect6(0,...,{0})", sc6(SYS_pselect6, 0, 0, 0, 0, (i64)&ts, 0));
r = sc1(SYS_epoll_create1, 0);
show("epoll_create1(0)", r);
#if defined(__x86_64__)
if (r >= 0)
show("epoll_wait(epfd,1,0)", sc4(SYS_epoll_wait, r, (i64)ubuf, 1, 0));
#else
if (r >= 0) {
/* arm64 only has epoll_pwait (22) */
show("epoll_pwait(epfd,1,0,NULL)", sc6(22, r, (i64)ubuf, 1, 0, 0, 0));
}
#endif
show("fcntl(F_GETFL)", sc4(SYS_fcntl, fd, 3, 0, 0));
r = sc4(SYS_fcntl, fd, 3, 0, 0);
show("fcntl(F_SETFL,|NONBLOCK)", sc4(SYS_fcntl, fd, 4, r | 0x800, 0));
show("fcntl(F_SETFL,&~NONBLOCK)", sc4(SYS_fcntl, fd, 4, r & ~0x800, 0));
{
int one = 1;
show("ioctl(FIONBIO)", sc3(SYS_ioctl, fd, 0x5421, (i64)&one));
}
sc1(SYS_close, fd);
}
/* --- TCP + DNS-relevant --- */
sfd = sc3(SYS_socket, 2, 1, 0);
show("socket(TCP)", sfd);
ns.f = 2; ns.p = 0x3500; ns.a = 0x08080808; /* 8.8.8.8:53 */
if (sfd >= 0) {
show("bind(UDP 0.0.0.0:0)", sc3(SYS_bind, sfd, (i64)&ns, 16));
sc1(SYS_close, sfd);
}
/* --- file/path handling used by apk's installer --- */
show("openat(create /root/utest)", fd = sc4(SYS_openat, -100, (i64)"/root/utest",
0x40 | 1 | 0x200, 0644));
if (fd >= 0) sc1(SYS_close, fd);
show("utimensat(AT_FDCWD, abs)", sc4(SYS_utimensat, -100, (i64)"/root/utest", 0, 0));
show("utimensat(AT_FDCWD, rel)", sc4(SYS_utimensat, -100, (i64)"utest", 0, 0));
dfd = sc4(SYS_openat, -100, (i64)"/root", 0x10000, 0);
show("openat(/root, O_DIRECTORY)", dfd);
if (dfd >= 0) {
show("utimensat(dirfd, rel) [apk style]", sc4(SYS_utimensat, dfd, (i64)"utest", 0, 0));
show("utimensat(dirfd, rel, NOFOLLOW)", sc4(SYS_utimensat, dfd, (i64)"utest", 0, 0x100));
ofd = sc4(SYS_openat, dfd, (i64)"utest", 0x200000, 0);
show("openat(dirfd, rel, O_PATH)", ofd);
if (ofd >= 0) {
show("utimensat(fd, \"\", AT_EMPTY_PATH)", sc4(SYS_utimensat, ofd, (i64)"", 0, 0x1000));
sc1(SYS_close, ofd);
}
sc1(SYS_close, dfd);
}
show("fstat(1)", sc2(SYS_fstat, 1, (i64)ubuf));
show("statfs(\"/\")", sc2(SYS_statfs, (i64)"/", (i64)ubuf));
/* --- process creation: what apk's trigger scripts and shells need --- */
{
i64 pid = sc6(SYS_clone, 17 /*SIGCHLD*/, 0, 0, 0, 0, 0);
if (pid == 0) { /* child: leave immediately */
sc1(SYS_exit, 0);
for (;;) {}
}
show("clone(SIGCHLD) [=fork]", pid);
if (pid > 0) {
i64 st = 0;
/* wait4(pid, &st, 0, 0) = 61 on x86_64 / 260 on aarch64 */
sc4(SYS_wait4, pid, (i64)&st, 0, 0);
}
}
show("unshare(CLONE_NEWNS)", sc1(SYS_unshare, 0x00020000));
show("getrandom(8)", sc3(SYS_getrandom, (i64)ubuf, 8, 1));
show("uname", sc1(SYS_uname, (i64)ubuf));
put("=== probe3 done ===\n");
/* last: chroot is irreversible enough to keep at the very end */
show("chroot(\"/\")", sc1(SYS_chroot, (i64)"/"));
sc1(SYS_exit, 0);
for (;;) {}
}
+209
View File
@@ -0,0 +1,209 @@
/* probe4.c - A2 investigation: reproduce apk's "preserve modification time"
* failure for hardlinked entries, and map which PRoot path operation breaks.
*
* apk's installer does, per file:
* extract -> <dir>/.apk.<sha256> (openat O_CREAT|O_WRONLY, write)
* utimensat(dirfd, ".apk.<sha>", times, AT_SYMLINK_NOFOLLOW)
* renameat(dirfd, ".apk.<sha>", dirfd, "<final name>")
* and for hardlinked entries it first creates a hard link to an earlier entry
* (linkat). PRoot's --link2symlink rewrites link() into symlink + sidecar
* ".proot-meta-file.*", which is what we see left behind in /usr/share/zoneinfo.
*
* This probe walks that exact sequence with raw syscalls and reports the first
* failing step, for every flag combination that could matter.
*
* Build: clang --target=aarch64-linux-android21 -nostdlib -static -O1 -o probe4 probe4.c
*/
typedef unsigned long u64;
typedef long i64;
#if defined(__x86_64__)
static inline i64 sc6(i64 n, i64 a1, i64 a2, i64 a3, i64 a4, i64 a5, i64 a6)
{
i64 ret;
register i64 r10 __asm__("r10") = a4;
register i64 r8 __asm__("r8") = a5;
register i64 r9 __asm__("r9") = a6;
__asm__ __volatile__("syscall" : "=a"(ret)
: "a"(n), "D"(a1), "S"(a2), "d"(a3), "r"(r10), "r"(r8), "r"(r9)
: "rcx", "r11", "memory");
return ret;
}
#define SYS_write 1
#define SYS_close 3
#define SYS_openat 257
#define SYS_utimensat 280
#define SYS_renameat 264
#define SYS_linkat 265
#define SYS_unlinkat 263
#define SYS_mkdirat 258
#define SYS_exit 60
#define SYS_lstat_newfstatat 262
#elif defined(__aarch64__)
static inline i64 sc6(i64 n, i64 a1, i64 a2, i64 a3, i64 a4, i64 a5, i64 a6)
{
register i64 x8 __asm__("x8") = n;
register i64 x0 __asm__("x0") = a1;
register i64 x1 __asm__("x1") = a2;
register i64 x2 __asm__("x2") = a3;
register i64 x3 __asm__("x3") = a4;
register i64 x4 __asm__("x4") = a5;
register i64 x5 __asm__("x5") = a6;
__asm__ __volatile__("svc #0" : "+r"(x0)
: "r"(x8), "r"(x1), "r"(x2), "r"(x3), "r"(x4), "r"(x5) : "memory");
return x0;
}
#define SYS_write 64
#define SYS_close 57
#define SYS_openat 56
#define SYS_utimensat 88
#define SYS_renameat 38
#define SYS_linkat 37
#define SYS_unlinkat 35
#define SYS_mkdirat 34
#define SYS_exit 93
#define SYS_lstat_newfstatat 79
#endif
static i64 sc0(i64 n) { return sc6(n, 0, 0, 0, 0, 0, 0); }
static i64 sc1(i64 n, i64 a) { return sc6(n, a, 0, 0, 0, 0, 0); }
static i64 sc2(i64 n, i64 a, i64 b) { return sc6(n, a, b, 0, 0, 0, 0); }
static i64 sc3(i64 n, i64 a, i64 b, i64 c) { return sc6(n, a, b, c, 0, 0, 0); }
static i64 sc4(i64 n, i64 a, i64 b, i64 c, i64 d) { return sc6(n, a, b, c, d, 0, 0); }
static void put(const char *s)
{
i64 n = 0;
while (s[n]) n++;
sc3(SYS_write, 1, (i64)s, n);
}
static void putnum(i64 v)
{
char b[24];
int i = 24, neg = v < 0;
unsigned long u = neg ? (unsigned long)(-v) : (unsigned long)v;
if (v == 0) { put("0"); return; }
b[--i] = 0;
while (u) { b[--i] = '0' + (u % 10); u /= 10; }
if (neg) b[--i] = '-';
put(&b[i]);
}
static void show(const char *name, i64 ret)
{
put(name); put(" = "); putnum(ret);
if (ret == -2) put(" [ENOENT]");
else if (ret == -22) put(" [EINVAL]");
else if (ret == -9) put(" [EBADF]");
else if (ret == -40) put(" [ELOOP]");
else if (ret == -38) put(" [ENOSYS]");
put("\n");
}
#define AT_FDCWD (-100)
#define AT_SYMLINK_NOFOLLOW 0x100
#define AT_EMPTY_PATH 0x1000
#define O_RDONLY 0
#define O_WRONLY 1
#define O_CREAT 0x40
#define O_TRUNC 0x200
#define O_DIRECTORY 0x10000
#define O_NOFOLLOW 0x20000
#define O_PATH 0x200000
#define O_CLOEXEC 0x80000
#define AT_REMOVEDIR 0x200
void prog_main(void);
#if defined(__x86_64__)
__asm__(".globl _start\n_start:\n andq $-16, %rsp\n call prog_main\n"
" movl $60, %eax\n xorl %edi, %edi\n syscall\n");
#else
__asm__(".globl _start\n_start:\n bl prog_main\n mov x0, #0\n"
" mov x8, #93\n svc #0\n");
#endif
void prog_main(void)
{
i64 dfd, ffd, ofd, r;
long st[64];
put("=== probe4: replicate apk's extract -> mtime -> link -> rename ===\n");
sc1(SYS_mkdirat, 0); /* no-op to keep syscall table honest */
/* step 0: fresh working directory */
sc4(SYS_openat, AT_FDCWD, (i64)"/root/p4dir", O_RDONLY | O_DIRECTORY, 0);
sc3(SYS_mkdirat, AT_FDCWD, (i64)"/root/p4dir", 0755);
put("--- opening the directory (apk keeps a dirfd per directory) ---\n");
/* report the flag variants, but use the plain O_RDONLY fd as the working
* dirfd so the rest of the sequence is meaningful even if one fails */
show("openat(/root/p4dir, RDONLY)", sc4(SYS_openat, AT_FDCWD, (i64)"/root/p4dir", O_RDONLY, 0));
show("openat(/root/p4dir, RDONLY|O_DIRECTORY)",
sc4(SYS_openat, AT_FDCWD, (i64)"/root/p4dir", O_RDONLY | O_DIRECTORY, 0));
show("openat(/root/p4dir, RDONLY|O_DIRECTORY|O_CLOEXEC)",
sc4(SYS_openat, AT_FDCWD, (i64)"/root/p4dir", O_RDONLY | O_DIRECTORY | O_CLOEXEC, 0));
show("openat(/root/p4dir, O_PATH)",
sc4(SYS_openat, AT_FDCWD, (i64)"/root/p4dir", O_PATH, 0));
show("openat(/root/p4dir, O_PATH|O_DIRECTORY)",
sc4(SYS_openat, AT_FDCWD, (i64)"/root/p4dir", O_PATH | O_DIRECTORY, 0));
show("working dirfd = openat(RDONLY)",
dfd = sc4(SYS_openat, AT_FDCWD, (i64)"/root/p4dir", O_RDONLY, 0));
if (dfd < 0) {
put("!! no usable dirfd, aborting\n");
sc1(SYS_exit, 1);
}
put("--- apk step 1: extract into .apk.<hash> (dirfd-relative) ---\n");
show("openat(dfd, \".apk.aaa\", O_CREAT|O_WRONLY|O_TRUNC)",
ffd = sc4(SYS_openat, dfd, (i64)".apk.aaa", O_CREAT | O_WRONLY | O_TRUNC, 0644));
if (ffd >= 0) {
sc3(SYS_write, ffd, (i64)"hello", 5);
sc1(SYS_close, ffd);
}
put("--- apk step 2: preserve mtime on the temp name ---\n");
show("utimensat(dfd, \".apk.aaa\", NULL, AT_SYMLINK_NOFOLLOW)",
sc4(SYS_utimensat, dfd, (i64)".apk.aaa", 0, AT_SYMLINK_NOFOLLOW));
show("utimensat(dfd, \".apk.aaa\", NULL, 0)",
sc4(SYS_utimensat, dfd, (i64)".apk.aaa", 0, 0));
show("newfstatat(dfd, \".apk.aaa\", NOFOLLOW)",
sc4(SYS_lstat_newfstatat, dfd, (i64)".apk.aaa", (i64)st, AT_SYMLINK_NOFOLLOW));
show("openat(dfd, \".apk.aaa\", O_PATH)",
ofd = sc4(SYS_openat, dfd, (i64)".apk.aaa", O_PATH, 0));
if (ofd >= 0) {
show("utimensat(ofd, \"\", NULL, AT_EMPTY_PATH)",
sc4(SYS_utimensat, ofd, (i64)"", 0, AT_EMPTY_PATH));
sc1(SYS_close, ofd);
}
put("--- apk step 3: hardlink to an earlier entry (tzdata's case) ---\n");
show("linkat(dfd, \".apk.aaa\" -> \".apk.bbb\")",
sc6(SYS_linkat, dfd, (i64)".apk.aaa", dfd, (i64)".apk.bbb", 0, 0));
show("newfstatat(dfd, \".apk.bbb\", NOFOLLOW)",
sc4(SYS_lstat_newfstatat, dfd, (i64)".apk.bbb", (i64)st, AT_SYMLINK_NOFOLLOW));
show("utimensat(dfd, \".apk.bbb\", NULL, AT_SYMLINK_NOFOLLOW)",
sc4(SYS_utimensat, dfd, (i64)".apk.bbb", 0, AT_SYMLINK_NOFOLLOW));
show("utimensat(dfd, \".apk.bbb\", NULL, 0) [follows l2s link]",
sc4(SYS_utimensat, dfd, (i64)".apk.bbb", 0, 0));
put("--- apk step 4: rename temp -> final name ---\n");
show("renameat(dfd, \".apk.aaa\" -> \"final1\")",
sc4(SYS_renameat, dfd, (i64)".apk.aaa", dfd, (i64)"final1"));
show("renameat(dfd, \".apk.bbb\" -> \"final2\")",
sc4(SYS_renameat, dfd, (i64)".apk.bbb", dfd, (i64)"final2"));
show("utimensat(dfd, \"final1\", NULL, 0)", sc4(SYS_utimensat, dfd, (i64)"final1", 0, 0));
show("utimensat(dfd, \"final2\", NULL, 0)", sc4(SYS_utimensat, dfd, (i64)"final2", 0, 0));
put("--- cleanup / listing evidence ---\n");
show("newfstatat(dfd, \"final2\", NOFOLLOW)",
sc4(SYS_lstat_newfstatat, dfd, (i64)"final2", (i64)st, AT_SYMLINK_NOFOLLOW));
show("unlinkat(dfd, \"final1\")", sc3(SYS_unlinkat, dfd, (i64)"final1", 0));
show("unlinkat(dfd, \"final2\")", sc3(SYS_unlinkat, dfd, (i64)"final2", 0));
if (dfd >= 0) sc1(SYS_close, dfd);
put("=== probe4 done ===\n");
sc1(SYS_exit, 0);
for (;;) {}
}
+106
View File
@@ -0,0 +1,106 @@
/* probe5.c - is the old "empty path triggers PRoot EINVAL" workaround still needed?
* Tests newfstatat(fd, "", buf, AT_EMPTY_PATH) and fstat(fd) under PRoot.
* Build: clang --target=aarch64-linux-android21 -nostdlib -static -O1 -o probe5 probe5.c
*/
typedef long i64;
#if defined(__x86_64__)
static inline i64 sc6(i64 n, i64 a1, i64 a2, i64 a3, i64 a4, i64 a5, i64 a6)
{
i64 ret;
register i64 r10 __asm__("r10") = a4;
register i64 r8 __asm__("r8") = a5;
register i64 r9 __asm__("r9") = a6;
__asm__ __volatile__("syscall" : "=a"(ret)
: "a"(n), "D"(a1), "S"(a2), "d"(a3), "r"(r10), "r"(r8), "r"(r9)
: "rcx", "r11", "memory");
return ret;
}
#define SYS_write 1
#define SYS_close 3
#define SYS_fstat 5
#define SYS_openat 257
#define SYS_newfstatat 262
#define SYS_exit 60
#elif defined(__aarch64__)
static inline i64 sc6(i64 n, i64 a1, i64 a2, i64 a3, i64 a4, i64 a5, i64 a6)
{
register i64 x8 __asm__("x8") = n;
register i64 x0 __asm__("x0") = a1;
register i64 x1 __asm__("x1") = a2;
register i64 x2 __asm__("x2") = a3;
register i64 x3 __asm__("x3") = a4;
register i64 x4 __asm__("x4") = a5;
register i64 x5 __asm__("x5") = a6;
__asm__ __volatile__("svc #0" : "+r"(x0)
: "r"(x8), "r"(x1), "r"(x2), "r"(x3), "r"(x4), "r"(x5) : "memory");
return x0;
}
#define SYS_write 64
#define SYS_close 57
#define SYS_fstat 80
#define SYS_openat 56
#define SYS_newfstatat 79
#define SYS_exit 93
#endif
static i64 sc1(i64 n, i64 a) { return sc6(n, a, 0, 0, 0, 0, 0); }
static i64 sc2(i64 n, i64 a, i64 b) { return sc6(n, a, b, 0, 0, 0, 0); }
static i64 sc3(i64 n, i64 a, i64 b, i64 c) { return sc6(n, a, b, c, 0, 0, 0); }
static i64 sc4(i64 n, i64 a, i64 b, i64 c, i64 d) { return sc6(n, a, b, c, d, 0, 0); }
static void put(const char *s) { i64 n = 0; while (s[n]) n++; sc3(SYS_write, 1, (i64)s, n); }
static void putnum(i64 v)
{
char b[24]; int i = 24, neg = v < 0;
unsigned long u = neg ? (unsigned long)(-v) : (unsigned long)v;
if (v == 0) { put("0"); return; }
b[--i] = 0;
while (u) { b[--i] = '0' + (u % 10); u /= 10; }
if (neg) b[--i] = '-';
put(&b[i]);
}
static void show(const char *n, i64 r)
{
put(n); put(" = "); putnum(r);
if (r == -22) put(" [EINVAL]");
else if (r == -2) put(" [ENOENT]");
else if (r == -9) put(" [EBADF]");
put("\n");
}
void prog_main(void);
#if defined(__x86_64__)
__asm__(".globl _start\n_start:\n andq $-16, %rsp\n call prog_main\n"
" movl $60, %eax\n xorl %edi, %edi\n syscall\n");
#else
__asm__(".globl _start\n_start:\n bl prog_main\n mov x0, #0\n mov x8, #93\n svc #0\n");
#endif
void prog_main(void)
{
i64 fd;
long st[64];
put("=== probe5: empty-path stat workaround check ===\n");
fd = sc4(SYS_openat, -100, (i64)"/etc/profile", 0, 0);
show("openat(/etc/profile)", fd);
if (fd >= 0) {
show("fstat(fd)", sc2(SYS_fstat, fd, (i64)st));
show("newfstatat(fd, \"\", st, 0) [empty path]",
sc4(SYS_newfstatat, fd, (i64)"", (i64)st, 0));
show("newfstatat(fd, \"\", st, AT_EMPTY_PATH) [0x1000]",
sc4(SYS_newfstatat, fd, (i64)"", (i64)st, 0x1000));
sc1(SYS_close, fd);
}
/* also a directory fd, which is what some tools use */
fd = sc4(SYS_openat, -100, (i64)"/etc", 0, 0);
show("openat(/etc, RDONLY)", fd);
if (fd >= 0) {
show("newfstatat(dfd, \"\", st, AT_EMPTY_PATH)",
sc4(SYS_newfstatat, fd, (i64)"", (i64)st, 0x1000));
sc1(SYS_close, fd);
}
put("=== probe5 done ===\n");
sc1(SYS_exit, 0);
for (;;) {}
}
+45
View File
@@ -0,0 +1,45 @@
#!/bin/sh
# acceptance for the slimmed shim (arm64: utimensat only)
exec >/root/acc.log 2>&1
echo "### slimmed-shim acceptance ###"
echo "shim: $(ls -l /lib/libfix_seccomp.so)"
echo
echo "=== [1] DNS via NATIVE musl resolver (no override on arm64) ==="
S=$(date +%s); apk update; echo "apk_update_rc=$?"; E=$(date +%s); echo "elapsed=$((E-S))s"
wget -T 8 -O /dev/null http://example.com/ ; echo "wget_rc=$?"
echo
echo "=== [2] hardlink-heavy package, fresh cycle (utimensat fix) ==="
apk del tzdata >/dev/null 2>&1
apk add tzdata 2>&1 | head -8
echo "tzdata_rc=$?"
ls -l /usr/share/zoneinfo/Africa/Accra 2>&1
echo
echo "=== [3] package with trigger (fork/pipe/dup2 no longer overridden) ==="
apk del tree >/dev/null 2>&1
apk add tree; echo "tree_rc=$?"
echo
echo "=== [4] broken flags (expect none) ==="
awk '/^P:/{p=$0} /^f:/{print "BROKEN: "p" "$0}' /lib/apk/db/installed
echo "(end)"
echo
echo "=== [5] stat family still fine without the shim ==="
/root/probe5
echo
echo "=== [6] syscall baseline ==="
/root/probe3 | tail -12
echo
echo "=== [7] coreutils-ish sanity ==="
ls -l /usr/bin/tree /usr/share/zoneinfo/Asia/Shanghai 2>&1
sha256sum /etc/profile >/dev/null 2>&1 && echo "sha256sum ok"
echo "pipe test: $(echo hi | cat)"
echo "subshell: $( (echo nested) )"
echo "### acceptance done ###"