Handle unnamed macOS Unix sockets

This commit is contained in:
Minh Vu 2026-07-10 00:50:30 +02:00
parent c3e209dce4
commit 1135bfdf99
1 changed files with 14 additions and 1 deletions

View File

@ -95,7 +95,11 @@ fn validate_vm_fd(vm_fd: RawFd) -> Result<()> {
});
}
if address.ss_family as libc::c_int != libc::AF_UNIX {
// macOS returns a zero-length address for unnamed UNIX-domain sockets,
// including socketpair descriptors. Other socket families return their
// address family when getsockname succeeds.
let is_unix_socket = address_len == 0 || address.ss_family as libc::c_int == libc::AF_UNIX;
if !is_unix_socket {
bail!("VM file descriptor {vm_fd} is not a Unix socket");
}
@ -112,6 +116,7 @@ impl AsRawFd for VM {
mod tests {
use super::VM;
use std::fs::File;
use std::net::UdpSocket;
use std::os::fd::AsRawFd;
use std::os::unix::net::{UnixDatagram, UnixStream};
@ -157,6 +162,14 @@ mod tests {
assert!(error.to_string().contains("not a Unix datagram socket"));
}
#[test]
fn test_new_rejects_internet_datagram_socket() {
let socket = UdpSocket::bind("127.0.0.1:0").unwrap();
let error = VM::new(socket.as_raw_fd()).err().unwrap();
assert!(error.to_string().contains("not a Unix socket"));
}
#[test]
fn test_new_does_not_close_original_fd_when_vm_is_dropped() {
let (socket, _peer) = UnixDatagram::pair().unwrap();