Skip to content

fix: doctor.sh no longer builds its stderr-capture temp path from $$ - #428

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

fdaviddpt merged 2 commits into
mainfrom
fix/427

Conversation

@fdaviddpt

@fdaviddpt fdaviddpt commented Aug 29, 2026 •

Copy link
Copy Markdown
Contributor

Closes #427.

scripts/doctor.sh built the stderr-capture path for resolve-paths.sh's own errors as ${TMPDIR:-/tmp}/remember-doctor-*-resolve-$$, at two sites (originally lines 102 and 195). That name is predictable from the PID and lives in a shared, often world-writable directory -- TMPDIR is commonly unset on Linux, so this is plain /tmp there. The shell's > redirection follows a symlink and truncates its target on open, before doctor.sh writes or reads a byte, so a pre-seeded symlink at that exact name let an attacker truncate an arbitrary file as the invoking user; the later rm -f removed only the symlink, never the already-truncated victim.

This predates the current delta -- the identical pattern is already in v0.23.0's own scripts/doctor.sh:107 -- and was surfaced by the round-one release audit of v0.23.0..9f3269e, which reported it unranked because no row in the ranking table's fix-mechanism list fits a bare predictable-path TOCTOU.

Both sites now use mktemp, matching the unpredictable-name convention scripts/save-session.sh already uses in six places elsewhere in this repo.

Tests

tests/test_doctor_predictable_tmp_427.py adds two tests:

  • test_predictable_temp_path_lets_a_symlink_truncate_an_arbitrary_file wins the TOCTOU race deterministically against a live doctor.sh subprocess (the PID is known to the caller the instant Popen returns, since ["bash", DOCTOR, ...] makes DOCTOR's own interpreter the top-level shell rather than a subshell) and asserts a planted victim file survives untouched. Red against the pre-fix script (the victim was truncated), green after.
  • test_no_script_builds_a_shared_tmpdir_path_from_pid is a static regex guard over scripts/*.sh pinning the class -- no script may build a shared-tmpdir path from $$ outside a private, already-resolved $REMEMBER_DIR/tmp. It is a text scan, not a dataflow check, and its limits are stated below.

Both passed together with the existing doctor.sh test files (49 passed total) after the fix.

Docs

README.md (the repo's only docs_targets entry) was read against this change and needs no update: this is an internal hardening fix to an unpredictable-name choice inside doctor.sh's error-capture plumbing, and nothing about doctor.sh's documented behavior, usage or --json output shape changes.

Diagnostic

The plugin's own doctor.py (the /oss:doctor diagnostic, distinct from this repo's scripts/doctor.sh) has no reference to resolve-paths.sh, mktemp or TMPDIR -- confirmed by grep -- so nothing there needed updating for this change.

Self-review findings

Two independent spawns (an Explore reviewer and oss:auditor) reviewed the first commit and both, independently, found the same real gap: scripts/lib-memory-dir.sh:274 and scripts/lib-env-cache.sh:80,194-195 carry the identical predictable-shared-tmpdir-plus-$$ class this fix closed in doctor.sh, and the new class-pin test's regex cannot see either because both go through an intermediate variable (SYS_TMPDIR, _REMEMBER_ENV_CACHE_FILE) rather than spelling ${TMPDIR:-/tmp} and $$ on the same line. lib-memory-dir.sh's file in particular can hold a live OAuth token per its own adjacent comment, raising this past plain truncation into possible credential disclosure if the planted symlink points somewhere attacker-readable. These two files are outside this issue's assigned lane (scripts/doctor.sh and its tests only, per this session's brief, which named two sibling lanes holding other files) and fixing them here would roughly double this diff with a second, separately-reviewable security fix -- filing them for a follow-up issue instead of bundling them in. Full reproduction is in the agent report's adjacent items.

Two review findings that were in scope got fixed in a second commit: the changelog fragment's off-by-one count of save-session.sh's existing mktemp uses (five, corrected to six), and the new class-pin test's module-level pytest.mark.skipif(win32), which had been skipping the pure regex/text-scan test alongside the genuinely-POSIX symlink-race test -- split so the class-pin test now runs on the Windows CI leg too.

One review finding was judged below the bar for action here: a below-bar reason is that if mktemp itself fails (unwritable TMPDIR, full disk) at either fixed doctor.sh site, the resulting empty redirect target produces the same could_not_resolve/"unknown error" JSON a genuine resolver failure with no stderr would also produce -- a pre-existing ambiguity this fix widens only slightly, with no reachable caller today that needs to tell the two apart, and disambiguating doctor.sh's own error taxonomy is a design decision outside this issue's scope.

Verified by the maintainer

  • Red re-run, observed. tests/test_doctor_predictable_tmp_427.py against main at 9f3269e: 2 failed — the symlink race truncates the victim, and the class pin fires on doctor.sh:102 and doctor.sh:195. Neither test is vacuous.
  • Checks: 13 legs, 13 passed, 0 pending — the states sum to the leg count.
  • The class pin is narrower than the class it names, and that is worth knowing before it is relied on. It matches a shared-tmpdir path built from $$. scripts/lib-env-cache.sh:80 builds ${TMPDIR:-/tmp}/remember-env-${_key} — predictable, in the same shared directory, and not $$-derived, so this pin does not see it. That is not a defect in this pull request: the fix and both tests do what they say. It means the pin should not be read as "no script under scripts/ can be raced this way". lib-memory-dir.sh and lib-env-cache.sh carry the same predictable-tmp truncation class #427 fixed, and one of them writes a config that can hold an OAuth token #429 carries that file and the trace that decides how it ranks.
  • Provenance confirmed: git show v0.23.0:scripts/doctor.sh carries the same pattern, so this predates the 0.24.0 delta and is not a regression from anything merged tonight.

`${TMPDIR:-/tmp}/remember-doctor-*-resolve-$$` is predictable and shared;
a pre-seeded symlink at that name let an attacker truncate an arbitrary
file the moment the redirection opened it, before doctor.sh wrote or read
a byte. Both sites now use mktemp, matching save-session.sh own
convention (#427).

Co-Authored-By: Max <noreply>
…ass-pin test, and let it run on Windows

Two independent reviewers of the #427 commit found the same real gap:
scripts/lib-memory-dir.sh:274 and scripts/lib-env-cache.sh:80,195 carry
the identical predictable-shared-tempfile-plus-$$ class this fix closed
in doctor.sh, and the new class-pin test's literal-text regex cannot see
either (both go through an intermediate variable rather than spelling
${TMPDIR:-/tmp} and $$ on the same line). Those two files are outside
this issue's assigned scope (doctor.sh and its tests only) and are
reported to the maintainer for filing rather than fixed here -- fixing
them would double this diff and touches files this task was not given.

Two review findings that ARE in scope, fixed here:
  - changelog.d/427.fixed.md miscounted save-session.sh's existing
    mktemp uses as five; it is six. Also softened the "pins the class"
    claim to state plainly that the guard is a text scan, not a
    dataflow check, and points at the two sites it cannot see.
  - test_no_script_builds_a_shared_tmpdir_path_from_pid is a pure
    regex/text scan with nothing platform-specific in it, but the
    module-level `pytestmark` skipped it on Windows too, alongside the
    genuinely-POSIX symlink-race test. Split the skip so only the
    symlink test carries it; the class-pin test now runs (and reports)
    on all three CI OSes.

Co-Authored-By: Max <noreply>
@fdaviddpt
fdaviddpt merged commit 29363f5 into main Aug 29, 2026
13 checks passed
@fdaviddpt
fdaviddpt deleted the fix/427 branch August 29, 2026 01:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

doctor.sh --json writes stderr to a predictable temp path instead of mktemp

1 participant