fix(native): survive Android's seccomp filter in ld.so, libc and posix_spawn children

This commit is contained in:
Kurt Himebauch committed 2026-11-08 16:46:33 -05:00
1 parent 74f7bfd99f
commit 70adc2d0f6
6 files changed
+214 -1

No files matched your search

+23
View File
@@ -31,3 +31,26 @@ int shm_unlink(const char *name) {
}
#endif /* !DDNATV_HOST_TEST */
#ifndef DDNATV_HOST_TEST
// glibc's posix_spawn child resets every signal handler to its default, the hook's SIGSYS handler
// included, before it runs the file actions, and "close everything from N" is close_range(), which
// Android's seccomp filter answers with SIGSYS: the child died of it (Python's subprocess with
// close_fds does this). The descriptors that are open now are closed one by one instead.
int posix_spawn_file_actions_addclosefrom_np(posix_spawn_file_actions_t *actions, int from) {
if (!actions || from < 0) return EINVAL;
DIR *dir = opendir("/proc/self/fd");
if (!dir) return errno ? errno : ENOENT;
int self = dirfd(dir);
int ret = 0;
struct dirent *entry;
while (ret == 0 && (entry = readdir(dir)) != NULL) {
if (entry->d_name[0] < '0' || entry->d_name[0] > '9') continue;
int fd = atoi(entry->d_name);
if (fd < from || fd == self) continue;
ret = posix_spawn_file_actions_addclose(actions, fd);
}
closedir(dir);
return ret;
}
#endif
+51
View File
@@ -0,0 +1,51 @@
// Runs a program under a seccomp filter that answers the system calls Android's app policy blocks
// with SIGSYS, the way a stock app process does, for testing on builds that have no filter:
// seccomp-sim [-a NR,NR...] PROGRAM [ARGS...]
// -a leaves the listed system calls allowed (to find which one kills a program).
#define _GNU_SOURCE
#include <errno.h>
#include <linux/audit.h>
#include <linux/filter.h>
#include <linux/seccomp.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/prctl.h>
#include <unistd.h>
static const int blocked[] = {99, 100, 97, 217, 218, 219, 268, 277, 280, 293, 143, 144, 145, 146, 147, 149, 151, 152, 159};
int main(int argc, char **argv) {
int skip[64], nskip = 0, arg = 1;
if (argc > 3 && strcmp(argv[1], "-a") == 0) {
for (char *p = strtok(argv[2], ","); p && nskip < 64; p = strtok(NULL, ",")) skip[nskip++] = atoi(p);
arg = 3;
}
if (arg >= argc) return 2;
struct sock_filter filter[128];
int n = 0;
filter[n++] = (struct sock_filter)BPF_STMT(BPF_LD | BPF_W | BPF_ABS, offsetof(struct seccomp_data, arch));
filter[n++] = (struct sock_filter)BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, AUDIT_ARCH_AARCH64, 1, 0);
filter[n++] = (struct sock_filter)BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW);
filter[n++] = (struct sock_filter)BPF_STMT(BPF_LD | BPF_W | BPF_ABS, offsetof(struct seccomp_data, nr));
for (int i = 0; i < nskip; i++) {
filter[n++] = (struct sock_filter)BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, skip[i], 0, 1);
filter[n++] = (struct sock_filter)BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW);
}
filter[n++] = (struct sock_filter)BPF_JUMP(BPF_JMP | BPF_JGE | BPF_K, 424, 0, 1);
filter[n++] = (struct sock_filter)BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_TRAP);
for (size_t i = 0; i < sizeof(blocked) / sizeof(blocked[0]); i++) {
filter[n++] = (struct sock_filter)BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, blocked[i], 0, 1);
filter[n++] = (struct sock_filter)BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_TRAP);
}
filter[n++] = (struct sock_filter)BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW);
struct sock_fprog prog = { .len = (unsigned short)n, .filter = filter };
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) || prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog)) {
perror("seccomp-sim");
return 1;
}
execv(argv[arg], argv + arg);
perror("seccomp-sim: exec");
return 127;
}