Repository navigation
Conversation
…eck after open A FIFO renamed over a resolved path between the stat and the open blocked open(2) on a blocking-pool thread that no timeout frees; a symlink swapped in after canonicalize escaped the base. Every fs-origin read now opens through open_resolved: O_NONBLOCK returns at once on a FIFO, O_NOFOLLOW refuses a swapped-in leaf symlink with ELOOP, and an fstat of the opened file refuses anything not a regular file. Closes #2802
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
Gas snapshotNo gas changes vs base. |
Code coverage overviewLanguages: Rust, Solidity Rust / code-coverage/cargo-llvm-cov
View coverage of the 3 most impacted files.
Solidity / code-coverage/forge
ℹ️ Calculations represent line coverage. Results may change as calculations are updated. Updated |
| #[tokio::test] | ||
| async fn a_fifo_swapped_in_after_resolve_is_refused_without_blocking() -> anyhow::Result<()> { | ||
| let tmp = tempfile::tempdir()?; | ||
| let origin = FilesystemOrigin::new(tmp.path()).await?; | ||
| let paths = seed_and_resolve(&origin).await?; | ||
| for (i, path) in paths.iter().enumerate() { | ||
| let fifo = origin.base().join(format!("fifo-{i}")); | ||
| make_fifo(&fifo)?; | ||
| tokio::fs::rename(&fifo, path).await?; | ||
| } | ||
| ensure_every_read_refuses(&paths, "not a regular file").await | ||
| } |
There was a problem hiding this comment.
Fixed in 81b2e48. The test now builds its own current-thread runtime and calls shutdown_timeout(1s) after block_on, so a stuck blocking-pool open no longer holds teardown. With the O_NONBLOCK | O_NOFOLLOW flags removed, the test now fails in ~6 s with read_range blocked on the swapped path instead of hanging.
alpergundogdu
left a comment
There was a problem hiding this comment.
The leaf handling is right: the fstat runs on the fd, a swapped-in FIFO or leaf symlink is refused, and the swap tests really swap after resolve. One change before merge:
Close the shard-directory gap in this PR. O_NOFOLLOW guards only the last path component. Someone with write access to the origin dir can replace {base}/ab/ with a symlink to another directory after the resolve, and open_resolved (crates/cache/src/origin/fs.rs ~L460) follows it. BLAKE3 verification limits the damage today, but the point of this PR is that the open never leaves the base, so it should hold for every component. Please walk the path with directory fds instead of opening the full path:
- open the base once as a directory (
O_DIRECTORY), openat(base, shard, O_NOFOLLOW | O_DIRECTORY),openat(shard, leaf, O_NOFOLLOW | O_NONBLOCK), then the existing fstat checks.
rustix::fs::openat gives this with a safe API on both macOS and Linux (or cap-std's Dir), in place of libc. Run it inside spawn_blocking as the current reads do.
Add a #[cfg(unix)] test that swaps the shard directory for a symlink after the resolve and asserts the read is refused as a swapped symlink.
Hardlinks from inside the base to a file outside it still pass. That predates this PR and BLAKE3 makes it harmless, so no change is needed there.
O_NOFOLLOW guarded only the leaf, so a shard directory swapped for a symlink after the resolve still redirected the open out of the base. Each read now opens its canonical path relative to a cap-std handle on the base, which refuses an escape at every component (openat2 RESOLVE_BENEATH on Linux, a checked walk elsewhere, Windows included). O_NONBLOCK and the post-open fstat stay.
|
@alpergundogdu Fixed in 159924b. Every fs-origin open now resolves the canonical path relative to a One deviation: the base handle is opened per read, not held for the origin's life, so a base on its own mount does not keep that mount busy. New test: |

Problem
FilesystemOrigin::resolve_regularcanonicalizes, checks containment and stats the path, but each read then opened the path with a plainFile::open. In that stat→open window:open(2)until a writer appears. The open runs on a tokiospawn_blockingthread; the retryDeadlineand the node's populate timeout drop the future but cannot free the thread, so enough won races exhaust the blocking pool.canonicalize— at the leaf or at the shard directory — is followed, so the read escapes the base.fetch_range_dataandfetch_size_proofhad no post-open check at all;fetch_outboardre-checked the file type but blocked on a FIFO before reaching it.Fix
All three reads open through one helper,
open_resolved:cap-stdDirhandle on the base, never as an absolute path. Every component resolves beneath the handle (openat2(RESOLVE_BENEATH)on Linux, cap-primitives' checked walk elsewhere, Windows included), so a leaf or a shard directory swapped for a symlink out of the base fails the open with a permanent "leads outside base" refusal. A symlink that stays inside the base is still followed, as the resolve follows it. This is the same crate and patterndecdn-client's bundle landing uses (bundle: harden the pull's destination paths, Ctrl-C window and region filter #2439).O_NONBLOCK: a read-only open of a FIFO returns at once (no effect on regular-file reads on a local filesystem).resolve_regularchecks stay as a cheap fail-fast.The base handle is opened per read on the blocking pool, not held for the origin's life, so a base on its own mount does not keep that mount busy.
libcis a unix-only dependency ofdecdn-cachefor theO_NONBLOCKconstant only — nounsafe.cap-stdandlibcwere already in the lock. Each reader's post-resolve half is a free function (read_range,read_outboard,read_size_proof) so the tests can swap the path between resolve and open.Hardlinks from inside the base to a file outside it still pass; BLAKE3 verification makes that harmless and it predates this PR.
Tests
a_fifo_swapped_in_after_resolve_is_refused_without_blocking: renames a FIFO over each resolved path and drives all three reads under a 5 s timeout, on an owned runtime with bounded shutdown. WithoutO_NONBLOCKit fails in ~6 s.a_symlink_swapped_in_after_resolve_is_refused: swaps each resolved leaf for a symlink out of the base; every read refuses. Without the base handle it reads the outside file.a_shard_directory_swapped_in_after_resolve_is_refused: swaps the shard directory for a symlink to an outside copy of it; every read refuses. Without the base handle it reads the copy.a_file_removed_after_resolve_reads_as_absent: a file removed between resolve and open is a miss, not a fault.every_read_rejects_a_special_file_inside_baseand the inside-base symlink tests) stay green.Verified on macOS:
cargo nextest run -p decdn-cache(497 passed),cargo clippy --workspace --all-targets -- -D warnings,cargo fmt -- --check,cargo docwith warnings denied, crate-edges check,cargo deny check bans licenses sources.cargo denyadvisories fails on main already (chrono, crypto-bigint, pin-utils), unrelated to this change.Closes #2802