Skip to content

fix: reject unresolved host paths and bound native filename scans - #152

Merged
kevin-orellana merged 3 commits into
mainfrom
feature/native-path-hardening
Nov 3, 2026
Merged

kevin-orellana merged 3 commits into
mainfrom
feature/native-path-hardening

Conversation

@kevin-orellana

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

Copy link
Copy Markdown
Collaborator

Human readable section

Problem: Suppose an application requests a file with a 4,096-character name inside an allowed directory. The operating system rejects the name as too long. Box previously treated that error as “the file does not exist” and approved the path for creation. The regression demonstrates incorrect path approval. It does not demonstrate a successful write to that invalid filename.

The macOS code that reads filesystem events has a separate problem. It scanned filename bytes before checking the supplied length. A malformed length could make that scan read beyond the available memory.

Solution: Box now permits a new filename only when the filesystem specifically reports NotFound. Other errors stop path approval. The macOS event code checks the filename length before it scans or copies the bytes.

Trade-off: File creation still works when the filename is valid and the file does not exist. Other filesystem errors stop approval. A separate process can still change a path between the check and the later operation.

Description

PathResolver::approve_host checks whether an existing path can be resolved or a new filename is absent. It now rejects resolution and metadata errors other than NotFound. This resolver also serves callers outside the macOS event code.

The macOS function new_path combines a directory path and a filename. It now rejects a filename that exceeds the remaining path capacity before inspecting its bytes.

The public API, configuration, event layout, and policy actions stay unchanged.

The tests reproduce invalid-name approval and an oversized filename scan before the fixes. The corrected event code also passes AddressSanitizer and UndefinedBehaviorSanitizer, tools that detect memory errors and undefined behavior. The memory-access test uses synthetic event data. It does not establish that a workload can cause Apple to supply a malformed event.

Prepared with Codex assistance at Kevin's request. Codex reviewed the diff. Independent human review remains pending.

Related Issues

None. This is a focused path-validation change.

Type of Change

Bug fix

Testing

Conflict repair validation

Updated to bb4a11a by merging current main. The bridge keeps both filename-bound checks and permission-event checks.

The all-feature policy suite passed on Linux, with seven existing ignored tests. Native macOS bridge checks passed normally and with AddressSanitizer and UndefinedBehaviorSanitizer. The native test used exactly the candidate's two C source files, verified by SHA-256. Formatting and policy lint passed with CONTRIBUTING's documented workspace allowances. Bare strict lint reports existing vendored-shell warnings.

Independent review found no actionable issues. Hosted checks are queued for this revision. The earlier results below retain their original revision and scope.

Local platform: macOS 27.0.1 ARM64, Rust 1.98.1. Tested source: f0ed0d7.

Check Result
cargo test -p strands-box-policy with default features and --all-features Passed. The all-feature run reported 406 passed and seven existing ignored tests.
Controlled bridge, through test-native-filesystem --test bridge Passed. Tests malformed names, truncation, root joins, capacity boundaries, and rejection before reading oversized tokens.
Bridge with -fsanitize=address,undefined Passed.
bash test-integ/run.sh against the local build GREEN: 121 passed, eight Linux-only cases skipped, zero failures, full manifest coverage.
Policy builds, clippy, and format checks with default and all features Passed. No policy-owned warnings.
RUSTDOCFLAGS="-D warnings" cargo doc -p strands-box-policy --all-features --no-deps Passed.
cargo fmt --all and just clippy Passed. Clippy used the vendored-code allowances required by CONTRIBUTING.
Full just check Formatting and clippy passed. The workspace run stopped at three egress request-write tests with macOS “No buffer space available.”
scripts/test-all.sh box-examples Build passed; no scripted examples exist, so no example execution is claimed.

The invalid-name regression failed against the original resolver. The guard-page bridge test terminated before the scan-order fix and passed afterward. The symbolic-link loop test is a compatibility control. Signed native kernel tests and Linux execution did not run.

  • 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

The full workspace run failed in unchanged egress tests while clients wrote large request bodies. All 15 tests passed when the same test binary ran serially. This does not make the original workspace run green. The literal strict clippy command above was not used; CONTRIBUTING prescribes just clippy with vendored-code allowances. GitHub checks and automated review are queued.

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

Kevin confirmed the author ownership checklist item. No repository documentation change is required for this correction behind existing interfaces.


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

## Problem

The host resolver treats every resolution error as a possible new file. The native bridge scans a new filename before checking its available buffer capacity.

## Solution

Allow the missing-file fallback only for NotFound. Refuse other errors from resolution and the final metadata check. Check the joined filename capacity before reading its bytes. Public interfaces and the event layout stay unchanged.

## Tests

The invalid-name regression failed on the original resolver. The guard-page bridge test failed before the scan-order fix. Both pass after the changes. The policy suite passed with default and all features. The controlled bridge passed with AddressSanitizer and UndefinedBehaviorSanitizer. The deterministic suite passed 121 cases and skipped eight Linux-only cases. Formatting, workspace clippy with the repository allowances, policy builds, and strict policy documentation passed. The full workspace tests are still running. Signed native kernel tests and Linux execution did not run.
@kevin-orellana
kevin-orellana marked this pull request as ready for review October 22, 2026 23:18
Comment thread crates/policy/src/path.rs Resolved
@github-actions

Copy link
Copy Markdown

Assessment: Approve

Focused, correct hardening of two path-validation seams with matching regression tests. The NotFound-only treatment in approve_host closes the "resolution error reads as a new file" gap, and reordering the capacity bound ahead of the memchr scans in new_path prevents an out-of-bounds read on an untrusted leaf.length. I verified the C bounds arithmetic has no unsigned underflow and no off-by-one at the capacity boundary.

Review Categories
  • Correctness/Security: Both fixes are sound; the short-circuit ordering and the error-kind narrowing behave as described.
  • Testing: Strong — the Rust cases guard their own assumption (assert_ne!(NotFound)), and the C suite uses a PROT_NONE guard page to prove no bytes are scanned past the bound.
  • Maintainability (non-blocking): Two suggestions inline to document the load-bearing ordering (C) and the NotFound-only invariant (Rust) so neither regresses during a later cleanup.
  • API: Behavior-only tightening behind existing interfaces; no frozen surface moves, so no API review needed.

Nicely scoped change with tests that reproduce the defects before the fix.

@kevin-orellana

Copy link
Copy Markdown
Collaborator Author

Investigated the failed macOS Rust job on unchanged head f0ed0d7f8f293a10abbfccf87a478c56c883c280.

The only failed test was the_first_catalog_of_256_pages_costs_at_most_three_times_one_of_128_pages. Its single wall-clock measurements were 10.663 seconds for 256 pages and 3.092 seconds for 128 pages: ratio 3.45 against the unchanged limit of 3. This PR does not modify that timing test.

The failed-job retry passed on the same head: 3.991 seconds and 1.723 seconds, ratio 2.32. The full workflow and CI Gate now pass: https://github.com/strands-agents/box/actions/runs/39870890815/attempts/2 .

No code, timing threshold, or test selection changed. This clears the failed check and establishes an intermittent timing failure; it does not establish a permanent fix for runner-sensitive benchmark timing. Automated review comments and human approval remain separate.

@ferdingler

Copy link
Copy Markdown
Collaborator

Box can classify a filesystem resolution error as a new file

I don't quite understand the problem

@kevin-orellana

Copy link
Copy Markdown
Collaborator Author

@ferdingler The description was too abstract. Here is the concrete case:

Suppose an application requests a file with a 4,096-character name inside an allowed directory. The operating system rejects the name as too long. Box previously treated that error as “the file does not exist” and approved the path for creation.

The fix distinguishes “file not found” from “the filesystem could not check this path.” Box permits a new filename only when the filesystem specifically reports NotFound. Other errors stop approval.

The regression demonstrates incorrect path approval. It does not demonstrate a successful write to that invalid filename.

I updated the PR description with this example and explained the separate macOS filename-length check.

@github-actions

Copy link
Copy Markdown

Re-reviewed at bb4a11a (previously reviewed f0ed0d7).

The "reconcile native bridge regressions with main" commit is a rebase/merge that pulls in main (e.g. bridge_checks.c now also runs the upstream permission_event() from main()). The net PR diff against main for the three substantive changes — the new_path capacity-before-scan reordering, the NotFound-only narrowing in approve_host, and the path_tokens test — is unchanged from what I reviewed. No new issues introduced by the reconcile.

My two earlier suggestions are still open and still apply (both non-blocking): a comment in new_path noting the length bound must precede the memchr scans, and a approve_host doc-comment update recording the NotFound-only-means-absent invariant.

Assessment: Approve — unchanged.

@kevin-orellana
kevin-orellana enabled auto-merge (squash) October 27, 2026 06:10
@github-actions

Copy link
Copy Markdown

Re-reviewed at 78ae3ae (previously bb4a11a).

The "docs: clarify native path lookup and filename scan guards" commit is docs-only and addresses both of my earlier non-blocking suggestions:

  • new_path now carries a comment stating the capacity bound precedes every scan of the untrusted leaf.
  • approve_host's summary now records that only NotFound means absence and all other lookup errors refuse.

The security-relevant logic in new_path, approve_host, and the path_tokens test is unchanged and still correct. No open items remain from my review.

Assessment: Approve.

@kevin-orellana
kevin-orellana merged commit 1e5bf9c into main Nov 3, 2026
20 checks passed
@kevin-orellana
kevin-orellana deleted the feature/native-path-hardening branch November 3, 2026 17:56
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