From 8a49d0485b9385076465d802d56b383979b9a41d Mon Sep 17 00:00:00 2001 From: sec-check <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Sun, 8 Nov 2026 10:37:56 -0500 Subject: [PATCH 1/4] =?UTF-8?q?=F0=9F=90=9B=20fix(knowledge/climemory):=20?= =?UTF-8?q?open=20memory=20files=20through=20their=20store=20root,=20never?= =?UTF-8?q?=20by=20path?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The drain listed agent-owned memory files with Lstat but then read them with os.ReadFile and emptied them with os.Truncate by path, both of which follow a symlink the agent swaps in after the listing. The hive runs with more privilege than the agent, so that was a read primitive into the knowledge vault and a truncate primitive over any hive-writable file. Every file now goes through an os.Root anchored at the store (checked against the store's Lstat with SameFile), is fstat-checked as a regular file under the size cap, and TruncateDrained truncates the descriptor it read from. Fixes #17251 Signed-off-by: sec-check <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- .../security-17251-climemory-nofollow.md | 1 + src/pkg/knowledge/climemory/drain.go | 22 +++- src/pkg/knowledge/climemory/legacy.go | 59 ++++++--- src/pkg/knowledge/climemory/memstore.go | 122 ++++++++++++++++++ src/pkg/knowledge/climemory/memstore_test.go | 113 ++++++++++++++++ src/pkg/knowledge/climemory/spool.go | 2 +- 6 files changed, 296 insertions(+), 23 deletions(-) create mode 100644 changelog.d/security-17251-climemory-nofollow.md create mode 100644 src/pkg/knowledge/climemory/memstore.go create mode 100644 src/pkg/knowledge/climemory/memstore_test.go diff --git a/changelog.d/security-17251-climemory-nofollow.md b/changelog.d/security-17251-climemory-nofollow.md new file mode 100644 index 0000000000..795109194a --- /dev/null +++ b/changelog.d/security-17251-climemory-nofollow.md @@ -0,0 +1 @@ +- The CLI-memory drain opens every agent memory file through an `os.Root` anchored at its store and truncates it through the descriptor it read, instead of reading and `os.Truncate`-ing by path after the listing. The store belongs to the agent and the drain runs with the hive's privileges, so an agent that swapped a listed `.md` (or the store directory) for a symlink between the listing and the open could have had the hive copy any file it can read into the knowledge vault, or empty any file it can write — `TruncateDrained` counts a file with no parseable fact as "fully stored". Such a path is now reported in `kept` and the drain error, never followed ([#17251](https://github.com/hivecommons/hive/issues/17251)). diff --git a/src/pkg/knowledge/climemory/drain.go b/src/pkg/knowledge/climemory/drain.go index d0b86b84c5..cc7979ff06 100644 --- a/src/pkg/knowledge/climemory/drain.go +++ b/src/pkg/knowledge/climemory/drain.go @@ -98,6 +98,9 @@ type Drainer struct { type memFile struct { cli, path, repo string spoolDir string + // store is the opened source the file is read through; nil for a file + // being retried from the spool. + store *memoryStore } // drainRun is the state of one Drain call. @@ -133,14 +136,23 @@ func (d *Drainer) Drain(req Request) Result { } var files []memFile for _, src := range req.Sources { - paths, err := memoryFiles(src.Path) + paths, err := listMemoryFiles(src.Path) if err != nil { res.Errors = append(res.Errors, err) continue } + if len(paths) == 0 { + continue + } + store, err := openMemoryStore(src.Path) + if err != nil { + res.Errors = append(res.Errors, fmt.Errorf("cli-memory drain: opening %s: %w", src.Path, err)) + continue + } + defer store.Close() repo := repoFromClaudeSlug(src.Path, req.Repos) for _, p := range paths { - files = append(files, memFile{cli: src.CLI, path: p, repo: repo}) + files = append(files, memFile{cli: src.CLI, path: p, repo: repo, store: store}) } } seen, err := r.prepareVault() @@ -165,7 +177,7 @@ func (d *Drainer) Drain(req Request) Result { } } for _, f := range files { - content, err := os.ReadFile(f.path) + content, err := f.store.read(f.path) if err != nil { res.Errors = append(res.Errors, fmt.Errorf("cli-memory drain: reading %s: %w", f.path, err)) continue @@ -346,6 +358,10 @@ func hashFromFrontmatter(path string) string { return "" } +// listMemoryFiles is memoryFiles behind a seam, so a test can hand Drain +// and TruncateDrained a listing the store no longer matches. +var listMemoryFiles = memoryFiles + // memoryFiles lists the markdown files under path (or path itself when it // is a file), skipping temp files, hidden entries, symlinks and oversized // files. A missing path is not an error: the CLI simply wrote nothing. diff --git a/src/pkg/knowledge/climemory/legacy.go b/src/pkg/knowledge/climemory/legacy.go index bbabf5ebc5..ed13439d69 100644 --- a/src/pkg/knowledge/climemory/legacy.go +++ b/src/pkg/knowledge/climemory/legacy.go @@ -46,7 +46,10 @@ func LegacyClaudeSources(projectsDir string) ([]Source, error) { // holding a fact the vault lacks — a write failed, the per-drain cap was // hit — is left as it is and reported in kept, so truncation never loses a // fact. A non-empty file with no fact to drain (a Claude index of links) -// counts as stored. Already-empty files are in neither list. +// counts as stored. Already-empty files are in neither list. Each file is +// read and truncated through one descriptor opened inside its store, so a +// path the agent swapped for a symlink since the listing is kept, not +// followed. func (d *Drainer) TruncateDrained(sources []Source) (truncated, kept []string, err error) { if d == nil || d.Dir == "" { return nil, nil, errors.New("cli-memory drain: no target directory") @@ -57,36 +60,54 @@ func (d *Drainer) TruncateDrained(sources []Source) (truncated, kept []string, e } var errs []error for _, src := range sources { - files, err := memoryFiles(src.Path) + files, err := listMemoryFiles(src.Path) if err != nil { errs = append(errs, err) continue } + if len(files) == 0 { + continue + } + store, err := openMemoryStore(src.Path) + if err != nil { + errs = append(errs, fmt.Errorf("cli-memory drain: opening %s: %w", src.Path, err)) + kept = append(kept, files...) + continue + } for _, file := range files { - content, err := os.ReadFile(file) + t, k, err := truncateIfStored(store, src.CLI, file, seen) if err != nil { - errs = append(errs, fmt.Errorf("cli-memory drain: reading %s: %w", file, err)) - kept = append(kept, file) - continue + errs = append(errs, err) } - if len(content) == 0 { - continue - } - if !allStored(ParseFor(src.CLI, string(content)), seen) { - kept = append(kept, file) - continue - } - if err := os.Truncate(file, 0); err != nil { - errs = append(errs, fmt.Errorf("cli-memory drain: truncating %s: %w", file, err)) - kept = append(kept, file) - continue - } - truncated = append(truncated, file) + truncated, kept = append(truncated, t...), append(kept, k...) } + store.Close() } return truncated, kept, errors.Join(errs...) } +func truncateIfStored(store *memoryStore, cli, file string, seen map[string]string) (truncated, kept []string, err error) { + f, err := store.open(file, os.O_RDWR) + if err != nil { + return nil, []string{file}, fmt.Errorf("cli-memory drain: opening %s: %w", file, err) + } + defer f.Close() + content, err := readMemoryFile(f) + if err != nil { + return nil, []string{file}, fmt.Errorf("cli-memory drain: reading %s: %w", file, err) + } + if len(content) == 0 { + return nil, nil, nil + } + if !allStored(ParseFor(cli, string(content)), seen) { + return nil, []string{file}, nil + } + if err := f.Truncate(0); err != nil { + return nil, []string{file}, fmt.Errorf("cli-memory drain: truncating %s: %w", file, err) + } + return []string{file}, nil, nil +} + func allStored(facts []Fact, seen map[string]string) bool { for _, f := range facts { if _, ok := seen[Hash(f.Body)]; !ok { diff --git a/src/pkg/knowledge/climemory/memstore.go b/src/pkg/knowledge/climemory/memstore.go new file mode 100644 index 0000000000..4d37e92fb4 --- /dev/null +++ b/src/pkg/knowledge/climemory/memstore.go @@ -0,0 +1,122 @@ +package climemory + +import ( + "errors" + "fmt" + "io" + "os" + "path/filepath" +) + +// errNotMemoryFile marks a listed path that is no longer a regular memory +// file inside its store by the time it is opened. The store is owned by the +// agent and the drain runs with the hive's privileges, so a symlink swapped +// in between memoryFiles and the open must never be followed: by path it +// would read, and TruncateDrained would empty, whatever the agent pointed +// it at. +var errNotMemoryFile = errors.New("cli-memory drain: not a regular memory file in its store") + +// memoryStore is one opened Source: the store directory (or the directory +// holding a single-file store) every memory file is opened through, so a +// listed path can only resolve inside it. +type memoryStore struct { + root *os.Root +} + +// openMemoryStore opens the store directory for path — path itself when it +// is a directory, its parent for a file store — and checks that the opened +// directory is the one Lstat sees at that path, so a store directory +// swapped for a symlink since memoryFiles listed it is refused rather than +// followed. +func openMemoryStore(path string) (*memoryStore, error) { + base := path + lfi, err := os.Lstat(path) + if err != nil { + return nil, err + } + if !lfi.IsDir() { + base = filepath.Dir(path) + if lfi, err = os.Lstat(base); err != nil { + return nil, err + } + } + if !lfi.IsDir() { + return nil, fmt.Errorf("%w: %s is not a directory", errNotMemoryFile, base) + } + root, err := os.OpenRoot(base) + if err != nil { + return nil, err + } + rfi, err := root.Stat(".") + if err != nil { + root.Close() + return nil, err + } + if !os.SameFile(lfi, rfi) { + root.Close() + return nil, fmt.Errorf("%w: %s changed while being opened", errNotMemoryFile, base) + } + return &memoryStore{root: root}, nil +} + +func (s *memoryStore) Close() error { + if s == nil || s.root == nil { + return nil + } + return s.root.Close() +} + +// open opens path, a file memoryFiles listed under this store, inside the +// store root (a symlink that leaves the root is refused by os.Root) and +// checks the open descriptor is a regular file within the size cap. flag is +// os.O_RDONLY or os.O_RDWR; the caller closes the file. +func (s *memoryStore) open(path string, flag int) (*os.File, error) { + if s == nil || s.root == nil { + return nil, fmt.Errorf("%w: %s has no open store", errNotMemoryFile, path) + } + rel, err := filepath.Rel(s.root.Name(), path) + if err != nil { + return nil, fmt.Errorf("%w: %s: %v", errNotMemoryFile, path, err) + } + f, err := s.root.OpenFile(rel, flag, 0) + if err != nil { + return nil, fmt.Errorf("%w: %s: %v", errNotMemoryFile, path, err) + } + fi, err := f.Stat() + if err != nil { + f.Close() + return nil, err + } + if !fi.Mode().IsRegular() { + f.Close() + return nil, fmt.Errorf("%w: %s is %s", errNotMemoryFile, path, fi.Mode().Type()) + } + if fi.Size() > maxMemoryFileBytes { + f.Close() + return nil, fmt.Errorf("%w: %s is %d bytes, over the %d cap", errNotMemoryFile, path, fi.Size(), maxMemoryFileBytes) + } + return f, nil +} + +// read returns the whole of path opened through the store. +func (s *memoryStore) read(path string) ([]byte, error) { + f, err := s.open(path, os.O_RDONLY) + if err != nil { + return nil, err + } + defer f.Close() + return readMemoryFile(f) +} + +// readMemoryFile reads an open memory file, capped even if it grows after +// the size check in open. +func readMemoryFile(f *os.File) ([]byte, error) { + data, err := io.ReadAll(io.LimitReader(f, maxMemoryFileBytes+1)) + if err != nil { + return nil, err + } + if int64(len(data)) > maxMemoryFileBytes { + return nil, fmt.Errorf("%w: %s grew past the %d cap while being read", errNotMemoryFile, f.Name(), maxMemoryFileBytes) + } + return data, nil +} diff --git a/src/pkg/knowledge/climemory/memstore_test.go b/src/pkg/knowledge/climemory/memstore_test.go new file mode 100644 index 0000000000..f96340ea2f --- /dev/null +++ b/src/pkg/knowledge/climemory/memstore_test.go @@ -0,0 +1,113 @@ +package climemory + +import ( + "errors" + "os" + "path/filepath" + "strings" + "testing" +) + +// swapForSymlink makes listMemoryFiles report path as a memory file and then +// replaces it with a symlink to target, the way an agent that owns the store +// can between the hive's listing and its open. +func swapForSymlink(t *testing.T, path, target string) { + t.Helper() + real := memoryFiles + listMemoryFiles = func(p string) ([]string, error) { + files, err := real(p) + if err != nil { + return nil, err + } + if err := os.Remove(path); err != nil && !errors.Is(err, os.ErrNotExist) { + t.Fatal(err) + } + if err := os.Symlink(target, path); err != nil { + t.Skip("symlinks unavailable") + } + return files, nil + } + t.Cleanup(func() { listMemoryFiles = real }) +} + +func TestTruncateDrainedNeverFollowsSymlinkSwappedInAfterListing(t *testing.T) { + outside := filepath.Join(t.TempDir(), "hive.yaml") + // No "- " bullets: parsed as zero facts, which TruncateDrained counts + // as fully stored, so a path-based truncate would empty the target. + writeFile(t, outside, "dashboard:\n token: not-a-fact\n") + store := filepath.Join(t.TempDir(), "-data-agents-scanner", "memory") + victim := filepath.Join(store, "MEMORY.md") + writeFile(t, victim, "# index\n") + swapForSymlink(t, victim, outside) + + d := &Drainer{Dir: t.TempDir(), Now: fixedNow} + truncated, kept, err := d.TruncateDrained([]Source{{CLI: CLIClaude, Path: store}}) + if len(truncated) != 0 { + t.Fatalf("truncated %v through a symlink", truncated) + } + if len(kept) != 1 || kept[0] != victim { + t.Fatalf("kept = %v, want the swapped path", kept) + } + if err == nil || !errors.Is(err, errNotMemoryFile) { + t.Fatalf("err = %v, want errNotMemoryFile", err) + } + if b, _ := os.ReadFile(outside); !strings.Contains(string(b), "not-a-fact") { + t.Fatalf("symlink target was truncated: %q", b) + } +} + +func TestDrainNeverReadsSymlinkSwappedInAfterListing(t *testing.T) { + outside := filepath.Join(t.TempDir(), "secret.md") + writeFile(t, outside, "- The hive's private token is in this line.\n") + store := filepath.Join(t.TempDir(), "-data-agents-scanner", "memory") + victim := filepath.Join(store, "MEMORY.md") + writeFile(t, victim, "- A fact the agent really wrote.\n") + swapForSymlink(t, victim, outside) + + vault := t.TempDir() + d := &Drainer{Dir: vault, SpoolDir: t.TempDir(), Now: fixedNow} + res := d.Drain(Request{Agent: "scanner", Sources: []Source{{CLI: CLIClaude, Path: store}}}) + if res.Drained != 0 || res.Spooled != 0 { + t.Fatalf("drained %d / spooled %d through a symlink", res.Drained, res.Spooled) + } + if len(res.Errors) != 1 || !errors.Is(res.Errors[0], errNotMemoryFile) { + t.Fatalf("errors = %v, want one errNotMemoryFile", res.Errors) + } + entries, _ := filepath.Glob(filepath.Join(vault, filePrefix+"*.md")) + for _, e := range entries { + if b, _ := os.ReadFile(e); strings.Contains(string(b), "private token") { + t.Fatalf("symlink target drained into %s", e) + } + } +} + +func TestOpenMemoryStoreRefusesSwappedStoreDirectory(t *testing.T) { + outside := t.TempDir() + writeFile(t, filepath.Join(outside, "MEMORY.md"), "x\n") + home := t.TempDir() + store := filepath.Join(home, "memory") + if err := os.Symlink(outside, store); err != nil { + t.Skip("symlinks unavailable") + } + if s, err := openMemoryStore(store); err == nil { + s.Close() + t.Fatal("opened a symlinked store directory") + } else if !errors.Is(err, errNotMemoryFile) { + t.Fatalf("err = %v, want errNotMemoryFile", err) + } +} + +func TestMemoryStoreOpenKeepsSingleFileStoreWorking(t *testing.T) { + home := t.TempDir() + gemini := filepath.Join(home, "GEMINI.md") + writeFile(t, gemini, "- Single-file stores open through their parent.\n") + s, err := openMemoryStore(gemini) + if err != nil { + t.Fatal(err) + } + defer s.Close() + b, err := s.read(gemini) + if err != nil || !strings.Contains(string(b), "Single-file") { + t.Fatalf("read = %q, %v", b, err) + } +} diff --git a/src/pkg/knowledge/climemory/spool.go b/src/pkg/knowledge/climemory/spool.go index 38bc27b369..d20cb0f6a7 100644 --- a/src/pkg/knowledge/climemory/spool.go +++ b/src/pkg/knowledge/climemory/spool.go @@ -47,7 +47,7 @@ func (r *drainRun) spool(f memFile, content []byte) { } if content == nil { var err error - if content, err = os.ReadFile(f.path); err != nil { + if content, err = f.store.read(f.path); err != nil { r.res.Errors = append(r.res.Errors, fmt.Errorf("cli-memory drain: reading %s to spool: %w", f.path, err)) return } From b22e0a72844cd823c85b2a136b116caac3488502 Mon Sep 17 00:00:00 2001 From: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Sun, 8 Nov 2026 10:47:56 -0500 Subject: [PATCH 2/4] =?UTF-8?q?=F0=9F=90=9B=20fix(knowledge/climemory):=20?= =?UTF-8?q?refuse=20a=20symlinked=20store=20path=20before=20the=20file-sto?= =?UTF-8?q?re=20fallback?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A symlinked store Lstat's as a non-directory, so openMemoryStore fell through to the single-file branch and opened the symlink's real parent, which passes the SameFile check. memoryFiles already skips symlinked stores, so one reaching the open was swapped in after the listing; refuse it, as TestOpenMemoryStoreRefusesSwappedStoreDirectory expects. Signed-off-by: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- src/pkg/knowledge/climemory/memstore.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/pkg/knowledge/climemory/memstore.go b/src/pkg/knowledge/climemory/memstore.go index 4d37e92fb4..bac59b9353 100644 --- a/src/pkg/knowledge/climemory/memstore.go +++ b/src/pkg/knowledge/climemory/memstore.go @@ -34,6 +34,12 @@ func openMemoryStore(path string) (*memoryStore, error) { if err != nil { return nil, err } + // memoryFiles never lists a symlinked store, so one here was swapped in + // after the listing; refuse it before the file-store fallback below + // would otherwise open its (real) parent directory and pass SameFile. + if lfi.Mode()&os.ModeSymlink != 0 { + return nil, fmt.Errorf("%w: %s is a symlink", errNotMemoryFile, path) + } if !lfi.IsDir() { base = filepath.Dir(path) if lfi, err = os.Lstat(base); err != nil { From 09b5c551c4c4caaa94bc9e9af0d185685be1e6eb Mon Sep 17 00:00:00 2001 From: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Sun, 8 Nov 2026 11:01:18 -0500 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=90=9B=20fix(knowledge/climemory):=20?= =?UTF-8?q?cover=20store-root=20error=20paths=20so=20the=20package=20clear?= =?UTF-8?q?s=20its=2090%=20coverage=20floor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- .../climemory/memstore_errors_test.go | 141 ++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 src/pkg/knowledge/climemory/memstore_errors_test.go diff --git a/src/pkg/knowledge/climemory/memstore_errors_test.go b/src/pkg/knowledge/climemory/memstore_errors_test.go new file mode 100644 index 0000000000..2db1b03aad --- /dev/null +++ b/src/pkg/knowledge/climemory/memstore_errors_test.go @@ -0,0 +1,141 @@ +package climemory + +import ( + "errors" + "os" + "path/filepath" + "strings" + "testing" +) + +func TestOpenMemoryStoreErrors(t *testing.T) { + dir := t.TempDir() + real := filepath.Join(dir, "real") + writeFile(t, filepath.Join(real, "MEMORY.md"), "- a fact\n") + link := filepath.Join(dir, "link") + if err := os.Symlink(real, link); err != nil { + t.Skip("symlinks unavailable") + } + + if _, err := openMemoryStore(filepath.Join(dir, "missing")); !errors.Is(err, os.ErrNotExist) { + t.Fatalf("missing store: err = %v, want ErrNotExist", err) + } + if _, err := openMemoryStore(link); !errors.Is(err, errNotMemoryFile) { + t.Fatalf("symlinked store: err = %v, want errNotMemoryFile", err) + } + if _, err := openMemoryStore(filepath.Join(link, "MEMORY.md")); err == nil { + t.Fatal("file under a symlinked directory should not open as a store") + } +} + +func TestOpenMemoryStoreDirAndFileStore(t *testing.T) { + dir := t.TempDir() + file := filepath.Join(dir, "MEMORY.md") + writeFile(t, file, "- a fact\n") + + for _, path := range []string{dir, file} { + s, err := openMemoryStore(path) + if err != nil { + t.Fatalf("openMemoryStore(%s): %v", path, err) + } + b, err := s.read(file) + if err != nil || string(b) != "- a fact\n" { + t.Fatalf("read = %q, %v", b, err) + } + if err := s.Close(); err != nil { + t.Fatal(err) + } + } +} + +func TestMemoryStoreCloseNil(t *testing.T) { + var s *memoryStore + if err := s.Close(); err != nil { + t.Fatal(err) + } + if err := (&memoryStore{}).Close(); err != nil { + t.Fatal(err) + } +} + +func TestMemoryStoreOpenRefusals(t *testing.T) { + dir := t.TempDir() + outside := filepath.Join(t.TempDir(), "secret.md") + writeFile(t, outside, "secret\n") + big := filepath.Join(dir, "big.md") + writeFile(t, big, strings.Repeat("x", maxMemoryFileBytes+1)) + if err := os.Mkdir(filepath.Join(dir, "sub"), 0o755); err != nil { + t.Fatal(err) + } + writeFile(t, filepath.Join(dir, "ok.md"), "ok\n") + escape := filepath.Join(dir, "escape.md") + symlinks := os.Symlink(outside, escape) == nil + + s, err := openMemoryStore(dir) + if err != nil { + t.Fatal(err) + } + defer s.Close() + + cases := []struct { + name string + path string + }{ + {"relative path", "ok.md"}, + {"outside the root", outside}, + {"missing file", filepath.Join(dir, "missing.md")}, + {"directory", filepath.Join(dir, "sub")}, + {"over the cap", big}, + } + if symlinks { + cases = append(cases, struct{ name, path string }{"symlink out of the root", escape}) + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + f, err := s.open(tc.path, os.O_RDONLY) + if err == nil { + f.Close() + t.Fatal("open succeeded, want errNotMemoryFile") + } + if !errors.Is(err, errNotMemoryFile) { + t.Fatalf("err = %v, want errNotMemoryFile", err) + } + if _, err := s.read(tc.path); !errors.Is(err, errNotMemoryFile) { + t.Fatalf("read err = %v, want errNotMemoryFile", err) + } + }) + } + + f, err := s.open(filepath.Join(dir, "ok.md"), os.O_RDWR) + if err != nil { + t.Fatal(err) + } + f.Close() +} + +func TestMemoryStoreOpenWithoutStore(t *testing.T) { + var s *memoryStore + if _, err := s.open("x", os.O_RDONLY); !errors.Is(err, errNotMemoryFile) { + t.Fatalf("nil store: err = %v", err) + } + if _, err := (&memoryStore{}).read("x"); !errors.Is(err, errNotMemoryFile) { + t.Fatalf("empty store: err = %v", err) + } +} + +func TestReadMemoryFileErrors(t *testing.T) { + dir := t.TempDir() + path := filepath.Join(dir, "m.md") + writeFile(t, path, strings.Repeat("x", maxMemoryFileBytes+1)) + f, err := os.Open(path) + if err != nil { + t.Fatal(err) + } + if _, err := readMemoryFile(f); !errors.Is(err, errNotMemoryFile) { + t.Fatalf("oversize: err = %v, want errNotMemoryFile", err) + } + f.Close() + if _, err := readMemoryFile(f); err == nil { + t.Fatal("reading a closed file should fail") + } +} From 1341a323330853bbc7a1eace9acdf24c3f46a6cc Mon Sep 17 00:00:00 2001 From: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Sun, 8 Nov 2026 11:07:23 -0500 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=90=9B=20fix(knowledge/climemory):=20?= =?UTF-8?q?refuse=20in-root=20symlinks=20and=20stores=20reached=20through?= =?UTF-8?q?=20symlinked=20ancestors?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review findings on #17252: - memoryStore.open Lstat's each entry through the os.Root and refuses a symlink even when its target stays inside the root (for a single-file store that root is the parent, e.g. ~/.gemini/), then requires the opened file to be the same inode Lstat saw. New tests cover a GEMINI.md swapped for a sibling and an in-store symlink. - cliMemoryDrainSources resolves each store with EvalSymlinks and drops any that no longer lies under the agent's own resolved home or CODEX_HOME, so a ~/.claude/projects/ pointed at another agent's store is never drained or truncated under this agent's name; duplicate resolved stores are drained once. - Design doc: replace the never-shipped Lstat-every-component/su-exec model with the open-through-store-root rule under 'Phase 2 as shipped'. Hive-Fix-Attempt: 1/6 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- .../security-17251-climemory-nofollow.md | 1 + src/crux/agent/cli_memory_drain.go | 40 +++++++++++++- src/crux/agent/cli_memory_drain_test.go | 52 +++++++++++++++++++ src/docs/design/cli-memory-drain.md | 23 ++++++-- src/pkg/knowledge/climemory/memstore.go | 26 ++++++++-- src/pkg/knowledge/climemory/memstore_test.go | 49 +++++++++++++++++ 6 files changed, 183 insertions(+), 8 deletions(-) diff --git a/changelog.d/security-17251-climemory-nofollow.md b/changelog.d/security-17251-climemory-nofollow.md index 795109194a..2f9529d58b 100644 --- a/changelog.d/security-17251-climemory-nofollow.md +++ b/changelog.d/security-17251-climemory-nofollow.md @@ -1 +1,2 @@ - The CLI-memory drain opens every agent memory file through an `os.Root` anchored at its store and truncates it through the descriptor it read, instead of reading and `os.Truncate`-ing by path after the listing. The store belongs to the agent and the drain runs with the hive's privileges, so an agent that swapped a listed `.md` (or the store directory) for a symlink between the listing and the open could have had the hive copy any file it can read into the knowledge vault, or empty any file it can write — `TruncateDrained` counts a file with no parseable fact as "fully stored". Such a path is now reported in `kept` and the drain error, never followed ([#17251](https://github.com/hivecommons/hive/issues/17251)). +- The drain also refuses a memory-file symlink whose target stays inside the store root (for a single-file store such as `GEMINI.md` that root is its parent, e.g. `~/.gemini/`), by `Lstat`ing each entry through the root and requiring the opened file to be the same inode, and `cliMemoryDrainSources` now resolves each store with `EvalSymlinks` and drops any that leaves the agent's own home — so a `~/.claude/projects/` pointed at another agent's store is never drained or truncated under this agent's name ([#17251](https://github.com/hivecommons/hive/issues/17251)). diff --git a/src/crux/agent/cli_memory_drain.go b/src/crux/agent/cli_memory_drain.go index 20e86509fc..9765fa703d 100644 --- a/src/crux/agent/cli_memory_drain.go +++ b/src/crux/agent/cli_memory_drain.go @@ -61,8 +61,26 @@ func (m *Manager) SetCLIMemoryDrainer(fn CLIMemoryDrainer) { // cliMemoryDrainSources lists the agent's memory stores with the CLI that // owns each. Same per-agent-home-only rule as cliMemoryStorePaths: a shared // or bridged store is never drained under one agent's name. +// +// The store paths come from a Glob under the agent's home, which follows a +// symlink in any ancestor (`.claude`, `projects`, the project slug). The +// agent owns those directories and the drain runs with the hive's +// privileges, so a slug pointed at another agent's store, or at any +// hive-readable tree holding `memory/*.md`, would be drained under this +// agent's name and, with truncate, emptied. Each store is therefore resolved +// with EvalSymlinks and kept only if it still lies under the agent's own +// resolved home (or CODEX_HOME); the resolved path is what the drainer opens +// its store root at, so the listing and the open name the same directory, +// and two listings that resolve to one store are drained once. func cliMemoryDrainSources(agent *AgentProcess, backend string) []CLIMemorySource { + homes := []string{AgentHome(agent.Name, agent.UID, backend), codexHomePath(agent.Name)} + for i, h := range homes { + if r, err := filepath.EvalSymlinks(h); err == nil { + homes[i] = r + } + } var sources []CLIMemorySource + seen := map[string]bool{} for _, p := range cliMemoryStorePaths(agent, backend) { cli := cliMemoryCodex switch { @@ -71,11 +89,31 @@ func cliMemoryDrainSources(agent *AgentProcess, backend string) []CLIMemorySourc case filepath.Base(p) == "memory": cli = cliMemoryClaude } - sources = append(sources, CLIMemorySource{CLI: cli, Path: p}) + real, err := filepath.EvalSymlinks(p) + if err != nil || !pathUnderAny(real, homes) || seen[real] { + continue + } + seen[real] = true + sources = append(sources, CLIMemorySource{CLI: cli, Path: real}) } return sources } +// pathUnderAny reports whether path is one of, or strictly inside one of, +// the given directories. All arguments are expected to be symlink-resolved. +func pathUnderAny(path string, dirs []string) bool { + for _, d := range dirs { + rel, err := filepath.Rel(d, path) + if err != nil { + continue + } + if rel != ".." && !strings.HasPrefix(rel, ".."+string(filepath.Separator)) { + return true + } + } + return false +} + // drainCLIMemoryForKick runs the installed drainer over the agent's memory // stores when agents.cli_memory.drain_to_knowledge is set. It is called // before clearCLIMemoryForKick so the facts survive the wipe, and it is diff --git a/src/crux/agent/cli_memory_drain_test.go b/src/crux/agent/cli_memory_drain_test.go index eef79b1e50..c62b533e6d 100644 --- a/src/crux/agent/cli_memory_drain_test.go +++ b/src/crux/agent/cli_memory_drain_test.go @@ -46,6 +46,58 @@ func TestCLIMemoryDrainSources_ClassifiesStoresByCLI(t *testing.T) { } } +func TestCLIMemoryDrainSources_DropsStoresReachedThroughSymlinkedAncestors(t *testing.T) { + withSharedAgentHome(t) + prev := codexHomePrefix + codexHomePrefix = filepath.Join(t.TempDir(), ".codex-") + t.Cleanup(func() { codexHomePrefix = prev }) + + mk := func(p string) { + t.Helper() + if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(p, []byte("- fact\n"), 0o644); err != nil { + t.Fatal(err) + } + } + home := interactiveHomePath("scanner") + projects := filepath.Join(home, ".claude", "projects") + // An honest store, and a second slug that is a symlink to it: inside the + // agent's own home, so it is drained once, at its resolved path. + own := filepath.Join(projects, "-data-agents-scanner", "memory") + mk(filepath.Join(own, "MEMORY.md")) + if err := os.Symlink(filepath.Join(projects, "-data-agents-scanner"), filepath.Join(projects, "-alias")); err != nil { + t.Skip("symlinks unavailable") + } + // A slug pointed at another agent's store, and a Gemini store whose + // .gemini directory is a symlink out of the home: both must be dropped. + victim := filepath.Join(interactiveHomePath("victim"), ".claude", "projects", "-data-agents-victim", "memory") + mk(filepath.Join(victim, "MEMORY.md")) + if err := os.Symlink(filepath.Dir(victim), filepath.Join(projects, "-data-agents-victim")); err != nil { + t.Fatal(err) + } + outside := t.TempDir() + mk(filepath.Join(outside, geminiMemoryFileName)) + if err := os.Symlink(outside, filepath.Join(home, ".gemini")); err != nil { + t.Fatal(err) + } + + ap := &AgentProcess{Name: "scanner", UID: 1001, Config: config.AgentConfig{Backend: "claude"}} + got := cliMemoryDrainSources(ap, "claude") + wantOwn, _ := filepath.EvalSymlinks(own) + var paths []string + for _, s := range got { + paths = append(paths, s.Path) + if s.Path != wantOwn || s.CLI != cliMemoryClaude { + t.Errorf("source %+v reaches outside the agent's resolved home (want only %s)", s, wantOwn) + } + } + if len(got) != 1 { + t.Fatalf("sources = %v, want the honest store once", paths) + } +} + func TestDrainCLIMemoryForKick_RunsDrainerBeforeWipeOnlyWhenEnabled(t *testing.T) { withSharedAgentHome(t) prev := codexHomePrefix diff --git a/src/docs/design/cli-memory-drain.md b/src/docs/design/cli-memory-drain.md index f10e9b72aa..6b9d5406f9 100644 --- a/src/docs/design/cli-memory-drain.md +++ b/src/docs/design/cli-memory-drain.md @@ -139,10 +139,12 @@ A file whose drain fails part-way is left untouched and retried on the next drain. That is safe because dedup makes the drain idempotent. Reads and the truncate follow the same rules as #17040's -`cliMemoryStores`. Only stores private to the agent are drained; every path -component is checked with `Lstat`, so a planted symlink is never followed. The -truncate and remove run as the agent through the same `su-exec` path as -`cliMemoryRemover`. A store that is still shared (a legacy whole-directory +`cliMemoryStores`. Only stores private to the agent are drained. The plan +was to `Lstat` every path component and to truncate and remove as the agent +through the same `su-exec` path as `cliMemoryRemover`; as shipped, the drain +runs in the hive process and instead opens every memory file through an +`os.Root` anchored at its store (see "Phase 2 as shipped" below and #17251). +A store that is still shared (a legacy whole-directory bridge, a UID 0 agent, `HIVE_SHARED_AGENT_HOME=1`) is **not** drained. It cannot be attributed to one agent, and truncating it would wipe other agents' memory. It is logged once per agent, exactly like `clear_on_kick`, and is left @@ -418,6 +420,19 @@ shipped code differs from the plan above, the shipped code is authoritative: `$CLAUDE_CONFIG_DIR/projects//memory/`). Its request carries `on_write`, which the hive answers as a no-op unless the agent has `drain_on_write`. +- **Open through the store root (#17251).** The drain never reads or + truncates a memory file by path. Each `Source` is resolved with + `EvalSymlinks` and dropped unless it still lies under the agent's own + resolved home (a slug or `.claude` directory symlinked at another agent's + store is not drained). The store is then opened as an `os.Root` (the parent + directory for a single-file store such as `GEMINI.md`), checked with + `SameFile` against what `Lstat` saw, and every listed file is `Lstat`ed + through the root — a symlink entry is refused even when its target stays + inside the root — opened through it, required to be a regular file within + the size cap and the same inode `Lstat` reported, and `TruncateDrained` + truncates through that read descriptor. A path swapped for a symlink + between the listing and the open lands in `kept` and the drain error; + nothing outside the store is ever read or emptied. - **Keys.** The new key is `agents.cli_memory.drain_on_write`, fleet-wide and per agent, and it has no effect without `drain_to_knowledge`. - **Seeding.** Not through `seedJSONFile`, which never touches an existing diff --git a/src/pkg/knowledge/climemory/memstore.go b/src/pkg/knowledge/climemory/memstore.go index bac59b9353..044d953108 100644 --- a/src/pkg/knowledge/climemory/memstore.go +++ b/src/pkg/knowledge/climemory/memstore.go @@ -73,9 +73,15 @@ func (s *memoryStore) Close() error { } // open opens path, a file memoryFiles listed under this store, inside the -// store root (a symlink that leaves the root is refused by os.Root) and -// checks the open descriptor is a regular file within the size cap. flag is -// os.O_RDONLY or os.O_RDWR; the caller closes the file. +// store root and checks the open descriptor is a regular file within the +// size cap. os.Root only refuses a symlink whose target leaves the root; one +// that stays inside it (another file of the same memory dir, or for a +// single-file store any sibling under its parent such as ~/.gemini/) would +// be followed and fstat would report the target as regular. So the entry is +// Lstat'ed through the root first and refused when it is a symlink, and the +// opened file must be the very inode Lstat saw, which closes the window +// between the two calls. flag is os.O_RDONLY or os.O_RDWR; the caller closes +// the file. func (s *memoryStore) open(path string, flag int) (*os.File, error) { if s == nil || s.root == nil { return nil, fmt.Errorf("%w: %s has no open store", errNotMemoryFile, path) @@ -84,6 +90,16 @@ func (s *memoryStore) open(path string, flag int) (*os.File, error) { if err != nil { return nil, fmt.Errorf("%w: %s: %v", errNotMemoryFile, path, err) } + lfi, err := s.root.Lstat(rel) + if err != nil { + return nil, fmt.Errorf("%w: %s: %v", errNotMemoryFile, path, err) + } + if lfi.Mode()&os.ModeSymlink != 0 { + return nil, fmt.Errorf("%w: %s is a symlink", errNotMemoryFile, path) + } + if !lfi.Mode().IsRegular() { + return nil, fmt.Errorf("%w: %s is %s", errNotMemoryFile, path, lfi.Mode().Type()) + } f, err := s.root.OpenFile(rel, flag, 0) if err != nil { return nil, fmt.Errorf("%w: %s: %v", errNotMemoryFile, path, err) @@ -97,6 +113,10 @@ func (s *memoryStore) open(path string, flag int) (*os.File, error) { f.Close() return nil, fmt.Errorf("%w: %s is %s", errNotMemoryFile, path, fi.Mode().Type()) } + if !os.SameFile(lfi, fi) { + f.Close() + return nil, fmt.Errorf("%w: %s changed while being opened", errNotMemoryFile, path) + } if fi.Size() > maxMemoryFileBytes { f.Close() return nil, fmt.Errorf("%w: %s is %d bytes, over the %d cap", errNotMemoryFile, path, fi.Size(), maxMemoryFileBytes) diff --git a/src/pkg/knowledge/climemory/memstore_test.go b/src/pkg/knowledge/climemory/memstore_test.go index f96340ea2f..51e3efed6b 100644 --- a/src/pkg/knowledge/climemory/memstore_test.go +++ b/src/pkg/knowledge/climemory/memstore_test.go @@ -111,3 +111,52 @@ func TestMemoryStoreOpenKeepsSingleFileStoreWorking(t *testing.T) { t.Fatalf("read = %q, %v", b, err) } } + +func TestTruncateDrainedNeverFollowsSymlinkToSiblingInsideStoreRoot(t *testing.T) { + // A single-file store opens through its parent, so an in-root symlink + // (GEMINI.md -> settings.json in the same ~/.gemini/) passes os.Root's + // escape check and fstat sees the target as a regular file. The entry + // itself must be refused. + home := filepath.Join(t.TempDir(), ".gemini") + sibling := filepath.Join(home, "settings.json") + writeFile(t, sibling, "{\"token\": \"not-a-fact\"}\n") + gemini := filepath.Join(home, "GEMINI.md") + writeFile(t, gemini, "# memory\n") + swapForSymlink(t, gemini, sibling) + + d := &Drainer{Dir: t.TempDir(), Now: fixedNow} + truncated, kept, err := d.TruncateDrained([]Source{{CLI: CLIGemini, Path: gemini}}) + if len(truncated) != 0 { + t.Fatalf("truncated %v through an in-root symlink", truncated) + } + if len(kept) != 1 || kept[0] != gemini { + t.Fatalf("kept = %v, want the swapped path", kept) + } + if err == nil || !errors.Is(err, errNotMemoryFile) { + t.Fatalf("err = %v, want errNotMemoryFile", err) + } + if b, _ := os.ReadFile(sibling); !strings.Contains(string(b), "not-a-fact") { + t.Fatalf("in-root symlink target was truncated: %q", b) + } +} + +func TestMemoryStoreOpenRefusesInRootSymlink(t *testing.T) { + store := filepath.Join(t.TempDir(), "memory") + target := filepath.Join(store, "notes.md") + writeFile(t, target, "- a sibling fact\n") + link := filepath.Join(store, "MEMORY.md") + if err := os.Symlink("notes.md", link); err != nil { + t.Skip("symlinks unavailable") + } + s, err := openMemoryStore(store) + if err != nil { + t.Fatal(err) + } + defer s.Close() + if _, err := s.read(link); err == nil || !errors.Is(err, errNotMemoryFile) { + t.Fatalf("read through in-root symlink: err = %v, want errNotMemoryFile", err) + } + if b, err := s.read(target); err != nil || !strings.Contains(string(b), "sibling fact") { + t.Fatalf("regular sibling read = %q, %v", b, err) + } +}