edge, host: round 4 (route scan never reaches the fleet socket, no root console, Linux-only success tests); copy pass 2
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
11 files changed,
+416
−139
docs/superpowers/notes/copy/hub-console-socket.md+22 −0docs/superpowers/specs/2026-11-24-hub-console-address-from-install-design.md+1 −0internal/edge/replicaconsole.go+47 −4internal/edge/replicaconsole_peer_linux.go+26 −0internal/edge/replicaconsole_peer_other.go+9 −0internal/edge/replicaconsole_scan_rootrunner_linux_test.go+77 −0internal/edge/replicaconsole_socket_linux_test.go+167 −0internal/edge/replicaconsole_socket_other_test.go+45 −0internal/edge/replicaconsole_socket_test.go+6 −130internal/host/consolefleet.go+7 −0internal/host/consolefleet_test.go+9 −5
diff --git a/docs/superpowers/notes/copy/hub-console-socket.md b/docs/superpowers/notes/copy/hub-console-socket.md| index eaed4fd09..6fb4c9cd2 100644 |
| --- a/docs/superpowers/notes/copy/hub-console-socket.md |
| +++ b/docs/superpowers/notes/copy/hub-console-socket.md |
| @@ -93,3 +93,25 @@ R10 `daemon-reload owed by an earlier pass failed: <error>` |
| Luna pass, 2026-11-24 (`codex exec -m gpt-5.6-luna -s read-only`, brief |
| `LUNA-COPY-BRIEF-hub-console-socket.md`): every row C1-C6 and R1-R10 |
| approved as drafted. Log lines L1-L6 are not copy and were not reviewed. |
| + |
| +## Strings added after the first pass (task reviews, rounds 1-4) |
| + |
| +Root's reasons (after `socket not ready: root's console fleet record is not exclusive: `, and in root's journal line): |
| +R11 `/run/carlos-console belongs to uid <n>, not console.service's user (uid <m>)` |
| +R12 `console.service sets DynamicUser=<v>, so its user cannot be checked` |
| +R13 `console.service runs as <user>, whose uid cannot be found: <error>` |
| +R14 `console.service runs as root, and a listener that started as root and dropped to another user still reads as root, so the socket's listener cannot be proved` |
| +R15 `checking /run/carlos-console: <error>` |
| + |
| +The edge's own reasons (after `socket not ready: `): |
| +E1 `root's console fleet record is not exclusive: <reason>` |
| +E2 `root's console fleet record: <error>` |
| +E3 `a change to console.service's drop-in is waiting for a daemon-reload` |
| +E4 `/run/carlos-console belongs to uid <n>, not the console's (uid <m>)` |
| +E5 `/run/carlos-console is not a directory only its owner can write (<mode>)` |
| +E6 `/run/carlos-console/fleet.sock is not a socket (<mode>)` |
| + |
| +Luna pass 2, 2026-11-24 (same brief): R11-R15, E1-E3 and E6 approved; E4 |
| +and E5 rewritten by Luna and applied verbatim: |
| +E4 `/run/carlos-console belongs to uid <n>, not the console user (uid <m>)` |
| +E5 `/run/carlos-console is not a directory that only its owner can write (<mode>)` |
diff --git a/docs/superpowers/specs/2026-11-24-hub-console-address-from-install-design.md b/docs/superpowers/specs/2026-11-24-hub-console-address-from-install-design.md| index d24df7194..7bef95dd5 100644 |
| --- a/docs/superpowers/specs/2026-11-24-hub-console-address-from-install-design.md |
| +++ b/docs/superpowers/specs/2026-11-24-hub-console-address-from-install-design.md |
| @@ -379,3 +379,4 @@ Round 2's P2s are applied in rev 3 (§15). |
| | Task 3 R1 | P2-2, P2-3 marker; trusted drop-in read | Edge refuses a record while the marker stands; drop-in read through rootstate | |
| | Task 3 R2 | P1 systemd re-chowns a directory holding a foreign listener (same family as R1: socket identity) | Stopping rule 2. **Paul chose cut 3, 2026-11-24: check the peer.** One dial function, `dialConsoleFleet` (`internal/edge/replicaconsole.go`), proves with `SO_PEERCRED` that the listener's uid is root's recorded console uid before a byte is written; no connection reuse, so every call is proved; regression test with a foreign listener under a re-chowned directory (`replicaconsole_peer_rootrunner_linux_test.go`) | |
| | Task 3 R2 | P2-2 symlinked drop-in not repaired; P2-3 inactive-console directory check dropped; P2-4 numeric `User=` | Applied (`readConsoleFleetDropInAt`, `exclusive`, `consoleUnitUID`) | |
| +| Task 3 R3 | P1-1 the kept route scan reaches the fleet socket around the peer check; P1-2 `SO_PEERCRED` is fixed at `listen()`, so a root console cannot be told from a listener that dropped privileges; P1-3 socket tests assume Linux | **Paul approved a fourth round, 2026-11-24, with these three fixes** (no fifth round): the route scan skips a socket route under `/run/carlos-console` and its socket dial refuses a listener bound there by the kernel's name for it (`newRouteScanSocketProxy`); root refuses `exclusive` for a console running as uid 0 (the flagship console runs as `carlos`); success tests Linux-only, refusal checks on other systems | |
diff --git a/internal/edge/replicaconsole.go b/internal/edge/replicaconsole.go| index a463ebc19..8d4a5e197 100644 |
| --- a/internal/edge/replicaconsole.go |
| +++ b/internal/edge/replicaconsole.go |
| @@ -95,6 +95,40 @@ func newReplicaConsoleSocketProxy(socket string) http.Handler { |
| }, true) |
| } |
| +// inConsoleFleetDir reports whether path names consolefleet.Dir or |
| +// anything in it. |
| +func inConsoleFleetDir(path string) bool { |
| + p := filepath.Clean(path) |
| + return p == consolefleet.Dir || strings.HasPrefix(p, consolefleet.Dir+"/") |
| +} |
| + |
| +// newRouteScanSocketProxy is the route scan's proxy for a console route on |
| +// a unix socket. Its dial refuses a connection whose listener is bound in |
| +// consolefleet.Dir, whatever path reached it (a symlink, a hard link): the |
| +// kernel's name for the listening end (getpeername) is where the listener |
| +// bound, not the path dialled. So the route scan can never be a way to the |
| +// console's fleet socket around dialConsoleFleet's peer check. No |
| +// connection is kept for reuse; box calls are few. |
| +func newRouteScanSocketProxy(socket string) http.Handler { |
| + d := &net.Dialer{Timeout: 5 * time.Second} |
| + return replicaConsoleProxy("console", func(ctx context.Context, _, _ string) (net.Conn, error) { |
| + c, err := d.DialContext(ctx, "unix", socket) |
| + if err != nil { |
| + return nil, err |
| + } |
| + bound, err := unixPeerPath(c) |
| + if err != nil { |
| + c.Close() |
| + return nil, fmt.Errorf("route scan socket %s: reading the listener's name: %w", socket, err) |
| + } |
| + if inConsoleFleetDir(bound) { |
| + c.Close() |
| + return nil, fmt.Errorf("route scan socket %s reaches the console's fleet socket (%s); box calls go there only with the listener's user proved", socket, bound) |
| + } |
| + return c, nil |
| + }, true) |
| +} |
| + |
| // consoleFleetUIDKey carries, in a box call's context, the console user |
| // root recorded when the edge chose the socket for it. |
| type consoleFleetUIDKey struct{} |
| @@ -255,13 +289,13 @@ func (c consoleFleetSource) ready() (int, error) { |
| return 0, err |
| } |
| if !fi.IsDir() || fi.Mode().Perm()&0o022 != 0 { |
| - return 0, fmt.Errorf("%s is not a directory only its owner can write (%v)", c.dir, fi.Mode()) |
| + return 0, fmt.Errorf("%s is not a directory that only its owner can write (%v)", c.dir, fi.Mode()) |
| } |
| // The directory must still be the console's: root recorded its user, |
| // and a process that held the directory before root looked would own |
| // it under another uid (task 3 review R1 P1). |
| if owner, ok := fileOwner(fi); !ok || owner != rec.ConsoleUID { |
| - return 0, fmt.Errorf("%s belongs to uid %d, not the console's (uid %d)", c.dir, owner, rec.ConsoleUID) |
| + return 0, fmt.Errorf("%s belongs to uid %d, not the console user (uid %d)", c.dir, owner, rec.ConsoleUID) |
| } |
| si, err := os.Lstat(c.socket) |
| if err != nil { |
| @@ -369,7 +403,16 @@ func (e *Edge) routeReplicaConsole(w http.ResponseWriter, r *http.Request) strin |
| if host != rt.Host || up.alias || up.regional != 0 || up.viaTunnel || rt.Account != "" || rt.App != "console" || rt.Unit != "console.service" || rt.ActivatorOwned() { |
| continue |
| } |
| - if rt.Socket == "" { |
| + proxy := up.proxy |
| + if rt.Socket != "" { |
| + // Never the console's fleet socket, by name or by what the |
| + // connection reaches (task 3 review R3 P1-1, Paul's round 4): a |
| + // box call goes there only through dialConsoleFleet's peer check. |
| + if inConsoleFleetDir(rt.Socket) { |
| + continue |
| + } |
| + proxy = newRouteScanSocketProxy(rt.Socket) |
| + } else { |
| addrs := rt.AddrList() |
| if len(addrs) == 0 { |
| continue |
| @@ -386,7 +429,7 @@ func (e *Edge) routeReplicaConsole(w http.ResponseWriter, r *http.Request) strin |
| UnavailableHeader(w, "replica-console-ambiguous", 2) |
| return ReplicaConsoleSourceRouteScan |
| } |
| - console = up.proxy |
| + console = proxy |
| } |
| if console == nil { |
| UnavailableHeader(w, "replica-console-missing", 2) |
diff --git a/internal/edge/replicaconsole_peer_linux.go b/internal/edge/replicaconsole_peer_linux.go| index 05bd73dab..971f550a9 100644 |
| --- a/internal/edge/replicaconsole_peer_linux.go |
| +++ b/internal/edge/replicaconsole_peer_linux.go |
| @@ -31,3 +31,29 @@ func unixPeerUID(c net.Conn) (int, error) { |
| } |
| return int(cred.Uid), nil |
| } |
| + |
| +// unixPeerPath is the name the listening end of a unix stream connection |
| +// bound to (getpeername), whatever path was dialled to reach it. |
| +func unixPeerPath(c net.Conn) (string, error) { |
| + uc, ok := c.(*net.UnixConn) |
| + if !ok { |
| + return "", errors.New("not a unix connection") |
| + } |
| + raw, err := uc.SyscallConn() |
| + if err != nil { |
| + return "", err |
| + } |
| + var sa unix.Sockaddr |
| + var saErr error |
| + if err := raw.Control(func(fd uintptr) { sa, saErr = unix.Getpeername(int(fd)) }); err != nil { |
| + return "", err |
| + } |
| + if saErr != nil { |
| + return "", saErr |
| + } |
| + u, ok := sa.(*unix.SockaddrUnix) |
| + if !ok { |
| + return "", errors.New("peer is not a unix address") |
| + } |
| + return u.Name, nil |
| +} |
diff --git a/internal/edge/replicaconsole_peer_other.go b/internal/edge/replicaconsole_peer_other.go| index 978417537..12090e47a 100644 |
| --- a/internal/edge/replicaconsole_peer_other.go |
| +++ b/internal/edge/replicaconsole_peer_other.go |
| @@ -12,3 +12,12 @@ import ( |
| func unixPeerUID(net.Conn) (int, error) { |
| return 0, errors.New("listener credentials are not available on this platform") |
| } |
| + |
| +// unixPeerPath: the listener's own name, as the platform reports it. |
| +func unixPeerPath(c net.Conn) (string, error) { |
| + a := c.RemoteAddr() |
| + if a == nil { |
| + return "", errors.New("no peer address") |
| + } |
| + return a.String(), nil |
| +} |
diff --git a/internal/edge/replicaconsole_scan_rootrunner_linux_test.go b/internal/edge/replicaconsole_scan_rootrunner_linux_test.go| new file mode 100644 |
| index 000000000..16b8a29be |
| --- /dev/null |
| +++ b/internal/edge/replicaconsole_scan_rootrunner_linux_test.go |
| @@ -0,0 +1,77 @@ |
| +//go:build linux && rootrunner |
| + |
| +package edge |
| + |
| +import ( |
| + "io" |
| + "net" |
| + "net/http" |
| + "net/http/httptest" |
| + "os" |
| + "path/filepath" |
| + "sync/atomic" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/consolefleet" |
| + "github.com/carlosframework/platform/internal/registry" |
| +) |
| + |
| +// TestRouteScanNeverReachesTheFleetSocket is task 3 review R3 P1-1: with |
| +// the socket not trusted (here root's record is not exclusive), an |
| +// accountless console route naming the fleet socket, directly or through |
| +// a symlink, must not reach it: the route scan skips the first and its |
| +// dial refuses the second by the listener's own name. Root only: it binds |
| +// in /run/carlos-console. |
| +func TestRouteScanNeverReachesTheFleetSocket(t *testing.T) { |
| + if os.Geteuid() != 0 { |
| + t.Skip("needs root") |
| + } |
| + if _, err := os.Lstat(consolefleet.Dir); err == nil { |
| + t.Skip(consolefleet.Dir + " exists on this machine") |
| + } |
| + if err := os.Mkdir(consolefleet.Dir, 0o755); err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(func() { os.RemoveAll(consolefleet.Dir) }) |
| + ln, err := net.Listen("unix", consolefleet.Socket) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + var hits atomic.Int32 |
| + srv := &http.Server{Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| + hits.Add(1) |
| + io.WriteString(w, "fleet") |
| + })} |
| + go srv.Serve(ln) |
| + t.Cleanup(func() { srv.Close() }) |
| + link := filepath.Join(t.TempDir(), "console.sock") |
| + if err := os.Symlink(consolefleet.Socket, link); err != nil { |
| + t.Fatal(err) |
| + } |
| + for name, socket := range map[string]string{"direct": consolefleet.Socket, "symlink": link} { |
| + t.Run(name, func(t *testing.T) { |
| + reg := openReg(t) |
| + if err := reg.Upsert(registry.Route{Host: "console.test", App: "console", Unit: "console.service", Kind: "instance", Socket: socket, Status: "ready"}); err != nil { |
| + t.Fatal(err) |
| + } |
| + e, err := New(reg, nil) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(e.probers.Stop) |
| + e.withConsoleFleet(consoleFleetSource{ |
| + record: func() (consolefleet.Record, error) { |
| + return consolefleet.Record{Schema: 1, Outcome: consolefleet.OutcomeUnchanged, Reason: "not exclusive"}, nil |
| + }, |
| + dir: consolefleet.Dir, socket: consolefleet.Socket, |
| + }) |
| + e.EnableTunnels(NewTunnelHub("tunnel.test", nil)) |
| + before := hits.Load() |
| + w := httptest.NewRecorder() |
| + e.ServeHTTP(w, newBoxCallRequest()) |
| + if hits.Load() != before || w.Code == http.StatusOK { |
| + t.Fatalf("%s: %d, fleet socket hits %d", name, w.Code, hits.Load()-before) |
| + } |
| + }) |
| + } |
| +} |
diff --git a/internal/edge/replicaconsole_socket_linux_test.go b/internal/edge/replicaconsole_socket_linux_test.go| new file mode 100644 |
| index 000000000..a3c1ddc55 |
| --- /dev/null |
| +++ b/internal/edge/replicaconsole_socket_linux_test.go |
| @@ -0,0 +1,167 @@ |
| +//go:build linux |
| + |
| +package edge |
| + |
| +import ( |
| + "io" |
| + "net" |
| + "net/http" |
| + "net/http/httptest" |
| + "os" |
| + "path/filepath" |
| + "strconv" |
| + "strings" |
| + "sync/atomic" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/registry" |
| +) |
| + |
| +// TestReplicaConsoleSocketSelected: with no operator endpoint, root's |
| +// exclusive record and the console's socket take the call, with the hub's |
| +// version header, and the route scan's console gets nothing. |
| +func TestReplicaConsoleSocketSelected(t *testing.T) { |
| + f := newFleetSocketFixture(t) |
| + e, routeHits := socketEdge(t, f) |
| + if e.LastReplicaConsoleCall() != nil { |
| + t.Fatal("a call recorded before any call") |
| + } |
| + w := postBoxCall(t, e) |
| + if w.Code != 200 || w.Body.String() != "socket" || f.hits.Load() != 1 || routeHits.Load() != 0 { |
| + t.Fatalf("%d %q socket %d route %d", w.Code, w.Body.String(), f.hits.Load(), routeHits.Load()) |
| + } |
| + if v := w.Header().Get("X-Carlos-Version"); v == "" || v == "route-version" { |
| + t.Fatalf("version header %q, want the hub's build", v) |
| + } |
| + if c := e.LastReplicaConsoleCall(); c == nil || c.Source != ReplicaConsoleSourceSocket || c.Result != "ok" || c.At.IsZero() { |
| + t.Fatalf("last call %+v", c) |
| + } |
| + if err := e.ConsoleFleetSocketReady(); err != nil { |
| + t.Fatalf("ready: %v", err) |
| + } |
| +} |
| + |
| +// TestReplicaConsoleSocketDialFailure: the socket chosen, then gone before |
| +// the dial (the console stopping): 502 with no version header, which the |
| +// box reads as the console unreachable, and no other upstream stands in. |
| +func TestReplicaConsoleSocketDialFailure(t *testing.T) { |
| + f := newFleetSocketFixture(t) |
| + e, routeHits := socketEdge(t, f) |
| + _ = f.srv.Close() // closes the listener; the socket file stays, refusing |
| + w := postBoxCall(t, e) |
| + if w.Code != http.StatusBadGateway || w.Header().Get("X-Carlos-Version") != "" || routeHits.Load() != 0 || f.hits.Load() != 0 { |
| + t.Fatalf("%d version %q route %d socket %d", w.Code, w.Header().Get("X-Carlos-Version"), routeHits.Load(), f.hits.Load()) |
| + } |
| + if c := e.LastReplicaConsoleCall(); c == nil || c.Source != ReplicaConsoleSourceSocket || c.Result != "502" { |
| + t.Fatalf("last call %+v", c) |
| + } |
| +} |
| + |
| +// TestReplicaConsoleSocketResults: the console's own refusals are recorded |
| +// as the status it answered. |
| +func TestReplicaConsoleSocketResults(t *testing.T) { |
| + f := newFleetSocketFixture(t) |
| + e, _ := socketEdge(t, f) |
| + for _, tc := range []struct { |
| + code int |
| + early bool |
| + }{{401, false}, {500, false}, {500, true}, {200, true}} { |
| + code := tc.code |
| + f.code.Store(int32(code)) |
| + f.early.Store(tc.early) |
| + // httptest's recorder keeps the first status written, 103 here, so |
| + // the final answer is checked through the last-call record below. |
| + if w := postBoxCall(t, e); !tc.early && w.Code != code { |
| + t.Fatalf("answered %d, want %d", w.Code, code) |
| + } |
| + want := strconv.Itoa(code) |
| + if code == 200 { |
| + want = "ok" |
| + } |
| + // An informational 103 before the final answer is not the result |
| + // (task 4 review R1 P2-2). |
| + if c := e.LastReplicaConsoleCall(); c == nil || c.Source != ReplicaConsoleSourceSocket || c.Result != want { |
| + t.Fatalf("early=%v: last call %+v, want %s", tc.early, c, want) |
| + } |
| + } |
| +} |
| + |
| +// TestReplicaConsoleSocketIgnoresRouteRows: a record writer's rows (an |
| +// account console.service row at another upstream) change nothing while |
| +// the socket is used. |
| +func TestReplicaConsoleSocketIgnoresRouteRows(t *testing.T) { |
| + f := newFleetSocketFixture(t) |
| + e, routeHits := socketEdge(t, f) |
| + var injectedHits atomic.Int32 |
| + injected := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { injectedHits.Add(1) })) |
| + defer injected.Close() |
| + if err := e.reg.Upsert(registry.Route{Host: "injected.test", Account: "x", App: "console", Unit: "console.service", Kind: "instance", Addr: injected.Listener.Addr().String(), Status: "ready"}); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := e.Reload(); err != nil { |
| + t.Fatal(err) |
| + } |
| + if w := postBoxCall(t, e); w.Body.String() != "socket" || injectedHits.Load() != 0 || routeHits.Load() != 0 { |
| + t.Fatalf("%q injected %d route %d", w.Body.String(), injectedHits.Load(), routeHits.Load()) |
| + } |
| +} |
| + |
| +// TestDialConsoleFleetRefusesAForeignListener: the one dial to the fleet |
| +// socket proves the listener's uid (SO_PEERCRED) before anything is |
| +// written; a listener under any other uid is closed on with not a byte |
| +// sent (Paul's cut 3, 2026-11-24). |
| +func TestDialConsoleFleetRefusesAForeignListener(t *testing.T) { |
| + dir, err := os.MkdirTemp("", "cfp") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(func() { os.RemoveAll(dir) }) |
| + sock := filepath.Join(dir, "s") |
| + ln, err := net.Listen("unix", sock) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + defer ln.Close() |
| + got := make(chan int, 4) |
| + go func() { |
| + for { |
| + c, err := ln.Accept() |
| + if err != nil { |
| + return |
| + } |
| + b, _ := io.ReadAll(c) |
| + c.Close() |
| + got <- len(b) |
| + } |
| + }() |
| + c, err := dialConsoleFleet(t.Context(), sock, os.Getuid()+1) |
| + if err == nil { |
| + c.Close() |
| + t.Fatal("a listener under another uid was accepted") |
| + } |
| + if !strings.Contains(err.Error(), "not the console's") { |
| + t.Fatalf("err = %v", err) |
| + } |
| + if n := <-got; n != 0 { |
| + t.Fatalf("%d bytes reached the foreign listener", n) |
| + } |
| + c, err = dialConsoleFleet(t.Context(), sock, os.Getuid()) |
| + if err != nil { |
| + t.Fatalf("own listener refused: %v", err) |
| + } |
| + c.Close() |
| +} |
| + |
| +func TestInConsoleFleetDir(t *testing.T) { |
| + for path, want := range map[string]bool{ |
| + "/run/carlos-console": true, |
| + "/run/carlos-console/fleet.sock": true, |
| + "/run/carlos-console/../carlos-console/x": true, |
| + "/run/carlos-consolex/fleet.sock": false, |
| + "/run/carlos/apps/console.test.sock": false, |
| + } { |
| + if got := inConsoleFleetDir(path); got != want { |
| + t.Errorf("%s: %v", path, got) |
| + } |
| + } |
| +} |
diff --git a/internal/edge/replicaconsole_socket_other_test.go b/internal/edge/replicaconsole_socket_other_test.go| new file mode 100644 |
| index 000000000..70135765a |
| --- /dev/null |
| +++ b/internal/edge/replicaconsole_socket_other_test.go |
| @@ -0,0 +1,45 @@ |
| +//go:build !linux |
| + |
| +package edge |
| + |
| +import ( |
| + "io" |
| + "net" |
| + "os" |
| + "path/filepath" |
| + "testing" |
| +) |
| + |
| +// TestDialConsoleFleetRefusesOffLinux: without SO_PEERCRED no listener is |
| +// proved, so the dial refuses even the edge's own listener and sends |
| +// nothing. |
| +func TestDialConsoleFleetRefusesOffLinux(t *testing.T) { |
| + dir, err := os.MkdirTemp("", "cfp") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(func() { os.RemoveAll(dir) }) |
| + sock := filepath.Join(dir, "s") |
| + ln, err := net.Listen("unix", sock) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + defer ln.Close() |
| + got := make(chan int, 1) |
| + go func() { |
| + c, err := ln.Accept() |
| + if err != nil { |
| + return |
| + } |
| + b, _ := io.ReadAll(c) |
| + c.Close() |
| + got <- len(b) |
| + }() |
| + if c, err := dialConsoleFleet(t.Context(), sock, os.Getuid()); err == nil { |
| + c.Close() |
| + t.Fatal("a listener was accepted with no way to prove its user") |
| + } |
| + if n := <-got; n != 0 { |
| + t.Fatalf("%d bytes reached the listener", n) |
| + } |
| +} |
diff --git a/internal/edge/replicaconsole_socket_test.go b/internal/edge/replicaconsole_socket_test.go| index 62fe807e6..ac5208651 100644 |
| --- a/internal/edge/replicaconsole_socket_test.go |
| +++ b/internal/edge/replicaconsole_socket_test.go |
| @@ -7,7 +7,6 @@ import ( |
| "net/http/httptest" |
| "os" |
| "path/filepath" |
| - "strconv" |
| "strings" |
| "sync/atomic" |
| "testing" |
| @@ -111,29 +110,6 @@ func postBoxCall(t *testing.T, e *Edge) *httptest.ResponseRecorder { |
| return w |
| } |
| -// TestReplicaConsoleSocketSelected: with no operator endpoint, root's |
| -// exclusive record and the console's socket take the call, with the hub's |
| -// version header, and the route scan's console gets nothing. |
| -func TestReplicaConsoleSocketSelected(t *testing.T) { |
| - f := newFleetSocketFixture(t) |
| - e, routeHits := socketEdge(t, f) |
| - if e.LastReplicaConsoleCall() != nil { |
| - t.Fatal("a call recorded before any call") |
| - } |
| - w := postBoxCall(t, e) |
| - if w.Code != 200 || w.Body.String() != "socket" || f.hits.Load() != 1 || routeHits.Load() != 0 { |
| - t.Fatalf("%d %q socket %d route %d", w.Code, w.Body.String(), f.hits.Load(), routeHits.Load()) |
| - } |
| - if v := w.Header().Get("X-Carlos-Version"); v == "" || v == "route-version" { |
| - t.Fatalf("version header %q, want the hub's build", v) |
| - } |
| - if c := e.LastReplicaConsoleCall(); c == nil || c.Source != ReplicaConsoleSourceSocket || c.Result != "ok" || c.At.IsZero() { |
| - t.Fatalf("last call %+v", c) |
| - } |
| - if err := e.ConsoleFleetSocketReady(); err != nil { |
| - t.Fatalf("ready: %v", err) |
| - } |
| -} |
| // TestReplicaConsoleSocketRefusals: every reason the socket is not used |
| // falls to today's route scan, and the socket gets nothing. |
| @@ -201,50 +177,7 @@ func TestReplicaConsoleSocketRefusals(t *testing.T) { |
| } |
| } |
| -// TestReplicaConsoleSocketDialFailure: the socket chosen, then gone before |
| -// the dial (the console stopping): 502 with no version header, which the |
| -// box reads as the console unreachable, and no other upstream stands in. |
| -func TestReplicaConsoleSocketDialFailure(t *testing.T) { |
| - f := newFleetSocketFixture(t) |
| - e, routeHits := socketEdge(t, f) |
| - _ = f.srv.Close() // closes the listener; the socket file stays, refusing |
| - w := postBoxCall(t, e) |
| - if w.Code != http.StatusBadGateway || w.Header().Get("X-Carlos-Version") != "" || routeHits.Load() != 0 || f.hits.Load() != 0 { |
| - t.Fatalf("%d version %q route %d socket %d", w.Code, w.Header().Get("X-Carlos-Version"), routeHits.Load(), f.hits.Load()) |
| - } |
| - if c := e.LastReplicaConsoleCall(); c == nil || c.Source != ReplicaConsoleSourceSocket || c.Result != "502" { |
| - t.Fatalf("last call %+v", c) |
| - } |
| -} |
| -// TestReplicaConsoleSocketResults: the console's own refusals are recorded |
| -// as the status it answered. |
| -func TestReplicaConsoleSocketResults(t *testing.T) { |
| - f := newFleetSocketFixture(t) |
| - e, _ := socketEdge(t, f) |
| - for _, tc := range []struct { |
| - code int |
| - early bool |
| - }{{401, false}, {500, false}, {500, true}, {200, true}} { |
| - code := tc.code |
| - f.code.Store(int32(code)) |
| - f.early.Store(tc.early) |
| - // httptest's recorder keeps the first status written, 103 here, so |
| - // the final answer is checked through the last-call record below. |
| - if w := postBoxCall(t, e); !tc.early && w.Code != code { |
| - t.Fatalf("answered %d, want %d", w.Code, code) |
| - } |
| - want := strconv.Itoa(code) |
| - if code == 200 { |
| - want = "ok" |
| - } |
| - // An informational 103 before the final answer is not the result |
| - // (task 4 review R1 P2-2). |
| - if c := e.LastReplicaConsoleCall(); c == nil || c.Source != ReplicaConsoleSourceSocket || c.Result != want { |
| - t.Fatalf("early=%v: last call %+v, want %s", tc.early, c, want) |
| - } |
| - } |
| -} |
| // TestReplicaConsoleEnvWinsOverSocket: the operator's endpoint takes the |
| // call even with the socket ready, and is recorded as env; a socket that is |
| @@ -278,25 +211,6 @@ func TestReplicaConsoleEnvWinsOverSocket(t *testing.T) { |
| } |
| } |
| -// TestReplicaConsoleSocketIgnoresRouteRows: a record writer's rows (an |
| -// account console.service row at another upstream) change nothing while |
| -// the socket is used. |
| -func TestReplicaConsoleSocketIgnoresRouteRows(t *testing.T) { |
| - f := newFleetSocketFixture(t) |
| - e, routeHits := socketEdge(t, f) |
| - var injectedHits atomic.Int32 |
| - injected := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { injectedHits.Add(1) })) |
| - defer injected.Close() |
| - if err := e.reg.Upsert(registry.Route{Host: "injected.test", Account: "x", App: "console", Unit: "console.service", Kind: "instance", Addr: injected.Listener.Addr().String(), Status: "ready"}); err != nil { |
| - t.Fatal(err) |
| - } |
| - if err := e.Reload(); err != nil { |
| - t.Fatal(err) |
| - } |
| - if w := postBoxCall(t, e); w.Body.String() != "socket" || injectedHits.Load() != 0 || routeHits.Load() != 0 { |
| - t.Fatalf("%q injected %d route %d", w.Body.String(), injectedHits.Load(), routeHits.Load()) |
| - } |
| -} |
| // TestReplicaConsoleProductionRecordReader: the edge's production reader is |
| // rootstate's trusted public store, so a record in a directory anyone but |
| @@ -353,48 +267,10 @@ func TestReplicaCallStatusUnwraps(t *testing.T) { |
| } |
| } |
| -// TestDialConsoleFleetRefusesAForeignListener: the one dial to the fleet |
| -// socket proves the listener's uid (SO_PEERCRED) before anything is |
| -// written; a listener under any other uid is closed on with not a byte |
| -// sent (Paul's cut 3, 2026-11-24). |
| -func TestDialConsoleFleetRefusesAForeignListener(t *testing.T) { |
| - dir, err := os.MkdirTemp("", "cfp") |
| - if err != nil { |
| - t.Fatal(err) |
| - } |
| - t.Cleanup(func() { os.RemoveAll(dir) }) |
| - sock := filepath.Join(dir, "s") |
| - ln, err := net.Listen("unix", sock) |
| - if err != nil { |
| - t.Fatal(err) |
| - } |
| - defer ln.Close() |
| - got := make(chan int, 4) |
| - go func() { |
| - for { |
| - c, err := ln.Accept() |
| - if err != nil { |
| - return |
| - } |
| - b, _ := io.ReadAll(c) |
| - c.Close() |
| - got <- len(b) |
| - } |
| - }() |
| - c, err := dialConsoleFleet(t.Context(), sock, os.Getuid()+1) |
| - if err == nil { |
| - c.Close() |
| - t.Fatal("a listener under another uid was accepted") |
| - } |
| - if !strings.Contains(err.Error(), "not the console's") { |
| - t.Fatalf("err = %v", err) |
| - } |
| - if n := <-got; n != 0 { |
| - t.Fatalf("%d bytes reached the foreign listener", n) |
| - } |
| - c, err = dialConsoleFleet(t.Context(), sock, os.Getuid()) |
| - if err != nil { |
| - t.Fatalf("own listener refused: %v", err) |
| - } |
| - c.Close() |
| + |
| +// newBoxCallRequest is postBoxCall's request. |
| +func newBoxCallRequest() *http.Request { |
| + req := httptest.NewRequest("POST", "https://tunnel.test/api/fleet/replicas/a/app/h/coordinator", strings.NewReader(`{"op":"acquire"}`)) |
| + req.Header.Set("Authorization", "Bearer fleet-token") |
| + return req |
| } |
diff --git a/internal/host/consolefleet.go b/internal/host/consolefleet.go| index 37a75bdcd..810817595 100644 |
| --- a/internal/host/consolefleet.go |
| +++ b/internal/host/consolefleet.go |
| @@ -212,6 +212,13 @@ func (s consoleFleetStep) exclusive(p consoleUnitProps) (bool, int, string) { |
| if err != nil { |
| return no("%s runs as %s, whose uid cannot be found: %v", consolefleet.Unit, p.User, err) |
| } |
| + // The edge proves the listener's uid with SO_PEERCRED, which the kernel |
| + // fixes at listen(). Only root can change its uid after that, so a |
| + // non-root console uid cannot be worn by a listener that started as |
| + // anyone else; uid 0 can (task 3 review R3 P1-2, Paul's round 4). |
| + if uid == 0 { |
| + return no("%s runs as root, and a listener that started as root and dropped to another user still reads as root, so the socket's listener cannot be proved", consolefleet.Unit) |
| + } |
| owner, exists, err := s.runDirOwner() |
| if err != nil { |
| return no("checking %s: %v", consolefleet.Dir, err) |
diff --git a/internal/host/consolefleet_test.go b/internal/host/consolefleet_test.go| index 76cb86dcd..e27f47c3a 100644 |
| --- a/internal/host/consolefleet_test.go |
| +++ b/internal/host/consolefleet_test.go |
| @@ -204,11 +204,15 @@ func TestConsoleFleetNotExclusive(t *testing.T) { |
| if rec := f.step().run(); !rec.Exclusive || rec.ConsoleUID != consoleUID { |
| t.Fatalf("running console's own directory: %+v", rec) |
| } |
| - // A console with no User= runs as root. |
| - f.props.User = "" |
| - f.dirOwner = 0 |
| - if rec := f.step().run(); !rec.Exclusive || rec.ConsoleUID != 0 { |
| - t.Fatalf("root console: %+v", rec) |
| + // A console running as root is never trusted with the socket: a |
| + // listener that started as root and dropped to another user still reads |
| + // as root (task 3 review R3 P1-2, Paul's round 4). No User=, or User=0. |
| + for _, user := range []string{"", "0"} { |
| + f.props.User = user |
| + f.dirOwner = 0 |
| + if rec := f.step().run(); rec.Exclusive || !strings.Contains(rec.Reason, "runs as root") { |
| + t.Fatalf("root console (User=%q): %+v", user, rec) |
| + } |
| } |
| } |