Repository navigation
fix(sandbox): bind the whole .git read-only in the command profile (#849) - #851
Merged
Merged
Conversation
… and config cannot be renamed around (#849)
🔍 Cora AI Code Review✅ No issues found. Code looks good! Review powered by cora-code · BYOK · MIT |
…rofile-git-mountpoint
… writable .git can be redirected with commondir (#849)
…rofile-git-mountpoint # Conflicts: # CHANGELOG.md
…of reading part of it (#849)
…rofile-git-mountpoint # Conflicts: # CHANGELOG.md
…rd links) and never block on a FIFO (#849)
…ld read differently (#849)
| } | ||
| } | ||
| // Linked worktrees anywhere in the workspace: the read-only `.git/worktrees` | ||
| // holds a back-pointer (`<id>/gitdir`) to each one's `.git` file. Bounded, |
| // 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"); |
…f scanning for it (#849)
This was referenced Nov 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Fixes a bypass of the
commandsandbox profile (run_command,job_start, the only consumers on develop; #845 will reuse the profile for the verify command): the code-running paths.git/hooksand.git/configwere not really protected. The top-level.gitdirectory 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.toleas before. Closes #849.Why
Designs reproduced against real bubblewrap 0.9.0 and git 2.43 and rejected, or extended after review:
mv .git .g && cp -a .g .gitsucceeds (a mount point cannot be renamed, its ancestor can); a hook planted in the copy runs on the host's nextgit commit..gitas a writable mount point with read-only leaves: git honourscommondir(andgitdir,config.worktree,info/,modules/*/{hooks,config},worktrees/*,objects/info/alternates) INSIDE the gitdir, soecho ../evil > .git/commondirafter plantingevil/hooks/pre-commitand anevil/configwithcore.fsmonitormade the host'sgit commitandgit statusrun both;rm -rf .gitdestroyed HEAD, objects and refs; a leaf missing under.gitcould be created..gitread-only only: gitdirs that live INSIDE the workspace stayed writable. (a) a registered submodule'ssub/.gitgitdir FILE could be moved aside and replaced by a copied gitdir withcore.fsmonitor: the owner's plaingit status/diff/add -Ain the OUTER repository ran it; (b)git init --separate-git-dir=ws/.gdleft.gd/hookswritable; (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..gitmodulesand.tole-worktreesare writable from the sandbox, so a command could rewrite or remove an entry and the NEXT launch would plan without that submodule's.git..gitmoduleswas read, so a command could copy the inner gitdir tomid/evilgd, setcore.fsmonitor, and pointmid/inner/.gitat it; (F2) a gitdir file whose in-workspace target (a worktree of a repositoryrepo.gitthat also lives in the workspace) is redirected bycommondir:git config --file repo.git/config core.fsmonitor Xsucceeded and the host ran it; (F3) a submodule listed in.gitmodulesbut not initialized (a clone without--recursive): there is no.gitto bind, so a command couldgit init sub, plantcore.fsmonitor, and the host'sgit statusin the outer repository ran it; (F4) a linked worktree (git worktree add) whose.gitfile lies elsewhere in the workspace.Changes
sandbox/command.rs,protect_plan(ws) -> Result<Vec<PathBuf>>(no change tosandbox.rsorProfile):.gitdirectory: one--ro-bindof the whole directory (no leaf mounts, no writable anchor)..toleread-only..gitgitdir 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).pathof.gitmodules(at most 64, each an initialized submodule with a.git) gets its.git(file or directory) and its target protected the same way..gitmodulesitself, and the whole.tole-worktreesdirectory (so no child-agent worktree can be removed or replaced, and every worktree.gitfile in it is covered). Cost: a sandboxed command cannot change files inside a child agent's worktree.optionalfollows its fallback policy):.gitfile 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>/gitdirof the gitdir in effect: the top-level.gitdirectory, 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. Aworktreesthat 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 rungit worktree removeor keep worktrees outside the workspace;.gitmodulessubmodule 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");.gitmodulesof its own (nested submodules, each listed directory checked without following links);commondirorconfig.worktreefile, the top-level.gitdirectory included (inside the read-only.git, a worktree's owncommondiris normal and accepted);.gitmoduleswith a second hard link..gitmodulesis accepted only in a plain subset, because git reads it as a config file (path = sub # noteissubto git) and the file is writable from the sandbox:[submodule "name"]sections,key = valuelines, 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.gitmodulesover 64 KiB or a gitdir file over 4 KiB is refused, never read in part; a non-regular.gitmodulesis refused; a FIFO or device in a protected name is never opened (it would block the launch), a.gitthat is neither directory nor file is bound without being read..git,.git/hooks,.git/config,.tole,.gitmodules, a submodule path,.tole-worktrees, a gitdir target), a path leaving the workspace, more than 64 submodules.hooksandconfigare not links,configand the files ofhooks(at most 256) have no second hard link, nohooksentry 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.gitthat appeared in between, or a directory swapped for a link, refuses the launch. Only the window between that check and the mount inbwrapis left (same-user race, design note §5).run_command/job_startgit writes fail with git's own "Read-only file system" (add,commit,checkout,branch,config,remote add,gc;git stashexits 1 with no message); reads work (log,diff,show,statuswithout 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-workspacecommondirlayout (refused). No config key makes.gitwritable (not decided).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):.git/.tolethat did not exist at launch (agit initinside the sandbox; a detachedjob_startbegun before.gitexisted: "nothing can be created in it" holds only for paths that existed at launch);.gitmodulesentry (the usual accidental embedded repository), including a directory that is empty or missing: nothing names it, so a sandboxed command cangit initthere and plantcore.fsmonitor, and the host's plaingit statusin the outer repository runs it (separate from the missing top-level.git/.toleitem);.gitmodules(an initialized entry) or inside.tole-worktrees(a vendored clone, anyx/.git): a plaingit status/diff/add -Aon the host in the outer repository can fire acore.fsmonitorplanted there;.git/modules/<name>/worktrees) elsewhere in the workspace: only the top-level worktree registry is read;.git/configis read-only but acore.hooksPath,include.pathorcore.fsmonitoralready in it that names scripts inside the workspace (husky style) leaves those scripts writable, and the host runs them (the S4 row no longer claimscore.hooksPathis read-only);.gitother thanconfig, hooks and gitdir files;.gitthat run later (.envrc, Makefile, package scripts,build.rs, CI configs,.husky,.pre-commit-config.yaml, lefthook,.vscode/tasks.json,.gitattributes);.gitinternals, 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_pathshad one caller,command::build(run_command, job_start);recheck_planis called fromcommand_prepared(the sandboxed branch) only;PROTECTED([".git", ".tole"]) is read bysandbox/report.rs(doctor) and the unit tests;Profile/bwrap_args_forare 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.rshas 26#[test]s (real bwrap;SKIPPED:line without it; CI'sSandbox (bubblewrap)job setsTOLE_REQUIRE_BWRAP=1at 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 withgc.auto=0andmaintenance.auto=falseso no background maintenance rewrites.gitwhile 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; hostgit statusandgit commitrun 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,.tolevariants, the first-roundmv .git .g && cp -achain), then compares every path and byte of.gitand.tolebefore 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).a_registered_submodule_cannot_get_a_planted_fsmonitor(realgit submodule add; the first attack step fails;.gitmodulescannot be rewritten, removed or renamed; hostgit status/diff/add -Ain the outer repo: no marker),a_separate_git_dir_inside_the_workspace_cannot_get_a_hook(hostgit commit: no marker),a_child_worktree_git_file_cannot_be_replaced(.tole-worktrees/wt1fromgit worktree add;rm -rf,mvandmkdirunder.tole-worktreesfail).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 plaingit clone:sub/empty; refused namingsub; the plantedgit init subscript never runs; aftersubmodule update --initthe workspace launches andsub/.gitis read-only),a_linked_worktree_elsewhere_in_the_workspace_refuses_the_launch(git worktree add other/wt; refused namingother/wt/.git, the command never runs; aftergit worktree removethe workspace launches again) anda_linked_worktree_of_a_separate_git_dir_refuses_the_launch(git init --separate-git-dir, registry in.gd/worktrees; refused, the command never runs)..gitand the original profile tests.command_tests.rs, 47 in the module): argv shape (one--ro-bindof.git, never--bind, nothing mounted inside); submodule, separate-gitdir and worktree plans; refusals (links in every place of the chain,../absolute/empty.gitmodulespaths, more than 64); an arbitrary nested repo is NOT protected (pinned so the doc stays exact);recheck_planrefuses a.gitthat appears or turns into a link after planning; an oversized.gitmodulesand gitdir file are refused; links and hard links into what git runs; a FIFO does not hang the plan; ten.gitmodulesforms git could read differently are refused while the plain subset is accepted; nested.gitmodules,commondir/config.worktreeredirects, 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.gitdirectory and separate gitdir registries refused; under.tole-worktrees, outside the workspace and stale pointers fine; a link or non-directoryworktrees, a link or directory back-pointer, 65 entries and an oversized pointer all refused).commondirrefusals 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.gitprotection 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..gitanchor: false positive (EXDEV, checked with the real binary), moot now; (2).gitmodulescut at 64 KiB: real, fixed; (3) links in a gitdir file target's hooks/config, FIFO hang, second hard links: real, fixed; (4).gitmodulessyntax that git reads differently: real, fixed with a plain-subset parser; (5).gitmodulesand.tole-worktreeswritable, so the plan's input can be rewritten: real, fixed by binding them read-only; (6) the per-worktree registry scan only read a.gitdirectory 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.--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).symlink_metadatacomponent 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.