Containarium icon indicating copy to clipboard operation
Containarium copied to clipboard

Epic: eBPF virtual patching (deny rules → cleartext signatures → userspace WAF)

Open hsinatfootprintai opened this issue 5 months ago • 10 comments

Epic: eBPF virtual patching

Virtual patching = a temporary, network-level rule that blocks a known exploit before it reaches vulnerable software, buying time until the real vendor patch ships (zero downtime, no app change). The classic form is a WAF/IPS signature; the cheap-and-reliable form is an L3/L4 block rule.

Containarium already ships the enforcement substrate from the eBPF network-isolation work (#315): per-container-veth TC clsact hooks that can TC_ACT_SHOT, a per-tenant LPM-trie allow-list, a reconcile loop that pushes rules into BPF maps with zero downtime, and audit perf events on every deny. What's missing is a deny primitive and the inspection depth for signatures.

This epic tracks turning that substrate into a virtual-patching capability, in three tiers of increasing depth and cost.

Tiers

  • Tier 1 — L3/L4 virtual patch (deny rules). Add a per-tenant deny LPM-trie (CIDR + optional port/proto) evaluated before the allow logic, returning TC_ACT_SHOT. Covers the large class of "a network-reachable service / upstream is vulnerable — block it now." Pure eBPF, reuses the whole substrate. Ships first. → #660.

  • Tier 2 — bounded cleartext signature match (kernel). Extend the BPF program with bpf_skb_load_bytes over the first ~256 payload bytes and a small bpf_loop scan for a curated set of cleartext signatures (Log4Shell ${jndi:, Shellshock () {). Caveats spelled out: single-packet only (segment-split evasion), cleartext only (no TLS), verifier-bounded pattern count/length. → #661.

  • Tier 3 — userspace WAF behind eBPF steering. eBPF redirects flagged flows into a host-side WAF (Coraza / ModSec CRS) that does reassembly, TLS termination, and vendor signature sets. The only WAF-grade path; eBPF's role shrinks to a fast classifier/redirector. → #662.

Product hook

Wire to the existing security scanner: a CVE finding emits a virtual-patch deny rule → reconcile pushes it → reconcile auto-expires it when the scanner confirms the upstream patch landed. "Band-Aid until the real patch ships, then auto-removed," end to end.

Honest constraints (why tiers, not one feature)

  • Pure-kernel eBPF sees one packet at a time (no reassembly) and can't read TLS payloads — so signature matching past Tier 1 is genuinely limited.
  • All tiers inherit the existing two opt-ins: feature off unless CONTAINARIUM_NETWORK_POLICY_BPF_OBJECT is set, and drops disarmed unless CONTAINARIUM_NETWORK_POLICY_ENFORCE=1. A misbroad deny rule must not blackhole a container by default.

Design doc: docs/security/VIRTUAL-PATCHING-DESIGN.md (added with Tier 1).

Prior art in-tree: docs/security/NETWORK-ISOLATION-DESIGN.md, experimental/ebpf-phaseA/netpolicy.bpf.c, internal/netbpf/, internal/server/network_policy_enforcer.go.

hsinatfootprintai avatar Jun 12 '26 04:06 hsinatfootprintai

Progress — Tier 1 (#660) implemented

Tier 1 (L3/L4 deny rules) is code-complete in PR #664 (ready for review).

Delivered:

  • proto NetworkPolicy.deny_rules + NetworkPolicyDenyRule (cidr, port, proto, note, expires_at)
  • BPF deny_cidr LPM trie evaluated before the allow-list (deny beats allow); deny_event.reason distinguishes a virtual-patch drop in the audit log
  • daemon reconcile installs/expires deny entries (diffDeny, expiry self-removal); audit action network_policy.virtual_patch
  • CLI network-policy patch add/rm/list; set preserves deny rules
  • design doc docs/security/VIRTUAL-PATCHING-DESIGN.md + runnable runbook experimental/ebpf-phaseA/validate-virtual-patch.sh + CHANGELOG

Validation:

  • ✅ full Go unit coverage (compile / plan / diff / expiry / CIDR-dedup / CLI)
  • ✅ kernel verifier accepts the new map + deny-first branch on a real 6.8 kernel (isolated bpftool prog loadall)
  • ⏳ end-to-end armed-drop test deferred to the normal backend release-upgrade cycle (won't swap a released binary on a live multi-tenant node just to test)

Status:

  • Tier 1 (#660) — code-complete, PR #664 in review
  • Tier 2 (#661) — designed, not started
  • Tier 3 (#662) — designed, not started

hsinatfootprintai avatar Jun 12 '26 10:06 hsinatfootprintai

Progress — Tier 2 (#661) PR-A implemented

Tier 2 PR-A (inbound cleartext signature scanning) is in PR #665 (draft, pending on-backend runtime validation).

Delivered:

  • In-kernel payload scan on the container-receive path (veth TC_EGRESS) — one bpf_loop over a (signature × offset) space, per-CPU scratch buffer, tiered constant-size loads. The verifier-acceptance unknown is resolved — proven on a 6.8 backend (three earlier shapes rejected; documented).
  • Built-in curated signature set (Log4Shell, Shellshock, Spring4Shell, path-traversal, /etc/passwd); network_policy.signature_match audit naming the signature.
  • Three independent opt-ins: BPF_OBJECT (load) / SIGNATURES=1 (scan) / ENFORCE=1 (drop). sig_config gate so the scan cost is only paid when enabled.
  • Design + runbook: docs/security/VIRTUAL-PATCHING-TIER2.md, validate-signature-scan.sh.

Honest constraints (documented, not hidden): single-packet (segment-split evasion), cleartext-only (TLS opaque), first 256 bytes — best-effort pre-filter, not a WAF.

Status:

  • Tier 1 (#660) — ✅ merged
  • Tier 2 (#661) — PR-A in review (#665); PR-B (operator signature CRUD API/CLI) is the follow-up
  • Tier 3 (#662) — designed, not started

hsinatfootprintai avatar Jun 12 '26 13:06 hsinatfootprintai

Progress — Tier 2 (#661) PR-B implemented

Operator-managed signatures (PR-B) are in PR #666 (draft, stacked on #665).

Adds containarium network-policy signature add/rm/list — global operator exploit signatures that augment PR-A's built-in set:

  • proto NetworkPolicySignature + Set/List/Delete RPCs (admin-only)
  • dedicated global store (mem + Postgres network_policy_signatures); stable ids in a reserved range (OperatorIDBase=1000) so audit matches name their source
  • enforcer merges built-ins + enabled operator sigs (capped to 32 slots), re-syncs on reconcile with a fingerprint to skip no-op rewrites
  • full unit coverage; gosec G115 clean

Status:

  • Tier 1 (#660) — ✅ merged
  • Tier 2 (#661) — PR-A #665 + PR-B #666 (both draft pending the shared on-backend runtime validation). #661 is now feature-complete pending that.
  • Tier 3 (#662) — designed, not started

hsinatfootprintai avatar Jun 12 '26 13:06 hsinatfootprintai

Epic status — all three tiers now have a landing place

  • Tier 1 (#660) — ✅ merged (L3/L4 deny rules)
  • Tier 2 (#661) — feature-complete: PR-A #665 (kernel signature scan) + PR-B #666 (operator signature CRUD), both draft pending the shared on-backend runtime validation
  • Tier 3 (#662) — design resolved + backend-validated in PR #667 (design-only); implementation is a phased PR-1…PR-5 effort to start on sign-off + a concrete need

The eBPF virtual-patching epic is now: Tier 1 shipped, Tier 2 built (awaiting backend validation), Tier 3 designed. The kernel-feasibility unknowns for all three are resolved on real hardware (Tier 1 deny-map verifier load, Tier 2 signature-scan verifier acceptance, Tier 3 steering-primitive availability).

hsinatfootprintai avatar Jun 12 '26 13:06 hsinatfootprintai

Tier 3 PR-1 (steering skeleton) landed in #667 — CI green

#667 now carries the design + PR-1 (the steering de-risk):

  • internal/waf transparent proxy: accepts TPROXY-steered conns, recovers the original dst via IP_TRANSPARENT (LocalAddr), forwards both ways. Forward-only — no WAF yet (Coraza = PR-2).
  • Off by default (CONTAINARIUM_WAF_TPROXY_ADDR), Linux-only at runtime (compiles everywhere), netns-isolated validation runbook.
  • All CI green (unit/e2e/gosec/govulncheck/golangci-lint).

Validation: backend capability confirmed (6.8: nft_tproxy + bpf_sk_assign + CAP_NET_ADMIN); the netns end-to-end run is scripted but deferred to a maintenance window (sudo+nft on the shared host).

Epic state: Tier 1 ✅ merged · Tier 2 built (#665/#666) · Tier 3 design + PR-1 (#667) — all draft pending the shared on-backend runtime validation + the Tier 3 architecture sign-off (Coraza-vs-caddy-plugin, waf build tag, fail-open/closed).

hsinatfootprintai avatar Jun 12 '26 14:06 hsinatfootprintai

Tier 3 PR-2 (inspection seam) landed in #667 — CI green

#667 now carries design + PR-1 (steering) + PR-2 (inspection):

  • pluggable Inspector wired into the proxy: reassembles the request head across TCP segments → inspect → 403 when armed (observe-only else; both audit network_policy.waf_block).
  • reference BuiltinInspector — already beats Tier 2 (catches a signature split across segments that the single-packet kernel scan can't; test proves it).
  • zero new dependencies — Coraza+CRS is a drop-in behind the same interface, deliberately deferred so the OSS supply-chain add is an explicit decision (the design's open question).

Epic state: Tier 1 ✅ merged · Tier 2 built (#665/#666) · Tier 3 design + PR-1 + PR-2 (#667), CI green. Remaining Tier 3 (Coraza engine, policy-driven nft, TLS, rule lifecycle = PR-3..5) waits on the dependency/architecture sign-off + the deferred netns runtime validation.

hsinatfootprintai avatar Jun 12 '26 14:06 hsinatfootprintai

Tier 2 merged — epic now: 2 of 3 tiers shipped

  • Tier 1 (#660) — ✅ merged (L3/L4 deny rules)
  • Tier 2 (#661) — ✅ merged + closed (PR-A #665 bb39d16 + PR-B #666 cf8d443): in-kernel cleartext signature scan + operator signature CRUD
  • Tier 3 (#662) — design + PR-1 (steering) + PR-2 (inspection seam) in #667 (draft); WAF-engine decision doc written (coraza-caddy-first hybrid). Awaiting human go/no-go on the Coraza dependency + east-west scope before PR-3+.

All three tiers' kernel-feasibility unknowns are resolved on real hardware. Remaining cross-tier item: the runtime acceptance of Tier 2 (armed drop + Postgres path) rides the next backend upgrade; Tier 3's netns steering run is similarly deferred to a maintenance window.

hsinatfootprintai avatar Jun 12 '26 14:06 hsinatfootprintai

Tier 3 PR-3 (coraza-caddy ingress WAF) — PR #670, CI green

Implements the signed-off coraza-caddy-first path:

  • coraza module added to the custom Caddy build only when WAF opted in (CaddyConfig.WAF) — daemon go.mod untouched (Coraza lives in the Caddy build).
  • CaddyWAFHandler prepended before reverse_proxy on ingress routes; CRS via DefaultWAFDirectives; SecRuleEngine DetectionOnly unless ENFORCE armed.
  • Off by default (CONTAINARIUM_WAF_INGRESS=1); routes byte-identical when off (tested).
  • 6 unit tests green; gosec/golangci clean.

Ingress only — east-west is the standalone TPROXY proxy (#667). Custom-Caddy build + deploy + Log4Shell-over-TLS validation are operator steps (runbook VIRTUAL-PATCHING-TIER3-INGRESS-WAF.md).

Epic state: Tier 1 ✅ merged · Tier 2 ✅ merged · Tier 3 = #667 (design + steering PR-1 + inspection PR-2, east-west) + #670 (ingress WAF PR-3), both draft. The WAF-grade tier now has working ingress (coraza-caddy) + east-west (TPROXY) halves.

hsinatfootprintai avatar Jun 12 '26 15:06 hsinatfootprintai

Capstone: scanner → virtual-patch (auto-quarantine) — PR #671, CI green

The epic's product hook, realized honestly. ClamAV malware detection → block the tenant's egress (deny-all virtual-patch rule) → auto-release on a clean scan.

Scoping call worth recording: wiring the generic 'CVE → virtual-patch rule' surfaced a real mismatch — Trivy package CVEs describe a vuln inside a container with no network endpoint to block, so a deny rule can't patch them (deliberately not wired). ClamAV's infected/clean verdict maps cleanly onto the Tier 1 deny rules, so that's what ships. Web-vuln → WAF rules is the other sound path (Tier 3, future).

Off by default (CONTAINARIUM_SECURITY_AUTO_QUARANTINE=1); safe against operator rules (note-marked, never clobbers an operator 0.0.0.0/0); self-healing (24h expiry backstop). Caveats: per-tenant egress block, only drops when the BPF enforcer is armed. Doc: docs/security/AUTO-QUARANTINE.md.

Epic state

  • Tier 1 (#660) ✅ merged · Tier 2 (#661) ✅ merged · Tier 3 (#662) = #667 (steering+inspection) + #670 (ingress WAF), draft
  • Capstone (scanner→virtual-patch): #671 (auto-quarantine), draft

All four kernel-feasibility unknowns resolved on hardware; the capstone ties the scanner to the deny-rule infra. Open PRs (#667/#670/#671) await review + the deferred backend runtime validations.

hsinatfootprintai avatar Jun 12 '26 16:06 hsinatfootprintai

Epic fully merged to main 🎉

All virtual-patching work is now on main:

Piece PR Squash
Tier 1 — L3/L4 deny rules (#660) #664 merged
Tier 2 — in-kernel signature scan (#661) #665 bb39d16
Tier 2 — operator signature CRUD (#661) #666 cf8d443
Capstone — scanner→auto-quarantine #671 8e60cd9
Tier 3 — design + steering + inspection (#662) #667 52622b6
Tier 3 — coraza-caddy ingress WAF (#662) #670 478a507

Capability arc: L3/L4 deny rules → in-kernel cleartext signatures → east-west TPROXY steering + reassembling inspection → north-south coraza-caddy ingress WAF → scanner-driven auto-quarantine. Every piece off-by-default; every kernel-feasibility unknown resolved on real hardware (6.8).

Outstanding (operational, not code)

The following are backend runtime validations deferred to maintenance windows (each off-by-default, so dormant until enabled):

  • Tier 2 armed-drop + Postgres signature path (validate-signature-scan.sh)
  • Tier 3 netns steering run (validate-tproxy-steering.sh) + custom-Caddy build + Log4Shell-over-TLS
  • Auto-quarantine EICAR + armed-enforcer end-to-end

Optional future refinements (designed, not built): Tier 3 PR-4 (proxy-side TLS termination for east-west) and PR-5 (per-tenant WAF rule lifecycle + scanner→WAF-rule hook). #662 can stay open to track those, or close as core-complete.

hsinatfootprintai avatar Jun 12 '26 16:06 hsinatfootprintai