Skip to content

feat(transfer): thread DirSandbox through temp_guard/temp_cleanup (SEC-1.r) - #4723

Merged
oferchen merged 2 commits into
masterfrom
feat/sec-1-r-temp-file-sandbox
May 22, 2026
Merged

oferchen merged 2 commits into
masterfrom
feat/sec-1-r-temp-file-sandbox

Conversation

@oferchen

Copy link
Copy Markdown
Owner

Summary

Closes 4 of the 5 SEC-1 GAP sites flagged in
docs/audits/sec-1-path-syscall-audit-2026-05-22.md rows #17-#21 by
threading the existing DirSandbox carrier into the receiver-side
temp-file lifecycle. Implements the plan in
docs/design/sec-1-r-temp-file-sandbox-2026-05-22.md. The 5th site
(row #20, fs::read_dir orphan scan) stays path-based as an audit-
acknowledged carrier-acceptable residual; see Deferred section below.

GAP closures

# Site After
17 temp_guard.rs:130 OpenOptions::create_new openat_via_sandbox_or_fallback(dirfd, leaf, O_WRONLY|O_CREAT|O_EXCL|O_NOFOLLOW, 0o600)
18 temp_guard.rs:142 create_dir_all recovery Unchanged (carrier-only mitigated per design; multi-component parent walk cannot anchor on a single dirfd)
19 temp_guard.rs:217 Drop remove_file unlink_via_sandbox_or_fallback(dirfd, leaf, File) via new SandboxAnchor field on TempFileGuard
20 temp_cleanup.rs:95 read_dir orphan scan Unchanged (no fdopendir equivalent in std; scan is read-only / informational)
21 temp_cleanup.rs:137 remove_file orphan cleanup unlink_via_sandbox_or_fallback(dirfd, leaf, File) via new cleanup_stale_temp_files_sandboxed

Receiver wiring (matches the established SEC-1.j pattern from #4697)

  • transfer_ops::ResponseContext already carried sandbox + dest_dir; this PR
    flips the field type from Option<&DirSandbox> to Option<&Arc<DirSandbox>>
    so the temp-file Drop anchor can clone a stable handle that outlives the
    borrow. The synchronous process_file_response path calls the new
    open_tmpfile_sandboxed; the existing renameat call site adapts via
    .map(Arc::as_ref).
  • DiskCommitConfig gains dest_dir: Option<PathBuf> and
    sandbox: Option<Arc<DirSandbox>> so the cross-thread disk-commit worker
    carries the carrier into process::open_output_file's temp-file branch.
  • The non-pipelined receiver/transfer/sync.rs loop reuses the existing
    setup.sandbox to call open_tmpfile_sandboxed directly.

Test plan

  • cargo fmt --all clean
  • New unit test sandbox_anchored_guard_resists_symlink_swap_on_parent -
    races a rename + symlink swap on the temp parent between create and Drop
    and verifies the unlinkat lands on the original dirfd (decoy survives,
    attacker-owned files untouched, real temp file removed from the renamed
    directory)
  • New unit tests for open_tmpfile_sandboxed happy path + --temp-dir
    fallback path + cleanup_stale_temp_files_sandboxed happy path + sandbox-
    absent fallback
  • CI fmt+clippy
  • CI nextest (stable, Windows, macOS, Linux musl)
  • CI interop

Cross-platform

  • Linux / macOS: sandbox fast path engages via existing
    openat_via_sandbox_or_fallback helper.
  • Windows: TempFileGuard::anchor and open_tmpfile_sandboxed are
    #[cfg(unix)]; the public open_tmpfile path on Windows is unchanged.
    Per SEC-1.l, Windows uses CreateFileW handle semantics (already in
    fast_io) instead of dirfd-based *at calls.
  • --temp-dir and multi-component paths: keep the path-based fallback
    because the single-component-leaf check fails. ENOENT recovery keeps
    create_dir_all as accepted residual (audit row Add streaming negotiation prologue detector #18).

Deferred (intentionally)

References

…C-1.r)

Routes receiver-side temp-file creation and the matching unlink-on-drop
through `openat(dirfd, leaf, ...)` / `unlinkat(dirfd, leaf, 0)` instead
of path-based syscalls so a TOCTOU symlink swap on the temp parent
cannot redirect the create or the cleanup.

- `temp_guard::open_tmpfile_sandboxed` dispatches the create through
  `fast_io::openat_via_sandbox_or_fallback` and seeds the returned
  `TempFileGuard` with a sandbox anchor for the matching Drop unlink.
- `temp_cleanup::cleanup_stale_temp_files_sandboxed` routes the per-
  entry unlink through `unlink_via_sandbox_or_fallback`; the read_dir
  scan stays path-based (audit row #20 carrier-acceptable residual).
- `DiskCommitConfig` gains `dest_dir` + `sandbox` so the disk-commit
  worker thread carries the carrier across the SPSC boundary; both
  pipeline + sync receiver paths plumb the existing
  `PipelineSetup::sandbox` into the new fields.
- `ResponseContext::sandbox` switches to `Option<&Arc<DirSandbox>>` so
  the temp-file Drop anchor can clone a stable handle that outlives the
  borrow; renameat call site updates to `.map(Arc::as_ref)`.

Includes a regression test that races a rename + symlink-swap on the
temp parent between create and Drop and verifies the unlink lands on
the original dirfd (decoy survives, real temp file is removed).

Falls back to the existing `OpenOptions::create_new` /
`std::fs::remove_file` path when the carrier is absent (non-Unix,
--temp-dir routed to a sibling tree, or multi-component temp paths);
ENOENT recovery keeps `create_dir_all` as accepted residual.
@github-actions github-actions Bot added the enhancement New feature or request label May 22, 2026
@oferchen
oferchen merged commit b55dd1b into master May 22, 2026
56 of 57 checks passed
@oferchen
oferchen deleted the feat/sec-1-r-temp-file-sandbox branch May 22, 2026 09:49
oferchen added a commit that referenced this pull request May 22, 2026
#4729)

Captures the receiver-side `--delete` wiring follow-up SEC-1.q (PR #4728)
intentionally deferred. The merged engine-side work added a sandbox-aware
DeleteFs trait surface plus the DeleteEmitter::with_sandbox carrier, but
the receiver loop in crates/transfer/src/receiver/directory/deletion.rs
still calls std::fs::remove_file / remove_dir_all / read_dir directly.

Lays out three options (migrate receiver to DeleteEmitter, swap to
fast_io *_via_sandbox_or_fallback helpers, defer behind Landlock) with
LoC scope, risk, test cost, and cross-platform plan. Recommends Option B
(direct helper swap) on effort-vs-risk grounds and because the SEC-1.r
temp_guard / temp_cleanup PR (#4723) is the established precedent for
exactly this carrier-threading shape.

Closes audit rows #5-#7 in docs/audits/sec-1-path-syscall-audit-2026-05-22.md
once the implementation PR lands. Docs-only.
oferchen added a commit that referenced this pull request May 22, 2026
…-1.q2) (#4732)

* feat(transfer): thread DirSandbox through receiver --delete path (SEC-1.q2)

Replace the three unanchored syscalls in delete_extraneous_files with
their sandbox-anchored siblings so a TOCTOU symlink swap on a top-level
extraneous entry cannot redirect the listing or the unlink to an
attacker-chosen inode outside the destination tree.

Adds fast_io::read_dir_via_sandbox_or_fallback (mirrors the existing
*_via_sandbox_or_fallback shape: openat(O_DIRECTORY | O_NOFOLLOW) +
fdopendir for single-component scans, std::fs::read_dir fallback
otherwise). Closes audit rows #5, #6, #7 from the SEC-1 path-syscall
audit.

Mirrors the SEC-1.r temp_guard/temp_cleanup carrier-threading pattern
(PR #4723): the Option<Arc<DirSandbox>> is cloned once into the
map_blocking worker closure and forwarded to every helper call.
Sandbox-off and multi-component paths take the documented identical-
behaviour fallback. No transfer-crate unsafe is introduced; all unsafe
stays behind the existing fast_io safe wrappers.

Windows continues through std::fs::remove_file / remove_dir_all /
read_dir (NTFS handle semantics already close the symlink-swap window
per the SEC-1.l audit).

* fix(engine): use Components::next() walk for plan_directory_to_relative

SkipWhile<Components, _> has no as_path() method on stable Rust 1.88;
the previous skip_while-then-as_path call introduced in SEC-1.q (#4728)
slipped through that PR's CI but breaks fmt+clippy on every subsequent
master CI run. Switch to a manual Components::next() walk that consumes
the leading Prefix/RootDir/CurDir components, then calls as_path() on
the unwrapped Components iterator.

* fix(transfer): cfg-gate rel_for_unlink to unix to silence Windows unused-var
oferchen added a commit that referenced this pull request Aug 24, 2026
…C-1.r) (#4723)

* feat(transfer): thread DirSandbox through temp_guard/temp_cleanup (SEC-1.r)

Routes receiver-side temp-file creation and the matching unlink-on-drop
through `openat(dirfd, leaf, ...)` / `unlinkat(dirfd, leaf, 0)` instead
of path-based syscalls so a TOCTOU symlink swap on the temp parent
cannot redirect the create or the cleanup.

- `temp_guard::open_tmpfile_sandboxed` dispatches the create through
  `fast_io::openat_via_sandbox_or_fallback` and seeds the returned
  `TempFileGuard` with a sandbox anchor for the matching Drop unlink.
- `temp_cleanup::cleanup_stale_temp_files_sandboxed` routes the per-
  entry unlink through `unlink_via_sandbox_or_fallback`; the read_dir
  scan stays path-based (audit row #20 carrier-acceptable residual).
- `DiskCommitConfig` gains `dest_dir` + `sandbox` so the disk-commit
  worker thread carries the carrier across the SPSC boundary; both
  pipeline + sync receiver paths plumb the existing
  `PipelineSetup::sandbox` into the new fields.
- `ResponseContext::sandbox` switches to `Option<&Arc<DirSandbox>>` so
  the temp-file Drop anchor can clone a stable handle that outlives the
  borrow; renameat call site updates to `.map(Arc::as_ref)`.

Includes a regression test that races a rename + symlink-swap on the
temp parent between create and Drop and verifies the unlink lands on
the original dirfd (decoy survives, real temp file is removed).

Falls back to the existing `OpenOptions::create_new` /
`std::fs::remove_file` path when the carrier is absent (non-Unix,
--temp-dir routed to a sibling tree, or multi-component temp paths);
ENOENT recovery keeps `create_dir_all` as accepted residual.

* fix(transfer): promote libc dep to cfg(unix) for macOS temp_guard openat flags
oferchen added a commit that referenced this pull request Aug 24, 2026
#4729)

Captures the receiver-side `--delete` wiring follow-up SEC-1.q (PR #4728)
intentionally deferred. The merged engine-side work added a sandbox-aware
DeleteFs trait surface plus the DeleteEmitter::with_sandbox carrier, but
the receiver loop in crates/transfer/src/receiver/directory/deletion.rs
still calls std::fs::remove_file / remove_dir_all / read_dir directly.

Lays out three options (migrate receiver to DeleteEmitter, swap to
fast_io *_via_sandbox_or_fallback helpers, defer behind Landlock) with
LoC scope, risk, test cost, and cross-platform plan. Recommends Option B
(direct helper swap) on effort-vs-risk grounds and because the SEC-1.r
temp_guard / temp_cleanup PR (#4723) is the established precedent for
exactly this carrier-threading shape.

Closes audit rows #5-#7 in docs/audits/sec-1-path-syscall-audit-2026-05-22.md
once the implementation PR lands. Docs-only.
oferchen added a commit that referenced this pull request Aug 24, 2026
…-1.q2) (#4732)

* feat(transfer): thread DirSandbox through receiver --delete path (SEC-1.q2)

Replace the three unanchored syscalls in delete_extraneous_files with
their sandbox-anchored siblings so a TOCTOU symlink swap on a top-level
extraneous entry cannot redirect the listing or the unlink to an
attacker-chosen inode outside the destination tree.

Adds fast_io::read_dir_via_sandbox_or_fallback (mirrors the existing
*_via_sandbox_or_fallback shape: openat(O_DIRECTORY | O_NOFOLLOW) +
fdopendir for single-component scans, std::fs::read_dir fallback
otherwise). Closes audit rows #5, #6, #7 from the SEC-1 path-syscall
audit.

Mirrors the SEC-1.r temp_guard/temp_cleanup carrier-threading pattern
(PR #4723): the Option<Arc<DirSandbox>> is cloned once into the
map_blocking worker closure and forwarded to every helper call.
Sandbox-off and multi-component paths take the documented identical-
behaviour fallback. No transfer-crate unsafe is introduced; all unsafe
stays behind the existing fast_io safe wrappers.

Windows continues through std::fs::remove_file / remove_dir_all /
read_dir (NTFS handle semantics already close the symlink-swap window
per the SEC-1.l audit).

* fix(engine): use Components::next() walk for plan_directory_to_relative

SkipWhile<Components, _> has no as_path() method on stable Rust 1.88;
the previous skip_while-then-as_path call introduced in SEC-1.q (#4728)
slipped through that PR's CI but breaks fmt+clippy on every subsequent
master CI run. Switch to a manual Components::next() walk that consumes
the leading Prefix/RootDir/CurDir components, then calls as_path() on
the unwrapped Components iterator.

* fix(transfer): cfg-gate rel_for_unlink to unix to silence Windows unused-var
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