diff --git a/tools/fex/patches/0013-path-syscalls-through-libc.patch b/tools/fex/patches/0013-path-syscalls-through-libc.patch index 122f7654d2..1aa641b311 100644 --- a/tools/fex/patches/0013-path-syscalls-through-libc.patch +++ b/tools/fex/patches/0013-path-syscalls-through-libc.patch @@ -1,5 +1,5 @@ diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/Syscalls/Passthrough.cpp b/Source/Tools/LinuxEmulation/LinuxSyscalls/Syscalls/Passthrough.cpp -index 11ff163..085a621 100644 +index 11ff163..4c3be85 100644 --- a/Source/Tools/LinuxEmulation/LinuxSyscalls/Syscalls/Passthrough.cpp +++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/Syscalls/Passthrough.cpp @@ -209,6 +209,41 @@ uint64_t SyscallPassthrough7(FEXCore::Core::CpuStateFrame* Frame, uint64_t arg1, @@ -44,6 +44,20 @@ index 11ff163..085a621 100644 static void RegisterCommon(FEX::HLE::SyscallHandler* Handler) { REGISTER_SYSCALL_IMPL(read, SyscallPassthrough3); REGISTER_SYSCALL_IMPL(write, SyscallPassthrough3); +@@ -220,11 +255,11 @@ static void RegisterCommon(FEX::HLE::SyscallHandler* Handler) { + REGISTER_SYSCALL_IMPL(shmctl, SyscallPassthrough3); + REGISTER_SYSCALL_IMPL(getpid, SyscallPassthrough0); + REGISTER_SYSCALL_IMPL(socket, SyscallPassthrough3); +- REGISTER_SYSCALL_IMPL(connect, SyscallPassthrough3); ++ REGISTER_SYSCALL_IMPL(connect, SyscallPathPassthrough3); + REGISTER_SYSCALL_IMPL(sendto, SyscallPassthrough6); + REGISTER_SYSCALL_IMPL(recvfrom, SyscallPassthrough6); + REGISTER_SYSCALL_IMPL(shutdown, SyscallPassthrough2); +- REGISTER_SYSCALL_IMPL(bind, SyscallPassthrough3); ++ REGISTER_SYSCALL_IMPL(bind, SyscallPathPassthrough3); + REGISTER_SYSCALL_IMPL(listen, SyscallPassthrough2); + REGISTER_SYSCALL_IMPL(getsockname, SyscallPassthrough3); + REGISTER_SYSCALL_IMPL(getpeername, SyscallPassthrough3); @@ -238,9 +273,9 @@ static void RegisterCommon(FEX::HLE::SyscallHandler* Handler) { REGISTER_SYSCALL_IMPL(flock, SyscallPassthrough2); REGISTER_SYSCALL_IMPL(fsync, SyscallPassthrough1); diff --git a/tools/nativeguest/hook/privileged.c b/tools/nativeguest/hook/privileged.c index 09a1074b86..d5639b450e 100644 --- a/tools/nativeguest/hook/privileged.c +++ b/tools/nativeguest/hook/privileged.c @@ -708,6 +708,16 @@ long syscall(long number, ...) { return (long)utimensat((int)arg1, (const char *)arg2, (const struct timespec *)arg3, (int)arg4); } #endif +#if defined(__NR_connect) + if (number == __NR_connect) { + return (long)connect((int)arg1, (const struct sockaddr *)arg2, (socklen_t)arg3); + } +#endif +#if defined(__NR_bind) + if (number == __NR_bind) { + return (long)bind((int)arg1, (const struct sockaddr *)arg2, (socklen_t)arg3); + } +#endif #if defined(__NR_truncate) if (number == __NR_truncate) { return (long)truncate((const char *)arg1, (off_t)arg2); diff --git a/tools/nativeguest/hook/sockets.c b/tools/nativeguest/hook/sockets.c index 2903505f9b..b4b12f8005 100644 --- a/tools/nativeguest/hook/sockets.c +++ b/tools/nativeguest/hook/sockets.c @@ -94,6 +94,13 @@ int listen(int sockfd, int backlog) { return ret; } +static void copy_unix_path(const struct sockaddr_un *sun, socklen_t addrlen, char *out) { + size_t max = addrlen - offsetof(struct sockaddr_un, sun_path); + if (max > sizeof(sun->sun_path)) max = sizeof(sun->sun_path); + memcpy(out, sun->sun_path, max); + out[max] = '\0'; +} + int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) { static int (*orig_bind)(int, const struct sockaddr *, socklen_t) = NULL; if (!orig_bind) orig_bind = (int (*)(int, const struct sockaddr *, socklen_t))dlsym(RTLD_NEXT, "bind"); @@ -104,7 +111,9 @@ int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) { struct sockaddr_un mod_sun; memset(&mod_sun, 0, sizeof(mod_sun)); mod_sun.sun_family = AF_UNIX; - rewrite_unix_socket_path(sun->sun_path, mod_sun.sun_path, sizeof(mod_sun.sun_path)); + 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; } } @@ -133,10 +142,7 @@ int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) { memset(&mod_sun, 0, sizeof(mod_sun)); mod_sun.sun_family = AF_UNIX; char guest[sizeof(sun->sun_path) + 1]; - size_t max = addrlen - offsetof(struct sockaddr_un, sun_path); - if (max > sizeof(sun->sun_path)) max = sizeof(sun->sun_path); - memcpy(guest, sun->sun_path, max); - guest[max] = '\0'; + 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)); diff --git a/tools/tests/test_native_hook_path_syscalls.py b/tools/tests/test_native_hook_path_syscalls.py index ab69fc3c83..be0967cbb0 100644 --- a/tools/tests/test_native_hook_path_syscalls.py +++ b/tools/tests/test_native_hook_path_syscalls.py @@ -12,9 +12,12 @@ PROBE = r""" #define _GNU_SOURCE #include #include +#include #include #include +#include #include +#include #include static void show(const char *name, long ret) { printf("%s\t%d\n", name, ret >= 0); } @@ -40,6 +43,29 @@ int main(int argc, char **argv) { snprintf(q, sizeof(q), "%s/made/link", dir); show("symlinkat", syscall(SYS_symlinkat, "file", AT_FDCWD, q)); show("unlinkat", syscall(SYS_unlinkat, AT_FDCWD, q, 0)); + snprintf(q, sizeof(q), "%s/made/sock", dir); + struct sockaddr_un addr = {.sun_family = AF_UNIX}, extra = {.sun_family = AF_UNIX}; + size_t len = strlen(q); + int fits = len + sizeof("-extra") <= sizeof(addr.sun_path); + if (fits) { + memcpy(addr.sun_path, q, len); + memcpy(extra.sun_path, q, len); + memcpy(extra.sun_path + len, "-extra", sizeof("-extra")); + } + int server = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0), client = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); + show("bind", fits ? syscall(SYS_bind, server, &addr, sizeof(addr)) : -1); + listen(server, 1); + show("connect", fits ? syscall(SYS_connect, client, &addr, sizeof(addr)) : -1); + close(client); + close(server); + syscall(SYS_unlinkat, AT_FDCWD, q, 0); + server = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0), client = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); + show("bind-length", fits ? syscall(SYS_bind, server, &extra, offsetof(struct sockaddr_un, sun_path) + len) : -1); + listen(server, 1); + show("connect-length", fits ? syscall(SYS_connect, client, &addr, sizeof(addr)) : -1); + close(client); + close(server); + syscall(SYS_unlinkat, AT_FDCWD, q, 0); snprintf(q, sizeof(q), "%s/made", dir); show("chdir", syscall(SYS_chdir, q)); show("cwd", getcwd(cwd, sizeof(cwd)) && strstr(cwd, "/made") ? 0 : -1); @@ -88,7 +114,7 @@ __attribute__((constructor)) static void early(void) { free(malloc(1)); } """ CALLS = ("mkdirat", "openat", "truncate", "fchmodat", "utimensat", "futimens", "renameat", "renameat2", "symlinkat", "unlinkat", - "chdir", "cwd", "unlinkat-file", "unlinkat-dir") + "bind", "connect", "bind-length", "connect-length", "chdir", "cwd", "unlinkat-file", "unlinkat-dir") class NativeHookPathSyscallsTest(unittest.TestCase): @@ -97,7 +123,7 @@ class NativeHookPathSyscallsTest(unittest.TestCase): cls.cc, cls.prefix = runner() if cls.cc is None: raise unittest.SkipTest("no aarch64 compiler and runner") - cls.tmp = tempfile.TemporaryDirectory() + 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)