S07-05 - Stream Hand-off
Purpose: How wb-netd hands each stream to wb-proxyd, and the
checks that keep a compromised wb-netd from widening what a stream
may reach.
Design
Section titled “Design”Stream hand-off
Section titled “Stream hand-off”This page 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-netd parses raw guest frames. That makes it the process most
likely to be compromised (S04-architectureS04-architecture: ArchitectureName the processes, the boundaries between them, and which requirement each boundary serves.). wb-proxyd takes nothing
from it that widens what a stream may reach:
- A stream’s VM comes from a per-VM hand-off socket that
wb-hostdcreates and labels (“One hand-off socket per VM”). wb-proxydre-checks the name against its own copy of the name mapping. The copy is built from checked name reports (S07-06-name-mappingS07-06-name-mapping: Name MappingHow wb-proxyd keeps its own checked copy of the names wb-netd gave synthetic addresses: name reports, releases, replay, lifetime, the second count of the wildcard budget, and the copy wb-hostd keeps.).- What
wb-netdwrites with a stream can only narrow (“Only narrows”).
Transport
Section titled “Transport”Each channel on this page is a Unix domain socketpair with no path, of
type SOCK_SEQPACKET or SOCK_DGRAM, as for the other descriptor
channels (S04-architectureS04-architecture: ArchitectureName the processes, the boundaries between them, and which requirement each boundary serves.). macOS has no SOCK_SEQPACKET for Unix
domain sockets, so it uses SOCK_DGRAM.
The design needs messages in the order they were sent, and a message
that doesn’t fit to fail at the sender instead of being dropped.
On macOS the ordering test of 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… (“Descriptor channels”)
shows both on a channel of fdchan.Socketpair(4096), and how many
records it queues before a send fails with ENOBUFS. A Linux or
Windows host gets the same test when its channels are built
(S12-platformsS12-platforms: PlatformsWhich host and guest operating systems Wraith Box targets, how platform-specific code is isolated, what each platform uses, and how wb works inside WSL.).
A send that fails, or that blocks for more than 100 ms, counts as
failed. A sender never retries out of order. wb-netd then refuses
the name of a report with the answer every refusal gets
(S07-04-dns-refusalsS07-04-dns-refusals: DNS RefusalsHow wb-netd refuses a name: approval events, the hold before each refusal, quiet refusals, and the answer that is the same for every reason.), or resets a stream’s TCP connection. Both are
logged with the rule handoff-send-failed.
A process that passes a socket keeps its copy until the receiver
holds it (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.). For a stream, the proof is
wb-proxyd’s first byte on it or a StreamReset for it. wb-netd keeps a
stream’s copy no longer than a deadline (SEC13-bounded-resourcesSEC13-bounded-resources: Bounded resourcesCPU, memory and disk of each VM are capped.),
then closes it without a reset, logged stream-hold-expired. Until
then it doesn’t read end of file there. So a stream that wb-proxyd
closes before a byte or a StreamReset, or drops for a full
descriptor table, gives the guest its FIN at the deadline.
The contract
Section titled “The contract”The messages of the hand-off socket, the control channels and the
accounting channels are in the package wraithbox.egress.v1
(proto/wraithbox/egress/v1): handoff.proto, control.proto and
accounting.proto. Their comments state each field’s bounds.
internal/egressmsg holds the form check of each direction, with a
fuzz target each (C02-streams-and-tlsC02-streams-and-tls: Streams, TLS, and the Inspection CAThe test cases for the streams wb-proxyd takes over: the hand-off from wb-netd, the first-byte and TLS checks, upstream addresses, the HTTP request form, and the inspection CA's leaves, rotation, and keys., “Fuzzing”). Each channel is made with
fdchan.Socketpair(4096). Each end sends the version record first
(S04-architectureS04-architecture: ArchitectureName the processes, the boundaries between them, and which requirement each boundary serves., “The version record”).
A message doesn’t name a VM when the socket it came on already does.
A hand-off message names none: its VM is the socket’s. A wb-netd’s
control and accounting channels are its VM’s. Only the messages
between wb-hostd and wb-proxyd, which serves every VM, name one.
Message form
Section titled “Message form”A message from wb-netd (NetdMessage) is one of three kinds:
| Kind | Descriptors |
|---|---|
| name report | none |
| release | none |
| stream | exactly one |
A hand-off message is at most 4 KiB, fixed in code. wb-proxyd reads
with a buffer one byte larger, and a control buffer with room for at
least two descriptors. On macOS the control buffer holds every
descriptor that one message can hold, with room to spare. Its kernel
puts each descriptor of a message in the receiving process even when
the control buffer cuts it off, and one cut off can’t be closed. wb-proxyd
refuses a message that:
- is over the size;
- is truncated (
MSG_TRUNC); - has control data that was cut (
MSG_CTRUNC); - holds another number of descriptors than its kind allows;
- arrived while the descriptor table was full, so the kernel discarded its descriptors.
Control data that was cut also ends the channel, because the process may then hold descriptors that it can’t name.
It closes every descriptor that arrived with a refused message.
Then egressmsg.DecodeNetdMessage refuses a message that doesn’t
decode, holds a field number its message doesn’t have at any depth,
doesn’t set a kind, or breaks a field’s bound. A record with two kinds
on the wire decodes as the last one, as protobuf decodes a oneof, and
that kind’s checks apply. A name must already be in canonical form
(S07-06-name-mappingS07-06-name-mapping: Name MappingHow wb-proxyd keeps its own checked copy of the names wb-netd gave synthetic addresses: name reports, releases, replay, lifetime, the second count of the wildcard budget, and the copy wb-hostd keeps., “Name reports”), and every address inside the
synthetic range of S07-03-dnsS07-03-dns: DNSHow wb-netd answers the guest's DNS queries: synthetic addresses, the name form, negative answers and the wildcard budget.. A refused message is malformed
(S07-06-name-mappingS07-06-name-mapping: Name MappingHow wb-proxyd keeps its own checked copy of the names wb-netd gave synthetic addresses: name reports, releases, replay, lifetime, the second count of the wildcard budget, and the copy wb-hostd keeps., “Malformed reports”).
The names and labels in hand-off messages are guest strings. A log record holds them escaped and cut (S07-02-packet-pathS07-02-packet-path: Packet PathHow wb-netd carries the guest's frames: the packet transport, the stack, the link filter, its caps, addressing, the MTU and connections., “Events and rate limits”).
wb-proxyd decides the messages of one hand-off socket in the order
it read them. So a stream behind a report that waits on wb-hostd
(the wildcard budget, S07-06-name-mappingS07-06-name-mapping: Name MappingHow wb-proxyd keeps its own checked copy of the names wb-netd gave synthetic addresses: name reports, releases, replay, lifetime, the second count of the wildcard budget, and the copy wb-hostd keeps.) waits too. It isn’t refused
for a missing entry.
Resets
Section titled “Resets”wb-proxyd sends one message kind on the hand-off socket,
StreamReset, with the stream id that wb-netd gave the stream. It
sends it when it resets a stream it accepted, before it closes the
stream’s socket. So wb-netd knows to reset the guest’s TCP
connection, not to close it, when it reads end of file on that socket.
wb-netd reads what waits on the hand-off socket and sends every byte
it already queued for the guest. Then it answers end of file with a
FIN, or with a reset when a StreamReset for the stream came first
(stream-reset). Another read error gets a reset, logged
proxyd-stream-read-failed when no StreamReset came. A guest that
takes no bytes within a deadline (guest-write-timeout,
SEC13-bounded-resourcesSEC13-bounded-resources: Bounded resourcesCPU, memory and disk of each VM are capped.) or fails a write (guest-write-failed) is
reset. Only the path that resets a stream first logs it.
When a StreamReset send fails (ENOBUFS or its deadline), wb-proxyd
closes the VM’s hand-off socket before the stream’s socket, and
wb-netd resets every stream of a VM whose hand-off socket closed
(stream-reset-unsent, S07-20-handoff-socketsS07-20-handoff-sockets: Hand-off SocketsThe life of one VM's hand-off socket: its identity, the acknowledgments of both ends, how it closes, who a failure is charged to, what each restart does to it, and which of its decisions reach the audit log., “Closing a socket”).
One hand-off socket per VM
Section titled “One hand-off socket per VM”wb-hostd creates a hand-off socketpair for a 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, and the other to the VM’s
wb-netd once wb-proxyd acknowledges its end. It starts or resumes
the VM only once wb-netd acknowledges its end too.
S07-20-handoff-socketsS07-20-handoff-sockets: Hand-off SocketsThe life of one VM's hand-off socket: its identity, the acknowledgments of both ends, how it closes, who a failure is charged to, what each restart does to it, and which of its decisions reach the audit log. has the socket’s states, its number, how it
closes, and what each restart does to it.
The build VM gets no hand-off socket, and its wb-netd hands no
stream (S07-18-build-networkS07-18-build-network: Build NetworkThe build VM's network: what its wb-netd serves, the one connection to the guest's port 22, and its refusal for session VMs.).
From wb-hostd
Section titled “From wb-hostd”wb-hostd passes these ends over a socketpair it gives wb-proxyd at
spawn, its control channel (S04-architectureS04-architecture: ArchitectureName the processes, the boundaries between them, and which requirement each boundary serves., “Control channels”).
wb-netd never holds an end of it.
Each message (HandoffSocket) holds exactly one descriptor, and the
fields VM, generation and socket number. wb-hostd keeps each VM’s
generation in state.db, and only increases it. S07-06-name-mappingS07-06-name-mapping: Name MappingHow wb-proxyd keeps its own checked copy of the names wb-netd gave synthetic addresses: name reports, releases, replay, lifetime, the second count of the wildcard budget, and the copy wb-hostd keeps.
(“Lifetime”) has the fresh field.
wb-proxyd refuses the message when:
- the message form is wrong;
- the descriptor isn’t a Unix domain socket of the platform’s hand-off type;
- the VM isn’t a VM slot of S06-04-vms-and-placementS06-04-vms-and-placement: VMs and PlacementThe work VM and the isolated VM, Xcode in them, and which VM a project's sessions run in., or the
recipeVM of a named image’s build. The build VM of an image build has no hand-off socket (S07-18-build-networkS07-18-build-network: Build NetworkThe build VM's network: what its wb-netd serves, the one connection to the guest's port 22, and its refusal for session VMs.); - the generation is older than the one it holds for the VM;
- the socket number isn’t above the one it holds for the VM.
It then closes the message’s descriptors and the control channel, as
control.proto says. It logs the refusal with the rule
handoff-socket-refused and the check that failed. A datagram whose
descriptors the channel refuses gets control-message-refused.
The recipe VM’s wb-netd runs in session mode, never in build mode
(S07-18-build-networkS07-18-build-network: Build NetworkThe build VM's network: what its wb-netd serves, the one connection to the guest's port 22, and its refusal for session VMs., “Build mode”). Its streams get the build policy
compiled into wb-hostd and nothing else: no user, project or
repository policy (S06-17-named-imagesS06-17-named-images: Named Images and RecipesThe build of a named image from a recipe on a clone of the sealed base., “The network of a recipe
build”).
A message it accepts replaces the VM’s old socket, and every stream
that came on it closes. With the generation it holds, that is the new
socket of a daemon restart (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”).
After a wb-proxyd restart, wb-proxyd holds no socket, and each
VM’s first message sets its generation and number.
Streams
Section titled “Streams”A stream message holds exactly one descriptor, a Unix domain
SOCK_STREAM socket. Its fields are:
- the generation;
- the synthetic address and port;
- a stream id, which a
StreamResetnames (“Resets”); - once X14-flow-attributionX14-flow-attribution: Guest flow attributionA Network Extension transparent proxy in a macOS guest (S13-guest-confinement): can it be approved during the image build without MDM? delivers labels, the guest’s label. Its field number is kept, and this build refuses a message that sets it.
A stream message of the wrong form is malformed (S07-06-name-mappingS07-06-name-mapping: Name MappingHow wb-proxyd keeps its own checked copy of the names wb-netd gave synthetic addresses: name reports, releases, replay, lifetime, the second count of the wildcard budget, and the copy wb-hostd keeps.).
wb-proxyd refuses the stream when:
- the descriptor isn’t a Unix domain
SOCK_STREAMsocket; - the generation isn’t the socket’s;
- the copy has no entry for the address;
- the VM’s current effective policy no longer allows the entry’s name, or doesn’t allow the port for it;
- a cap below is reached (
stream-cap).
It then sends a StreamReset for the stream id and closes the
descriptor. It logs the refusal with the rule stream-handoff-refused
and the check that failed. A stream dropped for a full descriptor table
(“Message form”) gets no StreamReset, as its record is never read.
wb-proxyd resolves no name upstream and fetches no binding before
these checks pass.
This section serves SEC13-bounded-resourcesSEC13-bounded-resources: Bounded resourcesCPU, memory and disk of each VM are capped.. Per hand-off socket,
wb-proxyd caps the streams open from it and the hand-off messages it
has read but not yet decided on.
Each cap is configuration with a default. Each hand-off socket has its own reader, so one VM’s flood doesn’t hold up another VM’s streams.
Only narrows
Section titled “Only narrows”A field wb-netd writes either matches what wb-proxyd already
holds, or refuses the stream. The label can only narrow
(S07-01-per-vm-policyS07-01-per-vm-policy: Enforced per VMWhy the VM is the unit the gateway enforces, how the policies of its projects combine, and what happens when they conflict or change., “Narrowing by label”).
So a compromised wb-netd can hand over a stream only under a name
and port that the VM’s effective policy allows, within the wildcard
budget. It can’t give a stream another VM’s policy, credential
bindings or CA, or the grants of a project with no session in the VM.
Some checks only wb-netd makes: the link filter, DHCP and its per-VM
caps, the query rate, and the count of refused lookups in the wildcard
budget. It alone gives the local and quiet answers and the one answer
to every refusal, and sends approval events and learn lists.
A compromised wb-netd that skips them can refuse or slow its own
VM’s traffic, or send approval events. wb-hostd applies the request
limits to every event it receives, whatever wb-netd checked. So
those events can’t raise more requests than the limits allow
(S09-10-approval-flowS09-10-approval-flow: Approval FlowHow an approval request for a name behaves, from the lookup that raises it to its answer or its expiry.).
None of them lets a stream reach a name or port the VM’s effective policy refuses.
Policy order
Section titled “Policy order”Every change to the effective policy goes to wb-proxyd first. Such
a change is a session start or end, an approval, a policy file edit,
or wb allow.
wb-hostdsends the change towb-proxyd(PolicyChange, its sequence rising per VM run, from 1 again after a freshHandoffSocket).wb-proxydapplies it and acknowledges it (PolicyAck).- Only then does
wb-hostdsend it towb-netd.
So a report for a newly allowed name isn’t refused. A narrowing
applies in wb-proxyd no later than in wb-netd. I173I173 (closed): Measure the delay of delivering an effective policy change to wb-proxyd before wb-netdFollow-up from the code review of #170 (#68). measures the
delay this adds.
The acknowledgment may not come, because wb-proxyd is waiting to be
restarted, or wb-hostd gave up on it. Then:
wb-netddoesn’t get a change that widens;- a change that narrows goes to
wb-netdanyway.
A restarted wb-proxyd starts from the current effective policy.
Decisions
Section titled “Decisions”Identity from wb-hostd, names re-checked, fields only narrow
Section titled “Identity from wb-hostd, names re-checked, fields only narrow”A stream’s VM comes from a per-VM hand-off socket that wb-hostd
creates and labels. wb-proxyd re-checks the name against its own
copy of the name mapping. What wb-netd writes with a stream can only
narrow.
- Why:
wb-netdis the process most likely to be compromised. - Rejected:
- A name written with each stream and checked only against policy. A
wb-netdbug that mixes up two allowed names would then go through. - Moving the synthetic pool into
wb-proxyd. It puts a round trip on every DNS answer, and doesn’t protect more: a compromisedwb-netdcan still ask for any name the policy allows.
- A name written with each stream and checked only against policy. A
- Decided: I68I68 (closed): wb-proxyd must bind stream identity to the connection, not to wb-netd's tagsFound in the security review of #65 (X23-sandboxed-daemons result). (B68-proxyd-stream-identityB68-proxyd-stream-identity: How does wb-proxyd know which VM and name a stream belongs to, when wb-netd may be compromised?From the per-VM socket the stream arrived on and from name reports it checked itself, so nothing wb-netd writes can widen what a stream reaches.).
Status: Draft