Skip to content

S04-02 - VM Provider and Streams

Purpose: How wb-vmd hands guest connections and the NIC endpoint to other processes, the fixed device set of a VM, and where wb-proxyd takes a stream’s identity from.

The VM provider passes descriptors, not bytes

Section titled “The VM provider passes descriptors, not bytes”

wb-vmd opens host-guest socket connections and creates the NIC endpoint. Then it hands the descriptors (handles on Windows) to wb-hostd. wb-hostd passes the NIC endpoint on to that VM’s wb-netd (S07-egress-gatewayS07-egress-gateway: Egress GatewayThe only path from a guest to any network: a userspace network stack (wb-netd) and a policy-enforcing proxy (wb-proxyd) on the host.).

Guest bytes stay out of wb-vmd’s code path. It does not relay or parse guest traffic, so the native code has the smallest possible exposure to guest input.

The device set of a VM is fixed in wb-vmd’s code. No device type is ever taken from the contract (“The device set is fixed and checked”, below).

On macOS (X18-vsock-handoffX18-vsock-handoff: Host-guest socket and descriptor hand-offCan wb-guestd, written in Go, open AFVSOCK sockets in a macOS guest?):

wb-hostd’s receiver is internal/platform/vmhandoff. It reads through the channel of internal/platform/fdchan, which refuses a record over 256 bytes or truncated (fdchan-record-size, fdchan-control-truncated) and two or more descriptors (fdchan-fd-count). It treats each record as untrusted input, because wb-vmd runs framework code on the guest’s behalf, and refuses each case the comment on HandoffMessage lists with the rule it names there. A refused record ends the open request it names. A request of a newer generation ends every open request of the older one.

Records go both ways on the channel. The receiver answers each record that decodes, accepted or refused, with a HandoffAck once recvmsg has returned. wb-vmd keeps its copy of each descriptor it sent until that acknowledgment, or for the connect timeout of 5 s at most, as S04-12-descriptor-passingS04-12-descriptor-passing: Passing DescriptorsHow one host process passes an open socket to another: who holds it when, how the receiver shows it holds it, when the sender closes its copy, and what a lost or bad acknowledgment does. asks. It checks each acknowledgment as input, refuses one of the wrong form (handoff-ack-malformed) or for a descriptor it doesn’t keep (handoff-ack-unexpected), and reads on. It logs a deadline that passes (handoff-ack-timeout) and a failed receive it skips (handoff-ack-receive-failed). Its reader ends, logged handoff-ack-channel-ended, only as S04-12-descriptor-passingS04-12-descriptor-passing: Passing DescriptorsHow one host process passes an open socket to another: who holds it when, how the receiver shows it holds it, when the sender closes its copy, and what a lost or bad acknowledgment does. says. wb-hostd logs an acknowledgment it can’t send (handoff-ack-send-failed). HandoffAck’s comments list these rules.

The host opens every host-guest connection (B29-vsock-handoffB29-vsock-handoff: Can wb-vmd hand guest connections to our Go daemons as descriptors, also across a restore?Yes, with conditions.).

A connection the guest opened would say nothing trustworthy about who opened it. Every process in the guest that can open AF_VSOCK can dial, and it picks its own source port.

Requests that start in the guest, from git-remote-wb and guest events, go to wb-guestd over a local Unix socket. There the peer user ID names the project user (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.).

wb-guestd sends them to wb-hostd on a connection the host opened. That is either multiplexed on an existing connection, or a further connection the host opens at wb-guestd’s request. Each request names the project it came from. The stream framing on that connection is a parser of guest bytes with a fuzz target (C11-host-daemonsC11-host-daemons: Host Daemons, Audit, and DisksThe test cases for the host daemons' own checks: wb-vmd's device set and slots, the image build bundle, supervision, the accounting and event channels, the daemon log pipes, the audit writer, free space, and the data…).

Attribution fails closed (SEC08-proj-isolationSEC08-proj-isolation: Per-project isolationEach project has its own guest user, state and history, and one project cannot persist into another., SEC10-auditSEC10-audit: AuditEvery egress decision, approval, and returned change is recorded on the host in a persisted log the guest cannot alter.):

  • wb-guestd refuses a peer user ID that isn’t an active, unlocked project user of this VM. Root, system accounts, user IDs created at runtime, and a locked project user are all refused.
  • wb-hostd refuses a request for a project that isn’t in the VM’s effective policy.
  • Both log each refusal with the rule.

Root in the guest can still pose as any project user. T11-shared-vm-grantsT11-shared-vm-grants: Projects in one VM share network grantsThe host sees one guest address per VM, and can't tell which project user, session or program opened a connection. and the isolated slot cover that.

A project user can’t pose as wb-guestd (X27-vsock-confinementX27-vsock-confinement: vsock confinement of project usersCan a non-root process in a macOS guest open AFVSOCK, and bind or listen on wb-guestd's port while wb-guestd is running or being restarted?). On guests that use vsock (macOS and Linux, S06-vm-lifecycleS06-vm-lifecycle: VM Lifecycle, Images and the Guest AgentHow guest images are built, how VMs are kept warm, how projects and sessions map onto guests, and what wb-guestd does.):

From 1024 up, any guest user can bind a free port. In X27-vsock-confinementX27-vsock-confinement: vsock confinement of project usersCan a non-root process in a macOS guest open AFVSOCK, and bind or listen on wb-guestd's port while wb-guestd is running or being restarted? one answered the host as wb-guestd for 21.7 s while launchd restarted the daemon.

wb-guestd logs and skips a failed accept, and never stops listening because of one. On macOS 27.0.1 a guest process that connects to the guest’s own CID makes accept fail with ECONNABORTED (X27-vsock-confinementX27-vsock-confinement: vsock confinement of project usersCan a non-root process in a macOS guest open AFVSOCK, and bind or listen on wb-guestd's port while wb-guestd is running or being restarted?). On 27.2 a guest-side operation stopped the guest’s kernel, cause open in I615I615 (open): Guest kernel panic found in X31-admin-channel: narrow it down and gate the conformance casesFound in #448 (X31-admin-channel), by chance. (T22-guest-kernel-panicT22-guest-kernel-panic: A guest process can panic its own VM's kernelIn X31-admin-channel a guest-side operation by an unprivileged user in a macOS 27.2 guest stopped the guest's kernel, and the guest rebooted.).

A guest process can repeat that connect in a loop: connect has no port check, and each one takes a slot in the listen queue until the accept fails. So the listener bounds what a flood costs it (SEC13-bounded-resourcesSEC13-bounded-resources: Bounded resourcesCPU, memory and disk of each VM are capped.):

  • It closes a connection from any CID but the host’s, because only the host may talk to wb-guestd.
  • It logs failed accepts and refused peers at most once per 10 seconds for each kind. Each record carries the count it held back since the last one.
  • After an error other than an aborted connection, such as running out of descriptors, it pauses before the next accept, longer each time, up to a second.

A restart can find the port still held by the old process. Then wb-guestd retries bind with a backoff from 100 ms that doubles up to 2 s, and logs each try with its errno. It doesn’t exit: launchd would start it again only after its throttle of 10 s, and the project processes stay stopped while the link is down (S13-01-process-hardeningS13-01-process-hardening: Layer 1: Process Hardening (v1)How wb-guestd hardens every process it starts for a project user, and how it stops and kills a project user's processes., “When the link drops”). Any other bind error, EACCES included, makes it exit non-zero with the errno logged, because such an error means the install is broken.

ConnectGuest also refuses, each with its rule: a guest port of 1024 or more (guest-port-not-reserved), request ID 0 (vm-request-id), a generation that is 0 or not the current one (vm-generation), and a VM that isn’t running (vm-not-running). A connect that fails or takes longer than a timeout in code is refused with vm-connect-failed or vm-connect-timeout, and a record that can’t be sent with handoff-send-failed. After each of these refusals, wb-vmd hasn’t sent a record, and has closed any connection it opened.

On the host, internal/vmdlink refuses a guest port of 0 or of 1024 or more itself (guest-port-invalid) before it asks wb-vmd. It gives the ConnectGuest call a deadline of its own, longer than wb-vmd’s connect timeout, so a wb-vmd that doesn’t answer can’t hold the caller. Once the call returns, it waits a short time for the record (handoff-missing).

A restore ends every connection. The saved VM’s descriptors read EOF when it stops. The guest sees EOF on its old connections when the restored VM resumes.

A frame written to the old network descriptor raises no error and draws no answer. So before resume, wb-hostd:

  1. starts a new wb-netd on the new descriptor (S04-09-netd-startS04-09-netd-start: Starting `wb-netd`How wb-hostd starts and stops each VM's wb-netd, what the program inherits, its profile, and what it does before its start message., I590I590 (closed): S04-02-vm-provider: a restore starts a fresh wb-netd for its generationFollow-up from #511, its decision 6, which the PR for #511 doesn't build.);
  2. resets that VM’s TCP flows and their wb-proxyd streams.

It doesn’t reset the guest’s DHCP lease or the synthetic DNS name mapping that goes with it. Cached answers in the resumed guest then still work.

Each restore starts a new VM generation. Hand-off messages and audit entries carry it, and a message from an older generation is refused. wb-hostd connects to wb-guestd again after every restore.

A VM’s devices are the ones in S06-06-devices-and-warm-startS06-06-devices-and-warm-start: Devices, Warm Start and TimeA VM's devices and resources, saving and restoring its state, and how the guest clock follows the host., “Devices”, fixed in wb-vmd’s code (X25-vmd-sandboxX25-vmd-sandbox: Self-sandboxed `wb-vmd`Can wb-vmd confine itself at start with a Seatbelt profile that allows the Virtualization framework, its VM bundles under <data>/vms and nothing else, and still create, start, save and restore a macOS VM?, SEC02-no-host-fs-shareSEC02-no-host-fs-share: No host filesystem sharingNo host directory is mounted into the guest, read-only or otherwise., SEC05-default-denySEC05-default-deny: Default-deny egress, enforced on the hostOnly allowlisted hostnames are reachable.).

The profile can’t refuse these, so only wb-vmd’s code keeps them out:

  • a NAT network, which Apple’s Virtualization service provides and which would bypass wb-netd and wb-proxyd;
  • a disk in the bundle;
  • a device on a descriptor wb-vmd holds.

A bridged network failed both ways in X25-vmd-sandboxX25-vmd-sandbox: Self-sandboxed `wb-vmd`Can wb-vmd confine itself at start with a Seatbelt profile that allows the Virtualization framework, its VM bundles under <data>/vms and nothing else, and still create, start, save and restore a macOS VM?. Confined, the framework offered no host interface to bridge to. Unconfined, it refused the attachment without the com.apple.vm.networking entitlement, which wb-vmd isn’t signed with.

Three checks verify the parts the profile doesn’t enforce:

  1. a unit test of the check (C11-host-daemonsC11-host-daemons: Host Daemons, Audit, and DisksThe test cases for the host daemons' own checks: wb-vmd's device set and slots, the image build bundle, supervision, the accounting and event channels, the daemon log pipes, the audit writer, free space, and the data…);
  2. an in-guest conformance check that the guest has one network interface, with its lease from wb-netd (C01-network-and-dnsC01-network-and-dns: Network and DNSThe test cases for the guest's network path: the link endpoint, addressing, and the DNS gateway with its hold, quiet refusals, and negative answers.);
  3. a static check in CI, the SwiftLint custom rule vmd_refused_device_class in .swiftlint.yml. It rejects at least these in the Swift package’s sources outside its tests: VZNATNetworkDeviceAttachment, VZBridgedNetworkDeviceAttachment, VZSharedDirectory, VZVirtioFileSystemDeviceConfiguration, VZHostAudioInputStreamSource, VZSpiceAgentPortAttachment and VZVirtioSoundDeviceInputStreamConfiguration. It also names the classes of the other kinds the check refuses with a rule of their own (network, share, audio input, console and storage), and of USB passthrough and custom virtio devices. It refuses the host vsock listener too: VZVirtioSocketListener, its delegate protocol, and setSocketListener (“The host opens every connection”).

The static check excepts one file, Sources/wb-dev-bundle/ProvisionBoot.swift: the NAT network that test guests in the spike cache get on their first boot. It is in wb-dev-bundle, test tooling that is never in the app bundle (S10-tech-stackS10-tech-stack: Technology ChoicesFix languages, libraries, packaging, and repository layout, with the reason for each choice., “Test tooling”), and wb-vmd doesn’t link it. A second rule, dev_bundle_refused_device_class, checks that file against every other class, so it can add only the NAT network.

wb-proxyd takes a stream’s identity from the connection, not from wb-netd

Section titled “wb-proxyd takes a stream’s identity from the connection, not from wb-netd”

This serves SEC04-no-guest-secretsSEC04-no-guest-secrets: No secrets in the guestThis includes the model credential. and SEC08-proj-isolationSEC08-proj-isolation: Per-project isolationEach project has its own guest user, state and history, and one project cannot persist into another..

wb-hostd creates a hand-off socket for each VM at each start and restore, and at the restarts of S04-07-supervisionS04-07-supervision: SupervisionWho starts and restarts each host process, what happens when wb-hostd stops or the bundle is upgraded, and daemon outages., “Restarting a daemon”. It passes one end to wb-proxyd, naming the VM, and the other to that VM’s wb-netd.

  • A stream’s VM comes from the socket it arrived on. So do its policy, credential bindings and CA.
  • Its name comes from the name reports that wb-proxyd checked and kept.
  • What wb-netd writes with a stream can only narrow.

So a compromised wb-netd can’t label a stream as another VM’s, or as the stream of a project that has no session in the VM (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.).

Status: Draft