Skip to content

fix(sandbox): bind the whole .git read-only in the command profile (#849) - #851

Merged
ajianaz merged 18 commits into
developfrom
security/command-profile-git-mountpoint
Nov 15, 2026
Merged

ajianaz merged 18 commits into
developfrom
security/command-profile-git-mountpoint

Conversation

@ajianaz

@ajianaz ajianaz commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

What

Fixes a bypass of the command sandbox profile (run_command, job_start, the only consumers on develop; #845 will reuse the profile for the verify command): the code-running paths .git/hooks and .git/config were not really protected. The top-level .git directory is now bound read-only, nested gitdirs inside the workspace that can be named without a scan are protected the same way (with the files that name them read-only too), the listed layouts are refused, and .tole as before. Closes #849.

Why

Designs reproduced against real bubblewrap 0.9.0 and git 2.43 and rejected, or extended after review:

  1. Leaf mounts only (the merged state): mv .git .g && cp -a .g .git succeeds (a mount point cannot be renamed, its ancestor can); a hook planted in the copy runs on the host's next git commit.
  2. .git as a writable mount point with read-only leaves: git honours commondir (and gitdir, config.worktree, info/, modules/*/{hooks,config}, worktrees/*, objects/info/alternates) INSIDE the gitdir, so echo ../evil > .git/commondir after planting evil/hooks/pre-commit and an evil/config with core.fsmonitor made the host's git commit and git status run both; rm -rf .git destroyed HEAD, objects and refs; a leaf missing under .git could be created.
  3. Top-level .git read-only only: gitdirs that live INSIDE the workspace stayed writable. (a) a registered submodule's sub/.git gitdir FILE could be moved aside and replaced by a copied gitdir with core.fsmonitor: the owner's plain git status/diff/add -A in the OUTER repository ran it; (b) git init --separate-git-dir=ws/.gd left .gd/hooks writable; (c) .tole-worktrees/<id>/.git (child-agent worktrees) was replaceable; (d) a path that appeared, or a directory swapped for a link, between planning and the mount went unnoticed.
  4. The plan's own inputs: .gitmodules and .tole-worktrees are writable from the sandbox, so a command could rewrite or remove an entry and the NEXT launch would plan without that submodule's .git.
  5. Layouts the plan did not cover (independent reviews): (F1) a submodule that has submodules of its own: only the top-level .gitmodules was read, so a command could copy the inner gitdir to mid/evilgd, set core.fsmonitor, and point mid/inner/.git at it; (F2) a gitdir file whose in-workspace target (a worktree of a repository repo.git that also lives in the workspace) is redirected by commondir: git config --file repo.git/config core.fsmonitor X succeeded and the host ran it; (F3) a submodule listed in .gitmodules but not initialized (a clone without --recursive): there is no .git to bind, so a command could git init sub, plant core.fsmonitor, and the host's git status in the outer repository ran it; (F4) a linked worktree (git worktree add) whose .git file lies elsewhere in the workspace.

Changes

sandbox/command.rs, protect_plan(ws) -> Result<Vec<PathBuf>> (no change to sandbox.rs or Profile):

  • Top-level .git directory: one --ro-bind of the whole directory (no leaf mounts, no writable anchor). .tole read-only.
  • A .git gitdir FILE (top level, submodule, worktree) is bound read-only itself, and so is the directory it points to when that is inside the workspace (outside it is not in the view at all).
  • Nested gitdirs, no workspace scan: every path of .gitmodules (at most 64, each an initialized submodule with a .git) gets its .git (file or directory) and its target protected the same way.
  • The inputs of the plan are read-only too: .gitmodules itself, and the whole .tole-worktrees directory (so no child-agent worktree can be removed or replaced, and every worktree .git file in it is covered). Cost: a sandboxed command cannot change files inside a child agent's worktree.
  • The listed layouts are refused, not half protected (optional follows its fallback policy):
    • a linked worktree whose .git file lies inside the workspace but not under .tole-worktrees (F4; chosen over per-worktree binding, which kept growing gaps). It is found through the registry <gitdir>/worktrees/<id>/gitdir of the gitdir in effect: the top-level .git directory, or the target of a gitdir file (a separate git dir; read on the host even when it lies outside the workspace). Only that top-level registry is read (see the residuals). A worktree outside the workspace is not in the sandbox's view and a stale pointer whose path is gone is fine. A worktrees that is a link or not a directory, a back-pointer that is a link or not a regular file, more than 64 entries or an oversized pointer are refused too, never skipped. The error says to run git worktree remove or keep worktrees outside the workspace;
    • a .gitmodules submodule that is not initialized (missing path, empty directory, directory without .git, regular file; the error names it and says "initialize it (git submodule update --init) or remove it");
    • a registered submodule that has a .gitmodules of its own (nested submodules, each listed directory checked without following links);
    • a gitdir directory not covered by an already read-only path that has a commondir or config.worktree file, the top-level .git directory included (inside the read-only .git, a worktree's own commondir is normal and accepted);
    • a gitdir file whose in-workspace target does not exist (a target outside the workspace is not in the view and stays accepted);
    • a .gitmodules with a second hard link.
  • .gitmodules is accepted only in a plain subset, because git reads it as a config file (path = sub # note is sub to git) and the file is writable from the sandbox: [submodule "name"] sections, key = value lines, blank lines, whole-line #/; comments; a comment after a value, quote, escape, continuation, another section, or a key outside a submodule section refuses the launch. A .gitmodules over 64 KiB or a gitdir file over 4 KiB is refused, never read in part; a non-regular .gitmodules is refused; a FIFO or device in a protected name is never opened (it would block the launch), a .git that is neither directory nor file is bound without being read.
  • Also refused: a symbolic link anywhere in the chain (.git, .git/hooks, .git/config, .tole, .gitmodules, a submodule path, .tole-worktrees, a gitdir target), a path leaving the workspace, more than 64 submodules.
  • What git runs inside a protected gitdir directory is checked, because a read-only mount stops writes by path, not by inode: hooks and config are not links, config and the files of hooks (at most 256) have no second hard link, no hooks entry is a link, a gitdir file has one name.
  • recheck_plan: the plan is made again right before the command line is handed out, and every protected path must be bound in the argument list: a .git that appeared in between, or a directory swapped for a link, refuses the launch. Only the window between that check and the mount in bwrap is left (same-user race, design note §5).
  • Cost, stated plainly: inside a sandboxed run_command/job_start git writes fail with git's own "Read-only file system" (add, commit, checkout, branch, config, remote add, gc; git stash exits 1 with no message); reads work (log, diff, show, status without index refresh, no hang, rev-parse, branch --list, ls-files). A model cannot commit from a sandboxed command, cannot edit files in a child agent's worktree, and cannot run in a workspace with an uninitialized or nested submodule, a linked worktree outside .tole-worktrees, or an in-workspace commondir layout (refused). No config key makes .git writable (not decided).
  • Docs (sandbox-design.md §4 row S4, §9.3 and its residuals, threat model, CHANGELOG) say only what this PR ships (run_command/job_start; verify and stop gates are not consumers yet). Residuals, stated exactly (NOT protected, NOT refused):
    • a .git/.tole that did not exist at launch (a git init inside the sandbox; a detached job_start begun before .git existed: "nothing can be created in it" holds only for paths that existed at launch);
    • a gitlink recorded in the index with no .gitmodules entry (the usual accidental embedded repository), including a directory that is empty or missing: nothing names it, so a sandboxed command can git init there and plant core.fsmonitor, and the host's plain git status in the outer repository runs it (separate from the missing top-level .git/.tole item);
    • a nested repository not named by .gitmodules (an initialized entry) or inside .tole-worktrees (a vendored clone, any x/.git): a plain git status/diff/add -A on the host in the outer repository can fire a core.fsmonitor planted there;
    • a linked worktree of a SUBMODULE (registered in .git/modules/<name>/worktrees) elsewhere in the workspace: only the top-level worktree registry is read;
    • a gitdir file or worktree back-pointer naming a path lexically outside the workspace but reaching it through an external symlink alias is classed as outside (git writes real paths, so this needs a hand-written pointer);
    • .git/config is read-only but a core.hooksPath, include.path or core.fsmonitor already in it that names scripts inside the workspace (husky style) leaves those scripts writable, and the host runs them (the S4 row no longer claims core.hooksPath is read-only);
    • hard links or links made before protection to files inside .git other than config, hooks and gitdir files;
    • files outside .git that run later (.envrc, Makefile, package scripts, build.rs, CI configs, .husky, .pre-commit-config.yaml, lefthook, .vscode/tasks.json, .gitattributes);
    • a diff does not show .git internals, so a workspace a sandboxed command wrote into is untrusted until its git config and hooks are inspected (or use a fresh clone).

Callers and impact (cora absent; grep): protected_paths had one caller, command::build (run_command, job_start); recheck_plan is called from command_prepared (the sandboxed branch) only; PROTECTED ([".git", ".tole"]) is read by sandbox/report.rs (doctor) and the unit tests; Profile/bwrap_args_for are unchanged (the MCP server sandbox is untouched). Tests rerun: command_tests.rs, report_tests.rs, command_sandbox.rs, whole tole-core and tole-cli suites.

Testing

command_sandbox.rs has 26 #[test]s (real bwrap; SKIPPED: line without it; CI's Sandbox (bubblewrap) job sets TOLE_REQUIRE_BWRAP=1 at job level and runs --test command_sandbox; its expected target count (4) is unchanged, no target was added). Each ends with a HOST-side proof where it applies; host git runs with gc.auto=0 and maintenance.auto=false so no background maintenance rewrites .git while a tree is compared (that race failed one CI run):

  • a_commondir_redirect_cannot_make_the_host_run_planted_code: the evil gitdir; the redirect fails; host git status and git commit run no hook and no fsmonitor (no marker file) and the commit lands in the real repository.
  • every_way_to_move_remove_or_write_the_git_dir_fails_and_its_tree_is_unchanged: one test that runs 24 commands, each of which must fail (moves, rm -rf, rmdir, rename-over, writes to HEAD/commondir/config/index.lock, mkdir, three hard links, .tole variants, the first-round mv .git .g && cp -a chain), then compares every path and byte of .git and .tole before and after.
  • a_repo_without_hooks_or_config_cannot_get_them_planted, a_submodule_gitdir_under_git_modules_cannot_get_a_hook_or_config (tree compare), git_reads_work_and_git_writes_fail_with_a_clear_error (7 reads work, 8 writes fail, no hang, tree unchanged).
  • Nested: a_registered_submodule_cannot_get_a_planted_fsmonitor (real git submodule add; the first attack step fails; .gitmodules cannot be rewritten, removed or renamed; host git status/diff/add -A in the outer repo: no marker), a_separate_git_dir_inside_the_workspace_cannot_get_a_hook (host git commit: no marker), a_child_worktree_git_file_cannot_be_replaced (.tole-worktrees/wt1 from git worktree add; rm -rf, mv and mkdir under .tole-worktrees fail).
  • Refusals with real repositories: a_submodule_with_submodules_of_its_own_refuses_the_launch (top, mid, inner), a_gitdir_redirected_by_commondir_inside_the_workspace_refuses_the_launch, an_uninitialized_submodule_refuses_the_launch_and_nothing_runs (a plain git clone: sub/ empty; refused naming sub; the planted git init sub script never runs; after submodule update --init the workspace launches and sub/.git is read-only), a_linked_worktree_elsewhere_in_the_workspace_refuses_the_launch (git worktree add other/wt; refused naming other/wt/.git, the command never runs; after git worktree remove the workspace launches again) and a_linked_worktree_of_a_separate_git_dir_refuses_the_launch (git init --separate-git-dir, registry in .gd/worktrees; refused, the command never runs).
  • Unchanged and kept: gitdir-file, symlink-refusal, missing-.git and the original profile tests.
  • Unit (command_tests.rs, 47 in the module): argv shape (one --ro-bind of .git, never --bind, nothing mounted inside); submodule, separate-gitdir and worktree plans; refusals (links in every place of the chain, ../absolute/empty .gitmodules paths, more than 64); an arbitrary nested repo is NOT protected (pinned so the doc stays exact); recheck_plan refuses a .git that appears or turns into a link after planning; an oversized .gitmodules and gitdir file are refused; links and hard links into what git runs; a FIFO does not hang the plan; ten .gitmodules forms git could read differently are refused while the plain subset is accepted; nested .gitmodules, commondir/config.worktree redirects, a missing in-workspace target, a hard-linked .gitmodules; uninitialized submodules (missing path, empty directory, directory without .git, regular file refused; initialized accepted); a_linked_worktree_elsewhere_in_the_workspace_is_refused (top-level .git directory and separate gitdir registries refused; under .tole-worktrees, outside the workspace and stale pointers fine; a link or non-directory worktrees, a link or directory back-pointer, 65 entries and an oversized pointer all refused).
  • Fail without the change (reverted by hand, restored): with the linked-worktree refusal disabled, the two new bwrap tests and the unit test fail; earlier rounds: uninitialized check disabled fails a bwrap test and a unit test; nested-submodule and commondir refusals disabled fail two bwrap tests and a unit test; nested handling and the recheck disabled fail the three nested bwrap tests and three unit tests; against the previous anchor design the commondir, sweep, no-hooks, submodule-gitdir and git-ops tests fail; with no .git protection at all 7 integration and 4 unit tests fail; with the size check, the content check or the FIFO guard removed the matching unit test fails.
  • Cora findings on the way: (1) hard link through a writable .git anchor: false positive (EXDEV, checked with the real binary), moot now; (2) .gitmodules cut at 64 KiB: real, fixed; (3) links in a gitdir file target's hooks/config, FIFO hang, second hard links: real, fixed; (4) .gitmodules syntax that git reads differently: real, fixed with a plain-subset parser; (5) .gitmodules and .tole-worktrees writable, so the plan's input can be rewritten: real, fixed by binding them read-only; (6) the per-worktree registry scan only read a .git directory and skipped a link or non-regular registry: real, fixed by replacing it with a refusal that reads the registry of the gitdir in effect and never skips a broken one. Independent reviews: F1 to F4 above plus a hard-linked .gitmodules, a dangling in-workspace gitdir target and the S4 doc overclaim: all fixed by refusing the listed layouts; the third review found no blocker and three doc overclaims (submodule worktrees, gitlink without .gitmodules, the "registered worktree" wording), fixed here by docs only.
  • Gates: fmt; clippy all-features and --no-default-features (core, cli, tui) clean; tole-core and tole-cli tests green (default and --no-default-features, TOLE_REQUIRE_BWRAP=1) on the last code commit (the last commit is docs only); develop merged (latest 038c8ec; CHANGELOG conflicts resolved by keeping both entries).
  • Self-review (cora absent locally): every nested path is inspected with symlink_metadata component by component before it is bound; gitdir targets are compared lexically against their canonical form so a link cannot hide in them; file reads are bounded, non-blocking and whole or refused; the plan's own inputs are read-only; the listed layouts are refused; the remaining routes are the documented residuals.

Comment thread crates/tole-core/src/sandbox.rs Fixed
@github-actions

github-actions Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

🔍 Cora AI Code Review

✅ No issues found. Code looks good!


Review powered by cora-code · BYOK · MIT

@ajianaz ajianaz changed the title fix(sandbox): make .git a mount point in the command profile so hooks and config cannot be renamed around (#849) fix(sandbox): bind the whole .git read-only in the command profile (#849) Nov 15, 2026
Comment thread crates/tole-core/src/sandbox/command.rs Outdated
}
}
// Linked worktrees anywhere in the workspace: the read-only `.git/worktrees`
// holds a back-pointer (`<id>/gitdir`) to each one's `.git` file. Bounded,
Comment thread crates/tole-core/src/sandbox/command.rs Outdated
// Linked worktrees anywhere in the workspace: the read-only `.git/worktrees`
// holds a back-pointer (`<id>/gitdir`) to each one's `.git` file. Bounded,
// no link followed, only what lies inside the workspace.
let registered = ws.join(".git/worktrees");
@ajianaz
ajianaz merged commit 832077b into develop Nov 15, 2026
23 checks passed
@ajianaz
ajianaz deleted the security/command-profile-git-mountpoint branch November 20, 2026 08:40
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.

security: the command sandbox profile protects .git/hooks and .git/config by leaf mounts only; renaming .git aside bypasses them

3 participants