Skip to content

fix(shred): fold POSIX drive-dialect operands before any overwrite (#1240) - #1250

Merged
caomengxuan666 merged 2 commits into
mainfrom
fix/1240-shred-drive-dialect
Oct 19, 2026
Merged

caomengxuan666 merged 2 commits into
mainfrom
fix/1240-shred-drive-dialect

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Ticket

#1240 - shred (secure erase) bare-drive-letter dialect misparse. Audited as the highest data-safety risk of the dialect sweep: shred is an overwrite-destroy command, so a dialect misparse here is either a wrong-target destruction or a false success. Both faces were probe-verified before the fix and both are closed.

The bug

shred handed the raw operand to CreateFileW. Win32 resolves a leading /e against the current drive, so from a C: cwd shred /e/Data/secret:

  • wrong target (probe-verified, pre-fix): with C:\e\probe-wc\victim.txt present, the tool overwrote THAT shadow file with random bytes and exited 0 - the requested E: victim was left completely intact (marker unchanged);
  • false failure: with no shadow, the same command reported cannot open '/e/probe-wc/victim.txt' (rc 1) for a perfectly good E: file.

/cygdrive/e/... and /mnt/e/... spellings misresolved identically.

The fix

Follows the #1233/#1234/#1245 boundary precedent exactly:

  • every operand folds once through native_path::make_api_path_operand before the overwrite, the --force read-only retry, the native-glob pass (a /e/*.log pattern now expands against the operand drive, not the cwd drive) and the -u/--remove unlink;
  • the Win32 calls take operand.extended (long-path safe, like chattr/cp post-mkdir 裸盘符根 /x 形态在 cwd 盘创建/误报——创建主路径未过盘符折叠(mkdir -p 会真建影子目录) #1233);
  • messages keep printing the operand spelling as typed (glob matches print the matched path), so error text never names a shadow path - matching MSYS behavior of quoting what the user referenced;
  • - (stdout wipe) handling is untouched; /dev/null-class operands now resolve through the standard pseudo-device boundary like sibling commands.

Probe evidence

PS/cmd native argv throughout; self-created probe files only; -n 0 = zero overwrite passes (content-preserving parse-layer proof). Shadow witnesses C:\e, C:\d, D:\e verified absent before, between and after every probe.

Baseline (pristine origin/main), cwd C:\Users\cmx:

operand rc resolution
/e/probe-wc/victim.txt 1 misresolved to C:\e\... (file exists on E:)
/cygdrive/e/..., /mnt/e/... 1 same misresolution
E:\probe-wc\victim.txt 0 correct
wrong-target shot (-n 1, shadow planted) 0 shadow C:\e\... destroyed, E: victim untouched

Fixed (this PR), 5 spellings x 2 cwds:

operand cwd C: cwd D: content marker shadows
/e/probe-wc/victim.txt rc 0 rc 0 intact (-n 0) absent throughout
/cygdrive/e/probe-wc/victim.txt rc 0 rc 0 intact absent throughout
/mnt/e/probe-wc/victim.txt rc 0 rc 0 intact absent throughout
E:\probe-wc\victim.txt (Windows form) rc 0 rc 0 intact absent throughout
E:/probe-wc/victim.txt (forward-slash form) rc 0 rc 0 intact absent throughout
/e/...absent-1240.txt (error path) rc 1 rc 1 - absent; message quotes the typed spelling

Plus, on the fixed binary: -n 1 overwrite lands on the E: file (marker replaced) while a planted shadow stayed untouched (exact inverse of the baseline smoking gun); a native-glob dialect pattern (/e/probe-wc/victim*.txt, WINUX_SHELL_GLOB=native) resolves on the operand drive; -u removes the E: file.

Oracle cross-check (MSYS shred, Git for Windows /usr/bin/shred): missing operand gives rc 1 under every spelling from both cwds - rc parity holds. Oracle messages quote the user-referenced path (MSYS prints either the raw spelling or its Windows fold depending on the invoking parent - never a shadow path); ours always quotes the typed spelling per the #1234 house rule.

Tests

8 new regressions in tests/unit/shred/shred_unit_test.cpp (dialect spelling synthesized from the TempDir's own drive, so they pass on any host; parse-layer -n 0 assertions plus one destructive overwrite proof on a test-created file):

  • zero-pass landing on operand drive + shadow exists() negative (the "shadow path is never shred" witness - parse-layer, not a real erase)
  • cygdrive + mnt spellings open the same file
  • destructive overwrite lands on operand drive, shadow never appears
  • cwd-on-other-drive shadow negative (skips without a second fixed drive)
  • Windows-form idempotence
  • missing dialect operand fails with the typed spelling (false-success guard)
  • bare drive root /e fails without creating <drive>:\e
  • wildcard dialect pattern expands on the operand drive; -u dialect removal

shred subset: 18/18 green. Full ctest: 3013/3013 (second run exit 0; the first run had 9 unrelated 0xc0000409 load flakes that all pass in isolation and in the second run). Build via scripts/build.py; changed files clang-format clean.

Notes

…1240)

shred handed the raw operand ('/e/f', '/cygdrive/e/f', '/mnt/e/f') to
CreateFileW, and Win32 resolves a leading '/e' against the CURRENT
drive. For the toolset's data-destroying command that misparse was
doubly dangerous (#1230 sweep): from a C: cwd with a shadow 'C:\e\f'
present, 'shred /e/f' silently overwrote the SHADOW and exited 0 -
probe-verified before the fix (shadow destroyed, requested E:\ file
untouched) - and with no shadow it reported 'cannot open' for a
perfectly good E:\ file. Both the wrong-target overwrite and the false
success/failure pair are gone: every operand folds once through the
shared make_api_path_operand boundary before the overwrite, the
--force retry, the native-glob pass and the -u/--remove unlink, and
the Win32 calls take the extended form like the other post-#1233
commands.

Messages keep printing the operand spelling as typed (glob matches
print the matched path), so a missing operand still fails with rc 1
quoting what the user typed, like MSYS. Regressions synthesize the
dialect spelling from the TempDir's own drive (zero-pass parse-layer
landing, one destructive overwrite proof, cygdrive/mnt spellings,
cwd-on-other-drive shadow negative, bare-drive-root no-creation,
Windows-form idempotence, missing-operand typed-spelling pin,
wildcard-dialect glob landing, -u removal) and pass on any host.
@caomengxuan666

Copy link
Copy Markdown
Member Author

Independent code review — verdict: APPROVE (no blockers; non-blocking notes only)

Reviewed independently from a fresh detached worktree at 4656417 (the branch is checked out in the implementation tree, so the review tree was detached at the same commit; merge-base with origin/main = 6e8ea19, the commit that includes #1245 — single commit, exactly as described). Baseline binary rebuilt from pristine 6e8ea19 in a second detached tree. All probes via ProcessStartInfo native argv (PS), cwd as stated — no shell argv translation anywhere.

Verified (all re-executed)

  1. Hitchhiking scan: single commit over 6e8ea19 touching exactly CHANGELOG.md, src/commands/shred.cpp, tests/unit/shred/shred_unit_test.cpp. Nothing else.
  2. Call-site closure audit (line-by-line): every path-taking Win32 call consumes the folded operand or a path derived from it — initial CreateFileW (job.api), --force's GetFileAttributesW/SetFileAttributesW/retry CreateFileW, the size==0 DeleteFileW (also remove-gated), the wipesync MoveFileW + parent-dir-sync CreateFileW + DeleteFileW of the renamed target, and the plain unlink DeleteFileW. The native-glob pass runs on operand.normalized (folded) and each match re-folds through make_api_path_operand_w. No implicit delete path exists outside the -u/--remove gate. The --force retry is OPEN_EXISTING after clearing READONLY — no create-clobber mode anywhere in the file. Pseudo-devices stay verbatim (/dev/null→NUL without the \\?\ prefix), bare drive roots stay un-prefixed (to_extended_path's 3-char rule), so /e cannot materialize <cwd>:\e.
  3. Live-fire wrong-target reproduction on the BASELINE (6e8ea19), cwd C:\Users\cmx, real target E:\probe-cr1240\victim.txt (marker B), shadow planted at the exact misresolution C:\e\probe-cr1240\victim.txt (marker A — the shadow tail must equal the operand tail; that is the bug's mechanic: leading /e roots on the cwd drive): shred -n 1 /e/probe-cr1240/victim.txt → rc 0, shadow overwritten with random bytes (SHA-256 D1B6F54F569C → E5F150917D2F), E: victim byte-intact (marker B verbatim). The smoking gun reproduces exactly as the PR describes. With no shadow present, the same command → rc 1 cannot open '/e/probe-cr1240/victim.txt' (the false-failure face). Both documented baseline faces confirmed.
  4. Live-fire on the PR binary, identical shape: rc 0, E: victim overwritten (hash change, first bytes random), shadow byte-identical (marker verbatim) — the exact inverse. A second shadow with a non-matching tail (C:\e\probe-cr1250\...) also untouched throughout.
  5. False-success / parse-layer guards: missing dialect operand → rc 1 quoting the typed spelling; bare /e → rc 1 cannot open '/e' with no <E>:\e created anywhere; -n 0 leaves content byte-intact for /e/, /cygdrive/e/, /mnt/e/ and both Windows forms (E:\..., E:/...) from both C: and D:\ cwds; -f on a READONLY dialect path: rc 1 without -f (typed spelling), rc 0 with -f and the attribute actually cleared — the retry path exercised through the fold; native glob /e/probe-cr1240/glob*.txt (WINUX_SHELL_GLOB=native) matches on the operand drive; -u (default wipesync: rename + dir sync + delete) removes the E: file through the folded path.
  6. Regressions: shred subset 18/18 green — all 9 new dialect TESTs genuinely executed here (the cwd-on-other-drive case ran, not skipped; skip judgment inspected: only without a second DRIVE_FIXED drive, and the bare-root negative only when <X>:\<X> doesn't pre-exist). Full ctest: 3013/3013, 0 failed in a single 308 s run — no flakes on this host.
  7. Oracle: rc parity holds (rc 1 for missing operands on both sides). Honesty note: which shred in this environment resolves to the installed Niubash/winuxcmd 1.1.16 shim (pre-shred 裸盘符根 /x 形态裸 CreateFileW 误解析——跨盘影子路径存在时会 shred 错文件(数据安全) #1240), not GNU — the same fake-green trap fix(native-path): fold the POSIX drive dialect in canonicalize_path_w (#1231) #1245's review flagged; its message spelling even varies with the invoking parent, consistent with the PR body's description. All load-bearing evidence above is native-argv, independent of that shim.
  8. Main drift: origin/main advanced to 4d8b57e (fix(tree/namei/look): fold POSIX drive-dialect operands onto the named drive (#1236 #1237 #1242) #1248) after this branch point. Overlap is CHANGELOG.md only (both append under [Unreleased] › Fixed — auto-mergeable); zero shred/test overlap. Rebase at merge time per discipline.

Nits (non-blocking)

  1. Glob-match messages quote the match in normalized Windows form (shred: 'E:\probe-cr1240\glob1.txt': passing 1..0), not the typed dialect expansion (/e/...). Defensible — it names exactly the file being destroyed, and unmatched patterns keep the typed spelling per the ln 裸盘符根 /x 形态被 std::filesystem::absolute 拼接到 cwd——链接解析盘符随 cwd 漂移 #1234 rule — but it is a third spelling convention next to the typed-spelling rule; worth a line in the eventual dialect-message-pinning follow-up.
  2. PR body says "8 new regressions"; the file adds 9 TEST macros (the body's last bullet folds wildcard and -u into one). Trivial count drift in the description only.
  3. The -s/--remove=HOW argument validation happens before the operand loop — correct (no path taken there), just noting the fold sits inside the positional loop so per-operand typed spelling stays available to every message, as claimed.

Cleanup witnessed: probe trees C:\e and E:\probe-cr1240 removed — Test-Path False on all seven candidate shadow paths including the never-created E:\e; both review worktrees deleted and the worktree list re-checked (no cr-1250 trees remain; wave2a/2b/2c/2d and the main checkout untouched). Not merging, per ticket discipline.

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.

1 participant