Skip to content

fix: refuse a bad --backup-dir and an unenterable dest root - #8882

Merged
oferchen merged 2 commits into
masterfrom
fix/refuse-like-upstream-four-cases
Nov 27, 2026
Merged

oferchen merged 2 commits into
masterfrom
fix/refuse-like-upstream-four-cases

Conversation

@oferchen

Copy link
Copy Markdown
Owner

Summary

Two receiver pre-flight refusals rsync 3.5.2 makes before it transfers
anything, which oc-rsync either reported with the wrong exit code or did not
make at all.

--backup-dir that is not a directory

rsync 3.5.2 main.c:1036-1057 do_recv() stats the backup directory once,
after get_local_name() has entered the destination, so a relative value
resolves against the destination:

  • the path exists but is not a directory: The backup-dir is not a directory: %s, RERR_SYNTAX (1);
  • stat fails for any reason but ENOENT (for example a path below a regular file, ENOTDIR): Failed to stat %s: %s, RERR_FILEIO (11);
  • ENOENT is accepted; the first backup creates it.

The name shown is backup_dir_buf: leading ./ pairs stripped and one
trailing slash removed (options.c:2514-2530, main.c:1039-1040).

oc-rsync never ran this check. It started the transfer and failed at the first
backup: exit 23 on a local copy, exit 11 with a keep_backup failed line on a
push or pull, and no refusal at all when no file needed backing up.

The fix adds transfer::check_backup_dir, shared by both receivers:

  • the client receiver (local copy, pull) checks before connecting, next to the
    existing --temp-dir check and ahead of it, as upstream orders them;
  • the --server receiver (push, daemon push) checks right before its
    --temp-dir check and sends the line to the client, as
    check_server_temp_dir does.

Destination root the receiver cannot enter

rsync 3.5.2 main.c:776-781 get_local_name() does change_dir() into an
existing destination directory and, when that fails, reports
change_dir#1 "<dest>/" failed: <strerror> and exits RERR_FILESELECT (3),
dry run included.

The local copy already exited 3. A pull or push into a mode-000 root did not:
the receiver went on, failed every entry, and exited 23; a dry-run pull exited
0. ensure_dest_root() now stats <dest>/. for an existing directory. Like
chdir, that needs search permission and not read permission. A failure
becomes a new SelectStep::ChangeDir1, which the existing DestRootSelectError
reporting already renders with upstream's role and exit code.

Before / after vs rsync 3.5.2

Measured on Linux (glibc, non-root) with a 3.5.2 build that has xxhash, zstd and lz4.

Case Leg rsync 3.5.2 oc before oc after
--backup-dir=<abs>/reg/sub (below a file) local 11 23 11
same push 11 11 (keep_backup failed) 11 (Failed to stat ...)
same pull 11 11 (keep_backup failed) 11 (Failed to stat ...)
--backup-dir=reg/sub (relative) daemon push 11 11 (keep_backup failed) 11 (Failed to stat reg/sub: ...)
--backup-dir=<abs>/reg (a file) local 1 23 1
--backup-dir=../reg (a file) push 1 not refused 1
mode-000 destination root local 3 3 3
same pull 3 23 3
same push 3 23 3
same dry-run pull 3 0 3

Not changed: --checksum-choice=xxh128 --protocol=29 and --compress-choice=lz4 --protocol=30

These were reported as cases where 3.5.2 exits 4. It does not. 3.5.2 has no
protocol gate on either choice (checksum.c:192-226 parse_checksum_choice(),
compat.c:183-221 parse_compress_choice()). The exit 4 comes from builds
compiled without xxhash or lz4, which print unknown checksum name: xxh128 or
unknown compress name: lz4. A 3.5.2 build with both libraries exits 0 on every
leg (local, ssh push and pull, daemon push and pull), and so does oc-rsync. No
change was made for these.

Tests

crates/cli/tests/receiver_preflight_refusals.rs: every cell asserts the
measured 3.5.2 exit code and message, and that the old destination file is
untouched (nothing was transferred). With the 3.5.2 oracle available, each cell
also runs 3.5.2 at both ends and in both mixed pairings.

  • backup-dir: four spellings (relative and absolute, below a file and a file)
    across local, push, pull and daemon push. The absolute spellings skip the
    daemon, which sanitizes them (options.c:2473).
  • unsearchable root: local, push, pull, dry-run pull. These skip when run as
    root, because root ignores mode 000.

Unit tests in transfer::receiver::backup_dir cover the displayed-name
normalization, ENOENT acceptance, trailing-slash handling and
destination-relative resolution.

Mutation check, with the 3.5.2 oracle required (OC_RSYNC_UPSTREAM_COMPAT=1):

  • with both fixes, all 8 tests pass;
  • with check_backup_dir() short-circuited to Ok(()), all 4 backup-dir tests
    fail (local, push, pull, daemon push);
  • with the <dest>/. probe short-circuited, the push, pull and dry-run pull
    unsearchable-root tests fail. The local cell still passes: the local copy
    already exited 3 before this change, and that cell guards against
    regressions.

Remaining differences

  • The local copy still words the mode-000 refusal failed to access destination directory '<dest>/' instead of change_dir#1 "<dest>/" failed. The exit code
    matches. The same function also words the destination symlink-walk refusal,
    so I left the rewording for a separate change.
  • Client-side refusals keep the existing oc-rsync error: <text> (code N) at ...
    single-line form that the --temp-dir refusals already use, not upstream's
    two lines.

Closes #8881

The receiver stats the backup directory before it transfers anything,
resolved against the destination, as rsync 3.5.2 does in do_recv()
(main.c:1036-1057). A path that exists but is not a directory ends the
run with "The backup-dir is not a directory: %s" and exit 1; any stat
failure except ENOENT, such as a path below a regular file, ends it with
"Failed to stat %s: %s" and exit 11. The name shown is backup_dir_buf:
leading "./" pairs stripped and one trailing slash removed
(options.c:2514-2530, main.c:1039-1040).

Previously the transfer started and failed at the first backup (exit 23
locally, a keep_backup error remotely), or did not fail at all when no
file needed a backup.

The client receiver checks before connecting, ahead of --temp-dir as
upstream orders them; the --server receiver checks before its --temp-dir
check and sends the line to the client.
rsync 3.5.2 get_local_name() (main.c:776-781) changes into an existing
destination directory and, when that fails, reports
change_dir#1 "<dest>/" failed: <strerror> and exits 3, dry run included.

A pull or push into a mode-000 destination root carried on instead,
failed every entry and exited 23; a dry-run pull exited 0. The receiver
pre-flight now stats "<dest>/." for an existing destination directory,
which like chdir needs search permission and not read permission, and
reports a failure as a new change_dir#1 select step through the existing
get_local_name() refusal path.
@oferchen oferchen added this to the v0.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 ae78f79 into master Nov 27, 2026
82 of 90 checks passed
@oferchen
oferchen deleted the fix/refuse-like-upstream-four-cases branch November 27, 2026 10:27
oferchen added a commit that referenced this pull request Nov 27, 2026
## 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

- Network receivers: #8870, #8863.

Closes #8858
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.

Receiver setup does not refuse a bad --backup-dir or an unsearchable destination root

1 participant