Skip to content

fix: enter a local copy's destination like change_dir does - #8890

Merged
oferchen merged 1 commit into
masterfrom
fix/single-file-destination-walk
Nov 27, 2026
Merged

oferchen merged 1 commit into
masterfrom
fix/single-file-destination-walk

Conversation

@oferchen

Copy link
Copy Markdown
Owner

Summary

A local copy into a single-file destination, or into a destination directory the run creates, resolved the destination with plain path calls. A symlink owned by another user in the leading directories was followed, so the copy wrote outside the operator's tree. rsync 3.5.2 enters the destination with change_dir() and refuses that symlink.

This change makes the local copy enter its destination the way get_local_name() does (main.c:777-868):

  • change_dir#1: an existing destination directory is entered through the ownership walk and pinned.
  • change_dir#2: a directory the run creates is entered and pinned after the mkdir.
  • change_dir#3: the parent of a single-file destination is entered and pinned, and every later open, rename and link of that entry goes through the pinned descriptor.
  • --mkpath builds the missing leading directories per component, as make_path() does. A relative destination uses do_mkdir_at()'s confined arm (syscall.c:2306-2332): an in-tree symlink is followed, one that leaves the tree fails with mkdir "<dest>" failed: Too many levels of symbolic links (40), exit 11.

A symlink the user owns is still followed, as in 3.5.2. --insecure-links keeps the old behaviour.

Output and exit codes

For each refused case oc prints 3.5.2's refusal line, then change_dir#N "<path>" failed: Too many levels of symbolic links (40), and exits 3. The "dest/" spelling of change_dir#1 keeps the operand's trailing slash, as 3.5.2 does. Messages queued before the failure are printed before the failure report, so the order matches 3.5.2.

Deliberate deviation from rsync 3.5.2

Case rsync 3.5.2 oc
A directory the run creates below another user's symlink: the created destination directory, and the missing parents of an absolute --mkpath destination A plain mkdir follows the symlink and creates the directory in the other user's tree (do_mkdir(), or make_path() creating outside/new), prints created directory under -v, then refuses at change_dir#2 / change_dir#3, exit 3 Walks the leading directories and refuses before any mkdir. Same refusal lines and exit 3; nothing is created and no created directory line is printed. Cells: a_directory_created_for_several_entries_under_an_untrusted_symlink_is_refused, a_directory_created_for_a_trailing_slash_under_an_untrusted_symlink_is_refused, mkpath::an_absolute_destination_through_a_foreign_symlink_is_refused_before_any_mkdir

A symlink the user owns is followed in both, so there is no difference there.

The mkdir that 3.5.2 performs first is the very write through an untrusted symlink that the CVE-2026-53796 fix exists to stop. Upstream master's get_local_name() and do_mkdir() are unchanged from 3.5.2, and the 4.0.0 milestone plans no change here.

Tests

  • Core cells for the single-file (immediate and interior symlink), created-directory and existing-directory cases, each refused for a foreign owner and followed for the user's own symlink, run unprivileged through the ownership test seam.
  • fast_io cells: a single-file commit lands in the pinned parent after the parent is swapped for a symlink, with a control showing an unpinned commit follows the swap; confined make_path cells for in-tree, absolute-target and climbing symlinks.
  • Measured with a real symlink owned by uid 65534 against rsync 3.5.2: local single-file and created-directory runs print the same lines and exit 3 (apart from the deviation above).
  • Stderr order: the cli and bin suites with the 3.5.2 oracle (4,800 tests) pass unchanged.

Cost

strace -c, local copy of 10,000 files (100 directories of 100) into an existing directory, two runs each:

syscall master this change
openat 20,346 20,346
openat2 10,001 10,001
newfstatat 20,420 20,420
statx 30,227 30,225
mkdir 100 100
getdents64 210 210

Scope

This change covers the local copy.

Rebase note

#8882 added a mode-000 destination-root check in core's client run path. This change removes it, because the engine now enters an existing destination the way change_dir#1 does (main.c:776-781) and reports that failure itself. Measured on a mode-000 destination root, master b56e3a1 against this branch:

Leg master this branch rsync 3.5.2
local exit 3, failed to access destination directory '<B>/dest/': Permission denied (13) exit 3, [Receiver] change_dir#1 "<B>/dest/" failed: Permission denied (13) and the code-3 [Receiver=] trailer exit 3, same two lines
push exit 3, change_dir#1 line and code-3 trailer identical bytes exit 3, same lines
pull exit 3, change_dir#1 line and code-3 trailer identical except the trailer's own source line, error.rs(443) -> error.rs(445) exit 3, same lines plus the sender's received SIGUSR1 line

The local row changes on purpose: it now prints rsync 3.5.2's lines. The dry-run pull matches the pull row. receiver_preflight_refusals runs all 8 cells unprivileged, none skipped. With the engine change_dir#1 entry removed, a_local_copy_refuses_an_unsearchable_destination_root fails (exit 0 instead of 3), so that cell exercises the new path. That cell now asserts the change_dir#1 "<dest>/" failed: Permission denied (13) line and the code-3 trailer on every leg, local included, and passes against the rsync 3.5.2 oracle too.

Remaining differences

Closes #8858

A local copy into a single-file destination, or into a destination
directory the run creates, resolved the destination with plain path
calls, so a symlink owned by another user in the leading directories
was followed and the copy wrote outside the operator's tree.

The engine now enters the destination the way rsync 3.5.2's
get_local_name() does (main.c:757-868): change_dir#1 for an existing
directory, change_dir#2 after creating one, change_dir#3 for a
single-file destination's parent. Each entry goes through the
ownership walk and pins the entered descriptor. A refusal prints
upstream's line and exits 3.

--mkpath builds missing parents per component like make_path(); a
relative destination uses do_mkdir_at()'s confined arm
(fast_io::make_path_beneath_cwd), which fails with ELOOP on a symlink
that leaves the tree.

Deliberate deviation: a directory the run would create below another
user's symlink is refused before the mkdir; rsync 3.5.2 creates it
first and refuses afterwards.

Closes #8858
@oferchen oferchen added this to the 0.6.5 milestone Nov 27, 2026
@github-actions github-actions Bot added the bug Something isn't working label Nov 27, 2026
@oferchen
oferchen merged commit a3f5687 into master Nov 27, 2026
91 checks passed
@oferchen
oferchen deleted the fix/single-file-destination-walk branch November 27, 2026 14:15
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)
oferchen added a commit that referenced this pull request Nov 28, 2026
## Summary

rsync 3.5.2 prints one line when the receiver cannot enter its
destination:
`rsyserr(FERROR, errno, "change_dir#N %s failed", full_fname(..))` in
`get_local_name()`:

- `change_dir#1` (main.c:779): an existing destination directory
- `change_dir#2` (main.c:837): a destination directory the run created
- `change_dir#3` (main.c:866, main.c:982): the parent of a single-file
destination

`rsyserr()` appends `: <strerror> (<errno>)` (log.c:575-585).

oc wrote this text in three places:

- `transfer` `receiver/dest_root.rs`: the `SelectStep` Display (push and
pull receivers)
- `core` `client/error.rs`: the local-copy error mapping
- `engine` `local_copy/error.rs`: the `DestinationEntryFailed` message

All three now render through one formatter, `logging::ChangeDirFailed`.
`logging` is the only crate all three already depend on (`transfer` does
not depend on `engine`).

## Design

- `logging::ChangeDirStep { One, Two, Three }`. `change_dir#4`
  (main.c:1214) has no caller in oc, so it gets no variant.
- `logging::ChangeDirFailed::new(step, fname, cause)` renders
`change_dir#N <fname> failed: <upstream_errno_text>`. The `rsync: [role]
`
  prefix stays with the caller, as before.
- `fname` arrives already rendered by `full_fname()`. Each caller keeps
its
  own name rules: `change_dir#1` keeps the operand's trailing slash
  (main.c:779 prints `dest_path` as given); `change_dir#2` lops it
  (main.c:810).
- Callers keep their own step enums and map them onto `ChangeDirStep`.
  `engine::DestinationEntry::number()` becomes `change_dir_step()`.

## Output

User-visible text is unchanged. The core and transfer lines were already
byte-for-byte upstream's, and the call sites now produce the same bytes
through the shared formatter. Engine's `DestinationEntryFailed` Display
is
not printed to users (core's `map_local_copy_error` builds the
user-visible
line); it now uses `full_fname()` like the other two, not `"{path}"`.

Oracle cells for #8890 (local copy) and #8899 (network receivers), run
on
master and on this branch, with `OC_RSYNC_UPSTREAM_BIN_3_5_2` set to
rsync
3.5.2 and `OC_RSYNC_UPSTREAM_COMPAT=1` so 3.5.2 is required:

| Target | master | this branch |
|---|---|---|
| `core --test untrusted_destination_symlink` | 14/14 | 14/14 |
| `cli --test receiver_preflight_refusals --test
mkpath_through_a_regular_file` | 12/12 | 12/12 |
| `bin --test destination_root_owner_walk` (root via sudo) | 9/9 | 9/9 |
| `transfer --lib test(dest_root)` | 4/4 | 4/4 |
| `core --lib test(change_dir) \| test(destination_entry)` | 2/2 | 2/2 |
| `engine --lib test(change_dir) \| test(destination_entry)` | 3/3 | 3/3
|

The same 44 cells by name, all passing, before and after.

## Tests

New `logging` unit table (`change_dir.rs`):

- Each step is checked against upstream's literal format string
(main.c:779,
  :837, :866), with `%s` filled in and the `rsyserr()` tail appended.
- The errno comes from a real failed `read_dir`, so the tail is the
  platform's own strerror text, not a string the test writes.
- A second test checks the name is printed as given.

Mutations, each restored afterwards:

| Mutation | logging unit | core local-copy cells | bin network cell |
|---|---|---|---|
| (a) `change_dir #N` (space added) | 2/2 red | 6/14 red | 1/9 red |
| (b) steps Two and Three swapped | 2/2 red | 5/14 red | 1/9 red |
| (c) ` failed` dropped | 2/2 red | 6/14 red | 1/9 red |

A mutation in the shared formatter turns the core and bin cells red, so
both
the local-copy path and the network receivers go through it.

## 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
- `RUSTDOCFLAGS="-D warnings" cargo doc --workspace --all-features
--no-deps --locked`: clean
- `cargo run --locked -p xtask -- no-placeholders`: OK
- `python3 tools/ci/citation_drift_audit.py --ratchet`: OK
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.

Local copy to a single-file destination writes through another user's symlink (CVE-2026-53796 residual)

1 participant