mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-24 15:26:04 +00:00
* Native hook: start a program again through /proc/self/exe as itself A program that execs /proc/self/exe (Chromium's network and utility services, Electron apps) reached the kernel link, which names ld.so, so the child was started without ld.so and saw libddexec as its own program. Chromium then looked for its ICU data beside libddexec and the network service crashed in a loop. The exec path now resolves /proc/self/exe to the program the hook knows it runs, so it is routed like any other guest program, and readlink of the exe link names a program from a bind by its guest path. * fix(hook): keep the host path of a running program a nested bind shadows
151 lines
6.1 KiB
Python
151 lines
6.1 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:
|
|
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, 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])
|
|
|
|
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()
|