Skip to content

fix(memory): GHSA-48j5-hj63-c82c - a race on saveGraph's temporary file can change the permissions of a file outside the memory path - #5676

Merged
cliffhall merged 2 commits into
v2/mainfrom
v2/fix/ghsa-48j5-hj63-c82c
Nov 27, 2026
Merged

cliffhall merged 2 commits into
v2/mainfrom
v2/fix/ghsa-48j5-hj63-c82c

Conversation

@cliffhall

Copy link
Copy Markdown
Member

Closes #5668

Fix for GHSA-48j5-hj63-c82c. It was written and reviewed in the advisory's private fork, where a subagent reviewer stood in for Copilot (which cannot review a private fork) until a clean round. Per the advisory flow this PR gets no Copilot review. The private fork has no Actions, so this PR's CI is the first CI run on the change; the full npm run local:gate passed on this exact head and on a rollup of all eight advisory fixes.

Fix for GHSA-48j5-hj63-c82c (private fork; links no public issue).

Description

KnowledgeGraphManager.saveGraph (src/memory/index.ts) created its random sibling temp file with fs.writeFile(tmp, …) (flag "w", follows symlinks), then fs.chmod(tmp, mode) and fs.rename(tmp, graph) by path. Anyone who can write the graph's directory could swap the temp name for a symlink between those calls, so the server chmodded (to the graph's mode) a file outside MEMORY_FILE_PATH, and the rename then left the graph path a symlink to it. A link planted at the name before creation was also written through.

The save now:

  1. creates the temp file with fs.open(tmp, "wx", mode) (O_CREAT | O_EXCL): it fails with EEXIST rather than open anything already at the name, a symlink included;
  2. writes and chmods it only through that handle (handle.writeFile, handle.chmod), so both reach the inode this call created regardless of what happens to the name;
  3. while the handle is still open (so the inode number cannot be reused), compares the handle's dev/ino (bigint) with an lstat of the name, then closes it; if the name no longer names that inode, the save is refused (Refused to save: … was replaced while the graph was being written to it. Nothing was saved.), the stray entry is unlinked (unlinking a link never touches its target), and the graph file is left as it was;
  4. renames as before.

Mode preservation from #4827/#5013 (create with the existing mode, then restore bits the umask dropped) and cleanup on failure are unchanged; on failure the handle is also closed.

Server Details

  • Server: memory (@modelcontextprotocol/server-memory)
  • Changed: saveGraph in src/memory/index.ts (every write tool persists through it); README "Saves are atomic" paragraph gains one sentence (the temp file is always newly created by the save). No tool, schema or option changes.

Motivation and Context

GHSA-48j5-hj63-c82c (verified, medium). Affects stdio and Streamable HTTP alike.

How Has This Been Tested?

Regression tests (src/memory/__tests__/persistence.test.ts, new the temp file's name block, protocol-level through an in-memory MCP client, POSIX only since symlinks need privileges on Windows). Each hooks the call that creates the temp file (fs.open or fs.writeFile, so the hook fires on both the old and the new code) and, against an outside 0600 file:

  • never writes through a link already at the temp name: plants a symlink at the name just before creation. v2/main: fails (save succeeds through the link). Branch: passes (tool error EEXIST; outside file still 0600 with its content; graph path a regular file with its previous content; no stray entry).
  • refuses the save when the temp name is replaced after it is created: unlinks the name and puts a symlink there right after creation. v2/main: fails (save succeeds; outside file chmodded; graph path becomes a symlink). Branch: passes (tool error was replaced; same assertions as above).
  • saves normally when nothing else touches the temp name: no-swap control; save succeeds, graph keeps 0644, outside file untouched.

Three existing tests that simulated a write failure / asserted the creation mode via fs.writeFile now do so via the temp file's fs.open handle (atomic-save.test.ts "write fails midway", persistence.test.ts "keeps serving after a failed save" and "keeps a hardened graph file's mode across a save", which now expects fs.open(<tmp>, "wx", <mode>)).

Coverage: memory suite 342 tests pass; index.ts 99.11 / 96.69 / 97.76 / 99.07 (stmts / branches / funcs / lines), every file ≥ 90.

Gate: npm run local:gate EXIT=0 on the head commit.

Inspector CLI (@modelcontextprotocol/inspector@2.12.0 --cli, stdio, built branch): created a new graph with create_entities, chmod 600 it, then create_entities once with --protocol-era legacy and once with --protocol-era modern: both exit 0, no isError; read_graph (modern) returns Alice, E-legacy and E-modern; the file is still -rw-------; the directory holds only memory.jsonl. A before/after pair through the Inspector shows nothing different, since the race cannot be driven from a client; the before/after is in the regression tests above. The advisory's inotify reproducer is Linux-only and was not run here (macOS).

LLM client: not run. The change has no surface an LLM client can exercise differently (no tool, schema or result change on the normal path); the observable difference needs a concurrent directory writer, covered by the tests.

Breaking Changes

None. No client configuration changes. A save whose temp name is replaced mid-save now fails with a tool error instead of succeeding.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature
  • Breaking change
  • Documentation update

Checklist

  • I have read the MCP Protocol Documentation (not applicable to the protocol surface: persistence-only change)
  • My changes follow MCP security best practices
  • I have updated the server's README accordingly
  • I have added a changeset (npm run changeset) if this changes what a TypeScript server publishes (patch, text fix(memory): GHSA-48j5-hj63-c82c)
  • I have tested this with an LLM client (not run: see above)
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have documented all environment variables and configuration options (not applicable: none added)

Additional context: what this leaves open

  • A narrow window remains between the lstat check and rename(2): a directory writer who replaces the name in that window still gets their entry renamed onto the graph path. No chmod or write is applied through it, and placing an entry at the graph path is something a writer of that directory can already do directly (rename over memory.jsonl), so it grants nothing new. Closing it fully would need directory-handle operations (renameat with a verified source), which Node does not expose. No follow-up card proposed; noting it for the advisory text.
  • The general guidance stands: the graph directory should not be writable by untrusted accounts.

🤖 Generated with Claude Code

cliffhall and others added 2 commits November 27, 2026 00:26
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: cliffhall <cliff@futurescale.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: cliffhall <cliff@futurescale.com>
@cliffhall cliffhall added the v2 label Nov 27, 2026
@changeset-bot

changeset-bot Bot commented Nov 27, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: e62a4b0

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@modelcontextprotocol/server-memory Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions

Copy link
Copy Markdown
Contributor

memory: MCP interface diff

Base: 90e6a48c8c90 (Merge pull request #5652 from modelcontextprotocol/v2/feat/5651-everything-events)
Head: the build in this checkout, at 785308001309 (Merge e62a4b0 into 90e6a48)

Diffed because it was named (--server).

✅ No interface changes detected.

Tools Prompts Resources Resource templates
Base 9 0 1 0
Head 9 0 1 0

Updated by this run for e62a4b0dc0e91aa0e16503d16ae5d34e8db1061c.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[GHSA-48j5-hj63-c82c] - server-memory: a race on saveGraph's temporary file can change the permissions of a file outside the memory path

1 participant