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.
Design
Section titled “Design”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).
Hand-off on macOS
Section titled “Hand-off on macOS”- A vsock connection’s descriptor is a Unix stream socket to
Virtualization’s own service process, which moves the bytes to the
device. The receiver can’t read the vsock ports from it, so
wb-vmdsends them along. - The hand-off channel is a socketpair that
wb-hostdpasses towb-vmdat spawn, with no path. Its type isSOCK_DGRAM, because macOS has noSOCK_SEQPACKETfor Unix domain sockets (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”). There is one for each VM’swb-vmd(S04-03-self-sandboxingS04-03-self-sandboxing: Self-SandboxingHow each host daemon confines itself at startup, and the wb-launcher that starts confined programs on macOS., “Each host daemon is self-sandboxed”). - Each message carries exactly one descriptor, and these fields: kind
(a guest connection, or the packet transport of 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.,
“Network device per slot”), request id, VM generation, direction,
source port and destination port. The message is
HandoffMessageinproto/wraithbox/vmd/v1/vmd.proto(S04-08-records-and-boundsS04-08-records-and-bounds: Records, Versions and BoundsThe records daemons send wb-hostd, the build version rules between processes, the bounds on host work a guest can cause, and logs.). - The receiver refuses a message it didn’t ask for, or with a wrong
descriptor count or socket type. It closes any descriptor in it, and
logs the refusal.
HandoffMessage’s comments list every rule.
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 connection
Section titled “The host opens every connection”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.).
wb-vmddoesn’t register a host vsock listener, so nothing in the guest can dial the host. The static check of “What only the code keeps out” refuses the listener class in the sources.wb-hostdaskswb-vmdto connect towb-guestd: at boot, after each restore, and afterwb-guestdrestarts.wb-hostdroutes a connection only on its destination port. That is the guest port it askedwb-vmdto connect to.wb-hostdasks only for the ports in a fixed table in its code, all below 1024. In V1-M2-walking-skeletonV1-M2-walking-skeleton: Walking skeletonwb-vmd boots a fixture VM bundle in its confined process, wb-guestd answers over vsock, a data disk made on the host holds the homes, and wb shell opens a PTY as a project user, with its output through the terminal fi… the table holdswb-guestd’s port. Any other port is refused withguest-port-unroutedbeforewb-vmdis asked.- S15-05-admin-pathS15-05-admin-path: The Admin PathHow the person at the host's terminal gets a root shell in their own guest, on a path that no agent, session, or guest process can start or reach. adds
wb-guestroot’s port to the table.wb-hostdasks for it only forwb vm admin, and refuses any other ask for it withadmin-port-unrequested. - Each connection is for one VM generation. A connection that opens after its generation’s run ended is closed at once.
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 from the guest
Section titled “Requests from the guest”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-guestdrefuses 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-hostdrefuses 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.
Guest ports below 1024
Section titled “Guest ports below 1024”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.):
wb-guestdlistens on a port below 1024, and every guest portwb-hostdconnects to is below 1024.- The guest kernel lets only root bind those ports. So no project
process can take one while
wb-guestdrestarts, inside the Seatbelt profile or outside it. - The session profile also denies
AF_VSOCKsockets (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.).
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.
Caps and closing
Section titled “Caps and closing”wb-vmdcaps the connects it has started and whose connection it hasn’t closed, at a value in its code (8), so a passed connection counts untilwb-vmdcloses its copy. Beyond the cap,ConnectGuestis refused withconnect-cap. A connect that timed out keeps its place until the framework completes it, and its connection is closed.wb-hostdcaps live connections per VM (guest-conn-cap-vm) and per project (guest-conn-cap-project), and the connections it opens to one VM in a minute (guest-reconnect-rate, S06-11-reconnect-and-recoveryS06-11-reconnect-and-recovery: Reconnect, Loss and RecoveryWhat happens when the host-guest link drops, when a session is lost, and how a lost session's work is recovered or discarded., “Reconnect”), at values in its code. Over a cap a connection is refused, never queued.- Both log each refusal (SEC13-bounded-resourcesSEC13-bounded-resources: Bounded resourcesCPU, memory and disk of each VM are capped.),
wb-hostdat most once per rule per VM in a window, with the count it held back. - Every connection of a VM closes when its run ends, so before its
generation changes. A copy
wb-vmdkeeps closes up to 5 s later. wb-vmdanswersConnectGuestafter it passed the descriptor, and keeps its copy untilwb-hostdholds it (“Hand-off on macOS”). The descriptor then works whilewb-vmdis stopped too.
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).
Restore
Section titled “Restore”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:
- starts a new
wb-netdon 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.); - resets that VM’s TCP flows and their
wb-proxydstreams.
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.
The device set is fixed and checked
Section titled “The device set is fixed and checked”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.).
- Check before start. Before every start and restore,
wb-vmdchecks its own description of the configuration against that set. It does that before it creates any Virtualization device or attachment object. It refuses any other device, any other attachment, a second network device, and a disk or serial port file outside the VM’s bundle. The network device per slot is 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., “Network device per slot”. Each refusal is logged with its rule, and returns the rule in the gRPC error. - Why first. Creating a disk attachment on a path the profile denies ends the confined process in an uncaught exception (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?).
- Profile. On macOS,
wb-vmd’s sandbox profile also refuses a shared directory, host audio input, and a disk or serial port file outside the bundle. It denies the lookup of the host clipboard service.
What only the code keeps out
Section titled “What only the code keeps out”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-netdandwb-proxyd; - a disk in the bundle;
- a device on a descriptor
wb-vmdholds.
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:
- 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…);
- 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.); - a static check in CI, the SwiftLint custom rule
vmd_refused_device_classin.swiftlint.yml. It rejects at least these in the Swift package’s sources outside its tests:VZNATNetworkDeviceAttachment,VZBridgedNetworkDeviceAttachment,VZSharedDirectory,VZVirtioFileSystemDeviceConfiguration,VZHostAudioInputStreamSource,VZSpiceAgentPortAttachmentandVZVirtioSoundDeviceInputStreamConfiguration. 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, andsetSocketListener(“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-proxydchecked and kept. - What
wb-netdwrites 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