From a9e8a01bb6bb700d93edd1a5a53651ef73c4231f Mon Sep 17 00:00:00 2001 From: MaxsTechReview Date: Sun, 29 Nov 2026 19:44:11 -0500 Subject: [PATCH] fix(native): prepare the rootfs before a helper starts outside a session (#3447) Prepare the rootfs before a native program starts outside a session The linker and libc are patched for Android's seccomp filter only when a session starts. A helper started between sessions, after a runtime update and before the first session, ran the unpatched linker and died of SIGSYS (exit code 159). The shared native process builder now prepares the rootfs itself when no session is running. --- .../droiddeck/launcher/runtime/NativeGuest.kt | 7 +- .../runtime/NativeGuestPrepareTest.kt | 79 +++++++++++++++++++ 2 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 app/src/test/java/com/droiddeck/launcher/runtime/NativeGuestPrepareTest.kt 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 49541416eb..babaae550a 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/NativeGuest.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/NativeGuest.kt @@ -112,8 +112,9 @@ object NativeGuest { File(BwrapSpawner.hostPath(binds.map { BwrapSpawner.Bind.parse(it) }, root.path, cwd)).takeIf { it.isDirectory } ?: root /** A process for [guest] (argv) started as proot would with [binds], in guest directory [cwd]. */ - fun processBuilder(context: Context, root: File, binds: List, guest: List, cwd: String = "/root"): ProcessBuilder = - ProcessBuilder(command(root, guest.first(), guest.drop(1))) + fun processBuilder(context: Context, root: File, binds: List, guest: List, cwd: String = "/root"): ProcessBuilder { + if (sessionMode == null) prepare(context, root) + return ProcessBuilder(command(root, guest.first(), guest.drop(1))) .directory(workingDirectory(root, binds, cwd)) .also { builder -> builder.environment().apply { @@ -121,6 +122,7 @@ object NativeGuest { putAll(sessionEnvironment(root, binds, guest.first(), SessionPrefs.guestHostname(context))) } } + } private const val RUNPATH_CONF = "etc/ld.so.conf.d/zz-droiddeck-native.conf" private val SCAN_DIRS = listOf("usr/lib", "usr/local/lib", "opt") @@ -149,6 +151,7 @@ object NativeGuest { } /** What a rootfs needs before anything runs in it natively; cheap once done. */ + @Synchronized fun prepare(context: Context, root: File) { val relinked = relinkAbsoluteSymlinks(context, root) if (relinked > 0) android.util.Log.i(TAG, "native userland: $relinked absolute symlinks made relative") diff --git a/app/src/test/java/com/droiddeck/launcher/runtime/NativeGuestPrepareTest.kt b/app/src/test/java/com/droiddeck/launcher/runtime/NativeGuestPrepareTest.kt new file mode 100644 index 0000000000..e5bc975892 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/runtime/NativeGuestPrepareTest.kt @@ -0,0 +1,79 @@ +package com.droiddeck.launcher.runtime + +import androidx.test.core.app.ApplicationProvider +import java.io.File +import java.nio.ByteBuffer +import java.nio.ByteOrder +import java.nio.file.Files +import org.junit.After +import org.junit.Assert.assertArrayEquals +import org.junit.Assert.assertEquals +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner + +@RunWith(RobolectricTestRunner::class) +class NativeGuestPrepareTest { + private val svc = 0xd4000001.toInt() + private val rseq = 0xd28024a8.toInt() + private val enosys = 0x928004a0.toInt() + + private fun unpatchedLoader(): 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) + b.putInt(0x100, rseq); b.putInt(0x104, svc) + return data + } + + private fun rootWithUnpatchedLoader(): Pair { + val root = Files.createTempDirectory("native-prepare").toFile() + val loader = File(root, "usr/lib/ld-linux-aarch64.so.1") + loader.parentFile!!.mkdirs() + loader.writeBytes(unpatchedLoader()) + return root to loader + } + + private fun instructionAfterRseq(loader: File): Int = + ByteBuffer.wrap(loader.readBytes()).order(ByteOrder.LITTLE_ENDIAN).getInt(0x104) + + @After + fun endSession() { + NativeGuest.sessionMode = null + } + + @Test + fun aProgramStartedOutsideASessionGetsALoaderThatSurvivesSeccomp() { + val (root, loader) = rootWithUnpatchedLoader() + NativeGuest.sessionMode = null + NativeGuest.processBuilder(ApplicationProvider.getApplicationContext(), root, emptyList(), listOf("/usr/bin/true")) + assertEquals(enosys, instructionAfterRseq(loader)) + } + + @Test + fun aProgramStartedInARunningSessionLeavesTheRootfsAsTheSessionPreparedIt() { + val (root, loader) = rootWithUnpatchedLoader() + val before = loader.readBytes() + NativeGuest.sessionMode = true + NativeGuest.processBuilder(ApplicationProvider.getApplicationContext(), root, emptyList(), listOf("/usr/bin/true")) + assertArrayEquals(before, loader.readBytes()) + } + + @Test + fun anAlreadyPreparedLoaderIsNotRewritten() { + val (root, loader) = rootWithUnpatchedLoader() + NativeGuest.processBuilder(ApplicationProvider.getApplicationContext(), root, emptyList(), listOf("/usr/bin/true")) + val modified = loader.lastModified() + val bytes = loader.readBytes() + loader.setLastModified(modified - 10_000) + NativeGuest.processBuilder(ApplicationProvider.getApplicationContext(), root, emptyList(), listOf("/usr/bin/true")) + assertArrayEquals(bytes, loader.readBytes()) + assertEquals(modified - 10_000, loader.lastModified()) + } +}