Skip to content

fix: refuse unsafe Linux file permission requests - #199

Merged
kevin-orellana merged 6 commits into
mainfrom
feature/linux-file-permission-policy
Nov 2, 2026
Merged

kevin-orellana merged 6 commits into
mainfrom
feature/linux-file-permission-policy

Conversation

@kevin-orellana

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

Copy link
Copy Markdown
Collaborator

Human readable section

Problem. An untrusted Linux program can leave special permission bits on writable host files after Box exits. Where execution rules honor these bits, an executable can run with the file owner's user or group identity. Box's existing execution restrictions do not prevent the stored permission change.

Solution. Reject file-creation and permission-change requests that explicitly include either special bit. Continue to permit ordinary writes under existing file-access grants. Continue to reject file-owner and group-owner changes.

Trade-off. Directories can still inherit their parent's shared group and the bit that passes this group to new subdirectories. Tools that explicitly request these shared-group permissions fail, including git init --shared, which can leave a partial repository.

Permission updates that preserve existing special bits also fail. This includes some chmod commands on directories that inherited the group bit. A request that clears both bits can succeed under existing grants. This change does not remove existing bits or expand file-access grants.

Description

The special bits are set-user-ID and set-group-ID. The group bit also controls directory group inheritance.

The existing Linux restriction filter now checks the requested mode for fchmod, fchmodat, fchmodat2, and mkdirat. For openat, it checks the mode only when O_CREAT or the complete O_TMPFILE flag is present. An unused mode argument remains ignored. Comparisons use the low 32 bits consumed by the kernel.

Tools that explicitly request shared-group modes remain incompatible: install -d -m 2775, mkdir -m g+s, and git init --shared fail. This also applies under an existing shared-group parent. Git can leave a partial repository. The tested ordinary versions of these commands succeed.

Permission changes, including executable-bit changes, require a requested mode without either special bit. GNU chmod g+w and chmod 755 fail on a directory that inherited its group bit because their requests preserve it. A request that clears both bits can succeed under existing grants.

Ownership calls and alternate creation routes retain their existing default denial. The native regression checks refusal results, final mode and ownership, and host-side absence after refused creation. Safe uncontained controls prove that the requested operations can succeed on owned temporary files.

This change is independent of the native-policy integration. It adds no policy input, configuration field, or dependency commit. This PR does not change whether workloads can read helper programs. It also excludes performance work and unrelated platform defects.

Related Issues

Addresses the file-permission portion of #51. The issue covers other controls, so this PR does not close it.

Type of Change

Bug fix

Testing

Current revision: ab5ede2cd599bfefb3f7d13cee11c22edf80a47b. This branch includes conflict-repair merge 452da395c24b4a2412c30ef8ce8908e7f3cd246b with main ef009143f5ed960d18c145c4fcc7063da1e9ec93. The sole conflict joined adjacent restriction-filter helpers. The resolution preserves this PR's special-permission checks and main's terminal-control restrictions unchanged. The permission regressions and main's terminal tests are unchanged. Codex reviewed the changed files, and a second agent independently reviewed the resolution. Formal maintainer approval remains separate.

Final naming-only follow-up. ab5ede2 addresses the human review nit by replacing the two numeric masks with libc::S_ISUID and libc::S_ISGID through lossless conversions. No policy value or control flow changed. Formatting, strict all-target containment lint, all 26 syscall unit tests, and both non-root permission regressions passed again with zero omitted prerequisites. A second agent reviewed the one-line diff. The reply records the change, and that thread is resolved. The full local suite and root controls below ran on 452da39; they were not repeated for this value-preserving naming change.

The human approval submitted for dd5eea7 was dismissed after the conflict-repair push. Formal approval of the current revision remains required.

Current hosted checks and automated review. The current-head CI run and automated-review trigger are pending. The trigger is not a completed reviewer assessment. Previous-revision hosted results below do not establish current-head approval.

Local validation of merge 452da39. Amazon Linux 2023 ARM64, kernel 6.12.113-138.209.amzn2023.aarch64, Rust 1.98.1; Linux experimental support enabled. An isolated target used four build jobs and four test threads. The product and containment build, whole-workspace formatting, strict permission-test lint, and the documented whole-workspace lint recipe passed. The lint recipe uses only its existing vendored allowances. All 26 syscall unit tests and seven terminal unit tests passed.

Both permission regressions passed as non-root and real root. Each identity produced 87 successful outside controls, 23 permitted contained operations, and 64 expected refusals, with zero omitted prerequisites. This includes inherited-directory permissions and requests that preserve or clear existing special bits. The two helper test entries are not additional security cases.

Two initial root launches failed during namespace setup because private parent directories prevented reopening the test executable; no contained permission assertion ran. The identical binary then passed from a unique, traversable task-owned directory under /tmp, with no source or shared-directory permission change. Its SHA-256 was 4c50795699ef4563f122986da8f21ff45724bd4950e54a2f1a8a6c2d5e6fc331 at both locations. The failed setup logs remain recorded alongside the successful runs.

The full cargo test --workspace --locked --all-features --no-fail-fast -- --show-output command completed successfully: 4,619 top-level pass entries, zero failures, and 17 ignored entries across 142 result blocks. One nested child result is excluded from those totals. The raw output audit failed because the installed-alias unit test lacked its helper beside the test binary. After applying the existing hosted build-output installation recipe, that exact test passed separately with zero skips; its separate output audit passed. The full suite was not repeated, and its raw audit failure remains recorded. Two documented omissions remain: the real credential-daemon fixture was unavailable, and the execute-only alias read assertion is unsupported by the namespace backend; the alias write assertions ran.

These local results do not qualify a release. No shared deterministic fixture suite ran locally for this repair. Historical tests and their limits follow.

Previous revision: November 1 hosted checks. The continuous integration gate passed for dd5eea7f19caec86aa2a7c41fe693231ff734c0a on November 1, 2026. This status is separate from the local and historical test results below. The optional Verdict job skipped and provides no coverage.

Previous head dd5eea7f19caec86aa2a7c41fe693231ff734c0a merges main e4ca8b80359b2213d684de31cd6ef32e0d61478a. The sole conflict combined two independent documentation additions; both remain intact. Both permission regressions passed again as root and non-root on this combined source. Strict scoped lint and formatting passed. Independent review confirmed that the merge retains main and introduces no additional permission-policy change.

Previous branch revision dd5eea7 passed the complete hosted validation run: Linux ARM64 and macOS all-feature workspace tests, both deterministic containment suites, native macOS checks, packaging, lint, and the final gate. Security analysis also passed. The optional Verdict job was skipped; it provides no coverage. These results do not establish deployment qualification or release acceptance.

The automated review of this exact revision reports Approve with no new findings. At that November 1 checkpoint, both review discussions were resolved. Current human review status is recorded above.

Earlier local validation.

Original tested source: e74840408aad18421651763e3c9d1808b8ef8160, based directly on main 6418aad802e92880b6c5c431c942cef5a283aaef. The original four-file source hashes exactly match the candidate tested and independently reviewed before publication. Follow-up commit 7396d35e3e3189d4f2e3b194962587c8e0fab6b0 adds only a two-line code comment about directory inheritance. Formatting and whitespace checks passed. At that revision, removing those comment lines reproduced the original tested source exactly.

Earlier follow-up: 0ee541f41fc4960e9d3db65c41b7b909e74a291f updates both guides and adds permission_changes_refuse_existing_special_bits_unless_cleared. It changes no enforcement code. The new regression covers actual directory inheritance and existing special-bit files, successful host controls, refusals that preserve the bits, successful requests that clear them, and final host mode/ownership checks.

The new regression passed under real-root and non-root launches through the production trampoline: 14 successful host controls, eight contained refusals, six contained successes, and zero skips. The original 80-case permission regression also passed under both identities. Formatting, whitespace checks, and cargo clippy --locked -p strands-box-containment --all-features --test file_permissions_linux -- -D warnings passed. An initial non-root assertion incorrectly compared host IDs with sandbox IDs; the corrected assertion compares identities within each namespace, and both final runs passed.

Follow-up environment: Amazon Linux 2023 ARM64, kernel 6.12.113-138.209.amzn2023.aarch64. Independent adversarial review found no remaining blocking issue in the final follow-up. The later November 1 revision passed the hosted checks and automated review recorded above.

Original revision: earlier validation and its limits

The remaining table records the original revision's broader validation and its limits.

Native target: ARM64 Linux, kernel 6.12.111-136.204.amzn2023.aarch64, Rust 1.98.1. Linux experimental support was enabled. Temporary fixtures used an owned executable filesystem. All permission-test host prerequisites were available.

Check Command or evidence Result
Containment build and tests cargo build --locked -p strands-box-containment --all-features; cargo test --locked -p strands-box-containment --all-features -- --nocapture Passed. The native permission test has 80 successful host controls, 20 permitted contained operations, and 60 refusals. No tested contained request explicitly stored either special bit.
Lint and formatting cargo clippy --locked -p strands-box-containment --all-targets --all-features; cargo fmt --check Passed. No owned containment lint warnings.
Feature-disabled build cargo build --locked -p strands-box-containment --no-default-features Passed, with 66 existing unused/dead-code warnings recorded.
Executable build cargo build --locked -p strands-box -p strands-box-containment --all-features --bins Passed.
Mutation proof Seven deliberate changes, applied only in isolated scratch copies All failed as expected. Removing mode guards stored forbidden bits; ownership and alternate-creation admission changed final metadata or created forbidden paths. Overfiltering unused modes broke an ordinary-operation control. The unchanged scratch baseline passed.
Shared-folder exception The unchanged native test binary ran under an owned parent with mode 02770 Passed. Ordinary subdirectories stored 02755 inside and outside Box. Explicit special-mode requests remained refused.
Deterministic integration bash test-integ/run.sh 119 cases passed, one failed, nine declared macOS-only cases skipped; zero errors, full case coverage, empty quarantine list.
Full workspace and examples bash scripts/test-all.sh Incomplete. The workspace phase reached its 600-second cap. Completed result blocks had 798 passes and no failures. The later examples build passed; the manual SDK tutorial was not run.

The sole deterministic failure is CN-L-02, the Linux home-directory containment case. Its fixture compares a canonical home path with a noncanonical spelling and raises StripPrefixError. A freshly rebuilt unchanged copy of the exact main revision reproduces the same failure. No candidate-only deterministic failure was found.

The metadata assertions reuse the principles of macOS file-control cases CN-F-02 and CN-F-03. macOS source is unchanged; native macOS tests and other Linux targets were not rerun for this candidate. The inheritance run is supplemental evidence; the regression source does not create that special parent itself.

Supplemental real-tool verification ran 32 commands on the original tested revision using coreutils 8.32 and Git 2.50.1. All 16 host controls and eight ordinary contained controls succeeded; eight explicit shared-mode operations failed as expected. Git results were confirmed with both empty and installed default templates. Final host metadata and syscall traces establish the distinction between inherited directory bits and refused explicit requests. These measurements do not establish successful package-manager workflows that request shared-group modes.

Independent adversarial review found no blocking production defect. At this earlier checkpoint, full repository validation was incomplete. The November 1 revision's completed hosted results are listed above.

  • I ran the relevant suites (just check, or cargo test --workspace --all-features)
    • The November 1 revision passed cargo test --workspace --locked --all-features on hosted Linux ARM64 and macOS runners. Linux also passed the unexpected-skip audit. The original local time limit is retained above as historical evidence.
  • If I touched Rust, I ran cargo fmt --all and the documented workspace lint recipe.
    • Whole-workspace formatting and the documented lint recipe passed locally on the current conflict repair without changing tracked files. Previous-revision hosted lint passed for all workspace targets and features with -D warnings and the vendored allowances required by CONTRIBUTING.md. That required recipe replaces the template's unsupported bare lint command; no new allowance was added. The changed permission test also passed strict scoped lint without allowances.

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
    • This author attestation records delegated Codex review at Kevin's request, including independent agent review of the conflict repair. It does not record formal maintainer approval.
  • 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

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 workloads can store set-user-ID and set-group-ID bits through permitted file-mode calls. Execution restrictions do not prevent those bits from remaining on the host file.

## Solution

Reject explicit special-bit requests during permission changes and creation. Preserve ordinary writes, executable-bit changes, and approved shared-directory inheritance. Retain existing ownership and alternate-creation refusals. Add no filesystem grant, configuration field, or dependency.

## Tests

The all-feature containment suite passes. direct_file_permissions_preserve_writes_and_refuse_special_modes checks 80 host controls, 20 contained successes, and 60 refusals with final metadata and host-path assertions. Seven deliberate mutations fail; an unchanged scratch baseline passes. An additional native run under an owned shared-group parent preserves inherited directory mode 02755.

Containment build, clippy, formatting, and feature-disabled build pass on native ARM64 Linux. The deterministic suite reports 119 passed cases, one failure, and nine macOS-only skips. The sole home-path assertion failure also reproduces on freshly rebuilt unchanged main. The full workspace phase exceeded its 600-second cap and remains incomplete. Native macOS and other Linux targets were not rerun for this four-file candidate. Independent adversarial review found no blocking production defect.
@github-actions

Copy link
Copy Markdown

Assessment: Comment (close to approve)

The fix closes a real gap — a nosuid bind stops set-*ID bits from changing identity on exec, but not from being stored on the shared host file — and the seccomp approach is sound. I traced the two-filter model (permit whitelist narrowed by the restriction blacklist): the mode-argument indices, the set-user-ID/set-group-ID OR across rules, the O_CREAT/complete-O_TMPFILE gating on openat, and the Dword (low-32-bit) comparisons are all correct, and the alternate creation routes (openat2, mknodat) remain denied by the permit whitelist.

Review notes
  • Correctness: No bypass found on the supported target. Legacy open/creat/chmod/mkdir/mknod don't exist on aarch64, and the remaining creation paths are whitelist-denied.
  • Scoping: The target_arch = "aarch64" gate on the new test is correct and consistent with facade.rs selecting the namespace backend only on ARM64 (x86_64 Linux is PlatformUnsupported), so this is not a CI coverage gap.
  • Dependencies / API: No new crates (reuses existing serde/sha2/tempfile), and no frozen public surface changes — purely pub(crate) internals plus a new test, so no API bar-raising review is required.
  • Testing: Strong host-vs-contained differential coverage including the 1<<32 upper-bit and unused-mode cases. The probe/assertion harness is dense and stringly-typed (the failures accumulator), which may be worth a comment block or small helper for future maintainers — not blocking.
  • One item to confirm: the mkdirat set-group-ID denial trade-off (see inline comment).

Thorough work — the threat model, enforcement, and docs are well aligned.

Comment thread docs/user/config.md Resolved
@github-actions

Copy link
Copy Markdown

Assessment: Comment (re-review of 7396d35)

The new commit adds only a two-line comment, which is accurate. The tested source still matches e748404. One correctness finding from the earlier source is new in this review (see inline on docs/user/config.md). The filter refuses every chmod on an entry that already has a set-ID bit, so the shared-group directories this PR keeps can't be chmod'ed unless the bit is dropped. I reproduced this with the production restriction filter. It is over-refusal, not a bypass. The user and design docs currently state otherwise.

Checks that held
  • Mode argument indices (fchmod=1; fchmodat/fchmodat2/mkdirat=2; openat=3 with flags=2) match the kernel signatures.
  • Either bit alone is refused (separate OR'd rules). Upper 32 bits are ignored (Dword).
  • openat with no creation flag and a special mode is not refused, which matches the kernel ignoring the mode there.
  • Complete-O_TMPFILE matching leaves no bypass: the kernel rejects __O_TMPFILE without O_DIRECTORY.
  • openat2, mknodat, and the ownership calls are still denied by the permit list. On aarch64 there is no legacy open/creat/chmod/mkdir/mknod route.
  • The aarch64-only test gate matches facade.rs:81, where the namespace backend is used only on ARM64.
  • No new dependency, no frozen surface touched (pub(crate) only), and no AGENTS.md premise weakened.

@github-actions

github-actions Bot commented Nov 1, 2026

Copy link
Copy Markdown

Assessment: Approve (re-review of 0ee541f)

0ee541f changes only the test and docs. crates/containment/src is byte-identical to 7396d35. The docs now give the right result for the finding on existing set-ID bits, and permission_changes_refuse_existing_special_bits_unless_cleared pins it. No new findings.

Checks that held
  • In all 7 EXISTING_MODES rows, the contained result is EPERM exactly when requested & 0o6000 != 0. That matches the restriction filter: refused for inherited-dir add-write and no-change, setuid keep, and setgid keep; allowed for each clear-bit row.
  • The fixture really produces an inherited bit: the 02770 parent plus assert_ne!(mode & 0o2000, 0) before the mode is fixed. The host-control branch proves that each requested mode can be stored outside Box.
  • A refused request leaves mode, uid and gid unchanged. The test checks this inside the probe and again from the host side, which matches the new doc sentence.
  • The user-doc examples (chmod g+w and chmod 755 keeping the bit) are limited to directories, which is correct: a numeric chmod on a regular file does clear the bit.
  • The test name quoted in docs/design/containment.md exists.

I could not run the new test itself: the file is gated to aarch64 and this runner is x86_64. Its result rests on the author's ARM64 run.

@github-actions

github-actions Bot commented Nov 1, 2026

Copy link
Copy Markdown

Assessment: Approve (re-review of dd5eea7, the merge of main)

The merge resolution keeps both sides. Compared with the new main parent (e4ca8b8), the PR still touches only its 4 files. syscall.rs, file_permissions_linux.rs and docs/user/config.md are byte-identical to 0ee541f. In docs/design/containment.md, the only merge change is that main's new check_read_grant paragraph now sits before the PR's section, and both texts are intact. No new findings.

Checks that held
  • Main's new native_filesystem_handoff parameter doesn't reach this filter. apply_backend returns UnsupportedCapability whenever it is Some, the trampoline passes None, and NamespaceBackend ignores it.
  • Main's seccomp listener (linux_enforcement/notify.rs) traps only getpid as a probe. Even if it later traps the chmod or creation calls, seccomp gives ERRNO precedence over USER_NOTIF, so the PR's EPERM would still win.
  • The merged tree builds: cargo test --no-run -p strands-box-containment --all-features succeeds, and the 26 namespace::syscall unit tests pass on x86_64.

The aarch64-gated permission regressions still can't run on this x86_64 runner. They rest on the author's post-merge ARM64 run.

pgrayy
pgrayy previously approved these changes Nov 2, 2026
@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown

Assessment: Approve (re-review of ab5ede2, after merge 452da39)

ab5ede2 changes one line: it swaps the literal masks [0o4000, 0o2000] for [u64::from(libc::S_ISUID), u64::from(libc::S_ISGID)]. The filter behaves exactly as before. Merge 452da39 keeps the PR intact against main ef00914: it still touches the same 4 files, with the same per-file line counts as at dd5eea7. No new findings.

Checks that held
  • libc::S_ISUID and libc::S_ISGID are 0o4000 and 0o2000. I printed them with the locked libc. mode_t is u32 on Linux, so u64::from is lossless, and the Dword comparison is unchanged.
  • In syscall.rs, the only thing the merge adds is main's ioctl rework (terminal_input_injection_rules becomes terminal_control_rules). The special_file_mode_rules dispatch arms for fchmod*, mkdirat and openat still run before the new ioctl arm.
  • The docs/user/config.md and docs/design/containment.md hunks from the merge are main's MCP and alias wording. The PR's "Linux file permission changes" text is unchanged.
  • cargo clippy -p strands-box-containment --all-features --all-targets -D warnings is clean, and the 26 namespace::syscall unit tests pass on x86_64.

The aarch64-gated permission regressions still can't run on this x86_64 runner. They rest on the author's ARM64 run.

@kevin-orellana
kevin-orellana merged commit c2bea3b into main Nov 2, 2026
22 of 23 checks passed
@kevin-orellana
kevin-orellana deleted the feature/linux-file-permission-policy branch November 2, 2026 22:59
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.

3 participants