From a760aea276db81f8d1f61ac5aa4ff3e1dae74d66 Mon Sep 17 00:00:00 2001 From: xXJsonDeruloXx Date: Sun, 11 Oct 2026 10:08:10 -0400 Subject: [PATCH] perf: relocated Wine code is shared between processes through a per-session cache file --- .../overlay/usr/local/bin/droiddeck-session | 1 + tools/linuxfs/preload/noexec.c | 75 +++++++++++ tools/tests/test_noexec_preload.py | 118 ++++++++++++++++++ 3 files changed, 194 insertions(+) diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-session b/tools/linuxfs/overlay/usr/local/bin/droiddeck-session index 94d9a69370..6a27e37e48 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-session +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-session @@ -114,6 +114,7 @@ fi # with the same flags, so no priority reaches the GPU. Kept for a driver that maps it, and off # unless the app's Performance page turns it on (BL_GAMESCOPE_REALTIME=1). export GAMESCOPE_FORCE_VULKAN_REALTIME=${BL_GAMESCOPE_REALTIME:-0} +rm -rf /tmp/.droiddeck-rx # A sleep that starts no process. Under proot every exec is a traced execve plus the loader's # opens and maps, and the watchers below poll for the whole session: `sleep 1` in a loop was 60 diff --git a/tools/linuxfs/preload/noexec.c b/tools/linuxfs/preload/noexec.c index fba3a4e733..62ed3ae62d 100644 --- a/tools/linuxfs/preload/noexec.c +++ b/tools/linuxfs/preload/noexec.c @@ -11,6 +11,8 @@ #include #include #include +#include +#include #include #include #include @@ -79,8 +81,81 @@ static int private_file_mapping(uintptr_t start, uintptr_t end) { return found; } +static const char rx_dir[] = "/tmp/.droiddeck-rx"; + +static uint64_t fnv1a(const unsigned char *p, size_t n) { + uint64_t h = 1469598103934665603ULL; + for (size_t i = 0; i < n; i++) { + h ^= p[i]; + h *= 1099511628211ULL; + } + return h; +} + +static char *put_hex(char *out, uint64_t v) { + for (int shift = 60; shift >= 0; shift -= 4) *out++ = "0123456789abcdef"[(v >> shift) & 15]; + return out; +} + +static int same_content(int fd, const unsigned char *p, size_t len) { + char buf[16384]; + for (size_t off = 0; off < len;) { + size_t want = len - off < sizeof buf ? len - off : sizeof buf; + if (syscall(SYS_pread64, fd, buf, want, off) != (long)want || memcmp(buf, p + off, want)) return 0; + off += want; + } + return 1; +} + +static int write_all(int fd, const unsigned char *p, size_t len) { + for (size_t off = 0; off < len;) { + long n = syscall(SYS_write, fd, p + off, len - off); + if (n <= 0) return -1; + off += (size_t)n; + } + return 0; +} + +static int rebake_shared(void *addr, size_t len, int prot) { + char path[sizeof rx_dir + 40], staged[sizeof path + 20]; + char *end = path + sizeof rx_dir - 1; + memcpy(path, rx_dir, sizeof rx_dir - 1); + *end++ = '/'; + end = put_hex(end, fnv1a(addr, len)); + *end++ = '-'; + end = put_hex(end, len); + *end = 0; + int fd = (int)syscall(SYS_openat, AT_FDCWD, path, O_RDONLY | O_CLOEXEC); + if (fd >= 0 && !same_content(fd, addr, len)) { + syscall(SYS_close, fd); + return -1; + } + if (fd < 0) { + syscall(SYS_mkdirat, AT_FDCWD, rx_dir, 0700); + memcpy(staged, path, (size_t)(end - path)); + char *tail = put_hex(staged + (end - path), (uint64_t)syscall(SYS_gettid)); + *tail = 0; + int out = (int)syscall(SYS_openat, AT_FDCWD, staged, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600); + if (out < 0) return -1; + int bad = write_all(out, addr, len); + syscall(SYS_close, out); + if (bad || syscall(SYS_renameat, AT_FDCWD, staged, AT_FDCWD, path)) { + syscall(SYS_unlinkat, AT_FDCWD, staged, 0); + return -1; + } + fd = (int)syscall(SYS_openat, AT_FDCWD, path, O_RDONLY | O_CLOEXEC); + if (fd < 0) return -1; + } + long mapped = syscall(SYS_mmap, addr, len, prot, MAP_PRIVATE | MAP_FIXED, fd, 0); + syscall(SYS_close, fd); + return mapped == (long)addr ? 0 : -1; +} + static int rebake(void *addr, size_t len, int prot) { + size_t page = getauxval(AT_PAGESZ); + if (page) len = (len + page - 1) & ~(page - 1); if (real_mprotect(addr, len, PROT_READ)) return -1; + if (!rebake_shared(addr, len, prot)) return 0; void *copy = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (copy == MAP_FAILED) return -1; memcpy(copy, addr, len); diff --git a/tools/tests/test_noexec_preload.py b/tools/tests/test_noexec_preload.py index 0e563ec49a..fb5ecc7645 100644 --- a/tools/tests/test_noexec_preload.py +++ b/tools/tests/test_noexec_preload.py @@ -70,5 +70,123 @@ class NoexecPreloadTest(unittest.TestCase): self.assertEqual(refused[1:], ["protect 13", "read -"]) +EXECMOD = r""" +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include + +static int private_file(void *addr) { + FILE *maps = fopen("/proc/self/maps", "r"); + char line[512]; + int found = 0; + while (maps && fgets(line, sizeof line, maps)) { + unsigned long lo, hi; + char perms[5]; + if (sscanf(line, "%lx-%lx %4s", &lo, &hi, perms) != 3) continue; + if ((unsigned long)addr >= lo && (unsigned long)addr < hi) { + found = perms[3] == 'p' && strchr(line, '/') != NULL; + break; + } + } + if (maps) fclose(maps); + return found; +} + +int mprotect(void *addr, size_t len, int prot) { + if ((prot & PROT_EXEC) && private_file(addr)) { + errno = EACCES; + return -1; + } + return (int)syscall(SYS_mprotect, addr, len, prot); +} +""" + +CALLER = r""" +#include +#include +#include +#include +#include +#include + +int main(int argc, char **argv) { + int fd = open(argv[1], O_RDWR); + unsigned char *code = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); +#if defined(__aarch64__) + static const unsigned int body[] = {0x52800540, 0xd65f03c0}; +#else + static const unsigned char body[] = {0xb8, 0x2a, 0x00, 0x00, 0x00, 0xc3}; +#endif + memcpy(code + 64, body, sizeof body); + int result = mprotect(code, 4096, PROT_READ | PROT_EXEC); + printf("protect %d\n", result ? errno : 0); + __builtin___clear_cache((char *)code, (char *)code + 4096); + if (!result) printf("call %d\n", ((int (*)(void))(code + 64))()); + printf("data %d\n", code[0]); + char line[512]; + FILE *maps = fopen("/proc/self/maps", "r"); + while (maps && fgets(line, sizeof line, maps)) { + unsigned long lo, hi; + if (sscanf(line, "%lx-%lx", &lo, &hi) == 2 && (unsigned long)code >= lo && (unsigned long)code < hi) + printf("shared %d\n", strstr(line, ".droiddeck-rx") != NULL); + } + return 0; +} +""" + + +@unittest.skipUnless(shutil.which("cc") and os.uname().sysname == "Linux", "needs cc on Linux") +class ExecmodRebakeTest(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.tmp = Path(tempfile.mkdtemp()) + cls.lib = cls.tmp / "libnoexec.so" + subprocess.run(["cc", "-O2", "-Wall", "-Werror", "-fPIC", "-shared", "-pthread", "-DNOEXEC_TEST", "-o", str(cls.lib), + str(PRELOAD / "noexec.c"), str(PRELOAD / "fsync.c"), str(PRELOAD / "robust.c"), "-ldl"], check=True) + (cls.tmp / "execmod.c").write_text(EXECMOD) + cls.execmod = cls.tmp / "libexecmod.so" + subprocess.run(["cc", "-O2", "-fPIC", "-shared", "-o", str(cls.execmod), str(cls.tmp / "execmod.c")], check=True) + (cls.tmp / "caller.c").write_text(CALLER) + cls.caller = cls.tmp / "wine" + subprocess.run(["cc", "-O2", "-o", str(cls.caller), str(cls.tmp / "caller.c")], check=True) + cls.image = cls.tmp / "ntdll.dll" + cls.image.write_bytes(bytes([7]) + bytes(4095)) + + @classmethod + def tearDownClass(cls): + shutil.rmtree(cls.tmp, ignore_errors=True) + + def setUp(self): + shutil.rmtree("/tmp/.droiddeck-rx", ignore_errors=True) + + def tearDown(self): + shutil.rmtree("/tmp/.droiddeck-rx", ignore_errors=True) + + def run_with(self, *libs): + env = dict(os.environ, LD_PRELOAD=":".join(str(lib) for lib in libs)) + result = subprocess.run([str(self.caller), str(self.image)], env=env, capture_output=True, text=True, check=True) + return result.stdout.split() + + def test_a_refused_relocated_image_runs_from_anonymous_memory(self): + self.assertEqual(self.run_with(self.lib, self.execmod), ["protect", "0", "call", "42", "data", "7", "shared", "1"]) + + def test_processes_share_one_copy_of_the_code(self): + self.run_with(self.lib, self.execmod) + self.run_with(self.lib, self.execmod) + self.assertEqual(len(os.listdir("/tmp/.droiddeck-rx")), 1) + + def test_without_the_preload_the_refusal_stands(self): + self.assertEqual(self.run_with(self.execmod), ["protect", "13", "data", "7", "shared", "0"]) + + def test_the_file_itself_is_not_changed(self): + self.run_with(self.lib, self.execmod) + self.assertEqual(self.image.read_bytes()[:65], bytes([7]) + bytes(64)) + + if __name__ == "__main__": unittest.main()