Skip to content

fix(tools): confine shell reads to workspace + session temp (#447) - #456

Merged
mohitranka merged 1 commit into
mainfrom
fix/447-exec-command-sandbox-confinement
Aug 27, 2026
Merged

mohitranka merged 1 commit into
mainfrom
fix/447-exec-command-sandbox-confinement

Conversation

@mohitranka

Copy link
Copy Markdown
Contributor

Summary

Closes #447 — exec_command (and the bash tool's run_shell_command path) bypassed the workspace sandbox boundary on the read side.

read_file/write_file confine paths to the workspace + session temp via ToolContext::resolve_path, with no approval path. Shell commands were already confined for writes by the OS sandbox (sandbox-exec on macOS, bwrap on Linux) applied at spawn, but the sandbox's read side was left open: the Seatbelt profile contained a blanket (allow file-read*), and bubblewrap exposed the host filesystem read-only (--ro-bind / /). A shell command could therefore cat ~/.ssh/... and ~/.aws/... that read_file refused — the exact escape in #447.

What changed

  • macOS (Seatbelt): removed (allow file-read*). Reads are now allowed only for:
    • the workspace root and granted session temp dir (the same boundary the file tools enforce, canonicalised);
    • OS-owned paths a process needs merely to start: /System, /usr, /bin, /sbin, /Library/Apple, /Library/Keychains (system roots only — user keychains under $HOME stay denied), /Library/Preferences, /private/etc + /etc, /opt/homebrew (root-owned ARM toolchain), timezone/DarwinDirectory user-lookup data, standard /dev nodes, and / (getcwd);
    • plus file-map-executable on the loader paths and file-read-metadata traversal for /var.
    • Everything user-writable ($HOME, per-user temp under /private/var/folders, volumes, /Network) is denied by the profile default. Denied reads surface as Operation not permitted → existing ToolError::SandboxDenied.
  • Linux (bwrap): --ro-bind / / stays, but /home and /root are now masked with a private tmpfs alongside /run and /tmp, so ~/.ssh/~/.aws do not exist inside the sandbox (reported as ENOENT → SandboxDenied). Masks are emitted before the workspace bind, so workspaces living under /home or /root are re-exposed like workspaces under /tmp.
  • Denials keep the existing typed SandboxDenied semantics and governance escalation (user approves → unconfined replay); no new failure mode introduced.
  • README + SECURITY.md updated: reads are no longer "broad"; the ~/.docker-under-$HOME socket gap is now closed, residual socket gap limited to unmasked trees (e.g. /var, /opt) — still tracked in Harden sandbox with Landlock RESOLVE_UNIX once kernels ship ABI 9 #392.

Tests

  • sandbox_enforcement.rs: reads_outside_the_workspace_are_refused (wrapper level, secret file outside the workspace must be unreadable), system_paths_needed_to_start_a_process_stay_readable (positive control), exec_command_cannot_read_outside_the_workspace (tool level, escalates SandboxDenied, secret must not leak into tool output), exec_command_reads_workspace_and_session_temp (positive control), bash_tool_cannot_read_outside_the_workspace (second spawn path). Existing write-denial tests unchanged.
  • permission_contract.rs: the "read outside the workspace" contract row flipped Allowed → Denied; network row wording updated.
  • sandbox.rs unit tests: blanket (allow file-read*) absence + workspace/system-path read allowlist asserted; bwrap test now asserts /home and /root masks precede the workspace bind.
  • Fixtures in forge-core/forge-tui mirroring the old denial text updated.

Validation (run on macOS; kernel-level tests are sandbox-gated and skip where a second sandbox cannot be entered — they run on CI's Linux+bubblewrap job)

  • cargo fmt --all -- --check — clean
  • cargo clippy --workspace --all-targets --locked -- -D warnings — clean
  • cargo test --workspace --all-targets --locked --no-fail-fast — all green (incl. forge-tools 327 lib + 37 enforcement + 15 contract, forge-core 1040, forge-tui)
  • cargo build --release --locked --package forge-cli + ./target/release/forge --version — ok

Platform limitations of the confinement

  • macOS Seatbelt is path-based and airtight for reads (EPERM), but the system read allowlist (e.g. /usr, /opt/homebrew) is a curated list; a system path forge does not know about fails closed and escalates to the user. User keychains and $HOME are denied.
  • Linux read denial is by masking, not by path: /home and /root are hidden, but other user-writable trees not masked (e.g. /mnt, /var/backups) remain readable read-only inside the sandbox, and a read-only mount does not stop connect() to a socket under an unmasked tree (residual gap Harden sandbox with Landlock RESOLVE_UNIX once kernels ship ABI 9 #392).
  • Writes were already confined and are unchanged. unconfined_shell replays after explicit user approval remain permitted by design.

exec_command and the bash tool ran inside the OS sandbox for writes, but the sandbox's read side was left open: the Seatbelt profile allowed file-read* everywhere and bubblewrap exposed the host filesystem read-only. A shell command could cat ~/.ssh and ~/.aws that read_file/write_file refused, which is the #447 escape.

Close the read side with the same boundary the file tools enforce. On macOS the profile now allows reads only for the workspace, the session temp dir, and the OS-owned paths a process needs to start; on Linux the home trees (/home, /root) are masked with a tmpfs alongside /run and /tmp. Denials keep the existing SandboxDenied escalation path.

Adds read-denial regression tests for both shell spawn paths (exec_command and run_shell_command) plus a workspace+session-temp positive control; updates the permission contract, README, and SECURITY.md.
@mohitranka
mohitranka force-pushed the fix/447-exec-command-sandbox-confinement branch from 706f085 to fee31bc Compare August 27, 2026 21:53
@mohitranka
mohitranka merged commit 21d1454 into main Aug 27, 2026
7 checks passed
@mohitranka
mohitranka deleted the fix/447-exec-command-sandbox-confinement branch August 27, 2026 22:11
mohitranka added a commit that referenced this pull request Sep 11, 2026
…confinement

fix(tools): confine shell reads to workspace + session temp (#447)
mohitranka added a commit that referenced this pull request Sep 11, 2026
…confinement

fix(tools): confine shell reads to workspace + session temp (#447)
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.

exec_command bypasses workspace sandbox confinement (read_file/write_file block it, bash does not)

1 participant