Skip to content

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

Merged
oferchen merged 4 commits into
masterfrom
feat/sec-1-s-recursive-unlinkat-helper-impl
May 22, 2026
Merged

oferchen merged 4 commits into
masterfrom
feat/sec-1-s-recursive-unlinkat-helper-impl

Conversation

@oferchen

Copy link
Copy Markdown
Owner

Summary

Ships recursive_unlinkat_via_sandbox_or_fallback in crates/fast_io/src/dir_sandbox/at_syscalls.rs per the design at docs/design/sec-1-s-recursive-unlinkat-helper-2026-05-22.md. This is 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).

Helper-only PR matching the openat_via_sandbox_or_fallback / readlinkat_via_sandbox_or_fallback precedent (PR #4716): no trait changes, no caller cutovers. The DeleteFs::remove_dir_all_at extension and the DeleteEmitter::emit_all / receiver::directory::deletion rewiring 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:

  1. openat(parent_dirfd, leaf, O_DIRECTORY | O_NOFOLLOW | O_RDONLY | O_CLOEXEC) - refuses a symlink at the descent root with ELOOP.
  2. fstatat_nofollow(parent_dirfd, leaf) seeds the (dev, ino) cycle detector.
  3. fdopendir + readdir(3) snapshots the children into an owned Vec so per-entry actions cannot race the open DIR* cursor.
  4. Each child is classified through fstatat(AT_SYMLINK_NOFOLLOW); directories recurse, everything else is unlinked through unlinkat(.., 0).
  5. After the inner loop drains, the descent dirfd is dropped and the parent issues unlinkat(.., AT_REMOVEDIR).

Cycle detection

(dev, ino) of every entered directory is threaded through the recursion via a HashSet. Re-entry trips io::ErrorKind::FilesystemLoop before any destructive syscall fires inside the cycle. Hardlink-to-directory is non-portable and requires CAP_SYS_ADMIN on Linux, but the detector closes the failure mode at near-zero cost.

TOCTOU classify-vs-act retry

When unlinkat(.., 0) returns EISDIR (Linux) or EPERM (other Unix), the entry was swapped to a directory between the classify and act calls. The helper retries once with UnlinkFlags::Dir; if the swap continued (ENOENT/ENOTEMPTY), the entry is logged via tracing::debug! and stepped over. The skipped entry produces ENOTEMPTY at the final rmdir which the caller surfaces or retries.

Error semantics

Matches upstream delete.c:48-176:

  • ENOENT on the descent root or a child: returned as Ok(()) (idempotent).
  • EACCES on a single child: logged via tracing::debug! and stepped over; descent continues.
  • ELOOP (sandbox path only): never followed.
  • ENOTEMPTY after the inner drain: propagated verbatim.
  • FilesystemLoop: cycle detector tripped on (dev, ino) re-entry.

Fallback

When sandbox is None or the relative path is multi-component, the helper delegates to std::fs::remove_dir_all(target_path) and absorbs ErrorKind::NotFound so 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 --local copies, callers that have not yet plumbed a DirSandbox).

Test coverage

Nine #[cfg(test)] cases in at_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 - asserts ELOOP and 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 vs std::fs::remove_dir_all on 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 surfaces ENOTDIR verbatim and leaves the entry intact.
  • recursive_unlinkat_handles_wide_directory - 256 entries to wrap several internal readdir buffer-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_ADMIN and is deferred to a gated integration test in the SEC-1.q follow-up.

Test plan

  • CI: fmt + clippy clean on Linux, macOS, Windows, Linux musl.
  • CI: cargo nextest run -p fast_io exercises the ten new tests on Linux and macOS (Windows is excluded - the module is #[cfg(unix)]).
  • Manual: SEC-1.q follow-up wires the helper into DeleteFs::remove_dir_all_at and runs the interop delete harness against upstream 3.4.1 / 3.4.2.

@github-actions github-actions Bot added the enhancement New feature or request label May 22, 2026
oferchen added 2 commits May 22, 2026 13:55
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
oferchen force-pushed the feat/sec-1-s-recursive-unlinkat-helper-impl branch from e39ab2e to 14de048 Compare May 22, 2026 10:55
@oferchen
oferchen merged commit 2a5d119 into master May 22, 2026
56 checks passed
@oferchen
oferchen deleted the feat/sec-1-s-recursive-unlinkat-helper-impl branch May 22, 2026 11:31
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
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