Skip to content

refactor(engine): DeleteFs trait sandbox refactor + emitter wiring (SEC-1.q) - #4727

Closed
oferchen wants to merge 1 commit into
feat/sec-1-s-recursive-unlinkat-helper-implfrom
feat/sec-1-q-delete-fs-trait-sandbox-refactor
Closed

oferchen wants to merge 1 commit into
feat/sec-1-s-recursive-unlinkat-helper-implfrom
feat/sec-1-q-delete-fs-trait-sandbox-refactor

Conversation

@oferchen

Copy link
Copy Markdown
Owner

Summary

SEC-1.q closes the six GAP sites in crates/engine/src/delete/emitter/fs.rs
that PR #4710's path-syscall audit flagged as remaining sandbox holes in
the --delete syscall surface. Extends the DeleteFs trait with
unix-only *_at siblings that take a parent dirfd plus a
single-component leaf, wires a DirSandbox carrier slot through
DeleteEmitter, and routes the dirfd-anchored siblings through
fast_io::unlinkat (SEC-1.g) and fast_io::recursive_unlinkat
(SEC-1.s).

Design reference

Six GAP sites closed

# site new dispatch route
22 unlink_file unlink_file_at -> unlinkat(parent_fd, name, 0)
23 rmdir rmdir_at -> unlinkat(parent_fd, name, AT_REMOVEDIR)
24 unlink_symlink unlink_symlink_at -> unlinkat(parent_fd, name, 0)
25 unlink_device unlink_device_at -> unlinkat(parent_fd, name, 0)
26 unlink_special unlink_special_at -> unlinkat(parent_fd, name, 0)
27 remove_dir_all remove_dir_all_at -> fast_io::recursive_unlinkat(parent_fd, name)

Implementation shape

  • Trait extension: *_at siblings are additive next to the existing
    path-based methods, so every existing caller stays on the legacy
    fallback. Path-based methods are documented as the no-sandbox /
    Windows route; the *_at siblings carry the security-relevant
    TOCTOU-closing dispatch shape.
  • RealDeleteFs: each *_at body is a one-line delegation to the
    corresponding fast_io helper.
  • RecordingDeleteFs / ScriptedDeleteFs: *_at impls discard
    parent_fd and record the leaf name. Every existing emitter unit
    test passes unchanged because the recorded sequence asserts paths
    that the dispatcher reconstructs from plan.directory.join(name).
  • DeleteEmitter::with_sandbox (new): attaches an Arc<DirSandbox>.
    drain_plan opens a per-plan dirfd against sandbox.root_dirfd()
    just before dispatch; on open failure (already-removed parent during
    an in-flight recursive drain) the dispatcher transparently falls
    back to the path-based methods.
  • fast_io::recursive_unlinkat: SEC-1.s's internal helper promoted
    to pub so the engine can drive the recursive peel directly off a
    dirfd it already holds.

Deferred receiver-deletion integration

The receiver-deletion call sites in crates/transfer/ (PR #4710 rows
#5-#7) are not wired in this PR. The receiver's delete traversal
does not yet route through DeleteEmitter; it issues its own per-entry
unlink/rmdir loop in crates/transfer/src/receiver/directory/.
Threading the sandbox through that loop is a separate refactor and
ships as a follow-up. The design doc has been updated with this
deferred-follow-up note.

Until that follow-up lands, DeleteEmitter::with_sandbox is exercised
by unit tests and any caller that builds the emitter directly. The
path-based fallback remains the production path for the live receiver
loop, so this is a net-positive defense-in-depth change with zero
behavioural regression.

Base branch

This PR targets feat/sec-1-s-recursive-unlinkat-helper-impl (PR
#4726, OPEN), not master. SEC-1.q depends on the SEC-1.s
recursive_unlinkat helper landing first.

Test plan

  • CI: fmt + clippy
  • CI: nextest (stable) - new sandbox.rs module exercises every
    *_at variant against a live tempdir
  • CI: Windows - path-based fallback still compiles and dispatches
    via the legacy dispatch / dispatch_dir shape
  • CI: macOS / Linux musl - the unix *_at dispatch shape compiles
    against std::os::fd::BorrowedFd and libc O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC

Files changed

  • crates/engine/src/delete/emitter/fs.rs - trait extension, blanket
    impl, RealDeleteFs *_at wiring, RecordingDeleteFs *_at
    recording shim.
  • crates/engine/src/delete/emitter/mod.rs - DeleteEmitter sandbox
    carrier slot, with_sandbox builder, sandbox-aware dispatch /
    dispatch_dir / drain_plan, per-plan openat helper.
  • crates/engine/src/delete/emitter/tests/mod.rs - ScriptedDeleteFs
    *_at failure-script forwarding.
  • crates/engine/src/delete/emitter/tests/sandbox.rs - new SEC-1.q
    sandbox dispatch test module.
  • crates/fast_io/src/dir_sandbox/at_syscalls.rs - promote
    recursive_unlinkat(parent_dirfd, leaf) to pub.
  • crates/fast_io/src/dir_sandbox/mod.rs - re-export
    recursive_unlinkat.
  • crates/fast_io/src/lib.rs - re-export recursive_unlinkat at the
    crate root.
  • docs/design/sec-1-q-delete-emitter-sandbox-2026-05-22.md -
    document the deferred receiver-deletion follow-up.

…EC-1.q)

Extend DeleteFs with unix-only `*_at` siblings (unlink_file_at, rmdir_at,
unlink_symlink_at, unlink_device_at, unlink_special_at, remove_dir_all_at)
that take a parent dirfd + single-component leaf and route through
fast_io::unlinkat / fast_io::recursive_unlinkat (SEC-1.s). Path-based
methods stay as the no-sandbox / Windows fallback so existing callers
keep working unchanged.

Closes the six GAP sites in crates/engine/src/delete/emitter/fs.rs from
PR #4710 audit section 4 (#22-#27) by giving RealDeleteFs a dirfd-
anchored dispatch shape. Adds DeleteEmitter::with_sandbox carrier slot
that opens a per-plan dirfd via DirSandbox::root_dirfd just before
dispatch so a mid-syscall symlink swap on the parent walk cannot
redirect the call. Plan-directory open failure transparently falls back
to the path-based methods so the drain stays compatible.

RecordingDeleteFs and ScriptedDeleteFs implement `*_at` by discarding
parent_fd and recording the leaf name so every emitter unit test keeps
working without sandbox plumbing. New sandbox.rs test module exercises
each `*_at` variant against a live tempdir, the recursive peel's
symlink-swap resistance, and the sandbox-off / missing-plan-directory
fallback paths.

Promotes recursive_unlinkat(parent_dirfd, leaf) from a SEC-1.s internal
helper to a pub entry point so engine can use it directly without
synthesising absolute-path arguments. Receiver-deletion integration in
crates/transfer/ deferred to a follow-up per design doc; the receiver
loop does not yet route through DeleteEmitter.
@oferchen
oferchen deleted the branch feat/sec-1-s-recursive-unlinkat-helper-impl May 22, 2026 11:31
@oferchen oferchen closed this May 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant