Skip to content

[M1.4c1] Confined container creation from validated specs - #151

Merged
samuelmbabhazi merged 3 commits into
mainfrom
feat/m1.4c-containers
Nov 5, 2026
Merged

samuelmbabhazi merged 3 commits into
mainfrom
feat/m1.4c-containers

Conversation

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator

Part of #136.

What changed

M1.4c1: the agent creates containers from a fully validated ContainerSpec.

  • Docker client: container calls (inspect, create, start, stop, remove). An HTTP 304 counts as "already in that state".
    • The create body is a typed struct with no field for privileged mode, host namespaces, devices or bind mounts, so those cannot be requested at all.
  • Spec validation (actions/container_spec.go):
    • Identity: name and network match sk-…; the image must be digest-pinned.
    • Command: an argv array of at most 64 arguments, 4 KiB each, no NUL.
    • Environment: names match [A-Za-z_][A-Za-z0-9_]{0,127}, no duplicates across env and secrets, bounded values, no NUL. secrets_as_files is refused until the tmpfs writer exists.
    • Mounts: managed volumes or bounded tmpfs (≤ 1 GiB) only, at clean absolute paths outside /, /proc, /sys, /dev, /etc, /var/run and /run, with unique targets, at most 16.
    • Ports: exposed on the Docker network only. A host port other than 0 or the container port is refused, so nothing is published on the host.
    • Resources: bounded CPU, memory and pids, with a default pids limit of 1024.
    • Other: DNS servers as canonical IPs (at most 3); capabilities from the allow-list only, no duplicates; a bounded restart policy; a clean absolute working directory; a numeric or simple-name user; a stop timeout of at most 10 min.
    • Always set: no-new-privileges, private IPC, an init process, bounded json-file logs, and the labels sk.managed, sk.name and sk.spec.
  • create_container handler.
    • The spec fingerprint hashes the spec with secret values replaced by their versions, so labels never reveal secrets.
    • A replay reuses the container with the same name and fingerprint. A different spec under the same name, or an unmanaged same-named container, is refused.
    • Compensation removes only the container this plan created.
  • Docs. SPEC 3.3 describes container confinement. The PLAN splits M1.4c into c1 (this PR), c2 (start, stop, remove) and c3 (health check, release task).

How it was tested

Everything runs locally.

  • The exact Docker body produced: network, security options, pids limit, IPC, capabilities, CPU and memory, restart policy, mount types, exposed ports, labels; secret present in the environment and absent from labels.
  • Idempotent replay (one create); a secret rotated under the same version keeps the fingerprint; a changed spec and an unmanaged container are refused.
  • 31 malicious specs, all rejected with no create call:
    • tagged image, host network, traversal name;
    • mounts: root, /etc/passwd, /proc, docker.sock, relative, traversal, duplicate, foreign volume, missing source, oversized tmpfs;
    • environment: injection, NUL, duplicate, oversized secret, secrets_as_files;
    • command: too many arguments, a NUL argument;
    • user injection, a DNS list packed in one string, unknown or unspecified capability;
    • published host port, port 0, reserved label, CPU overflow, a one-hour stop timeout, a restart storm, a relative working directory;
    • plus a missing spec.
  • The fingerprint follows versions, not values, and never mutates the spec.
  • FuzzMountTargets ran for 15 s with no finding.
  • go test -race ./agent/..., golangci-lint (0 issues) and the local gate pass.
  • Not run locally: a real Docker daemon. It runs in CI-1.

Security considerations

  • Container escape settings cannot be expressed.
  • Every field is bounded and validated before the journal and the daemon.
  • Secrets never appear in labels or update messages.

@samuelmbabhazi
samuelmbabhazi merged commit b801680 into main Nov 5, 2026
@samuelmbabhazi
samuelmbabhazi deleted the feat/m1.4c-containers branch November 5, 2026 20:00
samuelmbabhazi added a commit that referenced this pull request Nov 5, 2026
Closes #136 (with #151, #152).

## What changed

M1.4c3: health checks and release tasks. Every container action of M1.4
now has a handler.

- **Docker client:**
  - inspect now returns network addresses and exposed ports;
- `WaitContainer` uses a second HTTP client without the 30 s
response-header timeout, because the daemon answers only when the
container exits.
- **`health_check`.** The agent probes a managed container itself:
- **Target:** only its private network address, never loopback,
link-local (cloud metadata) or public addresses. The port is the
requested TCP port, or for HTTP the single exposed TCP port; it is
refused when ambiguous.
- **HTTP:** a GET that never follows redirects, reads at most 4 KiB and
treats 2xx/3xx as healthy. The path must be absolute, not network-path
(`//…`), printable and at most 2048 bytes.
- **Timing:** bounded intervals, timeouts, start period, thresholds and
deadline.
- **Result:** `HealthCheckResult{healthy, consecutive_successes}`; an
unhealthy container fails the action.
- **`run_release_task`:**
- The spec is validated like `CreateContainer`, with the restart policy
forced to `no`.
- Step 1 creates the container, reusing it by fingerprint and refusing
unmanaged ones.
- Step 2 is non-retryable: start, wait within the timeout (1 s–1 h),
then remove, so the exit code is the final outcome. On timeout the task
is killed and removed. After a crash it is compensated (removed), never
run again.
  - Result: `ReleaseTaskResult{exit_code}`.
- **Docs.** The SPEC describes both actions. In the PLAN all M1.4
handlers are done, but M1.4 stays open until the integration tests
against a real Docker daemon run (CI-1).

## How it was tested

- **Health check:**
- reaches its threshold after a failure, on the exposed port of the
private address;
  - an unhealthy container fails;
- metadata (`169.254.169.254`), loopback, public and empty addresses are
never probed;
  - ambiguous ports are refused;
- 11 malicious specs are rejected (relative path, `//evil` network path,
space, CRLF injection, oversize, port 0 or overflow, a tiny interval, a
timeout longer than the interval, a threshold storm), as is a one-hour
deadline.
- **Release task:**
- exit code 3 is reported, the task runs once with restart `no` and is
removed;
  - a hanging task is killed and removed;
  - an unmanaged container is never run;
  - a two-hour timeout is refused.
- `go test -race ./agent/...`, `golangci-lint` (0 issues) and the local
gate pass.
- **Not run locally:** a real Docker daemon. It runs in CI-1.

## Security considerations

- Probes cannot be aimed at host, metadata or external addresses.
- Migrations never run twice after a crash.

---------

Signed-off-by: Samuel Mbabhazi <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant