Skip to content

feat(fast_io): add DirSandbox + thread through receiver pipeline (SEC-1.e) - #4650

Merged
oferchen merged 2 commits into
masterfrom
feat/fast-io-dir-sandbox-sec1e
May 21, 2026
Merged

oferchen merged 2 commits into
masterfrom
feat/fast-io-dir-sandbox-sec1e

Conversation

@oferchen

Copy link
Copy Markdown
Owner

Summary

Adds the parent-dirfd carrier that the SEC-1.f-j cutover sites consume
to replace path-based syscalls with their *at siblings. Implements
the hybrid in-tree dirfd stack plus DashMap<PathBuf, Arc<OwnedFd>>
side-cache shape picked in docs/design/sec-1-b-dirfd-carrier.md.

  • crates/fast_io/src/dir_sandbox/ - new DirSandbox type (Unix
    only). Holds an in-tree dirfd stack for the linear depth-first
    descent (enter / exit / current_dirfd) and a DashMap side
    cache for cross-directory operands (--backup-dir, --temp-dir,
    --link-dest, --copy-dest, --compare-dest) accessed via
    secondary(path).
  • Resolution policy: openat2(RESOLVE_BENEATH | RESOLVE_NO_SYMLINKS)
    on Linux 5.6+ (gated by fast_io::linux_capabilities::openat2_supported()),
    openat(O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC) via rustix
    everywhere else. RESOLVE_BENEATH is correct because each *at
    open is anchored on a real dirfd, not AT_FDCWD (unlike the
    bootstrap secure_open_dir, where it had to be dropped per feat(fast_io): add secure_open_dir helper for SEC-1 dirfd sandbox #4618).
  • Threaded through transfer::receiver::ReceiverContext:
    setup_transfer opens a sandbox at the destination root, stores it
    on PipelineSetup, and forwards a borrow into ResponseContext so
    the per-entry response path sees the carrier today.

What this does NOT do

No syscalls are migrated in this PR. The existing path-based code
paths remain the active code; the carrier is plumbed but currently
unused at every syscall site. The site-by-site migration is the work
of SEC-1.f-j (see breakdown in
docs/design/sec-1-b-dirfd-carrier.md Section 7).

Prerequisites

Unblocks

  • SEC-1.f - receiver in-tree leaf swaps under
    crates/transfer/src/receiver/directory/.
  • SEC-1.g - engine in-tree leaf swaps under
    crates/engine/src/local_copy/executor/ (10 sites in
    file/guard.rs alone).
  • SEC-1.h - metadata-apply swaps in
    crates/metadata/src/apply/.
  • SEC-1.i - cross-directory operations (renameat / linkat
    consuming the secondary cache).
  • SEC-1.j - recursive dirfd_remove_dir_all + remaining edges.

Cross-references the 107-site inventory in
docs/audits/sec-1-a-path-syscall-surface-2026-05-20.md.

Carrier shape

#[cfg(unix)]
pub struct DirSandbox {
    root: Arc<OwnedFd>,            // secure_open_dir at session start
    stack: Vec<DirFrame>,          // in-tree dirfd stack (DFS cursor)
    secondaries: DashMap<PathBuf, Arc<OwnedFd>>, // operand side cache
}

impl DirSandbox {
    pub fn open_root(root: &Path) -> io::Result<Self>;
    pub fn enter(&mut self, child_name: &OsStr) -> io::Result<()>;
    pub fn exit(&mut self);
    pub fn current_dirfd(&self) -> BorrowedFd<'_>;
    pub fn secondary(&self, path: &Path) -> io::Result<Arc<OwnedFd>>;
    // plus root_dirfd / root_arc / depth / secondary_count
}

The BorrowedFd<'_> accessor is Copy + Send + Sync, so rayon
worker closures capture the parent dirfd by value with zero
synchronisation cost - the pattern called out in
docs/design/sec-1-b-dirfd-carrier.md Section 5.

Constraints satisfied

  • Unix-only behind #[cfg(unix)]; Windows continues to use
    handle-based NTFS APIs (SEC-1.l audit).
  • No new dependencies - reuses the existing workspace dashmap and
    rustix; only fast_io's Cargo.toml gains a dashmap line.
  • No #[allow(...)] waivers; all #[allow(unsafe_code)] blocks are
    inside fast_io per workspace policy and carry a documented
    SAFETY argument.
  • Public API of ReceiverContext / ParallelDeltaApplier is
    unchanged. ResponseContext gains one Unix-only optional field
    (sandbox: Option<&fast_io::DirSandbox>) populated internally; no
    external caller touches the struct.

Test plan

  • cargo fmt --all -- --check
  • cargo clippy --workspace --all-targets --all-features --no-deps -- -D warnings
  • cargo nextest run -p fast_io --features io_uring -E 'test(dir_sandbox)' -
    11/11 unit tests pass.
  • cargo nextest run -p engine --features parallel-receive-delta -
    4379/4379 pass (no regression).
  • cargo nextest run -p transfer --features incremental-flist -
    1611/1613 pass; the 2 failures (files_from::relative_absolute_source_preserves_full_prefix
    and empty_file_checksum_verification_sha1) reproduce on the
    base branch without this PR's changes and are unrelated.
  • cargo nextest run -p transfer -E 'binary(dir_sandbox_carrier)' -
    4/4 integration tests pass.

oferchen added 2 commits May 21, 2026 08:49
Extract the cached `openat2(2)` availability probe from
`secure_dir::linux::try_openat2` into a new `linux_capabilities` module so
that the SEC-1.e..j cutover sites (`renameat2`, `linkat`, `unlinkat`, etc.)
share a single source of truth instead of each reinventing the probe.

The helper issues one no-op `openat2(AT_FDCWD, ".", { resolve: 0 })`
syscall on first call, caches the outcome in a `OnceLock<bool>`, and is
thread-safe. `ENOSYS` maps to "unsupported"; any other errno still proves
the syscall reached the kernel and is recorded as supported. On non-Linux
Unix targets the helper is a compile-time `false` so callers stay
platform-portable behind a single `#[cfg(unix)]` gate.

`secure_dir::try_openat2` now defers to `openat2_supported()` for the
gating decision; the inline `OnceLock` is gone.
…-1.e)

Introduce a parent-dirfd carrier so the SEC-1.f-j cutover sites can
replace path-based syscalls with their `*at` siblings without
re-walking paths through the kernel.

The carrier implements the hybrid shape picked in
`docs/design/sec-1-b-dirfd-carrier.md`:

  * An in-tree dirfd **stack** (`enter` / `exit`) tracks the receiver's
    depth-first descent. `current_dirfd()` returns the top of the
    stack - or the root when empty - as a `BorrowedFd<'_>` that rayon
    workers can capture by copy with zero synchronisation cost.
  * A `DashMap<PathBuf, Arc<OwnedFd>>` **side cache** (`secondary`)
    covers the cross-directory operands (`--backup-dir`, `--temp-dir`,
    `--link-dest`, `--copy-dest`, `--compare-dest`). Entries are
    registered once at receiver setup and shared by `Arc`.

Resolution policy mirrors `secure_open_dir`:

  * Linux 5.6+ via `openat2(RESOLVE_BENEATH | RESOLVE_NO_SYMLINKS)` -
    `RESOLVE_BENEATH` is correct here because every `*at` open is
    anchored on a real dirfd, not `AT_FDCWD`.
  * Older Linux and other Unix targets fall back to
    `openat(O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC)` via `rustix`.

The `transfer::receiver::ReceiverContext` pipeline opens a
`DirSandbox` at the destination root the moment `setup_transfer`
resolves the destination path, stores it as
`Option<Arc<DirSandbox>>` on `PipelineSetup`, and forwards a borrow
into `ResponseContext` so the per-entry response path sees the
sandbox today.

No syscalls are migrated in this PR - the existing path-based code
paths remain the active code. SEC-1.f-j land the site-by-site
cutover on top of this carrier.

Carrier is `#[cfg(unix)]`; Windows continues to use NTFS handle-based
APIs (see the SEC-1.l audit) and does not need this plumbing.
@github-actions github-actions Bot added the enhancement New feature or request label May 21, 2026
@oferchen
oferchen merged commit ee8764f into master May 21, 2026
55 of 56 checks passed
@oferchen
oferchen deleted the feat/fast-io-dir-sandbox-sec1e branch May 21, 2026 07:45
oferchen added a commit that referenced this pull request Aug 24, 2026
…-1.e) (#4650)

* feat(fast_io): expose openat2_supported() probe helper (SEC-1.d)

Extract the cached `openat2(2)` availability probe from
`secure_dir::linux::try_openat2` into a new `linux_capabilities` module so
that the SEC-1.e..j cutover sites (`renameat2`, `linkat`, `unlinkat`, etc.)
share a single source of truth instead of each reinventing the probe.

The helper issues one no-op `openat2(AT_FDCWD, ".", { resolve: 0 })`
syscall on first call, caches the outcome in a `OnceLock<bool>`, and is
thread-safe. `ENOSYS` maps to "unsupported"; any other errno still proves
the syscall reached the kernel and is recorded as supported. On non-Linux
Unix targets the helper is a compile-time `false` so callers stay
platform-portable behind a single `#[cfg(unix)]` gate.

`secure_dir::try_openat2` now defers to `openat2_supported()` for the
gating decision; the inline `OnceLock` is gone.

* feat(fast_io): add DirSandbox + thread through receiver pipeline (SEC-1.e)

Introduce a parent-dirfd carrier so the SEC-1.f-j cutover sites can
replace path-based syscalls with their `*at` siblings without
re-walking paths through the kernel.

The carrier implements the hybrid shape picked in
`docs/design/sec-1-b-dirfd-carrier.md`:

  * An in-tree dirfd **stack** (`enter` / `exit`) tracks the receiver's
    depth-first descent. `current_dirfd()` returns the top of the
    stack - or the root when empty - as a `BorrowedFd<'_>` that rayon
    workers can capture by copy with zero synchronisation cost.
  * A `DashMap<PathBuf, Arc<OwnedFd>>` **side cache** (`secondary`)
    covers the cross-directory operands (`--backup-dir`, `--temp-dir`,
    `--link-dest`, `--copy-dest`, `--compare-dest`). Entries are
    registered once at receiver setup and shared by `Arc`.

Resolution policy mirrors `secure_open_dir`:

  * Linux 5.6+ via `openat2(RESOLVE_BENEATH | RESOLVE_NO_SYMLINKS)` -
    `RESOLVE_BENEATH` is correct here because every `*at` open is
    anchored on a real dirfd, not `AT_FDCWD`.
  * Older Linux and other Unix targets fall back to
    `openat(O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC)` via `rustix`.

The `transfer::receiver::ReceiverContext` pipeline opens a
`DirSandbox` at the destination root the moment `setup_transfer`
resolves the destination path, stores it as
`Option<Arc<DirSandbox>>` on `PipelineSetup`, and forwards a borrow
into `ResponseContext` so the per-entry response path sees the
sandbox today.

No syscalls are migrated in this PR - the existing path-based code
paths remain the active code. SEC-1.f-j land the site-by-site
cutover on top of this carrier.

Carrier is `#[cfg(unix)]`; Windows continues to use NTFS handle-based
APIs (see the SEC-1.l audit) and does not need this plumbing.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant