Skip to content

fix: protect receiver chdir against symlink-race TOCTOU - #5519

Merged
oferchen merged 4 commits into
masterfrom
fix/uts-16-chdir-symlink-race
Jun 10, 2026
Merged

oferchen merged 4 commits into
masterfrom
fix/uts-16-chdir-symlink-race

Conversation

@oferchen

@oferchen oferchen commented Jun 9, 2026

Copy link
Copy Markdown
Owner

Summary

Daemon receivers without chroot now refuse a leaf-symlink at the destination directory and route the client's positional dest through to the receiver instead of silently writing every transfer into the module root. Closes the chdir-symlink-race attack window (the upstream testsuite test of the same name).

Background

The upstream chdir-symlink-race test (testsuite/chdir-symlink-race.test) reproduces a post-CVE-2026-29518 TOCTOU race: the daemon receiver chdir's into a destination subdirectory while an attacker swaps that subdirectory for a symlink. Every subsequent path-relative syscall (open, chmod, lchown, ...) then inherits the escape and re-opens the same class of attack the original fix tried to close.

When run against oc-rsync the test failed at the positive control stage. The receiver was writing every push directly into the module root, ignoring the client's positional destination (upload/realdir/). That meant the attacks were vacuously safe (no write ever reached the symlink) but the test fails before exercising the security check.

Fix

Two coupled changes:

  • client_args.rs::build_server_config mirrors upstream's read_args() + glob_expand_module() (io.c:1295, util1.c:813-814). The wire positional after the . separator carries the module name prefix (e.g. upload/realdir/); we strip the bare module name and join the tail with the on-disk module path so the receiver writes into the requested subdirectory. The Generator path is left at the module root for now to keep the daemon pull tests stable. The resulting connection is tagged is_daemon_connection = true so the strict symlink-refusal branch fires.
  • receiver/transfer/setup.rs gains open_sandbox_for_dest_strict, which promotes ELOOP / ENOTDIR / EXDEV from the DirSandbox open into a transfer error when the connection is a daemon. ENOENT and generic errors keep the existing soft-fall-back so first-run pushes still create the destination. This mirrors upstream's use_secure_symlinks = am_daemon && !am_chrooted gate (clientserver.c:1018) and change_dir()'s secure_relative_open() + fchdir() branch (util1.c:1175-1216).

DirSandbox::open_root already uses openat2(RESOLVE_BENEATH | RESOLVE_NO_SYMLINKS) on Linux 5.6+ (via fast_io::secure_open_dir) and open(O_NOFOLLOW | O_DIRECTORY) on macOS / older Linux, so no new syscall plumbing was needed - only the refusal-on-failure policy.

Test plan

  • Unit tests for the new arg-rewrite helpers (extract_module_relative_paths, resolve_receiver_dest) cover the bare-module, subpath, sibling-prefix and absolute-arg edges.
  • Unit tests for open_sandbox_for_dest_strict confirm: strict mode refuses symlink dests with ELOOP/ENOTDIR, non-strict mode preserves the soft fall-back, real directories open under strict mode, missing destinations soft-fail (so first-run pushes still mkdir).
  • CI: fmt + clippy + nextest matrix.
  • Manual: upstream testsuite chdir-symlink-race passes on Linux when the oc-rsync binary is the daemon-side peer.

@github-actions github-actions Bot added the bug Something isn't working label Jun 9, 2026
oferchen added 2 commits June 10, 2026 05:58
Daemon receivers without chroot now refuse a leaf-symlink at the
destination directory and route the client's positional dest through
to the receiver instead of silently writing every transfer into the
module root.

Two coupled changes close the chdir-symlink-race attack window:

* `client_args.rs::build_server_config` now mirrors upstream's
  `read_args()` + `glob_expand_module()` (io.c:1295, util1.c:813-814).
  The wire positional after the `.` separator carries the module name
  prefix (e.g. `upload/realdir/`); we strip the bare module name and
  join the tail with the on-disk module path so the receiver writes
  into the requested subdirectory. The Generator path is left at the
  module root for now to keep the daemon pull tests stable. The
  resulting connection is tagged `is_daemon_connection = true` so the
  strict symlink-refusal branch fires.
* `receiver/transfer/setup.rs` gains `open_sandbox_for_dest_strict`,
  which promotes ELOOP / ENOTDIR / EXDEV from the DirSandbox open
  into a transfer error when the connection is a daemon. ENOENT and
  generic errors keep the existing soft-fall-back so first-run pushes
  still create the destination. This mirrors upstream's
  `use_secure_symlinks = am_daemon && !am_chrooted` gate
  (clientserver.c:1018) and `change_dir()`'s
  `secure_relative_open() + fchdir()` branch (util1.c:1175-1216).

Unit tests cover the new helpers and the symlink-refusal branches at
both the daemon arg-rewrite layer and the receiver sandbox layer.
@oferchen
oferchen force-pushed the fix/uts-16-chdir-symlink-race branch from b3b1571 to b7beb33 Compare June 10, 2026 02:58
oferchen added 2 commits June 10, 2026 06:30
io::ErrorKind::FilesystemLoop and NotADirectory are still gated behind the unstable io_error_more feature, which blocks stable builds (sync-async-wire-parity + fmt+clippy). Embed the raw errno in the wrapped error message and assert on that substring in the symlink-race test instead. Also collapses a trim_start_matches closure into a char-array pattern to satisfy clippy::manual_pattern_char_comparison.
oferchen added a commit that referenced this pull request Jun 10, 2026
Group PRs #5510, #5516, #5519, #5520, #5523, #5525-#5544 by shared root
cause rather than by Stream bucket. Seven clusters surface; the parser-
stores-string and field-vs-method shadowing clusters cover the largest
share of the series, and the receiver-driver wiring-parity gap explains
the NDX_DEL_STATS goodbye misses in both #5536 and #5542. Includes a
defense-in-depth audit mapping each cluster onto the rust-landlock
ruleset wired at crates/fast_io/src/landlock.rs.

Doc-only; no code paths touched.
oferchen added a commit that referenced this pull request Jun 10, 2026
Group PRs #5510, #5516, #5519, #5520, #5523, #5525-#5544 by shared root
cause rather than by Stream bucket. Seven clusters surface; the parser-
stores-string and field-vs-method shadowing clusters cover the largest
share of the series, and the receiver-driver wiring-parity gap explains
the NDX_DEL_STATS goodbye misses in both #5536 and #5542. Includes a
defense-in-depth audit mapping each cluster onto the rust-landlock
ruleset wired at crates/fast_io/src/landlock.rs.

Doc-only; no code paths touched.
@oferchen
oferchen merged commit bc31415 into master Jun 10, 2026
70 checks passed
@oferchen
oferchen deleted the fix/uts-16-chdir-symlink-race branch June 10, 2026 10:29
oferchen added a commit that referenced this pull request Aug 24, 2026
Group PRs #5510, #5516, #5519, #5520, #5523, #5525-#5544 by shared root
cause rather than by Stream bucket. Seven clusters surface; the parser-
stores-string and field-vs-method shadowing clusters cover the largest
share of the series, and the receiver-driver wiring-parity gap explains
the NDX_DEL_STATS goodbye misses in both #5536 and #5542. Includes a
defense-in-depth audit mapping each cluster onto the rust-landlock
ruleset wired at crates/fast_io/src/landlock.rs.

Doc-only; no code paths touched.
oferchen added a commit that referenced this pull request Aug 24, 2026
* fix: protect receiver chdir against symlink-race TOCTOU

Daemon receivers without chroot now refuse a leaf-symlink at the
destination directory and route the client's positional dest through
to the receiver instead of silently writing every transfer into the
module root.

Two coupled changes close the chdir-symlink-race attack window:

* `client_args.rs::build_server_config` now mirrors upstream's
  `read_args()` + `glob_expand_module()` (io.c:1295, util1.c:813-814).
  The wire positional after the `.` separator carries the module name
  prefix (e.g. `upload/realdir/`); we strip the bare module name and
  join the tail with the on-disk module path so the receiver writes
  into the requested subdirectory. The Generator path is left at the
  module root for now to keep the daemon pull tests stable. The
  resulting connection is tagged `is_daemon_connection = true` so the
  strict symlink-refusal branch fires.
* `receiver/transfer/setup.rs` gains `open_sandbox_for_dest_strict`,
  which promotes ELOOP / ENOTDIR / EXDEV from the DirSandbox open
  into a transfer error when the connection is a daemon. ENOENT and
  generic errors keep the existing soft-fall-back so first-run pushes
  still create the destination. This mirrors upstream's
  `use_secure_symlinks = am_daemon && !am_chrooted` gate
  (clientserver.c:1018) and `change_dir()`'s
  `secure_relative_open() + fchdir()` branch (util1.c:1175-1216).

Unit tests cover the new helpers and the symlink-refusal branches at
both the daemon arg-rewrite layer and the receiver sandbox layer.

* style: wrap resolve_receiver_dest signature for rustfmt

* fix: handle Windows absolute paths and verify ErrorKind in symlink-race fix

* fix: use stable errno instead of unstable ErrorKind variants

io::ErrorKind::FilesystemLoop and NotADirectory are still gated behind the unstable io_error_more feature, which blocks stable builds (sync-async-wire-parity + fmt+clippy). Embed the raw errno in the wrapped error message and assert on that substring in the symlink-race test instead. Also collapses a trim_start_matches closure into a char-array pattern to satisfy clippy::manual_pattern_char_comparison.
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