prepare / run / cleanup — sandbox a user-owned container¶
terok-sandbox provides three top-level CLI verbs for wiring a container
you launch yourself (via podman run, Podman Desktop, Compose, k8s, …)
into the sandbox's services — egress firewall (shield), credential
broker, git gate, and vault-served SSH agent. Container lifecycle stays
with you; sandbox only manages the services and per-container ancillary
state (tokens, shield rules, the supervisor sidecar). The emitted flags
include a terok.sandbox.sidecar annotation: the OCI hook installed by
terok-sandbox setup reads it and spawns a per-container supervisor
that hosts the broker, gate, and SSH signer for the container's
lifetime.
terok-sandbox prepare <container> [flags] [--json]
terok-sandbox run <container> [flags] -- <image> [cmd...]
terok-sandbox cleanup <container>
| Verb | What it does |
|---|---|
prepare |
Mints tokens, writes the sidecar + per-container state, prints podman flags |
run |
Same composition + collision check + os.execv into podman run |
cleanup |
Revokes minted tokens, calls shield.down, removes state |
Flags (shared by prepare and run)¶
--no-shield disable the egress firewall default on
--no-gate disable the git gate default on
--no-broker disable the vault token broker default on
--scope SCOPE credential scope; enables SSH wiring no default
--profiles A,B shield profiles (comma-separated) none
--json (prepare only) emit JSON array off
--scope is the toggle for the SSH agent: passing it enables the
vault SSH signer, omitting it disables it (the flag inherently needs a
value). The gate and broker also require --scope because their
services are scope-bound (the gate serves <scope>.git; the broker
serves the scope's credentials); when --scope is omitted they are
skipped with a note on stderr.
Host names in /etc/hosts¶
Podman builds a container's /etc/hosts from a base file, and its default
base is the host's /etc/hosts. That copies private host names into the
container. Sandbox sets --hosts-file none, so the container starts from an
empty base. Podman still adds localhost, the container name, and each
--add-host entry.
Set run.hosts_file in config.yml to change the base. The value goes to
podman unchanged: none, image, or an absolute host path. Use /etc/hosts
to copy the host's names into the container again. Embedded callers can set
SandboxConfig(hosts_file=...).
Podman older than 5.4 has no --hosts-file flag. On those releases sandbox
omits the flag, prints a notice, and the container copies the host's file.
Seccomp grants for ThreadSanitizer and perf¶
Set run.aslr_control: true in the host's config.yml to allow processes
to disable their ASLR for sanitizer/debugging runs. The default is false.
Embedded callers can set SandboxConfig(aslr_control=True); orchestrators
with project settings pass the resolved project override in that config.
Both prepare/run and Sandbox.run/Sandbox.create honor the setting.
For perf profiling, embedded callers request RunSpec(caps=("perfmon",));
terok forwards its run.perf: true setting through that capability channel.
Sandbox.run and Sandbox.create grant both the capability and the syscall.
The capability alone is insufficient: host seccomp profiles may still deny
perf_event_open unless CAP_SYS_ADMIN is present. Sandbox never grants
CAP_SYS_ADMIN to solve that restriction.
Sandbox reads the profile reported by
podman info --format '{{.Host.Security.SECCOMPProfilePath}}', copies it
to the container's host-side state as seccomp.json, and applies the requested
grants together in that single profile. ASLR control adds exact
personality argument grants: 0xffffffff (query), and ADDR_NO_RANDOMIZE
combined with PER_LINUX or PER_LINUX32, optionally UNAME26.
Perf replaces only perf_event_open entries with one unconditional allow,
removing that syscall from shared rules without changing their other members.
Unrelated syscall rules, capability conditions, and architecture mappings stay intact.
With neither opt-in, Podman's policy is left untouched.
The source profile is not modified. Missing/unreadable profiles, profiles
without SCMP_ACT_ERRNO as their default, and (when ASLR control is requested)
explicit personality denials
fail launch rather than silently changing the host's policy.
This grants permission, not a blanket ASLR disable. Disabling ASLR weakens
exploit mitigation; use it for development tasks, not services. Allowing perf
does not bypass kernel permissions: rootless CAP_PERFMON is not host-level
privilege. Host perf_event_paranoid, security policy, target access, and event
availability still apply. Start with user-only, per-process software events.
A host-side filter cannot grant syscalls inside a krun microVM; neither option
supports that runtime. Profile discovery checks the effective OCI runtime,
including Podman's configured default when no explicit override is passed,
and rejects krun before writing the profile. An outer sandbox can still deny the calls.
Recreate containers to change the policy; restarting an existing container
retains its creation-time policy. Verify in a newly created container with
setarch --addr-no-randomize true, then run an LLVM TSan-instrumented binary.
For perf, start with perf stat -e task-clock:u -- true; verify the generated
OCI policy has a perf_event_open allow and no matching deny.
Container-side contract¶
Sandbox produces a short stream of podman flags and environment variables; the container side needs two things to consume them:
socatinstalled in the image — the broker, gate, and SSH-agent bridges all use socat to relay between in-container endpoints and the host-side vault/gate/signer.- The bridge script sourced from your entrypoint —
ensure-bridges.shreads the sandbox-injected env vars and launches the right bridges idempotently.
Two equally supported delivery paths for the bridge script:
Build-time (COPY into image)¶
FROM ubuntu:24.04
RUN apt-get update && apt-get install -y socat git openssh-client \
&& rm -rf /var/lib/apt/lists/*
# Vendor the two bridge scripts into your build context first — they
# ship in the wheel under resources/bridges/ (locate them via
# `pip show -f terok-sandbox`).
COPY bridges/ /usr/local/share/terok-sandbox/bridges/
# Source it from your entrypoint (or /etc/bash.bashrc for interactive shells).
RUN echo 'source /usr/local/share/terok-sandbox/bridges/ensure-bridges.sh' \
>> /etc/bash.bashrc
Runtime (bind-mounted by sandbox)¶
FROM ubuntu:24.04
RUN apt-get update && apt-get install -y socat git openssh-client \
&& rm -rf /var/lib/apt/lists/*
Add the source line to your entrypoint, e.g.:
Sandbox bind-mounts the bridge resources read-only at
/usr/local/share/terok-sandbox/bridges/ when any of --gate /
--broker / --scope is active. Without socat in the image the
container is still sandboxed (shield/userns apply) but the bridges
can't connect — clear failure mode, not a security hole.
Trust model¶
Every subsystem you opt into authenticates the container via a container-held secret validated host-side:
| Flag | Token form | Wire (inside the container) |
|---|---|---|
--broker |
phantom (credentials.db) |
HTTP Authorization: Bearer … |
--gate |
gate token (stateless — lives only in the sidecar + container env) | HTTP basic-auth username |
--scope |
phantom (provider=ssh row in same DB) |
socat → ssh-agent-bridge.sh → vault container-facing endpoint |
The host persists a mandatory BrokerContext(scope, subject, credential_set)
in the sidecar, using the same identity that issued the HTTP proxy tokens.
All processes in the container share that authority: one task, one credential
set, for both OAuth and API keys. A live token for another task or set is
rejected, even if it exists in the vault. Shared OAuth markers identify only
a provider; they require a live grant for this container's exact context and
provider, and never fall back to the default set.
Revoking the task's token rows denies new HTTP requests immediately. Active HTTP streams and WebSocket forwarding are cancelled at the next authorization poll (100 ms); requests already performed upstream cannot be undone. The SSH signer checks its token and current key assignments before every identity or signature request, including on established connections.
Upgrade: sidecars without a valid broker context cannot start credential
services. Recreate those containers through the launch path so context and
tokens are issued together; do not hand-fill a guessed default context.
write_sidecar and VaultProxy require an explicit BrokerContext. Standalone
HTTP callers must mint tokens with the sidecar's exact identity; merely enabling
--broker does not confer a provider grant.
Subject for token rows is the container name. Cleanup keys on
(scope, container); the same name is used in shield's per-container
rule namespace.
The per-scope host-local SSH agent socket (
<runtime_dir>/ssh-agent-local-<scope>.sock) is the right path for host callers (e.g. terok's owngate-sync). It is not what--scopemounts into the container — that would mix the host trust model into a container path.
Recipes¶
Full sandbox¶
terok-sandbox run mybox --scope myproj \
-- my-image:latest bash -lc 'env | grep TEROK; ssh-add -l'
# … work in container …
terok-sandbox cleanup mybox
Shield-only¶
Emit flags only (Podman Desktop / Compose users)¶
$ terok-sandbox prepare mybox --scope myproj
--annotation=… --userns=keep-id:uid=1000,gid=1000 \
-v <pkg>/resources/bridges:/usr/local/share/terok-sandbox/bridges:z,ro \
-v /run/user/1000/terok/sandbox/run/mybox:/run/terok:z,ro \
-e TEROK_VAULT_LOOPBACK_PORT=9419 \
-e TEROK_VAULT_SOCKET=/run/terok/vault/vault.sock \
-e TEROK_GATE_SOCKET=/run/terok/gate/gate-server.sock \
-e TEROK_GATE_TOKEN=terok-g-… \
-e TEROK_SSH_SIGNER_TOKEN=terok-p-… \
-e TEROK_SSH_SIGNER_SOCKET=/run/terok/signer/ssh-agent.sock \
--name mybox \
--annotation terok.sandbox.sidecar=~/.local/share/terok/sandbox/sidecar/mybox.json
(Socket mode, the default. The per-container host directory
…/run/mybox is bind-mounted read-only at /run/terok/; each supervisor
service binds its socket in a service-only child directory. The container
can connect to those sockets but cannot replace their inodes or gate runtime
files. In TCP mode the env vars carry per-container loopback ports instead.)
Splice into your own podman run:
Or use --json for tooling that wants structured output.
Collisions sandbox rejects¶
run exec's into podman after the -- separator. These flags and
volume targets belong to sandbox; using them after -- raises an
explicit error:
- Flags:
--name,--network(--net),--hooks-dir,--annotation,--cap-add,--cap-drop,--userns. - Volume targets: the bridge mount path
(
/usr/local/share/terok-sandbox/bridges/),/run/terokitself, and anything under/run/terok/.
Lifecycle split¶
| Concern | Owned by |
|---|---|
podman create / start / stop / rm |
You |
| Shield rules (per-container) | sandbox (prepare / cleanup) |
| Minted tokens | sandbox (prepare / cleanup) |
| Per-container state dir + sidecar | sandbox (prepare / cleanup) |
| Vault / gate / SSH-signer services | the per-container supervisor (spawned/reaped by the OCI hooks) |
| OCI hooks (shield + supervisor), credentials DB | sandbox (setup / uninstall) |
If your container crashes or you forget to call cleanup, the next
cleanup <container> is idempotent — running it on an unknown
container is a no-op.