mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 12:28:40 +00:00
fix(desktop): Desktop draws again and Sunshine can capture it (#2780)
* fix(desktop): keep the bundled PipeWire paths out of Plasma so the desktop draws * fix(desktop): let the screencast portal use KWin so Sunshine can capture the desktop * fix(proot): /proc/<pid>/root reads as the guest root so portals accept callers * fix(nativeguest): /proc/<pid>/exe of another guest program names that program, not the loader * fix(nativeguest): trust another process's program only when it is mapped there, and read all of its environment * fix(nativeguest): accept versioned glibc loaders, and wait for the test program to run
This commit is contained in:
1 parent
fa95df37d0
commit
b2b2e38e67
28 files changed
+336
-60
No files matched your search
Whitespace-only changes.
Whitespace-only changes.
Whitespace-only changes.
Whitespace-only changes.
Whitespace-only changes.
Whitespace-only changes.
Whitespace-only changes.
Whitespace-only changes.
Whitespace-only changes.
Whitespace-only changes.
Whitespace-only changes.
Whitespace-only changes.
Whitespace-only changes.
Whitespace-only changes.
Whitespace-only changes.
Whitespace-only changes.
Whitespace-only changes.
Whitespace-only changes.
Whitespace-only changes.
Whitespace-only changes.
Whitespace-only changes.
@@ -125,9 +125,8 @@ if [ "$(cat /usr/share/droiddeck/kwin-compositor 2>/dev/null)" = opengl ]; then
|
||||
chmod 755 /run/droiddeck/kwin/kwin_wayland
|
||||
export PATH=/run/droiddeck/kwin:$PATH
|
||||
fi
|
||||
if /usr/local/bin/droiddeck-pipewire start; then
|
||||
eval "$(/usr/local/bin/droiddeck-pipewire env)"
|
||||
else
|
||||
# Plasma uses the rootfs libpipewire; the bundle's module and plugin paths leave it without a shell.
|
||||
if ! /usr/local/bin/droiddeck-pipewire start; then
|
||||
echo "== pipewire: not running; screen capture has no picture"
|
||||
fi
|
||||
echo "== droiddeck-desktop: Plasma on $WAYLAND_DISPLAY"
|
||||
|
||||
@@ -423,6 +423,9 @@ ssize_t readlink(const char *pathname, char *buf, size_t bufsiz) {
|
||||
if (is_proc_self_exe(pathname)) {
|
||||
ssize_t exe_len = handle_proc_self_exe(buf, bufsiz);
|
||||
if (exe_len >= 0) return exe_len;
|
||||
} else {
|
||||
ssize_t exe_len = handle_proc_other_exe(pathname, buf, bufsiz);
|
||||
if (exe_len >= 0) return exe_len;
|
||||
}
|
||||
static ssize_t (*orig_readlink)(const char *, char *, size_t) = NULL;
|
||||
if (!orig_readlink) orig_readlink = (ssize_t (*)(const char *, char *, size_t))dlsym(RTLD_NEXT, "readlink");
|
||||
@@ -443,9 +446,16 @@ ssize_t readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz) {
|
||||
if (!orig_readlinkat) orig_readlinkat = (ssize_t (*)(int, const char *, char *, size_t))dlsym(RTLD_NEXT, "readlinkat");
|
||||
DDNATV_AT_REMAP(dirfd, pathname);
|
||||
// after the remap: "proc/self/exe" relative to a descriptor for "/" is /proc/self/exe as well
|
||||
{
|
||||
ssize_t exe_len = handle_proc_exe_at(dirfd, pathname, buf, bufsiz);
|
||||
if (exe_len >= 0) return exe_len;
|
||||
}
|
||||
if (is_proc_self_exe(pathname)) {
|
||||
ssize_t exe_len = handle_proc_self_exe(buf, bufsiz);
|
||||
if (exe_len >= 0) return exe_len;
|
||||
} else {
|
||||
ssize_t exe_len = handle_proc_other_exe(pathname, buf, bufsiz);
|
||||
if (exe_len >= 0) return exe_len;
|
||||
}
|
||||
char pbuf[PATH_MAX];
|
||||
const char *target = (pathname && pathname[0] == '/') ? rewrite_path(pathname, pbuf, sizeof(pbuf)) : pathname;
|
||||
|
||||
+173
-49
@@ -165,64 +165,188 @@ static int is_proc_self_exe(const char *path) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void exe_from_cmdline(const char *cmdline_path, char *out, size_t outsz) {
|
||||
int fd = open(cmdline_path, O_RDONLY | O_CLOEXEC);
|
||||
if (fd < 0) return;
|
||||
char cmdline[8192];
|
||||
ssize_t n = read(fd, cmdline, sizeof(cmdline) - 1);
|
||||
close(fd);
|
||||
if (n <= 0) return;
|
||||
cmdline[n] = '\0';
|
||||
char *args[64] = {0};
|
||||
int ac = 0;
|
||||
char *p = cmdline;
|
||||
while (p < cmdline + n && ac < 64) {
|
||||
args[ac++] = p;
|
||||
p += strlen(p) + 1;
|
||||
}
|
||||
for (int i = 0; i < ac; i++) {
|
||||
if (strcmp(args[i], "--argv0") == 0 && i + 2 < ac) {
|
||||
ddnatv_copy_cstr(out, outsz, args[i + 2]);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (ac > 0 && args[0] && args[0][0] != '\0') ddnatv_copy_cstr(out, outsz, args[0]);
|
||||
}
|
||||
|
||||
static ssize_t copy_guest_exe(const char *real_exe, char *buf, size_t bufsiz) {
|
||||
const char *out_path = real_exe;
|
||||
if (g_ddnatv_root[0] != '\0') {
|
||||
size_t rlen = g_ddnatv_root_len;
|
||||
if (strncmp(real_exe, g_ddnatv_root, rlen) == 0 &&
|
||||
(real_exe[rlen] == '/' || real_exe[rlen] == '\0')) {
|
||||
out_path = real_exe + rlen;
|
||||
if (out_path[0] == '\0') out_path = "/";
|
||||
}
|
||||
}
|
||||
size_t len = strlen(out_path);
|
||||
size_t copy_len = (len < bufsiz) ? len : bufsiz;
|
||||
memcpy(buf, out_path, copy_len);
|
||||
return (ssize_t)copy_len;
|
||||
}
|
||||
|
||||
static ssize_t handle_proc_self_exe(char *buf, size_t bufsiz) {
|
||||
if (!buf || bufsiz == 0) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
init_ddnatv_hook();
|
||||
if (g_real_exe[0] == '\0') {
|
||||
int fd = open("/proc/self/cmdline", O_RDONLY);
|
||||
if (fd >= 0) {
|
||||
char cmdline[8192];
|
||||
ssize_t n = read(fd, cmdline, sizeof(cmdline) - 1);
|
||||
close(fd);
|
||||
if (n > 0) {
|
||||
cmdline[n] = '\0';
|
||||
char *args[64] = {0};
|
||||
int ac = 0;
|
||||
char *p = cmdline;
|
||||
while (p < cmdline + n && ac < 64) {
|
||||
args[ac++] = p;
|
||||
p += strlen(p) + 1;
|
||||
}
|
||||
for (int i = 0; i < ac; i++) {
|
||||
if (strcmp(args[i], "--argv0") == 0 && i + 2 < ac) {
|
||||
strncpy(g_real_exe, args[i + 2], sizeof(g_real_exe) - 1);
|
||||
g_real_exe[sizeof(g_real_exe) - 1] = '\0';
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (g_real_exe[0] == '\0' && ac > 0 && args[0] && args[0][0] != '\0') {
|
||||
strncpy(g_real_exe, args[0], sizeof(g_real_exe) - 1);
|
||||
g_real_exe[sizeof(g_real_exe) - 1] = '\0';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (g_real_exe[0] != '\0') {
|
||||
char stripped[PATH_MAX];
|
||||
const char *out_path = g_real_exe;
|
||||
if (g_ddnatv_root[0] != '\0') {
|
||||
size_t rlen = g_ddnatv_root_len;
|
||||
if (strncmp(g_real_exe, g_ddnatv_root, rlen) == 0 &&
|
||||
(g_real_exe[rlen] == '/' || g_real_exe[rlen] == '\0')) {
|
||||
out_path = g_real_exe + rlen;
|
||||
if (out_path[0] == '\0') out_path = "/";
|
||||
}
|
||||
}
|
||||
strncpy(stripped, out_path, sizeof(stripped) - 1);
|
||||
stripped[sizeof(stripped) - 1] = '\0';
|
||||
|
||||
size_t len = strlen(stripped);
|
||||
size_t copy_len = (len < bufsiz) ? len : bufsiz;
|
||||
memcpy(buf, stripped, copy_len);
|
||||
return (ssize_t)copy_len;
|
||||
}
|
||||
if (g_real_exe[0] == '\0') exe_from_cmdline("/proc/self/cmdline", g_real_exe, sizeof(g_real_exe));
|
||||
if (g_real_exe[0] != '\0') return copy_guest_exe(g_real_exe, buf, bufsiz);
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int is_ld_linux(const char *path);
|
||||
|
||||
static ssize_t (*real_readlinkat_fn(void))(int, const char *, char *, size_t) {
|
||||
static ssize_t (*fn)(int, const char *, char *, size_t) = NULL;
|
||||
if (!fn) fn = (ssize_t (*)(int, const char *, char *, size_t))dlsym(RTLD_NEXT, "readlinkat");
|
||||
return fn;
|
||||
}
|
||||
|
||||
static int proc_env_value(pid_t pid, const char *key, char *out, size_t outsz) {
|
||||
char path[64];
|
||||
snprintf(path, sizeof(path), "/proc/%d/environ", (int)pid);
|
||||
int fd = open(path, O_RDONLY | O_CLOEXEC);
|
||||
if (fd < 0) return 0;
|
||||
size_t cap = 65536, used = 0, keylen = strlen(key);
|
||||
char *env = malloc(cap + 1);
|
||||
ssize_t got;
|
||||
while (env && (got = read(fd, env + used, cap - used)) > 0) {
|
||||
used += (size_t)got;
|
||||
if (used == cap) {
|
||||
char *bigger = cap < (8u << 20) ? realloc(env, cap * 2 + 1) : NULL;
|
||||
if (!bigger) break;
|
||||
env = bigger;
|
||||
cap *= 2;
|
||||
}
|
||||
}
|
||||
close(fd);
|
||||
int found = 0;
|
||||
if (env) {
|
||||
env[used] = '\0';
|
||||
for (char *e = env; e < env + used; e += strlen(e) + 1) {
|
||||
if (strncmp(e, key, keylen) == 0 && e[keylen] == '=' && e[keylen + 1] != '\0') {
|
||||
ddnatv_copy_cstr(out, outsz, e + keylen + 1);
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
free(env);
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
// Overlay filesystems report a different device to stat() than in /proc/<pid>/maps; the path, as the
|
||||
// kernel resolved it, then stands in for the device.
|
||||
static int proc_maps_file(pid_t pid, dev_t dev, ino_t ino, const char *resolved) {
|
||||
char path[64];
|
||||
snprintf(path, sizeof(path), "/proc/%d/maps", (int)pid);
|
||||
int fd = open(path, O_RDONLY | O_CLOEXEC);
|
||||
if (fd < 0) return 0;
|
||||
char chunk[4096], line[1024];
|
||||
size_t len = 0;
|
||||
ssize_t got;
|
||||
int found = 0;
|
||||
while (!found && (got = read(fd, chunk, sizeof(chunk))) > 0) {
|
||||
for (ssize_t i = 0; i < got && !found; i++) {
|
||||
if (chunk[i] != '\n') {
|
||||
if (len < sizeof(line) - 1) line[len++] = chunk[i];
|
||||
continue;
|
||||
}
|
||||
line[len] = '\0';
|
||||
len = 0;
|
||||
unsigned long long offset, inode;
|
||||
unsigned int major_nr, minor_nr;
|
||||
int name_at = 0;
|
||||
if (sscanf(line, "%*s %*s %llx %x:%x %llu %n", &offset, &major_nr, &minor_nr, &inode, &name_at) == 4 &&
|
||||
offset == 0 && inode == (unsigned long long)ino &&
|
||||
(makedev(major_nr, minor_nr) == dev || (resolved && name_at > 0 && strcmp(line + name_at, resolved) == 0))) {
|
||||
found = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
close(fd);
|
||||
return found;
|
||||
}
|
||||
|
||||
// The kernel's /proc/<pid>/exe of every other guest program is the loader that started it. KWin
|
||||
// grants privileged Wayland interfaces by matching that path to a desktop file's Exec, so it is the
|
||||
// program the loader runs, as for the calling process. A process can set its environment and
|
||||
// arguments, so the program they name counts only if that process has the file mapped from its start.
|
||||
static ssize_t handle_proc_other_exe(const char *path, char *buf, size_t bufsiz) {
|
||||
if (!path || !buf || bufsiz == 0 || strncmp(path, "/proc/", 6) != 0) return -1;
|
||||
const char *p = path + 6;
|
||||
if (*p < '1' || *p > '9') return -1;
|
||||
while (*p >= '0' && *p <= '9') p++;
|
||||
if (strcmp(p, "/exe") != 0) return -1;
|
||||
pid_t pid = (pid_t)atoi(path + 6);
|
||||
if (pid == getpid()) return -1;
|
||||
init_ddnatv_hook();
|
||||
if (g_ddnatv_root[0] == '\0') return -1;
|
||||
ssize_t (*real_readlinkat)(int, const char *, char *, size_t) = real_readlinkat_fn();
|
||||
static int (*real_fstatat)(int, const char *, struct stat *, int) = NULL;
|
||||
if (!real_fstatat) real_fstatat = (int (*)(int, const char *, struct stat *, int))dlsym(RTLD_NEXT, "fstatat");
|
||||
if (!real_readlinkat || !real_fstatat) return -1;
|
||||
char loader[PATH_MAX];
|
||||
ssize_t n = real_readlinkat(AT_FDCWD, path, loader, sizeof(loader) - 1);
|
||||
if (n <= 0) return -1;
|
||||
loader[n] = '\0';
|
||||
const char *base = strrchr(loader, '/');
|
||||
base = base ? base + 1 : loader;
|
||||
if (strcmp(base, "libddexec.so") != 0 && !is_ld_linux(loader)) return -1;
|
||||
|
||||
char real_exe[PATH_MAX] = "";
|
||||
if (!proc_env_value(pid, "DDNATV_REAL_EXE", real_exe, sizeof(real_exe))) {
|
||||
char cmdline[64];
|
||||
snprintf(cmdline, sizeof(cmdline), "/proc/%d/cmdline", (int)pid);
|
||||
exe_from_cmdline(cmdline, real_exe, sizeof(real_exe));
|
||||
}
|
||||
static char *(*real_realpath)(const char *, char *) = NULL;
|
||||
if (!real_realpath) real_realpath = (char *(*)(const char *, char *))dlsym(RTLD_NEXT, "realpath");
|
||||
struct stat st;
|
||||
char resolved[PATH_MAX];
|
||||
if (real_exe[0] != '/' || real_fstatat(AT_FDCWD, real_exe, &st, 0) != 0) return -1;
|
||||
if (!real_realpath || !real_realpath(real_exe, resolved)) resolved[0] = '\0';
|
||||
if (!proc_maps_file(pid, st.st_dev, st.st_ino, resolved[0] ? resolved : NULL)) return -1;
|
||||
return copy_guest_exe(real_exe, buf, bufsiz);
|
||||
}
|
||||
|
||||
// readlinkat(<descriptor for /proc/<pid>>, "exe"), as the absolute path would be answered.
|
||||
static ssize_t handle_proc_exe_at(int dirfd, const char *pathname, char *buf, size_t bufsiz) {
|
||||
if (!pathname || pathname[0] == '/' || dirfd == AT_FDCWD || strcmp(pathname, "exe") != 0) return -1;
|
||||
ssize_t (*real_readlinkat)(int, const char *, char *, size_t) = real_readlinkat_fn();
|
||||
if (!real_readlinkat) return -1;
|
||||
char link[64], dir[PATH_MAX], full[PATH_MAX];
|
||||
snprintf(link, sizeof(link), "/proc/self/fd/%d", dirfd);
|
||||
ssize_t n = real_readlinkat(AT_FDCWD, link, dir, sizeof(dir) - 1);
|
||||
if (n <= 0) return -1;
|
||||
dir[n] = '\0';
|
||||
if (strncmp(dir, "/proc/", 6) != 0 || snprintf(full, sizeof(full), "%s/exe", dir) >= (int)sizeof(full)) return -1;
|
||||
if (is_proc_self_exe(full)) return handle_proc_self_exe(buf, bufsiz);
|
||||
return handle_proc_other_exe(full, buf, bufsiz);
|
||||
}
|
||||
|
||||
static const char *rewrite_path(const char *path, char *buffer, size_t bufsize) {
|
||||
if (!path) return NULL;
|
||||
// Relative paths must NEVER be rewritten to rootfs root
|
||||
|
||||
@@ -102,6 +102,12 @@ Added by DroidDeck:
|
||||
`libblsession.so`'s robust.c never sees: `libopenvr_api.so` opens it `RTLD_DEEPBIND`. Steam
|
||||
aborted on "futex robust_list not initialized by pthreads" as soon as SteamVR was registered.
|
||||
|
||||
- `0029-proc-pid-root-guest.patch` - `/proc/<pid>/root` of a tracee resolved to the rootfs's host
|
||||
path, which does not exist inside the guest, so opening it failed with `ENOENT`. It now reads
|
||||
as `/`, like `exe` and `cwd` read as guest paths. xdg-desktop-portal opens it for every caller to
|
||||
tell sandboxed apps from host ones, so every portal request failed with "Unable to open
|
||||
/proc/<pid>/root", Sunshine's screen capture included.
|
||||
|
||||
Prototype (inert unless `PROOT_FASTPATH` is set; see `docs/development/proot-performance.md`):
|
||||
|
||||
- `0014-fastpath-trampoline.patch` - with `PROOT_FASTPATH`, every `SECCOMP_RET_TRACE` in the filter
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
--- a/src/path/proc.c
|
||||
+++ b/src/path/proc.c
|
||||
@@ -112,7 +112,7 @@
|
||||
* of tracee->???. */
|
||||
SUBSTITUTE(exe, known_tracee->exe);
|
||||
SUBSTITUTE(cwd, known_tracee->fs->cwd);
|
||||
- SUBSTITUTE(root, get_root(known_tracee));
|
||||
+ SUBSTITUTE(root, "/");
|
||||
#undef SUBSTITUTE
|
||||
return DEFAULT;
|
||||
|
||||
@@ -38,11 +38,9 @@ pipewire() {
|
||||
|
||||
def test_desktop_serves_pipewire_before_plasma_and_stops_it_after(self):
|
||||
start = DESKTOP.index(HELPER + ' start')
|
||||
env = DESKTOP.index(HELPER + ' env')
|
||||
plasma = DESKTOP.index('dbus-run-session -- startplasma-wayland')
|
||||
stop = DESKTOP.index(HELPER + ' stop')
|
||||
self.assertLess(start, env)
|
||||
self.assertLess(env, plasma)
|
||||
self.assertLess(start, plasma)
|
||||
self.assertLess(plasma, stop)
|
||||
self.assertLess(stop, DESKTOP.index('droiddeck-steam-launch bigpicture'))
|
||||
|
||||
@@ -58,16 +56,17 @@ pipewire() {
|
||||
self.assertNotIn('if [ "${BL_BOTTOM_DESKTOP:-0}" != 1 ]; then', DESKTOP)
|
||||
for bottom in (False, True):
|
||||
self.assertEqual(['call:start'], self.run_lifecycle_block(
|
||||
'if ' + HELPER + ' start',
|
||||
'if ! ' + HELPER + ' start',
|
||||
'echo "== droiddeck-desktop: Plasma on $WAYLAND_DISPLAY"', bottom))
|
||||
self.assertEqual(['call:stop'], self.run_lifecycle_block(
|
||||
HELPER + ' stop',
|
||||
'echo "== droiddeck-desktop: Plasma ended ($rc)"', bottom))
|
||||
|
||||
def test_successful_start_exports_the_client_environment(self):
|
||||
def test_plasma_keeps_the_rootfs_pipewire_client_settings(self):
|
||||
self.assertNotIn(HELPER + ' env', DESKTOP)
|
||||
for bottom in (False, True):
|
||||
self.assertEqual(['call:start', 'env:set'], self.run_lifecycle_block(
|
||||
'if ' + HELPER + ' start',
|
||||
self.assertEqual(['call:start', 'env:unset'], self.run_lifecycle_block(
|
||||
'if ! ' + HELPER + ' start',
|
||||
'echo "== droiddeck-desktop: Plasma on $WAYLAND_DISPLAY"', bottom, probe_env=True))
|
||||
|
||||
def test_failed_start_reports_it_and_leaves_the_environment_alone(self):
|
||||
@@ -75,7 +74,7 @@ pipewire() {
|
||||
self.assertEqual(
|
||||
['call:start', '== pipewire: not running; screen capture has no picture', 'env:unset'],
|
||||
self.run_lifecycle_block(
|
||||
'if ' + HELPER + ' start',
|
||||
'if ! ' + HELPER + ' start',
|
||||
'echo "== droiddeck-desktop: Plasma on $WAYLAND_DISPLAY"', bottom,
|
||||
start_status=1, probe_env=True))
|
||||
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
from tools.tests.test_usr2_preload import runner
|
||||
|
||||
HOOK_BUILD = Path(__file__).resolve().parents[1] / "nativeguest/build.sh"
|
||||
|
||||
PROBE = r"""
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
for (int i = 1; i < argc; i++) {
|
||||
char buf[4096];
|
||||
ssize_t n;
|
||||
if (strncmp(argv[i], "at:", 3) == 0) {
|
||||
int dfd = open(argv[i] + 3, O_RDONLY | O_DIRECTORY);
|
||||
n = dfd >= 0 ? readlinkat(dfd, "exe", buf, sizeof(buf) - 1) : -1;
|
||||
if (dfd >= 0) close(dfd);
|
||||
} else {
|
||||
n = readlink(argv[i], buf, sizeof(buf) - 1);
|
||||
}
|
||||
buf[n > 0 ? n : 0] = 0;
|
||||
printf("%s\t%s\n", argv[i], n > 0 ? buf : "-");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
"""
|
||||
|
||||
SLEEPER = r"""
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
int main(void) { puts("ready"); fflush(stdout); sleep(60); return 0; }
|
||||
"""
|
||||
|
||||
|
||||
class NativeHookProcExeTest(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.cc, cls.prefix = runner()
|
||||
if cls.cc is None:
|
||||
raise unittest.SkipTest("no aarch64 compiler and runner")
|
||||
cls.tmp = tempfile.TemporaryDirectory()
|
||||
tmp = Path(cls.tmp.name)
|
||||
cls.hook = tmp / "hook.so"
|
||||
subprocess.run(["bash", str(HOOK_BUILD), str(cls.hook), cls.cc], check=True, capture_output=True)
|
||||
for name, source in (("probe", PROBE), ("sleeper", SLEEPER)):
|
||||
(tmp / f"{name}.c").write_text(source)
|
||||
subprocess.run([cls.cc, "-O1", "-o", str(tmp / name), str(tmp / f"{name}.c")], check=True)
|
||||
cls.root = tmp / "root"
|
||||
(cls.root / "usr/bin").mkdir(parents=True)
|
||||
cls.kwin = cls.root / "usr/bin/kwin_wayland"
|
||||
shutil.copy(tmp / "sleeper", cls.kwin)
|
||||
# The child's /proc/<pid>/exe must name the loader, as DroidDeck's libddexec.so does.
|
||||
cls.loader = tmp / "lib/libddexec.so"
|
||||
cls.loader.parent.mkdir()
|
||||
if cls.prefix:
|
||||
shutil.copy(shutil.which(cls.prefix[0]), cls.loader)
|
||||
cls.loader_args = list(cls.prefix[1:])
|
||||
else:
|
||||
ld = next(p for p in ("/lib/ld-linux-aarch64.so.1", "/lib64/ld-linux-aarch64.so.1") if os.path.exists(p))
|
||||
shutil.copy(ld, cls.loader)
|
||||
cls.loader_args = []
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
cls.tmp.cleanup()
|
||||
|
||||
def start(self, loader, real_exe=None, program=None):
|
||||
env = {k: v for k, v in os.environ.items() if k not in ("LD_PRELOAD", "DDNATV_REAL_EXE")}
|
||||
if real_exe:
|
||||
env["DDNATV_REAL_EXE"] = real_exe
|
||||
program = program or Path(self.tmp.name) / "sleeper"
|
||||
cmd = ([str(self.loader), *self.loader_args] if loader else list(self.prefix)) + [str(program)]
|
||||
child = subprocess.Popen(cmd, env=env, stdout=subprocess.PIPE, text=True)
|
||||
self.addCleanup(child.stdout.close)
|
||||
self.addCleanup(child.wait)
|
||||
self.addCleanup(child.kill)
|
||||
self.assertEqual("ready", child.stdout.readline().strip())
|
||||
return child.pid
|
||||
|
||||
def readlink(self, *paths):
|
||||
hook_env = {"DDNATV_ROOT": str(self.root), "LD_PRELOAD": str(self.hook)}
|
||||
env = {k: v for k, v in os.environ.items() if k != "LD_PRELOAD"}
|
||||
if self.prefix:
|
||||
cmd = list(self.prefix) + [arg for key, value in hook_env.items() for arg in ("-E", f"{key}={value}")]
|
||||
else:
|
||||
cmd = []
|
||||
env.update(hook_env)
|
||||
out = subprocess.run(cmd + [str(Path(self.tmp.name) / "probe"), *paths], env=env, capture_output=True,
|
||||
text=True, timeout=120)
|
||||
self.assertEqual(out.returncode, 0, out.stderr)
|
||||
return {line.split("\t")[0]: line.split("\t")[1] for line in out.stdout.splitlines()}
|
||||
|
||||
def test_another_guest_program_names_the_program_its_loader_runs(self):
|
||||
pid = self.start(loader=True, real_exe=str(self.kwin), program=self.kwin)
|
||||
link = f"/proc/{pid}/exe"
|
||||
self.assertEqual("/usr/bin/kwin_wayland", self.readlink(link)[link])
|
||||
|
||||
def test_a_descriptor_for_the_process_directory_gives_the_same_program(self):
|
||||
pid = self.start(loader=True, real_exe=str(self.kwin), program=self.kwin)
|
||||
at = f"at:/proc/{pid}"
|
||||
self.assertEqual("/usr/bin/kwin_wayland", self.readlink(at)[at])
|
||||
|
||||
def test_a_program_named_only_in_the_environment_is_not_believed(self):
|
||||
pid = self.start(loader=True, real_exe=str(self.kwin))
|
||||
link = f"/proc/{pid}/exe"
|
||||
self.assertEqual(os.readlink(link), self.readlink(link)[link])
|
||||
|
||||
def test_a_process_not_started_by_the_loader_is_unchanged(self):
|
||||
pid = self.start(loader=False, real_exe=str(self.kwin))
|
||||
link = f"/proc/{pid}/exe"
|
||||
self.assertEqual(os.readlink(link), self.readlink(link)[link])
|
||||
|
||||
def test_a_loader_without_a_known_program_keeps_the_kernel_link(self):
|
||||
pid = self.start(loader=True)
|
||||
link = f"/proc/{pid}/exe"
|
||||
self.assertEqual(os.readlink(link), self.readlink(link)[link])
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user