Skip to content

fix(box): skip hard-link scans when Linux mount and filename checks pass - #381

Merged
kevin-orellana merged 5 commits into
mainfrom
feature/linked-command-own-folder
Nov 9, 2026
Merged

kevin-orellana merged 5 commits into
mainfrom
feature/linked-command-own-folder

Conversation

@opieter-aws

@opieter-aws opieter-aws commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Strands Box scans writable directories before starting a workload to ensure they contain no hard link to a configured tool program. A hard link is another filename for the same file: writing through either name changes the program. This check applies when the user running Box can write the program and it has multiple hard links.

The program is selected by the first item in the command array under [tool.<name>] in box.toml. Even when all its hard-link names are in its own protected directory, Box scans the workload's writable directories. An unreadable subdirectory or a directory that changes during inspection can therefore prevent workload startup. Strands Shell and Monty, Box's shell and Python interpreters, repeat the check before requested directory changes and reject the request if inspection is incomplete.

Solution

On Linux, skip the recursive scan for a program when Box can establish that all its hard-link names are in its own protected directory and the filesystem's mount layout exposes no additional path to it in the directory being checked. A mount can expose the same files at another path without increasing their hard-link counts, so counting filenames alone is insufficient.

For example, a tool installation can keep bun and bunx as hard links in a protected tools directory. On a supported mount layout, an unrelated unreadable subdirectory in the writable workspace no longer prevents startup. This requires no package list, executable copies, or new box.toml setting.

The optional inspection applies only to directory targets. Files, missing paths, symbolic links, and metadata lookup errors retain the existing direct file-identity checks.

Before each skipped scan, Box requires all of the following:

  • The program, its containing directory, and the directory being made writable or changed are on the same ext4, XFS, or tmpfs mount, which exposes the filesystem's root (/).
  • No other mount shares that device or mount point, or occurs at or below either checked directory. Open files and resolved paths must identify the same objects.
  • The count of matching directory entries equals the open program file's current hard-link count. Box compares the program's link count and directory state before and after inspection and requires them to match.
  • The mount table is identical before and after counting.

Each optional inspection has a 30-second budget, checked between filesystem calls. It cannot interrupt a blocked call. An unmet condition, error, or timeout returns to the existing recursive scan. macOS retains that scan for directory targets.

The trade-off is that only these Linux layouts can avoid scanning. Where scanning remains necessary, an unreadable directory can still prevent workload startup or cause Box to reject a requested directory change.

The protection boundary remains workload writes to configured programs; updates by unrelated host programs remain outside it. Write grants cannot overwrite the program, change its symbolic-link routes, or replace an enclosing directory. Before each launch, Box verifies the program's identity, link count, owner, group, and mode. Strands Shell and Monty apply the same protections when they perform file operations in Box's trusted process. Configuration keys and error messages do not change. The implementation guidance in crates/box/AGENTS.md and the user and design documentation now describe the guarded shortcut.

Tests

Local follow-up checks cover commit a9bb66e:

  • Box builds with and without default features, plus the containment all-features build, passed. The no-default build retains 68 existing containment dependency warnings.
  • cargo test -p strands-box --all-features --locked --lib run::commands -- --show-output --test-threads=1: 26 test bodies completed, one explicitly skipped, no failures. The development host has an unsupported namespace-filesystem mount record, so this run does not prove positive shortcut use or a forced-false mutation failure.
  • Clippy for all Box targets and features passed with the seven existing justfile lint allowances and no diagnostics in changed code. Formatting passed.
  • cargo test -p strands-box --test site_manifest --locked --all-features: 27 passed.

GitHub checks for a9bb66e passed the full Linux workspace suite, including all 828 Box library tests. The captured output confirms that the positive shortcut test executed without any of its explicit skip messages. The full macOS workspace suite and its separate native-startup checks also passed. Linux and macOS deterministic containment tests, lint, packaging, the site-manifest check, and the final CI gate passed. The workflow skipped its conditional Verdict job as expected. GitHub tested merge 0f9ba5f, which combines this head with main at fbe415c.

macOS qualification limit: this workflow did not run the full sandbox-enforcement suite with signed components. Its informational check of saved evidence from an earlier run reported that evidence as stale. That step permits failure, so the overall workflow passed. The current macOS results cover the workspace suite, one native bridge test, and three startup tests without the native-filesystem feature; they do not establish fresh full signed-native qualification.

Automated review of a9bb66e found all three earlier findings addressed and no new code defect. The reviewer also reports that forcing the shortcut to return false makes the positive test fail on a supported Linux/ext4 runner. That mutation result is reviewer-reported; the direct GitHub test log independently establishes the normal positive result. Formal maintainer approval remains separate.

The command tests now separate shortcut use on eligible Linux hosts, refusal after an incomplete directory scan, and direct identity checks for files and missing paths. The shortcut test independently checks whether the host supports its fixture, then requires the shortcut to run. Unsupported hosts report an explicit skip; a skip is not evidence that the shortcut ran.

Tests with simulated mount records cover additional mounts, unsupported filesystems, mismatched identities, changing mount tables, and inspection deadlines. The mount-path concern was identified by source review. These tests do not demonstrate a sandbox escape.

Earlier runtime checks of guard commit 1d07dfd matched all 25 expected Linux outcomes and six expected macOS outcomes, including Bun and Rustup/Cargo. The Linux runs used a separate repair for an existing filesystem-layout parser failure on the development host; that repair is not included in this PR. The macOS runs required no parser repair. These results cover the guard commit, not the subsequent directory-only optimization.

Earlier local broad-suite attempts of guard commit 1d07dfd were not clean passes. Linux tests encountered the existing mount-table parser failure. The native macOS run stopped after 995 passes, nine failures, and four ignored tests; seven failures involved existing profile fixtures, and two were startup-marker timeouts without a confirmed cause. Hosted results are recorded separately.

Type of Change

Bug fix

Related Issues

Follows #347.

  • I ran the relevant suites (cargo test -p strands-box --all-features --lib)
  • If I touched Rust, I ran cargo fmt --all and cargo clippy for the changed packages with --all-features

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.

EC2 Default User and others added 2 commits November 9, 2026 16:27
## Problem

Counting hard-link names beside a configured tool does not cover additional
paths created by filesystem mounts. The optional count also had no time budget.
Skipping writable-directory scans on that evidence can miss a protected file.

## Solution

Check the current write or mutation target against the retained program and
parent-directory descriptors. Permit the shortcut only on one complete ext4,
XFS, or tmpfs mount, without another mount of that device or nested mounts in
the counted or writable paths. Require the mount table to remain unchanged
across the descriptor-based name count. Bound all optional inspection with the
existing 30-second cooperative budget. Errors, uncertainty, other filesystem
layouts, and macOS retain the existing recursive scan. Preserve launch checks
and update the affected documentation. No external interface changes.

## Tests

The 25 command unit tests pass. The mount module has 25 passes; its existing
namespace fixture is blocked by this host's unchanged nsfs parser defect.
The 12 new Linux unit tests detect 13 focused mutations in a standalone harness.
The supported-filesystem, duplicate-mount, nested-mount, descriptor-identity,
stable-table, inspection-error, and deadline tests cover the added guards.

Builds with all features and without default features pass. Clippy passes with
the repository's vendored-code allowances, formatting passes, and all 27 site
manifest tests pass. Cargo.lock is unchanged.

Twenty-five actual Linux compatibility cases pass for echo, Bun, and Rustup
running Cargo, including unsafe-link refusals and hosted directory mutations.
Those runs use an isolated copy with the existing nsfs parser prerequisite;
that unrelated repair is not included here. Six macOS Bun/Cargo runtime cases
confirm successful ordinary execution and retained unreadable/unsafe-link
refusals. No new mounted-path escape reproduction was performed.

Full local suite qualification is incomplete: Linux repeats the nsfs startup
blocker; the macOS library reports 797 passes and seven boundary-test failures
that refuse /private/tmp. Hosted CI must qualify the final PR revision.
gautamsirdeshmukh added a commit that referenced this pull request Nov 9, 2026
## Problem

Late Core messages overwrite the native workload screen. Boxy continues to consume feature/allow-multilink-commands while the command alias scan on main awaits #381.

## Solution

Apply merged commit 1e94f57 without changes to this feature branch. The workload keeps its terminal streams. Core retains later diagnostics and prints them after the workload exits.

## Tests

- Builds with all features and no default features, Clippy, and formatting pass.
- The workspace suite reports 4,766 passed, 2 failed, and 17 ignored. Both failures reproduce on unchanged 53a6c04: the same-folder hard-link refusal test and the tool grammar exit-status test.
- Deterministic containment reports 129 passed and 12 Linux-only skips.
- The diagnostics unit test and all three terminal integration tests pass. Each fails under its relevant deliberate mutation and passes after restoration.
- The local Linux cross-check requires the absent x86_64-linux-gnu-gcc compiler. The merged patch passed Linux CI on its original base.
Comment thread crates/box/src/run/commands.rs Resolved
Comment thread docs/design/decisions.md Resolved
Comment thread crates/box/src/run/commands.rs Resolved
@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown

Assessment: Request Changes

Reviewed head 4f92080. Since 1d07dfd, the only changes are a merge from main and the syscall.rs count fix. That fix corrects a stale assertion on main: the table has 113 entries, and the test passes. The skip logic itself looks correct. The open items are maintainer approval for the changed safety premise, and a test that actually pins the skip.

Assumptions and rules checked that held
  • If the folder count equals the current nlink, no name exists outside the folder. Extra names, and names that move during the count, are caught (directory metadata is compared before and after, and nlink before and after).
  • An alias name inside the folder cannot be mutated through the broker. enclosed_by → names matches by disk_identity, so it returns CommandOrAncestor before the skip runs.
  • A file write grant on a sibling hard link is still refused by command.names (identity match).
  • A bind mount of the command's folder or device elsewhere disables the skip (no other mount may share the device or the mount point).
  • A root / write grant (fix(linux): allow root directory write grants #348) never skips. Every mount point lies under /, and enclosed_by refuses the grant anyway.
  • Overlay, btrfs, and WSL2-style layouts, symlinked command paths, and targets on another filesystem all fall back to the scan. This is conservative: the behaviour is unchanged from main.
  • macOS always keeps the scan.

The fallback-on-any-doubt design is careful, and the mount-table parsing has good unit coverage.

Inspect mounts only for directory targets and retain direct identity checks
for files and missing paths. Require shortcut use on independently qualified
Linux fixtures, and test incomplete scans separately from file checks.

Align the implementation guidance and user documentation with the guarded
Linux shortcut and the unchanged macOS scan fallback.
@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown

Assessment: Comment

Reviewed head a9bb66e. a9bb66e resolves all three earlier findings, and I found nothing new in the directory-only gate or in the test rework. The remaining merge gate is not a code change: a maintainer must explicitly approve the changed safety premise, as root AGENTS.md requires.

Checks that held on a9bb66e
  • The shortcut test now pins the skip: when I forced every_name_beside to false, the test failed on this Linux/ext4 runner as non-root.
  • The fixture check that decides whether the test runs (separate mountinfo parse plus fstatfs) is independent of the code under test. Root and unsupported hosts get an explicit skip.
  • The directory gate: files and missing paths go back to a single lstat identity check; symlinks and metadata errors keep the existing check, as on main; only directories attempt the shortcut.
  • The full --lib suite gives 760 passed (757 plus the 3 new tests) with the same 64 environment failures as before, so nothing regressed.
  • crates/box/AGENTS.md, decisions.md, config.md, and the PR description now give one consistent account of the guarded shortcut.
  • The checks I listed as holding in my 4f92080 review are unaffected, because a9bb66e changes only the gate and the tests.

@kevin-orellana kevin-orellana changed the title fix(box): skip the alias scan when every name of a command shares its folder fix(box): skip hard-link scans when Linux mount and filename checks pass Nov 9, 2026
@kevin-orellana

Copy link
Copy Markdown
Collaborator

Counting hard links alone can miss another writable path to a tool program. Linux can display the same folder at both /tools and /workspace/tools through a filesystem mount. That creates another route to the same files without adding hard links. Finding every hard link in /tools therefore does not prove that the writable workspace cannot reach the program.

I pushed the mount checks and a follow-up directly to this PR. Box now checks the mount layout before skipping a directory scan. If it cannot verify the layout, it uses the existing scan. Changes to a single file keep their direct checks without inspecting mounts.

The trade-off is that fewer Linux filesystem setups can skip scanning. macOS keeps its existing scans. An unreadable subdirectory can therefore still prevent startup where scanning remains necessary.

The full Linux and macOS workspace checks passed. The Linux log confirms the shortcut test ran without skipping; all 828 Box library tests passed. Separate tests cover incomplete-scan refusals and protected files. The automated reviewer also reports that disabling the shortcut makes the positive test fail. The complete macOS sandbox-enforcement suite was not rerun; the saved evidence for that suite is stale.

Runtime checks of the first guard commit matched 25 expected Linux outcomes and six macOS outcomes, including Bun and Cargo. The Linux runs needed a separate fix for how Box reads the host’s mount information; that fix is not included here.

I confirmed the gap by reading the code. Tests with simulated mount information check the guard’s decisions; they do not demonstrate a sandbox escape.

@kevin-orellana kevin-orellana left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Approved. I approve the guarded Linux approach: Box may skip a writable-directory scan only after verifying both the program's hard-link names and the filesystem mount layout. If those checks cannot establish the required conditions, Box keeps the existing scan. macOS keeps its existing directory scans.

This approval covers a9bb66e, including the regression-test and documentation fixes. The Linux and macOS workspace checks passed. As documented above, the full signed macOS sandbox-enforcement suite was not rerun; this approval does not claim fresh qualification from that suite.

@kevin-orellana
kevin-orellana merged commit f4180a0 into main Nov 9, 2026
27 checks passed
@kevin-orellana
kevin-orellana deleted the feature/linked-command-own-folder branch November 9, 2026 20:28
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