Skip to content

Fixed a bug where nested sandboxes would fail if we tried to mount a file with a flag locked by the outer sandbox but not the inner - #208

Merged
evanspearman merged 1 commit into
mainfrom
evan/mount-flag-fix
May 13, 2026
Merged

evanspearman merged 1 commit into
mainfrom
evan/mount-flag-fix

Conversation

@evanspearman

@evanspearman evanspearman commented May 12, 2026 •

Copy link
Copy Markdown
Member

If you try to do something like:

minimal shell
cargo run -- shell

The second entry in the sandbox will fail with an error like:

invocation 0 failed: mount(Some(""), "host/Users/evan/Development/minimal/CLAUDE.md", None, MsFlags(MS_REMOUNT | MS_BIND), None) => EPERM

This is because Linux requires that any locked mount flags are preserved in the new sandbox. We weren't preserving them so it fails. This PR changes the way we're mounting them to always preserve locked flags.

Note that Runctl::Mountfallback is not a good solution here because it just falls back to only specifying the flags that the file currently has and not the flags that we have specified.

Summary by CodeRabbit

  • Bug Fixes

    • Improved sandbox execution error messages and added explicit mount-specific error reporting.
  • Chores

    • Strengthened sandbox mount isolation by honoring kernel locked mount flags and refining bind-mount semantics (recursive vs file mounts, optional read-only).
    • Added a Linux-only dependency to support mount flag handling.
  • Tests

    • Added unit tests validating mount-flag mapping and failure behavior.

Review Change Stack

@coderabbitai

coderabbitai Bot commented May 12, 2026 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

Adds the nix crate and a MountError variant, implements locked_mount_flags(path) using nix::statfs, centralizes bind-mount logic in Sandbox::bind_mount, refactors /state, /run, working-directory and filesystem mappings to use it, and maps the new error into mctx.

Changes

Sandbox Mount Flag Locking

Layer / File(s) Summary
Dependencies and error variant
Cargo.toml, crates/sandbox2/Cargo.toml, crates/sandbox2/src/error.rs, crates/mctx/src/error.rs
Adds workspace nix = "0.29" (fs feature) and nix.workspace = true for sandbox2 Linux target. Introduces ExecutionError::MountError { msg, path }, updates Display/Error, and maps the new variant to Error::Other in mctx conversion.
locked_mount_flags helper
crates/sandbox2/src/lib.rs
Adds locked_mount_flags(path) using nix::statfs to translate ST_* kernel flags to hakoniwa::MountOptions, with tests and fallback to empty options on statfs failure.
Sandbox::bind_mount and /state /run mounting
crates/sandbox2/src/lib.rs
Introduces private Sandbox::bind_mount that applies BIND
Working-directory and filesystem mapping mounts
crates/sandbox2/src/lib.rs
Refactors WdSetup::Isolated and WdSetup::BoundDir to use bind_mount for /build and host cwd; directories use recursive bind, files non-recursive; all mappings include NOSUID and locked_mount_flags.

Sequence Diagram(s)

sequenceDiagram
  participant Sandbox
  participant locked_mount_flags
  participant nix_statfs
  participant MountOptions
  Sandbox->>locked_mount_flags: locked_mount_flags(path)
  locked_mount_flags->>nix_statfs: statfs(path)
  alt statfs succeeds
    nix_statfs-->>locked_mount_flags: stat result (ST_*)
    locked_mount_flags->>MountOptions: map ST_* -> MountOptions
    MountOptions-->>locked_mount_flags: options
  else statfs fails
    nix_statfs-->>locked_mount_flags: error
    locked_mount_flags->>MountOptions: return empty options
    MountOptions-->>locked_mount_flags: options
  end
  locked_mount_flags-->>Sandbox: MountOptions
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

  • gominimal/minimal#188: Modifies /run mount setup; this PR routes /run through the new bind-mount + locked_mount_flags helper.

Suggested reviewers

  • mitodrummer
  • msample

Poem

🐰 In tunnels where the containers play,
I map the mounts and lock the way.
With nix I check each kernel flag,
Bind mounts snug—no silent snag.
Hoppity hop, the sandbox stays okay.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly and accurately summarizes the main change: fixing a bug related to nested sandboxes and mount flags locked by outer sandboxes.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@crates/sandbox2/src/lib.rs`:
- Line 392: The code currently calls build_path.to_str().unwrap() (and the other
to_str().unwrap() at the later occurrence), which can panic on non-UTF-8 paths;
change both sites to handle non-UTF-8 by returning an ExecutionError::MountError
(the same style used by bind_mount()) instead of unwrapping. Locate the
occurrences referencing build_path (and the second path variable) in
crates/sandbox2/src/lib.rs and replace the unwrap with a
to_str().ok_or_else(...) or equivalent that returns
Err(ExecutionError::MountError{ ... }) with the offending path and an
explanatory source message so the function returns an error rather than
panicking. Ensure the returned error shape and messaging match bind_mount's
MountError usage.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 468d382d-70a5-45b6-b0ee-dc03f25a63cf

📥 Commits

Reviewing files that changed from the base of the PR and between 986c633 and 379fee3.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (5)
  • Cargo.toml
  • crates/mctx/src/error.rs
  • crates/sandbox2/Cargo.toml
  • crates/sandbox2/src/error.rs
  • crates/sandbox2/src/lib.rs

Comment thread crates/sandbox2/src/lib.rs Resolved
…file with a flag locked by the outer sandbox but not the inner
@evanspearman
evanspearman force-pushed the evan/mount-flag-fix branch from 379fee3 to 0cb074f Compare May 13, 2026 16:49
@evanspearman evanspearman changed the title WIP: Fixed a bug where nested sandboxes would fail if we tried to mount a file with a flag locked by the outer sandbox but not the inner Fixed a bug where nested sandboxes would fail if we tried to mount a file with a flag locked by the outer sandbox but not the inner May 13, 2026
@evanspearman
evanspearman merged commit ec06be0 into main May 13, 2026
8 checks passed
@evanspearman
evanspearman deleted the evan/mount-flag-fix branch May 13, 2026 17:36
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.

2 participants