diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/FrameAcquireWorkerProbe.kt b/app/src/main/java/com/droiddeck/launcher/runtime/FrameAcquireWorkerProbe.kt index e33c4d041b..83122a7239 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/FrameAcquireWorkerProbe.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/FrameAcquireWorkerProbe.kt @@ -6,17 +6,20 @@ import android.system.Os import android.util.Log import com.droiddeck.launcher.wayland.WaylandCompositor import java.io.File +import java.nio.file.Files +import java.nio.file.LinkOption.NOFOLLOW_LINKS import java.security.MessageDigest /** Local paired fixture only, not a boundary against arbitrary code in the app's UID. */ internal object FrameAcquireWorkerProbe { + private const val SESSION_SCRIPT = "/usr/local/bin/droiddeck-session" private const val PATH = "/opt/droiddeck-agent/bin/gamescope" private const val TEST_INTENT = "DD_THOR_WORKER_ACQUIRE_TEST=1" private const val SHA = "09a56e234ac7605cae25ea0b69d02e2b04bc815bbb1a32bd5a61b64c11e88fdf" private const val SIZE = 4272072L private const val TTL_MS = 600_000L - internal data class Ticket(val owner: Int, val root: File, val binary: File, val expires: Long) + internal data class Ticket(val owner: Int, val root: File, val configuredRoot: File, val binary: File, val expires: Long) internal data class Qualified(val ticket: Ticket, val dev: Long, val ino: Long, val mtime: Long, val ctime: Long) private var intent: Ticket? = null private var nativeOwner = 0 @@ -39,20 +42,37 @@ internal object FrameAcquireWorkerProbe { !WaylandCompositor.frameWorkerProbeEnabled()) return reported.clear() intent = runCatching { - val root = LinuxRuntime.rootDir(context).canonicalFile + val configuredRoot = LinuxRuntime.rootDir(context).absoluteFile + val root = configuredRoot.canonicalFile val file = File(root.path + PATH) require(file.canonicalFile == file && file.isFile && file.length() == SIZE) - Ticket(owner, root, file, SystemClock.elapsedRealtime() + TTL_MS) + Ticket(owner, root, configuredRoot, file, SystemClock.elapsedRealtime() + TTL_MS) }.getOrNull() breadcrumb(Stage.ARM, if (intent == null) Reason.ARM_FILE else Reason.ENABLED) } - internal fun exactGamescopeArgs(argv: List): Boolean { + /** Native shebang handling can give Bash the staged host filename as its $0. */ + internal fun exactSteamScript(token: String, root: File, configuredRoot: File): Boolean { + if (token != SESSION_SCRIPT && token != root.path + SESSION_SCRIPT && + token != configuredRoot.path + SESSION_SCRIPT) return false + return runCatching { + val expected = File(root.path + SESSION_SCRIPT) + val supplied = if (token == SESSION_SCRIPT) expected else File(token) + // Reject symlinks within the authorized root, including a symlink replacing the script. + // Android's configured root itself may be an alias of its app-issued canonical root. + require(configuredRoot.canonicalFile == root && expected.canonicalFile == expected && + supplied.canonicalFile == expected && + Files.isRegularFile(expected.toPath(), NOFOLLOW_LINKS) && + Files.isRegularFile(supplied.toPath(), NOFOLLOW_LINKS)) + Files.isSameFile(supplied.toPath(), expected.toPath()) + }.getOrDefault(false) + } + + internal fun exactGamescopeArgs(argv: List, script: (String) -> Boolean): Boolean { if (argv.firstOrNull() !in listOf("gamescope", PATH)) return false val delimiter = argv.indexOf("--") - if (delimiter < 4 || argv.getOrNull(delimiter + 1) != "/usr/local/bin/droiddeck-session" || - argv.getOrNull(delimiter + 2) != "steam") return false - if (argv.size != delimiter + 3) return false + if (delimiter < 4 || argv.size != delimiter + 3 || argv[delimiter + 2] != "steam" || + !script(argv[delimiter + 1])) return false val opts = argv.subList(1, delimiter) if (opts.take(4) != listOf("--backend", "wayland", "--expose-wayland", "-f")) return false val dimensions = setOf("-W", "-H", "-w", "-h") @@ -87,7 +107,7 @@ internal object FrameAcquireWorkerProbe { SystemClock.elapsedRealtime() >= t.expires -> Reason.MATCH_EXPIRED file != PATH -> Reason.MATCH_PATH runCatching { root.canonicalFile }.getOrNull() != t.root -> Reason.MATCH_ROOT - !exactGamescopeArgs(argv) -> Reason.MATCH_ARGV + !exactGamescopeArgs(argv) { exactSteamScript(it, t.root, t.configuredRoot) } -> Reason.MATCH_ARGV else -> null } if (rejection != null) { diff --git a/app/src/test/java/com/droiddeck/launcher/runtime/FrameAcquireWorkerProbeTest.kt b/app/src/test/java/com/droiddeck/launcher/runtime/FrameAcquireWorkerProbeTest.kt new file mode 100644 index 0000000000..6f9b11a96f --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/runtime/FrameAcquireWorkerProbeTest.kt @@ -0,0 +1,59 @@ +package com.droiddeck.launcher.runtime + +import java.io.File +import java.nio.file.Files +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Test + +class FrameAcquireWorkerProbeTest { + @Test fun requiresExactSteamScriptInTheAppIssuedGuestRoot() { + val dir = Files.createTempDirectory("worker-probe-script").toFile().canonicalFile + try { + val root = File(dir, "root").apply { mkdir() } + val configured = File(dir, "configured") + Files.createSymbolicLink(configured.toPath(), root.toPath()) + val untrustedAlias = File(dir, "other-alias") + Files.createSymbolicLink(untrustedAlias.toPath(), root.toPath()) + val script = "/usr/local/bin/droiddeck-session" + val expected = File(root.path + script).apply { parentFile!!.mkdirs(); writeText("#!/bin/bash\n") } + val otherRoot = File(dir, "sibling") + File(otherRoot.path + script).apply { parentFile!!.mkdirs(); writeText("#!/bin/bash\n") } + val otherScript = File(expected.parentFile, "other-script").apply { writeText("#!/bin/bash\n") } + + val args = listOf("gamescope", "--backend", "wayland", "--expose-wayland", "-f", + "-W", "1280", "-H", "720", "-w", "1280", "-h", "720", "-r", "60", + "-e", "--xwayland-count", "2", "--default-touch-mode", "4", "--hdr-enabled", + "--", script, "steam") + fun valid(argv: List) = FrameAcquireWorkerProbe.exactGamescopeArgs(argv) { + FrameAcquireWorkerProbe.exactSteamScript(it, root, configured) + } + fun child(path: String) = args.dropLast(2) + listOf(path, "steam") + + assertTrue(valid(args)) + assertTrue(valid(child(root.path + script))) + assertTrue(valid(child(configured.path + script))) + for (path in listOf(otherRoot.path + script, untrustedAlias.path + script, + root.path + "/usr/local/bin/./droiddeck-session", root.path + "/../sibling" + script, + otherScript.path, "droiddeck-session")) { + assertFalse(valid(child(path))) + } + for (bad in listOf(listOf("gamescope", "--version"), args + "", + args + "steam://open/library", args.dropLast(1) + "desktop", + args.take(5) + "--unknown-flag" + args.drop(5), + args.take(5) + listOf("-W", "64") + args.drop(5))) { + assertFalse(valid(bad)) + } + + assertTrue(expected.delete()) + Files.createSymbolicLink(expected.toPath(), otherScript.toPath()) + assertFalse(valid(args)) + assertFalse(valid(child(configured.path + script))) + } finally { + // Don't follow the intentionally broken/foreign symlinks in this fixture. + Files.walk(dir.toPath()).use { paths -> + paths.sorted(Comparator.reverseOrder()).forEach { Files.deleteIfExists(it) } + } + } + } +}