carlos / platform Public

activator: flat env reads refuse a symlink or a FIFO at the name

Hunt H2-M5. readEnvFile was a plain os.ReadFile: a flat tenant, which
runs as the agent's uid and can write the data dir, could point
<host>.env or <host>.config.env at another file, or plant a FIFO there
and park the wake. It now reads through readOwnEnv, as the admin token's
append already did: no symlink, regular files only, opened non-blocking,
capped at 1 MiB. That covers the flat env layers, the control socket's
token reader and the sidecar env. Witness: hunt_H2_M5_test.go.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev d2d3de1b5456a771517da6bc4a949cd24c3fef75 parent 8eeeed1
2 files changed, +138 −4
  • internal/activator/backend_exec.go +15 −4
  • internal/activator/hunt_H2_M5_test.go +123 −0
diff --git a/internal/activator/backend_exec.go b/internal/activator/backend_exec.go
index 9a38476c0..22f48a128 100644
--- a/internal/activator/backend_exec.go
+++ b/internal/activator/backend_exec.go
@@ -3484,8 +3484,10 @@ func (b *ExecBackend) instanceEnv(rt registry.Route) (map[string]string, error)
func (b *ExecBackend) instanceEnvParts(rt registry.Route) (cfgEnv, ownEnv map[string]string, err error) {
paths := b.pathsFor(rt)
// A nested host's files live under a tenant-writable directory (design
- // §3.2): read them without following a symlink. A flat host's files sit
- // outside any tenant's reach, so the plain read stays exactly as it was.
+ // §3.2): read them without following a symlink at any component. A
+ // flat host's files sit in the data dir, which every flat tenant can
+ // write as the agent's own uid, so readEnvFile refuses a link or a
+ // FIFO at the name too (hunt H2-M5).
read := readEnvFile
if paths.layout == LayoutNested {
read = func(path string) (map[string]string, error) {
@@ -4191,9 +4193,18 @@ func nextHealthPoll(d time.Duration) time.Duration {
}
// readEnvFile parses a KEY=value EnvironmentFile (blank lines and #comments
-// ignored).
+// ignored). Every caller reads a flat row's file in the data dir, where a
+// flat tenant runs as the agent's own uid and can plant anything at the
+// name (hunt H2-M5). So the read goes through readOwnEnv, as the admin
+// token's append does: a symlink is refused, not followed; a FIFO or a
+// device is refused at once, not waited on; and the read is capped. That
+// is what instancefs does for a nested row. A missing file is still the
+// open's own error, so os.IsNotExist answers for it.
func readEnvFile(path string) (map[string]string, error) {
- raw, err := os.ReadFile(path)
+ raw, err := readOwnEnv(path)
+ if errors.Is(err, errOwnEnvSymlink) || errors.Is(err, errOwnEnvNotRegular) || errors.Is(err, errOwnEnvTooLarge) {
+ return nil, fmt.Errorf("%s: %w", path, err)
+ }
if err != nil {
return nil, err
}
diff --git a/internal/activator/hunt_H2_M5_test.go b/internal/activator/hunt_H2_M5_test.go
new file mode 100644
index 000000000..a4e6add2c
--- /dev/null
+++ b/internal/activator/hunt_H2_M5_test.go
@@ -0,0 +1,123 @@
+//go:build !windows
+
+package activator
+
+import (
+ "os"
+ "path/filepath"
+ "syscall"
+ "testing"
+ "time"
+
+ "github.com/carlosframework/platform/internal/provision"
+ "github.com/carlosframework/platform/internal/registry"
+)
+
+// Hunt H2-M5 (verifier: fable). instanceEnvParts picks its reader by
+// layout: nested rows read through instancefs (O_NOFOLLOW, O_NONBLOCK,
+// regular files only), flat rows through readEnvFile, which is a plain
+// os.ReadFile (internal/activator/backend_exec.go:2721-2740, :3305-3311).
+// The secure expectation asserted here: a FIFO at a flat row's env path is
+// refused promptly, as the nested reader refuses it. On current code the
+// flat read blocks on the FIFO open, so the test fails on its timeout.
+func TestHunt_H2_M5_FlatEnvReadRefusesAFIFOWithoutBlocking(t *testing.T) {
+ b := &ExecBackend{DataDir: t.TempDir()}
+ host := "f.example.com"
+ rt := registry.Route{Host: host, Kind: "instance", Hibernate: true,
+ DBPath: provision.DefaultDBPath(b.DataDir, host), Socket: provision.DefaultSocket(host)}
+ p := b.pathsFor(rt)
+ if p.layout != LayoutFlat {
+ t.Fatalf("route classified %v (%s), want flat", p.layout, p.reason)
+ }
+ // The sync-owned config layer is read first (instanceEnvParts), so the
+ // FIFO goes at that path; <host>.env would do the same.
+ if err := syscall.Mkfifo(p.configEnv, 0o600); err != nil {
+ t.Fatalf("mkfifo: %v", err)
+ }
+ t.Cleanup(func() {
+ // Unblock a reader still parked on the FIFO so the goroutine ends.
+ if w, err := os.OpenFile(p.configEnv, os.O_WRONLY|syscall.O_NONBLOCK, 0); err == nil {
+ w.Close()
+ }
+ })
+
+ done := make(chan error, 1)
+ go func() {
+ _, err := b.instanceEnv(rt)
+ done <- err
+ }()
+ select {
+ case err := <-done:
+ if err == nil {
+ t.Fatalf("flat env read accepted a FIFO at %s as an env file", p.configEnv)
+ }
+ t.Logf("flat env read refused the FIFO: %v", err)
+ case <-time.After(3 * time.Second):
+ t.Fatalf("flat env read of %s blocked for 3s on a FIFO: readEnvFile is a plain os.ReadFile with no O_NONBLOCK and no file-type check (backend_exec.go:2725, :3306)", p.configEnv)
+ }
+}
+
+// The nested reader is the contrast the finding draws: the same FIFO at a
+// nested row's env path is refused at once (instancefs opens O_NONBLOCK
+// and fstat-checks for a regular file).
+func TestHunt_H2_M5_NestedEnvReadRefusesAFIFOWithoutBlocking(t *testing.T) {
+ b := &ExecBackend{DataDir: t.TempDir()}
+ host := "n.example.com"
+ rt := registry.Route{Host: host, Kind: "instance", Hibernate: true,
+ DBPath: provision.NestedDBPath(b.DataDir, host), Socket: provision.NestedSocket(host)}
+ p := b.pathsFor(rt)
+ if p.layout != LayoutNested {
+ t.Fatalf("route classified %v (%s), want nested", p.layout, p.reason)
+ }
+ if err := os.MkdirAll(p.dataDir, 0o700); err != nil {
+ t.Fatal(err)
+ }
+ if err := syscall.Mkfifo(p.configEnv, 0o600); err != nil {
+ t.Fatalf("mkfifo: %v", err)
+ }
+ t.Cleanup(func() {
+ if w, err := os.OpenFile(p.configEnv, os.O_WRONLY|syscall.O_NONBLOCK, 0); err == nil {
+ w.Close()
+ }
+ })
+ done := make(chan error, 1)
+ go func() {
+ _, err := b.instanceEnv(rt)
+ done <- err
+ }()
+ select {
+ case err := <-done:
+ if err == nil {
+ t.Fatalf("nested env read accepted a FIFO at %s", p.configEnv)
+ }
+ t.Logf("nested env read refused the FIFO: %v", err)
+ case <-time.After(3 * time.Second):
+ t.Fatalf("nested env read of %s blocked for 3s on a FIFO", p.configEnv)
+ }
+}
+
+// The symlink half of the same asymmetry: a flat row's <host>.env that is a
+// symlink to a file elsewhere is followed by os.ReadFile and its contents
+// become the instance's env. The secure expectation: the link is refused,
+// as instancefs refuses it for a nested row.
+func TestHunt_H2_M5_FlatEnvReadDoesNotFollowASymlink(t *testing.T) {
+ b := &ExecBackend{DataDir: t.TempDir()}
+ host := "s.example.com"
+ rt := registry.Route{Host: host, Kind: "instance", Hibernate: true,
+ DBPath: provision.DefaultDBPath(b.DataDir, host), Socket: provision.DefaultSocket(host)}
+ p := b.pathsFor(rt)
+ if p.layout != LayoutFlat {
+ t.Fatalf("route classified %v (%s), want flat", p.layout, p.reason)
+ }
+ elsewhere := filepath.Join(t.TempDir(), "other.env")
+ if err := os.WriteFile(elsewhere, []byte("PLANTED=1\n"), 0o600); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.Symlink(elsewhere, p.envFile); err != nil {
+ t.Fatal(err)
+ }
+ env, err := b.instanceEnv(rt)
+ if err == nil && env["PLANTED"] == "1" {
+ t.Fatalf("flat env read followed the symlink %s -> %s and took PLANTED=1 into the instance env (readEnvFile is os.ReadFile, backend_exec.go:3306)", p.envFile, elsewhere)
+ }
+}