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

@@ -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
}
}
@@ -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<String>): List<String> {
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")
@@ -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) {
@@ -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))
}
}