Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/gate-holder-death.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@schlessera/brain-ui-sdk": patch
---

A worker launch whose server dies before releasing the start gate no longer starts its payload (#3392). bubblewrap's `--block-fd` starts the sandbox on the gate's end-of-file, and a dead server's gate reads end-of-file, so the payload used to run without its state, mounts or folder qualification. The gate now runs inside the sandbox in the trusted `/bin/sh`: it needs a complete `go` line, refuses end-of-file, a partial line or any other line, and closes its descriptor before the payload starts. Payload stdin is unchanged.
6 changes: 3 additions & 3 deletions docs/decisions/container-fresh-proc.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# A fresh /proc for agent workers in a container

The agent worker launcher gives every worker its own user and PID namespaces
and a fresh procfs (`workerCommand`, `packages/ui-sdk/src/server/worker-launcher.ts:509-689`).
and a fresh procfs (`workerCommand`, `packages/ui-sdk/src/server/worker-launcher.ts:518-698`).
Under Docker's default masked `/proc` paths the kernel refuses that procfs
mount, so every turn on such a host refuses with
`bwrap: Can't mount proc on /newroot/proc: Operation not permitted` (#1622,
Expand All @@ -22,7 +22,7 @@ does not change.
/proc because the parent /proc has masked or covered paths; in Docker, run
the container with --security-opt systempaths=unconfined", followed by
bubblewrap's own line. Every other launch failure keeps the generic list
(`FRESH_PROC_REFUSED`, `packages/ui-sdk/src/server/worker-launcher.ts:1636`).
(`FRESH_PROC_REFUSED`, `packages/ui-sdk/src/server/worker-launcher.ts:1650`).
The match is on bubblewrap's literal message, not a guess from the
environment: a host that refuses for another reason is not told to change
Docker options.
Expand Down Expand Up @@ -145,7 +145,7 @@ most host-global `/proc` files check only the file mode, not a capability.
**Ruling on #1629 (2026-10-11): a root-run server's worker gets its whole
`/proc` read-only. Root servers still qualify.** `workerCommand` adds
`--remount-ro /proc` after `--proc /proc` when the server's uid or effective
uid is 0 (`rootProcCover`, `packages/ui-sdk/src/server/worker-launcher.ts:334-336`).
uid is 0 (`rootProcCover`, `packages/ui-sdk/src/server/worker-launcher.ts:343-345`).
Measured as container root with `systempaths=unconfined` and the seccomp
profile above, with bubblewrap `0.9.0` from the image and the host's `0.12.0`,
each in the ordinary and the restricted mode:
Expand Down
24 changes: 12 additions & 12 deletions docs/decisions/governed-writable-workers.md
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ lists the keys each first-party command reads. The launcher withholds every
provider credential in the subprocess descriptor from a governed or strict
turn worker, except what its backend declares for its own inference and what
an operator admits (`const turnProfile`,
`packages/ui-sdk/src/server/worker-launcher.ts:680-690`). A relayed turn keeps no
`packages/ui-sdk/src/server/worker-launcher.ts:689-699`). A relayed turn keeps no
key. An unrelayed one keeps only its runtime's own inference credential, under
the 2026-10-22 ruling that hosts without managed native storage keep today's
inference credentials; pi resolves its own credentials there, so it keeps all
Expand Down Expand Up @@ -1029,7 +1029,7 @@ worker's command starts, the server opens that tmpfs from outside through the
sandbox's `/proc/<pid>/root`. It waits until the path is a tmpfs of exactly
the allowance on another device than the server's own `/tmp`, and keeps the
descriptor
(`function* openPrivateTurnTempSteps`, `packages/ui-sdk/src/server/worker-launcher.ts:1136-1164`).
(`function* openPrivateTurnTempSteps`, `packages/ui-sdk/src/server/worker-launcher.ts:1150-1178`).
Every later read goes through that descriptor, after the worker's namespace
is gone as well, and never enters another mount, so runtime state below
`/tmp` is never read as turn temp. The allowance (256 MiB by default,
Expand Down Expand Up @@ -2190,7 +2190,7 @@ a filesystem socket that is visible through the read-only host root: a Docker
socket, a session bus, an SSH agent or a host proxy. Each of those is an
unrecorded way out, and some reach privileged host effects. The launcher
already masks `.git` in restricted workers for exactly this reason
(`function maskedGit`, `packages/ui-sdk/src/server/worker-launcher.ts:226`).
(`function maskedGit`, `packages/ui-sdk/src/server/worker-launcher.ts:235`).

`governed` and `strict` workers therefore mask the host's runtime and state
directories with empty read-only tmpfs mounts. Maintenance workers share the
Expand All @@ -2208,7 +2208,7 @@ The worker's private `/tmp` and the other host masks remain in place.

After the masks, the launcher re-binds only the explicit runtime inputs the
worker needs, read-only, with the same resolution the restricted envelope uses
(`export function restrictedReadPaths`, `packages/ui-sdk/src/server/worker-launcher.ts:1911`):
(`export function restrictedReadPaths`, `packages/ui-sdk/src/server/worker-launcher.ts:1925`):

- the Bun executable;
- the backend entry's package, its `node_modules` chains and linked workspace
Expand All @@ -2232,7 +2232,7 @@ such ancestor with empty tmpfs instead; it never binds that ancestor's real
contents. Search permission uses the helper's primary shared group class when
that group owns the directory, including when only the other class has execute
permission. Existing canonical aliases and nested masks fold into that cover
(`maintenanceMounts`, `packages/ui-sdk/src/server/worker-launcher.ts:385-444`).
(`maintenanceMounts`, `packages/ui-sdk/src/server/worker-launcher.ts:394-453`).

This supports disjoint private-state layouts, including a nested operational
database below a server-owned `0700` parent. New ancestor coverage that would
Expand Down Expand Up @@ -2308,9 +2308,9 @@ and subdirectories through retained aliases. It does not restore unrelated
host-temp siblings or make runtime bytes writable.

The concrete checks and mounts are in (`validateRuntimePlacement`,
`packages/ui-sdk/src/server/worker-launcher.ts:609-615`),
(`temporaryRuntimeMounts`, `packages/ui-sdk/src/server/worker-launcher.ts:636-675`)
and (`runtimeMaskOverlays`, `packages/ui-sdk/src/server/worker-launcher.ts:791-809`).
`packages/ui-sdk/src/server/worker-launcher.ts:618-624`),
(`temporaryRuntimeMounts`, `packages/ui-sdk/src/server/worker-launcher.ts:645-684`)
and (`runtimeMaskOverlays`, `packages/ui-sdk/src/server/worker-launcher.ts:800-818`).
Alias restoration recognizes names already supplied by a read-only bind or an
explicit system-view symlink. It does not mount a canonical executable over an
existing alias such as `/bin/sh`; missing owned temporary paths still receive
Expand Down Expand Up @@ -3341,7 +3341,7 @@ before mounting them: both belong to the server, helper mode uses its primary
GID and exact `0750`/`0660` modes, and no-helper mode retains exact
`0700`/`0600`. Missing endpoints, aliases, non-sockets, foreign ownership and
wider modes refuse before payload execution
(`function restrictedMounts`, `packages/ui-sdk/src/server/worker-launcher.ts:250-284`).
(`function restrictedMounts`, `packages/ui-sdk/src/server/worker-launcher.ts:259-293`).
This compatibility check does not change default autonomous admission or add
another route.

Expand Down Expand Up @@ -3479,11 +3479,11 @@ An ordinary group-shared directory was accessible, but the worker's private
mount and namespace links through `/proc/<pid>` were denied. Changing the
private directory to `0770` did not remove that process-access barrier. The
current launcher opens mounts directly (`function openPrivateState`,
`packages/ui-sdk/src/server/worker-launcher.ts:1099-1127`;
`packages/ui-sdk/src/server/worker-launcher.ts:1113-1141`;
`function* openPrivateTurnTempSteps`,
`packages/ui-sdk/src/server/worker-launcher.ts:1136-1162`) before population and
`packages/ui-sdk/src/server/worker-launcher.ts:1150-1176`) before population and
start-gate release (`function* launchAgentWorkerSteps`,
`packages/ui-sdk/src/server/worker-launcher.ts:1325-1413`). Group modes and a uid
`packages/ui-sdk/src/server/worker-launcher.ts:1339-1427`). Group modes and a uid
switch alone therefore cannot implement the selected cross-uid lifecycle.

**Authority and launch.** One trusted external supervisor owns each worker
Expand Down
4 changes: 2 additions & 2 deletions docs/decisions/managed-worktrees.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Each governed or strict turn whose journal is recording opens one worker
generation, and every worker of that turn binds the generation's root-owned
directory whole at `/tmp/brain-worktrees`, inside the turn's own `/tmp`
(`// Managed worktrees (#2430): a journaled turn's workers bind one empty generation.`, `packages/ui-server/src/ws/run-session.ts:571-583`;
`// Managed worktrees (#2430): the whole access generation`, `packages/ui-sdk/src/server/worker-launcher.ts:852-863`).
`// Managed worktrees (#2430): the whole access generation`, `packages/ui-sdk/src/server/worker-launcher.ts:861-872`).
A granted worktree appears there as `/tmp/brain-worktrees/<worktree id>/` on the
worker's next path lookup; the worker cannot create entries beside it, and a
restricted worker never binds a generation. Nothing returns a host path to the
Expand Down Expand Up @@ -212,7 +212,7 @@ nothing.
**Shared source is read through views.** Each such turn's workers bind the
public view and their principal's private view whole and read-only at
`/tmp/brain-reference/public` and `/tmp/brain-reference/private`
(`// Shared reference source (#2432)`, `packages/ui-sdk/src/server/worker-launcher.ts:864-875`).
(`// Shared reference source (#2432)`, `packages/ui-sdk/src/server/worker-launcher.ts:873-884`).
A view the domain has not published to yet is opened empty first, so the turn
sees its own first acquisition
(`async bindable(principalId: string`, `packages/ui-server/src/brain/reference-source.ts:741-753`).
Expand Down
81 changes: 78 additions & 3 deletions docs/decisions/policy-write-boundary.md
Original file line number Diff line number Diff line change
Expand Up @@ -497,13 +497,13 @@ sessions.
before the runtime starts and read it after the worker is gone, while the
tmpfs is reachable only inside the worker. The launcher does both from the
host (`function* launchAgentWorkerSteps`,
`packages/ui-sdk/src/server/worker-launcher.ts:1325-1581`):
`packages/ui-sdk/src/server/worker-launcher.ts:1339-1595`):

1. It finds the sandbox's first process from the kernel's record of
bubblewrap's only child, then opens the tmpfs root through
`/proc/<pid>/root` and keeps that directory descriptor
(`function openPrivateState`,
`packages/ui-sdk/src/server/worker-launcher.ts:1099-1123`). bubblewrap's
`packages/ui-sdk/src/server/worker-launcher.ts:1113-1137`). bubblewrap's
process exists before its mounts do, so the open is accepted only once the
path is its own mount (a device other than its parent's), a tmpfs of
exactly the allowance, and empty; until then it would name the host's
Expand All @@ -513,7 +513,7 @@ host (`function* launchAgentWorkerSteps`,
code: the absolute `/bin/sh` reads one line from the worker's stdin with
its `read` builtin, which only the server writes, and then becomes the
command; closed stdin or any other line ends the worker unstarted
(`const STATE_GATE`, `packages/ui-sdk/src/server/worker-launcher.ts:56`).
(`const STATE_GATE`, `packages/ui-sdk/src/server/worker-launcher.ts:54`).
The gate runs no `PATH` lookup, so nothing in the worker's environment or
the brain can stand in for it, and the shell reads a pipe a byte at a time,
so the runtime's own input is untouched. The server copies the snapshot in
Expand Down Expand Up @@ -677,3 +677,78 @@ exported; a server killed mid-turn leaves no worker behind; and the actual
Claude runtime's full-state turn ends as `worker_state_exceeded` with its
unsaved answer absent from history. A root-run server's copy-in, limit and
copy-out were also measured in that container.

### The start gate fails closed on its holder's death — 2026-11-03

Ruling on [#3392](https://github.com/schlessera/brain-kit/issues/3392). It
amends the runtime-state mechanism above (step 4 and the
`--block-fd`/`--info-fd` alternative) and the live launch that moved to
bubblewrap's `--block-fd` with the separate-identity work
([#2069](https://github.com/schlessera/brain-kit/pull/2069)).

**Decision.** A status-gated live launch no longer uses bubblewrap's
`--block-fd`. The fixed host launch shell still opens the server-held gate FIFO
as fd 4 after the bootstrap's descriptor filter. bubblewrap passes it into the
sandbox, where the trusted absolute `/bin/sh` reads one line from it, requires
exactly `go`, closes fd 4 and only then execs the payload. EOF, a partial line
or any other line ends the sandbox with exit 125. `release()` writes `go\n`
(`const START_GATE`, `packages/ui-sdk/src/server/worker-launcher.ts:65`).

**Why.** bubblewrap proceeds past `--block-fd` on EOF: its sandbox child reads
one byte and ignores the result. That child arms `PR_SET_PDEATHSIG` only after
the read, in its init process and again before exec (source review of
[bubblewrap 0.11.1](https://github.com/containers/bubblewrap/releases/tag/v0.11.1)),
so `--die-with-parent` does not cover the gate window. In the spike's probe
(a rootless container with no network, Bun 1.4.4, bubblewrap 0.11.1), when
the server died between spawn and release, the kernel closed the gate writer and the
unqualified payload ran 25/25 (24/25 with `--die-with-parent`). With the
in-sandbox gate it ran 0/25 in each server-death case: EOF, a lone `g`, and
`go` with no newline.

Two guards now hold the window. With no `--block-fd` read in the way, the
sandbox's PID 1 arms its parent-death signal before it forks the gate shell, so
a dead server's `--die-with-parent` chain usually ends the namespace first.
Where the gate does read end-of-file (its writer closed while bubblewrap
lives), the gate's own refusal keeps the payload out. #3387's ordering, which
kills a refused launch's sandbox before closing its gate, stays as a further
guard.

**What changes in the descriptor rule.** The gate descriptor now exists inside
the sandbox, but only in the trusted shell, before any payload process exists.
It is closed before `exec`: the payload's descriptor table has no fd 4. Payload
stdin is still the protocol pipe, byte for byte. The bootstrap still refuses
every unexpected inherited descriptor. The status child PID is bubblewrap's
PID-1 reaper, as it already was after release.

Step 4 above now reads: if the server dies before release, the gate shell
reads EOF or the namespace is killed with bubblewrap, and the payload never
starts. After release, `--die-with-parent` ends the worker.

**Alternatives rejected.**

- *A trusted keeper holding the gate writer:* its own death reopens the hole
(25/25 when killed with the server). It also needs a PID hand-off, and
becomes a broker to close the hand-off gap.
- *`--sync-fd`:* signals sandbox exit outwards. It is not a gate.
- *`--userns-block-fd`:* gates before the mounts exist, needs the server to
write the uid/gid maps, and fails closed only by accident or by hanging.
- *A heartbeat gate:* needs an in-sandbox reader anyway and adds timers.
- *A pre-frozen cgroup:* optional per host, with an unfrozen window from spawn
to freeze.

**Cost.** One extra shell exec: about 0.5 ms from release to payload start
(p50 1.89 ms against 1.41 ms).

**Proof.** `packages/ui-sdk/tests/gate-holder-crash.test.ts` runs the real
governed launch in a separate server process, SIGKILLs it during setup and
after setup before release (synchronous and asynchronous launches, and after a
partial or wrong gate line), waits until every process of the launch is dead
and requires no payload marker. Further cases close the gate's writer while the
server lives, so only the gate's refusal of EOF and of a partial line stands
between it and the payload. A released control starts the payload exactly once,
with descriptors 0-2 only and stdin byte-identical.

**Not covered.** The separate-UID identity helper gates with its own
`--block-fd` pipe. Whether its death fails closed is tracked in
[#3402](https://github.com/schlessera/brain-kit/issues/3402). Post-release
server death is unchanged.
6 changes: 3 additions & 3 deletions docs/decisions/worker-writable-areas.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ on the transcript persist path and is not part of this record.

**One resolution, two consumers.** `prepareWorkerSandbox` resolves the
areas and per-turn XDG layout once per worker
(`export function prepareWorkerSandbox`, `packages/ui-sdk/src/server/worker-launcher.ts:901-942`).
(`export function prepareWorkerSandbox`, `packages/ui-sdk/src/server/worker-launcher.ts:910-951`).
The backend passes the result both to the launcher, which turns it into
bubblewrap arguments, and to `workerSandboxNote`, which renders the agent's
note (`export function workerSandboxNote`, `packages/ui-sdk/src/server/worker-sandbox.ts:592-657`).
The launcher re-checks the resolved layout against the actual launch: the
same brain and envelope, each writable area still a real directory at its
own path in the brain. The backend selects runtime HOME separately.
A mismatch refuses the launch instead of binding something the note does not
describe (`function sandboxMounts`, `packages/ui-sdk/src/server/worker-launcher.ts:823-853`).
describe (`function sandboxMounts`, `packages/ui-sdk/src/server/worker-launcher.ts:832-862`).
A launch without a resolved layout keeps the 0.41 view: only the per-turn
scratch is writable.

Expand Down Expand Up @@ -153,7 +153,7 @@ through the roadblock path. This is a worker write boundary, not an immutable
view: host-side changes to that directory remain visible. A backend may select a different runtime HOME
as described below; it does not make the host home writable. `prepareWorkerSandbox` resolves this same XDG mode on every eligible
host (`export function prepareWorkerSandbox`,
`packages/ui-sdk/src/server/worker-launcher.ts:901-942`). Host-backed overlay
`packages/ui-sdk/src/server/worker-launcher.ts:910-951`). Host-backed overlay
uppers cannot live on the worker's private tmpfs; keeping them would leave
an unbounded write area. Tools reading XDG config see the per-turn directory,
while direct reads of real HOME retain the read-only host view.
Expand Down
Loading
Loading