Repository navigation
fix(receiver): never block opening a non-regular destination as basis - #7594
Merged
Merged
Conversation
A FIFO with no writer parks open(O_RDONLY) in fifo_open() until a writer arrives. The receiver's basis lookup opened whatever it found at the destination with no file-type check, so a FIFO planted there wedged the transfer indefinitely on the default path, with no flag required. The same --server receiver backs a daemon module, so a writable module with a planted FIFO was an unbounded wedge. Upstream 3.5.0 never reaches its own basis open with a non-regular fnamecmp. generator.c:2148 removes the obstacle first (delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_FILE), leaving statret = -1), and each remaining candidate is gated on S_ISREG: the alt-dest lookup at generator.c:1084, find_fuzzy() at generator.c:860,888, and the --partial-dir fallback at generator.c:2175. By the time do_open_checklinks(fnamecmp) runs at generator.c:2313 the basis is always a regular file. oc has no such removal, so state the invariant at the open instead. open_basis_nofollow() now refuses a non-regular node. The decision is made with fstat(2) on the descriptor rather than a pre-open fstatat(2), so a node swapped between check and open cannot defeat it; the open carries O_NONBLOCK so it can never park, exactly as upstream does for an unchecked leaf at generator.c:1024 (O_RDONLY | O_NOFOLLOW | O_NONBLOCK), and O_NONBLOCK is cleared before a regular-file descriptor is returned so the delta read sees the file status flags it saw before. Both production callers are covered by fixing the one helper: try_open_file (the exact destination, the --partial-dir fallback and the --fuzzy candidate) and try_reference_directories (--compare-dest / --copy-dest / --link-dest, which checked is_file() only after the open, so it parked too). Callers discard the error and fall through to a full send, which reaches upstream's observable result: exit 0 with the destination replaced by a regular file holding the source bytes.
…k runs open_basis_nofollow documents InvalidInput as the refusal when the opened node is not a regular file, and callers treat that as "no basis". On Unix the O_RDONLY open of a directory succeeds and require_regular_basis makes that decision. On Windows CreateFileW refuses a directory without FILE_FLAG_BACKUP_SEMANTICS, so the open failed with PermissionDenied and the ? propagated it before require_regular_basis ever ran - the documented contract held on one platform only. Opening with the flag keeps "is this a regular file" a single decision on every platform. refuses_directory_basis is deliberately left ungated so it pins the contract on both.
This was referenced Sep 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The defect
A FIFO at the destination hung the receiver indefinitely, on the default path, with no flag required. Kernel stack from the live
oc-rsync --serverchild on aarch64 Linux:Site:
crates/transfer/src/receiver/basis.rs:225,try_open_file->fast_io::open_basis_nofollow. It opened the destination as the delta basis with no file-type check and noO_NONBLOCK. A FIFO with no writer blocks infifo_open()forever.The same
--serverreceiver backs a daemon module, so a writable module with a planted FIFO was an unbounded wedge, and an unbounded external operation regardless.Why upstream 3.5.0 does not have it
Upstream gets safety from ordering, not from a type check.
generator.c:2148:The obstacle is already unlinked, so there is no basis to open. Every other candidate that can become
fnamecmpis separately gated onS_ISREG: the alt-dest lookup (generator.c:1084,basis_link_stat(cmpbuf, &sxp->st) < 0 || !S_ISREG(sxp->st.st_mode)),find_fuzzy()(generator.c:860,888,!S_ISREG(fp->mode)), and the--partial-dirfallback (generator.c:2175,S_ISREG(partial_st.st_mode)). By the timedo_open_checklinks(fnamecmp)runs atgenerator.c:2313, the basis is always a regular file.The fix
open_basis_nofollow()now states that invariant directly: it refuses a non-regular node, and the receiver falls through to a full send. That reaches upstream's observable result — exit 0 with the destination replaced by a regular file holding the source bytes.Shape chosen: the type check (option a), made race-free on the descriptor. A pre-open
fstatat(..., AT_SYMLINK_NOFOLLOW)would leave a window in which the node it checked is replaced by the FIFO the check exists to refuse — and this check exists precisely because an attacker can write to the destination directory. So:O_NONBLOCK, so it can never park — this is upstream's own idiom for an unchecked leaf,generator.c:1024:do_open_atfd(dfd, leaf, O_RDONLY | O_NOFOLLOW | O_NONBLOCK, 0);fstat(2)on the resulting descriptor decides;O_NONBLOCKis cleared before a regular-file descriptor is handed back, so the delta read sees exactly the file status flags it saw before.O_NONBLOCKalone (option b) was rejected: it stops the park but leaves a FIFO descriptor in hand whose zero-lengthmetadata()would then be signed as a basis, and it states nothing about intent.Two call sites, not one
fast_io::open_basis_nofollowhas two production callers, both incrates/transfer/src/receiver/basis.rs:try_open_file(:222)--fuzzycandidate (:308), the--partial-dirfallback (:658)try_reference_directories(:184)--compare-dest/--copy-dest/--link-destmeta.is_file()ran after the open — parked just the sameThe alt-dest site was a real second park, not a theoretical one: its regular-file check was on the wrong side of the open. Fixing the shared helper covers both, and the regression test exercises both.
crates/transfer/src/pipeline/async_signature.rs:183(try_open_file_for_signature) has the same unguarded shape but no production caller; left alone.Non-vacuity
tests/basis_nonregular_dest_no_hang.rsdrives the real--serverreceiver over an--rshshim (a plain local copy goes through the local-copy executor and never reaches the basis lookup). Each case:mkfifothat silently produced a regular file cannot make the case inert;Mutation proof, production change reverted and restored around the same test binary:
Unit cover in
crates/fast_io/src/nofollow_open.rsadds a FIFO case (bounded by a deadline on a worker thread), a directory case, and an assertion that a regular basis descriptor comes back withO_NONBLOCKcleared.Known remaining — NOT fixed here
All of these are the same missing
generator.c:2148removal, which is a separate change.--inplace/--write-devicesstill parks, at a different site:fast_io::inplace_open::InplaceResolution::open_write(crates/fast_io/src/inplace_open.rs:108) issuesO_WRONLY|O_CREAT, which blocks on a FIFO waiting for a reader.--write-devicesreaches it becauseapply_append_implies_inplace(crates/transfer/src/config/mod.rs:860) promotes inplace for every file. Sampled stack of the parked--serverchild:process_whole_file -> open_output_file -> fast_io::inplace_open::open_inplace_output -> InplaceResolution::open_write -> std::fs::OpenOptions::open -> open(2).That is not a basis open, and refusing it would not reach upstream's answer either: upstream succeeds with exit 0 there, by having deleted the FIFO at
generator.c:2148. Bolting a refusal on would replace one wrong answer with another.rmdirs an empty dir and succeeds (rc 0). Non-empty: both refuse but oc reports 12 vs upstream's 23, and upstream's diagnosticcould not make way for new regular file: obj(delete.c:283) exists nowhere incrates/.--inplace/--write-devices: oc keeps the symlink and writes through it onto the target, which also picks up the source's mode (0644 -> 0777). Upstream deletes the symlink and writes a fresh regular file, target untouched. The absolute out-of-tree variant IS refused withELOOPby the sandbox resolver, so containment holds at the tree boundary.--inplace/--write-devices: oc exits 12 (ENXIO); upstream succeeds.Verification
cargo fmt --all -- --checkcargo clippy --locked --workspace --all-targets --all-features --no-deps— no diagnostic on either changed filecargo nextest run --locked -p fast_io— 835 passedcargo nextest run --locked -p transfer— 2768 passed, 3 skippedcargo nextest run --locked -p bin(the root-leveltests/integration_*.rsand friends, which per-crate runs do not cover) — 517 passed, 33 skipped