release: root reads its channel floor only from its own dir, fd-safe
The census finding on DigestRatchet.readWatermark (2026-10-25): the floor was read with os.ReadFile from every ratchet directory, root's included reading the agent's <data>/pointer-digests, which the agent and any flat workload with its uid can write. A symlink there was followed, a FIFO hung the read, and the read had no size cap. Now root reads a floor only from its own directory, the root-owned <opt>/pointer-digests (floorDirs); the agent and the edge still read their own and root's, which they cannot write. Every floor is read through instancefs.ReadBeneathLimitOrAbsent: the directory opened with no symlink at any component, the floor beneath that descriptor with openat2 (RESOLVE_BENEATH|NO_SYMLINKS|NO_MAGICLINKS), O_NONBLOCK, regular files only, at most 4 KiB. Absent is only a ratchet directory not made yet or ENOENT beneath its descriptor; anything else refuses, retryably, as an unreadable mark does. Tests: TestWatermarkRootReadsOnlyItsOwnDirectory, TestWatermarkReadIsDescriptorSafe (symlinked floor, symlinked account dir, FIFO, oversized floor) and TestWatermarkDirectoryAbsentIsNoFloorButASymlinkedOneRefuses; each failed before (the floor was read through the link, the FIFO blocked, root read the agent's floor).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+240
−18
internal/release/ratchet.go+2 −1internal/release/watermark.go+73 −16internal/release/watermark_read_linux_test.go+160 −0internal/release/watermark_test.go+5 −1
diff --git a/internal/release/ratchet.go b/internal/release/ratchet.go| index 15da10d37..616a21645 100644 |
| --- a/internal/release/ratchet.go |
| +++ b/internal/release/ratchet.go |
| @@ -85,7 +85,8 @@ func EdgeRatchetDir(dataDir string) string { return filepath.Join(dataDir, "poin |
| // pointer has the same floor in internal/host/systemapply.go (seen.json); |
| // this one lives beside the digest marks because they are the same kind of |
| // state, per box and per channel, written by root and the agent to their |
| -// own directory and read from both. |
| +// own directory. The agent reads both; root reads only its own, since the |
| +// agent's is one the agent can write (readWatermark, floorDirs). |
| type DigestRatchet struct { |
| own string |
| also []string |
diff --git a/internal/release/watermark.go b/internal/release/watermark.go| index 3a2ec1538..e9ab9a972 100644 |
| --- a/internal/release/watermark.go |
| +++ b/internal/release/watermark.go |
| @@ -10,9 +10,9 @@ import ( |
| "path/filepath" |
| "slices" |
| "strings" |
| - "syscall" |
| "time" |
| + "github.com/carlosframework/platform/internal/instancefs" |
| "github.com/carlosframework/platform/internal/safefile" |
| ) |
| @@ -34,7 +34,7 @@ type watermark struct { |
| // watermarkPath is a channel's floor: <dir>/<acct>/<app>/<channel>.watermark, |
| // beside the channel's digest mark, with "/" escaped as markPath does. |
| func watermarkPath(dir, acct, app, channel string) string { |
| - return filepath.Join(dir, acct, app, strings.ReplaceAll(channel, "/", "%2F")+".watermark") |
| + return filepath.Join(dir, watermarkRel(acct, app, channel)) |
| } |
| // admits reports whether p may follow this floor. A later promoted_at is |
| @@ -133,30 +133,87 @@ func (r *DigestRatchet) replayed(acct string, p *ChannelPointer, wm watermark) e |
| wm.Version, wm.At.UTC().Format(time.RFC3339)) |
| } |
| -// readWatermark is the highest floor in any of the ratchet's directories. |
| -// Only a confirmed absence (ENOENT, or ENOTDIR on a path component) counts |
| -// as no floor; any other answer, a file that does not parse included, is an |
| -// error the caller treats as fail-closed, as marked does for a mark. |
| +// readWatermark is the highest floor in the directories this process reads |
| +// floors from (floorDirs). Only a confirmed absence counts as no floor: the |
| +// ratchet directory not made yet, or no entry at the floor's name beneath |
| +// it. Any other answer, a file that does not parse included, is an error |
| +// the caller treats as fail-closed, as marked does for a mark. |
| +// |
| +// Each floor is read descriptor-safe (readFloorFile), never by path: the |
| +// directory is opened with no symlink at any component of its path, and |
| +// the floor beneath that descriptor with openat2 (RESOLVE_BENEATH, |
| +// RESOLVE_NO_SYMLINKS, RESOLVE_NO_MAGICLINKS), O_NONBLOCK, accepted only |
| +// as a regular file and read to at most watermarkReadLimit bytes |
| +// (instancefs.ReadBeneathLimitOrAbsent). A symlink, a FIFO, a directory or |
| +// an oversized file where the floor belongs refuses; none is followed, |
| +// waited on or read whole. |
| func (r *DigestRatchet) readWatermark(acct, app, channel string) (watermark, error) { |
| var out watermark |
| - for _, dir := range append([]string{r.own}, r.also...) { |
| - if dir == "" { |
| - continue |
| - } |
| - path := watermarkPath(dir, acct, app, channel) |
| - raw, err := os.ReadFile(path) |
| - if errors.Is(err, fs.ErrNotExist) || errors.Is(err, syscall.ENOTDIR) { |
| - continue |
| - } |
| + rel := watermarkRel(acct, app, channel) |
| + for _, dir := range r.floorDirs() { |
| + raw, absent, err := readFloorFile(dir, rel) |
| if err != nil { |
| return watermark{}, err |
| } |
| + if absent { |
| + continue |
| + } |
| var wm watermark |
| if err := json.Unmarshal(raw, &wm); err != nil || wm.At.IsZero() { |
| - return watermark{}, fmt.Errorf("channel watermark %s is malformed", path) |
| + return watermark{}, fmt.Errorf("channel watermark %s is malformed", filepath.Join(dir, rel)) |
| } |
| out = later(out, wm) |
| } |
| out.durable = true |
| return out, nil |
| } |
| + |
| +// floorDirs is where this process reads channel floors from: its own |
| +// directory always, and the ratchet's other directories only when it is |
| +// not root. Root's own directory is <opt>/pointer-digests, root-owned; its |
| +// other one is the agent's <data>/pointer-digests, where the agent, and |
| +// any flat workload running as the agent's uid, can create and name |
| +// entries. Root opens nothing there (the census finding on readWatermark, |
| +// 2026-10-25): its floor is the one it wrote itself, for the pointers it |
| +// followed. The agent and the edge (uid carlos) read their own directory |
| +// and root's, which they cannot write, so a floor root raised holds for |
| +// them too. |
| +func (r *DigestRatchet) floorDirs() []string { |
| + var dirs []string |
| + if r.own != "" { |
| + dirs = append(dirs, r.own) |
| + } |
| + if ratchetIsRoot() { |
| + return dirs |
| + } |
| + for _, d := range r.also { |
| + if d != "" { |
| + dirs = append(dirs, d) |
| + } |
| + } |
| + return dirs |
| +} |
| + |
| +// watermarkRel is watermarkPath beneath its ratchet directory. |
| +func watermarkRel(acct, app, channel string) string { |
| + return filepath.Join(acct, app, strings.ReplaceAll(channel, "/", "%2F")+".watermark") |
| +} |
| + |
| +// readFloorFile reads rel beneath dir as readWatermark describes. absent is |
| +// true only for a ratchet directory that does not exist (a fresh box, before |
| +// its first mark) or no entry at rel beneath the directory's descriptor |
| +// (ENOENT, a missing account or app directory included). A directory that |
| +// is a symlink, or cannot be opened for any other reason, is an error. |
| +func readFloorFile(dir, rel string) (raw []byte, absent bool, err error) { |
| + raw, absent, err = instancefs.ReadBeneathLimitOrAbsent(dir, rel, watermarkReadLimit) |
| + if err != nil && errors.Is(err, instancefs.ErrDirUnopened) && errors.Is(err, fs.ErrNotExist) { |
| + return nil, true, nil |
| + } |
| + return raw, absent, err |
| +} |
| + |
| +// watermarkReadLimit caps a floor file; one is a single short JSON line. |
| +const watermarkReadLimit = 4 << 10 |
| + |
| +// ratchetIsRoot reports whether this process runs as root. A test seam. |
| +var ratchetIsRoot = func() bool { return os.Geteuid() == 0 } |
diff --git a/internal/release/watermark_read_linux_test.go b/internal/release/watermark_read_linux_test.go| new file mode 100644 |
| index 000000000..9d2d0df34 |
| --- /dev/null |
| +++ b/internal/release/watermark_read_linux_test.go |
| @@ -0,0 +1,160 @@ |
| +//go:build linux |
| + |
| +package release |
| + |
| +import ( |
| + "errors" |
| + "os" |
| + "path/filepath" |
| + "strings" |
| + "syscall" |
| + "testing" |
| + "time" |
| +) |
| + |
| +// admitWithin runs Admit and fails the test if it does not return within |
| +// a few seconds: a FIFO opened for reading without O_NONBLOCK would block |
| +// for ever, and that hang is the failure being looked for. |
| +func admitWithin(t *testing.T, r *DigestRatchet, acct string, p *ChannelPointer) error { |
| + t.Helper() |
| + done := make(chan error, 1) |
| + go func() { done <- r.Admit(acct, p) }() |
| + select { |
| + case err := <-done: |
| + return err |
| + case <-time.After(5 * time.Second): |
| + t.Fatal("Admit blocked reading the channel watermark") |
| + return nil |
| + } |
| +} |
| + |
| +// writeFloor puts a valid floor file at path, creating its directories. |
| +func writeFloor(t *testing.T, path string, at time.Time, version string) { |
| + t.Helper() |
| + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { |
| + t.Fatal(err) |
| + } |
| + body := `{"promoted_at":"` + at.UTC().Format(time.RFC3339) + `","version":"` + version + `"}` + "\n" |
| + if err := os.WriteFile(path, []byte(body), 0o644); err != nil { |
| + t.Fatal(err) |
| + } |
| +} |
| + |
| +// Root reads a channel's floor from its own directory only (the census |
| +// finding on DigestRatchet.readWatermark): the other directory is the |
| +// agent's data dir, where the agent, and any flat workload running as its |
| +// uid, creates and names entries. So nothing there is opened by root: not a |
| +// floor, and not a FIFO that would hang the read. A process that is not |
| +// root still reads both, its own and root's. |
| +func TestWatermarkRootReadsOnlyItsOwnDirectory(t *testing.T) { |
| + t0 := time.Date(2026, 10, 1, 12, 0, 0, 0, time.UTC) |
| + own, agent := t.TempDir(), t.TempDir() |
| + writeFloor(t, watermarkPath(agent, "bab", "km", "stable"), t0.Add(time.Hour), "v9") |
| + if err := os.MkdirAll(filepath.Dir(watermarkPath(agent, "bab", "km", "beta")), 0o755); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := syscall.Mkfifo(watermarkPath(agent, "bab", "km", "beta"), 0o644); err != nil { |
| + t.Fatal(err) |
| + } |
| + |
| + old := ratchetIsRoot |
| + t.Cleanup(func() { ratchetIsRoot = old }) |
| + |
| + ratchetIsRoot = func() bool { return false } |
| + if err := admitWithin(t, NewDigestRatchet(own, []string{agent}, false), "bab", stamped("stable", "v1", t0)); !errors.Is(err, ErrPointerReplayed) { |
| + t.Fatalf("not root: the other directory's floor was not read: %v", err) |
| + } |
| + |
| + ratchetIsRoot = func() bool { return true } |
| + if err := admitWithin(t, NewDigestRatchet(own, []string{agent}, false), "bab", stamped("stable", "v1", t0)); err != nil { |
| + t.Fatalf("root read the agent's directory: %v", err) |
| + } |
| + if err := admitWithin(t, NewDigestRatchet(own, []string{agent}, false), "bab", stamped("beta", "v1", t0)); err != nil { |
| + t.Fatalf("root opened the agent's FIFO: %v", err) |
| + } |
| + // Root's own floor still holds. |
| + if err := admitWithin(t, NewDigestRatchet(own, []string{agent}, false), "bab", stamped("stable", "v0", t0.Add(-time.Minute))); !errors.Is(err, ErrPointerReplayed) { |
| + t.Fatalf("root's own floor was not read: %v", err) |
| + } |
| +} |
| + |
| +// The floor is opened beneath its directory with no symlink at any |
| +// component, never a FIFO or anything but a regular file, and never more |
| +// than a small cap: each of these refuses (retryably, as any unreadable |
| +// floor does), and none is followed, waited on or read whole. |
| +func TestWatermarkReadIsDescriptorSafe(t *testing.T) { |
| + t0 := time.Date(2026, 10, 1, 12, 0, 0, 0, time.UTC) |
| + elsewhere := filepath.Join(t.TempDir(), "floor.watermark") |
| + // A floor that would refuse v1 as a replay, were it followed. |
| + writeFloor(t, elsewhere, t0.Add(time.Hour), "v9") |
| + |
| + cases := map[string]func(t *testing.T, dir string){ |
| + "symlinked floor": func(t *testing.T, dir string) { |
| + p := watermarkPath(dir, "bab", "km", "stable") |
| + if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.Symlink(elsewhere, p); err != nil { |
| + t.Fatal(err) |
| + } |
| + }, |
| + "symlinked account directory": func(t *testing.T, dir string) { |
| + target := t.TempDir() |
| + writeFloor(t, filepath.Join(target, "km", "stable.watermark"), t0.Add(time.Hour), "v9") |
| + if err := os.Symlink(target, filepath.Join(dir, "bab")); err != nil { |
| + t.Fatal(err) |
| + } |
| + }, |
| + "FIFO": func(t *testing.T, dir string) { |
| + p := watermarkPath(dir, "bab", "km", "stable") |
| + if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := syscall.Mkfifo(p, 0o644); err != nil { |
| + t.Fatal(err) |
| + } |
| + }, |
| + "oversized floor": func(t *testing.T, dir string) { |
| + p := watermarkPath(dir, "bab", "km", "stable") |
| + if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil { |
| + t.Fatal(err) |
| + } |
| + body := `{"promoted_at":"2026-10-01T13:00:00Z","version":"v9","pad":"` + strings.Repeat("x", watermarkReadLimit) + `"}` |
| + if err := os.WriteFile(p, []byte(body), 0o644); err != nil { |
| + t.Fatal(err) |
| + } |
| + }, |
| + } |
| + for name, plant := range cases { |
| + t.Run(name, func(t *testing.T) { |
| + dir := t.TempDir() |
| + plant(t, dir) |
| + err := admitWithin(t, NewDigestRatchet(dir, nil, false), "bab", stamped("stable", "v1", t0)) |
| + if err == nil || errors.Is(err, ErrPointerReplayed) || !errors.Is(err, errRatchetMarkUnreadable) { |
| + t.Fatalf("Admit = %v, want an unreadable (retryable) refusal, never a floor read through it", err) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +// A ratchet directory not made yet (a fresh box) is no floor, as a missing |
| +// floor file is; a symlink where the directory belongs is not. |
| +func TestWatermarkDirectoryAbsentIsNoFloorButASymlinkedOneRefuses(t *testing.T) { |
| + t0 := time.Date(2026, 10, 1, 12, 0, 0, 0, time.UTC) |
| + missing := filepath.Join(t.TempDir(), "pointer-digests") |
| + ratchet := NewDigestRatchet(missing, nil, false) |
| + if err := admitWithin(t, ratchet, "bab", stamped("stable", "v1", t0)); err != nil { |
| + t.Fatalf("a fresh box with no ratchet directory: %v", err) |
| + } |
| + |
| + real := t.TempDir() |
| + writeFloor(t, watermarkPath(real, "bab", "km", "stable"), t0.Add(time.Hour), "v9") |
| + link := filepath.Join(t.TempDir(), "pointer-digests") |
| + if err := os.Symlink(real, link); err != nil { |
| + t.Fatal(err) |
| + } |
| + err := admitWithin(t, NewDigestRatchet(link, nil, false), "bab", stamped("stable", "v1", t0)) |
| + if err == nil || errors.Is(err, ErrPointerReplayed) || !errors.Is(err, errRatchetMarkUnreadable) { |
| + t.Fatalf("Admit = %v, want an unreadable refusal for a symlinked ratchet directory", err) |
| + } |
| +} |
diff --git a/internal/release/watermark_test.go b/internal/release/watermark_test.go| index 3d93776c9..b2ead2c2f 100644 |
| --- a/internal/release/watermark_test.go |
| +++ b/internal/release/watermark_test.go |
| @@ -88,7 +88,11 @@ func TestWatermarkSurvivesARestartAndIsReadFromEveryDir(t *testing.T) { |
| if err := NewDigestRatchet(root, []string{agent}, false).Admit("bab", stamped("stable", "v1", t0)); !errors.Is(err, ErrPointerReplayed) { |
| t.Fatalf("a new process forgot the floor: %v", err) |
| } |
| - // The agent reads root's floor as its own. |
| + // The agent reads root's floor as its own. (Root reads only its own |
| + // directory, so this half is the agent's: pinned for a run as root.) |
| + old := ratchetIsRoot |
| + t.Cleanup(func() { ratchetIsRoot = old }) |
| + ratchetIsRoot = func() bool { return false } |
| if err := NewDigestRatchet(agent, []string{root}, false).Admit("bab", stamped("stable", "v1", t0)); !errors.Is(err, ErrPointerReplayed) { |
| t.Fatalf("the other process's floor was not read: %v", err) |
| } |