Repository navigation
test: point the free-port squatter test at a port it owns - #8898
Merged
Merged
Conversation
FreePortTest bound taken + 1 raw and errored when a concurrent oracle run on the host already held that port; the pid offset in interop_oracle.py gives two runs the same range one time in four. The squatter now binds a kernel-assigned port, and the test patches _PORT_BASE, _ports and PORTS_PER_RUN so the allocator's whole range is that one port. bind_free_port must skip it and report the range exhausted, leaving the tap unbound. The test touches no port it does not own and computes no port past the squatter's, which can be 65535 on macOS.
oferchen
added a commit
that referenced
this pull request
Nov 28, 2026
## Summary A push or pull into a destination below a symlink owned by another user had two problems. When the destination was new, the network receiver created it with a plain path mkdir, which followed the symlink. When it existed, the refusal carried an invented line, `failed to access destination directory`, which rsync 3.5.2 never prints. This affects the `--server` receiver of a push and the client receiver of a pull (issues #8870, #8863). rsync 3.5.2 enters the destination in `get_local_name()` (main.c:714-873) through `change_dir()`, whose non-daemon arm walks the path with `open_no_attacker_symlinks_dirfd()` and `fchdir()`s to the result (util1.c:1363-1392). This change makes both network receivers do the same: - `change_dir#1` (main.c:777-781): an existing destination directory is entered through the ownership walk and the walked descriptor is pinned. - `change_dir#2` (main.c:836-839): a directory the run creates is made from the walked parent and entered the same way. - `change_dir#3` (main.c:865-868): the parent of a single-file destination is walked and pinned. - `--mkpath` (main.c:757-773) builds the missing leading directories per component through the walk; the result is then entered as `change_dir#1` (statret refreshed at :770). A symlink the user or root owns is still followed. `--insecure-links` on the receiving side keeps the plain follow; like 3.5.2, a client does not forward it (options.c:3146-3149). ## Design - **I/O stays in fast_io.** The walk uses fast_io's confined-path and pinned-root API: `operator_open_destination` (the ownership walk), `operator_mkdir`, `operator_create_dir_all`, `make_path_beneath_cwd`, then `confinement::pin_destination_root`. `transfer` does not open-code `openat` or `O_NOFOLLOW` steps. - **One walk for both legs.** `ensure_dest_root(.., Entry)` in `receiver/dest_root.rs` serves the push and the pull receiver through the same `ReceiverContext` setup; the only switch is `destination_entry()` (walked unless a daemon module or `--insecure-links`). - **One message site in this crate.** This branch adds no new copy of the `change_dir#N` text: it adds the `ChangeDir2` arm to the existing `SelectStep` Display (`receiver/dest_root.rs`). The text is written today in three places across the workspace - `transfer` `receiver/dest_root.rs` (`SelectStep`), `core` `client/error.rs` and `engine` `local_copy/error.rs` (both from #8890). Folding them into one shared formatter is task 1233. - **The late re-open and the `canonicalize()` block are deleted.** The walked descriptor is pinned instead. Evidence (below): `symlink-dirlink-basis` passes with this change, and passes with and without the pin; the pin is what keeps the two swap-after-entry cells and upstream `rrsync-receiver-file-arg` green. - **Success-path cost:** no extra syscalls. A clean push into an existing ordinary directory, strace on client and server, both binaries built with `cargo build -p bin`: | Tree | master openat / stat | this branch openat / stat | |---|---|---| | 1 dir, 10 files | 89 / 126 | 89 / 125 | | 10 dirs, 100 files | 296 / 513 | 296 / 512 | The one stat fewer per run is the `lstat` that guarded the deleted `canonicalize()` block. The walk's search check on `dest/.` (chdir's implicit search-permission check, which an `O_PATH` open skips) replaces the `dest/.` stat master already made, so it adds nothing. readlink counts are equal (12 and 12). The difference is the same at both tree sizes. ## Output and exit codes Measured with rsync 3.5.2 and with this branch, same fixture, per leg: ``` # fixture: ROOT/flink -> ROOT/real (lchown nobody:nobody), ROOT/olink -> ROOT/own_real # push: rsync -r -e ./lsh --rsync-path=rsync ROOT/src/ localhost:DEST # pull: rsync -r -e ./lsh --rsync-path=rsync localhost:ROOT/src/ DEST # lsh: #!/bin/sh / shift / exec sh -c "$*" ``` | Case | rsync 3.5.2 (push and pull) | this branch | |---|---|---| | `flink/new/` | refusal line, `[Receiver] change_dir#2 "ROOT/flink/new" failed: Too many levels of symbolic links (40)`, exit 3 | same lines, exit 3 | | `flink/` | refusal line, `change_dir#1 "ROOT/flink/"`, exit 3 | same | | `flink/single` (one file) | refusal line, `change_dir#3 "ROOT/flink"`, exit 3 | same | | `--mkpath flink/a/b/` | refusal line, `change_dir#1 "ROOT/flink/a/b/"`, exit 3 | same | | `olink/new/` (own link) | exit 0 | exit 0 | | `--insecure-links flink/new/` | push: refused, exit 3; pull: followed, exit 0 | same | `change_dir#1` keeps the operand's trailing slash, as 3.5.2 does: `full_fname` prints `dest_path` as given (main.c:779). The trailer names oc's own file(line), as for every oc trailer. ## Deliberate deviation from rsync 3.5.2 | Case | rsync 3.5.2 | oc | |---|---|---| | A directory the run creates below another user's symlink: the new destination directory, and the missing parents of an absolute `--mkpath` destination | A plain mkdir follows the symlink and creates `real/new` (main.c:817) or `real/a/b` (make_path, main.c:757-773), then refuses at `change_dir#2` (main.c:836-839) or `change_dir#1` (main.c:777-781), exit 3 | Walks the leading directories and refuses before any mkdir. Same refusal lines and exit 3; nothing is created. | This is the same deviation the local copy made in #8890. The mkdir that 3.5.2 performs first is the write through an untrusted symlink that the CVE-2026-53796 fix exists to stop. ## Not covered - Push line order. 3.5.2 prints the refusal line first: ``` refusing to follow a symlink owned by an untrusted user; use --insecure-links locally or "insecure links = yes" in a daemon module ONLY if every path component is trusted rsync: [Receiver] change_dir#2 "ROOT/flink/new" failed: Too many levels of symbolic links (40) rsync error: errors selecting input/output files, dirs (code 3) at main.c(839) [Receiver=3.5.2] ``` On a push, oc prints the `change_dir#N` line and the trailer first and the refusal line last. A pull matches. Tracked with task 1104. - Pull sender line. On a pull, 3.5.2's sender adds `rsync error: received SIGUSR1 (code 19) at main.c(1691) [sender=3.5.2]` after the receiver's trailer; the oc sender prints nothing. Tracked with task 548. - The non-chrooted daemon receiver's module walk (util1.c:1351) is task 1227. - The shared `change_dir#N` formatter is task 1233. ## Tests `tests/destination_root_owner_walk.rs`: - New `network_receivers_stop_at_upstreams_change_dir_step`: push and pull, four stops (new dir `change_dir#2`, existing dir `change_dir#1`, single file `change_dir#3`, `--mkpath` `change_dir#1`). Each asserts exit 3, the refusal line, the exact `[Receiver] change_dir#N "<path>" failed: ` line from the 3.5.2 run above, no invented text, and that nothing was created below the symlink. - The existing refused cell's push arm expected the deleted invented text; it now expects the refusal line, which a `--server` receiver relays as MSG_ERROR (log.c:437-446). - The old `skip()` is now `missing_privilege()`, which panics: a host that cannot plant a symlink owned by another uid fails these cells instead of reporting a pass. Privileged fixture. Planting the foreign-owned symlink needs root or passwordless `sudo -n`. The file runs in these required jobs, both hosted `ubuntu-latest` runners with passwordless sudo (their own setup steps run `sudo apt-get` / `sudo rm` non-interactively): | Job | Runs this file | Privilege | |---|---|---| | nextest (stable) | yes (`--workspace`) | passwordless sudo | | Linux musl (stable) | yes (`--workspace`), no `container:` | passwordless sudo | | macOS | no (tests six named crates only) | - | | Windows | no (`cfg(unix)`) | - | Mutations, each restored afterwards: | Mutation | Result | |---|---| | `create_root` uses a plain `std::fs::create_dir` | new-dir cell red: created below the symlink | | change_dir#3 parent walk removed | single-file cell red: exit 23 (mkstemp ELOOP) instead of 3 | | pin skipped (`pin_operator_destination(.., None)`) | both swap-after-entry cells red (push, pull); upstream `rrsync-receiver-file-arg` red ("in-place transfer changed the outside fixture"); `symlink-dirlink-basis` still passes | Upstream testsuite (rsync 3.5.2 `runtests.py`, binary copied to a file named `rsync`): master 289 passed, 87 skipped, 0 failed; this branch 289 passed, 87 skipped, 0 failed. `symlink-dirlink-basis`, `rrsync-receiver-file-arg` and `rrsync-userns-procfs` pass on both. `rrsync-userns-procfs` fails on master too when the binary is named `oc-rsync` ("cannot locate the rsync executable", rrsync-userns-procfs_test.py:31-41); the copy named `rsync` passes on master, so that failure is harness naming, not this change. ## Verification - `cargo fmt --all -- --check`, `tools/ci/check_rustfmt_all.py`: clean - `cargo clippy --workspace --all-targets --all-features --no-deps -- -D warnings`: clean - the same clippy for `x86_64-pc-windows-gnu`: clean - `cargo nextest run -p bin --test destination_root_owner_walk` (root): 9 passed - `cargo nextest run -p transfer -E 'test(dest_root) | test(receiver::)'`: 937 passed - `cargo nextest run -p core` (scoped): 17 passed - no-placeholders, citations, citation drift ratchet, dead-code census, unsafe-owner deps, test-seam gates: OK - `tools/ci/run_tools_tests.sh`: OK. The gate run once reported `FAILED (errors=1, skipped=2)`. Rerun with full logs: 5 of 5 sequential runs pass, and 3 of 6 runs made concurrently with a second copy fail with exactly that summary, from `FreePortTest.test_bind_free_port_skips_a_port_taken_since_allocation` (`OSError: [Errno 98]`). That is the concurrent-run port race fixed by #8898, not this change. Closes #8870 Part of #8863 (push line order: task 1104; daemon receiver: task 1227)
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.
What
FreePortTest.test_bind_free_port_skips_a_port_taken_since_allocationboundtaken + 1directly. It failed withOSError: [Errno 98] Address already in usewhenever another oracle run on the same host already held that port.tools/ci/interop_oracle.py:266-267offsets the port base byos.getpid() % 4, so two concurrent runs share a range one time in four. This was reproduced in 1 of 3 concurrent same-tree runs ofrun_tools_tests.sh.The test now touches only a socket it owns:
mock.patch.objectpoints_PORT_BASEat that port, resets_portsto a freshitertools.count()and setsPORTS_PER_RUNto 1, so the allocator's only candidate is the squatter's port.bind_free_portmust skip the held port and raisedaemon port range exhausted. The tap stays unbound (getsockname()port 0). The test asserts both.OverflowError.Only the test changes.
bind_free_portalready skipped a port it could not bind.Verification
except OSError: continueremoved frombind_free_port, the test fails with Errno 98. With the line in place, it passes.tools/ci/run_tools_tests.sh: 417 tests, OK (3 skipped, all pre-existing).