Sandbox resource budgets
Resource budgets are independent of shell approvals. This implements §8 of
Contained Shell, Gated Egress.
It does not enable the proposed sealed-shell permission default.
Trusted operator configuration (restart required):
sandbox:
limits:
pids: 512
memory: 4g
disk: 30g
Sizes accept positive integer bytes or binary k, m, g, t suffixes
(optionally B/iB). Limits cannot be disabled with zero or max. Discovered
project YAML cannot replace these budgets; explicit --config is trusted.
Enforcement and degradation
Every isolated/host-read-only shell launch and every light-sandbox foreground,
detached and helper launch passes admission before spawning on Linux. Office
workers use the isolated strategy's same controller. Tier-1 external agents use
the active conversation controller when supplied by their per-turn context;
standalone invocations use and close their own controller. Session strategy
rebuilds and networked views share one controller. Plain host-direct execution,
including an explicitly approved light-sandbox sandbox: false, is not covered.
On Linux with a delegated cgroup v2 subtree, each session gets a child cgroup.
The server writes and reads back pids.max, memory.max, and cpu.weight=100
(standard weight, not a CPU time cap). The trusted launch stub joins that cgroup
before exec; descendants inherit membership. The server is never moved, and
nothing outside the delegated subtree is modified.
cgroup v2 does not let a cgroup that holds processes distribute controllers to
children, so the server's own cgroup cannot host session groups by itself.
Session groups are created in:
- the server's own cgroup, if
pids,memoryandcpuare already enabled in itscgroup.subtree_control(set up by the operator); otherwise - its systemd unit's cgroup, when the unit is delegated with the server in a subgroup. The server enables the three controllers in that (empty) unit cgroup. Slices and non-delegated units are never touched.
For a systemd service (system or --user, systemd 254 or newer), add:
[Service]
Delegate=pids memory cpu
DelegateSubgroup=server
then systemctl [--user] daemon-reload and restart the service. Without this,
the fallback below applies. When workspace isolation or the light sandbox is
loaded, the server logs which backend applies once at startup:
Sandbox resource limits: cgroup v2, session groups under … or
Sandbox resource limits: prlimit fallback, … : <reason>.
Otherwise the trusted launch stub installs and reads back both soft and hard
RLIMIT_NPROC with prlimit, preserving any stricter inherited hard limit.
This is weaker than a cgroup:
- The kernel checks
RLIMIT_NPROCagainst all processes and threads of the real user: other sessions, other instances running as the same user, and the server itself. The limit is therefore set to that user's task count at spawn time pluspids, so the budget is headroom, not an absolute number. Other load running as the same user can still use up that headroom. Linux exempts root (and privileged processes), so root fallback status is not sufficient for a sealed-shell resource prerequisite. - No memory control.
RLIMIT_ASis not applied: it caps each process's virtual address space, not memory use, and breaks runtimes that reserve large address ranges (Node/V8, Chromium, Go, OpenBLAS thread pools). - No CPU weight or CPU control is available in this fallback.
There is no threaded preexec_fn and no post-exec attach race. Regular spawns
use an isolated, stdlib-only trusted helper (bundled builds dispatch the same
helper before importing the CLI). The helper starts with a minimal environment;
the payload environment travels over a private descriptor, so loader variables
cannot run payload code before limits are installed. The Landlock launcher's single-threaded
child installs limits and joins via a passed cgroup descriptor before applying
its final confinement and exec. Setup failure prevents payload execution.
On non-Linux hosts the existing sandbox still executes, but resource status stays
unverified/unenforced (no Linux limits are claimed and sealed gating must remain
disabled). Native resource budgets are not implemented by this phase.
Disk is admission, not a running quota
Before each spawn, the controller scans writable roots without following
symlinks, sums logical file/metadata sizes, and deduplicates hardlinks and
overlapping roots. Isolated sessions include upper, session home and tmp,
worktree git overlay upper, read-write repository paths, and the current
background log directories. Otherwise-ephemeral isolated home/tmp use
host-backed session directories so they remain measurable across launches.
Kernel-private overlay work directories (often mode 000) are not scanned or
chmodded; payload file data is accounted in upper. Light sandbox scans all
policy writable roots (real checkout, session temp,
cache, and any per-call background directories); shared roots therefore count
against each session admitting them.
This is not a filesystem quota: a running command, parallel commands, deleted
open files, or writes after the scan can exceed the disk budget and fill the
host disk. Sparse files count logically, but inode accounting is approximate.
The next spawn is refused at/above the cap or on a scan error; the refusal
names sandbox.limits.disk and tells the user they can free space or raise it
(for example through the Configuration Assistant) and restart. This admission
choice is expressly allowed by the design; deployments needing a hard running
quota must supply filesystem quotas or size-limited mounts independently.
Verified status and lifetime
Strategies expose resource_enforcement() -> ResourceStatus: verified,
backend, aggregate, pids_enforced, cpu_weight, disk, reason, and the
convenience enforced property. Configuring files alone is not verification:
a child must acknowledge successful setup before status becomes verified.
Failed setup/admission and closed sessions revoke the status. enforced is
false for unverified/failed setup and for root's NPROC-exempt fallback. Disk is
always labelled spawn_admission_only; consumers must not reinterpret it as a
hard quota. Permission policy must check this capability (default false when
absent), not infer resource enforcement from filesystem containment or isolation.
Eviction/deletion/shutdown closes session controllers, kills tracked live
process groups, and uses cgroup.kill for all cgroup descendants before removing
the group. A failed cgroup cleanup is surfaced, never worked around by moving
children back into an unbounded cgroup. Fallback cleanup can kill tracked process
groups, but cannot reliably collect descendants which escaped their group;
PID-namespace backends already supply the stronger descendant lifetime boundary.