diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/ElfSeccompPatch.kt b/app/src/main/java/com/droiddeck/launcher/runtime/ElfSeccompPatch.kt new file mode 100644 index 0000000000..22595b9524 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/runtime/ElfSeccompPatch.kt @@ -0,0 +1,81 @@ +package com.droiddeck.launcher.runtime + +import java.io.File +import java.nio.ByteBuffer +import java.nio.ByteOrder + +/** + * Android's app seccomp filter kills a process with SIGSYS for rseq, set_robust_list and clone3. glibc's + * dynamic linker makes the first two before any preloaded library (so before the hook's SIGSYS + * handler) exists, so the call sites in the rootfs's ld.so and libc answer by themselves instead: the + * `svc` after `mov x8, #nr` becomes a no-op for set_robust_list (glibc ignores the result) and + * `mov x0, #-ENOSYS` for rseq and clone3 (glibc falls back from both). Under proot the same + * answers are what proot gives. + */ +object ElfSeccompPatch { + private const val SVC = 0xd4000001.toInt() + private const val NOP = 0xd503201f.toInt() + private const val MOV_X0_ENOSYS = 0x928004a0.toInt() + private const val MOV_X8_SET_ROBUST_LIST = 0xd2800c68.toInt() + private const val MOV_X8_CLONE3 = 0xd2803668.toInt() + private const val MOV_X8_RSEQ = 0xd28024a8.toInt() + private const val WINDOW = 16 + + /** Patches [data] in place and returns the number of call sites changed. */ + fun patch(data: ByteArray): Int { + if (data.size < 64 || data[0] != 0x7f.toByte() || data[1] != 'E'.code.toByte() || data[4].toInt() != 2 || data[5].toInt() != 1) return 0 + val buffer = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN) + if (buffer.getShort(18).toInt() != 183) return 0 + val phoff = buffer.getLong(32).toInt() + val phentsize = buffer.getShort(54).toInt() and 0xffff + val phnum = buffer.getShort(56).toInt() and 0xffff + var patched = 0 + for (i in 0 until phnum) { + val at = phoff + i * phentsize + if (at + 56 > data.size) break + if (buffer.getInt(at) != 1 || (buffer.getInt(at + 4) and 1) == 0) continue + val start = ((buffer.getLong(at + 8).toInt()) + 3) and 3.inv() + val end = minOf(data.size, buffer.getLong(at + 8).toInt() + buffer.getLong(at + 32).toInt()) and 3.inv() + var pos = start + while (pos + 4 <= end) { + val replacement = when (buffer.getInt(pos)) { + MOV_X8_SET_ROBUST_LIST -> NOP + MOV_X8_CLONE3, MOV_X8_RSEQ -> MOV_X0_ENOSYS + else -> null + } + if (replacement != null) { + var next = pos + 4 + val limit = minOf(end, pos + 4 + WINDOW * 4) + while (next < limit) { + if (buffer.getInt(next) == SVC) { + buffer.putInt(next, replacement) + patched++ + break + } + next += 4 + } + } + pos += 4 + } + } + return patched + } + + /** Patches the file behind [path] (symlinks followed) if it still has unpatched call sites. */ + fun patchFile(path: File): Int { + val target = path.canonicalFile + if (!target.isFile) return 0 + val data = target.readBytes() + val count = patch(data) + if (count == 0) return 0 + val temp = File(target.parentFile, "." + target.name + ".seccomp") + temp.writeBytes(data) + temp.setExecutable(true, false) + temp.setReadable(true, false) + if (!temp.renameTo(target)) { + temp.delete() + throw java.io.IOException("could not replace ${target.path}") + } + return count + } +} 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 379dfaf69e..606b1970d1 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/NativeGuest.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/NativeGuest.kt @@ -23,6 +23,13 @@ object NativeGuest { fun enabled(context: Context): Boolean = 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. + */ + 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 hook(root: File): File = File(root, HOOK) fun loader(root: File): File? = @@ -46,7 +53,8 @@ object NativeGuest { fun command(root: File, program: String, args: List): List { val loader = checkNotNull(loader(root)) { "no glibc loader in ${root.path}" } val host = File(root, program.removePrefix("/")) - return listOf(loader.path, "--argv0", host.name, host.path) + args + val launch = listOf(loader.path, "--argv0", host.name, host.path) + args + return simulator(root)?.let { listOf(it.path) + launch } ?: launch } /** The environment of the session process: the hook's root, the mounts proot used to make, the preload list. */ @@ -87,6 +95,13 @@ object NativeGuest { stamp.writeText(signature) } + /** See [ElfSeccompPatch]: the linker and libc must survive Android's seccomp filter before the hook loads. */ + fun patchForSeccomp(root: File): Int = + listOf("usr/lib/ld-linux-aarch64.so.1", "usr/lib/aarch64-linux-gnu/ld-linux-aarch64.so.1", + "usr/lib/aarch64-linux-gnu/libc.so.6", "usr/lib/libc.so.6") + .map { File(root, it) }.filter { it.exists() } + .distinctBy { it.canonicalPath }.sumOf { runCatching { ElfSeccompPatch.patchFile(it) }.getOrDefault(0) } + private val HOST_LINK_TARGETS = listOf("/proc", "/dev", "/sys", "/sdcard", "/storage", "/data", "/system", "/vendor", "/apex", "/system_ext", "/product", "/odm", "/linkerconfig", "/mnt") diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt index ff7ad51a27..ae85d8e3ec 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt @@ -260,6 +260,8 @@ object SessionFiles { if (nativeUserland) { val relinked = com.droiddeck.launcher.runtime.NativeGuest.relinkAbsoluteSymlinks(context, root) if (relinked > 0) Log.i(TAG, "native userland: $relinked absolute symlinks made relative") + val patched = com.droiddeck.launcher.runtime.NativeGuest.patchForSeccomp(root) + if (patched > 0) Log.i(TAG, "native userland: $patched system call sites in ld.so and libc answer Android's blocked calls") com.droiddeck.launcher.runtime.NativeGuest.syncLibraryDirs(root) } if (ProotFastPath.enabled(context) && !nativeUserland) { diff --git a/app/src/test/java/com/droiddeck/launcher/runtime/ElfSeccompPatchTest.kt b/app/src/test/java/com/droiddeck/launcher/runtime/ElfSeccompPatchTest.kt new file mode 100644 index 0000000000..ec751d7397 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/runtime/ElfSeccompPatchTest.kt @@ -0,0 +1,41 @@ +package com.droiddeck.launcher.runtime + +import java.nio.ByteBuffer +import java.nio.ByteOrder +import org.junit.Assert.assertEquals +import org.junit.Test + +class ElfSeccompPatchTest { + private fun elfWith(vararg words: Int): ByteArray { + val data = ByteArray(0x200) + val b = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN) + b.put(0, 0x7f); b.put(1, 'E'.code.toByte()); b.put(2, 'L'.code.toByte()); b.put(3, 'F'.code.toByte()) + b.put(4, 2); b.put(5, 1) + b.putShort(18, 183) + b.putLong(32, 64) + b.putShort(54, 56); b.putShort(56, 1) + b.putInt(64, 1); b.putInt(68, 5) + b.putLong(72, 0x100); b.putLong(96, 0x100) + words.forEachIndexed { i, w -> b.putInt(0x100 + i * 4, w) } + return data + } + + @Test + fun theSvcAfterEachBlockedCallIsAnsweredInPlace() { + val svc = 0xd4000001.toInt() + val data = elfWith(0xd2800c68.toInt(), 0xaa0003e1.toInt(), svc, 0xd28024a8.toInt(), svc, 0xd2803668.toInt(), svc) + assertEquals(3, ElfSeccompPatch.patch(data)) + val b = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN) + assertEquals(0xd503201f.toInt(), b.getInt(0x108)) + assertEquals(0x928004a0.toInt(), b.getInt(0x110)) + assertEquals(0x928004a0.toInt(), b.getInt(0x118)) + assertEquals(0, ElfSeccompPatch.patch(data)) + } + + @Test + fun otherSystemCallsAreLeftAlone() { + val svc = 0xd4000001.toInt() + val data = elfWith(0xd2800ba8.toInt(), svc) + assertEquals(0, ElfSeccompPatch.patch(data)) + } +} diff --git a/tools/nativeguest/hook/posix_shm.c b/tools/nativeguest/hook/posix_shm.c index a53f5219ee..86364a5d1a 100644 --- a/tools/nativeguest/hook/posix_shm.c +++ b/tools/nativeguest/hook/posix_shm.c @@ -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 diff --git a/tools/nativeguest/seccomp-sim.c b/tools/nativeguest/seccomp-sim.c new file mode 100644 index 0000000000..941225933c --- /dev/null +++ b/tools/nativeguest/seccomp-sim.c @@ -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 +#include +#include +#include +#include +#include +#include +#include +#include +#include + +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; +}