egress: root checks the host shape before the layout path and refuses a symlinked registry (census hardening)
boxWhollyNested refuses a row whose Host is not a host name (provision.ValidHostShape) before Classify builds <dataDir>/layout/<host>.json from it, with no filesystem access and a reason that names neither the host nor a path. registryWritableByOthers reads each path with Lstat and refuses a symbolic link outright, so root never reports another file's mode bits to the agent.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+118
−1
internal/host/egress_rail.go+9 −1internal/host/egress_rail_linux_test.go+62 −0internal/host/egress_source.go+9 −0internal/host/egress_source_test.go+38 −0
diff --git a/internal/host/egress_rail.go b/internal/host/egress_rail.go| index 154e4c39f..f4b7c325b 100644 |
| --- a/internal/host/egress_rail.go |
| +++ b/internal/host/egress_rail.go |
| @@ -198,16 +198,24 @@ func cachedLookup(lookup func(string) (string, bool, error)) func(string) (strin |
| // journal files that exist, or its directory being group- or |
| // world-writable: a user other than the agent could then write the rows. |
| // A fresh bootstrap makes /data 0750 and registry.db 0640. |
| +// |
| +// Each path is read with Lstat and a symbolic link is refused outright |
| +// (census hardening): the journal files may be absent, so the agent could |
| +// create one as a link, and following it would report another file's mode |
| +// bits back to the agent. |
| func registryWritableByOthers(dataDir string) (string, error) { |
| base := filepath.Join(dataDir, "registry.db") |
| for _, p := range []string{dataDir, base, base + "-wal", base + "-shm", base + "-journal"} { |
| - fi, err := os.Stat(p) |
| + fi, err := os.Lstat(p) |
| if err != nil { |
| if p != dataDir && p != base && errors.Is(err, fs.ErrNotExist) { |
| continue |
| } |
| return "", err |
| } |
| + if fi.Mode()&fs.ModeSymlink != 0 { |
| + return fmt.Sprintf("%s is a symbolic link", p), nil |
| + } |
| if perm := fi.Mode().Perm(); perm&0o022 != 0 { |
| return fmt.Sprintf("%s is writable by users other than the agent (mode %04o)", p, perm), nil |
| } |
diff --git a/internal/host/egress_rail_linux_test.go b/internal/host/egress_rail_linux_test.go| index fcd47cacd..9d3f43cc3 100644 |
| --- a/internal/host/egress_rail_linux_test.go |
| +++ b/internal/host/egress_rail_linux_test.go |
| @@ -258,3 +258,65 @@ func TestRegistryEgressSourceFlatProcessWithoutARow(t *testing.T) { |
| t.Fatalf("a tenant unit of the agent's user with no row: %+v", got) |
| } |
| } |
| + |
| +// TestRegistryWritableByOthersRefusesASymlink: the journal files may be |
| +// absent, so the agent could create one as a link; the rail reads each |
| +// path with Lstat and refuses a link outright rather than reporting the |
| +// target's mode bits. |
| +func TestRegistryWritableByOthersRefusesASymlink(t *testing.T) { |
| + t.Parallel() |
| + for _, name := range []string{"registry.db", "registry.db-wal", "registry.db-shm", "registry.db-journal"} { |
| + t.Run(name, func(t *testing.T) { |
| + t.Parallel() |
| + dd := filepath.Join(t.TempDir(), "data") |
| + if err := os.Mkdir(dd, 0o750); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.Chmod(dd, 0o750); err != nil { |
| + t.Fatal(err) |
| + } |
| + if name != "registry.db" { |
| + if err := os.WriteFile(filepath.Join(dd, "registry.db"), nil, 0o640); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.Chmod(filepath.Join(dd, "registry.db"), 0o640); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + // A private target: following the link would pass. |
| + target := filepath.Join(t.TempDir(), "target") |
| + if err := os.WriteFile(target, nil, 0o600); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.Symlink(target, filepath.Join(dd, name)); err != nil { |
| + t.Fatal(err) |
| + } |
| + reason, err := registryWritableByOthers(dd) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if !strings.Contains(reason, name+" is a symbolic link") { |
| + t.Fatalf("reason %q, want the link refused", reason) |
| + } |
| + }) |
| + } |
| + t.Run("private files pass", func(t *testing.T) { |
| + t.Parallel() |
| + dd := filepath.Join(t.TempDir(), "data") |
| + if err := os.Mkdir(dd, 0o750); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.Chmod(dd, 0o750); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.WriteFile(filepath.Join(dd, "registry.db"), nil, 0o640); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.Chmod(filepath.Join(dd, "registry.db"), 0o640); err != nil { |
| + t.Fatal(err) |
| + } |
| + if reason, err := registryWritableByOthers(dd); err != nil || reason != "" { |
| + t.Fatalf("registryWritableByOthers = %q, %v; want a pass", reason, err) |
| + } |
| + }) |
| +} |
diff --git a/internal/host/egress_source.go b/internal/host/egress_source.go| index 8ac53a6d7..4b696b967 100644 |
| --- a/internal/host/egress_source.go |
| +++ b/internal/host/egress_source.go |
| @@ -29,6 +29,7 @@ import ( |
| "github.com/carlosframework/platform/internal/activator" |
| "github.com/carlosframework/platform/internal/instancectl" |
| "github.com/carlosframework/platform/internal/layout" |
| + "github.com/carlosframework/platform/internal/provision" |
| "github.com/carlosframework/platform/internal/registry" |
| ) |
| @@ -61,6 +62,14 @@ func boxWhollyNested(rows []registry.Route, dataDir string, unitAsAgent func(uni |
| Journal: func(host string) bool { return layout.JournalPresent(dataDir, host) }, |
| } |
| for _, rt := range rows { |
| + // The registry is the agent's file and does not validate Host, and |
| + // Classify's journal check builds <dataDir>/layout/<host>.json from |
| + // it: a host that is not a host name is refused first, with no |
| + // filesystem access, and the reason names neither it nor a path |
| + // built from it (census hardening). |
| + if !provision.ValidHostShape(rt.Host) { |
| + return false, "a row's host is not a valid host name" |
| + } |
| if rt.IsSidecar() { |
| return false, rt.Host + " is a sidecar member, which runs flat" |
| } |
diff --git a/internal/host/egress_source_test.go b/internal/host/egress_source_test.go| index 6c98a45a3..6c21baeba 100644 |
| --- a/internal/host/egress_source_test.go |
| +++ b/internal/host/egress_source_test.go |
| @@ -122,6 +122,44 @@ func TestBoxWhollyNestedTable(t *testing.T) { |
| } |
| } |
| +// TestBoxWhollyNestedRefusesAnInvalidHost: the registry is the agent's |
| +// file and does not validate Host, and the journal check builds |
| +// <dataDir>/layout/<host>.json from it. A host that is not a host name is |
| +// refused by its shape before any filesystem access: a file planted where |
| +// the traversal lands would otherwise fence the row with a reason naming |
| +// that path, an existence oracle for root. The reason names neither the |
| +// host nor a path. |
| +func TestBoxWhollyNestedRefusesAnInvalidHost(t *testing.T) { |
| + t.Parallel() |
| + dd := t.TempDir() |
| + if err := os.MkdirAll(filepath.Join(dd, "layout"), 0o755); err != nil { |
| + t.Fatal(err) |
| + } |
| + // "../planted" lands on <dd>/planted.json, which exists. |
| + if err := os.WriteFile(filepath.Join(dd, "planted.json"), []byte("{}"), 0o644); err != nil { |
| + t.Fatal(err) |
| + } |
| + unitAsAgent := func(string) (bool, error) { |
| + t.Fatal("a row with an invalid host reached the unit reader") |
| + return false, nil |
| + } |
| + for _, host := range []string{"../planted", "../../etc/x", "a/b.example.com", "", "UPPER.example.com"} { |
| + bad := nestedRow(dd, "shop.example.com") |
| + bad.Host = host |
| + bad.Unit = "never-read.service" |
| + ok, reason := boxWhollyNested([]registry.Route{nestedRow(dd, "shop-a.example.com"), bad}, dd, unitAsAgent) |
| + if ok { |
| + t.Fatalf("host %q: boxWhollyNested passed", host) |
| + } |
| + if reason != "a row's host is not a valid host name" { |
| + t.Fatalf("host %q: reason %q, want the shape refusal", host, reason) |
| + } |
| + if strings.Contains(reason, dd) || (host != "" && strings.Contains(reason, host)) { |
| + t.Fatalf("host %q: reason %q echoes the host or a path", host, reason) |
| + } |
| + } |
| +} |
| + |
| // egressRegistry is a temp registry in its own data dir (the package's |
| // testRegistry: <dataDir>/registry.db) holding rows, with policy columns |
| // set by SetPolicy, the agent's writer. |