fix(gate)!: refuse a nested grant with fewer rights than the grant around it

Merged
KI
22d ago
+856-124

Problem

A gate can be told "the project read-write, its .git read-only", and it accepted that, but could not hold it:

gate.grant("/proj", Rights::READ | Rights::WRITE | Rights::CREATE)?;
gate.grant("/proj/.git", Rights::READ)?;
gate.create("/proj/.git/hooks/pre-commit")?; // RightsExceeded
gate.create("/proj/link/hooks/pre-commit")?; // succeeds: `link` -> `.git`

The second request routes to /proj by name, and the symlink stays beneath /proj's handle, so the write lands in .git. On a case-insensitive filesystem, as macOS uses by default, /proj/.GIT/... does the same with no symlink. It works through a Broker too, so a worker that can make a symlink in /proj can plant a git hook. After lock() the kernel allows the same, because Landlock allows what any rule allows; the threat model listed that part, not the gate's.

Found in an internal review at ea159a6. The maintainer chose to refuse such policies, as a public PR.

Root cause

Routing picks the longest grant whose path contains the request, and RFC 0001 calls that routing, not security: "the worst outcome is an operation under a directory that was still deliberately granted". That holds only if a nested grant never has fewer rights than the grant around it. Nothing checked that.

Change

RFC 0012, accepted, and its implementation:

  • Gate::grant and Gate::narrow refuse a grant on the same directory as another live grant, or beneath or above one, when the inner of the two would hold fewer rights than the outer. The error is the new GateError::NestedGrant { inner_path, inner, outer_path, outer }, and the audit event is one deny with the new reason nested_grant:
    grant for `/proj/.git` would hold READ within the grant for `/proj`, which holds READ | WRITE | CREATE; a grant within another must hold every right the other holds, so give the inner grant more rights or the outer grant fewer
    
  • Nesting is read from the directories, not their names. Each grant records the device and inode numbers of its directory and every directory above it, by fstatat on .., ../.., and so on from the held descriptor. So a grant named through a symlink, or in another case, is caught. Narrowing an outer grant, an identical second grant, and revoking are unchanged.
  • fsgate-broker refuses such --grant flags at startup with the same message, exit 1.
  • Tests that used a same-directory grant with fewer rights as a shortcut to get a second grant ID now grant a separate directory. lock.rs's a_nested_grant_with_fewer_rights_binds_only_the_gate is removed: its policy is now refused.
  • The model test predicts every refusal, with both grants' paths and rights. In the kernel test, the nested-grant gap can now only come from a symlink that stays in one grant and enters a nested grant with more rights, where only the gate refuses. Its fixed case is now that. The model predicts it in about 7 of 512 cases, so a single run would miss it about once in a thousand. The kernel test now goes on with another 512 cases when a run misses a gap, up to 3 runs, as the model test already does.
  • The gate_paths fuzz target's granted/nested grant now holds READ | LIST, the rights of the grant around it, instead of READ.

Safety and compatibility

Breaking: a policy that nests a grant with fewer rights now fails to build, so this belongs in 0.5.0. The changelog entry says so and gives the replacement: grant the parent read-only and the writable parts beneath it.

GateError and AuditReason are #[non_exhaustive], so the new variants are additive. Broker maps NestedGrant with the other errors a serving broker never returns.

Not caught, and recorded in RFC 0012 and the threat model: a directory moved into another grant after both were made (a new test shows it), and a bind mount, where .. at the mount's root leads to the mount point.

Verification

Before the change, on main (a scratch test file, not committed), all five failed: a nested grant with fewer rights, the outer grant second, the inner grant named through a symlink, and narrowing below the outer grant were all accepted, and a create through /proj/link wrote into the read-only .git:

test nested_fewer ... FAILED
test outer_second ... FAILED
test through_symlink ... FAILED
test narrow_below ... FAILED
test bypass_writes_into_the_read_only_grant ... FAILED
test result: FAILED. 0 passed; 5 failed

After it, on macOS with Rust 1.99.1:

  • cargo test --locked: 132 passed, 0 failed. That includes the case-insensitive test taking its refusal branch on APFS, and the model test reaching 512 refused grants and 95 refused narrows in one run of 512 cases.
  • cargo fmt --all --check passed. cargo clippy --locked --all-targets -- -D warnings is clean on macOS and for x86_64-unknown-linux-gnu, with and without --all-features. cargo +1.85.0 check --locked --all-targets passed.
  • cargo +nightly fuzz run gate_paths over its seeds passed, then 90 seconds and 57,730 runs into a scratch corpus found nothing.
  • fsgate-broker --grant <dir>:rwc --grant <dir>/.git:r -- /usr/bin/true exits 1 with the message above.
  • The kernel gap's frequency comes from replaying the model's predictions for ten runs of 512 generated cases (4 to 10 cases a run). It is an estimate, not a kernel measurement.

Not verified locally: the Linux kernel tests (gate_model.rs::kernel, including the new fixed case and the retry), because this machine has no Linux. This PR's CI runs them.

Documentation and release

RFC 0012 (new). RFC 0001, 0002 and 0005, the threat model, the review brief, the README, fuzz/README.md, and the changelog. No version change. The M9 roadmap entry records the CI and fuzz runs on 106ef72 as evidence: all 25 checks passed, including both kernel model tests on Linux 6.6, 6.12 and 7.3, and 901 seconds of gate_paths on Linux and macOS with no finding.