carlos / platform Public

edge fence: check the fence when the box's addresses cannot be read (review R1)

Review round 1 (Codex), P1: with the fence exempting and the address
read failing, the refresh skipped the fence check entirely, so a flush
left running instances open for as long as the read failed.
fenceWithoutAddresses now checks the remembered addresses on every tick
and before every start: the fence reads back, or is reloaded, or the
whole deny is rebuilt from the last deny plus those addresses.

P2: not_needed claimed calls already work, which nothing observed. The
state is gone; a box with no public address reports off, saying so.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
James Adam pushed by james@keymail.dev cbf450cc32dd535e6c685e484e8f9e56ef58b5d7 parent 7aad44c
9 files changed, +183 −30
  • docs/superpowers/plans/2026-11-13-same-box-calls-edge-fence.md +18 −3
  • internal/instancectl/edgefence.go +41 −5
  • internal/instancectl/edgefence_test.go +93 −4
  • internal/instancectl/hardening.go +18 −1
  • internal/instancectl/names.go +5 −7
  • internal/provision/testdata/plan14-root-reads-same-box-edge-fence.tsv +3 −3
  • internal/status/snapshot.go +2 −2
  • status_instances.go +1 −3
  • status_instances_test.go +2 −2
diff --git a/docs/superpowers/plans/2026-11-13-same-box-calls-edge-fence.md b/docs/superpowers/plans/2026-11-13-same-box-calls-edge-fence.md
index b7d7bc170..763fd6cc7 100644
--- a/docs/superpowers/plans/2026-11-13-same-box-calls-edge-fence.md
+++ b/docs/superpowers/plans/2026-11-13-same-box-calls-edge-fence.md
@@ -38,6 +38,22 @@ package (stopping rule 4).
untested (Evidence, run 2). A round 2 is the Task 7 review of the
code, not of this plan.
+**Branch review history (stopping rules apply):**
+- **Round 1** (Codex `gpt-6-astra`, 2026-11-13, at `7aad44ce7`): NOT
+ READY, 1 P1, 1 P2, no P3; census: every unit approved, the
+ `internal/instancectl` envelope rejected for the two findings.
+ - P1, family **"fail-closed enforcement of the box-address
+ boundary"** (first round in it): with the fence exempting and the
+ box's addresses unreadable, the refresh skipped the fence check, so
+ a flush left running instances open for as long as the read failed.
+ Fixed: `fenceWithoutAddresses` checks the remembered addresses on
+ every tick and before every start, reloads the fence, or rebuilds
+ the whole deny from the last deny plus those addresses.
+ - P2, family **"status describes observed state"**: `not_needed`
+ asserted calls already worked. Fixed: the state is gone; a box with
+ no public address reports `off (no public address on this box)`.
+- A round 2 checks those two fixes (rule 1: round 1 had a P1).
+
## The invariant
> An instance reaches an address of its own box only on TCP 80 or 443,
@@ -387,9 +403,8 @@ Where the build departed from the text above, and why:
- [x] The box report carries the fence state and reason, and the CLI
shows it per box (UX pass first, per AGENTS.md). James chose the
placement on 2026-11-13: one line per box in `carlos status --layout`,
- after `exec-sandbox:`. States: `on`, `off (reason)`, `not needed
- (reason)` (the public address is not on the box, as on EC2 without
- IPv6: calls already go out and back), `unknown`.
+ after `exec-sandbox:`. States: `on`, `off (reason)`, `unknown`
+ (`not needed` was cut in review round 1, P2).
**Where (traced 2026-11-13):** `carlos instances list` has one row per
instance, not per box, and shows nothing from the probe today. The
diff --git a/internal/instancectl/edgefence.go b/internal/instancectl/edgefence.go
index 7e894cbbb..49cc599ae 100644
--- a/internal/instancectl/edgefence.go
+++ b/internal/instancectl/edgefence.go
@@ -52,7 +52,7 @@ type fenceInternalKey struct{}
const (
fenceOffSwitch = "turned off with " + EdgeFenceEnv + "=off"
fenceOffGroup = "a region group box: the group hairpin decides"
- fenceOffNoAddr = "the public address is not on this box, so calls already go out and back"
+ fenceOffNoAddr = "no public address on this box"
fenceOffUnwired = "not wired"
fenceOffClaims = "host claims mode"
)
@@ -246,11 +246,47 @@ func (s *Server) edgeFenceReport() *EdgeFenceReport {
if !s.denyApplied {
return nil
}
- switch {
- case len(s.fenceAddrs) > 0:
+ if len(s.fenceAddrs) > 0 {
return &EdgeFenceReport{State: EdgeFenceOn}
- case s.fenceReason == fenceOffNoAddr:
- return &EdgeFenceReport{State: EdgeFenceNotNeeded, Reason: s.fenceReason}
}
return &EdgeFenceReport{State: EdgeFenceOff, Reason: s.fenceReason}
}
+
+// fenceOffUnread is the reason when the box's addresses could not be read
+// and the remembered fence did not read back or reload.
+const fenceOffUnread = "the box's addresses could not be read and the fence did not read back"
+
+// fenceWithoutAddresses keeps the invariant when the box's addresses
+// cannot be read while the applied deny exempts some for the edge fence
+// (review round 1, P1): the fence for the remembered addresses must read
+// back, or reload, or the whole deny is rebuilt from what was last
+// applied plus those addresses and applied. whole reports that last case.
+// The caller holds denyMu.
+func (s *Server) fenceWithoutAddresses(ctx context.Context) (whole bool, err error) {
+ s.mu.Lock()
+ addrs, last := slices.Clone(s.fenceAddrs), slices.Clone(s.lastDeny)
+ s.mu.Unlock()
+ if len(addrs) == 0 || s.NFT == nil {
+ return false, nil
+ }
+ verr := edgefence.Verify(ctx, s.NFT, addrs)
+ if verr == nil {
+ return false, nil
+ }
+ internal := context.WithValue(ctx, fenceInternalKey{}, true)
+ if _, serr := s.Systemctl(internal, "start", InstancesSlice); serr == nil {
+ if edgefence.Install(ctx, s.NFT, addrs) == nil && edgefence.Verify(ctx, s.NFT, addrs) == nil {
+ s.logf("instance-control: edge fence: %v; reloaded (the box's addresses could not be read)", verr)
+ return false, nil
+ }
+ }
+ rebuilt := last
+ for _, a := range addrs {
+ rebuilt = append(rebuilt, netip.PrefixFrom(a, a.BitLen()))
+ }
+ if err := s.applyLocalDeny(ctx, rebuilt, true); err != nil {
+ return false, fmt.Errorf("edge fence: %v; the whole deny could not be applied: %w", verr, err)
+ }
+ s.recordFence(nil, fenceOffUnread)
+ return true, verr
+}
diff --git a/internal/instancectl/edgefence_test.go b/internal/instancectl/edgefence_test.go
index 79fb3f565..a397666e0 100644
--- a/internal/instancectl/edgefence_test.go
+++ b/internal/instancectl/edgefence_test.go
@@ -389,9 +389,9 @@ func TestEveryStartIsSeenByTheFenceCheck(t *testing.T) {
}
}
-// /ready carries the fence's state once a deny has been applied: on, off
-// with the reason, or not needed where the public address is not on the
-// box (EC2 without IPv6), where a call already leaves the box.
+// /ready carries the fence's state once a deny has been applied: on, or
+// off with the reason (a box with no public address is off, saying so,
+// never "not needed": review round 1, P2).
func TestEdgeFenceReport(t *testing.T) {
s, _, _ := fenceServer(t)
if r := s.edgeFenceReport(); r != nil {
@@ -415,7 +415,7 @@ func TestEdgeFenceReport(t *testing.T) {
if err := s.denyLocalAddrs(context.Background()); err != nil {
t.Fatal(err)
}
- if r := s.edgeFenceReport(); r == nil || r.State != EdgeFenceNotNeeded {
+ if r := s.edgeFenceReport(); r == nil || r.State != EdgeFenceOff || r.Reason != fenceOffNoAddr {
t.Fatalf("no public address on the box: %+v", r)
}
}
@@ -476,3 +476,92 @@ func TestDenyLoopRetriesAFailedFence(t *testing.T) {
t.Fatal("a fence switched off was retried")
}
}
+
+// Review round 1, P1: with the fence exempting and the box's addresses
+// unreadable, a flushed fence is still checked every tick: reloaded for
+// the remembered addresses, or the whole deny rebuilt from the last deny
+// plus those addresses. The pre-start check does the same and refuses the
+// start.
+func TestFenceIsCheckedWhenAddressesCannotBeRead(t *testing.T) {
+ unreadable := func() ([]netip.Prefix, error) { return nil, errors.New("fib_trie: permission denied") }
+ loop := func(t *testing.T, s *Server) func() {
+ ticks, handled := make(chan time.Time), make(chan struct{})
+ ctx, cancel := context.WithCancel(context.Background())
+ t.Cleanup(cancel)
+ go s.denyLoopWith(ctx, ticks, func() { handled <- struct{}{} })
+ return func() { ticks <- time.Time{}; <-handled }
+ }
+
+ t.Run("reloaded", func(t *testing.T) {
+ s, ctl, nft := fenceServer(t)
+ tick := loop(t, s)
+ tick()
+ s.mu.Lock()
+ s.LocalAddrs = unreadable
+ s.mu.Unlock()
+ nft.set(func(f *fakeNFT) { f.loaded = "" })
+ _, before := nft.counts()
+ for i := 0; i < 3; i++ {
+ tick()
+ }
+ if _, after := nft.counts(); after-before < 3 {
+ t.Fatalf("%d readbacks over 3 ticks with unreadable addresses", after-before)
+ }
+ if loads, _ := nft.counts(); loads != 2 {
+ t.Fatalf("loads = %d, want the first and one reload", loads)
+ }
+ if !s.EdgeFence().On || ctl.count("set-property") != 1 {
+ t.Fatalf("a reloaded fence changed the deny: %+v %q", s.EdgeFence(), ctl.joined())
+ }
+ })
+
+ t.Run("whole deny", func(t *testing.T) {
+ s, ctl, nft := fenceServer(t)
+ tick := loop(t, s)
+ tick()
+ s.mu.Lock()
+ s.LocalAddrs = unreadable
+ s.mu.Unlock()
+ nft.set(func(f *fakeNFT) { f.loaded, f.failLoad = "", errors.New("exit status 1") })
+ tick()
+ if n := ctl.count(wholeDeny); n != 1 {
+ t.Fatalf("whole deny applied %d times; %q", n, ctl.joined())
+ }
+ if r := s.edgeFenceReport(); r == nil || r.State != EdgeFenceOff || r.Reason != fenceOffUnread {
+ t.Fatalf("/ready = %+v", r)
+ }
+ tick()
+ tick()
+ if n := ctl.count("set-property"); n != 2 {
+ t.Fatalf("set-property %d times, want the fenced deny then the whole one", n)
+ }
+ // Addresses readable and nft working again: the fence comes back.
+ s.mu.Lock()
+ s.LocalAddrs = fenceLocal
+ s.mu.Unlock()
+ nft.set(func(f *fakeNFT) { f.failLoad = nil })
+ tick()
+ if !s.EdgeFence().On || ctl.count(fencedDeny) != 2 {
+ t.Fatalf("the fence did not come back: %+v %q", s.EdgeFence(), ctl.joined())
+ }
+ })
+
+ t.Run("before a start", func(t *testing.T) {
+ s, ctl, nft := fenceServer(t)
+ s.Systemctl = s.guardStarts(ctl.run)
+ if err := s.denyLocalAddrs(context.Background()); err != nil {
+ t.Fatal(err)
+ }
+ s.LocalAddrs = unreadable
+ nft.set(func(f *fakeNFT) { f.stale, f.failLoad = true, errors.New("exit status 1") })
+ if _, err := s.Systemctl(context.Background(), "start", "carlos-instance@a.service"); err == nil {
+ t.Fatal("the start went ahead")
+ }
+ if ctl.count("start carlos-instance@a.service") != 0 {
+ t.Fatal("the unit started")
+ }
+ if n := ctl.count(wholeDeny); n != 1 {
+ t.Fatalf("whole deny applied %d times before the refusal; %q", n, ctl.joined())
+ }
+ })
+}
diff --git a/internal/instancectl/hardening.go b/internal/instancectl/hardening.go
index 4b5a1aee1..0738cb663 100644
--- a/internal/instancectl/hardening.go
+++ b/internal/instancectl/hardening.go
@@ -317,7 +317,14 @@ func (s *Server) denyLocalAddrsWith(ctx context.Context, refresh bool) error {
func (s *Server) denyLocalAddrsLocked(ctx context.Context, refresh bool) error {
local, err := s.localPrefixes()
if err != nil {
- return fmt.Errorf("this box's addresses: %v; the last deny stands", err)
+ // #990: the last deny stands, but if it exempts addresses for the
+ // edge fence, that fence is checked from what was remembered:
+ // reloaded, or the whole deny rebuilt (review round 1, P1).
+ whole, fenceErr := s.fenceWithoutAddresses(ctx)
+ if whole {
+ return errors.Join(fmt.Errorf("this box's addresses: %v; the whole box-address deny applies", err), fenceErr)
+ }
+ return errors.Join(fmt.Errorf("this box's addresses: %v; the last deny stands", err), fenceErr)
}
local, hairpinErr := s.hairpinDeny(ctx, local)
if hairpinErr != nil {
@@ -425,6 +432,16 @@ func (s *Server) denyLoopWith(ctx context.Context, ticks <-chan time.Time, done
case <-ticks:
}
local, err := s.localPrefixes()
+ if err != nil {
+ // #990: an exempting fence is still checked when the addresses
+ // cannot be read (fenceWithoutAddresses).
+ s.mu.Lock()
+ fence := len(s.fenceAddrs) > 0
+ s.mu.Unlock()
+ if fence {
+ err = s.denyLocalAddrsWith(ctx, true)
+ }
+ }
logErr(err)
if err == nil {
s.mu.Lock()
diff --git a/internal/instancectl/names.go b/internal/instancectl/names.go
index d4f71d13f..08b95453f 100644
--- a/internal/instancectl/names.go
+++ b/internal/instancectl/names.go
@@ -662,10 +662,9 @@ type ProbeReport struct {
}
// EdgeFenceReport is whether instances on this box can call other apps on
-// it through the edge (#990): State is EdgeFenceOn, EdgeFenceOff or
-// EdgeFenceNotNeeded (the box's public address is not on the box, so a
-// call already leaves it and comes back through the edge); Reason says
-// why when it is not on.
+// it through the edge (#990): State is EdgeFenceOn or EdgeFenceOff, and
+// Reason says why when it is off. It reports what root applied, never a
+// routing outcome root did not observe (review round 1, P2).
type EdgeFenceReport struct {
State string `json:"state"`
Reason string `json:"reason,omitempty"`
@@ -673,9 +672,8 @@ type EdgeFenceReport struct {
// The edge fence report's states.
const (
- EdgeFenceOn = "on"
- EdgeFenceOff = "off"
- EdgeFenceNotNeeded = "not_needed"
+ EdgeFenceOn = "on"
+ EdgeFenceOff = "off"
)
// BuildHeader carries the control service's version.Build on every /ready
diff --git a/internal/provision/testdata/plan14-root-reads-same-box-edge-fence.tsv b/internal/provision/testdata/plan14-root-reads-same-box-edge-fence.tsv
index 78573fc92..c90f6be74 100644
--- a/internal/provision/testdata/plan14-root-reads-same-box-edge-fence.tsv
+++ b/internal/provision/testdata/plan14-root-reads-same-box-edge-fence.tsv
@@ -22,10 +22,10 @@ internal/instanceexec/probe_hardening.go readBoxAddrs legacy accepted-legacy acc
internal/instancectl/server.go runWithClaim authority C,D,G Claim instance controller adds held-release RPC dispatch within existing launch/participation execution boundary.;rev3=census-ruling-rootfs-apps-2;basis=takes a RegistryView (HostApp and HostRuntime) for the bare hostApp, and the production Server literal gains HostRuntime, ProbeImage: BuildProbeImage, ProbeImageFile: ProbeImagePath, RootfsProbeOut under /run/carlos-control and DenyEvery: 10s; this body reads nothing new itself; the readers it wires (instanceRegistryHostRuntime, BuildProbeImage, the rootfs probe's output) are decision rows;rev3=census-ruling-instance-egress-sizes-328-329;basis=#328 plan 2 Task 4 and #329 plan 3 Task 5: takes the egress asserter as a parameter, sets it on the production Server literal (Egress) and starts its 30 s Loop beside probeLoop and diskLoop; the literal also names the fixture check ProbeFixturesOK (renamed in plan 2 Task 14). It sets no control drop-in directory: plan 3's ControlDropInDir default (/run/systemd/system.control) was deleted with removeGenControlDropIn in ef38e13b (the P1 EROFS fix), so instance control names systemd's control directory only to read it (TrustedDropinReader). The same four sites (the two exec.CommandContext closures, statfsBytes, the data literal), offsets moved; no read added.;rev3=census-ruling-agent-start-demo-1;basis=Wires root-owned demo config and existing fixed paths into the service.;rev3=census-ruling-region-ha-p3b;basis=Resume root-owned hairpin state before probes/control service work; verify current systemd agent identity and kernel fence/addresses, restore pidfd watch or revoke stale state. Same direct exec/statfs/data sites; indirect inputs are protected root snapshot/state and kernel evidence, never receipt data.;rev3=census-ruling-agent-start-demo-3;basis=Construct the demo snapshot supervisor for configured demo boxes; startup cleanup gates builder readiness. Census read-site delta: unchanged (4).;rev3=census-ruling-gvisor-demos;basis=Retain sandbox modes and error; bind host-provided baseline and runtime callbacks without importing host templates. Existing reads unchanged. Task 6: Capture HostClaims once from ControlConfig/hostwrite.Enabled and set the fixed root-owned bundle discovery directory. Existing command and configuration reads unchanged. Task 7: enable gVisor worker construction through fixed unitFamily(roleBuild); readiness proof rechecks sandbox admission and current Ready without reaching the constructor fatal path for transient refusals. Existing command/config reads unchanged. Final R1 P2: wire the fixed /var/lib/carlos/oci-rootfs parent for native readiness evidence; no request-supplied path. Carry the root-only host.env validation callback into Ready; no agent read or caller-selected path.;rev3=census-ruling-same-box-edge-fence;basis=Root. Sets NFT: edgefence.Exec (runs /usr/sbin/nft from PATH with fixed arguments and root-derived stdin, see the envelope) and EdgeFenceOff from os.Getenv; wraps s.Systemctl with guardStarts and calls bindEgressFence; calls denyLocalAddrs once before Serve so a leftover runtime exemption is checked or replaced by the whole deny before any request is served. No new file read; the exec is nft's. data-literal@2661,file:CommandContext@1151,file:CommandContext@1329,file:Exec@2530,file:Statfs@2682 c6779927278f6143036a9339b5dbcb1e09c2e887a888430068e3583b1222cf99
@remove internal/instanceexec/probe_hardening.go boxAddrClosed
@removed internal/instanceexec/probe_hardening.go boxAddrClosed H3 census-ruling-same-box-edge-fence;basis=Probe (uid carlos). Its ReadFile of box-addrs moved into readBoxAddrs, which it now calls; it reads nothing itself. The verdict rule is unchanged: a UDP datagram to every listed address must get EPERM (fenced addresses get it from the fence's drop rule).
-@coverage . a1fa1b1540b62650911c9417af5653715b32588cfac5d405fab643e2d817e2ef
+@coverage . ecdd7dd5f446b4531b804cc225048411e771ed5caa952319af49b8581499a99c
@coverage internal/edgefence 115531fbda852d7ea66268f229f532f89c419f34b9d81670ec89012767389ee8
@coverage internal/host 4ecc37f7d8da95b326d41c2c1e427f6abd76d36f074be23d6617690cdf55f7b6
@coverage internal/host/units 16617c256a62a706c691f42c7bed9c05d3fdd56cc1b4abb938b9c39eff0fefd7
-@coverage internal/instancectl 6a3a0c941bcd8a31198d84130addfce11ec473158442dc21469ab6460075d665
+@coverage internal/instancectl e3308298508c556287cc104e871719f09f149ae6f5b71e2a0dd8bfe1e6be8bdf
@coverage internal/instanceexec c6071fb5114f8512ac922f4a9c19d0b282a6a22b37894f742425806a2c26aef1
-@coverage internal/status c3509b4c23030866200da6c597952006ddf7b97dd570017aeedb17f8b5058fdd
+@coverage internal/status 75501a65f2408dbf473e8f314769f3853aa966a93f4c3f44076d934b10a1e7e6
diff --git a/internal/status/snapshot.go b/internal/status/snapshot.go
index d9fae9a20..f81d61f59 100644
--- a/internal/status/snapshot.go
+++ b/internal/status/snapshot.go
@@ -310,8 +310,8 @@ type ExecSandbox struct {
Blocked string `json:"blocked,omitempty"`
}
-// EdgeFence is root's edge fence report (#990): State is "on", "off" or
-// "not_needed", and Reason says why when it is not on.
+// EdgeFence is root's edge fence report (#990): State is "on" or "off",
+// and Reason says why when it is off.
type EdgeFence struct {
State string `json:"state"`
Reason string `json:"reason,omitempty"`
diff --git a/status_instances.go b/status_instances.go
index 479d614c4..0e12a0e5c 100644
--- a/status_instances.go
+++ b/status_instances.go
@@ -363,7 +363,7 @@ type statusEdgeFence struct {
// printEdgeFenceStatus says, one line per box, whether instances there can
// call other apps on the same box by hostname (#990): on, off with why,
-// not needed with why, or unknown.
+// or unknown.
func printEdgeFenceStatus(w io.Writer, boxes []statusLayoutBox) {
sorted := append([]statusLayoutBox(nil), boxes...)
sort.Slice(sorted, func(i, j int) bool { return sorted[i].Box < sorted[j].Box })
@@ -376,8 +376,6 @@ func printEdgeFenceStatus(w io.Writer, boxes []statusLayoutBox) {
fmt.Fprintf(w, "%s same-box calls: unknown\n", b.Box)
case f.State == "on":
fmt.Fprintf(w, "%s same-box calls: on\n", b.Box)
- case f.State == "not_needed":
- fmt.Fprintf(w, "%s same-box calls: not needed (%s)\n", b.Box, f.Reason)
default:
fmt.Fprintf(w, "%s same-box calls: %s (%s)\n", b.Box, f.State, sandboxWord(f.Reason))
}
diff --git a/status_instances_test.go b/status_instances_test.go
index 7b0f4efcf..41d8ec10d 100644
--- a/status_instances_test.go
+++ b/status_instances_test.go
@@ -324,7 +324,7 @@ func TestStatusLayoutSandbox(t *testing.T) {
func TestStatusLayoutSameBoxCalls(t *testing.T) {
var boxes []statusLayoutBox
raw := `[{"box":"leo","edge_fence":{"state":"on"},"rows":[]},` +
- `{"box":"ec2","edge_fence":{"state":"not_needed","reason":"the public address is not on this box, so calls already go out and back"},"rows":[]},` +
+ `{"box":"ec2","edge_fence":{"state":"off","reason":"no public address on this box"},"rows":[]},` +
`{"box":"held","edge_fence":{"state":"off","reason":"turned off with CARLOS_EDGE_FENCE=off"},"rows":[]},` +
`{"box":"old","rows":[]},` +
`{"box":"stale","stale":true,"edge_fence":{"state":"on"},"rows":[]}]`
@@ -335,7 +335,7 @@ func TestStatusLayoutSameBoxCalls(t *testing.T) {
printStatusLayout(&out, boxes)
for _, want := range []string{
"leo same-box calls: on\n",
- "ec2 same-box calls: not needed (the public address is not on this box, so calls already go out and back)\n",
+ "ec2 same-box calls: off (no public address on this box)\n",
"held same-box calls: off (turned off with CARLOS_EDGE_FENCE=off)\n",
"old same-box calls: unknown\n",
"stale same-box calls: unknown (stale report)\n",