mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-05 20:45:43 +00:00
* fix(native): reopen refused memfds and answer links in the form they are named Android's app policy refuses the open permission on memory files, so a read-only reopen of an own memfd through /proc/self/fd/N, /proc/thread-self/fd/N, /proc/<own pid or thread>/fd/N or /dev/fd/N failed with EACCES while read, write and mmap of the descriptor still worked. The hook now answers such a refused read-only open with a new memfd holding the same bytes, name, mode and seals. Writable opens, other processes' descriptors and regular files stay refused. FEX routes guest openat through libc syscall(), so x86 guests get the same answer. readlink() stripped the rootfs prefix from every target, so a link stored with a real target (Proton's s: drive) read as /root/... while getcwd(), /proc/self/fd and /proc/self/maps give the real path, and a path resolved through the link no longer matched the working directory. A link named by its real rootfs path, and /proc/<pid>/cwd, now keep the real target, as realpath() of a real path does; guest and relative names keep the guest form. * fix(native): keep the memfd copy read-only, leave openat2 errors native, keep guest form for remapped link names The copy that answers a refused read-only memfd reopen is now sealed against writes and resizing, so it cannot be written through as a read-only descriptor could not. openat2 reads its open_how only after the kernel refused the open with EACCES, so an unreadable pointer gets the kernel's EFAULT. readlinkat and fstatat keep the guest form for a relative name the hook remapped to a real path; only a name the caller supplied in real form keeps the real target. * fix(native): pin the memfd being copied and refuse copies that cannot match it * Copy a refused memfd only when the path and the descriptor name one file
192 lines
8.1 KiB
Python
192 lines
8.1 KiB
Python
import errno
|
|
import os
|
|
from pathlib import Path
|
|
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 <errno.h>
|
|
#include <fcntl.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
#include <unistd.h>
|
|
|
|
static void show(const char *name, ssize_t len, const char *buf) {
|
|
printf("%s\t%zd\t%.*s\n", name, len, len > 0 ? (int)len : 0, buf);
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
const char *link = argv[1];
|
|
struct stat st;
|
|
if (lstat(link, &st) != 0) return 2;
|
|
char *exact = malloc((size_t)st.st_size + 1);
|
|
show("sized", readlink(link, exact, (size_t)st.st_size), exact);
|
|
char full[4096];
|
|
show("full", readlink(link, full, sizeof(full)), full);
|
|
char small[4];
|
|
show("small", readlink(link, small, sizeof(small)), small);
|
|
show("sized-at", readlinkat(AT_FDCWD, link, exact, (size_t)st.st_size), exact);
|
|
errno = 0;
|
|
ssize_t zero = readlink(link, full, 0);
|
|
int zero_errno = errno;
|
|
printf("zero\t%zd\t%d\n", zero, zero_errno);
|
|
errno = 0;
|
|
zero = readlinkat(AT_FDCWD, link, full, 0);
|
|
zero_errno = errno;
|
|
printf("zero-at\t%zd\t%d\n", zero, zero_errno);
|
|
return 0;
|
|
}
|
|
"""
|
|
|
|
FORMS = r"""
|
|
#define _GNU_SOURCE
|
|
#include <fcntl.h>
|
|
#include <limits.h>
|
|
#include <sys/stat.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
|
|
int main(int argc, char **argv) {
|
|
if (chdir(argv[1]) != 0) return 2;
|
|
char buf[PATH_MAX];
|
|
printf("cwd\t%s\n", getcwd(buf, sizeof(buf)) ? buf : "");
|
|
ssize_t n = readlink("/proc/self/cwd", buf, sizeof(buf) - 1);
|
|
printf("proc-cwd\t%.*s\n", n > 0 ? (int)n : 0, buf);
|
|
n = readlink(argv[2], buf, sizeof(buf) - 1);
|
|
printf("relative\t%.*s\n", n > 0 ? (int)n : 0, buf);
|
|
char climbing[PATH_MAX];
|
|
snprintf(climbing, sizeof(climbing), "../../../../../../../../../../../../etc/alternatives/%s", argv[2]);
|
|
n = readlinkat(AT_FDCWD, climbing, buf, sizeof(buf) - 1);
|
|
printf("climbing-at\t%.*s\n", n > 0 ? (int)n : 0, buf);
|
|
struct stat st;
|
|
printf("climbing-size\t%ld\n", fstatat(AT_FDCWD, climbing, &st, AT_SYMLINK_NOFOLLOW) == 0 ? (long)st.st_size : -1L);
|
|
for (int i = 3; i < argc; i++) {
|
|
char *got = realpath(argv[i], NULL);
|
|
printf("realpath:%s\t%s\n", argv[i], got ? got : "");
|
|
free(got);
|
|
}
|
|
return 0;
|
|
}
|
|
"""
|
|
|
|
|
|
class NativeHookReadlinkTest(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(dir="/tmp")
|
|
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)
|
|
(tmp / "probe.c").write_text(PROBE)
|
|
cls.probe = tmp / "probe"
|
|
subprocess.run([cls.cc, "-O1", "-o", str(cls.probe), str(tmp / "probe.c")], check=True)
|
|
cls.root = tmp / "root"
|
|
(cls.root / "bin").mkdir(parents=True)
|
|
(cls.root / "etc/alternatives").mkdir(parents=True)
|
|
(cls.root / "bin/nano").write_text("")
|
|
os.symlink(cls.root / "bin/nano", cls.root / "etc/alternatives/editor")
|
|
os.symlink("/bin/nano", cls.root / "etc/alternatives/guest")
|
|
(tmp / "forms.c").write_text(FORMS)
|
|
cls.forms = tmp / "forms"
|
|
subprocess.run([cls.cc, "-O1", "-o", str(cls.forms), str(tmp / "forms.c")], check=True)
|
|
cls.steam = cls.root / "root/.local/share/Steam"
|
|
(cls.steam / "steamapps/common/Game").mkdir(parents=True)
|
|
(cls.steam / "steamapps/common/Game/Game.exe").write_text("")
|
|
(cls.root / "root/pfx/dosdevices").mkdir(parents=True)
|
|
os.symlink(cls.steam, cls.root / "root/pfx/dosdevices/s:")
|
|
|
|
@classmethod
|
|
def tearDownClass(cls):
|
|
cls.tmp.cleanup()
|
|
|
|
def run_hooked(self, argv):
|
|
hook_env = {"DDNATV_ROOT": str(self.root), "LD_PRELOAD": str(self.hook)}
|
|
env = {k: v for k, v in os.environ.items() if k not in ("LD_PRELOAD", "DDNATV_ROOT")}
|
|
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 + argv, env=env, capture_output=True, text=True, timeout=120)
|
|
self.assertEqual(0, out.returncode, out.stdout + out.stderr)
|
|
return {line.split("\t")[0]: line.split("\t")[1:] for line in out.stdout.splitlines()}
|
|
|
|
def read(self, link):
|
|
return self.run_hooked([str(self.probe), link])
|
|
|
|
def path_forms(self, cwd, relative_link, *paths):
|
|
return {key: value[0] for key, value in self.run_hooked([str(self.forms), cwd, relative_link, *paths]).items()}
|
|
|
|
def test_a_buffer_sized_from_lstat_gets_the_guest_target(self):
|
|
result = self.read("/etc/alternatives/editor")
|
|
self.assertEqual(["9", "/bin/nano"], result["sized"])
|
|
self.assertEqual(["9", "/bin/nano"], result["sized-at"])
|
|
self.assertEqual(["9", "/bin/nano"], result["full"])
|
|
|
|
def test_a_short_buffer_gets_the_start_of_the_guest_target(self):
|
|
self.assertEqual(["4", "/bin"], self.read("/etc/alternatives/editor")["small"])
|
|
|
|
def test_a_guest_target_is_unchanged(self):
|
|
result = self.read("/etc/alternatives/guest")
|
|
self.assertEqual(["9", "/bin/nano"], result["sized"])
|
|
self.assertEqual(["4", "/bin"], result["small"])
|
|
|
|
def test_a_zero_sized_buffer_is_rejected(self):
|
|
result = self.read("/etc/alternatives/editor")
|
|
self.assertEqual(["-1", str(errno.EINVAL)], result["zero"])
|
|
self.assertEqual(["-1", str(errno.EINVAL)], result["zero-at"])
|
|
|
|
def test_a_link_named_by_its_real_path_keeps_a_real_target(self):
|
|
steam = str(self.steam)
|
|
result = self.read(str(self.root / "root/pfx/dosdevices/s:"))
|
|
self.assertEqual([str(len(steam)), steam], result["sized"])
|
|
self.assertEqual([str(len(steam)), steam], result["sized-at"])
|
|
self.assertEqual([str(len(steam)), steam], result["full"])
|
|
|
|
def test_a_link_named_by_its_guest_path_keeps_the_guest_target(self):
|
|
result = self.read("/root/pfx/dosdevices/s:")
|
|
self.assertEqual([str(len("/root/.local/share/Steam")), "/root/.local/share/Steam"], result["full"])
|
|
self.assertEqual(["9", "/bin/nano"], self.read("/etc/alternatives/editor")["sized"])
|
|
|
|
def test_a_link_the_hook_wrote_reads_in_the_form_it_is_named_in(self):
|
|
real = str(self.root / "bin/nano")
|
|
self.assertEqual([str(len(real)), real], self.read(str(self.root / "etc/alternatives/editor"))["sized"])
|
|
self.assertEqual(["9", "/bin/nano"], self.read(str(self.root / "etc/alternatives/guest"))["sized"])
|
|
|
|
def test_a_game_reached_through_a_drive_link_is_named_like_the_working_directory(self):
|
|
game = self.steam / "steamapps/common/Game"
|
|
through_drive = str(self.root / "root/pfx/dosdevices/s:/steamapps/common/Game/Game.exe")
|
|
result = self.path_forms(str(game), "Game.exe", through_drive, "Game.exe")
|
|
self.assertEqual(str(game), result["cwd"])
|
|
self.assertEqual(str(game / "Game.exe"), result[f"realpath:{through_drive}"])
|
|
self.assertTrue(result[f"realpath:{through_drive}"].startswith(result["cwd"] + "/"))
|
|
|
|
def test_the_proc_working_directory_link_matches_getcwd(self):
|
|
game = str(self.steam / "steamapps/common/Game")
|
|
result = self.path_forms(game, "Game.exe")
|
|
self.assertEqual(result["cwd"], result["proc-cwd"])
|
|
|
|
def test_guest_and_relative_names_keep_the_guest_form(self):
|
|
guest_drive = "/root/pfx/dosdevices/s:/steamapps/common/Game/Game.exe"
|
|
result = self.path_forms(str(self.root / "etc/alternatives"), "editor", guest_drive, "/etc/alternatives/editor")
|
|
self.assertEqual("/bin/nano", result["relative"])
|
|
self.assertEqual("/bin/nano", result["climbing-at"])
|
|
self.assertEqual("9", result["climbing-size"])
|
|
self.assertEqual("/root/.local/share/Steam/steamapps/common/Game/Game.exe", result[f"realpath:{guest_drive}"])
|
|
self.assertEqual("/bin/nano", result["realpath:/etc/alternatives/editor"])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|