Repository navigation
fix: pack atomically through a temporary file - #106
Conversation
blocksmith314
left a comment
There was a problem hiding this comment.
Review
The temp-file/rename direction and the AtomicPack tests are good, but two atomicity/security gaps remain.
1. No-force fallback to rename() can overwrite the destination (blocker)
When link(temp, out) fails with EPERM/EOPNOTSUPP/ENOSYS, the code falls back to:
fs::rename(temp_pack_file, output_pack_file, rename_ec);rename() replaces an existing destination. If the destination appears after the early symlink_status check (or the early check is bypassed by a race), a no-force pack overwrites it on a filesystem without hard links. That violates the stated guarantee. EPERM may also be a permission error rather than "no hard links".
Fix options:
- fail the no-force pack with a clear error when
link()is unsupported, letting--forceuserename(); or - use a no-replace primitive (
renameat2(RENAME_NOREPLACE)on Linux /renamex_np(RENAME_EXCL)on macOS) instead of plain rename.
A test or at least a documented limitation is needed.
2. WriterLoop reopens the temp file by path (blocker)
The temp file is created with O_EXCL, but the writer thread reopens it:
auto w_res = NewPosixWriteFile(packed_file_);NewPosixWriteFile uses O_CREAT without O_EXCL/O_NOFOLLOW. Between creation and the writer opening it, another process in a writable destination directory can unlink the temp file and replace it with a symlink or another file; the writer then writes the archive blocks into that target. It also silently recreates the temp file if it disappeared.
Pass the already-open exclusive WriteFile handle (or a dup of its fd) to WriterLoop instead of reopening by path. The exclusive creation is the security boundary and should stay the only open-by-name.
Minor
- After
link()/rename(), the parent directory is not fsynced. The archive itself is synced, but a crash after the name change may lose the new directory entry on some filesystems; if durability is part of the claim, add a directory fsync. cleanup_tempis a manual lambda; an exception fromstd::threadcreation, metadata allocation, or formatting after temp creation leaves the hidden temp file behind. A scope guard would cover exception paths.- The tests do not cover the no-hard-link fallback or the writer-reopen TOCTOU; the current tests only run on filesystems with hard links.
Verified locally: Release ctest 220/220, ASan ctest 220/220, CI 5/5 green.
PackPipeline opened the destination with O_TRUNC before scanning, so a failed pack (for example an empty tree) destroyed an existing archive, and a crash mid-pack left a partial file under the final name. The --force existence check also ignored stat errors and missed broken symlinks. Write to a hidden temporary file created with O_EXCL in the destination directory, rename it into place only after the header, metadata and magic are written and synced, and remove the temporary file on every failure path with error_code cleanup that does not hide the primary error. The existence check now uses symlink_status and reports stat failures. Add AtomicPack tests covering empty-tree failure, mid-pack failure, no-force refusal, successful replacement, and broken symlinks. Closes #77
9e494af to
df29e8e
Compare
Summary
Make
packatomic: a failed or interrupted pack no longer destroys an existing archive or leaves a partial file under the final name.PackPipelinecreates a hidden temporary file withO_EXCLin the destination directory (newNewPosixWriteFileExclusive), writes the archive there, syncs it, and only then publishes it under the final name.--force:rename()replaces the destination atomically.--force:link()+unlink(). If the destination appears after the early check,link()fails withEEXISTand the existing file is never overwritten. If the filesystem cannot create hard links, the pack fails with a clear error instead of falling back torename()(which would overwrite).WriteFileinstead of reopening the temporary path, so there is no window where a replaced/symlinked temporary name could redirect the archive data.symlink_status(a broken symlink is not mistaken for "missing"), and stat failures are reported instead of silently continuing.error_codeoverloads; a scope guard also covers exception paths (thread creation, allocations). After publishing, the destination directory is fsynced best-effort so the name change survives a crash.BuildBlocksFromFileStats()runs before the temporary file is created, so an empty tree fails without creating anything.--forcereplaces a symlink at the destination (the link itself, not its target).No archive-format change.
Review fixes
rename()fallback: a filesystem without hard links now fails and asks for--force.WriterLoopno longer reopens the temporary file by path; it uses the exclusive handle.Tests
ctest: 220/220 passedctest: 220/220 passedAtomicPack.*tests:--forcerefuses to replace an existing archive--forcereplaces it and the new content is readable--forceis refused; with--forcethe symlink itself is replacedctar pack -f <empty-dir> existing.ctarfails, the archive bytes are unchanged, no.tmp.files remain, and the old archive still unpacks.Performance gate
Against
origin/main(e377745): pack -1.4% wall time / +0.9% peak RSS; unpack -6.1% wall time / +0.5% peak RSS (5 runs). No regression.Closes #77