oficina / equipo Public

fix: bind the egress socket through its verified directory

codex agent, operated by paul@keymail.dev pushed by claude agent, operated by paul@keymail.dev f3ea07cc1ff53376ffcb1c915cd65f7948928e2c parent e06320a
4 files changed, +234 −4
  • docs/superpowers/plans/CONTROLLER-REPORT-plan-2b.md +138 −0
  • internal/runner/containment/egress_test.go +47 −0
  • internal/runner/egress/proxy.go +16 −4
  • internal/runner/egress/proxy_test.go +33 −0

CI failed — run details

diff --git a/docs/superpowers/plans/CONTROLLER-REPORT-plan-2b.md b/docs/superpowers/plans/CONTROLLER-REPORT-plan-2b.md
index 15282226..a4f4d1b2 100644
--- a/docs/superpowers/plans/CONTROLLER-REPORT-plan-2b.md
+++ b/docs/superpowers/plans/CONTROLLER-REPORT-plan-2b.md
@@ -4213,3 +4213,141 @@ No UI text, README prose or `copy.go` entries were added. The new internal diagn
No known failing implementation gate remains. Real provider compatibility, refresh hostnames and complete deployed-policy certification remain Task 11b release work; this run claims local fixture and namespace evidence only. The actual auth-host fields remain empty by default. The strict CONNECT parser permits only Host, User-Agent, Proxy-Connection and Connection headers; Task 11b should verify the pinned provider transports against that grammar without relaxing framing or destination checks. Full CI and controller review remain outside this task's requested gate list.
All fixture listeners and processes finished or were stopped. Scratch created for test evidence and the runner build is removed after copying it into both reports. No server, watcher, provider session, credential home or background command is left running. No other worktree was touched. The controller owns review and push.
+
+
+## Task 3 fix round 1: Bind the socket through its verified directory
+
+Status: DONE. Base: `e06320a` on `plan-2b`.
+Commit subject: `fix: bind the egress socket through its verified directory`.
+
+### What changed
+
+`egress.Open` now binds `/proc/self/fd/<fd>/proxy.sock` using the directory descriptor already checked by `openat2` and `fstat`. Socket address length no longer depends on ScratchRoot. The real Linux launch test confirms that this machine resolves the descriptor path during bind.
+
+The proxy uses the same descriptor path for chmod and retains the directory until the listener closes and unlinks the socket. Failed construction closes the descriptor too. This also fixes cleanup after a directory rename: the proxy removes its original socket and leaves a replacement directory untouched.
+
+`SocketPath()` still returns the real filesystem path. Containment continues to open that path with its existing symlink-rejecting `pin()` function and passes the retained socket descriptor to bubblewrap with `--ro-bind-fd`. No containment production code or mount rules changed.
+
+Files changed:
+
+- `internal/runner/egress/proxy.go`: descriptor-relative bind, chmod and cleanup lifetime.
+- `internal/runner/egress/proxy_test.go`: `TestEgressSocketDirectoryPinned` renames the socket directory, installs a replacement file, and verifies cleanup affects only the original directory.
+- `internal/runner/containment/egress_test.go`: `TestEgressLongScratchRoot` creates a deep subdirectory under the existing on-disk TMPDIR, asserts a root of at least 70 bytes, starts a full namespace launch, exchanges a CONNECT request and policy-denial response through the relay, checks the real socket path and mode, and verifies cleanup after exit.
+- `docs/superpowers/plans/CONTROLLER-REPORT-plan-2b.md`: this appended section.
+- `.superpowers/sdd/2026-11-03-equipo-plan-2b-direct/task-3-report.md`: the same appended evidence, retained locally under the repository's existing ignore rule.
+
+### Interpretations
+
+This fix supersedes original-report interpretation 1, which required callers to keep Unix socket paths short. Descriptor-relative bind works on this machine, so no constructor length rejection or new error string is needed.
+
+The full launch uses an owned Python fixture inside the production namespace helper. Its request to `localhost:443` receives the exact `403 Forbidden` response from the host proxy through the namespace relay. It does not resolve, connect to or prompt a provider. The existing TLS fixture and destination-policy tests also pass in the covering command.
+
+The task's explicit rules override generic writing and handoff workflows: no copy-review skill/server, delegation, model call or `.claude/HANDOFF.md`. All commands ran in the specified worktree, with default Go caches and `CGO_ENABLED=0` for Go checks. Absolute package paths below select the same packages as the requested covering commands.
+
+### RED before implementation
+
+Both regression tests were written before changing production code. This command exited 1 with compiled test failures:
+
+```sh
+CGO_ENABLED=0 go test /home/paulca/amadan.net/oficina/equipo-plan-2b/internal/runner/egress /home/paulca/amadan.net/oficina/equipo-plan-2b/internal/runner/containment -run 'TestEgressLongScratchRoot|TestEgressSocketDirectoryPinned' -count=1
+```
+
+```text
+--- FAIL: TestEgressSocketDirectoryPinned (0.00s)
+ proxy_test.go:425: socket survived close in verified directory: <nil>
+ proxy_test.go:428: close touched replacement directory: "" open /home/paulca/t/e2b/e3-1942956935/private/proxy.sock: no such file or directory
+FAIL amadan.net/oficina/equipo/internal/runner/egress 0.003s
+--- FAIL: TestEgressLongScratchRoot (0.00s)
+ egress_test.go:109: namespace capability/start failed (userns, pidfd required): listen unix /home/paulca/t/e2b/e3-1724877323/s/deepdeepdeepdeepdeepdeepdeepdeepdeepdeepdeepdeepdeepdeepdeepdeepdeepdeepdeepdeep/state/attempt-e4c7c01b4fa5d537e515509ec81955cc/egress/proxy.sock: bind: invalid argument
+FAIL amadan.net/oficina/equipo/internal/runner/containment 0.004s
+```
+
+### GREEN before mutations
+
+The same focused command then exited 0:
+
+```text
+ok amadan.net/oficina/equipo/internal/runner/egress 0.002s
+ok amadan.net/oficina/equipo/internal/runner/containment 0.060s
+```
+
+The covering command and namespace gate also passed before mutations:
+
+```sh
+CGO_ENABLED=0 go test /home/paulca/amadan.net/oficina/equipo-plan-2b/internal/runner/egress /home/paulca/amadan.net/oficina/equipo-plan-2b/internal/runner/containment -run 'TestEgress|TestNamespaceHidesHostControl' -count=1
+make -C /home/paulca/amadan.net/oficina/equipo-plan-2b runner-namespace-test
+```
+
+```text
+ok amadan.net/oficina/equipo/internal/runner/egress 3.269s
+ok amadan.net/oficina/equipo/internal/runner/containment 0.456s
+CGO_ENABLED=0 go test ./internal/runner/containment -run '^TestNamespace' -count=1
+ok amadan.net/oficina/equipo/internal/runner/containment 3.205s
+```
+
+### Independent mutations
+
+Each mutation ran alone against the green source and was restored in a Python `finally` block before the next one. All three compiled and exited 1 with the named test failure.
+
+1. Replace `net.Listen("unix", bindPath)` with `net.Listen("unix", path)`.
+
+```sh
+CGO_ENABLED=0 go test /home/paulca/amadan.net/oficina/equipo-plan-2b/internal/runner/containment -run '^TestEgressLongScratchRoot$' -count=1
+```
+
+```text
+--- FAIL: TestEgressLongScratchRoot (0.00s)
+ egress_test.go:109: namespace capability/start failed (userns, pidfd required): listen unix /home/paulca/t/e2b/e3-522231684/s/deepdeepdeepdeepdeepdeepdeepdeepdeepdeepdeepdeepdeepdeepdeepdeepdeepdeepdeepdeep/state/attempt-92cfe44ad327dd4bee20cdab87708242/egress/proxy.sock: bind: invalid argument
+FAIL amadan.net/oficina/equipo/internal/runner/containment 0.004s
+```
+
+2. Insert `directory.Close()` immediately before `directory = nil` in `open`, ending the retained descriptor's lifetime before listener cleanup.
+
+```sh
+CGO_ENABLED=0 go test /home/paulca/amadan.net/oficina/equipo-plan-2b/internal/runner/egress -run '^TestEgressSocketDirectoryPinned$' -count=1
+```
+
+```text
+--- FAIL: TestEgressSocketDirectoryPinned (0.00s)
+ proxy_test.go:425: socket survived close in verified directory: <nil>
+FAIL amadan.net/oficina/equipo/internal/runner/egress 0.003s
+```
+
+3. Change `SocketPath()` to return `p.listener.Addr().String()`, exposing the proc alias to containment instead of the real path. The existing mount pin rejects that magic link.
+
+```sh
+CGO_ENABLED=0 go test /home/paulca/amadan.net/oficina/equipo-plan-2b/internal/runner/containment -run '^TestEgressLongScratchRoot$' -count=1
+```
+
+```text
+--- FAIL: TestEgressLongScratchRoot (0.00s)
+ egress_test.go:109: namespace capability/start failed (userns, pidfd required): namespace_mount_rejected: too many levels of symbolic links
+FAIL amadan.net/oficina/equipo/internal/runner/containment 0.005s
+```
+
+### Final GREEN after restoration
+
+```sh
+CGO_ENABLED=0 go test /home/paulca/amadan.net/oficina/equipo-plan-2b/internal/runner/egress /home/paulca/amadan.net/oficina/equipo-plan-2b/internal/runner/containment -run 'TestEgress|TestNamespaceHidesHostControl' -count=1
+make -C /home/paulca/amadan.net/oficina/equipo-plan-2b runner-namespace-test
+CGO_ENABLED=0 go vet /home/paulca/amadan.net/oficina/equipo-plan-2b/internal/runner/egress /home/paulca/amadan.net/oficina/equipo-plan-2b/internal/runner/containment
+git -C /home/paulca/amadan.net/oficina/equipo-plan-2b diff --check
+gofmt -l /home/paulca/amadan.net/oficina/equipo-plan-2b/internal/runner/egress/proxy.go /home/paulca/amadan.net/oficina/equipo-plan-2b/internal/runner/egress/proxy_test.go /home/paulca/amadan.net/oficina/equipo-plan-2b/internal/runner/containment/egress_test.go
+```
+
+```text
+ok amadan.net/oficina/equipo/internal/runner/egress 3.260s
+ok amadan.net/oficina/equipo/internal/runner/containment 0.440s
+CGO_ENABLED=0 go test ./internal/runner/containment -run '^TestNamespace' -count=1
+ok amadan.net/oficina/equipo/internal/runner/containment 2.220s
+```
+
+Every command exited 0. Vet, diff checking and formatting produced no output. No full suite or `make ci` ran.
+
+### User-facing strings and concerns
+
+No new user-facing strings, protocol responses, diagnostics or `copy.go` entries. Only test assertion messages and one implementation comment were added.
+
+No new concerns or failing gates. Provider certification remains the existing Task 11b release gate. Tests closed their listeners, stopped their namespace processes and removed their owned TMPDIR trees. No separate scratch, background server or credential home was created. No push, merge, deployment or other worktree access occurred. The controller owns review and push.
+
+The final fixture reads the response through EOF so a split TCP read cannot cause a false failure. After that test-only adjustment, `CGO_ENABLED=0 go test /home/paulca/amadan.net/oficina/equipo-plan-2b/internal/runner/containment -run '^TestEgressLongScratchRoot$' -count=1` passed (`ok amadan.net/oficina/equipo/internal/runner/containment 0.061s`), and `git diff --check` remained clean.
diff --git a/internal/runner/containment/egress_test.go b/internal/runner/containment/egress_test.go
index 16915538..15752cb7 100644
--- a/internal/runner/containment/egress_test.go
+++ b/internal/runner/containment/egress_test.go
@@ -92,6 +92,53 @@ except OSError:
}
}
+func TestEgressLongScratchRoot(t *testing.T) {
+ m, l := egressFixture(t)
+ cfg := m.cfg
+ cfg.ScratchRoot = filepath.Join(cfg.ScratchRoot, strings.Repeat("deep", 20), "state")
+ if len(cfg.ScratchRoot) < 70 {
+ t.Fatal("fixture scratch root is too short")
+ }
+ if e := os.MkdirAll(cfg.ScratchRoot, 0700); e != nil {
+ t.Fatal(e)
+ }
+ m, e := New(cfg)
+ if e != nil {
+ t.Fatal(e)
+ }
+ p := launch(t, m, l, `import socket, sys
+s=socket.create_connection(('127.0.0.1',18080),2)
+s.sendall(b'CONNECT localhost:443 HTTP/1.1\r\nHost: localhost:443\r\n\r\n')
+with s.makefile('rb') as response:
+ assert response.read() == b'HTTP/1.1 403 Forbidden\r\n\r\n'
+s.close()
+print('relay-ready', flush=True)
+sys.stdin.read(1)
+`)
+ line, e := bufio.NewReader(p.Stdout()).ReadString('\n')
+ if e != nil || line != "relay-ready\n" {
+ t.Fatalf("long-root relay: %q %v", line, e)
+ }
+ path := p.(*process).egress.SocketPath()
+ if !strings.HasPrefix(path, cfg.ScratchRoot+"/") || len(path) <= 107 {
+ t.Fatalf("SocketPath must retain the real long path: %q", path)
+ }
+ info, e := os.Stat(path)
+ if e != nil || info.Mode()&os.ModeSocket == 0 || info.Mode().Perm() != 0600 {
+ t.Fatalf("real socket path: %v %v", info, e)
+ }
+ if _, e = p.Stdin().Write([]byte{'x'}); e != nil {
+ t.Fatal(e)
+ }
+ exit, e := p.Wait(context.Background())
+ if e != nil || exit.Code != 0 {
+ t.Fatalf("long-root launch: %+v %v", exit, e)
+ }
+ if _, e = os.Stat(path); !os.IsNotExist(e) {
+ t.Fatalf("long-root socket survived exit: %v", e)
+ }
+}
+
func TestEgressDiesWithAttempt(t *testing.T) {
for _, mode := range []string{"stop", "context", "proxy_failure", "exit"} {
t.Run(mode, func(t *testing.T) {
diff --git a/internal/runner/egress/proxy.go b/internal/runner/egress/proxy.go
index df2f11d9..a19c0f89 100644
--- a/internal/runner/egress/proxy.go
+++ b/internal/runner/egress/proxy.go
@@ -10,6 +10,7 @@ import (
"net/netip"
"os"
"path/filepath"
+ "strconv"
"strings"
"sync"
"time"
@@ -45,6 +46,7 @@ func defaults() dependencies {
type Proxy struct {
listener net.Listener
+ directory *os.File
path string
hosts map[string]bool
deps dependencies
@@ -78,7 +80,12 @@ func open(ctx context.Context, dir string, policy Policy, d dependencies) (*Prox
if e != nil {
return nil, e
}
- defer unix.Close(fd)
+ directory := os.NewFile(uintptr(fd), dir)
+ defer func() {
+ if directory != nil {
+ directory.Close()
+ }
+ }()
var st unix.Stat_t
if e = unix.Fstat(fd, &st); e != nil {
return nil, e
@@ -87,16 +94,20 @@ func open(ctx context.Context, dir string, policy Policy, d dependencies) (*Prox
return nil, errPolicy
}
path := filepath.Join(dir, "proxy.sock")
- ln, e := net.Listen("unix", path)
+ // Bind and chmod through the verified directory. Keep its descriptor until
+ // listener.Close unlinks the socket, even if the directory has been renamed.
+ bindPath := "/proc/self/fd/" + strconv.Itoa(fd) + "/proxy.sock"
+ ln, e := net.Listen("unix", bindPath)
if e != nil {
return nil, e
}
- if e = os.Chmod(path, 0600); e != nil {
+ if e = os.Chmod(bindPath, 0600); e != nil {
ln.Close()
return nil, e
}
ctx, cancel := context.WithCancel(ctx)
- p := &Proxy{listener: ln, path: path, hosts: map[string]bool{}, deps: d, ctx: ctx, cancel: cancel, done: make(chan struct{}), clients: map[net.Conn]struct{}{}, upstreams: map[net.Conn]struct{}{}}
+ p := &Proxy{listener: ln, directory: directory, path: path, hosts: map[string]bool{}, deps: d, ctx: ctx, cancel: cancel, done: make(chan struct{}), clients: map[net.Conn]struct{}{}, upstreams: map[net.Conn]struct{}{}}
+ directory = nil
for _, h := range policy.Hosts {
p.hosts[h] = true
}
@@ -121,6 +132,7 @@ func (p *Proxy) shutdown() {
p.closed = true
p.cancel()
p.listener.Close()
+ p.directory.Close()
for c := range p.clients {
c.Close()
}
diff --git a/internal/runner/egress/proxy_test.go b/internal/runner/egress/proxy_test.go
index 00881ade..d9118e5e 100644
--- a/internal/runner/egress/proxy_test.go
+++ b/internal/runner/egress/proxy_test.go
@@ -396,6 +396,39 @@ func TestEgressPrivateSocket(t *testing.T) {
}
}
+func TestEgressSocketDirectoryPinned(t *testing.T) {
+ root := scratch(t)
+ dir := filepath.Join(root, "private")
+ if e := os.Mkdir(dir, 0700); e != nil {
+ t.Fatal(e)
+ }
+ p, e := Open(context.Background(), dir, Policy{Provider: "claude", Hosts: []string{"api.anthropic.com"}})
+ if e != nil {
+ t.Fatal(e)
+ }
+ defer p.Close()
+ moved := filepath.Join(root, "moved")
+ if e = os.Rename(dir, moved); e != nil {
+ t.Fatal(e)
+ }
+ if e = os.Mkdir(dir, 0700); e != nil {
+ t.Fatal(e)
+ }
+ decoy := filepath.Join(dir, "proxy.sock")
+ if e = os.WriteFile(decoy, []byte("replacement"), 0600); e != nil {
+ t.Fatal(e)
+ }
+ if e = p.Close(); e != nil {
+ t.Fatal(e)
+ }
+ if _, e = os.Stat(filepath.Join(moved, "proxy.sock")); !os.IsNotExist(e) {
+ t.Errorf("socket survived close in verified directory: %v", e)
+ }
+ if b, e := os.ReadFile(decoy); e != nil || string(b) != "replacement" {
+ t.Fatalf("close touched replacement directory: %q %v", b, e)
+ }
+}
+
func TestEgressActiveTunnelClose(t *testing.T) {
for _, mode := range []string{"close", "cancel", "listener_failure"} {
t.Run(mode, func(t *testing.T) {