Repository navigation
π fix(knowledge/climemory): never follow a symlinked ancestor when opening a memory store - #17259
Conversation
β¦ening a memory store cliMemoryDrainSources resolves each store with EvalSymlinks and keeps it only under the agent's own home, but openMemoryStore then Lstat'ed the store path and opened it with os.OpenRoot, both of which follow a symlink in any ancestor component. The agent owns every directory from its home down, so swapping `.claude`, `projects` or a project slug for a symlink after the listing still had the hive drain, and with truncate empty, another agent's store under this agent's name. The store is now reached by opening its directories one component at a time from the filesystem root: each entry is Lstat'ed through its parent root, refused when it is a symlink or not a directory, and the opened directory must be the inode Lstat saw. Source.Path is already symlink-free when the crux side lists it, so any symlink found on the way was swapped in afterwards. Fixes hivecommons#17256 Hive-Run: hivecommons#17256 Hive-Plan: reviewer-backlog-security-17256 Hive-Spec: climemory-drain#store-open-no-follow Signed-off-by: Karibhou <Karibhou@users.noreply.github.com>
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
There was a problem hiding this comment.
Correctness review (head d1f774b) β the no-follow component walk is sound: Root.OpenRoot chains names to the clean directory (memstore.go:79-82), so open()'s filepath.Rel(s.root.Name(), path) (memstore.go:156) still resolves for both directory and GEMINI.md stores; parent.Lstat(name) keeps ErrNotExist for a missing store (memstore_errors_test.go:20); and crux hands over absolute, EvalSymlinks-resolved paths (src/crux/agent/cli_memory_drain.go:92-97), so the new IsAbs refusal has no live caller impact.
One advisory (non-blocking, test portability):
openDirNoFollownow refuses any symlinked ancestor, including operator-level ones. CI is ubuntu so this is green, but every existing test that opens a store under a rawt.TempDir()β e.g.memstore_test.go:142-145,memstore_test.go:84-92,memstore_errors_test.go:12-26β will fail on macOS, where$TMPDIRlives under/var β /private/var. The new test already guards this withfilepath.EvalSymlinks(t.TempDir())(memstore_test.go:106); the older tests need the same, or a shared helper:
func realTempDir(t *testing.T) string {
t.Helper()
dir, err := filepath.EvalSymlinks(t.TempDir())
if err != nil {
t.Fatal(err)
}
return dir
}β hive: agent=adjudicator backend=copilot model=claude-fable-5.1 copilot=1.0.88
|
Hive status β updated in place; edits do not notify. Full history is in the Activity Monitor. Review findings from Read the diff against What I verified:
One non-blocking note: Review findings from Read What I verified:
One documented tradeoff worth a maintainer's eye rather than a defect: the hive now needs read permission on every ancestor from stage 095-pre-merge-soak β 090-ready-for-human-merge: untrusted-author-role Evidence bundle `#17259f3fbdfc` sha256:b2b4f630a4c6ec384af571d8d16df3a63da48192e9f2f291cb8c451ab48d6fbf β download from the dashboard or `hivectl review evidence #17259`. |
Open PR overlap checkBase branch: Conflicts are checked with every PR merged onto the current tip of No conflicting open PRs or same-file overlaps were found. |
openMemoryStore refuses every symlinked ancestor, so on macOS, where $TMPDIR sits under /var -> /private/var, stores created under a raw t.TempDir() could not be opened. Resolve TMPDIR once in TestMain so all tests in the package use a symlink-free path. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HRH6kmtNmwccuH34XZzoUp Signed-off-by: Karibhou <157208370+Karibhou@users.noreply.github.com>
|
Review follow-up at
No production code changed. π Hive Agent: |
There was a problem hiding this comment.
Looks correct to me. I read the diff against the v5 tree and both callers; no defects found.
What I verified
- Both callers (
src/pkg/knowledge/climemory/drain.go:147,legacy.go:71) keep the existing fail-closed shape: anopenMemoryStoreerror is appended to the errors and, inTruncateDrained, the listed files go tokept. Nothing new is truncated on failure. memoryStore.open(memstore.go:88) computesfilepath.Rel(s.root.Name(), path), so the walk must return a root whoseName()equals the store directory. It does:Root.OpenRootnames the childjoinPath(parent.Name(), name)(Goos/root_unix.go:78), so walking from/yields exactly the cleaneddir. The single-file (GEMINI.md) branch returns the parent root, same as the oldfilepath.Dir(path)base.- The
LstatβOpenRootβSameFile(".")sequence inopenChildDirNoFollowcloses the race the comment describes: a symlink swapped in between the calls has a different inode from the directoryLstatreported, so it is refused even whenos.Rootwould have followed it inside the root. - Descriptor handling: every early return in
openDirNoFollow/openMemoryStorecloses the root it holds; the only root that escapes is the one placed inmemoryStore. - The regression test is a real red/green:
memoryFiles(drain.go:368)Lstats only the store path, which is still a real directory behind the symlinked slug, so onv5the store would open and the victim'sMEMORY.mdwould be truncated. CI'stest (rest β¦),coverageandpatch-coverageare green on this head.
One suggestion (not a defect)
src/docs/design/cli-memory-drain.md:427-429 still says the store "is then opened as an os.Root β¦ checked with SameFile against what Lstat saw". That remains true but no longer describes the ancestor walk this PR adds. A short clause there ("reached one component at a time from /, refusing any symlinked ancestor") would keep the design doc matching the code, since the Tradeoffs section of this PR (operator /data symlink now fails closed) is exactly the kind of thing an operator would look for in that doc.
I have not approved or changed anything; this is a comment only.
β hive: agent=reviewer-2 backend=copilot model=claude-fable-5.1 copilot=1.0.88
There was a problem hiding this comment.
Read the new openMemoryStore / openDirNoFollow / openChildDirNoFollow against both callers and the crux side that produces the paths. Looks correct to me.
- Premise holds.
crux/agent/cli_memory_drain.go:75-99(cliMemoryDrainSources) resolves each store withfilepath.EvalSymlinksand keeps it only under the resolved agent home, andsrc/cmd/hive/boot_cli_memory_drain.go:82-85passes those paths through unchanged. So every ancestor is a real directory at listing time, and walking from/with per-componentLstat+OpenRoot+SameFile(memstore.go,openDirNoFollow/openChildDirNoFollow) only refuses what was swapped after the listing β it will not reject a legitimate store. - The test fails without the fix. Pre-change,
os.OpenRoot(store)on a store whose<slug>ancestor is a symlink follows it;legacy.go:truncateIfStoredreads# another agent's index\n,ParseForyields zero facts,allStoredis vacuously true, andTruncate(0)fires. With the fix,TruncateDrained(legacy.go:71-75) lands in thekept/error branch exactly as the test asserts. - Single-file stores keep working: the
defaultarm returns the no-follow-opened parent as root, matching the previousfilepath.Dir(path)behaviour. TestMainresolvingTMPDIRis necessary for macOS (/var -> /private/var) now that every ancestor is checked; harmless on Linux CI.- Changelog fragment uses the
securitycategory, whichchangelog.d/README.md:17-18lists.
One non-blocking note: the SameFile check after parent.OpenRoot(name) closes the TOCTOU window between Lstat and open, so I don't see a remaining race. Nothing to request.
β hive: agent=reviewer backend=copilot model=claude-fable-5.1 copilot=1.0.88
|
Ready for a human merge: verdict approved @f3fbdfc, 61/61 checks green, DCO yes, soak 10m0s elapsed. Blocked only by The hive never approves on your behalf: |
|
Merged by hive contributor-lane auto-merge: CI green for β₯10m, reviewer verdict approve, @Karibhou holds the trusted tier in this hive's contributor registry. |
Top-up after hivecommons#17265: brings v5.202.0 and the maintainer-notes feature (hivecommons#17245), the crux checkout cap / private-repo auth / kubejob network hardening (hivecommons#17269) and the climemory symlink fix (hivecommons#17259) onto v6. Conflicts: imports/allowlist in main.go, server.go and dashboard_import_allowlist.txt (both sides); import_count_test ceiling 66 -> 67 for pkg/maintainernotes; tracker-filter.js keeps v6's finisher sort and v5's maintainer-notes UI; CHANGELOG keeps both lines' sections (v5.202.0 under v6.0.1); api-reference pages take v6 and add the /api/notes rows, citations resynced; index.html regenerated. Signed-off-by: Andrew Anderson <andy@clubanderson.com>
What
openMemoryStorenow reaches the store directory by opening it one path component at a time from the filesystem root (openDirNoFollow/openChildDirNoFollow). For each component:Lstatthrough the parentos.Root,Root.OpenRootand requiresos.SameFilewith theLstatresult.os.Rootstill follows a symlink whose target stays inside the root, so this check catches a swap between the two calls.Single-file stores (
GEMINI.md) still open through their parent directory, and the path's last component is still refused if it is a symlink. A relativeSource.Pathis now refused.Why
#17252 added
EvalSymlinksplus a "stays under the agent's resolved home" check incliMemoryDrainSources. That covers the static case in the finding. The drainer still opened the store by path, though:os.Lstat(path)andos.OpenRoot(path)both follow symlinks in ancestor directories. The agent owns everything from its home down. After the listing, it can rename~/.claude/projects/<slug>away and put a symlink to another agent'sprojects/<slug2>in its place. The store path stillLstats as a real directory, theSameFilecheck passes, and the hive drains that store under the requesting agent's name. Withtruncate: trueit also empties the store.Source.Pathis already fully resolved when crux lists it, so any symlink found on the way to the store was added after the listing. The walk refuses it. This is the same no-follow component walkmkdirAllNoFollow(crux/agent/manager_homes.go) uses for inference homes.Tradeoffs
/data/home/.claude/projects, not resolved by the caller) now fails closed if an operator has made/dataa symlink. In that case the store is reported inkeptand the drain error; nothing is followed.os.Rootalready needs that for nested store entries.Verification
TestTruncateDrainedNeverFollowsSymlinkedAncestorOfStore. It swaps the project slug for a symlink to another agent's project after the listing, then asserts that nothing is truncated, the file is reported inkept, and the error iserrNotMemoryFile.go testlocally; CI is the verdict.upstream/v5and patchedmemstore.goand ran them against the same filesystem:MEMORY.mdwas truncated to"".β¦/-data-agents-scanner is a symlink(errNotMemoryFile).ErrNotExist.gofmt -lis clean.Fixes #17256
β hive: backend=omp model=anthropic/claude-opus-5-5
π Hive Agent:
contributor| SHA:d1f774b27