diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/BwrapSpawner.kt b/app/src/main/java/com/droiddeck/launcher/runtime/BwrapSpawner.kt index 90eb97642..aa8dad033 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/BwrapSpawner.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/BwrapSpawner.kt @@ -415,7 +415,7 @@ object BwrapSpawner { "LD_LIBRARY_PATH" to SANDBOX_LIB_DIRS.map(::host).filter { it.isDirectory }.joinToString(":") { it.path }, "LD_PRELOAD" to hook.path, ).filterValues { it.isNotEmpty() } - return (NativeGuest.simulator(rootfs)?.let { listOf(it.path) + launch } ?: launch) to env + return (NativeGuest.simulator(rootfs)?.let { it + launch } ?: launch) to env } private val runpathDirs = java.util.concurrent.ConcurrentHashMap>() diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/NativeGuest.kt b/app/src/main/java/com/droiddeck/launcher/runtime/NativeGuest.kt index a90d10673..71c686c38 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/NativeGuest.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/NativeGuest.kt @@ -25,11 +25,18 @@ object NativeGuest { SessionPrefs.nativeUserland(context) && available(LinuxRuntime.rootDir(context)) /** - * Development switch: a guest path in etc/droiddeck/seccomp-sim names a helper that applies Android's - * app seccomp filter before the program starts, for devices (eng builds) that run apps without one. + * Development switch: etc/droiddeck/seccomp-sim names a helper (a guest path, then its arguments) + * that applies Android's app seccomp filter before the program starts, for devices that run apps + * without one (eng builds, or a permissive SELinux, where zygote skips it). Guest paths among the + * arguments are given as their places in the rootfs, as the helper runs before the hook. */ - fun simulator(root: File): File? = File(root, "etc/droiddeck/seccomp-sim").takeIf { it.isFile } - ?.readText()?.trim()?.takeIf { it.startsWith("/") }?.let { File(root, it.removePrefix("/")) }?.takeIf { it.isFile } + fun simulator(root: File): List? { + val words = File(root, "etc/droiddeck/seccomp-sim").takeIf { it.isFile }?.readText()?.trim() + ?.split(Regex("\\s+"))?.filter { it.isNotEmpty() } ?: return null + val helper = words.firstOrNull()?.takeIf { it.startsWith("/") }?.let { File(root, it.removePrefix("/")) } + ?.takeIf { it.isFile } ?: return null + return listOf(helper.path) + words.drop(1).map { if (it.startsWith("/")) File(root, it.removePrefix("/")).path else it } + } fun hook(root: File): File = File(root, HOOK) @@ -55,7 +62,7 @@ object NativeGuest { val loader = checkNotNull(loader(root)) { "no glibc loader in ${root.path}" } val host = File(root, program.removePrefix("/")) val launch = listOf(loader.path, "--argv0", host.name, host.path) + args - return simulator(root)?.let { listOf(it.path) + launch } ?: launch + return simulator(root)?.let { it + launch } ?: launch } /** The environment of the session process: the hook's root, the mounts proot used to make, the preload list. */ diff --git a/app/src/test/java/com/droiddeck/launcher/runtime/NativeGuestTest.kt b/app/src/test/java/com/droiddeck/launcher/runtime/NativeGuestTest.kt index ed28f325e..3188eb4e6 100644 --- a/app/src/test/java/com/droiddeck/launcher/runtime/NativeGuestTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/runtime/NativeGuestTest.kt @@ -12,6 +12,20 @@ class NativeGuestTest { File(it, "usr/lib/ld-linux-aarch64.so.1").writeText("") } + @Test + fun theSeccompSwitchNamesAHelperAndItsArgumentsInTheRootfs() { + val root = Files.createTempDirectory("rootfs").toFile() + assertEquals(null, NativeGuest.simulator(root)) + File(root, "usr/local/bin").mkdirs() + File(root, "usr/local/bin/seccomp-sim").writeText("") + File(root, "etc/droiddeck").mkdirs() + File(root, "etc/droiddeck/seccomp-sim").writeText("/usr/local/bin/seccomp-sim -f /etc/droiddeck/app.bpf\n") + assertEquals(listOf(File(root, "usr/local/bin/seccomp-sim").path, "-f", File(root, "etc/droiddeck/app.bpf").path), + NativeGuest.simulator(root)) + File(root, "etc/droiddeck/seccomp-sim").writeText("/usr/local/bin/missing") + assertEquals(null, NativeGuest.simulator(root)) + } + @Test fun aWorkingDirectoryInAMountIsItsSource() { val root = Files.createTempDirectory("rootfs").toFile() diff --git a/tools/nativeguest/app-seccomp-dump.c b/tools/nativeguest/app-seccomp-dump.c new file mode 100644 index 000000000..1ec09ea25 --- /dev/null +++ b/tools/nativeguest/app-seccomp-dump.c @@ -0,0 +1,50 @@ +// Writes the seccomp filter Android's zygote gives an app (from the device's libandroid_runtime.so) +// to a file, as the raw sock_filter array, for seccomp-sim -f on devices whose zygote skips it +// (a permissive SELinux). A bionic program, built with the NDK and run from adb shell: +// aarch64-linux-android33-clang -O2 -rdynamic -o app-seccomp-dump app-seccomp-dump.c -ldl +// app-seccomp-dump OUT +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static const char *g_out; +static int g_dumped; + +// the runtime's own prctl() call lands here first +__attribute__((visibility("default"))) int prctl(int option, ...) { + va_list ap; + va_start(ap, option); + unsigned long a2 = va_arg(ap, unsigned long), a3 = va_arg(ap, unsigned long); + unsigned long a4 = va_arg(ap, unsigned long), a5 = va_arg(ap, unsigned long); + va_end(ap); + if (option == PR_SET_SECCOMP && a2 == SECCOMP_MODE_FILTER && g_out) { + const struct sock_fprog *prog = (const struct sock_fprog *)a3; + FILE *f = fopen(g_out, "wb"); + if (f) { + fwrite(prog->filter, sizeof(prog->filter[0]), prog->len, f); + fclose(f); + fprintf(stderr, "app-seccomp-dump: %u instructions to %s\n", prog->len, g_out); + g_dumped = 1; + } + return 0; // not installed in this process + } + return (int)syscall(SYS_prctl, option, a2, a3, a4, a5); +} + +int main(int argc, char **argv) { + if (argc != 2) { fprintf(stderr, "usage: %s OUT\n", argv[0]); return 2; } + g_out = argv[1]; + void *rt = dlopen("libandroid_runtime.so", RTLD_NOW); + if (!rt) { fprintf(stderr, "%s\n", dlerror()); return 2; } + int (*set_app_filter)(void) = (int (*)(void))dlsym(rt, "_Z22set_app_seccomp_filterv"); + if (!set_app_filter) { fprintf(stderr, "no set_app_seccomp_filter\n"); return 2; } + set_app_filter(); + return g_dumped ? 0 : 1; +} diff --git a/tools/nativeguest/seccomp-sim.c b/tools/nativeguest/seccomp-sim.c index 079782a69..e1b50a09c 100644 --- a/tools/nativeguest/seccomp-sim.c +++ b/tools/nativeguest/seccomp-sim.c @@ -1,7 +1,9 @@ // 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). +// seccomp-sim -f FILTER PROGRAM [ARGS...] +// -a leaves the listed system calls allowed (to find which one kills a program). -f installs a +// filter as it is (raw struct sock_filter entries), such as the app filter a device's zygote makes. #define _GNU_SOURCE #include #include @@ -19,7 +21,32 @@ static const int blocked[] = {99, 100, 97, 217, 218, 219, 268, 277, 280, 293, 14 #define MAX_ALLOWED 64 #define NBLOCKED (sizeof(blocked) / sizeof(blocked[0])) +static int run_filter_file(const char *path, char **argv) { + static struct sock_filter filter[BPF_MAXINSNS]; + FILE *f = fopen(path, "rb"); + if (!f) { + perror("seccomp-sim: -f"); + return 2; + } + size_t n = fread(filter, sizeof(filter[0]), BPF_MAXINSNS, f); + int extra = fgetc(f) != EOF; + fclose(f); + if (n == 0 || extra) { + fprintf(stderr, "seccomp-sim: %s holds no filter of at most %d instructions\n", path, BPF_MAXINSNS); + return 2; + } + 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[0], argv); + perror("seccomp-sim: exec"); + return 127; +} + int main(int argc, char **argv) { + if (argc > 3 && strcmp(argv[1], "-f") == 0) return run_filter_file(argv[2], argv + 3); int skip[MAX_ALLOWED], nskip = 0, arg = 1; if (argc > 3 && strcmp(argv[1], "-a") == 0) { for (char *p = argv[2];; p++) {