Skip to content

fix: follow symlink at dest_root to match upstream main.c:745-754 - #5675

Merged
oferchen merged 2 commits into
masterfrom
fix/uts-sldb-follow-symlink-dest-root
Jun 11, 2026
Merged

oferchen merged 2 commits into
masterfrom
fix/uts-sldb-follow-symlink-dest-root

Conversation

@oferchen

Copy link
Copy Markdown
Owner

Summary

The receiver-side ensure_dest_root_exists() pre-flight used symlink_metadata() (lstat) and explicitly rejected any symlinked destination with InvalidInput. Upstream main.c:745-754 (get_local_name()) instead calls do_stat() which follows symlinks, accepts the destination when S_ISDIR(st.st_mode) is true, and then enters the resolved directory via change_dir(dest_path, CD_NORMAL). The upstream symlink-dirlink-basis interop test (issue #715) drives exactly this code path with \$HOME/dir -> \$HOME/real-dir and -K (--copy-dirlinks).

This change brings the helper in line with upstream:

  • Switch to metadata() so a symlink-to-existing-directory dest is accepted.
  • Keep dangling symlinks safe: when metadata() reports NotFound, do an lstat follow-up and surface NotFound instead of letting create_dir_all resolve the link and materialize a directory at the link target.
  • Canonicalize the symlinked dest in transfer::receiver::transfer::setup::setup_transfer so the downstream DirSandbox::open_root opens the resolved target dirfd; per-entry *at syscalls then anchor at the canonical path.
  • Skip the canonicalize step under daemon connections so open_sandbox_for_dest_strict keeps refusing symlinked dests via ELOOP/ENOTDIR (the chdir-symlink-race defense).

Security model

Per-entry writes are sandboxed by the SEC-1 `*at` chain (see `crates/fast_io/src/dir_sandbox/mod.rs:150` `secure_open_dir` and `crates/transfer/src/receiver/transfer/setup.rs:310` `open_sandbox_for_dest_strict`). Opens route through `openat2(RESOLVE_BENEATH | RESOLVE_NO_SYMLINKS)` on Linux 5.6+ and `openat(O_NOFOLLOW | O_DIRECTORY)` elsewhere. Accepting a symlinked dest at the pre-flight does not weaken containment because the canonical resolved path becomes the sandbox root and every subsequent open is anchored at that resolved fd. Daemon connections keep the strict refusal at the sandbox layer.

The chdir-symlink-race tests (UTS-16) live in `crates/transfer/src/receiver/transfer/setup.rs::symlink_race_tests` and target `open_sandbox_for_dest_strict` (per-entry symlink swap, not dest-root). They test a different attack surface and remain unchanged.

Test changes

  • `crates/transfer/tests/uts_2_dest_root_mkdir.rs`:
    • Updated: `refuses_broken_symlink_to_outside_module` -> `rejects_broken_symlink_dest_root` now asserts `NotFound` (was `InvalidInput`).
    • Removed and replaced: `refuses_broken_symlink_to_intra_module_target`, `refuses_symlink_to_existing_intra_module_directory`, and `refuses_symlink_to_existing_directory_outside_module` are superseded by `accepts_symlink_to_existing_directory` and `accepts_symlink_to_existing_directory_outside_parent`, which pin the new accept-on-symlink-to-dir behavior.
    • Removed: `existing_non_directory_path_is_noop_for_helper` (the helper now errors on non-directory dests, matching upstream `main.c:756-761`); replaced by `rejects_existing_non_directory_at_dest_root`.
    • Added: `rejects_chained_dangling_symlink_dest_root` to pin the dangling-symlink fallback for a chained `link_a -> link_b -> nothing` shape.

Test plan

  • CI fmt+clippy passes
  • CI nextest (stable) passes
  • CI Windows, macOS, Linux musl pass
  • Interop test `symlink-dirlink-basis` reaches test 7 without the `real-dir/file` FileNotFoundError

@github-actions github-actions Bot added the bug Something isn't working label Jun 11, 2026
@oferchen

Copy link
Copy Markdown
Owner Author

Auto-rebase blocked: substantive conflict in crates/transfer/tests/uts_2_dest_root_mkdir.rs between this branch's accepts_symlink_to_existing_directory_outside_parent (mirrors upstream main.c:745-754 which follows the symlink) and master's newly-landed refuses_symlink_to_existing_directory_outside_module (defense-in-depth refusal at the pre-flight layer). Both can't coexist; needs author decision on policy + rebase. SEC-1 *at chain still anchors per-entry writes regardless.

@oferchen
oferchen force-pushed the fix/uts-sldb-follow-symlink-dest-root branch 2 times, most recently from 0e73b5f to fd28ea2 Compare June 11, 2026 17:59
oferchen added 2 commits June 11, 2026 21:30
The receiver-side `ensure_dest_root_exists()` pre-flight used
`symlink_metadata()` (lstat) and rejected any symlinked destination
with `InvalidInput`. Upstream `main.c:745-754` calls `do_stat()`
(follows symlinks), accepts the dest when `S_ISDIR(st.st_mode)` is
true, then enters the resolved directory via `change_dir()`. The
upstream `symlink-dirlink-basis` interop test (issue #715) drives
this path with `$HOME/dir -> $HOME/real-dir` and `-K`.

Switch the helper to `metadata()` so a symlink-to-existing-directory
dest is accepted, and surface a dangling-symlink dest as `NotFound`
via an `lstat` follow-up (instead of letting `create_dir_all`
materialize the directory at the link target). Canonicalize the
symlinked dest in `setup_transfer` so the downstream `DirSandbox`
opens the resolved target dirfd once; per-entry `*at` syscalls then
anchor at the canonical path. Skip the canonicalize step for daemon
connections so the strict `open_sandbox_for_dest_strict` path keeps
refusing symlinked dests (chdir-symlink-race defense).

Security model: per-entry writes are sandboxed by the SEC-1 `*at`
chain (`openat2(RESOLVE_BENEATH | RESOLVE_NO_SYMLINKS)` on Linux
5.6+, `openat(O_NOFOLLOW | O_DIRECTORY)` elsewhere). Accepting a
symlinked dest at the pre-flight does not weaken that containment
because the canonical resolved path becomes the sandbox root.

Tests:
- Update `uts_2_dest_root_mkdir.rs` to assert the new accept-on-
  symlink-to-dir behavior and the dangling-symlink `NotFound`
  surface; remove the four pre-PR refusal tests they superseded.
- Add a chained-dangling-symlink regression and an explicit
  rejected-non-directory test.
@oferchen
oferchen force-pushed the fix/uts-sldb-follow-symlink-dest-root branch from fd28ea2 to e49563a Compare June 11, 2026 18:30
@oferchen
oferchen merged commit 46ba15c into master Jun 11, 2026
9 checks passed
@oferchen
oferchen deleted the fix/uts-sldb-follow-symlink-dest-root branch June 12, 2026 14:39
oferchen added a commit that referenced this pull request Aug 24, 2026
)

* fix(receiver): follow symlink at dest_root to match upstream

The receiver-side `ensure_dest_root_exists()` pre-flight used
`symlink_metadata()` (lstat) and rejected any symlinked destination
with `InvalidInput`. Upstream `main.c:745-754` calls `do_stat()`
(follows symlinks), accepts the dest when `S_ISDIR(st.st_mode)` is
true, then enters the resolved directory via `change_dir()`. The
upstream `symlink-dirlink-basis` interop test (issue #715) drives
this path with `$HOME/dir -> $HOME/real-dir` and `-K`.

Switch the helper to `metadata()` so a symlink-to-existing-directory
dest is accepted, and surface a dangling-symlink dest as `NotFound`
via an `lstat` follow-up (instead of letting `create_dir_all`
materialize the directory at the link target). Canonicalize the
symlinked dest in `setup_transfer` so the downstream `DirSandbox`
opens the resolved target dirfd once; per-entry `*at` syscalls then
anchor at the canonical path. Skip the canonicalize step for daemon
connections so the strict `open_sandbox_for_dest_strict` path keeps
refusing symlinked dests (chdir-symlink-race defense).

Security model: per-entry writes are sandboxed by the SEC-1 `*at`
chain (`openat2(RESOLVE_BENEATH | RESOLVE_NO_SYMLINKS)` on Linux
5.6+, `openat(O_NOFOLLOW | O_DIRECTORY)` elsewhere). Accepting a
symlinked dest at the pre-flight does not weaken that containment
because the canonical resolved path becomes the sandbox root.

Tests:
- Update `uts_2_dest_root_mkdir.rs` to assert the new accept-on-
  symlink-to-dir behavior and the dangling-symlink `NotFound`
  surface; remove the four pre-PR refusal tests they superseded.
- Add a chained-dangling-symlink regression and an explicit
  rejected-non-directory test.

* style: apply cargo fmt to receiver dest_root + test wrappings
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