Skip to content

X18 - Host-guest socket and descriptor hand-off

Purpose: Whether wb-guestd in Go can use vsock in a macOS guest, and whether wb-vmd can hand vsock connections and the network endpoint to other processes as descriptors, also across save and restore.

Brief: B29-vsock-handoffB29-vsock-handoff: Can wb-vmd hand guest connections to our Go daemons as descriptors, also across a restore?Yes, with conditions.

From X00-indexX00-index: SpikesThe open questions that must be answered by a throwaway prototype before work builds on them, in the order they are taken up.: can wb-guestd, written in Go, open AF_VSOCK sockets in a macOS guest? Do the file descriptors of a vsock connection and of the file-handle network attachment keep working after wb-vmd passes them to another process (SCM_RIGHTS), and across save and restore? The maintainer’s comment on I29I29 (closed): X18-vsock-handoff: Host-guest socket and descriptor hand-offX18-vsock-handoff (added to S11-verification-and-spikes in #14) adds: measure the time from restoreMachineStateFrom to the Go listener answering, from a fresh APFS clone of the state and disks.

Yes, with conditions. Each part, with the evidence in “Measurements”:

  1. Go opens vsock in a macOS guest: yes. golang.org/x/sys/unix has AF_VSOCK, SockaddrVM and IOCTL_VM_SOCKETS_GET_LOCAL_CID for darwin, so wb-guestd doesn’t need cgo or a raw-syscall shim. A Go LaunchDaemon in the guest listened on port 1024, accepted connections, dialed the host (CID 2), and served gRPC on both. The guest’s CID was 3. The spike wraps each socket in an os.File, which the Go runtime poller handles. On the host, socket(AF_VSOCK) fails with “operation not supported by device”: host processes reach the guest only through Virtualization.

  2. A passed vsock descriptor works: yes. The descriptor that VZVirtioSocketConnection gives is not a vsock socket. It is a Unix-domain stream socket whose peer is Virtualization’s service process (com.apple.Virtualization.VirtualMachine), which moves the bytes to the virtual device. wb-vmd passed it to a Go process, and the Go process kept working in each of these cases:

    • wb-vmd kept the connection object open;
    • wb-vmd called close() on it right after the pass;
    • wb-vmd dropped its last reference without close();
    • wb-vmd’s main thread was blocked during the transfer;
    • the whole wb-vmd process was stopped with SIGSTOP while the Go process echoed 8 MiB over gRPC (43 ms) and the network answered.

    Connections the guest opens, accepted with VZVirtioSocketListener, behave the same. Half-close works in both directions: either side can shutdown(SHUT_WR), the other reads to EOF and can still write (the assumption of X07-git-round-tripX07-git-round-trip: Git round tripRemote helper over vsock, read-only upload-pack, restricted receive-pack into the landing repository, flagging of risky paths.).

  3. A passed network descriptor works: yes. wb-vmd passed the host end of the SOCK_DGRAM socket pair behind VZFileHandleNetworkDeviceAttachment and closed its own copy at once. The Go process exchanged frames with the guest from then on.

  4. Save and restore: yes, with conditions.

    • The VM saves with open connections. Resumed in the same VM, every passed descriptor and an open gRPC stream keep working.
    • A restore ends every connection. When the saved VM stops, every passed vsock descriptor of it reads EOF. In the restored VM, the guest sees EOF on every connection it had at the moment it resumes, while its listener keeps listening. A new connection is accepted 5 ms after resume, on the first try.
    • The restored VM gets a new network socket pair in its configuration (as in X02-warm-startX02-warm-start: Warm startMeasure time to restore a macOS guest from saved state with a vsock device and a file-handle network attachment.). Frames written to the old pair get no answer, and neither a read nor a write on it reports an error while the old VM object exists.
    • From a fresh APFS clone, the guest listener accepts 3.97 s (p50) and 4.15 s (p95) after the restore starts, and wb-guestd’s gRPC health check answers through the Go process at 4.13 s and 4.21 s.

The conditions:

  1. wb-hostd routes only on the destination port. The receiving process sees a Unix socket, so it can’t read the vsock ports from it, and wb-vmd sends them with the descriptor. The destination port is one wb-hostd chose: a guest port it asked wb-vmd to connect to, or a host listener registered at its request. The source port of a guest-initiated connection is chosen by whatever guest process dialed, so it is only logged. Any guest process that can open AF_VSOCK can dial a host listener, so guest-initiated connections are refused (recommended) or bound to a session by a one-time nonce from the host (open decision 1). S13-guest-confinementS13-guest-confinement: Guest ConfinementControls inside the guest that narrow what a project user's processes can do, and tell the host which program opened each connection. also denies AF_VSOCK to project processes, as a second layer.
  2. The hand-off channel carries one descriptor per message. The spike framed requests as lines on a stream socket and attached the descriptor to the first line, which can pair a descriptor with the wrong request when two arrive together. The product uses a SOCK_SEQPACKET or SOCK_DGRAM socketpair inherited at spawn, with exactly one descriptor and the fields request id, VM generation, direction, source port and destination port in each message. The receiver refuses, closes and logs on a wrong descriptor count, a wrong socket type or a destination port it didn’t ask for (S04-architectureS04-architecture: ArchitectureName the processes, the boundaries between them, and which requirement each boundary serves., contract in I52I52 (closed): S04-architecture, S09-policy-credentials-audit, S10-tech-stack: first .proto contracts and settings schemaS04-architecture says all IPC is protobuf over gRPC and S10-tech-stack adds proto/ "with the first RPC".).
  3. Every restore starts new connections and a new VM generation. wb-hostd drops its wb-guestd connections when a VM is saved, and connects again after the restore. Before resume, it passes the new network descriptor to wb-netd and resets that VM’s TCP flows and their wb-proxyd streams (not the guest’s DHCP lease or its synthetic DNS name mapping, so the resumed guest’s cached answers still work), because the old descriptor never reports an error. Hand-off messages and audit entries carry the VM generation, and a message from an older generation is refused.
  4. wb-vmd may close its copy of every passed descriptor at once. Nothing in wb-vmd has to remain for a passed descriptor to work, except the VM itself. Note (2026-11-10): “at once” doesn’t hold for a socket still in flight. On macOS 27 the kernel can take a socket whose every reference is in a message as garbage, and the receiver gets it dead (I637I637 (closed): Descriptor senders close their copy before the receiver holds it (darwin socket GC)Found while fixing #633.). wb-vmd keeps its copy of a passed socket until wb-hostd holds it (S07-05-stream-hand-offS07-05-stream-hand-off: Stream Hand-offHow wb-netd hands each stream to wb-proxyd, and the checks that keep a compromised wb-netd from widening what a stream may reach., “Transport”). After that, nothing in wb-vmd has to remain.
  5. Connections are capped. wb-vmd caps the connections it has accepted but not yet passed, per listener and in total, and wb-hostd caps them per VM and per project. Both log each refusal (SEC13-bounded-resourcesSEC13-bounded-resources: Bounded resourcesCPU, memory and disk of each VM are capped.).
  6. The device set is fixed in wb-vmd’s code. The contract never names a device type, so a request can’t add a NAT network or a shared directory to a VM.

Host: Apple M2, 8 cores, 16 GB, macOS 27.0.1 (26A434), Go 1.27.1, golang.org/x/sys 0.48.0, gRPC-Go 1.84.0. Guest: macOS 27.0.1, 4 vCPU, 4 GiB, the X02-warm-startX02-warm-start: Warm startMeasure time to restore a macOS guest from saved state with a vsock device and a file-handle network attachment. configuration (VZVirtioSocketDevice, VZFileHandleNetworkDeviceAttachment on a datagram socket pair, a 64 GiB system disk and an 8 GiB data disk, both sparse). The guest was provisioned once (see “How the listener got into the guest”) and then sat at the login window. The clock starts before the VZVirtualMachine is created, as in X02-warm-startX02-warm-start: Warm startMeasure time to restore a macOS guest from saved state with a vsock device and a file-handle network attachment..

Measurement p50 p95 Runs
Restore, fresh clone: restoreMachineStateFrom done 3.76 s 3.95 s 20
Restore, fresh clone: resume done 3.97 s 4.15 s 20
Restore, fresh clone: guest listener accepts 3.97 s 4.15 s 20
Restore, fresh clone: wb-guestd gRPC answers through Go 4.13 s 4.21 s 20
Restore, fresh clone: guest answers on the network 3.98 s 4.16 s 20
First gRPC health check after restore 155 ms 188 ms 20
Cold boot, fresh clone: guest listener accepts 10.93 s 13.48 s 10
saveMachineStateTo, 4 GiB, idle guest 3.06 s 1

A stock guest runs nothing on vsock (X02-warm-startX02-warm-start: Warm startMeasure time to restore a macOS guest from saved state with a vsock device and a file-handle network attachment.). This spike used VZMacGuestProvisioningOptions, new in macOS 27: the first boot after installing creates a user with Remote Login on. The setup script then connected over SSH, on a NAT network that only this setup boot had, installed the daemon as a root LaunchDaemon with sudo, and shut the guest down. It took 38 to 40 s from the first boot. No host process mounted a guest disk. This is a shortcut for the spike, not the product path, which X17-image-buildX17-image-build: Unattended image buildCan a macOS guest installed from an Apple restore image get past Setup Assistant, with wb-guestd running as a root LaunchDaemon, without SSH, scripted clicks, or MDM? decides. It is evidence for X17-image-buildX17-image-build: Unattended image buildCan a macOS guest installed from an Apple restore image get past Setup Assistant, with wb-guestd running as a root LaunchDaemon, without SSH, scripted clicks, or MDM? that provisioning works on a macOS 27 host and guest. The provisioned bundles have Remote Login on and an admin user with a password, so no product image may descend from them.

Branch spike/x18-vsock-handoff, at 6312bbc: the Swift wb-vmd stand-in, the Go guest daemon and host receiver, the provisioning script, the series script, and the raw results in results/ with their p50 and p95 in results/stats.txt.

Status: Answered 2026-10-05: yes, with conditions