host: root-runner witness for egress allow prefixes (#1357)
TestRootEgressPrefixKernelProof: the production asserter writes IPAddressAllow=10.250.64.0/20 on a host slice; a tenant under the template's IPAddressDeny= reaches 10.250.64.10, is refused at 10.250.80.10, and a revoke closes 10.250.64.10. Targets sit in their own namespace (carlos-egr3) so the own-address check stays on.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
internal/host/egress_prefix_rootrunner_linux_test.go+318 −0
Intent
<task-notification> <task-id>aba74a40d0b588afc</task-id> <tool-use-id>toolu_01N6mcSJwqPS6rE8brfCoFVC</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/22311489-9a69-4708-aa90-e32c2ae39ed2/tasks/aba74a40d0b588afc.output</output-file> <status>completed</status> <summary>Agent "Implement Task 3: console door + floor" finished</summary> <note>A task-notification fires each time this agent stops with no live background children of its own. The user can send it another message and resume it, so the same task-id may notify more than once.</note> <result>Status: all three review fixes are done and committed as 8e5b29c2. 1. **CLI help:** the longer `carlos apps set -h` help now lists the 8-entry limit, that host bits are refused (`10.0.64.5/20`, write `10.0.64.0/20`), the v0.43.0 floor on every box, and that a box refuses a range holding one of its own addresses. The copy record's IP1-53 entry matches. 2. **Fleet witnesses:** new test `TestEgressGrantRangeFloorCountsFleetBoxes` covers the three cases: - an old box under `fleets/fx/` gives a 409; - a detached box is not counted; - an unreadable fleet heartbeat gives a 503. I broke the code three ways on purpose (skip fleets, keep detached boxes, ignore an incomplete read) and the test caught each one. 3. **Warning wording:** it now reads "not checked against these boxes, which publish no address list (fleet boxes never do): %s. each one still refuses an entry that holds its own address", which is true for platform and fleet boxes. `cli.md:264` and the copy record (IP2-30) match. Tests: the targeted root, console, updatewords and census tests pass, and so do vet, gofmt and the darwin and windows builds. Concerns: - The root package's census hash moved again (copy only). I rewrote the `egress-allow-cidr-console` overlay in place; it still needs a reviewer verdict on #280. - The new copy in the copy record still needs the copy pass. - git again warned about the old `packed-refs.lock` (not from this task), but the commit landed. Report, with the fix report appended and the Strings section updated: /home/paulca/amadan.net/carlos/platform/.claude/worktrees/egress-allow-cidr/.superpowers/sdd/2026-11-22-egress-allow-cidr/task-3-report.md</result> <usage><subagent_tokens>165559</subagent_tokens><tool_uses>16</tool_uses><duration_ms>403868</duration_ms></usage> </task-notification>
Attributed to this commit: 4.15 g CO₂e (464,202 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on egress-allow-cidr, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.
diff --git a/internal/host/egress_prefix_rootrunner_linux_test.go b/internal/host/egress_prefix_rootrunner_linux_test.go| new file mode 100644 |
| index 000000000..f09c89273 |
| --- /dev/null |
| +++ b/internal/host/egress_prefix_rootrunner_linux_test.go |
| @@ -0,0 +1,318 @@ |
| +//go:build linux && rootrunner |
| + |
| +package host |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "io/fs" |
| + "net" |
| + "net/netip" |
| + "os" |
| + "os/exec" |
| + "path/filepath" |
| + "sync" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/instancectl" |
| +) |
| + |
| +// The prefix witness's names and addresses (#1357 Task 4). The namespace |
| +// and veth pair are its own: carlos-egr0 (kernel proof), carlos-egr1/1n |
| +// (Task 13) and carlos-egr2/2n (Task 14) belong to other witnesses. |
| +const ( |
| + egressPrefixNetns = "carlos-egr3" |
| + egressPrefixHostEnd = "carlos-egr3" |
| + egressPrefixNsEnd = "carlos-egr3n" |
| + |
| + egressPrefixAllow = "10.250.64.0/20" |
| + egressPrefixInside = "10.250.64.10" // inside the allow |
| + egressPrefixOutside = "10.250.80.10" // outside it, in the same /16 |
| + // egressPrefixBoxAddr is the box's end of the veth pair: outside the |
| + // allow, so the asserter's own-address check passes the prefix. |
| + egressPrefixBoxAddr = "10.250.96.1" |
| + |
| + egressPrefixHost = "egress-prefix.example.com" |
| + egressPrefixUnit = "carlos-egress-prefix-a" |
| +) |
| + |
| +// TestRootEgressPrefixKernelProof is the kernel witness for egress allow |
| +// prefixes (#1357): a host slice's IPAddressAllow=10.250.64.0/20, written |
| +// by the production asserter (newEgressAsserter: the real systemctl, the |
| +// box's own addresses from procfs, cgroupfs for the slice's running |
| +// units), lets a tenant in a child unit carrying the template's |
| +// IPAddressDeny= reach an address inside the prefix, refuses it one |
| +// outside the prefix in the same /16, and once the policy is revoked and |
| +// the asserter runs again, refuses the inside address too. |
| +// |
| +// The asserter's source is an in-memory policy for one host, so no |
| +// registry or control service is involved; everything from the source on |
| +// is production code. Its DueDir is in the test's scratch directory, not |
| +// the box's control directory. |
| +// |
| +// Network: the test reaches nothing beyond its own target fixture, a |
| +// network namespace carlos-egr3 behind a veth pair (carlos-egr3 on the |
| +// box with 10.250.96.1, carlos-egr3n inside with the two targets). The |
| +// targets are not addresses of the box, so the asserter's check against |
| +// LocalAddresses stays on. The tenant is this binary (plan14-tenant) |
| +// behind a Unix socket in a scratch directory. |
| +// |
| +// It logs one "verdict:" line per question (inside reachable, outside |
| +// refused, revoke closes inside), fails on any wrong answer, fails, never |
| +// skips, without root, systemd as PID 1 or cgroup v2, and removes every |
| +// unit, drop-in, slice state, link, namespace, listener and file it made. |
| +func TestRootEgressPrefixKernelProof(t *testing.T) { |
| + verdicts := map[string]string{} |
| + order := []string{"inside reachable", "outside refused", "revoke closes inside"} |
| + t.Cleanup(func() { |
| + for _, k := range order { |
| + v := verdicts[k] |
| + if v == "" { |
| + v = "not reached" |
| + } |
| + t.Logf("verdict: %s: %s", k, v) |
| + } |
| + }) |
| + verdict := func(k string, ok bool) { |
| + if ok { |
| + verdicts[k] = "yes" |
| + } else { |
| + verdicts[k] = "no" |
| + } |
| + } |
| + |
| + egressRootPrereqs(t) |
| + ctx := context.Background() |
| + host := egressPrefixHost |
| + slice := instancectl.SliceName(host) |
| + unitName := egressPrefixUnit + ".service" |
| + |
| + // Refuse anything this test would otherwise overwrite or remove. The |
| + // nudge leaves a runtime drop-in for the unit, removed on cleanup. |
| + unitControl := filepath.Join("/run/systemd/system.control", unitName+".d") |
| + if _, err := os.Lstat(unitControl); !errors.Is(err, fs.ErrNotExist) { |
| + t.Fatal("unit already has set-property drop-ins", unitControl, err) |
| + } |
| + |
| + scratch, err := os.MkdirTemp("", "egrp") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(func() { |
| + if err := os.RemoveAll(scratch); err != nil { |
| + t.Error(err) |
| + } |
| + }) |
| + |
| + targets := egressPrefixTargets(t) |
| + inside, outside := targets[egressPrefixInside], targets[egressPrefixOutside] |
| + |
| + egressSliceProperties(t, slice) |
| + |
| + // The production asserter over an in-memory policy for the host. |
| + var mu sync.Mutex |
| + allow := []string{egressPrefixAllow} |
| + src := func(_ context.Context, h string) (instancectl.EgressPolicy, error) { |
| + mu.Lock() |
| + defer mu.Unlock() |
| + if h != host { |
| + return instancectl.EgressPolicy{}, nil |
| + } |
| + return instancectl.EgressPolicy{Allow: append([]string(nil), allow...), Account: "abc", App: "shop"}, nil |
| + } |
| + e := newEgressAsserter(src, func() ([]string, error) { return []string{host}, nil }) |
| + e.Logf = t.Logf |
| + e.DueDir = filepath.Join(scratch, "egress-due") |
| + |
| + // Grant before the child starts (the start-path order). |
| + if err := e.Assert(ctx, host); err != nil { |
| + t.Fatal("grant: Assert:", err) |
| + } |
| + raw := egressShow(t, slice, "IPAddressAllow") |
| + t.Logf("slice IPAddressAllow after the grant: %q", raw) |
| + held, err := egressProofPrefixes(raw) |
| + if err != nil { |
| + t.Fatal("grant:", err) |
| + } |
| + if len(held) != 1 || held[0] != netip.MustParsePrefix(egressPrefixAllow) { |
| + t.Fatalf("grant: slice IPAddressAllow is %v, want exactly [%s]", held, egressPrefixAllow) |
| + } |
| + rep := e.Reports()[host] |
| + t.Logf("report after the grant: %+v", rep) |
| + if rep.State != "" || len(rep.Refused) != 0 { |
| + t.Fatalf("grant: report %+v, want no state and nothing refused", rep) |
| + } |
| + |
| + // The tenant. The cleanup of the nudge's drop-in is registered first, |
| + // so it runs after the unit's own cleanup has stopped and removed it. |
| + t.Cleanup(func() { |
| + if err := os.RemoveAll(unitControl); err != nil { |
| + t.Error(err) |
| + } |
| + if out, err := egressCtl("daemon-reload"); err != nil { |
| + t.Errorf("daemon-reload: %v: %s", err, out) |
| + } |
| + }) |
| + deny := egressTemplateDeny(t) |
| + socket := filepath.Join(scratch, "a.sock") |
| + c := egressUnitTenant(t, egressPrefixUnit, slice, deny, socket) |
| + t.Logf("unit's IPAddressAllow: %q, IPAddressDeny: %q", egressShow(t, unitName, "IPAddressAllow"), egressShow(t, unitName, "IPAddressDeny")) |
| + |
| + // Question 1: an address inside the prefix is reachable. |
| + in := egressWithin(t, time.Now().Add(egressLiveBound), c, true, inside) |
| + verdict("inside reachable", in) |
| + if !in { |
| + egressFirewallDiag(t, unitName, slice, c, inside) |
| + t.Errorf("inside: %s (in %s) not reachable under the slice's allow", egressPrefixInside, egressPrefixAllow) |
| + } |
| + |
| + // Question 2: an address outside it, in the same /16, is refused. |
| + out := !egressReach(t, c, outside) |
| + verdict("outside refused", out) |
| + if !out { |
| + egressFirewallDiag(t, unitName, slice, c, outside) |
| + t.Errorf("outside: %s (outside %s) reachable", egressPrefixOutside, egressPrefixAllow) |
| + } |
| + |
| + // Question 3: revoke the policy; the asserter's next run closes the |
| + // inside address for the running tenant (live, or by restarting it). |
| + mu.Lock() |
| + allow = nil |
| + mu.Unlock() |
| + if err := e.Assert(ctx, host); err != nil { |
| + t.Fatal("revoke: Assert:", err) |
| + } |
| + if got := egressShow(t, slice, "IPAddressAllow"); got != "" { |
| + t.Fatalf("revoke: slice IPAddressAllow is %q, want empty", got) |
| + } |
| + rep = e.Reports()[host] |
| + t.Logf("report after the revoke: %+v", rep) |
| + restarted := rep.Detail == "revoked (restarted)" |
| + deadline := time.Now().Add(egressLiveBound) |
| + if restarted { |
| + deadline = time.Now().Add(egressRestartBound) |
| + egressServingBy(t, unitName, c, deadline) |
| + } |
| + closed := egressWithin(t, deadline, c, false, inside) |
| + verdict("revoke closes inside", closed) |
| + t.Logf("revoke closed by restart: %v", restarted) |
| + if !closed { |
| + egressFirewallDiag(t, unitName, slice, c, inside) |
| + t.Errorf("revoke: %s still reachable after the policy was revoked (restarted %v)", egressPrefixInside, restarted) |
| + } |
| +} |
| + |
| +// egressPrefixTargets is the prefix witness's target fixture, built as |
| +// egressTargetNetnsAt builds Task 13's with its own names and addresses: |
| +// a namespace carlos-egr3 behind a veth pair, 10.250.96.1 on the box end, |
| +// 10.250.64.10 and 10.250.80.10 inside with a TCP listener on each served |
| +// by this process. It returns each target's host:port, keyed by address, |
| +// after checking each answers from the box and that no address of the |
| +// box lies in 10.250.64.0/20. Everything goes in t.Cleanup, last-made |
| +// first. |
| +func egressPrefixTargets(t *testing.T) map[string]string { |
| + t.Helper() |
| + var steps []func() |
| + t.Cleanup(func() { |
| + for i := len(steps) - 1; i >= 0; i-- { |
| + steps[i]() |
| + } |
| + }) |
| + cleanup := func(f func()) { steps = append(steps, f) } |
| + |
| + nsPath := filepath.Join("/run/netns", egressPrefixNetns) |
| + if _, err := os.Lstat(nsPath); !errors.Is(err, fs.ErrNotExist) { |
| + t.Fatal("network namespace already exists", nsPath, err) |
| + } |
| + if _, err := net.InterfaceByName(egressPrefixHostEnd); err == nil { |
| + t.Fatal("link already exists", egressPrefixHostEnd) |
| + } |
| + // Before anything is added: the asserter would refuse a prefix holding |
| + // an address of the box, and the fixture must not lean on one. |
| + prefix := netip.MustParsePrefix(egressPrefixAllow) |
| + local, err := instancectl.LocalAddresses() |
| + if err != nil { |
| + t.Fatal("LocalAddresses:", err) |
| + } |
| + for _, p := range local { |
| + if p.Overlaps(prefix) { |
| + t.Fatalf("this box already holds %s, inside %s: the witness cannot run here", p, prefix) |
| + } |
| + } |
| + |
| + egressIP(t, "netns", "add", egressPrefixNetns) |
| + cleanup(func() { |
| + if out, err := exec.Command("ip", "netns", "del", egressPrefixNetns).CombinedOutput(); err != nil { |
| + t.Errorf("ip netns del %s: %v: %s", egressPrefixNetns, err, out) |
| + } |
| + if _, err := os.Lstat(nsPath); !errors.Is(err, fs.ErrNotExist) { |
| + t.Errorf("network namespace %s left behind: %v", nsPath, err) |
| + } |
| + }) |
| + egressIP(t, "link", "add", egressPrefixHostEnd, "type", "veth", "peer", "name", egressPrefixNsEnd, "netns", egressPrefixNetns) |
| + cleanup(func() { |
| + if _, err := net.InterfaceByName(egressPrefixHostEnd); err == nil { |
| + if out, err := exec.Command("ip", "link", "del", egressPrefixHostEnd).CombinedOutput(); err != nil { |
| + t.Errorf("ip link del %s: %v: %s", egressPrefixHostEnd, err, out) |
| + } |
| + } |
| + // ip link del returns once the pair is gone. |
| + if _, err := net.InterfaceByName(egressPrefixHostEnd); err == nil { |
| + t.Errorf("link %s left behind", egressPrefixHostEnd) |
| + } |
| + }) |
| + egressIP(t, "addr", "add", egressPrefixBoxAddr+"/32", "dev", egressPrefixHostEnd) |
| + egressIP(t, "link", "set", egressPrefixHostEnd, "up") |
| + for _, a := range []string{egressPrefixInside, egressPrefixOutside} { |
| + egressIP(t, "route", "add", a+"/32", "dev", egressPrefixHostEnd, "src", egressPrefixBoxAddr) |
| + } |
| + |
| + in := func(args ...string) { |
| + t.Helper() |
| + egressIP(t, append([]string{"-n", egressPrefixNetns}, args...)...) |
| + } |
| + in("link", "set", "lo", "up") |
| + in("addr", "add", egressPrefixInside+"/32", "dev", egressPrefixNsEnd) |
| + in("addr", "add", egressPrefixOutside+"/32", "dev", egressPrefixNsEnd) |
| + in("link", "set", egressPrefixNsEnd, "up") |
| + in("route", "add", egressPrefixBoxAddr+"/32", "dev", egressPrefixNsEnd) |
| + |
| + addrs := map[string]string{} |
| + for _, a := range []string{egressPrefixInside, egressPrefixOutside} { |
| + ln, err := egressListenIn(nsPath, net.JoinHostPort(a, "0")) |
| + if err != nil { |
| + t.Fatalf("listen on %s in %s: %v", a, egressPrefixNetns, err) |
| + } |
| + cleanup(func() { ln.Close() }) |
| + go func() { |
| + for { |
| + c, err := ln.Accept() |
| + if err != nil { |
| + return |
| + } |
| + c.Close() |
| + } |
| + }() |
| + addrs[a] = ln.Addr().String() // an explicit port: the tenant dials host:port |
| + } |
| + // Each answers from the box itself (no cgroup filter here). IPv4 only, |
| + // so no neighbour-discovery wait: the dial's own SYN retries cover ARP. |
| + for a, hp := range addrs { |
| + c, err := net.DialTimeout("tcp", hp, 5*time.Second) |
| + if err != nil { |
| + t.Fatalf("target %s (%s) unreachable from the box: %v", a, hp, err) |
| + } |
| + c.Close() |
| + } |
| + // And still none inside the prefix with the fixture up. |
| + if local, err = instancectl.LocalAddresses(); err != nil { |
| + t.Fatal("LocalAddresses:", err) |
| + } |
| + for _, p := range local { |
| + if p.Overlaps(prefix) { |
| + t.Fatalf("the fixture left %s, inside %s, as an address of this box", p, prefix) |
| + } |
| + } |
| + return addrs |
| +} |