Skip to content

fix(containment): preserve Linux grant identity through mounting - #160

Merged
kevin-orellana merged 4 commits into
mainfrom
feature/linux-grant-identity
Oct 26, 2026
Merged

kevin-orellana merged 4 commits into
mainfrom
feature/linux-grant-identity

Conversation

@kevin-orellana

@kevin-orellana kevin-orellana commented Oct 23, 2026 •

Copy link
Copy Markdown
Collaborator

Human readable section

Problem: Linux could validate a filesystem grant, then mount a different object placed at the same path before startup.

Example: You grant read access to /work/report.txt, and Box validates that file.
Before Box makes it available to the workload, another host process renames it and puts a different file at /work/report.txt.
Previously, the workload could read that replacement even though Box had validated the original file.
With this fix, Box either uses the original file it already opened or refuses startup when it detects the replacement.
When the original file stays in place, the workload can still read it.

Solution: Retain the validated object, mount through a checked open descriptor, and verify the mounted object before starting the workload.
Refuse startup when the object identity changes.

Trade-off: Prepared paths retain descriptors until release or drop.
A directory grant still permits changes inside that directory.
Configuration serialization remains unchanged; the receiving process captures identity when it prepares paths.

Description

Preserve explicit filesystem and socket grant identity through Linux mount completion.
Retaining the original object prevents inode reuse from defeating the comparison.
Opening the mount source inside the new namespace preserves existing namespace permissions.
The mounted-target check verifies that the bind installed the checked object.

Keep protection checks and descriptor cleanup ordering.
Correct Linux test fixtures so private-state assertions reach workload operations under existing grant rules.
Retry only the transient text-file-busy error in the uncontained copied-executable control, with a two-second deadline.
Equivalent fixed-size parsing loops clear owned lint warnings.
No public configuration or wire format changes.

Related Issues

No public issue is linked.

Type of Change

Bug fix

Testing

Conflict repair validation

Updated to 1d3d158 by merging current main. The conflicting configuration fixtures use main's existing Linux read-grant helper. Their private-state refusal assertions remain unchanged.

The full all-feature containment suite passed on Linux without runtime prerequisite skips. All 24 configuration tests, formatting, and strict all-target/all-feature containment lint passed. Independent review checked the combined grant-identity and dependency-admission changes and found no actionable issues.

Hosted checks are queued for this revision. The human ownership attestation remains unchanged. The earlier results below retain their original revision and scope.

Previous follow-up 6ac4917 handles the compiler's bare-name response when its optional linker plugin is absent.
That case reports an explicit prerequisite skip; required helpers, malformed responses, and failed compiler queries still fail.
On Linux, the normal compiler control, four injected-response controls, all 519 containment tests, formatting, and strict containment lint passed.
Independent review verified the changed file and test receipts. Hosted checks are rerunning for this commit.

The integration results below cover the preceding revision 060a1bf, whose required hosted checks also passed.

The integration revision incorporates main at f0dc6f49ea8bb8608502afffa50c8748f6c24ca2.
Its ten changed source files match the integration review and test manifest.
Runtime validation used an unprivileged ARM64 Amazon Linux development host, kernel 6.12.111, Rust 1.98.1, and native temporary filesystems.
Effective capabilities were zero before namespace setup.

Check Result
just check Passed formatting, repository workspace lint, and the full all-feature workspace test suite. Workspace lint uses the documented vendored-shell allowances.
Complete containment package 519 passed, zero failures or ignored tests, and no runtime prerequisite skips.
Concurrent grant replacements 12,000 unique attempts: 8,811 validation refusals, 1,607 mount refusals, and 1,582 starts with granted bytes. No replacement reads or unknown outcomes.
Loader and access controls Permitted reads/writes and compiled-library execution passed. Dependency replacement, private-file reads, and malicious compiled dependencies were refused.
Unsupported-host control Injected ID-map permission denial produces an explicit optional skip. Required-runtime mode fails on the same denial. Unexpected errors still fail.
Fork isolation Grant and loader tests restart in an isolated test process. The existing execute-permission control also passed 20 consecutive isolated runs.
Additional checks Strict all-target/all-feature containment lint and native macOS containment compilation passed.
Independent review Reviewed merged pin selection, descriptor release, unchanged refusal assertions, fault-control results, and final source hashes. Hosted checks for the new commit remain separate.

The host prerequisite probe does not call production namespace setup, so a production regression cannot turn itself into a prerequisite skip.
The compiler fixture grants its discovered linker plugin and the standard /usr/lib64 library directory where present.
Its private-file and malicious-dependency refusal assertions remain unchanged.
One intermediate workspace run failed an existing execute-permission test without reporting the underlying error.
That test now runs in isolation and reports actual kernel errors; the historical cause is not established.

The original patch also passed the deterministic suite with 120 cases and nine macOS-only skips.
That pre-integration result is historical; the standalone deterministic suite, examples script, minimal-feature build, and Linux x86-64 compile check were not repeated locally for this follow-up.
The workspace retains inherited ignored tests and optional/platform exclusions.
Local tests and compile checks do not establish managed-runtime or release acceptance.

  • I ran the relevant suites (just check, or cargo test --workspace --all-features)
  • If I touched Rust, I ran cargo fmt --all and cargo clippy --workspace --all-targets --all-features -- -D warnings

Formatting used cargo fmt --all and just fmt-check.
The exact second attestation remains unchecked because workspace lint used the required repository just clippy recipe and its documented allowances.
Strict Clippy also passed for the containment package.

Checklist

  • I have read the CONTRIBUTING document
  • I have reviewed and understand every line of code in this PR, including any generated by AI tools, and I can explain why it works
  • My change is focused and reasonably small; I have split unrelated work into separate PRs
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • My changes generate no new warnings

This pull request was prepared with AI assistance. The human contributor must review and adopt the description and ownership attestation.
Code comments document the internal identity and descriptor rules; no user-facing configuration documentation changes are needed.


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

## Problem

Linux grant validation could approve an object and later mount a replacement at the same pathname.
The swapped_files_directories_and_sockets_refuse_before_startup regression reproduces this gap on the base.

## Solution

Retain each validated object with an open descriptor and its device, inode, and type.
Check an opened mount source against that identity, bind through its descriptor, and verify the mounted target before startup.
Release retained descriptors before inherited-descriptor cleanup.
Keep public configuration and wire formats unchanged.
Correct Linux fixtures so private-state and permitted-access assertions reach their intended operations.
Bound the copied-executable host control retry to the existing transient ETXTBSY case.

## Tests

On Linux ARM64, bash scripts/test-all.sh passed the full workspace, including all 503 containment tests.
concurrent_source_swaps_never_expose_replacement_bytes completed 12,000 attempts without replacement reads or unknown outcomes.
read_only_and_writable_grants_keep_their_access_modes and unchanged_symlinks_and_hardlinks_preserve_permitted_reads passed.
a_grant_mount_that_does_not_install_the_checked_object_is_refused detects a missing mounted-target check.
The deterministic suite passed 120 cases with nine macOS-only skips.
just fmt-check, just clippy, and strict all-feature containment clippy passed.
Linux x86-64 and native macOS containment compile checks passed; runtime qualification on those targets was not run.
Deliberate fault controls failed as expected. The copied-executable control passed 20 repetitions and still failed with a persistent writer.
The workspace retains inherited ignored/optional cases, a base-reproduced telemetry teardown panic, and an existing debug latency target miss.
The no-default-features build passed with existing dead-code warnings. The SDK tutorial and managed-runtime qualification were not run.
Independent adversarial review verified the final source hashes and test receipts. Human acceptance and hosted checks remain pending.
@kevin-orellana
kevin-orellana marked this pull request as ready for review October 23, 2026 16:43
@kevin-orellana
kevin-orellana enabled auto-merge (squash) October 23, 2026 16:47
@github-actions

Copy link
Copy Markdown

Assessment: Comment (approve-leaning)

This fix closes a real TOCTOU on the Linux grant path and does so fail-closed. I traced the full flow and the core mechanism is sound: the O_PATH pin reserves the inode so it cannot be reused, the bind mounts the pinned descriptor via /proc/self/fd/N rather than the attacker-controllable path, and require_grant_identity re-checks the mounted target. The guard is well pinned by tests that inject the defect (inode-reuse, path swap, a no-op mount via seccomp) and observe the refusal.

Review notes
  • Premise alignment: Strengthens "validate once, then act on the identity you approved" on Linux, which previously held only pre-mount.
  • Interface/wire: No public surface change. identity/pin are #[serde(skip)], and PathGrantWire's TryFrom re-runs PathGrant::new, so the receiving (trampoline) process re-captures identity when it prepares paths — serialization is unchanged, as described.
  • Defensive refusal: The missing grant identity path in establish cannot fire for legitimate configs — Grant/Socket bind sources are always the grant's resolved key, dependency binds use Interpreter/Library origin, and Dir-scope grants are fresh tmpfs, not binds.
  • Pin lifetime: Releasing grant_pins after pivot_into is safe since the bind mount itself keeps the inode pinned.
  • Tests: One latent flakiness concern (see inline comment on the direct fork in the multi-threaded harness). Minor nits not worth blocking: .read(true) is ignored under O_PATH; release() maps a poisoned lock to ConfigValidation.

Nicely scoped change with thorough adversarial tests, including the 3,000-iteration race control.

@kevin-orellana kevin-orellana added the linux Linux containment and Linux launch work label Oct 23, 2026
@github-actions

Copy link
Copy Markdown

Assessment: Comment (approve-leaning) — re-review of 060a1bf

Re-reviewed after the rebase onto #158. The previously-raised fork-in-multithreaded-harness concern is resolved: in_child now asserts an isolation marker and every forking test runs through in_fresh_process (or the dedicated single-test race process). I also traced the new work that landed in this revision.

What changed since the last review
  • Loader reconciliation (new): establish now selects mount_source between this PR's grant pin (Grant/Socket) and #158's loader dependency_sources pin (Interpreter/Library). The two are mutually exclusive by origin; and where a path could be both, the grant pin wins and require_grant_identity still enforces the checked identity. The is_dir()/metadata() call sites were moved to mount_source consistently and remain equivalent to the prior source for dependency binds. Looks correct.
  • Test isolation: grant_identity_tests, the merged loader_admission tests (15 before/after — none lost, just reordered), and the exec-permission test in mod.rs now run in a fresh single-threaded process. Good.
  • Stronger coverage: box_shell.rs replaces refusal-counting with explicit deny markers, a host-side content-equality check, and a permitted-write positive control — a genuinely better test. run_config.rs adds a Linux-only read grant beside the write grant while still asserting the box-directory-child refusal. contains_exec_target_linux.rs adds a bounded (2s) ETXTBSY retry.

One new non-blocking suggestion inline: the liblto_plugin.so fixture grant can panic on hosts whose cc doesn't ship the LTO plugin (bare-name fallback), where a graceful skip would be safer.

Solid revision — the fork isolation is exactly right and the loader-pin reconciliation is handled cleanly.

@kevin-orellana

Copy link
Copy Markdown
Collaborator Author

Addressed the optional compiler-plugin suggestion in 6ac4917.

The test now recognizes the compiler's bare liblto_plugin.so response and reports an explicit prerequisite skip. It does not skip missing required helpers, malformed plugin output, or compiler-query failures.

Linux validation covered the normal installed-plugin build and refusal controls, the absent-plugin response, a missing required helper, empty plugin output, and a failed query. Only the optional absence skipped; the three failure controls still failed. All 519 containment tests, formatting, and strict containment lint passed. Independent adversarial review verified the source hash and receipts.

@github-actions

Copy link
Copy Markdown

Assessment: Approve — re-review of 6ac4917

The one open suggestion from the previous pass is resolved. loader_dependencies_linux.rs now detects when cc -print-prog-name=liblto_plugin.so returns the bare name (plugin absent / non-gcc cc) and skips with a clear message instead of panicking on allow(...).unwrap(). The guard is correctly scoped to the optional plugin only, so cc1/collect2 coverage is unchanged, and hosts without the LTO plugin degrade to a clean skip rather than a hard failure.

Combined with the earlier-verified fixes (fork isolation via in_fresh_process + the in_child isolation-marker assertion, and the grant-pin/#158 loader-dependency-pin reconciliation in establish), I have no remaining concerns. Nicely scoped security fix with thorough adversarial tests.

@github-actions

Copy link
Copy Markdown

Assessment: Approve — re-review of merge 1d3d158

This head commit is a merge of main (96de955) into the PR branch (6ac4917). I reproduced a clean auto-merge of the two parents and diffed it against this commit: the only hand-edit in the entire merge is the conflict resolution in crates/box/tests/run_config.rs — everything else auto-merged cleanly, with no stray "evil merge" edits.

The conflict resolution is correct: it drops the branch-side inline if cfg!(target_os = "linux") block in favor of main's "[agent.filesystem]\n{read}write = [{:?}]\n" template, fed by the pre-existing linux_read_grant() helper. This preserves the PR's Linux-requires-explicit-read semantics while adopting main's cleaner formatting. The output is semantically identical on Linux (both emit read + write; TOML table-key order is irrelevant, and the affected tests assert on output text, not exact config equality). cargo check -p strands-box --test run_config passes.

My earlier finding (the liblto_plugin.so panic → skip guard) survived the merge intact. No remaining concerns.

@EA-b13

EA-b13 commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

I think we should also be explicit that this check doesnt carry over to MacOS.

Linux binds one object at startup, so freezing the checked object closes the gap. Seatbelt rules are path strings that the kernel matches on every access for the whole run. So if a host process puts a different file at /work/report.txt, the workload on macOS reads the replacement, whether that happens before or after start. Adding an inode check before sandbox_init would only narrow the window before startup. On macOS this is how a path grant behaves, not a missed check, and Seatbelt can't attach a rule to a specific file object.

@kevin-orellana
kevin-orellana merged commit d103a0c into main Oct 26, 2026
22 checks passed
@kevin-orellana
kevin-orellana deleted the feature/linux-grant-identity branch October 26, 2026 22:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

linux Linux containment and Linux launch work

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants