Skip to content

feat(fast_io): add secure_open_dir helper for SEC-1 dirfd sandbox - #4618

Merged
oferchen merged 5 commits into
masterfrom
feat/fast-io-secure-open-dir
May 21, 2026
Merged

oferchen merged 5 commits into
masterfrom
feat/fast-io-secure-open-dir

Conversation

@oferchen

Copy link
Copy Markdown
Owner

Summary

  • Adds fast_io::secure_open_dir(path: &Path) -> io::Result<OwnedFd> as the entry point for the SEC-1 dirfd sandbox (SEC-1.c, test: add comprehensive server invocation flag forwarding tests #2521). SEC-1.e onward will anchor its *at calls against the descriptor this helper returns.
  • Linux 5.6+ uses openat2(2) with RESOLVE_BENEATH | RESOLVE_NO_SYMLINKS so any symlink anywhere in the path and any .. traversal that would escape the open point are refused (ELOOP / EXDEV). The first ENOSYS is cached in a OnceLock<bool> and subsequent calls skip the syscall.
  • Older Linux, macOS, and other Unix targets fall through to open(O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC), which still rejects a symlink at the leaf (ELOOP on Linux, ENOTDIR on macOS / BSD where O_DIRECTORY is evaluated first).
  • Windows returns io::ErrorKind::Unsupported. NTFS handle-based APIs sidestep path TOCTOU without needing a sandbox dirfd (SEC-1.l audit).

The libc calls live behind a single #[allow(unsafe_code)] per call site with the SAFETY argument documented inline, matching the existing fast_io unsafe-code policy.

Design reference: docs/audits/sec-1-a-path-syscall-surface-2026-05-20.md.

Test plan

  • cargo nextest run -p fast_io --features io_uring -E 'test(secure_dir)' (2 passed locally on macOS).
  • cargo fmt --all -- --check.
  • cargo clippy --workspace --all-targets --all-features --no-deps -- -D warnings.
  • CI: full nextest matrix (Linux musl, macOS, Windows). Windows must hit the Unsupported stub branch; Linux must exercise both openat2 and the symlink-leaf rejection paths.

@github-actions github-actions Bot added the enhancement New feature or request label May 20, 2026
oferchen added a commit that referenced this pull request May 20, 2026
…sion

libc 0.2.155+ marks `open_how` as `#[non_exhaustive]`, so direct struct
expressions fail outside the libc crate with E0639. Zero-initialise via
`mem::zeroed()` (safe for repr(C) integer-only structs; documented in
openat2(2) as "no constraint" for unset fields) and then assign the
fields we care about.

Fixes MSRV 1.88, Build oc-rsync, and Sequential vs Parallel build
failures on PR #4618.
oferchen added 3 commits May 21, 2026 02:20
…(SEC-1.c)

Adds `fast_io::secure_open_dir` as the entry point used by the SEC-1.e onward
cutover from path-based syscalls to dirfd-anchored `*at` siblings. The helper
returns an `OwnedFd` over a directory opened with strict-resolution semantics:

* Linux 5.6+ uses `openat2(2)` with `RESOLVE_BENEATH | RESOLVE_NO_SYMLINKS`,
  rejecting any symlink anywhere in the path and any `..` traversal that would
  escape the open point. The first `ENOSYS` is cached in a `OnceLock<bool>` so
  later calls skip the syscall.
* Older Linux, macOS, and other Unix targets fall through to
  `open(O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC)`, which still rejects
  a symlink at the leaf with `ELOOP` (or `ENOTDIR` on macOS / BSD where
  `O_DIRECTORY` is evaluated first).
* Windows returns `io::ErrorKind::Unsupported`; NTFS handle-based APIs
  sidestep path TOCTOU without needing a sandbox dirfd (SEC-1.l audit).

Unit tests cover: successful directory open, symlink-leaf rejection, Linux
`openat2` `..` rejection when the kernel supports it, and the Windows
unsupported stub.

Refs docs/audits/sec-1-a-path-syscall-surface-2026-05-20.md.
…sion

libc 0.2.155+ marks `open_how` as `#[non_exhaustive]`, so direct struct
expressions fail outside the libc crate with E0639. Zero-initialise via
`mem::zeroed()` (safe for repr(C) integer-only structs; documented in
openat2(2) as "no constraint" for unset fields) and then assign the
fields we care about.

Fixes MSRV 1.88, Build oc-rsync, and Sequential vs Parallel build
failures on PR #4618.
`std::os::fd::OwnedFd` is Unix-only, so the unconditional import broke
Windows builds (E0432). The Windows stub never had a useful purpose -
SEC-1.l audit confirmed Windows uses NTFS handle-based APIs that
sidestep path TOCTOU naturally, and callers were always going to need
`#[cfg(unix)]` guards anyway.

Gate the module + re-export with `#[cfg(unix)]` and drop the Windows
stub branch + test. The doc comment now points Windows callers at the
handle-based path documented in the SEC-1.l audit.

Fixes the Windows IOCP build failure on PR #4618.
@oferchen
oferchen force-pushed the feat/fast-io-secure-open-dir branch from 38c8285 to 0ac3c55 Compare May 20, 2026 23:20
The Linux io_uring CI runner has a 5.6+ kernel, so the openat2 path
exercises edge cases the macOS / older-Linux runners didn't catch:

1. `opens_real_directory`: CI `/tmp` can be a symlink (or staged through
   one). `RESOLVE_NO_SYMLINKS` would refuse such a path, so the test
   should canonicalise via `std::fs::canonicalize` first - it is
   exercising the success path, not the deliberate-rejection path.

2. `rejects_symlink_leaf`: `openat2(RESOLVE_BENEATH | RESOLVE_NO_SYMLINKS)`
   reports the absolute-target symlink as `EXDEV` (cross-resolution-scope
   violation) rather than `ELOOP`. Either errno proves the symlink was
   refused, so accept the third option alongside ELOOP / ENOTDIR.

Fixes the `Feature flag combinations / io_uring (linux)` failure on PR
#4618.
oferchen added a commit that referenced this pull request May 21, 2026
…items (#4639)

- Remove restatement comments in status::platform_io_capabilities and
  syscall_batch tests that echo the code below them.
- Tighten two comments in syscall_batch tests to describe routing intent
  and ordering invariant instead of restating the assertion.
- Preserve every SAFETY block, upstream rsync citation, and meaningful
  WHY note across the crate.

Excluded from this pass: `secure_dir.rs` and `lib.rs` (active in-flight
work in #4618). The audit walked every other `.rs` file under
`crates/fast_io/`; the bulk of the crate was already clean - SAFETY
docs, kernel version gates, and platform quirks all preserved verbatim.
`RESOLVE_BENEATH` constrains path resolution to live beneath the dirfd
anchor. With `AT_FDCWD` and an absolute path - which is how every
realistic caller (daemon root, tempdir, user-supplied dest) invokes
this helper - the kernel evaluates the absolute path's location
against the process cwd. CI runners and most real workloads run with a
cwd that is NOT an ancestor of the target directory, so the kernel
returns `EXDEV` and every legitimate open fails.

This helper is the **bootstrap** dirfd open: it produces the parent
fd that subsequent `*at` syscalls (SEC-1.f..j) will anchor against.
`RESOLVE_BENEATH` belongs at *those* call sites, where the dirfd's
directory is the resolution scope - never at the bootstrap open with
`AT_FDCWD`. Keep `RESOLVE_NO_SYMLINKS` so the bootstrap still refuses
symlinks anywhere in the path.

The dotdot-traversal regression test is removed from this module with
a TODO referring to SEC-1.e, where the dirfd-anchored test belongs.
The symlink-leaf test no longer accepts EXDEV (only ELOOP / ENOTDIR
remain as valid rejection codes).

Fixes the Linux nextest/musl/io_uring/copy_file_range failures on PR
#4618.
@oferchen
oferchen merged commit 2781995 into master May 21, 2026
55 checks passed
@oferchen
oferchen deleted the feat/fast-io-secure-open-dir branch May 21, 2026 04:22
oferchen added a commit that referenced this pull request Aug 24, 2026
…items (#4639)

- Remove restatement comments in status::platform_io_capabilities and
  syscall_batch tests that echo the code below them.
- Tighten two comments in syscall_batch tests to describe routing intent
  and ordering invariant instead of restating the assertion.
- Preserve every SAFETY block, upstream rsync citation, and meaningful
  WHY note across the crate.

Excluded from this pass: `secure_dir.rs` and `lib.rs` (active in-flight
work in #4618). The audit walked every other `.rs` file under
`crates/fast_io/`; the bulk of the crate was already clean - SAFETY
docs, kernel version gates, and platform quirks all preserved verbatim.
oferchen added a commit that referenced this pull request Aug 24, 2026
)

* feat(fast_io): add secure_open_dir helper for SEC-1 dirfd sandboxing (SEC-1.c)

Adds `fast_io::secure_open_dir` as the entry point used by the SEC-1.e onward
cutover from path-based syscalls to dirfd-anchored `*at` siblings. The helper
returns an `OwnedFd` over a directory opened with strict-resolution semantics:

* Linux 5.6+ uses `openat2(2)` with `RESOLVE_BENEATH | RESOLVE_NO_SYMLINKS`,
  rejecting any symlink anywhere in the path and any `..` traversal that would
  escape the open point. The first `ENOSYS` is cached in a `OnceLock<bool>` so
  later calls skip the syscall.
* Older Linux, macOS, and other Unix targets fall through to
  `open(O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC)`, which still rejects
  a symlink at the leaf with `ELOOP` (or `ENOTDIR` on macOS / BSD where
  `O_DIRECTORY` is evaluated first).
* Windows returns `io::ErrorKind::Unsupported`; NTFS handle-based APIs
  sidestep path TOCTOU without needing a sandbox dirfd (SEC-1.l audit).

Unit tests cover: successful directory open, symlink-leaf rejection, Linux
`openat2` `..` rejection when the kernel supports it, and the Windows
unsupported stub.

Refs docs/audits/sec-1-a-path-syscall-surface-2026-05-20.md.

* fix(fast_io): construct libc::open_how via zeroed() not struct expression

libc 0.2.155+ marks `open_how` as `#[non_exhaustive]`, so direct struct
expressions fail outside the libc crate with E0639. Zero-initialise via
`mem::zeroed()` (safe for repr(C) integer-only structs; documented in
openat2(2) as "no constraint" for unset fields) and then assign the
fields we care about.

Fixes MSRV 1.88, Build oc-rsync, and Sequential vs Parallel build
failures on PR #4618.

* fix(fast_io): gate secure_dir Unix-only and remove Windows stub

`std::os::fd::OwnedFd` is Unix-only, so the unconditional import broke
Windows builds (E0432). The Windows stub never had a useful purpose -
SEC-1.l audit confirmed Windows uses NTFS handle-based APIs that
sidestep path TOCTOU naturally, and callers were always going to need
`#[cfg(unix)]` guards anyway.

Gate the module + re-export with `#[cfg(unix)]` and drop the Windows
stub branch + test. The doc comment now points Windows callers at the
handle-based path documented in the SEC-1.l audit.

Fixes the Windows IOCP build failure on PR #4618.

* fix(fast_io): accept EXDEV + canonicalise tempdir in secure_dir tests

The Linux io_uring CI runner has a 5.6+ kernel, so the openat2 path
exercises edge cases the macOS / older-Linux runners didn't catch:

1. `opens_real_directory`: CI `/tmp` can be a symlink (or staged through
   one). `RESOLVE_NO_SYMLINKS` would refuse such a path, so the test
   should canonicalise via `std::fs::canonicalize` first - it is
   exercising the success path, not the deliberate-rejection path.

2. `rejects_symlink_leaf`: `openat2(RESOLVE_BENEATH | RESOLVE_NO_SYMLINKS)`
   reports the absolute-target symlink as `EXDEV` (cross-resolution-scope
   violation) rather than `ELOOP`. Either errno proves the symlink was
   refused, so accept the third option alongside ELOOP / ENOTDIR.

Fixes the `Feature flag combinations / io_uring (linux)` failure on PR
#4618.

* fix(fast_io): drop RESOLVE_BENEATH from bootstrap secure_open_dir

`RESOLVE_BENEATH` constrains path resolution to live beneath the dirfd
anchor. With `AT_FDCWD` and an absolute path - which is how every
realistic caller (daemon root, tempdir, user-supplied dest) invokes
this helper - the kernel evaluates the absolute path's location
against the process cwd. CI runners and most real workloads run with a
cwd that is NOT an ancestor of the target directory, so the kernel
returns `EXDEV` and every legitimate open fails.

This helper is the **bootstrap** dirfd open: it produces the parent
fd that subsequent `*at` syscalls (SEC-1.f..j) will anchor against.
`RESOLVE_BENEATH` belongs at *those* call sites, where the dirfd's
directory is the resolution scope - never at the bootstrap open with
`AT_FDCWD`. Keep `RESOLVE_NO_SYMLINKS` so the bootstrap still refuses
symlinks anywhere in the path.

The dotdot-traversal regression test is removed from this module with
a TODO referring to SEC-1.e, where the dirfd-anchored test belongs.
The symlink-leaf test no longer accepts EXDEV (only ELOOP / ENOTDIR
remain as valid rejection codes).

Fixes the Linux nextest/musl/io_uring/copy_file_range failures on PR
#4618.
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