mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-25 15:56:01 +00:00
* fix(native): programs started through the rootfs loader show their own command line Without proot every dynamic guest program runs as "ld.so --preload <hook> --library-path <dirs> --argv0 <name> <program> <args>", and the kernel's /proc/<pid>/cmdline kept all of it. Launchers and the session match programs by their command line, and Wine only moves its Windows command line over its loader's name when its arguments follow each other, so the Battle.net launcher never saw Battle.net.exe, gave up after five minutes and ended the session with the game still running. The hook now moves the program's own arguments to the front of that area at start, as an exec of the program would have laid them out. * fix(native): programs the app starts natively keep their path for /proc/<pid>/exe * fix(native): the rewritten command line keeps AT_EXECFN naming the program --------- Co-authored-by: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com>
161 lines
6.8 KiB
Python
161 lines
6.8 KiB
Python
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)
|
|
cls.runtime = tmp / "runtime"
|
|
cls.runtime.mkdir()
|
|
cls.bound = cls.runtime / "chrome"
|
|
shutil.copy(tmp / "sleeper", cls.bound)
|
|
cls.shadowed = cls.runtime / "bin/chrome"
|
|
cls.shadowed.parent.mkdir()
|
|
shutil.copy(tmp / "sleeper", cls.shadowed)
|
|
cls.override = tmp / "override"
|
|
cls.override.mkdir()
|
|
shutil.copy(tmp / "sleeper", cls.override / "chrome")
|
|
# 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:
|
|
cls.ld_so = next(p for p in ("/lib/ld-linux-aarch64.so.1", "/lib64/ld-linux-aarch64.so.1") if os.path.exists(p))
|
|
shutil.copy(cls.ld_so, cls.loader)
|
|
cls.loader_args = []
|
|
|
|
@classmethod
|
|
def tearDownClass(cls):
|
|
cls.tmp.cleanup()
|
|
|
|
def start(self, loader, real_exe=None, program=None):
|
|
program = program or Path(self.tmp.name) / "sleeper"
|
|
cmd = ([str(self.loader), *self.loader_args] if loader else list(self.prefix)) + [str(program)]
|
|
return self.spawn(cmd, {"DDNATV_REAL_EXE": real_exe} if real_exe else {})
|
|
|
|
def spawn(self, cmd, extra_env):
|
|
env = {k: v for k, v in os.environ.items() if k not in ("LD_PRELOAD", "DDNATV_ROOT", "DDNATV_REAL_EXE")}
|
|
env.update(extra_env)
|
|
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, binds=None):
|
|
hook_env = {"DDNATV_ROOT": str(self.root), "LD_PRELOAD": str(self.hook)}
|
|
if binds:
|
|
hook_env["DDNATV_BINDS"] = binds
|
|
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_guest_program_from_a_bind_is_named_by_its_guest_path(self):
|
|
pid = self.start(loader=True, real_exe=str(self.bound), program=self.bound)
|
|
link = f"/proc/{pid}/exe"
|
|
self.assertEqual("/app/extra/chrome", self.readlink(link, binds=f"/proc:/proc|{self.runtime}:/app/extra")[link])
|
|
|
|
def test_a_guest_program_shadowed_by_a_nested_bind_keeps_its_own_path(self):
|
|
pid = self.start(loader=True, real_exe=str(self.shadowed), program=self.shadowed)
|
|
link = f"/proc/{pid}/exe"
|
|
binds = f"/proc:/proc|{self.runtime}:/app|{self.override}:/app/bin"
|
|
self.assertEqual(str(self.shadowed), self.readlink(link, binds=binds)[link])
|
|
|
|
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])
|
|
|
|
@unittest.skipIf(runner()[1], "qemu answers /proc/self/cmdline from its own copy of the arguments")
|
|
def test_a_program_whose_hook_moved_its_arguments_is_still_named(self):
|
|
cmd = [self.ld_so, "--preload", str(self.hook), "--argv0", self.kwin.name, str(self.kwin)]
|
|
pid = self.spawn(cmd, {"DDNATV_ROOT": str(self.root), "DDNATV_REAL_EXE": str(self.kwin)})
|
|
self.assertEqual(b"kwin_wayland", Path(f"/proc/{pid}/cmdline").read_bytes().rstrip(b"\0"))
|
|
link = f"/proc/{pid}/exe"
|
|
self.assertEqual("/usr/bin/kwin_wayland", 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()
|