claude-code icon indicating copy to clipboard operation
claude-code copied to clipboard

bwrap sandbox for the Bash tool binds the entire /home directory, causing Permission denied on writes outside the invoking users own home (e.g. /home/.mcp.json)

Open bottenbenny opened this issue 2 months ago • 6 comments

Summary

When the Bash tool's sandbox (bwrap) is active, its mount setup binds the entire /home directory into the sandbox rather than scoping it to the invoking user's own home directory. Since a bind mount does not change underlying ownership, and /home itself is typically root:root with mode 755 on Linux, any code path that resolves a path relative to a truncated/bare /home (instead of /home/<user>) fails with a permission error when attempting to write there — observed concretely as:

bwrap: Can't create file at /home/.mcp.json: Permission denied

Environment

  • Claude Code CLI v2.1.224, native install, Linux (Debian/Ubuntu-based)
  • Triggered with CLAUDE_CODE_SUBPROCESS_ENV_SCRUB=1 and --safe-mode
  • $HOME correctly set to /home/<user> for the whole invocation (verified via /proc/<pid>/environ)

Reproduction

  1. Run claude with --safe-mode, CLAUDE_CODE_SUBPROCESS_ENV_SCRUB=1, and --tools Bash --allowedTools Bash.
  2. Prompt it to run any shell command via the Bash tool.
  3. Trace the resulting process tree (strace -f -e trace=execve). Observed:
    execve("/bin/bash", ["bash", "-c", "bwrap --new-session --die-with-parent ..."])
    execve("/usr/bin/bwrap", ["bwrap", "--new-session", "--die-with-parent",
      "--ro-bind", "/", "/",
      "--bind", "/tmp/claude", "/tmp/claude",
      "--bind", "/home/<user>/.npm/_logs", "/home/<user>/.npm/_logs",
      "--bind", "/home", "/home",
      "--bind", "/root", "/root",
      "--bind", "/tmp", "/tmp",
      "--bind", "/var", "/var",
      "--bind", "/opt", "/opt",
      "--bind", "/run", "/run",
      "--bind", "/mnt", ...])
    
    Note --bind /home /home — the whole directory, not --bind /home/<user> /home/<user>.

Impact

Any internal path resolution inside the sandbox that lands on bare /home/... instead of the actual $HOME (e.g. /home/<user>/...) — whether from an env var mishap upstream or internal logic — fails with a permission error that's non-obvious to debug from the caller's side, since $HOME itself can be completely correct in the invoking environment.

Suggested fix

Scope the sandbox's home-related bind mount to the actual $HOME (e.g. --bind $HOME $HOME) rather than binding the parent /home directory wholesale.

bottenbenny avatar Sep 13 '26 11:09 bottenbenny

Follow-up clarification

The reproduction and the observed --bind /home /home argument are confirmed. Additional static inspection of the bundled Claude Code v2.1.224 binary suggests that the broad bind is an enabling condition, but is not necessarily the path-selection root cause.

Claude's internal Linux sandbox mask builder appears to:

  • obtain the project root and real home independently;
  • register .mcp.json at ancestor paths; and
  • when the project and home trees are separate, walk the home ancestry as a fallback.

With a project under /tmp/... and HOME=/home/<user>, this can generate /home/.mcp.json as a sandbox mask target while HOME remains correct. Because /home is typically root:root and mode 755, Bubblewrap then fails when it tries to materialize that target.

Therefore, scoping the bind to $HOME is a sensible hardening change, but may not fully fix the failure if the mask builder still emits or materializes nonexistent ancestor .mcp.json paths. The more complete fix would be to avoid generating those out-of-scope/nonexistent targets, or to create synthetic mask files only in a writable staging area.

The current workaround is to leave CLAUDE_CODE_SUBPROCESS_ENV_SCRUB unset. In the affected launcher, third-party credential variables are explicitly unset, while Claude's base child-environment sanitization still removes the OAuth token from the Bash subprocess environment.

bottenbenny avatar Sep 13 '26 11:09 bottenbenny

Update: more precise root cause, and a ruled-out workaround

Further tracing (strace on openat/execve) narrowed this down further.

The actual sequence: before bwrap is even invoked, Claude Code's own process walks up the ancestor directories of $HOME, probing each level for a .mcp.json:

/<cwd>/.mcp.json        → ENOENT
/tmp/.mcp.json          → ENOENT
/home/<user>/.mcp.json  → ENOENT   (this is $HOME itself)
/home/.mcp.json         → ENOENT   (walked past $HOME to its parent)

bwrap then attempts to materialize a locked-down .mcp.json "mask" at each of those ancestor levels as part of --safe-mode's hardening, and fails the moment it reaches /home, which is root-owned (root:root, mode 755) on essentially any standard Linux install.

Impact is broader than it first appears: since /home is root-owned by default on virtually any standard multi-user Linux system, this isn't specific to unusual setups — any user whose --safe-mode session triggers this ancestor walk will hit it, because the walk always reaches /home (a directory no normal user can write to) before it would reach anything the user actually owns higher up.

Ruled out as a workaround: I initially hypothesized that --strict-mcp-config --mcp-config <path-to-an-explicit-config> might avoid this by skipping auto-discovery. Tested directly (same strace methodology) — it does not help. The ancestor-mask write attempt still occurs and fails identically:

bwrap: Can't create file at /home/.mcp.json: Permission denied

If anything, --strict-mcp-config may be part of what triggers the ancestor-masking behavior in the first place, rather than avoiding it.

Currently the only working mitigation we've found is setting CLAUDE_CODE_SUBPROCESS_ENV_SCRUB=0, which — worth noting explicitly — does not just skip credential-stripping as the name implies; it disables the entire bwrap sandbox for that invocation (confirmed via strace: zero bwrap execve calls with SCRUB=0, vs. multiple real --ro-bind/--bind sandbox calls with SCRUB=1). That's only an acceptable trade-off for read-only/no-tool-execution sessions, not for anything with Write/Edit/Bash tool access, where losing the sandbox has real consequences beyond this specific bug.

Suggested fix still stands: scope the ancestor .mcp.json masking (and the sandbox's home bind mount) to $HOME itself, and stop the upward walk at $HOME rather than continuing into its parent directory.

bottenbenny avatar Sep 13 '26 12:09 bottenbenny

Adding the consequence of the argv shown here, and a workaround.

Tested on Claude Code 2.1.318 and 2.1.311 (native binaries from npm), Linux arm64 in an Ubuntu 24.04 VM, bubblewrap 0.9.0, with sandbox.enabled: true, failIfUnavailable: true, an empty network allowlist, and a command that reads /proc/self/mountinfo and tries touch in several directories. I drove the binary with a local stub model server (no real model), so I have not seen this in an ordinary live session, though the sandbox launch path is the same.

  • Without CLAUDE_CODE_SUBPROCESS_ENV_SCRUB: the only read-write mounts are the working directory and a few Claude-internal paths. touch in /tmp, /var/tmp, /opt, /mnt, /run and a sibling of the working directory fails with "Read-only file system".
  • With CLAUDE_CODE_SUBPROCESS_ENV_SCRUB=1: mountinfo inside the sandbox lists /home, /root, /tmp, /var, /opt, /run and /mnt read-write, whatever the working directory or HOME (tried a cwd under /home, /work, /srv, /opt and /var). Writes there succeed. /srv and other directories stay read-only.

The docs for the variable describe credential stripping and an isolated PID namespace on Linux. They do not say it changes where commands can write, so turning it on for credential hygiene silently weakens the sandbox's write confinement.

Workaround that held: sandbox.filesystem.denyWrite listing the seven directories, with the working directory outside them. With the working directory under /home the deny wins and the working directory becomes read-only.

Question: is the wide write list intended (it looks built for GitHub Actions runners), and if so could it be documented, or limited to when GITHUB_ACTIONS is set?

amartersteck avatar Nov 05 '26 06:11 amartersteck

Same bwrap: Can't create file at ...: Permission denied failure on 2.1.319 (Linux, bubblewrap 0.9.0), with a trigger specific to running Claude Code as root.

Setup. Claude Code runs as root. The project's .claude directory belongs to another user (uid 1000) and carries the POSIX ACL entry user:root:r-x. Some protected children (settings.local.json, launch.json, skills/, workflows/) do not exist yet.

Result. Every sandboxed Bash command fails before it starts:

bwrap: Can't create file at <project>/.claude/settings.local.json: Permission denied

The model then retries the command with dangerouslyDisableSandbox, so in practice the sandbox is bypassed in that project.

Cause. The mount-point check runs in the Claude process and calls access(W_OK|X_OK) on the parent. Root passes it through CAP_DAC_OVERRIDE, so the path is classed as makeable. bwrap then runs with --unshare-user and --cap-drop ALL. uid 1000 is not mapped into that namespace, so user:root:r-x applies, the create fails with EACCES and the whole command aborts.

Expected. The path is classed Unmakeable and skipped, as absent paths are elsewhere. Nothing inside the sandbox can create it either, because the same ACL entry blocks the sandboxed process.

Suggested fix. Decide makeability with the credentials bwrap will have: for example, probe from a user namespace when euid is 0 and the parent's owner is not 0, instead of relying on the host process's access().

Workaround. We gave uid 0 write on that one .claude directory (access ACL user:0:rwx, no default ACL). It then behaves like a root-owned .claude: the runtime creates and removes its placeholders, and the protected children stay read-only inside the sandbox.

Popovonline avatar Nov 08 '26 13:11 Popovonline

Hey I'm with the Claude Code team. Bash shouldn't fail with that /home permission error anymore, as of 2.1.227. The sandbox does still mount /home, so if you hit a different error on a newer version, could you paste it here?

franklee-ant avatar Nov 24 '26 00:11 franklee-ant

This was fixed in Claude Code 2.1.227, so we're closing this issue. If you disagree, let us know here and we can re-open it. Thanks for reporting!

bpasero-ant avatar Nov 29 '26 18:11 bpasero-ant