mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-06 04:55:44 +00:00
Authorize the paired compositor probe through its owned worker
This commit is contained in:
1 parent
3b274746fb
commit
dda7301a4c
13 files changed
+442
-129
No files matched your search
@@ -648,7 +648,7 @@ jobs:
|
||||
python3 tools/release/check_session_assets.py app/build/outputs/apk/release/app-release.apk
|
||||
python3 tools/release/runtime_inputs.py apk app/build/outputs/apk/release/app-release.apk
|
||||
unzip -p app/build/outputs/apk/release/app-release.apk lib/arm64-v8a/libdroiddeckwayland.so > "$RUNNER_TEMP/compositor-probe.so"
|
||||
LC_ALL=C grep -aFq 'debug.droiddeck.frame_acquire_pid' "$RUNNER_TEMP/compositor-probe.so"
|
||||
LC_ALL=C grep -aFq 'frame acquire reserved one app-verified worker' "$RUNNER_TEMP/compositor-probe.so"
|
||||
rm "$RUNNER_TEMP/compositor-probe.so"
|
||||
|
||||
- name: Check the store engines' library
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
allocation. Binding does not opt the client out of ordinary wl_buffer
|
||||
release or existing synchronization protocols. The server advertises it
|
||||
only when it implements all semantics for the selected display path and
|
||||
only to the test fixture's designated same-UID Gamescope peer.
|
||||
only to the test fixture's app-authorized Gamescope worker.
|
||||
</description>
|
||||
<request name="destroy" type="destructor"/>
|
||||
<enum name="error">
|
||||
|
||||
@@ -26,7 +26,10 @@
|
||||
#include "frame_acquire.h"
|
||||
#ifdef DROIDDECK_FRAME_ACQUIRE_PROBE
|
||||
#include "droiddeck-frame-acquire-v1-server-protocol.h"
|
||||
#include <sys/system_properties.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <sys/sysmacros.h>
|
||||
#include <sys/stat.h>
|
||||
#include "frame_worker_peer.h"
|
||||
#endif
|
||||
#include "display_buffers.h"
|
||||
#include "viewporter-server-protocol.h"
|
||||
@@ -1725,30 +1728,119 @@ int droiddeck_surface_dup_acquire(const struct surface *s, const struct dmabuf_b
|
||||
}
|
||||
|
||||
#ifdef DROIDDECK_FRAME_ACQUIRE_PROBE
|
||||
// Private, signed test fixture only. The shell designates a single verified
|
||||
// pre-exec Gamescope PID; same-UID clients and all other Wayland globals keep
|
||||
// their ordinary behavior but cannot bind (or use) this protocol.
|
||||
static int frame_acquire_peer_allowed(const struct wl_client *client) {
|
||||
char value[PROP_VALUE_MAX] = {0};
|
||||
int len = __system_property_get("debug.droiddeck.frame_acquire_pid", value);
|
||||
if (len <= 0 || len >= (int)sizeof(value) || value[0] < '1' || value[0] > '9')
|
||||
return 0;
|
||||
int wanted = 0;
|
||||
for (int i = 0; i < len; i++) {
|
||||
int digit = value[i] - '0';
|
||||
if (digit < 0 || digit > 9 || wanted > (INT_MAX - digit) / 10)
|
||||
return 0;
|
||||
wanted = wanted * 10 + digit;
|
||||
// Private signed fixture: only the app-owned worker decision may issue a grant.
|
||||
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 uint64_t frame_worker_starttime(pid_t pid) {
|
||||
char proc[64], line[4096];
|
||||
snprintf(proc, sizeof(proc), "/proc/%d/stat", pid);
|
||||
FILE *f = fopen(proc, "re");
|
||||
if (!f) return 0;
|
||||
bool read = fgets(line, sizeof(line), f) != NULL;
|
||||
fclose(f);
|
||||
if (!read) return 0;
|
||||
char *p = strrchr(line, ')');
|
||||
if (!p || p[1] != ' ') return 0;
|
||||
p += 2;
|
||||
// "state" is field 3, the kernel starttime is field 22.
|
||||
for (int field = 3; field < 22; ++field) {
|
||||
p = strchr(p, ' ');
|
||||
if (!p) return 0;
|
||||
while (*p == ' ') ++p;
|
||||
}
|
||||
pid_t peer_pid = 0;
|
||||
uid_t peer_uid = (uid_t)-1;
|
||||
wl_client_get_credentials(client, &peer_pid, &peer_uid, NULL);
|
||||
return wanted > 1 && peer_pid == wanted && peer_uid == getuid();
|
||||
char *end;
|
||||
errno = 0;
|
||||
if (*p < '0' || *p > '9') return 0;
|
||||
uint64_t start = strtoull(p, &end, 10);
|
||||
return !errno && end != p && *end == ' ' ? start : 0;
|
||||
}
|
||||
|
||||
void droiddeck_frame_worker_revoke(int owner, int pid) {
|
||||
pthread_mutex_lock(&frame_worker_lock);
|
||||
if (frame_worker_grant.owner == (uint32_t)owner &&
|
||||
(!pid || frame_worker_grant.pid == (uint32_t)pid)) {
|
||||
if (frame_worker_pidfd >= 0) close(frame_worker_pidfd);
|
||||
frame_worker_pidfd = -1;
|
||||
frame_worker_grant = (struct dd_worker_grant){0};
|
||||
}
|
||||
pthread_mutex_unlock(&frame_worker_lock);
|
||||
}
|
||||
|
||||
int droiddeck_frame_worker_grant(int owner, pid_t pid, uint64_t expires, const char *path,
|
||||
uint64_t expected_dev, uint64_t expected_ino) {
|
||||
struct stat st;
|
||||
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;
|
||||
int pidfd = syscall(__NR_pidfd_open, pid, 0);
|
||||
if (pidfd < 0) {
|
||||
droiddeck_log("probe", "frame acquire worker pidfd unavailable: %s", strerror(errno));
|
||||
return 0; // No fallback to a recyclable PID.
|
||||
}
|
||||
uint64_t start = frame_worker_starttime(pid);
|
||||
if (!start) { close(pidfd); return 0; }
|
||||
pthread_mutex_lock(&frame_worker_lock);
|
||||
if (frame_worker_pidfd < 0) {
|
||||
strcpy(frame_worker_file, path);
|
||||
frame_worker_grant = (struct dd_worker_grant){
|
||||
.owner = owner, .uid = getuid(), .pid = pid, .start_ticks = start, .expires = expires,
|
||||
.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_pidfd = pidfd;
|
||||
pidfd = -1;
|
||||
}
|
||||
pthread_mutex_unlock(&frame_worker_lock);
|
||||
if (pidfd >= 0) { 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;
|
||||
snprintf(proc, sizeof(proc), "/proc/%u/maps", g->pid);
|
||||
FILE *f = fopen(proc, "re");
|
||||
if (!f) return false;
|
||||
bool matched = false;
|
||||
size_t budget = 2 * 1024 * 1024;
|
||||
while (budget && fgets(line, sizeof(line), f)) {
|
||||
size_t n = strlen(line);
|
||||
if (!n || line[n - 1] != '\n' || n > budget) break;
|
||||
budget -= n;
|
||||
if (dd_worker_grant_matches(g, g->owner, g->uid, g->pid, g->start_ticks, now, line)) {
|
||||
matched = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
fclose(f);
|
||||
return matched;
|
||||
}
|
||||
|
||||
static int frame_acquire_peer_allowed(const struct wl_client *client, bool verify_exec) {
|
||||
pid_t pid = 0;
|
||||
uid_t uid = (uid_t)-1;
|
||||
wl_client_get_credentials(client, &pid, &uid, NULL);
|
||||
struct timespec t;
|
||||
if (clock_gettime(CLOCK_BOOTTIME, &t) < 0) return 0;
|
||||
uint64_t now = (uint64_t)t.tv_sec * 1000 + (uint64_t)t.tv_nsec / 1000000;
|
||||
pthread_mutex_lock(&frame_worker_lock);
|
||||
const struct dd_worker_grant *g = &frame_worker_grant;
|
||||
struct pollfd alive = {.fd = frame_worker_pidfd, .events = POLLIN};
|
||||
bool allowed = frame_worker_pidfd >= 0 && uid == getuid() && pid > 1 &&
|
||||
g->pid == (uint32_t)pid && g->uid == uid && now < g->expires &&
|
||||
poll(&alive, 1, 0) == 0 && (!verify_exec || frame_worker_verified_exec_locked(g, now));
|
||||
pthread_mutex_unlock(&frame_worker_lock);
|
||||
return allowed;
|
||||
}
|
||||
|
||||
static bool frame_acquire_global_filter(const struct wl_client *client, const struct wl_global *global, void *data) {
|
||||
return wl_global_get_interface(global) != &droiddeck_frame_acquire_v1_interface ||
|
||||
frame_acquire_peer_allowed(client);
|
||||
frame_acquire_peer_allowed(client, true);
|
||||
}
|
||||
|
||||
static void frame_acquire_destroy(struct wl_client *client, struct wl_resource *resource) {
|
||||
@@ -1757,7 +1849,7 @@ static void frame_acquire_destroy(struct wl_client *client, struct wl_resource *
|
||||
|
||||
static void frame_acquire_set(struct wl_client *client, struct wl_resource *resource,
|
||||
struct wl_resource *surface, struct wl_resource *buffer, int32_t fd) {
|
||||
if (!frame_acquire_peer_allowed(client)) {
|
||||
if (!frame_acquire_peer_allowed(client, false)) {
|
||||
close(fd);
|
||||
wl_resource_post_error(resource, DROIDDECK_FRAME_ACQUIRE_V1_ERROR_UNAUTHORIZED_PEER,
|
||||
"paired capture peer is not authorized");
|
||||
@@ -1790,7 +1882,7 @@ static const struct droiddeck_frame_acquire_v1_interface frame_acquire_impl = {
|
||||
};
|
||||
|
||||
static void bind_frame_acquire(struct wl_client *client, void *data, uint32_t version, uint32_t id) {
|
||||
if (!frame_acquire_peer_allowed(client)) {
|
||||
if (!frame_acquire_peer_allowed(client, true)) {
|
||||
wl_client_post_implementation_error(client, "paired capture peer is not authorized");
|
||||
return;
|
||||
}
|
||||
@@ -1801,6 +1893,7 @@ static void bind_frame_acquire(struct wl_client *client, void *data, uint32_t ve
|
||||
return;
|
||||
}
|
||||
wl_resource_set_implementation(resource, &frame_acquire_impl, NULL, NULL);
|
||||
droiddeck_log("probe", "frame acquire bound to app-issued worker and exact executable map");
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
#ifndef DD_WORKER_PEER_H
|
||||
#define DD_WORKER_PEER_H
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
/* Model of one app-issued, session-bound authorization record. No callers,
|
||||
* property access, proc traversal, or authorization is installed by this file. */
|
||||
struct dd_worker_grant {
|
||||
uint32_t owner, uid, pid, dev_major, dev_minor;
|
||||
uint64_t start_ticks, ino, expires;
|
||||
const char *canonical_file;
|
||||
bool app_launch_verified, final_go;
|
||||
};
|
||||
|
||||
static inline bool dd_worker_map_matches(const struct dd_worker_grant *g, const char *line) {
|
||||
unsigned major, minor;
|
||||
uint64_t lo, hi, off, ino;
|
||||
int prefix = 0;
|
||||
char mode[5];
|
||||
if (!g->canonical_file || !*g->canonical_file ||
|
||||
sscanf(line, "%" SCNx64 "-%" SCNx64 " %4s %" SCNx64 " %x:%x %" SCNu64 " %n",
|
||||
&lo,&hi,mode,&off,&major,&minor,&ino,&prefix)!=7 ||
|
||||
prefix <= 0 || hi <= lo || strlen(mode)!=4 || mode[2]!='x' ||
|
||||
major!=g->dev_major || minor!=g->dev_minor || !ino || ino!=g->ino)
|
||||
return false;
|
||||
size_t path_len=strcspn(line+prefix,"\r\n");
|
||||
return strlen(g->canonical_file)==path_len && !memcmp(line+prefix,g->canonical_file,path_len);
|
||||
}
|
||||
|
||||
static inline bool dd_worker_grant_matches(const struct dd_worker_grant *g, uint32_t owner,
|
||||
uint32_t uid, uint32_t pid, uint64_t start, uint64_t now, const char *executable_map) {
|
||||
return g && g->app_launch_verified && g->final_go && owner && owner==g->owner &&
|
||||
uid==g->uid && pid>1 && pid==g->pid && start && start==g->start_ticks &&
|
||||
now<g->expires && executable_map && dd_worker_map_matches(g,executable_map);
|
||||
}
|
||||
#endif
|
||||
@@ -872,3 +872,42 @@ JNIEXPORT jint JNICALL
|
||||
Java_com_droiddeck_launcher_wayland_WaylandCompositor_nativeStreamFrames(JNIEnv *env, jclass clazz) {
|
||||
return (jint)droiddeck_stream_frames();
|
||||
}
|
||||
|
||||
|
||||
#ifdef DROIDDECK_FRAME_ACQUIRE_PROBE
|
||||
extern int droiddeck_frame_worker_grant(int owner, pid_t pid, uint64_t expires, const char *path,
|
||||
uint64_t dev, uint64_t ino);
|
||||
extern void droiddeck_frame_worker_revoke(int owner, int pid);
|
||||
#endif
|
||||
|
||||
JNIEXPORT jboolean JNICALL
|
||||
Java_com_droiddeck_launcher_wayland_WaylandCompositor_frameWorkerProbeEnabled(JNIEnv *env, jclass c) {
|
||||
#ifdef DROIDDECK_FRAME_ACQUIRE_PROBE
|
||||
return JNI_TRUE;
|
||||
#else
|
||||
return JNI_FALSE;
|
||||
#endif
|
||||
}
|
||||
|
||||
JNIEXPORT jboolean JNICALL
|
||||
Java_com_droiddeck_launcher_wayland_WaylandCompositor_frameWorkerProbeGrant(JNIEnv *env, jclass c,
|
||||
jint owner, jint pid, jlong expires, jstring path, jlong dev, jlong ino) {
|
||||
#ifdef DROIDDECK_FRAME_ACQUIRE_PROBE
|
||||
if (!path || expires <= 0 || ino <= 0) return JNI_FALSE;
|
||||
const char *name = (*env)->GetStringUTFChars(env, path, NULL);
|
||||
if (!name) return JNI_FALSE;
|
||||
int granted = droiddeck_frame_worker_grant(owner, pid, (uint64_t)expires, name, (uint64_t)dev, (uint64_t)ino);
|
||||
(*env)->ReleaseStringUTFChars(env, path, name);
|
||||
return granted ? JNI_TRUE : JNI_FALSE;
|
||||
#else
|
||||
return JNI_FALSE;
|
||||
#endif
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
Java_com_droiddeck_launcher_wayland_WaylandCompositor_frameWorkerProbeRevoke(JNIEnv *env, jclass c,
|
||||
jint owner, jint pid) {
|
||||
#ifdef DROIDDECK_FRAME_ACQUIRE_PROBE
|
||||
droiddeck_frame_worker_revoke(owner, pid);
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
package com.droiddeck.launcher.runtime
|
||||
|
||||
import android.content.Context
|
||||
import android.os.SystemClock
|
||||
import android.system.Os
|
||||
import com.droiddeck.launcher.wayland.WaylandCompositor
|
||||
import java.io.File
|
||||
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 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 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
|
||||
private var nativePid = 0
|
||||
|
||||
@Synchronized fun arm(context: Context, owner: Int, agentEnvironment: List<String>) {
|
||||
intent = null
|
||||
if (owner <= 0 || TEST_INTENT !in agentEnvironment ||
|
||||
!WaylandCompositor.frameWorkerProbeEnabled()) return
|
||||
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()
|
||||
}
|
||||
|
||||
internal fun exactGamescopeArgs(argv: List<String>): 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
|
||||
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")
|
||||
val seen = mutableSetOf<String>()
|
||||
var i = 4
|
||||
while (i < opts.size) {
|
||||
val option = opts[i++]
|
||||
if (!seen.add(option)) return false
|
||||
when (option) {
|
||||
"-e", "--hdr-enabled", "--mangoapp" -> Unit
|
||||
"-S" -> if (opts.getOrNull(i++) != "stretch") return false
|
||||
"--default-touch-mode" -> if (opts.getOrNull(i++) != "4") return false
|
||||
"-W", "-H", "-w", "-h", "-r", "--xwayland-count" -> {
|
||||
val value = opts.getOrNull(i++) ?: return false
|
||||
if (value.length !in 1..6 || value.any { it !in '0'..'9' } ||
|
||||
value.toIntOrNull()?.let { it > 0 } != true) return false
|
||||
}
|
||||
else -> return false
|
||||
}
|
||||
}
|
||||
return seen.containsAll(dimensions)
|
||||
}
|
||||
|
||||
fun match(owner: Int, root: File, file: String, argv: List<String>, 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
|
||||
// Hash outside the worker handshake and authorizing lock.
|
||||
return runCatching {
|
||||
require(t.binary.canonicalFile == t.binary)
|
||||
val before = Os.stat(t.binary.path)
|
||||
require(before.st_size == SIZE)
|
||||
val digest = MessageDigest.getInstance("SHA-256")
|
||||
t.binary.inputStream().use { file ->
|
||||
val bytes = ByteArray(65536)
|
||||
while (true) {
|
||||
val n = file.read(bytes)
|
||||
if (n < 0) break
|
||||
digest.update(bytes, 0, n)
|
||||
}
|
||||
}
|
||||
val after = Os.stat(t.binary.path)
|
||||
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)
|
||||
Qualified(t, after.st_dev, after.st_ino, after.st_mtime, after.st_ctime)
|
||||
}.getOrNull()
|
||||
}
|
||||
|
||||
/** 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
|
||||
intent = null // At most one candidate per explicitly armed session.
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Synchronized fun revoke(owner: Int, pid: Int = 0) {
|
||||
if (owner <= 0) return
|
||||
if (pid == 0 && intent?.owner == owner) intent = null
|
||||
if (nativeOwner == owner && (pid == 0 || pid == nativePid)) {
|
||||
WaylandCompositor.frameWorkerProbeRevoke(owner, pid)
|
||||
nativeOwner = 0
|
||||
nativePid = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -684,6 +684,7 @@ object GuestWorkers {
|
||||
}
|
||||
}
|
||||
context?.let { app -> drained.forEach { release(app, it) } }
|
||||
FrameAcquireWorkerProbe.revoke(of)
|
||||
val pids = guestPids(of)
|
||||
if (pids.isEmpty()) return
|
||||
pids.forEach { Process.sendSignal(it, OsConstants.SIGTERM) }
|
||||
@@ -901,16 +902,20 @@ object GuestWorkers {
|
||||
val owner = request.optInt("session", 0).coerceAtLeast(0)
|
||||
// The sender is the forked child (or droiddeck-spawn): its priority is the one the program would inherit
|
||||
val nice = runCatching { Process.getThreadPriority(client.peerCredentials.pid) }.getOrDefault(Process.THREAD_PRIORITY_DEFAULT)
|
||||
val testWorker = FrameAcquireWorkerProbe.match(owner, root, file, argv, ack)
|
||||
val pid = start(app, command, env.map { (k, v) -> "$k=$v" }, programFds, targets, true, owner = owner, nice = nice, onExit = { code ->
|
||||
if (testWorker != null && worker > 1) FrameAcquireWorkerProbe.revoke(owner, worker)
|
||||
status = code
|
||||
if (table != null && slot >= 0) reportExit(table, slot, code, parent, child, worker)
|
||||
exited.countDown()
|
||||
}, confirmed = {
|
||||
!ack || runCatching {
|
||||
val confirmed = !ack || runCatching {
|
||||
out.write("ok\n".toByteArray())
|
||||
out.flush()
|
||||
confirmedToHook = true
|
||||
}.isSuccess
|
||||
if (confirmed && testWorker != null) FrameAcquireWorkerProbe.publish(testWorker, worker)
|
||||
confirmed
|
||||
}, commit = { started ->
|
||||
worker = started
|
||||
if (!ack) return@start true
|
||||
@@ -930,6 +935,7 @@ object GuestWorkers {
|
||||
ended && line.toString() == "go"
|
||||
}.getOrDefault(false)
|
||||
})
|
||||
if (pid <= 0 && testWorker != null && worker > 1) FrameAcquireWorkerProbe.revoke(owner, worker)
|
||||
handedOver = true
|
||||
programFds.filter { it !== table }.forEach { runCatching { it.close() } }
|
||||
if (pid <= 0) table?.let { runCatching { it.close() } }
|
||||
|
||||
@@ -712,7 +712,11 @@ class SessionService : Service() {
|
||||
// starts no workers) or owns them
|
||||
ownWorkers = synchronized(stopLock) {
|
||||
if (gen != sessionGen || !SessionState.running) 0
|
||||
else com.droiddeck.launcher.runtime.GuestWorkers.serve(this).also { guestWorkerSession = it }
|
||||
else com.droiddeck.launcher.runtime.GuestWorkers.serve(this).also {
|
||||
guestWorkerSession = it
|
||||
if (SessionState.mode == MODE_STEAM)
|
||||
com.droiddeck.launcher.runtime.FrameAcquireWorkerProbe.arm(this, it, agentEnvironment)
|
||||
}
|
||||
}
|
||||
if (ownWorkers > 0) {
|
||||
guest.add("DD_SPAWN=" + com.droiddeck.launcher.runtime.GuestWorkers.socketName(this))
|
||||
|
||||
@@ -9,6 +9,10 @@ public final class WaylandCompositor {
|
||||
|
||||
private WaylandCompositor() {}
|
||||
|
||||
public static native boolean frameWorkerProbeEnabled();
|
||||
public static native boolean frameWorkerProbeGrant(int owner, int pid, long expires, String path, long dev, long ino);
|
||||
public static native void frameWorkerProbeRevoke(int owner, int pid);
|
||||
|
||||
private static volatile Runnable firstFrameListener;
|
||||
|
||||
public static void setFirstFrameListener(Runnable r) { firstFrameListener = r; }
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
#define _GNU_SOURCE
|
||||
#include "frame_worker_peer.h"
|
||||
#include <assert.h>
|
||||
#include <fcntl.h>
|
||||
#include <poll.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <sys/sysmacros.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
static uint64_t start_ticks(pid_t id) {
|
||||
char proc[64], line[4096]; snprintf(proc,sizeof(proc),"/proc/%d/stat",id);
|
||||
FILE *f=fopen(proc,"r");assert(f);assert(fgets(line,sizeof(line),f));fclose(f);
|
||||
char *p=strrchr(line,')');assert(p&&p[1]==' ');p+=2;
|
||||
for(int field=3;field<22;field++){p=strchr(p,' ');assert(p);while(*p==' ')p++;}
|
||||
return strtoull(p,0,10);
|
||||
}
|
||||
static bool check_maps(const struct dd_worker_grant *g,pid_t id,uint64_t ticks) {
|
||||
char proc[64],line[4096];snprintf(proc,sizeof(proc),"/proc/%d/maps",id);
|
||||
FILE *f=fopen(proc,"r");assert(f);bool found=false;
|
||||
while(fgets(line,sizeof(line),f)) if(dd_worker_grant_matches(g,4,getuid(),id,ticks,4,line)){ found=true;break;}
|
||||
fclose(f);return found;
|
||||
}
|
||||
int main(void){
|
||||
char p[]="/tmp/dd-live-map.XXXXXX";int fd=mkstemp(p);assert(fd>=0);
|
||||
assert(ftruncate(fd,4096)==0);struct stat st;assert(fstat(fd,&st)==0);
|
||||
int a[2],b[2];assert(pipe(a)==0&&pipe(b)==0);pid_t id=fork();assert(id>=0);
|
||||
if(id==0){
|
||||
close(a[0]);close(b[1]);
|
||||
void *m=mmap(NULL,4096,PROT_READ|PROT_EXEC,MAP_PRIVATE,fd,0);assert(m!=MAP_FAILED);
|
||||
assert(write(a[1],"y",1)==1);
|
||||
char c;assert(read(b[0],&c,1)==1);assert(mprotect(m,4096,PROT_READ)==0);
|
||||
assert(write(a[1],"y",1)==1);assert(read(b[0],&c,1)==1);_exit(0);
|
||||
}
|
||||
close(a[1]);close(b[0]);char c;assert(read(a[0],&c,1)==1);
|
||||
int pfd=syscall(SYS_pidfd_open,id,0);assert(pfd>=0);
|
||||
struct pollfd pfd_poll={.fd=pfd,.events=POLLIN};assert(poll(&pfd_poll,1,0)==0);
|
||||
uint64_t start=start_ticks(id);assert(start);
|
||||
struct dd_worker_grant g={.owner=4,.uid=getuid(),.pid=id,.dev_major=major(st.st_dev),.dev_minor=minor(st.st_dev),.ino=st.st_ino,.start_ticks=start,.expires=10,.canonical_file=p,.app_launch_verified=true,.final_go=true};
|
||||
assert(check_maps(&g,id,start));assert(!check_maps(&g,id,start+1));
|
||||
g.final_go=false;assert(!check_maps(&g,id,start));g.final_go=true;
|
||||
assert(write(b[1],"n",1)==1);assert(read(a[0],&c,1)==1);assert(!check_maps(&g,id,start));
|
||||
assert(write(b[1],"n",1)==1);assert(poll(&pfd_poll,1,2000)==1);
|
||||
assert(pfd_poll.revents&POLLIN);int status;assert(waitpid(id,&status,0)==id&&WIFEXITED(status)&&WEXITSTATUS(status)==0);
|
||||
close(fd); close(pfd);unlink(p); puts("Live executable map, start-time reuse rejection, no-GO revocation, non-executable map rejection, and pidfd exit readiness passed.");
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
#include "frame_worker_peer.h"
|
||||
#include <assert.h>
|
||||
|
||||
static const char file[]="/app/private/linuxfs/opt/droiddeck-agent/bin/gamescope";
|
||||
static const char good[]="1000-2000 r-xp 00001000 fe:15 5502 /app/private/linuxfs/opt/droiddeck-agent/bin/gamescope\n";
|
||||
int main(void) {
|
||||
struct dd_worker_grant g={.owner=3,.uid=10197,.pid=650,.dev_major=254,.dev_minor=21,
|
||||
.start_ticks=251191,.ino=5502,.expires=1000,.canonical_file=file,.app_launch_verified=true,.final_go=true};
|
||||
assert(dd_worker_grant_matches(&g,3,10197,650,251191,999,good));
|
||||
assert(!dd_worker_grant_matches(&g,3,10197,650,251191,1000,good));
|
||||
assert(!dd_worker_grant_matches(&g,4,10197,650,251191,999,good));
|
||||
assert(!dd_worker_grant_matches(&g,3,10198,650,251191,999,good));
|
||||
assert(!dd_worker_grant_matches(&g,3,10197,651,251191,999,good));
|
||||
assert(!dd_worker_grant_matches(&g,3,10197,650,251192,999,good));
|
||||
assert(!dd_worker_grant_matches(&g,3,10197,650,251191,999,NULL));
|
||||
const char *invalid[]={
|
||||
"1000-2000 r--p 0 fe:15 5502 /app/private/linuxfs/opt/droiddeck-agent/bin/gamescope",
|
||||
"1000-2000 r-xp 0 fe:15 5503 /app/private/linuxfs/opt/droiddeck-agent/bin/gamescope",
|
||||
"1000-2000 r-xp 0 fe:16 5502 /app/private/linuxfs/opt/droiddeck-agent/bin/gamescope",
|
||||
"1000-2000 r-xp 0 fe:15 5502 /app/private/linuxfs/opt/droiddeck-agent/bin/gamescope (deleted)",
|
||||
"1000-2000 r-xp 0 fe:15 5502 /app/private/linuxfs/opt/droiddeck-agent/bin/gamescope.bak",
|
||||
"1000-2000 r-xp 0 fe:15 5502 /system/lib64/libddexec.so", "", NULL};
|
||||
for (int i=0;invalid[i];i++) assert(!dd_worker_map_matches(&g,invalid[i]));
|
||||
g.final_go=false;
|
||||
assert(!dd_worker_grant_matches(&g,3,10197,650,251191,999,good));
|
||||
g.final_go=true; g.app_launch_verified=false;
|
||||
assert(!dd_worker_grant_matches(&g,3,10197,650,251191,999,good));
|
||||
}
|
||||
@@ -1,110 +1,43 @@
|
||||
"""Build the production compositor's designated-peer checks with test doubles.
|
||||
|
||||
Linux checks a real socket's SO_PEERCRED; the property source and Wayland
|
||||
global/resource hooks are doubles. Android NDK builds exercise the real APIs.
|
||||
"""
|
||||
from pathlib import Path
|
||||
"""Focused worker-grant contract; no Android or guest boot required."""
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
from test_vk_pending_completion import function
|
||||
|
||||
SOURCE = Path(__file__).resolve().parents[2] / 'app/src/main/cpp/waylandcomp/src/compositor.c'
|
||||
HEAD = r'''
|
||||
#define _GNU_SOURCE
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <limits.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
#define PROP_VALUE_MAX 92
|
||||
|
||||
static const char *property;
|
||||
struct wl_client { pid_t pid; uid_t uid; int rejected; };
|
||||
struct wl_global { const void *interface; };
|
||||
struct wl_resource { int unused; };
|
||||
static int droiddeck_frame_acquire_v1_interface, frame_acquire_impl, bound, installed;
|
||||
static struct wl_resource resource;
|
||||
|
||||
static int __system_property_get(const char *name, char *value) {
|
||||
assert(!strcmp(name,"debug.droiddeck.frame_acquire_pid"));
|
||||
if (!property) return 0;
|
||||
size_t len = strlen(property);
|
||||
if (len < PROP_VALUE_MAX) memcpy(value,property,len+1);
|
||||
return (int)len;
|
||||
}
|
||||
static void wl_client_get_credentials(const struct wl_client *c,pid_t *pid,uid_t *uid,gid_t *gid) {
|
||||
assert(!gid); *pid=c->pid; *uid=c->uid;
|
||||
}
|
||||
static const void *wl_global_get_interface(const struct wl_global *g) { return g->interface; }
|
||||
static void wl_client_post_implementation_error(struct wl_client *c,const char *why) {
|
||||
assert(why && *why); c->rejected++;
|
||||
}
|
||||
static struct wl_resource *wl_resource_create(struct wl_client *c,const void *i,int version,uint32_t id) {
|
||||
assert(c && i==&droiddeck_frame_acquire_v1_interface && version==1 && id==9);
|
||||
bound++; return &resource;
|
||||
}
|
||||
static void wl_client_post_no_memory(struct wl_client *c) { (void)c; abort(); }
|
||||
static void wl_resource_set_implementation(struct wl_resource *r,const void *i,void *data,void *destroy) {
|
||||
assert(r==&resource && i==&frame_acquire_impl && !data && !destroy); installed++;
|
||||
}
|
||||
'''
|
||||
TAIL = r'''
|
||||
int main(void) {
|
||||
char pid[32], wrong[32];
|
||||
struct wl_client permitted={.pid=getpid(),.uid=getuid()};
|
||||
#ifdef __linux__
|
||||
int sock[2]; assert(socketpair(AF_UNIX,SOCK_STREAM,0,sock)==0);
|
||||
struct ucred cred; socklen_t len=sizeof(cred);
|
||||
assert(getsockopt(sock[0],SOL_SOCKET,SO_PEERCRED,&cred,&len)==0);
|
||||
assert(cred.pid==getpid() && cred.uid==getuid());
|
||||
permitted.pid=cred.pid; permitted.uid=cred.uid;
|
||||
close(sock[0]); close(sock[1]);
|
||||
#endif
|
||||
snprintf(pid,sizeof(pid),"%ld",(long)permitted.pid);
|
||||
snprintf(wrong,sizeof(wrong),"%ld",(long)permitted.pid+1);
|
||||
const char *bad[] = {NULL, "", "0","1","-2","+25"," 12","12 ","12x","012","2147483648",
|
||||
"99999999999999999999999999999999999", wrong};
|
||||
struct wl_global probe={&droiddeck_frame_acquire_v1_interface}, ordinary={NULL};
|
||||
for (size_t n=0; n<sizeof(bad)/sizeof(*bad); n++) {
|
||||
property=bad[n];
|
||||
assert(frame_acquire_global_filter(&permitted,&ordinary,NULL));
|
||||
assert(!frame_acquire_global_filter(&permitted,&probe,NULL));
|
||||
bind_frame_acquire(&permitted,NULL,1,9);
|
||||
assert(!bound && !installed);
|
||||
}
|
||||
assert(permitted.rejected==(int)(sizeof(bad)/sizeof(*bad)));
|
||||
property=pid;
|
||||
struct wl_client other_uid=permitted, other_pid=permitted;
|
||||
other_uid.uid++; other_pid.pid++;
|
||||
assert(!frame_acquire_peer_allowed(&other_uid));
|
||||
assert(!frame_acquire_peer_allowed(&other_pid));
|
||||
assert(frame_acquire_global_filter(&permitted,&probe,NULL));
|
||||
bind_frame_acquire(&permitted,NULL,1,9);
|
||||
assert(bound==1&&installed==1);
|
||||
property=NULL; // removal also revokes ability to use an existing binding
|
||||
assert(!frame_acquire_peer_allowed(&permitted));
|
||||
return 0;
|
||||
}
|
||||
'''
|
||||
ROOT=Path(__file__).resolve().parents[2]
|
||||
SRC=ROOT/'app/src/main/cpp/waylandcomp/src'
|
||||
JAVA=ROOT/'app/src/main/java/com/droiddeck/launcher'
|
||||
|
||||
|
||||
class FrameAcquirePeerGateTest(unittest.TestCase):
|
||||
def test_only_designated_uid_pid_can_see_or_bind_the_probe(self):
|
||||
source = SOURCE.read_text()
|
||||
actual = '\n'.join(function(source, name) for name in (
|
||||
'frame_acquire_peer_allowed', 'frame_acquire_global_filter', 'bind_frame_acquire'))
|
||||
with tempfile.TemporaryDirectory(prefix='droiddeck-frame-peer-') as tmp:
|
||||
tmp = Path(tmp)
|
||||
probe = tmp / 'probe.c'
|
||||
binary = tmp / 'probe'
|
||||
probe.write_text(HEAD + actual + TAIL)
|
||||
subprocess.run([os.environ.get('CC', 'cc'), '-std=gnu11', '-Werror', '-Wall', '-Wextra',
|
||||
'-Wno-unused-parameter', str(probe), '-o', str(binary)], check=True)
|
||||
subprocess.run([str(binary)], check=True, timeout=10)
|
||||
def test_app_issued_identity_and_executable_mapping(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
image=Path(tmp)/'probe'
|
||||
subprocess.run([os.environ.get('CC','cc'), '-std=c11','-Wall','-Wextra','-Werror',
|
||||
'-I',str(SRC),str(Path(__file__).with_name('frame_worker_peer_test.c')),
|
||||
'-o',str(image)],check=True)
|
||||
subprocess.run([str(image)],check=True,timeout=10)
|
||||
|
||||
@unittest.skipUnless(sys.platform.startswith("linux"), "Requires Linux procfs and pidfd")
|
||||
def test_live_worker_mapping_starttime_and_revocation(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
image = Path(tmp) / "probe"
|
||||
subprocess.run([os.environ.get("CC", "cc"), "-std=c11", "-Wall", "-Wextra", "-Werror",
|
||||
"-I", str(SRC), str(Path(__file__).with_name("frame_worker_peer_live_test.c")),
|
||||
"-o", str(image)], check=True)
|
||||
subprocess.run([str(image)], check=True, timeout=10)
|
||||
|
||||
def test_publish_before_worker_go_and_require_map_at_bind(self):
|
||||
workers=(JAVA/'runtime/GuestWorkers.kt').read_text()
|
||||
match=workers.index('val testWorker = FrameAcquireWorkerProbe.match')
|
||||
confirmation=workers.index('}, confirmed = {',match)
|
||||
publish=workers.index('FrameAcquireWorkerProbe.publish',confirmation)
|
||||
self.assertLess(publish,workers.index('}, commit = { started ->',confirmation))
|
||||
self.assertIn('confirmed() && Os.write(go[1]',workers)
|
||||
self.assertIn('if (pid <= 0 && testWorker != null && worker > 1) FrameAcquireWorkerProbe.revoke',workers)
|
||||
comp=(SRC/'compositor.c').read_text()
|
||||
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)
|
||||
@@ -18,6 +18,7 @@ HEAD = r'''
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/ioctl.h>
|
||||
@@ -50,7 +51,7 @@ struct surface {
|
||||
};
|
||||
static int wl_surface_interface, surface_impl;
|
||||
static int peer_allowed=1;
|
||||
static int frame_acquire_peer_allowed(const struct wl_client *c) { (void)c; return peer_allowed; }
|
||||
static int frame_acquire_peer_allowed(const struct wl_client *c, bool verify_exec) { (void)c; return peer_allowed; }
|
||||
static struct wl_client *wl_resource_get_client(struct wl_resource *r) { return r->owner; }
|
||||
static void *wl_resource_get_user_data(struct wl_resource *r) { return r->data; }
|
||||
static int wl_resource_instance_of(struct wl_resource *r, const void *iface, const void *impl) {
|
||||
|
||||
Reference in new issue
Block a user