Skip to content

S06-12 - Guest Agent

Purpose: What wb-guestd, the service in the guest, does for the host, and how it updates itself.

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.

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 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”):

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.

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.

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.).

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:

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).

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:

  1. installs the disk’s programs;
  2. runs the installed wb-guestd in its own place.

On macOS, in this order:

  1. It waits for the disk. Disk Arbitration mounts the disk’s volume, wb-guest-tools, at /Volumes/wb-guest-tools during boot, which can be after launchd starts wb-guestd. wb-guestd looks again until the disk is there, for a bounded time.
  2. 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-guestd checks 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.
  3. 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-guestd follows no symbolic link on the disk. When it equals wb-guestd’s own version, the update ends here.
  4. 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-guestd sets both when it installs.
  5. It installs them into /Library/WraithBox/libexec, where the LaunchDaemon runs wb-guestd from. 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-guestd comes last. After a failure, the next start finds the old wb-guestd and installs again.
  6. It runs the installed wb-guestd with execve. 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). When execve fails, wb-guestd exits, 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.

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.

After an install, wb-guestd replaces itself with the installed program through execve, rather than having the service manager start it again.

wb-guestd takes the tools disk from its mount point, and trusts it only when root mounted it read-only.

Status: Draft