fix(fex): x86 programs reach Unix sockets inside the rootfs (#3097)

* fix(fex): x86 programs reach Unix sockets inside the rootfs

* fix(nativeguest): bind uses only the socket path length the caller gives
This commit is contained in:
MaxsTechReview authored and GitHub committed 2026-11-23 18:48:52 -05:00
1 parent ec31c9e2b6
commit ba3f840b8d
4 files changed
+64 -8

No files matched your search

@@ -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<SYSCALL_DEF(read)>);
REGISTER_SYSCALL_IMPL(write, SyscallPassthrough3<SYSCALL_DEF(write)>);
@@ -220,11 +255,11 @@ static void RegisterCommon(FEX::HLE::SyscallHandler* Handler) {
REGISTER_SYSCALL_IMPL(shmctl, SyscallPassthrough3<SYSCALL_DEF(shmctl)>);
REGISTER_SYSCALL_IMPL(getpid, SyscallPassthrough0<SYSCALL_DEF(getpid)>);
REGISTER_SYSCALL_IMPL(socket, SyscallPassthrough3<SYSCALL_DEF(socket)>);
- REGISTER_SYSCALL_IMPL(connect, SyscallPassthrough3<SYSCALL_DEF(connect)>);
+ REGISTER_SYSCALL_IMPL(connect, SyscallPathPassthrough3<SYSCALL_DEF(connect)>);
REGISTER_SYSCALL_IMPL(sendto, SyscallPassthrough6<SYSCALL_DEF(sendto)>);
REGISTER_SYSCALL_IMPL(recvfrom, SyscallPassthrough6<SYSCALL_DEF(recvfrom)>);
REGISTER_SYSCALL_IMPL(shutdown, SyscallPassthrough2<SYSCALL_DEF(shutdown)>);
- REGISTER_SYSCALL_IMPL(bind, SyscallPassthrough3<SYSCALL_DEF(bind)>);
+ REGISTER_SYSCALL_IMPL(bind, SyscallPathPassthrough3<SYSCALL_DEF(bind)>);
REGISTER_SYSCALL_IMPL(listen, SyscallPassthrough2<SYSCALL_DEF(listen)>);
REGISTER_SYSCALL_IMPL(getsockname, SyscallPassthrough3<SYSCALL_DEF(getsockname)>);
REGISTER_SYSCALL_IMPL(getpeername, SyscallPassthrough3<SYSCALL_DEF(getpeername)>);
@@ -238,9 +273,9 @@ static void RegisterCommon(FEX::HLE::SyscallHandler* Handler) {
REGISTER_SYSCALL_IMPL(flock, SyscallPassthrough2<SYSCALL_DEF(flock)>);
REGISTER_SYSCALL_IMPL(fsync, SyscallPassthrough1<SYSCALL_DEF(fsync)>);
+10
View File
@@ -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);
+11 -5
View File
@@ -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));
+28 -2
View File
@@ -12,9 +12,12 @@ PROBE = r"""
#define _GNU_SOURCE
#include <fcntl.h>
#include <limits.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#include <sys/un.h>
#include <unistd.h>
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)