Skip to content

fix(cache): open fs-origin reads with O_NONBLOCK|O_NOFOLLOW and re-check after open - #3012

Open
yigitdot wants to merge 5 commits into
mainfrom
fix/2802-fs-origin-swap-window
Open

yigitdot wants to merge 5 commits into
mainfrom
fix/2802-fs-origin-swap-window

Conversation

@yigitdot

@yigitdot yigitdot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Problem

FilesystemOrigin::resolve_regular canonicalizes, checks containment and stats the path, but each read then opened the path with a plain File::open. In that stat→open window:

  • A FIFO renamed into place blocks open(2) until a writer appears. The open runs on a tokio spawn_blocking thread; the retry Deadline and the node's populate timeout drop the future but cannot free the thread, so enough won races exhaust the blocking pool.
  • A symlink swapped in after canonicalize — at the leaf or at the shard directory — is followed, so the read escapes the base.

fetch_range_data and fetch_size_proof had no post-open check at all; fetch_outboard re-checked the file type but blocked on a FIFO before reaching it.

Fix

All three reads open through one helper, open_resolved:

  • The canonical path is opened relative to a cap-std Dir handle 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 pattern decdn-client's bundle landing uses (bundle: harden the pull's destination paths, Ctrl-C window and region filter #2439).
  • On unix the open carries O_NONBLOCK: a read-only open of a FIFO returns at once (no effect on regular-file reads on a local filesystem).
  • An fstat of the opened file refuses anything that is not a regular file. The outboard and size-proof caps are re-checked against that fstat length.
  • The pre-open resolve_regular checks 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. libc is a unix-only dependency of decdn-cache for the O_NONBLOCK constant only — no unsafe. cap-std and libc were 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. Without O_NONBLOCK it 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.
  • Existing tests (including every_read_rejects_a_special_file_inside_base and 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 doc with warnings denied, crate-edges check, cargo deny check bans licenses sources. cargo deny advisories fails on main already (chrono, crypto-bigint, pin-utils), unrelated to this change.

Closes #2802

…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
Copilot AI balanced review requested due to automatic review settings November 26, 2026 17:38

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@github-actions

Copy link
Copy Markdown

Gas snapshot

No gas changes vs base.

@github-code-quality

github-code-quality Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Code coverage overview

Languages: Rust, Solidity

Rust / code-coverage/cargo-llvm-cov

main d769020 fix/2802-fs-origin-s... 159924b +/-
78% 78% 0%
View coverage of the 3 most impacted files.
File main d769020 fix/2802-fs-origin-s... 159924b +/-
crates/client/src/lib.rs 89% 89% 0%
crates/protocol/src/framing.rs 92% 93% +1%
crates/cache/src/origin/fs.rs 76% 84% +8%

Solidity / code-coverage/forge

main d769020 fix/2802-fs-origin-s... 159924b +/-
93% 93% 0%

ℹ️ Calculations represent line coverage. Results may change as calculations are updated.


Updated November 27, 2026 23:40 UTC

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The FIFO regression test can hang during runtime teardown despite its timeout because the blocking open continues, requiring an explicitly owned runtime with bounded shutdown.

1 open issue

Review effort: Balanced

Comment thread crates/cache/src/origin/fs/tests.rs Outdated
Comment on lines +788 to +799
#[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
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 alpergundogdu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@yigitdot

Copy link
Copy Markdown
Contributor Author

@alpergundogdu Fixed in 159924b. Every fs-origin open now resolves the canonical path relative to a cap-std Dir handle on the base. That is the same crate and pattern decdn-client's bundle landing uses (#2439), rather than a hand-rolled openat walk. Every component resolves beneath the handle: openat2(RESOLVE_BENEATH) on Linux, with EXDEV mapped to the escape refusal, and cap-primitives' checked walk elsewhere, Windows included. A leaf or a shard directory swapped for a symlink out of the base is refused as "leads outside base". A symlink that stays inside the base is still followed, as the resolve follows it. O_NONBLOCK and the post-open fstat stay, and the open runs on the blocking pool.

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: a_shard_directory_swapped_in_after_resolve_is_refused replaces the shard directory with a symlink to an outside copy of it after the resolve, and every read refuses. With the base handle replaced by a plain path open, the test fails by reading the copy. Hardlinks are left as you noted.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fs origin: a FIFO swapped in between resolve and open still blocks a blocking-pool thread

3 participants