From 5fec4a2d257a3a9afef8ebc2f7ff4132bbb2298e Mon Sep 17 00:00:00 2001
From: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com>
Date: Mon, 16 Nov 2026 22:06:58 +0000
Subject: [PATCH] fix(wayland): hooked guest clients reach sockets whose host
path is longer than sun_path
The native-userland hook and the Go shim name a long socket path through /proc/self/fd/
/, the rule the compositor listener and proot already use, instead of a hashed /tmp name nobody listens on.
---
tools/nativeguest/gosys/gosys.c | 51 ++++---
tools/nativeguest/hook/fileops.c | 6 +-
tools/nativeguest/hook/setup.c | 82 +++++------
tools/nativeguest/hook/sockets.c | 18 +--
tools/tests/test_native_hook_long_socket.py | 144 ++++++++++++++++++++
5 files changed, 217 insertions(+), 84 deletions(-)
create mode 100644 tools/tests/test_native_hook_long_socket.py
diff --git a/tools/nativeguest/gosys/gosys.c b/tools/nativeguest/gosys/gosys.c
index 4a38cf4364..2b03b2cfc9 100644
--- a/tools/nativeguest/gosys/gosys.c
+++ b/tools/nativeguest/gosys/gosys.c
@@ -34,7 +34,7 @@ enum {
NR_LLISTXATTR = 12, NR_REMOVEXATTR = 14, NR_LREMOVEXATTR = 15, NR_GETCWD = 17,
NR_INOTIFY_ADD_WATCH = 27, NR_MKNODAT = 33, NR_MKDIRAT = 34, NR_UNLINKAT = 35, NR_SYMLINKAT = 36,
NR_LINKAT = 37, NR_RENAMEAT = 38, NR_STATFS = 43, NR_TRUNCATE = 45, NR_FACCESSAT = 48,
- NR_CHDIR = 49, NR_CHROOT = 51, NR_FCHMODAT = 53, NR_FCHOWNAT = 54, NR_OPENAT = 56,
+ NR_CHDIR = 49, NR_CHROOT = 51, NR_FCHMODAT = 53, NR_FCHOWNAT = 54, NR_OPENAT = 56, NR_CLOSE = 57,
NR_READLINKAT = 78, NR_NEWFSTATAT = 79, NR_UTIMENSAT = 88, NR_BIND = 200, NR_CONNECT = 203,
NR_EXECVE = 221, NR_NAME_TO_HANDLE_AT = 264, NR_RENAMEAT2 = 276, NR_STATX = 291,
NR_OPENAT2 = 437, NR_FACCESSAT2 = 439, NR_FCHMODAT2 = 452, NR_SETREGID = 143, NR_SETGID = 144,
@@ -269,17 +269,17 @@ static i64 unmap_prefix(const struct gosys_hdr *h, char *buf, i64 n, int nul) {
// ---- Unix sockets ------------------------------------------------------------------------
-static void put_hex16(char *out, u64 v) {
- static const char digits[] = "0123456789abcdef";
- for (int i = 15; i >= 0; i--) {
- out[i] = digits[v & 15];
- v >>= 4;
- }
+static u64 put_dec(char *out, u64 v) {
+ char digits[20];
+ u64 n = 0;
+ do {
+ digits[n++] = (char)('0' + v % 10);
+ v /= 10;
+ } while (v);
+ for (u64 i = 0; i < n; i++) out[i] = digits[n - 1 - i];
+ return n;
}
-// bind() and connect() of a Unix socket with a path: mapped like hook/setup.c does, and hashed
-// into /tmp when it does not fit the 108 bytes of sun_path (the same name, so a Go server and a
-// program using the C library find each other)
static i64 with_sockaddr(const struct gosys_hdr *h, u32 *held, i64 nr, i64 a0, i64 a1, i64 a2) {
const unsigned char *sa = (const unsigned char *)a1;
if (!sa || a2 < 4 || a2 > 128 || sa[0] != 1 /* AF_UNIX */ || sa[1] != 0 || sa[2] != '/')
@@ -290,7 +290,7 @@ static i64 with_sockaddr(const struct gosys_hdr *h, u32 *held, i64 nr, i64 a0, i
if (max > 108) max = 108;
mcopy(orig, (const char *)sa + 2, max);
orig[max] = '\0';
- const char *rw = rw_path(h, orig, slot + SLOT_PATH_B, SLOT_PATH_CAP);
+ char *rw = (char *)rw_path(h, orig, slot + SLOT_PATH_B, SLOT_PATH_CAP);
u64 rl = slen(rw);
char *out = slot + SLOT_ARGV;
out[0] = 1;
@@ -299,18 +299,25 @@ static i64 with_sockaddr(const struct gosys_hdr *h, u32 *held, i64 nr, i64 a0, i
mcopy(out + 2, rw, rl + 1);
return sc(nr, a0, (i64)out, (i64)(2 + rl + 1), 0, 0, 0);
}
- u64 hash = 14695981039346656037ULL;
- for (const char *p = rw; *p; p++) {
- hash ^= (unsigned char)*p;
- hash *= 1099511628211ULL;
+ u64 cut = rl;
+ while (cut > 0 && rw[cut - 1] != '/') cut--;
+ if (cut < 2 || cut == rl) return sc(nr, a0, a1, a2, 0, 0, 0);
+ rw[cut - 1] = '\0';
+ i64 dir = sc(NR_OPENAT, -100 /* AT_FDCWD */, (i64)rw, 0x200000 | 0x4000 | 0x80000 /* O_PATH | O_DIRECTORY | O_CLOEXEC */, 0, 0, 0);
+ if (dir < 0) return dir;
+ mcopy(out + 2, "/proc/self/fd/", 14);
+ u64 n = 14;
+ n += put_dec(out + 2 + n, (u64)dir);
+ out[2 + n++] = '/';
+ u64 name = rl - cut;
+ if (n + name + 1 > 108) {
+ sc(NR_CLOSE, dir, 0, 0, 0, 0, 0);
+ return -36 /* ENAMETOOLONG */;
}
- u64 n = h->root_len;
- if (n + 5 + 11 + 16 + 1 > 108) return sc(nr, a0, a1, a2, 0, 0, 0);
- mcopy(out + 2, h->root, n);
- mcopy(out + 2 + n, "/tmp/.ctx_sock_", 15);
- put_hex16(out + 2 + n + 15, hash);
- out[2 + n + 15 + 16] = '\0';
- return sc(nr, a0, (i64)out, (i64)(2 + n + 15 + 16 + 1), 0, 0, 0);
+ mcopy(out + 2 + n, rw + cut, name + 1);
+ i64 r = sc(nr, a0, (i64)out, (i64)(2 + n + name + 1), 0, 0, 0);
+ sc(NR_CLOSE, dir, 0, 0, 0, 0, 0);
+ return r;
}
// ---- Starting programs -------------------------------------------------------------------
diff --git a/tools/nativeguest/hook/fileops.c b/tools/nativeguest/hook/fileops.c
index b20c07f814..741bfd1d60 100644
--- a/tools/nativeguest/hook/fileops.c
+++ b/tools/nativeguest/hook/fileops.c
@@ -113,7 +113,6 @@ int unlink(const char *pathname) {
if (!orig_unlink) orig_unlink = (int (*)(const char *))dlsym(RTLD_NEXT, "unlink");
char buf[PATH_MAX];
const char *target = rewrite_path(pathname, buf, sizeof(buf));
- unlink_mapped_unix_socket(target);
dev_t hl_dev = 0;
ino_t hl_ino = 0;
int hardlinked = lookup_emulated_hardlink_at(AT_FDCWD, target, &hl_dev, &hl_ino);
@@ -137,10 +136,7 @@ int unlinkat(int dirfd, const char *pathname, int flags) {
dev_t hl_dev = 0;
ino_t hl_ino = 0;
int hardlinked = 0;
- if (target) {
- unlink_mapped_unix_socket(target);
- hardlinked = lookup_emulated_hardlink_at(dirfd, target, &hl_dev, &hl_ino);
- }
+ if (target) hardlinked = lookup_emulated_hardlink_at(dirfd, target, &hl_dev, &hl_ino);
int ret = orig_unlinkat ? orig_unlinkat(dirfd, target, flags) : -1;
if (ret != 0 && orig_unlinkat && refused_in_repaired_dir(dirfd, target)) {
hardlinked = target ? lookup_emulated_hardlink_at(dirfd, target, &hl_dev, &hl_ino) : 0;
diff --git a/tools/nativeguest/hook/setup.c b/tools/nativeguest/hook/setup.c
index 9befbed47e..3468b57b9c 100644
--- a/tools/nativeguest/hook/setup.c
+++ b/tools/nativeguest/hook/setup.c
@@ -462,63 +462,47 @@ static const char *usrmerge_retry_path(const char *target, char *alt, size_t alt
return alt;
}
-static const char *rewrite_unix_socket_path(const char *sun_path, char *out_buf, size_t out_size) {
- if (!sun_path || out_size == 0) return sun_path;
- // Abstract sockets start with null byte (\0); leave untouched
- if (sun_path[0] == '\0') {
- return sun_path;
- }
-
+static int unix_socket_address(const char *sun_path, struct sockaddr_un *out, int *dir_fd) {
char full[PATH_MAX];
+ *dir_fd = -1;
+ memset(out, 0, sizeof(*out));
+ out->sun_family = AF_UNIX;
const char *rw = (sun_path[0] == '/') ? rewrite_path(sun_path, full, sizeof(full)) : sun_path;
size_t rw_len = strlen(rw);
-
- // If rewritten path fits safely in sockaddr_un.sun_path (usually 108 bytes)
- if (rw_len < out_size) {
- strncpy(out_buf, rw, out_size - 1);
- out_buf[out_size - 1] = '\0';
- return out_buf;
+ if (rw_len < sizeof(out->sun_path)) {
+ memcpy(out->sun_path, rw, rw_len + 1);
+ return 0;
}
-
- // Path exceeds sockaddr_un 108-byte limit!
- // Deterministically hash the path into /tmp to guarantee it fits safely (< 80 bytes)
- // while remaining 100% deterministic between server and client.
- unsigned long long hash = 14695981039346656037ULL;
- for (const char *p = rw; *p; p++) {
- hash ^= (unsigned char)(*p);
- hash *= 1099511628211ULL;
+ const char *slash = strrchr(rw, '/');
+ if (!slash || slash == rw || !slash[1]) {
+ errno = ENAMETOOLONG;
+ return -1;
}
-
- init_ddnatv_hook();
- if (g_ddnatv_root[0] != '\0') {
- snprintf(out_buf, out_size, "%s/tmp/.ctx_sock_%016llx", g_ddnatv_root, hash);
- } else {
- snprintf(out_buf, out_size, "/tmp/.ctx_sock_%016llx", hash);
+ char dir[PATH_MAX];
+ size_t dir_len = (size_t)(slash - rw);
+ if (dir_len >= sizeof(dir)) {
+ errno = ENAMETOOLONG;
+ return -1;
}
- out_buf[out_size - 1] = '\0';
- return out_buf;
+ memcpy(dir, rw, dir_len);
+ dir[dir_len] = '\0';
+ int fd = (int)syscall(SYS_openat, AT_FDCWD, dir, O_PATH | O_DIRECTORY | O_CLOEXEC);
+ if (fd < 0) return -1;
+ int written = snprintf(out->sun_path, sizeof(out->sun_path), "/proc/self/fd/%d/%s", fd, slash + 1);
+ if (written < 0 || (size_t)written >= sizeof(out->sun_path)) {
+ syscall(SYS_close, fd);
+ errno = ENAMETOOLONG;
+ return -1;
+ }
+ *dir_fd = fd;
+ return 0;
}
-static void unlink_mapped_unix_socket(const char *target) {
- if (!target) return;
- size_t t_len = strlen(target);
- if (t_len >= 107) {
- unsigned long long hash = 14695981039346656037ULL;
- for (const char *p = target; *p; p++) {
- hash ^= (unsigned char)(*p);
- hash *= 1099511628211ULL;
- }
- char short_path[PATH_MAX];
- init_ddnatv_hook();
- if (g_ddnatv_root[0] != '\0') {
- snprintf(short_path, sizeof(short_path), "%s/tmp/.ctx_sock_%016llx", g_ddnatv_root, hash);
- } else {
- snprintf(short_path, sizeof(short_path), "/tmp/.ctx_sock_%016llx", hash);
- }
- static int (*orig_unlink)(const char *) = NULL;
- if (!orig_unlink) orig_unlink = (int (*)(const char *))dlsym(RTLD_NEXT, "unlink");
- if (orig_unlink) orig_unlink(short_path);
- }
+static void release_socket_dir(int dir_fd) {
+ if (dir_fd < 0) return;
+ int saved_errno = errno;
+ syscall(SYS_close, dir_fd);
+ errno = saved_errno;
}
#endif /* !DDNATV_HOST_TEST */
diff --git a/tools/nativeguest/hook/sockets.c b/tools/nativeguest/hook/sockets.c
index b4b12f8005..60f1b70cc2 100644
--- a/tools/nativeguest/hook/sockets.c
+++ b/tools/nativeguest/hook/sockets.c
@@ -109,12 +109,13 @@ int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
const struct sockaddr_un *sun = (const struct sockaddr_un *)addr;
if (sun->sun_path[0] != '\0') {
struct sockaddr_un mod_sun;
- memset(&mod_sun, 0, sizeof(mod_sun));
- mod_sun.sun_family = AF_UNIX;
char guest[sizeof(sun->sun_path) + 1];
copy_unix_path(sun, addrlen, guest);
- rewrite_unix_socket_path(guest, mod_sun.sun_path, sizeof(mod_sun.sun_path));
- return orig_bind ? orig_bind(sockfd, (struct sockaddr *)&mod_sun, sizeof(mod_sun)) : -1;
+ int dir_fd;
+ if (unix_socket_address(guest, &mod_sun, &dir_fd) != 0) return -1;
+ int ret = orig_bind ? orig_bind(sockfd, (struct sockaddr *)&mod_sun, sizeof(mod_sun)) : -1;
+ release_socket_dir(dir_fd);
+ return ret;
}
}
int ret = orig_bind ? orig_bind(sockfd, addr, addrlen) : -1;
@@ -139,14 +140,15 @@ int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
const struct sockaddr_un *sun = (const struct sockaddr_un *)addr;
if (sun->sun_path[0] != '\0') {
struct sockaddr_un mod_sun;
- memset(&mod_sun, 0, sizeof(mod_sun));
- mod_sun.sun_family = AF_UNIX;
char guest[sizeof(sun->sun_path) + 1];
copy_unix_path(sun, addrlen, guest);
char followed[PATH_MAX];
const char *path = guest[0] == '/' ? follow_guest_link(guest, followed, sizeof(followed)) : guest;
- rewrite_unix_socket_path(path, mod_sun.sun_path, sizeof(mod_sun.sun_path));
- return orig_connect ? orig_connect(sockfd, (struct sockaddr *)&mod_sun, sizeof(mod_sun)) : -1;
+ int dir_fd;
+ if (unix_socket_address(path, &mod_sun, &dir_fd) != 0) return -1;
+ int ret = orig_connect ? orig_connect(sockfd, (struct sockaddr *)&mod_sun, sizeof(mod_sun)) : -1;
+ release_socket_dir(dir_fd);
+ return ret;
}
}
diff --git a/tools/tests/test_native_hook_long_socket.py b/tools/tests/test_native_hook_long_socket.py
new file mode 100644
index 0000000000..d1f8c12eb1
--- /dev/null
+++ b/tools/tests/test_native_hook_long_socket.py
@@ -0,0 +1,144 @@
+import os
+from pathlib import Path
+import subprocess
+import tempfile
+import unittest
+
+from tools.tests.test_usr2_preload import runner
+
+ROOT = Path(__file__).resolve().parents[2]
+HOOK_BUILD = ROOT / "tools/nativeguest/build.sh"
+LISTEN_SRC = ROOT / "app/src/main/cpp/waylandcomp/src/unix_listen.c"
+SUN_PATH = 108
+GUEST_SOCKET = "/run/droiddeck/wayland-0"
+
+HOOKED = r"""
+#include
+#include
+#include
+#include
+#include
+
+int main(int argc, char **argv) {
+ struct sockaddr_un addr = { .sun_family = AF_UNIX };
+ strncpy(addr.sun_path, argv[2], sizeof(addr.sun_path) - 1);
+ int fd = socket(AF_UNIX, SOCK_STREAM, 0);
+ if (strcmp(argv[1], "serve") == 0) {
+ if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) != 0 || listen(fd, 4) != 0) {
+ perror("serve");
+ return 1;
+ }
+ puts("ready");
+ fflush(stdout);
+ int peer = accept(fd, NULL, NULL);
+ char got = 0;
+ if (peer < 0 || read(peer, &got, 1) != 1) return 1;
+ printf("got %c\n", got);
+ return 0;
+ }
+ if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
+ perror("connect");
+ return 1;
+ }
+ return write(fd, "x", 1) == 1 ? 0 : 1;
+}
+"""
+
+PLAIN_LISTENER = r"""
+#include
+#include
+#include
+#include "unix_listen.h"
+
+int main(int argc, char **argv) {
+ int fd = droiddeck_unix_listen(argv[1], "wayland-0", 4);
+ if (fd < 0) {
+ perror("listen");
+ return 1;
+ }
+ puts("ready");
+ fflush(stdout);
+ int peer = accept(fd, NULL, NULL);
+ char got = 0;
+ if (peer < 0 || read(peer, &got, 1) != 1) return 1;
+ printf("got %c\n", got);
+ return 0;
+}
+"""
+
+
+class LongSocketPathTest(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)
+ (tmp / "hooked.c").write_text(HOOKED)
+ (tmp / "listener.c").write_text(PLAIN_LISTENER)
+ subprocess.run([cls.cc, "-O1", "-o", str(tmp / "hooked"), str(tmp / "hooked.c")], check=True)
+ subprocess.run([cls.cc, "-O1", "-D_GNU_SOURCE", "-I", str(LISTEN_SRC.parent), "-o", str(tmp / "listener"),
+ str(tmp / "listener.c"), str(LISTEN_SRC)], check=True)
+ cls.hooked = tmp / "hooked"
+ cls.listener = tmp / "listener"
+ cls.root = tmp / "root"
+ cls.root.mkdir()
+
+ @classmethod
+ def tearDownClass(cls):
+ cls.tmp.cleanup()
+
+ def runtime_dir(self, long):
+ directory = Path(tempfile.mkdtemp(dir=self.tmp.name))
+ while long and len(str(directory)) + len("/wayland-0") < SUN_PATH + 10:
+ directory = directory / ("e" * 30)
+ directory.mkdir(parents=True, exist_ok=True)
+ return directory
+
+ def start(self, argv, hooked, directory):
+ env = {k: v for k, v in os.environ.items() if k != "LD_PRELOAD"}
+ hook_env = {"DDNATV_ROOT": str(self.root), "DDNATV_BINDS": f"{directory}:/run/droiddeck", "LD_PRELOAD": str(self.hook)}
+ cmd = list(self.prefix)
+ if hooked:
+ if self.prefix:
+ cmd += [arg for key, value in hook_env.items() for arg in ("-E", f"{key}={value}")]
+ else:
+ env.update(hook_env)
+ return subprocess.Popen(cmd + [str(a) for a in argv], env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
+
+ def exchange(self, directory, server_hooked):
+ server = self.start([self.hooked, "serve", GUEST_SOCKET] if server_hooked else [self.listener, directory], server_hooked, directory)
+ try:
+ self.assertEqual("ready", server.stdout.readline().strip(), server.stderr.read() if server.poll() is not None else "")
+ client = self.start([self.hooked, "connect", GUEST_SOCKET], True, directory)
+ _, client_err = client.communicate(timeout=30)
+ self.assertEqual(0, client.returncode, client_err)
+ out, err = server.communicate(timeout=30)
+ self.assertEqual(0, server.returncode, err)
+ self.assertIn("got x", out)
+ finally:
+ server.kill()
+ server.communicate()
+
+ def test_hooked_client_reaches_a_listener_at_a_path_longer_than_sun_path(self):
+ directory = self.runtime_dir(True)
+ self.assertGreaterEqual(len(str(directory)) + len("/wayland-0"), SUN_PATH)
+ self.exchange(directory, False)
+
+ def test_hooked_server_and_client_share_a_path_longer_than_sun_path(self):
+ directory = self.runtime_dir(True)
+ self.exchange(directory, True)
+ self.assertTrue((directory / "wayland-0").exists())
+
+ def test_a_path_that_fits_is_unchanged(self):
+ directory = self.runtime_dir(False)
+ self.assertLess(len(str(directory)) + len("/wayland-0"), SUN_PATH)
+ self.exchange(directory, True)
+ self.assertTrue((directory / "wayland-0").exists())
+
+
+if __name__ == "__main__":
+ unittest.main()