Repository navigation
π fix(knowledge/climemory): open memory files through their store root, never by path - #17252
Conversation
β¦t, never by path 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>
|
[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 |
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. |
β¦file-store fallback 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>
|
Hive status β updated in place; edits do not notify. Full history is in the Activity Monitor. hive/awaiting-ci: pushed a test-only commit covering the new store-root error paths (coverage was 89.2% vs 90% floor). CI pending β sweep will merge when green. π Hive Agent: β hive: agent=scanner backend=copilot model=claude-fable-5.1 copilot=1.0.88 Review findings from Conclusion: looks correct to me. The TOCTOU the changelog describes is real at the base, and the fix closes it for directory stores; I traced the one path I thought might regress (spool retries) and it does not. What I verified against
One open question (not a defect) For a single-file store the root is the file's parent directory ( The regression tests fail without the fix (they swap the path inside the listing seam and assert Review findings from Read the diff against the tree at What I verified:
Open question (not a defect, outside #17251's scope): Review findings from Security perspective β approve. Read at head b22e0a7. The three contract items from #17251 are implemented: Out-of-scope (pre-existing, for backlog): the store path itself is still resolved by following symlinks in ancestor components. Hive filed review findings that do not block this PR as backlog items:
Cap: 3 backlog item(s) per PR. Review findings from Correctness review (head Advisory β Race-free fix if you want to close it fully: before Everything else verified: the store Review findings from Intent alignment vs #17251 β read at head b22e0a7. The diff covers all three acceptance boxes in #17251 with no unrelated scope: Blocker (in-scope, CI): the Advisory, not blocking: for single-file stores (Gemini |
There was a problem hiding this comment.
Correctness review at head 8a49d04 (read diff + drain.go, spool.go, legacy.go, memstore.go, crux/agent/cli_memory_drain.go, crux/agent/cli_memory.go; package tests could not be run in this pod, CI still pending).
The fix does what #17251 asks: every read goes through memoryStore.open (memstore.go:78-104), TruncateDrained truncates the fd it read (legacy.go:106), the store root is SameFile-checked (memstore.go:60-63), and the nil-store spool path is only reachable with a store set (drain.go:162 is the sole spool(f, nil) caller; spooled() entries never call read). The seam listMemoryFiles is a package global, but no test in the package uses t.Parallel, so it is safe.
Two advisory notes, neither blocking:
-
Residual TOCTOU above the store (out-of-scope, low).
openMemoryStoreanchors atsrc.Pathonly (memstore.go:33-63). Components above it β~/.claude/projects/<slug>β are agent-owned too;Lstat,OpenRootandSameFileall follow a symlink swapped in there consistently, so the root lands wherever<slug>now points. The target must containmemory/<listed-name>.md, which rules outhive.yaml/vault entries, but another agent's~/.claude/projects/<slug>/memory/MEMORY.mdfits (slugs are cwd-derived and predictable). Cross-agent read/truncate of memory files is far smaller than the issue's primitive and the issue's recommendation did not call for it, so this is backlog: anchor the root at the agent home (AgentHome,crux/agent/cli_memory.go:179) or verify each intermediate component. -
Single-file store under a symlinked parent now errors each kick (in-scope, low). For
~/.gemini/GEMINI.md,openMemoryStorefalls back toLstat(filepath.Dir(path))and refuses a symlinked parent witherrNotMemoryFile(memstore.go:44-51).memoryFiles(drain.go:376-381) andcliMemoryStorePathsboth accept that layout, so a bridged~/.geminithat drained before 8a49d04 now surfaces as ares.Errorsentry (andout.Errinboot_cli_memory_drain.go:92, which also disables truncation) on every kick instead of being silently skipped like a symlinked directory store. Consistent intent (per-agent-home only), just noisier; consider returning nil-store/skip for that case asmemoryFilesdoes.
β hive: agent=adjudicator backend=copilot model=claude-fable-5.1 copilot=1.0.88
Review follow-ups (below
|
β¦ge clears its 90% coverage floor Signed-off-by: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com>
β¦d through symlinked ancestors 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/<slug> 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>
There was a problem hiding this comment.
Read the diff against the tree at 1341a32, including the callers (cliMemoryStorePaths, drainCLIMemoryOnRequest, claudeProjectSources, drainRun.spool). The fix does what the changelog says and I found no defect.
What I verified:
- Sources are server-derived. Both drain entry points (
src/crux/agent/cli_memory_drain.go:138and:195) buildSourcesfromcliMemoryDrainSources; nothing in the on-write request supplies a path. So theEvalSymlinks+pathUnderAnyfilter is applied to every store the drainer ever opens. filepath.RelinmemoryStore.openis sound.memoryFiles(drain.go:389) yields paths fromWalkDir(path, β¦), i.e. joined undersrc.Path, and for a file store returnssrc.Pathitself whileopenMemoryStoreanchors the root at its parent β sorelis always a clean in-root path andos.Rootcan enforce it.- The in-root-symlink gap is closed.
os.Rootalone allows a symlink whose target stays inside the root;openLstats through the root first, refusesModeSymlink, and requiresSameFilebetween the Lstat and the opened fd (memstore.go), which also covers a swap between the two calls.TestMemoryStoreOpenRefusesInRootSymlink/β¦SiblingInsideStoreRootpin it. - Spool path. Spool-retry
memFiles carrystore == nil, butspool()returns early onf.spoolDir != ""(spool.go:44) before reachingf.store.read, and their content is read from the spool atdrain.go:168. The onlyspool(f, nil)call (drain.go:162) is for freshly listed files, which always have a store. go 1.26insrc/go.mod, soos.Rootis available.
One behavioural nuance worth a maintainer's glance (info, not a defect): cliMemoryDrainSources now emits the resolved store path, and claudeProjectSources (cli_memory_drain.go:216) matches the on-write cwd slug against Base(Dir(s.Path)). If an agent's own project slug directory is itself a symlink to another slug inside the same home (the -alias case in the new test), on-write drains for that cwd no longer match and the store is only drained at session end. That seems like the intended trade for "two listings that resolve to one store are drained once", but it is a change from before.
β hive: agent=reviewer backend=copilot model=claude-fable-5.1 copilot=1.0.88
There was a problem hiding this comment.
Read against the v5 tree (src/pkg/knowledge/climemory, src/crux/agent/cli_memory_drain.go) β this looks correct to me.
What I verified:
- Every agent-owned store read/write in the package now goes through
memoryStore(drain.go:180βstore.read,spool.go:50βstore.read,legacy.goβtruncateIfStoredon anos.Root-opened descriptor). Theos.ReadFile/os.Opencalls that remain (drain.go:156,drain.go:323hashFromFrontmatter,spool.go:96/130) operate on the hive-owned vault and spool dirs, not on paths the agent controls, so they are out of scope for this fix. memFile.storeis nil only for entries fromspooled(); the onespool(f, nil)call (drain.go:150) runs overfiles, which all carry a store, andspoolreturns early onf.spoolDir != ""before reachingstore.read. No nil-store read path.memstore.goopen:Root.Lstatβ refuse symlink βRoot.OpenFileβSameFile(lfi, fi)closes the in-root symlink gap thatos.Rootalone would not (a link to a sibling inside~/.gemini/would otherwise be followed).openMemoryStorelikewise refuses a store path that became a symlink, including the single-file-store case where the parent would otherwise passSameFile.cliMemoryDrainSources: resolving both the home and each store withEvalSymlinksand gating onpathUnderAnymeansclaudeProjectSources(cli_memory_drain.go:178) still matches on the slug basename β resolving ancestors doesn't change the slug directory's name, and an in-home alias collapses to the real slug viaseen. No regression in the cwd-narrowing path.
Tests cover the alias-inside-home, slug-to-victim and .gemini-out-of-home cases, and the listMemoryFiles seam lets the swap-after-listing race be exercised deterministically. CI is green; tide is waiting on a human approval.
β hive: agent=reviewer backend=copilot model=claude-fable-5.1 copilot=1.0.88
|
Thank you for your contribution! Your PR has been merged. We'd love to hear how your experience was: share feedback |
Security Fix
The CLI-memory drain lists agent-owned memory stores safely (
Lstat, symlinks dropped) but then acted on the listed paths by name:os.ReadFileinDrain/spool, andos.ReadFile+os.TruncateinTruncateDrained. Both follow a symlink the agent swaps in after the listing, and the agent controls the timing (on-demand drain via the drop-box watcher, always withtruncate: true; the kick-path drain on every kick). Because the hive has more privilege than the agent UID, that was a read primitive into the knowledge vault and β since a file with no parseable fact counts as "fully stored" β a truncate primitive over any hive-writable file.Why this shape: every file is now opened through an
os.Rootanchored at its store (and the opened root is checked against the store'sLstatwithos.SameFile, so a swapped store directory is refused too), fstat-checked as a regular file under the size cap, read through aLimitReader, andTruncateDrainedtruncates the descriptor it read from rather than reopening by path.os.Rootrather thanO_NOFOLLOWkeeps it portable and also covers intermediate components;climemorygets its own small helper instead of importingpkg/github's drop-box reader so the knowledge package does not grow a dependency on the GitHub package. A rejected path lands inkeptand in the returned error, so no fact is ever lost by the change.listMemoryFilesis a one-line seam so the regression tests can hand the drain a listing the store no longer matches β the only way to exercise the post-listing swap deterministically.Fixes #17251
Filed by sec-check agent (ACMM L6 β full mode)
β hive: agent=sec-check backend=copilot model=claude-fable-5.1 copilot=1.0.88