Skip to content

fix(receiver): never block opening a non-regular destination as basis - #7594

Merged
oferchen merged 3 commits into
masterfrom
fix/basis-nonregular-dest-hang
Sep 2, 2026
Merged

oferchen merged 3 commits into
masterfrom
fix/basis-nonregular-dest-hang

Conversation

@oferchen

@oferchen oferchen commented Sep 1, 2026

Copy link
Copy Markdown
Owner

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 --server child on aarch64 Linux:

[<0>] wait_for_partner+0xbc/0x150
[<0>] fifo_open+0x354/0x398
[<0>] do_dentry_open ... vfs_open ... do_open
syscall: 56 (openat) flags=0x88000   # O_RDONLY|O_NOFOLLOW|O_CLOEXEC, dirfd=dst/

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 no O_NONBLOCK. A FIFO with no writer blocks in fifo_open() forever.

The same --server receiver 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:

if (statret == 0 && !(stype == FT_REG || (write_devices && stype == FT_DEVICE))) {
        if (delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_FILE) != 0)
                goto cleanup;
        statret = -1;
        stat_errno = ENOENT;
}

The obstacle is already unlinked, so there is no basis to open. Every other candidate that can become fnamecmp is separately gated on S_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-dir fallback (generator.c:2175, S_ISREG(partial_st.st_mode)). By the time do_open_checklinks(fnamecmp) runs at generator.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:

  1. the open carries 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);
  2. fstat(2) on the resulting descriptor decides;
  3. O_NONBLOCK is cleared before a regular-file descriptor is handed back, so the delta read sees exactly the file status flags it saw before.

O_NONBLOCK alone (option b) was rejected: it stops the park but leaves a FIFO descriptor in hand whose zero-length metadata() would then be signed as a basis, and it states nothing about intent.

Two call sites, not one

fast_io::open_basis_nofollow has two production callers, both in crates/transfer/src/receiver/basis.rs:

caller reaches pre-fix behaviour
try_open_file (:222) the exact destination (:598), the --fuzzy candidate (:308), the --partial-dir fallback (:658) no type check at all — parked
try_reference_directories (:184) --compare-dest / --copy-dest / --link-dest meta.is_file() ran after the open — parked just the same

The 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.rs drives the real --server receiver over an --rsh shim (a plain local copy goes through the local-copy executor and never reaches the basis lookup). Each case:

  • bounds the child with a deadline and reports a timeout as a failure, rather than wedging CI;
  • asserts the fixture really is a FIFO before the run, so a mkfifo that silently produced a regular file cannot make the case inert;
  • asserts the positive outcome — exit 0 and the destination is a regular file holding the source bytes — so an outright failed transfer cannot pass.

Mutation proof, production change reverted and restored around the same test binary:

# production change reverted
test fifo_destination_does_not_park_the_receiver ... FAILED
test fifo_in_compare_dest_does_not_park_the_basis_lookup ... FAILED
  ... oc-rsync did not exit within 60s - the receiver parked opening a
      non-regular destination as the delta basis
test result: FAILED. 0 passed; 2 failed;   (finished in 60.03s)

# production change restored
test fifo_in_compare_dest_does_not_park_the_basis_lookup ... ok
test fifo_destination_does_not_park_the_receiver ... ok
test result: ok. 2 passed; 0 failed;       (finished in 1.68s)

Unit cover in crates/fast_io/src/nofollow_open.rs adds a FIFO case (bounded by a deadline on a worker thread), a directory case, and an assertion that a regular basis descriptor comes back with O_NONBLOCK cleared.

Known remaining — NOT fixed here

All of these are the same missing generator.c:2148 removal, which is a separate change.

  • FIFO destination under --inplace / --write-devices still parks, at a different site: fast_io::inplace_open::InplaceResolution::open_write (crates/fast_io/src/inplace_open.rs:108) issues O_WRONLY|O_CREAT, which blocks on a FIFO waiting for a reader. --write-devices reaches it because apply_append_implies_inplace (crates/transfer/src/config/mod.rs:860) promotes inplace for every file. Sampled stack of the parked --server child:
    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.
  • Directory destination: oc exits 12; upstream rmdirs an empty dir and succeeds (rc 0). Non-empty: both refuse but oc reports 12 vs upstream's 23, and upstream's diagnostic could not make way for new regular file: obj (delete.c:283) exists nowhere in crates/.
  • In-tree symlink destination under --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 with ELOOP by the sandbox resolver, so containment holds at the tree boundary.
  • Socket destination under --inplace / --write-devices: oc exits 12 (ENXIO); upstream succeeds.

Verification

  • cargo fmt --all -- --check
  • cargo clippy --locked --workspace --all-targets --all-features --no-deps — no diagnostic on either changed file
  • cargo nextest run --locked -p fast_io — 835 passed
  • cargo nextest run --locked -p transfer — 2768 passed, 3 skipped
  • cargo nextest run --locked -p bin (the root-level tests/integration_*.rs and friends, which per-crate runs do not cover) — 517 passed, 33 skipped

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.
@github-actions github-actions Bot added the bug Something isn't working label Sep 1, 2026
…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.
@oferchen
oferchen merged commit 290a4ac into master Sep 2, 2026
72 checks passed
@oferchen
oferchen deleted the fix/basis-nonregular-dest-hang branch September 2, 2026 06:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant