Repository navigation
feat(fast_io): add secure_open_dir helper for SEC-1 dirfd sandbox - #4618
Merged
Merged
Conversation
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.
…(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
force-pushed
the
feat/fast-io-secure-open-dir
branch
from
May 20, 2026 23:20
38c8285 to
0ac3c55
Compare
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.
4 of 5 tasks
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.
This was referenced May 21, 2026
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
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*atcalls against the descriptor this helper returns.openat2(2)withRESOLVE_BENEATH | RESOLVE_NO_SYMLINKSso any symlink anywhere in the path and any..traversal that would escape the open point are refused (ELOOP/EXDEV). The firstENOSYSis cached in aOnceLock<bool>and subsequent calls skip the syscall.open(O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC), which still rejects a symlink at the leaf (ELOOPon Linux,ENOTDIRon macOS / BSD whereO_DIRECTORYis evaluated first).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 existingfast_iounsafe-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.Unsupportedstub branch; Linux must exercise bothopenat2and the symlink-leaf rejection paths.