diff --git a/app/src/main/cpp/waylandcomp/src/compositor.c b/app/src/main/cpp/waylandcomp/src/compositor.c index fe6bd62bc0..1c9992f735 100644 --- a/app/src/main/cpp/waylandcomp/src/compositor.c +++ b/app/src/main/cpp/waylandcomp/src/compositor.c @@ -1733,6 +1733,7 @@ static pthread_mutex_t frame_worker_lock = PTHREAD_MUTEX_INITIALIZER; static char frame_worker_file[PATH_MAX]; static struct dd_worker_grant frame_worker_grant; static int frame_worker_pidfd = -1; +static bool frame_worker_map_reported; static uint64_t frame_worker_starttime(pid_t pid) { char proc[64], line[4096]; @@ -1775,14 +1776,21 @@ int droiddeck_frame_worker_grant(int owner, pid_t pid, uint64_t expires, const c if (owner <= 0 || pid <= 1 || !path || strlen(path) >= sizeof(frame_worker_file) || lstat(path, &st) < 0 || !S_ISREG(st.st_mode) || (uint64_t)st.st_dev != expected_dev || (uint64_t)st.st_ino != expected_ino || - st.st_uid != getuid()) return 0; + st.st_uid != getuid()) { + droiddeck_log("probe", "worker_admission=NATIVE_FILE"); + return 0; + } int pidfd = syscall(__NR_pidfd_open, pid, 0); if (pidfd < 0) { - droiddeck_log("probe", "frame acquire worker pidfd unavailable: %s", strerror(errno)); + droiddeck_log("probe", "worker_admission=NATIVE_PIDFD"); return 0; // No fallback to a recyclable PID. } uint64_t start = frame_worker_starttime(pid); - if (!start) { close(pidfd); return 0; } + if (!start) { + droiddeck_log("probe", "worker_admission=NATIVE_STARTTIME"); + close(pidfd); + return 0; + } pthread_mutex_lock(&frame_worker_lock); if (frame_worker_pidfd < 0) { strcpy(frame_worker_file, path); @@ -1791,21 +1799,28 @@ int droiddeck_frame_worker_grant(int owner, pid_t pid, uint64_t expires, const c .dev_major = major(st.st_dev), .dev_minor = minor(st.st_dev), .ino = st.st_ino, .canonical_file = frame_worker_file, .app_launch_verified = true, .final_go = true, }; + frame_worker_map_reported = false; frame_worker_pidfd = pidfd; pidfd = -1; } pthread_mutex_unlock(&frame_worker_lock); - if (pidfd >= 0) { close(pidfd); return 0; } + if (pidfd >= 0) { + droiddeck_log("probe", "worker_admission=NATIVE_ALREADY_RESERVED"); + close(pidfd); + return 0; + } droiddeck_log("probe", "frame acquire reserved one app-verified worker"); return 1; } static bool frame_worker_verified_exec_locked(const struct dd_worker_grant *g, uint64_t now) { char proc[64], line[PATH_MAX + 128]; - if (frame_worker_starttime(g->pid) != g->start_ticks) return false; + const char *failure = "worker_admission=MAP_STARTTIME"; + if (frame_worker_starttime(g->pid) != g->start_ticks) goto reject; snprintf(proc, sizeof(proc), "/proc/%u/maps", g->pid); FILE *f = fopen(proc, "re"); - if (!f) return false; + failure = "worker_admission=MAP_OPEN"; + if (!f) goto reject; bool matched = false; size_t budget = 2 * 1024 * 1024; while (budget && fgets(line, sizeof(line), f)) { @@ -1818,7 +1833,14 @@ static bool frame_worker_verified_exec_locked(const struct dd_worker_grant *g, u } } fclose(f); - return matched; + if (matched) return true; + failure = "worker_admission=MAP_IDENTITY"; +reject: + if (!frame_worker_map_reported) { + frame_worker_map_reported = true; + droiddeck_log("probe", "%s", failure); + } + return false; } static int frame_acquire_peer_allowed(const struct wl_client *client, bool verify_exec) { 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 67818ffa60..e33c4d041b 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/FrameAcquireWorkerProbe.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/FrameAcquireWorkerProbe.kt @@ -3,6 +3,7 @@ package com.droiddeck.launcher.runtime import android.content.Context import android.os.SystemClock import android.system.Os +import android.util.Log import com.droiddeck.launcher.wayland.WaylandCompositor import java.io.File import java.security.MessageDigest @@ -21,16 +22,29 @@ internal object FrameAcquireWorkerProbe { private var nativeOwner = 0 private var nativePid = 0 + private enum class Stage { ARM, MATCH, PUBLISH } + private enum class Reason { + ENABLED, ARM_FILE, MATCH_ACK, MATCH_OWNER, MATCH_EXPIRED, MATCH_PATH, MATCH_ROOT, + MATCH_ARGV, MATCH_FILE, MATCH_DIGEST, QUALIFIED, PUBLISH_TICKET, PUBLISH_RESTAT, + PUBLISH_NATIVE, RESERVED + } + private val reported = mutableSetOf() + @Synchronized private fun breadcrumb(stage: Stage, reason: Reason) { + if (reported.add(stage)) Log.i("FrameAcquireWorkerProbe", "worker_admission=" + reason.name) + } + @Synchronized fun arm(context: Context, owner: Int, agentEnvironment: List) { intent = null if (owner <= 0 || TEST_INTENT !in agentEnvironment || !WaylandCompositor.frameWorkerProbeEnabled()) return + reported.clear() intent = runCatching { val root = LinuxRuntime.rootDir(context).canonicalFile val file = File(root.path + PATH) require(file.canonicalFile == file && file.isFile && file.length() == SIZE) Ticket(owner, root, file, SystemClock.elapsedRealtime() + TTL_MS) }.getOrNull() + breadcrumb(Stage.ARM, if (intent == null) Reason.ARM_FILE else Reason.ENABLED) } internal fun exactGamescopeArgs(argv: List): Boolean { @@ -64,11 +78,24 @@ internal object FrameAcquireWorkerProbe { fun match(owner: Int, root: File, file: String, argv: List, ack: Boolean): Qualified? { val t = synchronized(this) { intent } ?: return null - if (!ack || t.owner != owner || SystemClock.elapsedRealtime() >= t.expires || file != PATH || - runCatching { root.canonicalFile }.getOrNull() != t.root) return null - if (!exactGamescopeArgs(argv)) return null + // Startup's --version worker and unrelated workers must not consume the one diagnostic. + val candidate = "--version" !in argv && (file.substringAfterLast('/') == "gamescope" || + argv.firstOrNull()?.substringAfterLast('/') == "gamescope") + val rejection = when { + !ack -> Reason.MATCH_ACK + t.owner != owner -> Reason.MATCH_OWNER + 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 + else -> null + } + if (rejection != null) { + if (candidate) breadcrumb(Stage.MATCH, rejection) + return null + } // Hash outside the worker handshake and authorizing lock. - return runCatching { + val qualified = runCatching { require(t.binary.canonicalFile == t.binary) val before = Os.stat(t.binary.path) require(before.st_size == SIZE) @@ -85,25 +112,35 @@ internal object FrameAcquireWorkerProbe { require(before.st_dev == after.st_dev && before.st_ino == after.st_ino && before.st_size == after.st_size && before.st_mtime == after.st_mtime && before.st_ctime == after.st_ctime) - require(digest.digest().joinToString("") { "%02x".format(it.toInt() and 255) } == SHA) + if (digest.digest().joinToString("") { "%02x".format(it.toInt() and 255) } != SHA) { + breadcrumb(Stage.MATCH, Reason.MATCH_DIGEST) + return null + } Qualified(t, after.st_dev, after.st_ino, after.st_mtime, after.st_ctime) }.getOrNull() + breadcrumb(Stage.MATCH, if (qualified == null) Reason.MATCH_FILE else Reason.QUALIFIED) + return qualified } /** Runs just before the final GO byte. Failure never changes ordinary launch. */ @Synchronized fun publish(q: Qualified, pid: Int) { val t = q.ticket - if (intent !== t || pid <= 1 || SystemClock.elapsedRealtime() >= t.expires) return + if (intent !== t || pid <= 1 || SystemClock.elapsedRealtime() >= t.expires) { + if (intent === t) breadcrumb(Stage.PUBLISH, Reason.PUBLISH_TICKET) + return + } intent = null // At most one candidate per explicitly armed session. - runCatching { + val status = runCatching { val st = Os.lstat(t.binary.path) require(st.st_size == SIZE && st.st_dev == q.dev && st.st_ino == q.ino && st.st_mtime == q.mtime && st.st_ctime == q.ctime) if (WaylandCompositor.frameWorkerProbeGrant(t.owner, pid, t.expires, t.binary.path, st.st_dev, st.st_ino)) { nativeOwner = t.owner nativePid = pid - } - } + Reason.RESERVED + } else Reason.PUBLISH_NATIVE + }.getOrDefault(Reason.PUBLISH_RESTAT) + breadcrumb(Stage.PUBLISH, status) } @Synchronized fun revoke(owner: Int, pid: Int = 0) { diff --git a/tools/tests/test_frame_acquire_peer_gate.py b/tools/tests/test_frame_acquire_peer_gate.py index 082b3a35d8..2dd0388ee7 100644 --- a/tools/tests/test_frame_acquire_peer_gate.py +++ b/tools/tests/test_frame_acquire_peer_gate.py @@ -41,3 +41,26 @@ class FrameAcquirePeerGateTest(unittest.TestCase): bind=comp.split('static void bind_frame_acquire',1)[1].split('#endif',1)[0] self.assertIn('frame_acquire_peer_allowed(client, true)',bind) self.assertIn('poll(&alive, 1, 0) == 0',comp) + + + def test_admission_diagnostics_are_coarse_bounded_and_probe_only(self): + kt=(JAVA/'runtime/FrameAcquireWorkerProbe.kt').read_text() + comp=(SRC/'compositor.c').read_text() + jni=(SRC/'waylandcomp_jni.c').read_text() + arm=kt.split('fun arm(',1)[1].split('fun exactGamescopeArgs',1)[0] + match=kt.split('fun match(',1)[1].split('fun publish(',1)[0] + self.assertLess(arm.index('!WaylandCompositor.frameWorkerProbeEnabled()'), + arm.index('breadcrumb(Stage.ARM')) + self.assertLess(match.index('?: return null'),match.index('breadcrumb(')) + self.assertIn('"--version" !in argv',match) + self.assertIn('if (reported.add(stage))',kt) + self.assertIn('"worker_admission=" + reason.name',kt) + for name in ('ARM_FILE','MATCH_ACK','MATCH_OWNER','MATCH_EXPIRED','MATCH_PATH','MATCH_ROOT', + 'MATCH_ARGV','MATCH_FILE','MATCH_DIGEST','QUALIFIED','PUBLISH_TICKET', + 'PUBLISH_RESTAT','PUBLISH_NATIVE','RESERVED'): + self.assertIn('Reason.'+name,kt) + self.assertIn(' return JNI_FALSE;\n#endif',jni.split('frameWorkerProbeEnabled(',1)[1]) + self.assertIn('if (!frame_worker_map_reported)',comp) + for name in ('NATIVE_FILE','NATIVE_PIDFD','NATIVE_STARTTIME','NATIVE_ALREADY_RESERVED', + 'MAP_STARTTIME','MAP_OPEN','MAP_IDENTITY'): + self.assertIn('"worker_admission='+name+'"',comp)