Repository navigation
docs(sbx): SBX0.3 lease mode design - #698
Conversation
Opt-in lease mode for mocactl sbx sandboxes: level 1 keeps a warm guest between Execs with every process killed at the end of each Exec; level 2 keeps processes started through process_start. Idle timeout, maximum duration with a hard grace limit, measured admission with reclaim, owner-only mode switch from mocactl. Validated by E17 (moca-experiments). Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Paolo Dettori <[email protected]>
Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Paolo Dettori <[email protected]>
…ions ADR-0048 (Proposed) records the fourteen decisions taken while designing SBX0.3 lease mode, each marked as the user's or our call, and revises ADR-0035's per-Exec-only decision for SBX sandboxes. ADR-0035 gains a 2026-11-08 revision note; the spec links the ADR; index row added. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Paolo Dettori <[email protected]>
ingpaolodettori-dev
left a comment
There was a problem hiding this comment.
Docs-only design spec + ADR-0048 for SBX0.3 lease mode (opt-in warm guest; level 2 keeps only process_start processes). The design is careful and well-sourced: I checked the cited code against main 316056a (budget.go:418-425, pool.go, gates_kvm_test.go:178, agent.go:28-56, runpool.go, config.go, sandbox-store.ts:9-23, sandbox-caller.ts:256-261, build-snapshot.sh:604-610/1803, sandbox.proto:47), and every reference is accurate.
Two blocking items:
- Guest privilege model is missing. Execs run as root in the guest today. In a lease, a root Exec can move itself into
tracked/or compromise the PID-1 agent, which defeats both the level-1 kill and the level-2 strict rule. TestGateLeaseReclaim(and §13) assert a per-Execfallback that §6 and ADR-0048 decision 6 explicitly reject.
Also: the rootfs is read-only, so "system installs/~ persist" overstates the surface, and /tmp (tmpfs) is guest RAM. The unknown-epoch case lets a stale token restore level 2. Plus three small nits.
Author: pdettori (MEMBER — maintainer)
Areas reviewed: Docs (spec, ADR-0048, ADR-0035 note, ADR index), cross-checked against main code
Agent/IDE config (.claude/.vscode): none
Commits: 3 commits, all signed-off: yes
CI status: proto is red at "Build and test remote-worker". The PR changes no code, and that job was green on main at the same base 316056a, so it's likely flaky; please re-run. check and deploy-scripts-bash32 were still pending at review time. Everything else is green.
|
|
||
| The guest agent runs each Exec in its own cgroup v2 inside the guest. At the end of the Exec, whether | ||
| it exits, times out or is aborted, the agent writes `cgroup.kill` for that cgroup and waits until it is | ||
| empty before it sends `End`. `setsid`, double forks and re-parenting to init cannot escape a cgroup, |
There was a problem hiding this comment.
must-fix: The end-of-Exec kill and the level-2 "only process_start survives" rule are enforced inside the guest, but the spec never says which user an Exec runs as. Today that's root: init execs the agent as PID 1 (build-snapshot.sh:658), and runFreshChild/forkShell start bash with no credential drop (guestagent/agent.go:97, :433). Under per-Exec that doesn't matter, because the VM is destroyed. In a lease it does. Once cgroup2 is mounted, a root Exec can escape with echo $$ > /sys/fs/cgroup/<tracked>/cgroup.procs (or a sibling cgroup it creates). It can also ptrace or write /proc/1/mem of the agent, and from then on hide processes from process_list, skip cgroup.kill, or forge the stopped-process list in notices for every later Exec in the lease. That breaks acceptance §1.3 items 3 and 4, and it is exactly the "injected command leaves a process running unnoticed" case that §15 rejects.
Please state the guest privilege model. For example: Execs and tracked processes run as an unprivileged uid with no capabilities, the agent stays root, the cgroup tree is root-owned and not delegated, and kernel.yama.ptrace_scope is set or the agent is otherwise protected from the Exec uid. Say what that changes for per-Exec (workspace file ownership, tools that expect root). Then add a gate: an Exec that tries to migrate into tracked/, create its own cgroup, or signal or ptrace PID 1 fails, and its processes still die. ADR-0048 decision 1 ("setsid cannot escape it") needs the same qualifier.
There was a problem hiding this comment.
Fixed in 3e491f3. New §3.3, the guest privilege model, applies to both lease levels:
- Execs and tracked processes run as an unprivileged uid with an empty capability set (bounding set dropped,
no_new_privs). - The agent stays root as PID 1, sets itself non-dumpable, and owns a root-owned, non-delegated cgroup tree.
- I checked SBX0's guest kernel (
6.18.51+):CONFIG_SECURITY_YAMAis not set, soptrace_scopeisn't available. The uid boundary plus the empty capability set is the protection. - The build tree has setuid binaries (
su,mount,umount,newgrp,gpasswd,chage,unix_chkpwd,pam_timestamp_check).no_new_privsneutralises them, and the image work also strips the bit. - User namespaces are off (
CONFIG_USER_NS=y, souser.max_user_namespaces=0). - Workspace ownership moves to the Exec uid.
pushalready extracts with--no-same-owner, and existing workspaces get a one-timechown. - Per-
Execstays root in v1. Moving it to the same uid is open item 4, for the security review.
Also added: TestGateLeasePrivilege (cgroup migration and creation, signal/ptrace//proc/1/mem against PID 1, setuid, userns; the Exec's processes still die), acceptance item 9, and a §11 row. ADR-0048 decision 1 is qualified, and decision 15 is new. E17 measures the privilege drop's cost (moca-experiments#21, 88f0d88).
| | `TestGateLeaseTeardown` | Each end reason destroys the VM and leaks no cgroup, tap, vsock or jail on the host (the lease variant of `TestGateLeakFreeTeardown`) | | ||
| | `TestGateLeaseStaleEpoch` | An Exec with an older `mode_epoch` is refused, and a downgrade stops tracked processes | | ||
| | `TestGateLeaseNotice` | Every end reason produces exactly one notice, on the next response and in `sandbox_info` | | ||
| | `TestGateLeaseReclaim` | Under a budget squeeze the idle-longest lease goes first, busy leases are never reclaimed for memory, and a lease VM that does not fit falls back to per-`Exec` | |
There was a problem hiding this comment.
must-fix: TestGateLeaseReclaim says "a lease VM that does not fit falls back to per-Exec". §6 (lines 245-251) and ADR-0048 decision 6 both say the opposite: "There is no per-Exec fallback … the worker reclaims, then refuses". §13's worker bullet also lists "fallback to per-Exec". As written, the gate would assert the behavior the design rejects. Change it to "reclaims idle-longest leases, then refuses with memory-budget", and drop the §13 fallback item.
There was a problem hiding this comment.
Fixed in 3e491f3. TestGateLeaseReclaim now asserts "reclaims idle-longest leases, then refuses with memory-budget", and §13's worker bullet says the same. Neither document has a per-Exec fallback anywhere now.
| | --------------------------------------------------------- | --------------- | ------------------------------------------ | ------------------------------ | | ||
| | Guest across Execs | fresh each time | the same guest until the lease ends | same as level 1 | | ||
| | Page cache, CPU caches | cold | warm | warm | | ||
| | `/tmp`, files outside the workspace, system-wide installs | lost | kept until the lease ends | kept until the lease ends | |
There was a problem hiding this comment.
suggestion: On main the guest rootfs is mounted read-only (build-snapshot.sh:1530 boot args ro, :1545 is_read_only:true). Outside /workspace, only the /tmp and /var tmpfs mounts are writable. So "system-wide installs" (and ~ in the §11 row at line 392, unless HOME points into a tmpfs) don't survive a lease, because they fail in every mode. Please narrow this row and §11 to /tmp and /var. Those tmpfs files are also guest RAM: they raise memory.current and aren't freed until the lease ends. §6 and E17 should count that (a pip install --target /tmp/... or dataset cache in /tmp is the cache-heavy workload this spec targets).
There was a problem hiding this comment.
Fixed in 3e491f3. I verified ro and is_read_only:true at build-snapshot.sh:1530/:1545.
- The modes table now has separate rows for
/tmpand/var(tmpfs, counted in guest RAM) and for the read-only rootfs. - §3 explains the scope, with the
pip install --target /tmp/...case. - Acceptance item 3 and the §11 row are narrowed to
/tmpand/var. - ADR-0048 decision 1 and its consequences are updated to match.
- E17 now samples guest
Shmemper Exec besidememory.current.pycachewrites its caches to/tmpin half of its sessions, so the cost is measured (moca-experiments#21, 88f0d88).
| for its remaining 15 minutes. | ||
| - **The epoch survives a worker restart.** The worker writes it next to the workspace on the host | ||
| disk (the workspace already survives a restart) and reads it back before serving. A missing or | ||
| unreadable file fails closed: the sandbox runs `per-exec` until an Exec arrives with a higher epoch. |
There was a problem hiding this comment.
suggestion: A missing epoch file "fails closed … until an Exec arrives with a higher epoch". When the stored epoch is unknown, any epoch counts as higher, including one from a token minted before a downgrade. That token still has up to 15 minutes left, so after a restart that lost the file, the first Exec on a stale lease+processes token re-establishes level 2. That's the case §8.2 says the epoch prevents. Consider staying per-exec on an unknown epoch until a LeaseControl set_mode arrives (which mocactl already re-sends). Alternatively, have the relay check the claim against the sandbox record's current modeEpoch. Add the lost-file case to TestGateLeaseStaleEpoch.
There was a problem hiding this comment.
Fixed in 3e491f3. With an unknown epoch the sandbox stays per-exec, and no Exec epoch can restore a higher mode. Only a LeaseControl set_mode does. The control plane signs it with the record's current modeEpoch, under the sandbox token key with a distinct typ. mocactl sends it on every change and re-sends it from sbx get. The lost-file case is now part of TestGateLeaseStaleEpoch, and ADR-0048 has it as decision 16.
| | Tool | Arguments | Result | | ||
| | --------------- | ------------------------------------------------------------------------------------ | -------------------------------------------- | | ||
| | `process_start` | `command`, `cwd` (in the workspace, default `/workspace`), `name` (optional, unique) | `id`, `name`, `pid` | | ||
| | `process_list` | — | id, name, pid, state (`running | exited(code) | killed(signal)`), age, memory | |
There was a problem hiding this comment.
nit: The | characters inside the backticks in running | exited(code) | killed(signal) split this row into extra cells, so the Result column renders broken. Escape them as \|, as the §8.3 table does.
There was a problem hiding this comment.
Fixed in 3e491f3. The pipes are escaped as \|, and the row renders as one cell.
| process started any other way (`&`, `nohup`, `setsid`, a daemon that forks itself) dies at the end | ||
| of its Exec, exactly as in level 1. | ||
| 5. A lease ends on an idle timeout, at the maximum duration (when the sandbox is idle, or at the hard | ||
| grace limit), on `reset`, on a mode downgrade, on memory reclaim, on heartbeat loss, or on a worker |
There was a problem hiding this comment.
nit: Change "on heartbeat loss" to "on keepalive loss". Heartbeat {} is worker liveness (§1.2, sandbox.proto:47), and §4 and §4.1 call this keepalive_lost. This list also leaves out teardown_failed, agent_dead and sandbox_deleted from §4.1.
There was a problem hiding this comment.
Fixed in 3e491f3. Item 5 now says "keepalive loss" and lists every end reason from §4.1, including teardown_failed, agent_dead and sandbox_deleted.
| the fact. The guest init must mount `cgroup2`, which changes every host's snapshot once, per-`Exec` | ||
| included. | ||
| - **Follow-up owed:** E17 (a and b); a lease section and a level-2 section in the SBX security review; | ||
| the implementation plan; a 2026-11 revision note on ADR-0035 pointing here; fixing the snapshot |
There was a problem hiding this comment.
nit: This PR already adds the "2026-11 revision note on ADR-0035 pointing here" (0035, 2026-11-08 section), so drop it from "Follow-up owed". Separately, the spec header says SBX0.3 is "to be registered in the milestone registry", and docs/specs/README.md isn't touched here. Do that in this PR or list it as a follow-up.
There was a problem hiding this comment.
Fixed in 3e491f3. I dropped the ADR-0035 note from "Follow-up owed". SBX0.3 is now registered in docs/specs/README.md in this PR, linking the spec, ADR-0048 and E17, and the spec header says "registered".
…m gate, tmpfs scope - Spec §3.3 (new): lease Execs and tracked processes run as an unprivileged uid with no capabilities (no_new_privs, setuid bits stripped, user namespaces off); the agent stays root, non-dumpable, with an undelegated cgroup tree. The guest kernel has no Yama. TestGateLeasePrivilege, acceptance item 9, a security-table row. - TestGateLeaseReclaim and §13: reclaim, then refuse with memory-budget (no per-Exec fallback, matching §6 and ADR-0048 decision 6). - The rootfs is read-only: only /tmp and /var (tmpfs, guest RAM) persist across a lease; modes table, acceptance and §11 narrowed. - Unknown epoch never restores a higher mode; only a signed LeaseControl set_mode does. Lost-file case added to the epoch gate. - Nits: escaped pipes in the process_list row, keepalive loss and the full end-reason list in acceptance item 5. - ADR-0048: decisions 15 (privilege) and 16 (unknown epoch); decision 1 qualified; the done ADR-0035 note dropped from follow-ups. - SBX0.3 registered in docs/specs/README.md. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Paolo Dettori <[email protected]>
ingpaolodettori-dev
left a comment
There was a problem hiding this comment.
Follow-up to our 656a8fa review. The single fix commit 3e491f3 closes all 7 earlier items (2 must-fix, 2 suggestions, 3 nits). I checked each one against the head:
- Guest privilege model: the new §3.3 adds an unprivileged Exec uid with an empty capability set and
no_new_privs, keeps the agent as root PID 1 and non-dumpable, leaves the cgroup tree root-owned and undelegated, strips setuid bits, and turns off user namespaces. It also addsTestGateLeasePrivilege, acceptance item 9, a §11 row, and ADR-0048 decision 15 (decision 1 is now qualified). TestGateLeaseReclaimand §13 now say "reclaim, then refuse withmemory-budget". No per-Execfallback is left anywhere.- The read-only rootfs and the
/tmp//vartmpfs scope are reflected in the modes table, acceptance item 3, §11 and ADR-0048, and tmpfs is counted as guest RAM. - An unknown epoch never restores a higher mode. Only a signed
LeaseControl set_modedoes (ADR-0048 decision 16), and the lost-file case is now inTestGateLeaseStaleEpoch. - Nits: the pipes are escaped, the text says keepalive loss and lists every end reason, the ADR-0035 follow-up is dropped, and SBX0.3 is registered in
docs/specs/README.md.
I've left two suggestions on the new §3.3 (same-uid ptrace into tracked processes; workspace ownership after a per-Exec period). Neither blocks.
Author: pdettori (MEMBER — maintainer)
Areas reviewed: Docs (incremental 656a8fa..3e491f3: spec, ADR-0048, specs README)
Agent/IDE config (.claude/.vscode): none
Commits: 1 new commit, signed-off: yes
CI status: all green (proto now passes); dependency-review was still pending at review time
| forge the stopped-process list in later notices. That defeats acceptance items 3, 4 and 6. So, in | ||
| both lease levels: | ||
|
|
||
| - **The agent stays root (PID 1). Execs and tracked processes run as an unprivileged uid** (`sbx`, |
There was a problem hiding this comment.
suggestion: Tracked processes and later Execs run as the same uid, and with no Yama in the guest kernel, same-uid ptrace is allowed by default. A later (possibly injected) Exec can therefore ptrace/process_vm_writev/write /proc/<pid>/mem of a running process_start process and run its own code inside it. That code outlives the Exec and is not visible in process_list, which still shows the original command. This partly reopens the level-2 strict rule (§1.3 item 4, ADR-0048 decision 2) through a side door, even though the uid boundary now protects PID 1. Options: start each tracked process under its own uid (for example from a small range), deny ptrace/process_vm_writev to Execs with a seccomp filter (which also covers /proc/<pid>/mem, since that access is ptrace-mode checked), or have the agent re-apply PR_SET_DUMPABLE 0 to tracked processes (note that execve resets it, so this needs care). Please add the Exec→tracked-process case to TestGateLeasePrivilege. If you accept the risk, say so in the §11 row.
There was a problem hiding this comment.
Fixed in 3fbfb4f. I took the separate-uid option and added seccomp as defence in depth.
- Each tracked process now runs under its own uid from a reserved range. A uid is not reused while any of its processes is alive in the lease. A different uid fails the ptrace-mode check for
ptrace,process_vm_writevand/proc/<pid>/mem, and it also stops signals. - Execs also run under a seccomp filter that denies
ptraceandprocess_vm_readv/writev. One correction: seccomp can't cover/proc/<pid>/mem, because that is anopen()followed by a ptrace-mode permission check, not a ptrace syscall. So the uid split is what protects that path. The spec says so. - Not
PR_SET_DUMPABLE. As you noted,execveresets it. Only the process itself can set it, so it can't be applied from outside. - The workspace is shared through an
sbx-wsgroup, with setgid directories andumask 0002, so the agent and a dev server can still edit each other's files. TestGateLeasePrivilegenow covers the Exec → tracked-process case: ptrace,process_vm_writev,/proc/<pid>/memand signals, with the tracked process's memory and command unchanged.- One residual is now accepted in §11. A tracked process that loads code from the workspace (hot reload) can be changed by editing files. The strict rule limits which processes exist and are visible, not what code a visible process loads.
- ADR-0048 decision 15 is extended to match.
| - **Workspace ownership.** The workspace becomes owned by the Exec uid. `mocactl sbx push` already | ||
| extracts with `--no-same-owner` (`packages/mocactl/src/sbx/push.ts:502`), so pushed files take the | ||
| extracting uid. A workspace created under root gets a one-time `chown -R`, done by the agent as | ||
| root, at the first Exec of its first lease. |
There was a problem hiding this comment.
suggestion: The chown -R runs once, "at the first Exec of its first lease". Per-Exec stays root in v1 (§3.3, last bullet), so after lease → per-exec → lease, any files created or rewritten during the per-Exec period are root-owned again, and the next lease's unprivileged Exec can't modify them (a git pull, a rebuilt venv). Either re-normalise ownership at the start of every lease, for example by tracking whether a root Exec has run since the last lease, or make the per-Exec path create files as the Exec uid. Also, a chown -R on a large workspace adds to the first Exec's latency, which E17 could measure. Open item 4 removes this case if it lands, but v1 needs a rule.
There was a problem hiding this comment.
Fixed in 3fbfb4f. The rule is now per lease, not once.
- Any per-
ExecExec on the workspace sets aroot_wroteflag in the sandbox's state file, next to the epoch. Workspaces that predate lease mode start with the flag set. - At the start of every lease whose flag is set, the agent normalises ownership (Exec uid,
sbx-wsgroup, setgid directories, group write) before the first Exec, then clears the flag. A lease that follows another lease skips the walk. - I also hardened the walk. It never follows symlinks (
lchown/fchownat(AT_SYMLINK_NOFOLLOW)) and stays on the workspace mount. Otherwise a rootchown -Rcould follow a symlink planted in the workspace and change the owner of a file in/tmpor/var. - New
TestGateLeaseOwnershipcoverslease→per-exec→lease, the symlink case, and lease-after-lease skipping the walk. - ADR-0048 has it as decision 17. E17 measures the walk's time at 10k, 100k and 500k files (moca-experiments#21, 96ca4a5). Open item 4 would remove the walk.
…rship per lease - §3.3: each tracked process runs under its own uid from a reserved range, and Execs get a seccomp filter on ptrace and process_vm_*, so a later Exec cannot inject into a running tracked process (same-uid ptrace is allowed and the guest has no Yama). The workspace is shared through an sbx-ws group with umask 0002. - §3.3: ownership is normalised at the start of every lease that follows root (per-Exec) writes, tracked by a root_wrote flag; the walk never follows symlinks and stays on the workspace mount. - TestGateLeasePrivilege covers Exec -> tracked process; new TestGateLeaseOwnership; two §11 rows, one an accepted residual (a tracked process loading code from the workspace). - ADR-0048 decision 15 extended, decision 17 added. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Paolo Dettori <[email protected]>
Summary
Design spec for SBX0.3, lease mode for
mocactl sbxsandboxes. Per-Exec stays the default and keeps all its guarantees. Lease mode is opt-in per sandbox:lease(level 1): the microVM stays warm between Execs until an idle timeout (default 300 s). Every process is still killed at the end of each Exec (per-Exec cgroup kill in the guest), and each Exec gets a fresh shell. What carries over: page cache, CPU caches, files outside the workspace.lease+processes(level 2): only processes started through the newprocess_starttool survive between Execs.&,nohupandsetsidstill die.mocactl(user API token). The agent has no tool for it. The relay setsmodeandmode_epochfrom token claims. The worker refuses stale epochs.Motivated by E15 (metal: resident with a 300 s idle timeout held 128 sessions against per-Exec's 80) and E16 (low duty, in progress), and by the 2026-11-05 build-vs-adopt assessment. Validation is E17, a separate spec: rossoctl/moca-experiments#21.
ADR-0048 (
docs/adrs/0048-sbx-lease-mode.md, Proposed) records the 14 decisions taken during design. Each is marked as the user's decision or our call. It revises ADR-0035's 2026-10-08 "per-Exec only" decision for SBX sandboxes. ADR-0035 gets a 2026-11-08 revision note that points to it, and the ADR index gets a row.Guest kernel checked on the SBX0 host (2026-11-08): 6.18.51+ with cgroup v2 and
cgroup.kill. Only acgroup2mount in the guest init is needed (spec §16).Status: draft, design not approved. Next: approval, then the implementation plan.
Assisted-By: Claude Code