Skip to content

fix(linux): refuse root file creation through built-in interpreters - #329

Closed
kevin-orellana wants to merge 1 commit into
mainfrom
feature/linux-root-interpreter-files
Closed

kevin-orellana wants to merge 1 commit into
mainfrom
feature/linux-root-interpreter-files

Conversation

@kevin-orellana

@kevin-orellana kevin-orellana commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

Human readable section

Status: Withdrawn in favor of PR #348, the active proposal for Linux root directory-write support.

The two PRs take different approaches to the same root-output behavior. This PR extends #300 by refusing host file creation and renames through built-in Shell and Python when Box runs as root. PR #348 restores sandbox directory-write grants and documents that permitted output can remain root-owned; a separate host program consumes that output using its own authority.

We are consolidating on the #348 model. This PR's blanket ownership refusal will not be merged or copied into #348. Existing policy checks and reachable-path restrictions still apply to built-in operations. The implementation and validation below are retained as historical evidence of the withdrawn approach. This consolidation does not assert closure of the related security assessment.

Historical proposal and validation

Human readable section

Problem: When root starts Box on Linux, built-in Shell and Python operations can create files with the trusted process's root identity. The sandbox restriction in #300 does not cover these operations.

Solution: On Linux, check whether either real or effective user ID is zero. Refuse new files, directories, symbolic links, and renames through the built-in interpreters for those launches. Reads, in-place writes, and appends to existing files remain subject to policy and the reachable paths check. Shell's in-memory filesystem remains writable.

Trade-off: Run Box as a non-root account when scripts need to create or rename entries in the surrounding OS. Root launches also refuse saves that replace an existing file through rename. Non-root Linux and macOS behavior stays unchanged.

Description

The ownership check runs after policy and the reachable paths check. A refused operation records an enforcement denial and a failed history outcome. An ordinary I/O failure remains distinct from an ownership refusal.

Host file opens through Shell and Python omit O_CREAT for Linux root. The operating system therefore refuses a missing file without creating a replacement, including when an approved file disappears before the open. Directory creation, symbolic-link creation, and renames have corresponding checks before the operation. Existing identity checks remain in place.

The refusal tells the operator to run Box as a non-root account. Operator documentation describes the restriction and preserved behavior. The change adds no policy actions or configuration keys and retains #300's sandbox restriction. It does not remove policy-approved access to existing file contents.

Related Issues

Follow-up to #300.

Type of Change

Breaking change

Testing

Tested on Amazon Linux 2023 ARM64, kernel 6.12.113-138.209.amzn2023.aarch64. The tests use the production Shell and Monty paths within the test binary. Local results cover the working-tree candidate based on 3f69359a1ae257fd2b299b002050928d526a518f. All 13 changed-file SHA-256 hashes match commit 5f513820fa5ad3b1a06e1a4990b336ec8a16054a. The local checkout was not yet committed during execution; hosted CI will test the commit directly.

Check Result
Focused interpreter tests 15 passed, zero failed, zero ignored. Covers creation and rename refusals, existing-file access, disappearing-file controls, Shell memory storage, non-root creation, and denial records.
Actual host-root execution 7 behavior tests and 6 audit tests passed. These repeat focused cases under actual root; they are not additional unique coverage.
Disabled-check comparisons Disabling the initial guard caused 3 test failures. Disabling final admission checks and Shell's creation-flag removal caused 5 failures. Restoring the candidate returned all 15 focused tests to passing.
File-open traces The guard-disabled Shell calls included O_CREAT for both write and append. Fixed and restored calls omitted it for both operations.
Full workspace build and tests Local scripts/test-all.sh stopped after build-time disk exhaustion, before tests ran. Hosted all-targets/all-features builds completed on Linux ARM64 and macOS; full workspace tests remain in progress in the direct branch run.
Formatting and Clippy Local cargo fmt --all --check and git diff --check passed. Hosted workspace lint passed on the committed head with the repository-prescribed vendored-code allowances; no warnings appeared.
No-default-feature checks After task-owned cache cleanup, cargo build -p strands-box --no-default-features and cargo test -p strands-box --lib --no-default-features --no-run passed on the committed head. This compiled the actual library test harness; it did not execute tests. The build reported 66 warnings in unchanged containment code and none in the changed Box crate.
Deterministic integration Hosted Linux integration passed: 131 cases, zero failures/errors, nine declared macOS-only exclusions, full coverage gate, empty quarantine. Checkout and build logs pin the committed head; the verdict file itself says box_commit: unknown. macOS integration is still running.
Documentation site manifest Hosted site-manifest validation passed: 27 tests, zero failures or ignored tests.

Draft status: independent automated source, design, and documentation review found no remaining defect. Full validation remains incomplete. Direct CI for the committed branch is running both platform suites, including containment tests omitted from draft-triggered CI. Automated review awaits the manual-approval environment decision from strands-box-maintainers; it has not started.

AI-assisted implementation, testing, description, and author-side source review: Codex. Independent maintainer review remains pending.

Mixed real/effective user IDs remain unmeasured: the identity-changing setup was killed before any test ran. Local execution covers Linux ARM64. Hosted macOS all-feature builds and native adapter tests passed; its full workspace and deterministic tests are still running.

  • 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

Hosted Clippy used -D warnings with the current repository allowances: too_many_arguments, only_used_in_recursion, await_holding_refcell_ref, explicit_auto_deref, chunks_exact_to_as_chunks, dead_code, and non_snake_case. Owned-crate lint settings remain enforced.

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

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

## Problem

Root-launched Shell and Python operations can create host-root-owned entries.
The sandbox directory-write restriction does not cover these trusted operations.

## Solution

Refuse new host files, directories, symbolic links, and renames on Linux when
real or effective user ID is zero. Preserve policy-approved existing-file
access and Shell virtual storage. Admit the ownership floor after policy and
Reach, so each refusal records one effective denial and a failed outcome.
Remove the creation flag at the final host open to prevent recreation races.

Document the root restriction and non-root requirement for creation examples.
No CLI, configuration, policy-action, or public API names change. macOS behavior
is unchanged. Existing-file contents remain writable when policy permits them.

## Tests

On Linux ARM64, all 15 focused ownership tests passed with no ignored tests:
- root_shell_refuses_new_host_entries and root_python_refuses_new_host_entries
  cover refused creation and rename.
- root_shell_preserves_existing_reads_writes_and_append and its Python pair
  cover existing-file access.
- nonroot_shell_preserves_host_creation and its Python pair cover ordinary use.
- root_shell_preserves_virtual_storage covers writable in-memory paths.
- root_python_open_does_not_recreate_a_leaf_removed_after_reach covers a
  disappearing file. The Shell resolution counterpart pins the older defense.
- root_ownership_refusals_have_one_effective_denial (Shell and Python),
  root_ownership_admission_keeps_policy_first,
  root_ownership_rename_denial_records_failed_history, and both
  root_ownership_admission_does_not_reclassify_io_failures cases cover records.

Direct host-root execution passed seven behavior and six audit cases. Disabling
the original guard caused three failures. Bypassing admission and the Shell
create-flag removal caused five failures. Restored source passed all 15 tests.
Syscall traces showed O_CREAT for mutant write/append and its absence after
restoration. Formatting and diff whitespace checks passed.

Full-workspace testing failed during build with ENOSPC before tests ran.
Clippy, default-off builds, site-manifest, deterministic integration, and macOS
checks remain pending. Mixed-identity setup was killed before test execution.
The draft PR records these limits and requires hosted validation and review.
@kevin-orellana
kevin-orellana marked this pull request as ready for review November 5, 2026 15:19
@kevin-orellana
kevin-orellana enabled auto-merge (squash) November 5, 2026 15:30
| OsFunctionCall::AppendText(_)
| OsFunctionCall::AppendBytes(_) => true,
OsFunctionCall::Open(args) => args.mode.create(),
_ => false,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggestion (non-blocking): This security classifier is the only OsFunctionCall match in this file with a wildcard arm. Its siblings mutates_authority and monty_decision_subject list every variant.

Example: Every variant that reaches _ today is correct: the reads, Stat/Exists/Is*/Iterdir/Resolve/Absolute, Unlink/Rmdir, and the env/clock calls. Now suppose a later monty-types release (monty-types = "1.0.0" is a caret requirement) adds a variant that creates an entry, such as a symlink or touch call. The sibling matches then stop compiling. This one keeps compiling, and it puts the new variant in the "never creates" group. If the new call uses a new HeldPath method rather than open, nothing strips O_CREAT or refuses it. Root AGENTS.md says: "Make an invalid state impossible by construction."

Suggestion: Replace _ => false with an explicit list of the remaining variants, as mutates_authority does. A new variant then has to be classified before the code compiles.

let [decision] = recorded.as_slice() else {
panic!("exactly one effective record: {recorded:?}");
};
let (_, _, rule, verdict, reason) = decision.parts();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggestion (non-blocking): The new denial-record tests discard action and resource. They also check the reason with a substring match. A denial recorded against the wrong subject would therefore pass.

Example: Take the rename and symlink cases in this loop. If the ownership denial were recorded against from (existing) and not against the destination, or under the wrong action, all of these asserts would still pass. effects.rs:1352 has the same pattern, including Path.rename, where the subject should be dst. Nothing in the tuple is non-deterministic. The reason is the constant ROOT_CREATION_REFUSAL, and the resource comes from the fixture.

Suggestion: For each case, assert the whole tuple, for example assert_eq!(decision.parts(), (expected_action, expected_resource.as_str(), &EffectiveRule::Enforcement("ownership-floor"), EffectiveVerdict::Deny, Some(ROOT_CREATION_REFUSAL))). Expose the constant as pub(super) for the tests.

Comment thread docs/user/shell.md
| Any other | The path changed between the check and the operation, for example a directory was swapped for a symlink. The Shell refuses rather than act on a file policy didn't judge. Run the command again. | `strands-shell: <command>: filesystem identity changed during effect admission` |
| Any other | The operation would change a configured tool program or an enclosing path. Update the program between runs. | `<path>: is a configured tool command or its ancestor` |
| Any other | The operation would change an installed Box program or an enclosing path. [Update the installation between runs](config.md#installation-integrity). | `<path>: cannot change a trusted Box executable or its path` |
| Any other | Linux root refused creation or rename in the surrounding OS. Run Box as a non-root account. | `root launch cannot create or move host filesystem entries; run Box as a non-root account` |

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Important (non-blocking): Two existing pages that describe policy-independent refusals are now wrong. The decision log gets a new rule value that no page documents.

Example: Box runs as root on Linux, and policy permits fs:write. A user runs echo hi > ~/proj/new.txt. The refusal is recorded with strands.box.policy.cause = enforcement and strands.box.policy.rule = enforcement:ownership-floor. An operator who looks up this denial finds the following:

  • docs/user/security.md:51 ("What Box always enforces") says "One rule holds whatever a policy permits". The only rule value it names is enforcement:reach-floor.
  • docs/user/policy.md:98 says "Two refusals look like a denial, and no permit lifts them". Neither of the two it lists is this one.

.agents/references/doc-updates.md covers this case: "Changes a refusal, a denial, or a warning that an operator reads → … policy.md for … how a denial reads".

Suggestion: Add the Linux-root ownership refusal to the list in policy.md. In security.md, add a sentence that names enforcement:ownership-floor and its reason text, and link to it from the shell.md/monty.md sections you already added.

@github-actions

github-actions Bot commented Nov 5, 2026

Copy link
Copy Markdown

Assessment: Comment (reviewed head 5f51382)

I found no correctness defect. The two layers, the admission-time floor plus CallKernel/HeldPath removing the creation flag or refusing, cover every host-creating kernel and HeldPath path I traced. My comments cover documentation the change makes wrong, one wildcard match that fails open if a new variant is added later, and test assertions too loose to catch a denial recorded against the wrong subject.

Assumptions and rules checked that held
  • Every host creation goes through a gated call: Shell host creation goes only through open/create_dir/rename/symlink on CallKernel. Python goes only through HeldPath::{open, create_directory, rename_to, rename_to_new}. No other caller of HeldPath or CallKernel is affected.
  • Path not identified as host-backed: when binding() is None (identity unavailable), VfsKernel already returns stale-resolution for open, mkdir, rename and symlink, so skipping the strip there creates nothing.
  • Leaf disappears after the floor check: the open without O_CREAT fails for both Present (ENOENT) and Missing (NotFound) identities.
  • Existing directory with mkdir -p: the Shell checks with stat first and does not reach create_dir.
  • Shell in-memory storage: unaffected, because binding() is None and names_a_host_path is false.
  • Shell FsOperation/FsPairOperation wildcards: no variant that reaches them creates an entry.
  • "Deny-only floors stay beneath policy" (root AGENTS.md): holds. Policy is asked first, and root_ownership_admission_keeps_policy_first covers this.
  • is_root on uid or euid 0: refuses user-namespace root as well. This matches fix(containment): refuse Linux root sandbox directory writes #300's documented container behaviour. The macOS exclusion is documented consistently.
  • CI: unshare --map-root-user needs apparmor_restrict_unprivileged_userns=0. rust-test-lint.yml sets it, and the Ubuntu and macOS unit tests pass on this head.

Closing the open finding with a race-safe design that has two layers is a solid improvement.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants