| 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) |
| + } |
| +} |