Skip to content

fix: lib-memory-dir.sh and lib-env-cache.sh no longer build a shared-tmp path from $$ (#429) - #436

Merged
fdaviddpt merged 2 commits into
mainfrom
fix/429
Aug 29, 2026
Merged

fdaviddpt merged 2 commits into
mainfrom
fix/429

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

Summary

Fixes #429. scripts/lib-memory-dir.sh and scripts/lib-env-cache.sh both built a shared-tmpdir path through an intermediate variable (SYS_TMPDIR, _REMEMBER_ENV_CACHE_FILE) suffixed with the shell's own $$ -- the same predictable-name TOCTOU #427 fixed in scripts/doctor.sh, invisible to #427's own class-pin regex because the path is built through a variable rather than a literal on one line.

The central question this issue left open, answered

The issue asked whether the credential-bearing write can actually land at a symlink's target, or whether something intermediate defeats that. Traced with a live reproduction rather than assumed: it lands there. A pre-seeded symlink at the predictable ${TMPDIR:-/tmp}/remember-config-<pid>.json is truncated by (umask 077; : > "$_merged_cfg") first, and then the actual merge write that follows (jq -s ... > "$_merged_cfg", or the Python fallback's open(out_path, "w")) follows the SAME symlink and writes the real merged config through it -- including haiku.oauth_token when configured. Manual repro, before any test scaffolding existed:

victim.txt after run:
{
  "haiku": { "oauth_token": "sk-ant-oat-SECRET-VALUE-1234567890" },
  "model": "haiku"
}

This is discloses in the release-ranking table, not the milder truncation-only shape doctor.sh had. The EXIT trap's rm -f afterwards removes only the symlink, never the victim file it pointed at.

lib-env-cache.sh's publish-temp path (_t="${_REMEMBER_ENV_CACHE_FILE}.$$") is the same shape without the credential -- disclosed as discloses for lib-memory-dir.sh, and the plain truncation class (same rank as #427) for lib-env-cache.sh.

The fix

Both now use mktemp, matching save-session.sh's six existing uses and #427's fix to doctor.sh -- with the X's as the literal LAST characters of the template and no trailing suffix after them. This mattered in practice: mktemp "/tmp/foo-XXXXXX.json" on this developer's macOS (BSD mktemp) silently creates a file named literally foo-XXXXXX.json, unrandomized, because BSD mktemp -- unlike GNU -- only substitutes a TRAILING run of X's and leaves anything after them literal with no error. That would have shipped a fix that looked identical to the working GNU form while reintroducing the exact vulnerability.

tests/test_predictable_tmp_429.py is a live symlink-race reproduction: two must-not-fire attack tests (credential lands at the symlink target / env-cache publish temp survives a planted symlink) each paired with a must-fire positive control proving the legitimate write still happens -- including the token -- when nothing interferes.

Cross-cutting guards this diff had to widen

The full suite (not the narrow subset) caught two guards a filename-based subset would have missed entirely:

  • tests/test_case_divergence_298.py pins lib-memory-dir.sh's executable code byte-identical to origin/main, specifically to catch a silent spawn addition to this hot-path library. mktemp is a spawn, so it failed exactly as designed. Widened via an explicit _SANCTIONED_DIVERGENCE substitution scoped to exactly the two changed blocks (verified: any OTHER divergence from origin/main still fails the guard), following the precedent set by commit fd721fc (perf(post-tool): give PostToolUse the #227 env-cache fast path #352) for the same test.
  • tests/test_post_tool_hook_spawns.py's spawn-count budget moved from 16 to 17 measured spawns, documented as the mktemp fix's real, necessary, one-time cost -- not a regression to chase back out.

Self-review

Two independent reviewers (Explore + oss:auditor) both flagged the same real bug: the mktemp failure fallback left $_merged_cfg empty, which fed an empty redirect target to jq ... > "$_merged_cfg" -- a shell-level "ambiguous redirect" error 2>/dev/null cannot suppress, an edge case the old literal-path code never had. Fixed in a follow-up commit: the merge attempt is now skipped outright when mktemp fails, leaving REMEMBER_CONFIG empty exactly as every consumer already treats a genuinely-absent config file.

Two more items, not fixed here:

  • Below the bar: the new attack tests' symlink-planting synchronization loop is dead code by its own comment (shared_tmp already exists before the subprocess starts, so the wait never actually spins) -- but the auditor independently verified 30/30 real races caught against a reverted, pre-fix copy of the scripts, so this is not currently a false-pass risk. Tightening it would mean adding a testing hook to production shell scripts for a risk that is not manifesting today.
  • Report for filing: stale "0600 per PID" prose in scripts/log.sh:78,392, tests/test_log_sh.py:874 and README.md:276 is no longer accurate now that the merged config gets an unpredictable mktemp name rather than one derived from the PID. Not fixed here because it also touches scripts/post-tool-hook.sh:73,178, a file held by the live lane fix/424.

docs_targets

README.md (this repo's only docs_targets entry): not-read -- this is internal hardening of an implementation detail (a temp-file naming scheme) the README never documented (grepped for remember-config-, remember-env-, mktemp: zero hits), so there was nothing to check it against and opening it would not have changed anything about this diff.

Test plan

  • tests/test_predictable_tmp_429.py -- red before the fix (both attack tests failed, both positive controls passed), green after
  • tests/test_case_divergence_298.py -- green, widened guard verified to still catch any other divergence
  • Full suite: 1787 passed, 43 skipped, 0 failed (one transient error from a concurrent sibling lane's own worktree add, reproduced as unrelated to this diff and confirmed passing in isolation)
  • python3 .oss/assemble_changelog.py --check -- ok

fdaviddpt and others added 2 commits August 29, 2026 04:12
…tmp path from $$ (#429)

Both built a predictable name through an intermediate variable
(SYS_TMPDIR, _REMEMBER_ENV_CACHE_FILE) -- the same TOCTOU #427 fixed in
doctor.sh, invisible to its class-pin regex because the path goes through
a variable rather than a literal. Traced with a live symlink reproduction:
lib-memory-dir.sh's merged-config write follows the symlink and delivers
the actual merged JSON -- including a live haiku.oauth_token when
configured -- to the attacker-chosen target, not just a truncation. Both
now use mktemp, matching save-session.sh and #427's fix, with no trailing
suffix after the X's (BSD/macOS mktemp only substitutes a run of X's at
the very end of the template). test_case_divergence_298.py's byte-pin
guard against origin/main now explicitly sanctions this one line.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…temp fallback

The reviewer and auditor spawned against the #429 commit both flagged the
same real gap: an empty $_merged_cfg (mktemp itself failing) fell straight
into `jq ... > "$_merged_cfg"` with an empty redirect target, a shell-level
"ambiguous redirect" error 2>/dev/null cannot suppress -- an edge case the
old literal-path code never had, since it always produced a non-empty path
string even when that path was unwritable. The merge attempt is now skipped
outright when mktemp fails; REMEMBER_CONFIG ends up empty exactly as every
consumer already treats a genuinely-absent config file.

test_case_divergence_298.py's _SANCTIONED_DIVERGENCE is widened to also
cover this if/elif restructuring (still scoped to exactly this one block --
anything else diverging from origin/main still fails the guard), and
test_post_tool_hook_spawns.py's POST_TOOL_SPAWN_MEASURED moves 16 -> 17: the
mktemp #429 added is a real, necessary, one-time spawn on the resolving
path, not a regression to chase back out.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@fdaviddpt
fdaviddpt merged commit d2cf399 into main Aug 29, 2026
13 checks passed
@fdaviddpt
fdaviddpt deleted the fix/429 branch August 29, 2026 03:05
fdaviddpt added a commit that referenced this pull request Aug 29, 2026
…ives its own merge (#441)

* fix: give #429's sanctioned-divergence guard a third state so it survives its own merge (#440)

The guard compared origin/main's lib-memory-dir.sh against a keyed
old->new allowance and asserted whenever old_code was not found. That
was correct while #436 was open and unsatisfiable the moment it
merged: origin/main then held new_code, old_code was gone, and the
guard failed on the default branch itself. _apply_sanctioned_divergence()
now recognizes old-absent/new-present as the post-merge steady state
rather than staleness, only asserting when neither side is found.

Co-Authored-By: Max <noreply>

* fix: keep the new sanctioned-divergence unit tests out of the whole-file Windows skip (#440)

Self-review on #440 found that the three new _apply_sanctioned_divergence
regression tests, added inside tests/test_case_divergence_298.py, silently
inherited that module's whole-file `pytestmark` skip on win32 -- a skip
that exists for the file's bash-spawning tests and does not describe these
three, which are pure Python string manipulation. Relocated them to a new
file, tests/test_sanctioned_divergence_state_440.py, so they actually run
on windows-latest CI instead of reporting green while never executing.

Co-Authored-By: Max <noreply>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant