fix(gate)!: list a directory as names and types so a listing opens nothing

Merged
KI
22d ago
+311-39

Problem

Gate::read_dir needs only LIST, but what it returned could do everything:

let grant = gate.grant("/data", Rights::LIST)?;
let entries: Vec<_> = gate.read_dir("/data")?.collect();
gate.revoke(grant)?;
let entry = entries[0].as_ref()?;
entry.open()?;        // reads /data/secret.txt
entry.remove_file()?; // and removes it

It returned cap-std's ReadDir. Each entry holds the listed directory's descriptor and has open, open_with (write, create, truncate), open_dir (a full Dir for a subdirectory), metadata, remove_file, and remove_dir, all relative to that descriptor, none through the gate. So LIST gave every right beneath the directory, nothing was audited, and the entries kept working after revoke and narrow.

A Broker's worker was not affected: the broker sends names. After lock(), the kernel refuses what the rules do not give, since Landlock checks the object an open reaches. Code that holds a Gate before lock() could get around its rights and revocations.

Found in an internal review at ea159a6. The maintainer chose to return names and types, as a public PR.

Root cause

Gate::open has refused directories since 0.3.0, because a directory descriptor is authority the gate cannot see used. read_dir handed one out inside the iterator, and ADR 0001 had re-exported ReadDir on the view that wrapping it had no security value.

Change

RFC 0013, accepted, and its implementation:

  • Gate::read_dir returns Result<Vec<DirEntry>, GateError>. It reads the whole directory before returning, so the descriptor is closed when the caller gets the listing. An error partway fails the call and is audited as allow with its errno; a partial listing is never returned.
  • fsgate::DirEntry is new: file_name() -> &OsStr and file_type() -> FileType, with no descriptor and no method that touches the filesystem. A symlink is listed as a symlink.
  • fsgate::FileType (cap-std's, a plain value) is re-exported; fsgate::ReadDir is removed.
  • The broker builds its name list from the new entries. The protocol does not change.
  • The model test and the gate_paths fuzz target compare a listing's names and types with the directory the path should reach, since entries no longer have inode numbers. The fuzz target asserts at startup that every directory in its fixture lists something different, so its check still names the directory. The model's generated trees reuse four names, so a listing there can match another directory's; its open and metadata checks, which route and resolve the same way, still compare inode numbers. RFC 0013 says this.
  • ADR 0001 gets an amendment, RFC 0001's API listing shows the new signature, and the threat model gets a row.

Safety and compatibility

Breaking: code that iterates read_dir's result as io::Result<DirEntry>, or opens, removes, or stats an entry, stops compiling, so this belongs in 0.5.0. The changelog entry says so and gives the replacement: gate.open(dir.join(entry.file_name())), which needs READ and is audited. A deprecated alias for ReadDir would keep the defect, so there is none.

A listing is now a snapshot held in memory; RFC 0013 records both. The broker already collected listings that way.

Verification

Before the change, on main (a scratch test, not committed): a LIST-only grant, read_dir, then revoke, and the listed entry still read the file and removed it. The scratch test reached its final panic! after both assertions held:

panicked at crates/fsgate/tests/readdir_bypass.rs:26:5:
on main, a LIST-only listing read and removed a file after revoke
test result: FAILED. 0 passed; 1 failed

That code cannot compile now, so the regression test is a compile_fail,E0599 doctest on DirEntry that calls open on an entry.

After it, on macOS with Rust 1.99.1, merged with main at fd998d5:

  • cargo test --locked: 135 passed, 0 failed, including the doctest (DirEntry (line 77) - compile fail ... ok) and the new gate.rs::read_dir_returns_each_entry_name_and_type.
  • cargo fmt --all --check passed. cargo clippy --locked --all-targets -- -D warnings is clean on macOS and for x86_64-unknown-linux-gnu. cargo +1.85.0 check --locked --all-targets passed. RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --locked passed. The fuzz crate passes cargo fmt --check and cargo check --locked.
  • cargo +nightly fuzz run gate_paths (cargo 1.102.0-nightly, cargo-fuzz 0.13.2) over its seeds, then 91 seconds into a scratch corpus, found nothing, with the new fixture assertion holding: 76,347 runs before merging main, and 15,788 after, with RFC 0012's nested-grant rights in the fixture.

Not verified locally: the Linux-only tests, because this machine has no Linux. This PR's CI runs them.

Documentation and release

RFC 0013 (new). RFC 0001, ADR 0001, the threat model, the README status, fuzz/README.md, and the changelog. No version change. The M9 roadmap entry records the CI and fuzz runs on 15c5ee9 as evidence: all 25 checks passed, including the doctest on macOS and the runners' Linux kernel, the listing test there and on Linux 6.6, 6.12 and 7.3, and 901 seconds of gate_paths on Linux and macOS with no finding.