S06-12 - Guest Agent
Purpose: What wb-guestd, the service in the guest, does for the
host, and how it updates itself.
Design
Section titled “Design”wb-guestd
Section titled “wb-guestd”wb-guestd is a Go service that runs with full privileges in the
guest. On macOS it is a root LaunchDaemon. 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. covers other
guests. It serves gRPC over the host-guest socket, to wb-hostd only.
It has one code base for every guest OS, with OS-specific parts behind interfaces, as on the host.
The port
Section titled “The port”On guests that use vsock (macOS and Linux), wb-guestd listens on a
port below 1024.
- On macOS a process needs effective user ID 0 to bind one.
- On Linux it needs
CAP_NET_BIND_SERVICE.
So a project user can’t take the port while the service manager
restarts wb-guestd (S04-architectureS04-architecture: ArchitectureName the processes, the boundaries between them, and which requirement each boundary serves., 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?).
The port is 1023, the highest one only root can bind. The Go code of
guest and host takes it from one constant, Port in
internal/platform/guestsock. How the
listener keeps serving through aborted accepts, connect floods, and
restart races is in S04-02-vm-providerS04-02-vm-provider: VM Provider and StreamsHow 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., “Guest ports below 1024”.
The hello
Section titled “The hello”The first call on every connection the host opens is
GuestService.Hello. wb-guestd answers with its build version and
the .proto packages it serves. wb-hostd checks the answer before
anything else uses the connection (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., “With
wb-guestd”):
- It sets a receive cap of 4 KiB as a call option on
Hello, so gRPC refuses a larger answer before it decodes it, and it waits at most 5 s. Later calls on the connection take the 64 KiB cap of every guest connection (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., “Host work the guest can cause”). - A timeout is refused with
guest-hello-timeout. A failed call, an answer over the cap, or an error status is refused withguest-hello-invalid. - The version and the package list get the checks of 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., each with its rule.
A refusal closes the connection and is logged with its rule, and guest
text in the record is escaped and cut. wb-guestd takes at most
64 KiB per message, and a session start of up to 1 MiB + 64 KiB.
wb-guestd logs one JSON object per line on standard error, as
wb-vmd does. On macOS its LaunchDaemon sends that to a file in the
guest.
On a Windows guest or a Windows host, the spikes of
S12-01-platform-interfacesS12-01-platform-interfaces: Platform InterfacesHow platform-specific code is isolated behind interfaces, and what each host OS uses for each. (“Host-guest socket: keeping wb-guestd’s
endpoint”) decide how a project user is kept from posing as
wb-guestd.
What it does
Section titled “What it does”- It creates and locks project users (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., “Locked users”). It removes a project user’s home, and keeps its record, locked and disabled (S06-08-projects-and-sessionsS06-08-projects-and-sessions: Projects and Sessions Inside a VMHow projects map onto guest users and sessions onto worktrees, and how Claude Code runs in the guest., “Removing a home”).
- It kills a project user’s processes, and keeps the result as a cleanup record (S06-10-leftover-processesS06-10-leftover-processes: Leftover ProcessesThe results of the kill of a project user, how wb-hostd accepts them, and what it does when processes are left.). It stops every project user’s processes when the link drops and when it starts (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”).
- When it starts, it checks that cron and
atare closed to project users and that no project user has a crontab oratjob, and doesn’t serve when either check fails. The kill loop checks the user’s crontab andatjobs in each round (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., “Cron and at”). - It runs commands as a project user, with:
- a PTY or pipes;
- a sanitized environment: an explicit allowlist, with placeholders for credentials;
- per-session resource limits;
- the session’s Seatbelt profile (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.).
- It installs the VM’s CA certificates (S09-08-guest-trustS09-08-guest-trust: Guest TrustHow wb-guestd makes the guest's clients trust the VM's CAs.). It writes the trust files and sets the toolchain trust variables. On Linux and Windows guests it also adds them to the system trust store. A macOS guest refuses that without a user.
- It sets the guest’s clock from the host’s time, with
GuestService.SetTime(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., “Time and sleep”). - It acts as the guest end of the git transport (S08-workspace-and-gitS08-workspace-and-git: Workspace and Git Round TripHow code gets into the guest and how the agent's work gets back out, without sharing the host filesystem.).
- It relays the flow labels of the guest Network Extension, once spike 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? allows it (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.). The host uses them only to narrow rules.
Session helper records
Section titled “Session helper records”A session’s helper (S06-09-session-lifecycleS06-09-session-lifecycle: Session LifecycleA session's states from wb claude to its summary, how it starts and ends, its deadlines, and when its VM may be saved., “Session helper”) and
wb-guestd talk over two pipes. Each message is a record of 8 bytes:
a kind byte, three zero bytes, and a big-endian 32-bit value.
The helper writes reports on its descriptor 3:
| Report | Value |
|---|---|
| started | the program’s process ID, 2 to 2147483647 |
| stopped | 0 |
| continued | 0 |
| exited | the exit code, 0 to 255 |
| signaled | the signal number that ended it, 1 to 64 |
Started comes first and once. Stopped and continued alternate, starting with stopped, and at most 1024 stopped reports come: each one is a frame to the host, and the program can stop and continue itself in a loop. A wait status holds one change, and two changes between waits read as the second. The helper adds the continued report it missed between two stops, and drops a continued report whose stop it missed.
Exited or signaled comes last, and the pipe then closes. The helper runs as the project user, so wb-guestd
reads the reports as guest input (SEC11-root-gains-nothingSEC11-root-gains-nothing: Guest root is not a privilegeFull control of the guest grants no control over enforcement, secrets, or policy.). A record
out of form, out of bounds, out of order, or after the last one ends
the session with the rule session-helper-report. A helper that ends
without the last report ends it with session-helper-gone. Both then
hang up and kill the session (S06-09-session-lifecycleS06-09-session-lifecycle: Session LifecycleA session's states from wb claude to its summary, how it starts and ends, its deadlines, and when its VM may be saved., “Closing the
terminal ends the session”).
The helper reads commands on its descriptor 4, and applies each to the program’s process group only:
| Command | Signals |
|---|---|
| hangup | SIGHUP, then SIGCONT |
| stop | SIGSTOP |
| resume | SIGCONT |
| signal | the value: SIGINT, SIGQUIT, SIGTERM, SIGTSTP or SIGCONT |
A command out of form, or the end of the commands, counts as a hangup. After a hangup, the helper reads no more commands.
Updates of wb-guestd
Section titled “Updates of wb-guestd”wb-guestd updates itself from a read-only disk image that wb-hostd
attaches, never from the network. The host treats every response from
wb-guestd as untrusted input (SEC11-root-gains-nothingSEC11-root-gains-nothing: Guest root is not a privilegeFull control of the guest grants no control over enforcement, secrets, or policy.).
Guest tools disk
Section titled “Guest tools disk”The bundle holds a disk image with wb-guestd and the other programs
Wraith Box puts in the guest. It is built with the bundle, and stamped
with its build version (S10-tech-stackS10-tech-stack: Technology ChoicesFix languages, libraries, packaging, and repository layout, with the reason for each choice.).
wb-vmd attaches no disk file outside the VM’s bundle
(S04-02-vm-providerS04-02-vm-provider: VM Provider and StreamsHow 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 device set is fixed and checked”). So before
each cold start, wb-hostd clones the bundle’s image into the VM’s
bundle as tools.img, replacing the copy there. On macOS that is an
APFS clone.
wb-vmd may have changed anything in the VM’s bundle
(S04-04-vm-processesS04-04-vm-processes: Per-VM ProcessesOne wb-vmd for each VM, the wb-vmd-layer entry of the Claude Code layer build, the wb-vmd-recipe entry of a named image's recipe, and wb-diskfmt, which formats a VM's new data disk., “Reaching an instance”). So wb-hostd:
- clones to a temporary name in the bundle, created exclusively, and
renames it over
tools.img; - follows no symbolic link at either name. A link at
tools.imgis replaced, and its target is left alone; - never opens or reads the old copy (SEC15-no-guest-disk-mountSEC15-no-guest-disk-mount: The host never mounts a guest-written diskA disk image is guest-written once a guest has run with it attached, and every copy or clone of a guest-written image is guest-written too.);
- deletes a temporary file an earlier crash left, without following it.
A failure refuses the start with tools-disk-clone-failed, and
wb-hostd deletes the temporary file.
wb-vmd can write the VM’s bundle. A change it made to the copy is
gone at the next cold start. wb-vmd attaches the copy read-only.
A restore keeps the copy the state was saved with. A saved state of another build version is deleted (“The host checks, not the guest” below).
Update at start
Section titled “Update at start”When it starts, wb-guestd compares its own build version with the
one recorded on the disk. When they differ, before it listens for the
host, it:
- installs the disk’s programs;
- runs the installed
wb-guestdin its own place.
On macOS, in this order:
- It waits for the disk. Disk Arbitration mounts the disk’s
volume,
wb-guest-tools, at/Volumes/wb-guest-toolsduring boot, which can be after launchd startswb-guestd.wb-guestdlooks again until the disk is there, for a bounded time. - It checks the mount. The volume there has to be APFS, read-only,
and mounted by root. Disk Arbitration mounts a disk attached at boot
as root. A disk image that a project user attaches is mounted as that
user, so it is refused. While the tools disk holds the path, such an
image gets another one.
wb-guestdchecks the mount of the directory it opened, and reads the disk only through that directory. So a volume mounted at the path after the check isn’t what it installs. - It reads
VERSION. It has to be a regular file that holds a build version in the form of 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 guest’s version is a label”, and nothing else.wb-guestdfollows no symbolic link on the disk. When it equalswb-guestd’s own version, the update ends here. - It checks every program before it copies any. It reads only the
programs that the bundle’s layout table puts on the disk
(S17-01-bundleS17-01-bundle: The BundleWhat a Wraith Box bundle holds, and how mise run build builds and signs it., “The entitlement check”), and no other file. Each
has to be a regular file under a size cap.
Their owners and modes on the disk don’t count. The host’s user
built the disk, and
wb-guestdsets both when it installs. - It installs them into
/Library/WraithBox/libexec, where the LaunchDaemon runswb-guestdfrom. It makes the directory when it is missing. The directory and each parent of it have to be directories that root owns and that neither their group nor others can write. A symbolic link there is refused. Each program is copied to a new file created exclusively in the directory, made mode 0755 and synced, and renamed over the old one, so a link at either name is replaced and not followed.wb-guestdcomes last. After a failure, the next start finds the oldwb-guestdand installs again. - It runs the installed
wb-guestdwithexecve. The process ID stays, so launchd doesn’t see an exit, and its throttle doesn’t hold the start back (“Exec after the install” below). Whenexecvefails,wb-guestdexits, and launchd starts the installed one.
The new wb-guestd compares again. When the disk’s version still isn’t
its own, while the installed wb-guestd already holds the disk’s
bytes, the disk’s VERSION doesn’t match its program. wb-guestd
doesn’t install again then, so it can’t loop.
wb-guestd installs every program on the disk but wb-guestroot.
wb-guestroot (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.) runs the same steps at its own
start for itself only, and in step 6 runs the installed
wb-guestroot. So each program has one installer.
The two run at one boot without a lock: launchd runs both from the install directory, so it exists, and each renames only its own files (X31-admin-channelX31-admin-channel: A root shell from a second guest daemonCan wb-guestroot, a root LaunchDaemon apart from wb-guestd on a vsock port of its own below 1024, give the host a root shell on a PTY while wb-guestd is dead, stopped or restarting?). When both make a missing directory at once, the one that loses the race checks it as an old one, so neither fails.
Each refusal is logged with its rule, and wb-guestd then serves as it
is. The values are in code: the paths and the checks of the mount and
the directory in internal/platform/guesttools, and the wait, the size
cap and the rest in internal/guestupdate.
The host checks, not the guest
Section titled “The host checks, not the guest”wb-hostd accepts only a wb-guestd of its own build version. It
refuses any other before any session request
(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., “Version skew”). A guest that skips the
update doesn’t get a session.
A saved state written under another build version is deleted, and the
VM cold boots, as after a configuration change
(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., “Warm start”). The restored guest
would run the old wb-guestd.
Decisions
Section titled “Decisions”Exec after the install
Section titled “Exec after the install”After an install, wb-guestd replaces itself with the installed
program through execve, rather than having the service manager start
it again.
- Why: launchd starts a
KeepAlivejob at most once every 10 s. The update runs during boot, right after launchd startedwb-guestd, so an exit would wait out most of those 10 s before the host gets its hello. Cold boot towb-guestdis above the NFR01-startupNFR01-startup: StartupGoals: a warm VM reaches the Claude prompt in about 4 s and a suspended VM in about 7 s, and no start takes more than 10 s (p95), excluding first-time project setup and the first boot after a host restart. ceiling already (I86I86 (open): Cold boot to wb-guestd takes about 11 s, above the NFR01-startup ceilingFound by X18-vsock-handoff (#29).). Afterexecvethe process keeps its ID, and its memory and threads are the new program’s. - Rejected: an exit and the
KeepAliverestart, for the throttle.launchctl kickstart -kon its own job, which puts the job’s label inwb-guestd, and kills the process that asked. A shorterThrottleIntervalin the LaunchDaemon, which also shortens the pause of a crash loop. - Decided: I379I379 (closed): wb-guestd: update at start from the guest tools disk, and the guest side of the clock (the rest of M2.6)The implementation plan gives package M2.6 (#163) "tools-disk update, clock", and #288 is blocked by M2.6 for both: "wb-guestd updates itself from the tools disk at start (S06-12-guest-agent, "Update at start"), and….
The mount says which disk it is
Section titled “The mount says which disk it is”wb-guestd takes the tools disk from its mount point, and trusts it
only when root mounted it read-only.
- Why: a project user can’t mount a volume as root, and the tools
disk is the only read-only disk a session’s VM gets. The Xcode disk
goes only to a build VM, which has no project users
(S06-14-xcode-imageS06-14-xcode-image: The Xcode ImageHow an image gets full Xcode and the simulator runtimes from the host, as a read-only disk the host builds, and who runs Xcode's first launch.). The update runs
before the host link, so
wb-guestdcan’t ask the host which disk it is. - Rejected: finding the disk by its volume name alone, which any
disk image can have. Mounting it again at a path of
wb-guestd’s own, as the data volume is (X08-data-diskX08-data-disk: Data disk for homesGuest user homes on a second disk, surviving a system-disk replacement.). The checks above already refuse what that would keep out, and the programs run from the install directory. - Decided: I379I379 (closed): wb-guestd: update at start from the guest tools disk, and the guest side of the clock (the rest of M2.6)The implementation plan gives package M2.6 (#163) "tools-disk update, clock", and #288 is blocked by M2.6 for both: "wb-guestd updates itself from the tools disk at start (S06-12-guest-agent, "Update at start"), and….
Status: Draft