Repository navigation
feat(fast_io): recursive_unlinkat sandbox helper for SEC-1.q (SEC-1.s) - #4726
Merged
Merged
Conversation
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
force-pushed
the
feat/sec-1-s-recursive-unlinkat-helper-impl
branch
from
May 22, 2026 10:55
e39ab2e to
14de048
Compare
4 tasks
This was referenced May 22, 2026
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
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
Ships
recursive_unlinkat_via_sandbox_or_fallbackincrates/fast_io/src/dir_sandbox/at_syscalls.rsper the design atdocs/design/sec-1-s-recursive-unlinkat-helper-2026-05-22.md. This is the dirfd-anchored recursive-remove helper SEC-1.q needs forDeleteFs::remove_dir_all_atand the receiver'sdelete_extraneous_filesrecursive branch (audit rows #6 and #27 indocs/audits/sec-1-path-syscall-audit-2026-05-22.md).Helper-only PR matching the
openat_via_sandbox_or_fallback/readlinkat_via_sandbox_or_fallbackprecedent (PR #4716): no trait changes, no caller cutovers. TheDeleteFs::remove_dir_all_atextension and theDeleteEmitter::emit_all/receiver::directory::deletionrewiring ship under SEC-1.q.Algorithm
The sandbox path mirrors upstream rsync's
delete_dir_contents+delete_item(target/interop/upstream-src/rsync-3.4.1/delete.c:48-176) while pinning every level of the descent on its own dirfd:openat(parent_dirfd, leaf, O_DIRECTORY | O_NOFOLLOW | O_RDONLY | O_CLOEXEC)- refuses a symlink at the descent root withELOOP.fstatat_nofollow(parent_dirfd, leaf)seeds the(dev, ino)cycle detector.fdopendir+readdir(3)snapshots the children into an ownedVecso per-entry actions cannot race the openDIR*cursor.fstatat(AT_SYMLINK_NOFOLLOW); directories recurse, everything else is unlinked throughunlinkat(.., 0).unlinkat(.., AT_REMOVEDIR).Cycle detection
(dev, ino)of every entered directory is threaded through the recursion via aHashSet. Re-entry tripsio::ErrorKind::FilesystemLoopbefore any destructive syscall fires inside the cycle. Hardlink-to-directory is non-portable and requiresCAP_SYS_ADMINon Linux, but the detector closes the failure mode at near-zero cost.TOCTOU classify-vs-act retry
When
unlinkat(.., 0)returnsEISDIR(Linux) orEPERM(other Unix), the entry was swapped to a directory between the classify and act calls. The helper retries once withUnlinkFlags::Dir; if the swap continued (ENOENT/ENOTEMPTY), the entry is logged viatracing::debug!and stepped over. The skipped entry producesENOTEMPTYat the final rmdir which the caller surfaces or retries.Error semantics
Matches upstream
delete.c:48-176:ENOENTon the descent root or a child: returned asOk(())(idempotent).EACCESon a single child: logged viatracing::debug!and stepped over; descent continues.ELOOP(sandbox path only): never followed.ENOTEMPTYafter the inner drain: propagated verbatim.FilesystemLoop: cycle detector tripped on(dev, ino)re-entry.Fallback
When
sandboxisNoneor the relative path is multi-component, the helper delegates tostd::fs::remove_dir_all(target_path)and absorbsErrorKind::NotFoundso both paths share an idempotent-root contract. The fallback is vulnerable to symlink-swap TOCTOU and is intended only for non-sandbox contexts (test fixtures, client-side--localcopies, callers that have not yet plumbed aDirSandbox).Test coverage
Nine
#[cfg(test)]cases inat_syscalls.rs, mirroring the SEC-1.f / SEC-1.g test layout:recursive_unlinkat_removes_three_deep_tree- happy path with siblings and a nested symlink at the deepest level.recursive_unlinkat_treats_missing_root_as_success- sandbox-path idempotent ENOENT.recursive_unlinkat_refuses_to_follow_symlink_at_descent_root- assertsELOOPand that the symlink target survives intact.recursive_unlinkat_unlinks_symlinks_inside_tree_without_following- symlinks inside the tree are unlinked, not dereferenced.recursive_unlinkat_fallback_matches_std_remove_dir_all- fallback equivalence vsstd::fs::remove_dir_allon a parallel tree.recursive_unlinkat_fallback_treats_missing_root_as_success- fallback-path idempotent ENOENT.recursive_unlinkat_falls_back_for_multi_component_relative- multi-component relative path takes the path-based fallback.recursive_unlinkat_propagates_enotdir_for_non_directory_leaf- non-directory leaf surfacesENOTDIRverbatim and leaves the entry intact.recursive_unlinkat_handles_wide_directory- 256 entries to wrap several internalreaddirbuffer-fill rounds.recursive_unlinkat_via_sandbox_or_fallback_with_no_sandbox_removes_tree- end-to-end no-sandbox fallback.The hardlink-cycle test from section 7 (case 5) requires
CAP_SYS_ADMINand is deferred to a gated integration test in the SEC-1.q follow-up.Test plan
cargo nextest run -p fast_ioexercises the ten new tests on Linux and macOS (Windows is excluded - the module is#[cfg(unix)]).DeleteFs::remove_dir_all_atand runs the interop delete harness against upstream 3.4.1 / 3.4.2.