Security & Design¶
Threat model¶
terok-shield provides egress network filtering for rootless Podman containers running untrusted workloads (AI coding agents).
Attacker model: arbitrary code execution inside the container, no host access. The container boundary (rootless Podman + user namespaces) is assumed intact. terok-shield adds the network layer: even with full container control, the workload cannot reach arbitrary internet hosts.
Host privilege model: no root required. The firewall operates within rootless Podman's user namespace.
Goals:
- Default-deny outbound connectivity
- Allowlist-based access to specific destinations (domains or IPs)
- Block private-range traffic (RFC 1918 + RFC 4193, plus link-local RFC 3927 / RFC 4291) unless explicitly allowlisted (prevent lateral movement)
- Audit log all firewall events
- Fail closed on any hook failure
Non-goals:
- Inbound filtering (containers are not exposed)
- DNS exfiltration through allowed resolvers
- Host kernel / container-escape attacks
- Application-layer inspection
Security boundary: nft/rules.py¶
All nftables rulesets are generated by RulesetBuilder in nft/rules.py.
Import isolation: only stdlib (ipaddress, re, textwrap) +
nft/constants.py (pure literals: table name, network defaults, private
ranges, log prefixes). Enforced by an AST import isolation test and bandit
SAST scan.
Input validation: all values are validated before string interpolation:
safe_ip()— validates and normalizes IPs/CIDRs viaipaddress.ip_address/ip_network(dual-stack); returns canonical string form to ensure reliable state-file comparisons_safe_port()— validates port range, rejects bools_safe_ident()/_safe_timeout()— validate nft set/table names and set-element timeouts_safe_seconds()— validates a per-element override timeout: whole seconds, at least 1 (nft readstimeout 0sas never), at most the longest seconds literal nft accepts_safe_window_timeout()— validates a bypass-window duration: a_safe_timeout()value with a non-zero count, for the same reason — a zero-length window would never close
The OCI hook also writes nft syntax for one thing: the expiring t10 override
elements it times at apply (override.expiry, see below). Each line is
checked as untrusted input — the address must parse, the deadline must be an
integer — and a line that fails is skipped, so a malformed entry can only take
an override away.
Self-verification: RulesetBuilder.verify_up() checks post-apply
invariants: managed table present, drop policy, both chains, reject type,
dual-stack allow and deny sets declared, all private ranges (RFC 1918/3927 +
RFC 4193/4291) blocked, terminal deny rule with the BLOCKED log prefix,
and — for a slirp4netns container — the reject of its gateway above t10.
verify_down() checks accept policy on output, drop on input, the DOWN
log prefix, and the range rejects — present for DOWN, with the gateway
reject, absent for DISENGAGED (which must also carry no deny-set reject). The allow-tier accepts the DOWN
postures log ALLOWED ahead of their catch-all are deliberately not
verified: they shape the audit trail, not what is enforced, and a down table
applied before they existed must still read as its posture;
verify_quarantine() checks the blackout ruleset. shield up / down /
quarantine run the matching verifier after applying and raise on any error.
Trust boundaries¶
┌─────────────────────────────────────────────────────┐
│ Host │
│ │
│ ┌──────────────────────────────────────────────┐ │
│ │ Container netns │ │
│ │ │ │
│ │ ┌────────────────────────────────────────┐ │ │
│ │ │ nftables rules │ │ │
│ │ │ (applied by OCI hook) │ │ │
│ │ │ │ │ │
│ │ │ policy: DROP │ │ │
│ │ │ allow: DNS, lo, @t40_project_allow │ │ │
│ │ │ gateway/loopback ports: literal IPs │ │ │
│ │ │ reject: @t20_security_deny, private │ │ │
│ │ └────────────────────────────────────────┘ │ │
│ │ │ │
│ │ ┌────────────────────────────────────────┐ │ │
│ │ │ Workload (untrusted) │ │ │
│ │ │ CAP_NET_ADMIN dropped │ │ │
│ │ │ CAP_NET_RAW dropped │ │ │
│ │ └────────────────────────────────────────┘ │ │
│ └──────────────────────────────────────────────┘ │
│ │
│ Host services (loopback only) │
└─────────────────────────────────────────────────────┘
The workload cannot modify nftables rules because CAP_NET_ADMIN and
CAP_NET_RAW are dropped.
Chain evaluation order¶
Hook mode (per-container netns, output chain):
preamble (lo, established, DNS, infra ports, +localhost grants) → t00 hard-deny (link-local + IMDS + the host's own loopback) → t10 override (kernel-timed to its expiry) → t20 security-deny (@t20_security_deny + RFC1918/RFC4193) → t30 provider allow → t40 project allow → bypass window (UP only) → terminal reject (log BLOCKED)
Rule ordering rationale: tier order is the authority order — an
accept/reject is a terminal verdict, so the first tier that matches wins.
The security-deny tier (@t20_security_deny_v4/_v6, plus the RFC 1918 /
RFC 4193 private-range rejects) sits above the provider- and project-allow
tiers, so an explicit deny wins over an allow, and a private-range host can be
reached only via the break-glass override tier (@t10_override_v4/_v6) —
not by adding it to an allow set. The hard-deny floor (t00, link-local +
169.254.0.0/16 IMDS) sits above even the override and is absolute. So does
the host's own loopback: pasta's map lies inside the link-local floor, and
under slirp4netns t00 refuses the gateway too, past the ports the preamble
opens. The gateway lies in a private range, whose reject sits below the
override, so without that rule a t10 element taking it in would open every
port the host listens on locally. The gateway is the one the container
launched with: pre_start records it in the bundle, and every rebuild and
override check reads it from there, so a cidr= changed in containers.conf
since the launch moves no running container's gateway. Denied
destinations are rejected and logged with the DENIED prefix and stay denied
across shield up / restarts via the security-deny tier and the policy/live
overlay. The terminal rule is a reject (not a bare drop).
The timed bypass window sits below every deny: it accepts what the allowlist would refuse, never a vault host, a private range or the hard-deny floor, and only the UP chain reads it.
Break-glass expiry is kernel-enforced¶
A t10 override with %expires is a set element with a kernel timeout: the
kernel drops it when its override runs out (a date holds through that day,
host local time), as it closes the bypass window — nothing in userspace keeps
the clock, so a crashed or absent host process cannot stretch it. An override
that already ran out is never authored. ruleset.nft is replayed verbatim at
every start of the container, so expiring overrides stay out of it: the OCI
hook reads their absolute deadlines from override.expiry and times each
element when it applies the ruleset, in the same transaction. A restart that
replays an old ruleset therefore never re-grants an override past its expiry.
Every shield up / down re-times the elements from the same deadlines.
That is the address plane. On the dnsmasq-* tiers an override of a name
also opens it in the DNS sinkhole, and a running dnsmasq keeps serving the
config it loaded: past the expiry the name still resolves, so an address the
host rotates to — one the t20 set never held — is reachable while the shield
is down or a bypass window is open. That window closes at the next shield up
/ down, which compares the dnsmasq config the policy calls for with the one
dnsmasq runs and relaunches it when they differ, or at a restart through the
orchestrator's refresh. A plain podman start replays the last written
config, so it keeps the name open until the next posture change.
A live break-glass (Shield.override) needs a reason as well as an expiry —
the shield refuses one without — and records both on the tier entry. Its
override audit event is attempted the moment the nft transaction commits,
ahead of the tier entry and the dnsmasq reload a name needs, so neither of
those failing can cost a grant the kernel holds its record. The record is
still best-effort, like every audit event: with audit disabled none is
written, and a write that fails only logs a warning — the grant stands either
way. The tier entry comes after the kernel: the override names
resolve first, held in memory, and the swap is computed from the tier as the
grant will leave it; the entry is written only once the kernel has taken that
swap, and the override cache takes the answers the swap was seeded from only
after the entry, so the next rebuild finds the cache fresh and resolves
nothing. A refused or failed grant therefore leaves nothing behind that a
later shield up / down would grant, and leaves the cache as it was — a
grant refused during a DNS outage does not empty the cache the overrides
already in force are reseeded from. The granted name must answer afresh;
another override whose lookup fails meanwhile keeps the addresses the cache
holds for it, so one grant never ends another. If the tier cannot record a
grant the kernel already holds, the error says what the next rebuild seeds in
its place: nothing, or the earlier entry for that target, whose own expiry may
be later. dnsmasq is reloaded either way, but its config comes from the tier,
so a first grant of a name the tier could not record stays sinkholed — the
error says that too.
A grant and a revoke never interleave. Each holds a per-container lock
(override.lock, an flock in the state directory) from its read of the t10
tier through its swap and its record, so a revoke that lands while a grant's
names resolve stays revoked — the grant swaps from the tier as the revoke left
it — and two grants at once both stay in the kernel. A grant resolves its
names before it takes the lock, so a revoke never waits on a resolver. An
expiry that runs out while the names resolve is refused before anything
reaches the kernel or the audit trail.
A revoke (Shield.revoke_override, terok-shield override --off) fails
closed. The tier entry goes first, then the ruleset.nft / override.expiry
a start replays, and only then the running t10 set. A failure at any later
step therefore leaves nothing that a rebuild or a plain start would re-open.
An override_revoked event is written only once no running ruleset holds the
override. If nft cannot drop it, the revoke fails, says the running ruleset
still admits it until the next shield up / down, and writes no such event.
A ruleset listing that fails is never taken for a stopped container: the
revoke — and a live allow — asks podman whether the container has a
process, swaps the running set when it does, treats a container podman has
no record of as stopped, and changes nothing when podman cannot say. A name's
dnsmasq config is rewritten even when no dnsmasq runs to reload it, and the
revoke reports that nothing serves the change before the next start.
The kernel ends new connections only, at a revoke as at an expiry: the ruleset accepts established traffic ahead of every tier, so a connection opened through the override — a tunnel, a keep-alive — keeps its direct route until it closes. The verb says so whenever it reports an override closing.
The t10 seed holds only what an override may open. A grant checks its own
target's fresh answer, yet it re-resolves every override name, and so do a
re-resolve and a rebuild over a stale cache. So the seed itself leaves out
any address on the host's own loopback or in the hard-deny floor, and notes
it in the log and the audit trail. The t00 rules refuse those addresses
above t10 regardless.
The deny on tiers without dnsmasq¶
The security tier denies vault-relayed provider hosts by address, so that
the deny holds in the DOWN posture and against direct-IP access. On the
dnsmasq-* tiers the names are also sinkholed in DNS, so an address a vault
host rotates to is never even learned. On lookup and getent nothing
sinkholes them: the deny holds only the addresses the names resolved to, and
an address they rotate to escapes it — while the shield is down, or while up
through an allowlisted name that shares the address or an open bypass window.
Shield narrows the gap (shield up / down re-resolve caches older than an
hour or than the policy before they rebuild) and names it: the log, an audit
note and the CLI verbs say which denied names it holds by resolved address
only, worded for the posture. Run a dnsmasq tier to close it.
Dual-stack (IPv4 + IPv6)¶
The firewall operates in dual-stack mode using nftables inet tables, which
handle both IPv4 and IPv6 within the same ruleset. Two parallel allow sets
are maintained:
t40_project_allow_v4(type ipv4_addr; flags interval;) — IPv4 allowlistt40_project_allow_v6(type ipv6_addr; flags interval;) — IPv6 allowlist
On the live tier the project-allow sets also carry a timeout flag so
dnsmasq-learned entries auto-expire. Parallel t20_security_deny_v4 /
t20_security_deny_v6 sets hold explicitly denied destinations. The
t10_override and bypass_window sets always carry the timeout flag with
no default: each element brings its own timeout, or none at all.
DNS resolution queries both A and AAAA records, and resolved addresses are automatically routed to the correct set. Rejects are cross-family:
- All private ranges use
reject with icmpx admin-prohibited - Default deny also uses
reject with icmpx admin-prohibited
Fail-closed guarantees¶
With setup-installed global hooks and a bundle prepared by
pre_start(), this
invariant holds: no path from "firewall setup failed" to "container running
unrestricted."
| Failure | Result |
|---|---|
| OCI hook raises an exception | Container torn down by podman (non-zero exit) |
nft binary missing or fails |
Hook exits non-zero → torn down |
state_dir annotation missing or invalid |
Hook exits non-zero → torn down |
| Bundle version mismatch | Hook exits non-zero → torn down |
ruleset.nft missing |
Hook exits non-zero → torn down |
| Ruleset fails to load | Hook exits non-zero → torn down |
| dnsmasq fails to start (dnsmasq tiers) | Hook exits non-zero → torn down |
shield up / down / quarantine additionally verify the applied ruleset
(verify_up / verify_down / verify_quarantine) and raise if any
invariant is missing.
terok-shield setup installs the global hooks. Use terok-shield run
(or call pre_start() via the Python API) before starting containers to
verify setup and prepare their annotated state bundles. Containers without
Shield annotations are not protected by these hooks.