Skip to content

Handle too-old peer protocol advertisements gracefully - #6

Merged
oferchen merged 1 commit into
masterfrom
codex/implement-rsync-protocol-32-parity-in-rust
Oct 15, 2025
Merged

oferchen merged 1 commit into
masterfrom
codex/implement-rsync-protocol-32-parity-in-rust

Conversation

@oferchen

@oferchen oferchen commented Oct 15, 2025 •

Copy link
Copy Markdown
Owner

Summary

  • keep track of the minimum protocol version a peer advertised below the supported range
  • ignore those too-old offers when a compatible protocol is available and surface the stored value if no agreement is possible
  • exercise the new behaviour with unit tests for mixed, empty, and invalid peer advertisements

Testing

  • cargo test

@oferchen
oferchen merged commit 81084f6 into master Oct 15, 2025
@oferchen
oferchen deleted the codex/implement-rsync-protocol-32-parity-in-rust branch October 15, 2025 11:15
oferchen added a commit that referenced this pull request May 22, 2026
#4716)

Adds four new SEC-1 helpers in `fast_io::dir_sandbox::at_syscalls`:

- `openat(dirfd, name, flags, mode) -> File` raw libc wrapper.
- `openat_via_sandbox_or_fallback(...)` adaptor that takes the sandbox
  fast path on a single-component leaf beneath `dest_dir` and falls back
  to `std::fs::OpenOptions` against `link_path` with best-effort `O_*`
  bit translation otherwise.
- `readlinkat(dirfd, name) -> PathBuf` raw libc wrapper with a growing
  buffer (256B -> PATH_MAX, doubling each round trip).
- `readlinkat_via_sandbox_or_fallback(...)` adaptor mirroring the
  openat shape but falling back to `std::fs::read_link`.

The helpers follow the established SEC-1 pattern: pinning the parent
via the sandbox dirfd closes the TOCTOU window between path walk and
the kernel reaching the inode. The `*_via_sandbox_or_fallback` adaptors
keep behaviour byte-identical for callers that have not yet plumbed a
`DirSandbox`, so individual call sites can be cut over one at a time.

This PR adds the helpers and re-exports them through
`dir_sandbox::mod` and `fast_io::lib`. It deliberately does NOT wire
any caller; per-site wiring lands in follow-up PRs so each cutover gets
its own diff and review.

Extends the existing tests module with 10 new tests covering:
- raw `openat` success and ENOENT,
- `openat_via_sandbox_or_fallback` fast path, multi-component
  fallback, and absent-sandbox fallback,
- raw `readlinkat` success and EINVAL on a non-symlink,
- `readlinkat_via_sandbox_or_fallback` fast path, EINVAL, and
  multi-component fallback.

All helpers are `#[cfg(unix)]` (the `dir_sandbox` module is itself
Unix-only); Windows continues to use path-based stdlib opens per the
SEC-1.l NTFS handle audit.

Closes (post-wire) 16+ GAPs from the SEC-1 path-syscall coverage audit
in PR #4710: #4 (readlinkat) plus the 9 direct openat sites (#9-#17)
and lays foundations for #5, #20 (open + readdir-loop) and #6, #27
(recursive *at peel).

Refs PR #4710.
oferchen added a commit that referenced this pull request May 22, 2026
Ship `recursive_unlinkat_via_sandbox_or_fallback` in
`crates/fast_io/src/dir_sandbox/at_syscalls.rs`, the dirfd-anchored
recursive remove helper SEC-1.q needs for `DeleteFs::remove_dir_all_at`
and the receiver's `delete_extraneous_files` recursive branch (audit
rows #6 and #27 in `docs/audits/sec-1-path-syscall-audit-2026-05-22.md`).

The sandbox path opens the descent root with
`openat(parent_dirfd, leaf, O_DIRECTORY | O_NOFOLLOW | O_RDONLY |
O_CLOEXEC)` so a symlink at the leaf is refused with `ELOOP`, snapshots
the children through `fdopendir` + `readdir(3)`, re-anchors the
per-entry `fstatat`/`unlinkat` calls on a freshly opened dirfd, and
finishes with `unlinkat(.., AT_REMOVEDIR)` against the original
parent. A `(dev, ino)` cycle detector tripped at re-entry returns
`io::ErrorKind::FilesystemLoop`; a classify-vs-act TOCTOU
(`was-file-now-dir`) retries once with `UnlinkFlags::Dir` and
otherwise logs and steps over. `ENOENT` on the root or a child is
treated as idempotent success (matching upstream
`delete.c:48-176`), and `EACCES` on a single child is logged via
`tracing::debug!` and stepped over.

The fallback path delegates to `std::fs::remove_dir_all` for callers
that have not yet plumbed a `DirSandbox` (test fixtures, client-side
`--local` copies, `--inplace` without daemon gating). The fallback
intentionally absorbs `ErrorKind::NotFound` so both paths share an
idempotent-root contract.

Helper-only: no caller wiring, no trait changes. SEC-1.q will land
the `DeleteFs::remove_dir_all_at` extension and the
`DeleteEmitter::emit_all` / `receiver::directory::deletion` cutovers
as a follow-up.

Tests cover the eight cases in section 7 of the design doc that do
not require root: three-deep tree happy path, idempotent ENOENT
(sandbox + fallback), ELOOP on a symlink at the descent root,
symlinks inside the tree are unlinked without dereference, fallback
equivalence vs `std::fs::remove_dir_all`, multi-component relative
falls back, ENOTDIR on a non-directory leaf, and a wide directory
that wraps several internal `readdir` buffer fills. The hardlink-cycle
test from the design requires `CAP_SYS_ADMIN` and is deferred to a
gated integration test in the SEC-1.q follow-up.
oferchen added a commit that referenced this pull request May 22, 2026
#4726)

* feat(fast_io): recursive_unlinkat sandbox helper for SEC-1.q (SEC-1.s)

Ship `recursive_unlinkat_via_sandbox_or_fallback` in
`crates/fast_io/src/dir_sandbox/at_syscalls.rs`, the dirfd-anchored
recursive remove helper SEC-1.q needs for `DeleteFs::remove_dir_all_at`
and the receiver's `delete_extraneous_files` recursive branch (audit
rows #6 and #27 in `docs/audits/sec-1-path-syscall-audit-2026-05-22.md`).

The sandbox path opens the descent root with
`openat(parent_dirfd, leaf, O_DIRECTORY | O_NOFOLLOW | O_RDONLY |
O_CLOEXEC)` so a symlink at the leaf is refused with `ELOOP`, snapshots
the children through `fdopendir` + `readdir(3)`, re-anchors the
per-entry `fstatat`/`unlinkat` calls on a freshly opened dirfd, and
finishes with `unlinkat(.., AT_REMOVEDIR)` against the original
parent. A `(dev, ino)` cycle detector tripped at re-entry returns
`io::ErrorKind::FilesystemLoop`; a classify-vs-act TOCTOU
(`was-file-now-dir`) retries once with `UnlinkFlags::Dir` and
otherwise logs and steps over. `ENOENT` on the root or a child is
treated as idempotent success (matching upstream
`delete.c:48-176`), and `EACCES` on a single child is logged via
`tracing::debug!` and stepped over.

The fallback path delegates to `std::fs::remove_dir_all` for callers
that have not yet plumbed a `DirSandbox` (test fixtures, client-side
`--local` copies, `--inplace` without daemon gating). The fallback
intentionally absorbs `ErrorKind::NotFound` so both paths share an
idempotent-root contract.

Helper-only: no caller wiring, no trait changes. SEC-1.q will land
the `DeleteFs::remove_dir_all_at` extension and the
`DeleteEmitter::emit_all` / `receiver::directory::deletion` cutovers
as a follow-up.

Tests cover the eight cases in section 7 of the design doc that do
not require root: three-deep tree happy path, idempotent ENOENT
(sandbox + fallback), ELOOP on a symlink at the descent root,
symlinks inside the tree are unlinked without dereference, fallback
equivalence vs `std::fs::remove_dir_all`, multi-component relative
falls back, ENOTDIR on a non-directory leaf, and a wide directory
that wraps several internal `readdir` buffer fills. The hardlink-cycle
test from the design requires `CAP_SYS_ADMIN` and is deferred to a
gated integration test in the SEC-1.q follow-up.

* style(fast_io): apply rustfmt to SEC-1.s helper tests

* fix(fast_io): use ELOOP errno instead of unstable ErrorKind::FilesystemLoop

* test(fast_io): accept ENOTDIR or ELOOP for symlink-at-descent-root refusal
oferchen added a commit that referenced this pull request May 22, 2026
…-1.q2) (#4732)

* feat(transfer): thread DirSandbox through receiver --delete path (SEC-1.q2)

Replace the three unanchored syscalls in delete_extraneous_files with
their sandbox-anchored siblings so a TOCTOU symlink swap on a top-level
extraneous entry cannot redirect the listing or the unlink to an
attacker-chosen inode outside the destination tree.

Adds fast_io::read_dir_via_sandbox_or_fallback (mirrors the existing
*_via_sandbox_or_fallback shape: openat(O_DIRECTORY | O_NOFOLLOW) +
fdopendir for single-component scans, std::fs::read_dir fallback
otherwise). Closes audit rows #5, #6, #7 from the SEC-1 path-syscall
audit.

Mirrors the SEC-1.r temp_guard/temp_cleanup carrier-threading pattern
(PR #4723): the Option<Arc<DirSandbox>> is cloned once into the
map_blocking worker closure and forwarded to every helper call.
Sandbox-off and multi-component paths take the documented identical-
behaviour fallback. No transfer-crate unsafe is introduced; all unsafe
stays behind the existing fast_io safe wrappers.

Windows continues through std::fs::remove_file / remove_dir_all /
read_dir (NTFS handle semantics already close the symlink-swap window
per the SEC-1.l audit).

* fix(engine): use Components::next() walk for plan_directory_to_relative

SkipWhile<Components, _> has no as_path() method on stable Rust 1.88;
the previous skip_while-then-as_path call introduced in SEC-1.q (#4728)
slipped through that PR's CI but breaks fmt+clippy on every subsequent
master CI run. Switch to a manual Components::next() walk that consumes
the leading Prefix/RootDir/CurDir components, then calls as_path() on
the unwrapped Components iterator.

* fix(transfer): cfg-gate rel_for_unlink to unix to silence Windows unused-var
oferchen added a commit that referenced this pull request Aug 24, 2026
#4716)

Adds four new SEC-1 helpers in `fast_io::dir_sandbox::at_syscalls`:

- `openat(dirfd, name, flags, mode) -> File` raw libc wrapper.
- `openat_via_sandbox_or_fallback(...)` adaptor that takes the sandbox
  fast path on a single-component leaf beneath `dest_dir` and falls back
  to `std::fs::OpenOptions` against `link_path` with best-effort `O_*`
  bit translation otherwise.
- `readlinkat(dirfd, name) -> PathBuf` raw libc wrapper with a growing
  buffer (256B -> PATH_MAX, doubling each round trip).
- `readlinkat_via_sandbox_or_fallback(...)` adaptor mirroring the
  openat shape but falling back to `std::fs::read_link`.

The helpers follow the established SEC-1 pattern: pinning the parent
via the sandbox dirfd closes the TOCTOU window between path walk and
the kernel reaching the inode. The `*_via_sandbox_or_fallback` adaptors
keep behaviour byte-identical for callers that have not yet plumbed a
`DirSandbox`, so individual call sites can be cut over one at a time.

This PR adds the helpers and re-exports them through
`dir_sandbox::mod` and `fast_io::lib`. It deliberately does NOT wire
any caller; per-site wiring lands in follow-up PRs so each cutover gets
its own diff and review.

Extends the existing tests module with 10 new tests covering:
- raw `openat` success and ENOENT,
- `openat_via_sandbox_or_fallback` fast path, multi-component
  fallback, and absent-sandbox fallback,
- raw `readlinkat` success and EINVAL on a non-symlink,
- `readlinkat_via_sandbox_or_fallback` fast path, EINVAL, and
  multi-component fallback.

All helpers are `#[cfg(unix)]` (the `dir_sandbox` module is itself
Unix-only); Windows continues to use path-based stdlib opens per the
SEC-1.l NTFS handle audit.

Closes (post-wire) 16+ GAPs from the SEC-1 path-syscall coverage audit
in PR #4710: #4 (readlinkat) plus the 9 direct openat sites (#9-#17)
and lays foundations for #5, #20 (open + readdir-loop) and #6, #27
(recursive *at peel).

Refs PR #4710.
oferchen added a commit that referenced this pull request Aug 24, 2026
#4726)

* feat(fast_io): recursive_unlinkat sandbox helper for SEC-1.q (SEC-1.s)

Ship `recursive_unlinkat_via_sandbox_or_fallback` in
`crates/fast_io/src/dir_sandbox/at_syscalls.rs`, the dirfd-anchored
recursive remove helper SEC-1.q needs for `DeleteFs::remove_dir_all_at`
and the receiver's `delete_extraneous_files` recursive branch (audit
rows #6 and #27 in `docs/audits/sec-1-path-syscall-audit-2026-05-22.md`).

The sandbox path opens the descent root with
`openat(parent_dirfd, leaf, O_DIRECTORY | O_NOFOLLOW | O_RDONLY |
O_CLOEXEC)` so a symlink at the leaf is refused with `ELOOP`, snapshots
the children through `fdopendir` + `readdir(3)`, re-anchors the
per-entry `fstatat`/`unlinkat` calls on a freshly opened dirfd, and
finishes with `unlinkat(.., AT_REMOVEDIR)` against the original
parent. A `(dev, ino)` cycle detector tripped at re-entry returns
`io::ErrorKind::FilesystemLoop`; a classify-vs-act TOCTOU
(`was-file-now-dir`) retries once with `UnlinkFlags::Dir` and
otherwise logs and steps over. `ENOENT` on the root or a child is
treated as idempotent success (matching upstream
`delete.c:48-176`), and `EACCES` on a single child is logged via
`tracing::debug!` and stepped over.

The fallback path delegates to `std::fs::remove_dir_all` for callers
that have not yet plumbed a `DirSandbox` (test fixtures, client-side
`--local` copies, `--inplace` without daemon gating). The fallback
intentionally absorbs `ErrorKind::NotFound` so both paths share an
idempotent-root contract.

Helper-only: no caller wiring, no trait changes. SEC-1.q will land
the `DeleteFs::remove_dir_all_at` extension and the
`DeleteEmitter::emit_all` / `receiver::directory::deletion` cutovers
as a follow-up.

Tests cover the eight cases in section 7 of the design doc that do
not require root: three-deep tree happy path, idempotent ENOENT
(sandbox + fallback), ELOOP on a symlink at the descent root,
symlinks inside the tree are unlinked without dereference, fallback
equivalence vs `std::fs::remove_dir_all`, multi-component relative
falls back, ENOTDIR on a non-directory leaf, and a wide directory
that wraps several internal `readdir` buffer fills. The hardlink-cycle
test from the design requires `CAP_SYS_ADMIN` and is deferred to a
gated integration test in the SEC-1.q follow-up.

* style(fast_io): apply rustfmt to SEC-1.s helper tests

* fix(fast_io): use ELOOP errno instead of unstable ErrorKind::FilesystemLoop

* test(fast_io): accept ENOTDIR or ELOOP for symlink-at-descent-root refusal
oferchen added a commit that referenced this pull request Aug 24, 2026
…-1.q2) (#4732)

* feat(transfer): thread DirSandbox through receiver --delete path (SEC-1.q2)

Replace the three unanchored syscalls in delete_extraneous_files with
their sandbox-anchored siblings so a TOCTOU symlink swap on a top-level
extraneous entry cannot redirect the listing or the unlink to an
attacker-chosen inode outside the destination tree.

Adds fast_io::read_dir_via_sandbox_or_fallback (mirrors the existing
*_via_sandbox_or_fallback shape: openat(O_DIRECTORY | O_NOFOLLOW) +
fdopendir for single-component scans, std::fs::read_dir fallback
otherwise). Closes audit rows #5, #6, #7 from the SEC-1 path-syscall
audit.

Mirrors the SEC-1.r temp_guard/temp_cleanup carrier-threading pattern
(PR #4723): the Option<Arc<DirSandbox>> is cloned once into the
map_blocking worker closure and forwarded to every helper call.
Sandbox-off and multi-component paths take the documented identical-
behaviour fallback. No transfer-crate unsafe is introduced; all unsafe
stays behind the existing fast_io safe wrappers.

Windows continues through std::fs::remove_file / remove_dir_all /
read_dir (NTFS handle semantics already close the symlink-swap window
per the SEC-1.l audit).

* fix(engine): use Components::next() walk for plan_directory_to_relative

SkipWhile<Components, _> has no as_path() method on stable Rust 1.88;
the previous skip_while-then-as_path call introduced in SEC-1.q (#4728)
slipped through that PR's CI but breaks fmt+clippy on every subsequent
master CI run. Switch to a manual Components::next() walk that consumes
the leading Prefix/RootDir/CurDir components, then calls as_path() on
the unwrapped Components iterator.

* fix(transfer): cfg-gate rel_for_unlink to unix to silence Windows unused-var
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant