Repository navigation
prepare-llama.sh: prevent git -C from escaping into an enclosing repo - #780
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Hardens prepare-llama.sh against accidentally mutating an enclosing Git repository when $LLAMA_WORKDIR is missing/corrupt by constraining Git’s repository discovery and validating the workdir is a real repo before running git -C operations.
Changes:
- Export
GIT_CEILING_DIRECTORIESto prevent Git discovery from walking above$LLAMA_WORKDIR’s parent. - Replace a
.gitdirectory existence check withgit rev-parse --git-dirto detect partial/corrupt checkouts and force a safe re-clone.
| # missing .git then errors loudly here instead of corrupting the parent repo. | ||
| # This is transparent to a normal build, where $LLAMA_WORKDIR has its own .git | ||
| # at or below this ceiling. | ||
| export GIT_CEILING_DIRECTORIES="$(cd "$(dirname "$LLAMA_WORKDIR")" && pwd)" |
| # missing .git then errors loudly here instead of corrupting the parent repo. | ||
| # This is transparent to a normal build, where $LLAMA_WORKDIR has its own .git | ||
| # at or below this ceiling. | ||
| export GIT_CEILING_DIRECTORIES="$(cd "$(dirname "$LLAMA_WORKDIR")" && pwd)" |
scripts/prepare-llama.sh runs its git operations as `git -C "$LLAMA_WORKDIR"`. `git -C` only changes directory; it does not pin git to that directory. When $LLAMA_WORKDIR has no valid .git (an interrupted or partial clone), git's upward repository discovery walks past it to the nearest enclosing .git. When mesh-llm is consumed as a git/cargo dependency it is vendored inside the consuming repo's working tree (cargo and hermit git checkouts do this). In that layout the 'nearest enclosing .git' is the CONSUMING repo, so the script mutates it instead of the llama checkout: `remote set-url origin` rewrote the consuming repo's origin to ggml-org/llama.cpp, and the patch series was `git am`'d onto the consuming repo's HEAD. Observed in a real consuming repo during a routine `cargo clippy`; both required manual repair. Two minimal, behavior-preserving guards: - Export GIT_CEILING_DIRECTORIES pinned to the parent of $LLAMA_WORKDIR so git discovery can never escape upward. A missing .git now errors loudly here instead of corrupting the parent repo. Transparent to a normal build, where $LLAMA_WORKDIR has its own .git at or below the ceiling. - Tighten the clone guard from `[[ ! -d $LLAMA_WORKDIR/.git ]]` to `git rev-parse --git-dir` so a partial/corrupt checkout is re-cloned rather than passed through. clone_llama_workdir rm -rf's first, so this is safe. No change to the script's happy path or to mesh-llm's own build. Harden the ceiling resolution itself: resolve the parent with `pwd -P` so it matches git's symlink-resolved comparison, and fail loudly if the parent cannot be resolved rather than exporting an empty GIT_CEILING_DIRECTORIES (which would silently disable the guard).
wesbillman
force-pushed
the
fix/prepare-llama-git-c-escape
branch
from
June 2, 2026 18:30
e055d4e to
d96d002
Compare
This was referenced Jun 2, 2026
Collaborator
|
I think ok @i386 build maven - this ok? |
michaelneale
approved these changes
Jun 2, 2026
michaelneale
added a commit
that referenced
this pull request
Jun 3, 2026
…rtising * origin/main: Fix release native runtime runner labels Align release smoke native runtime version Make smoke model downloads resilient to HF rate limits Trust release container checkout before version prep Fix dispatched release version preparation feat: native rust SDK for rust consumers. (#736) Expose peer throughput hints in status (#757) prepare-llama.sh: prevent git -C from escaping into an enclosing repo (#780) chore: restore github actions for CI Harden mDNS LAN discovery contract (#766) Diagnose split startup blockers (#774) # Conflicts: # crates/mesh-llm-host-runtime/src/runtime/mod.rs
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.
Problem
scripts/prepare-llama.shruns all of its git operations asgit -C "$LLAMA_WORKDIR".git -C <dir>only changes directory — it does not pin git to that directory. If$LLAMA_WORKDIR(default.deps/llama.cpp) has no valid.gitat the moment those commands run (an interrupted or partial clone, or a wiped.git), git's normal upward repository discovery walks past it to the nearest enclosing.git.When mesh-llm is consumed as a git/cargo dependency, it gets vendored inside the consuming repo's working tree — cargo's git checkout dir and hermit's
.hermit/rust/git/checkouts/...both do this. In that layout, the "nearest enclosing.git" that discovery finds is the consuming repo, so the script mutates it instead of the llama checkout.Observed impact (real, in a consuming repo)
Triggered during a routine
cargo clippy --workspace --all-targetsthat built mesh-llm:git -C "$LLAMA_WORKDIR" remote set-url origin "$LLAMA_UPSTREAM_URL"resolved against the consuming repo and rewrote itsremote.origin.urltohttps://github.com/ggml-org/llama.cpp.git. A latergit pushthen attempted to push the consuming repo's commits toggml-org/llama.cpp(it 403'd for us — no write access — so nothing leaked, but it was a wrong-destination push attempt).git am --3way "${PATCHES[@]}"series applied onto the consuming repo's worktree HEAD: HEAD detached, severalMesh-LLM CI <[email protected]>commits stacked on top, working tree showing thousands of phantom deletions.Both required manual repair. The danger is that it's silent (happens during a normal build, not an explicit git op) and, with write access to the rewritten upstream, the origin rewrite could exfiltrate the consuming repo's code.
Root cause
The clone guard
[[ ! -d "$LLAMA_WORKDIR/.git" ]]only re-clones when.gitis entirely absent. A partial/corrupt checkout (dir present,.gitmissing or incomplete) passes straight through to thegit -Coperations, and discovery then escapes upward.Fix
Two minimal, behavior-preserving guards in
scripts/prepare-llama.sh:export GIT_CEILING_DIRECTORIESto the absolute parent of$LLAMA_WORKDIR. Git discovery can no longer walk above that ceiling, so a missing/partial.gitnow errors loudly here instead of corrupting the parent repo. This closes everygit -Ccall site at once (including therev-parse HEADin the "already prepared" fast-path).[[ ! -d .../.git ]]withgit rev-parse --git-dir, so a partial/corrupt checkout is re-cloned rather than passed through.clone_llama_workdiralreadyrm -rfs before cloning, so re-cloning a partial dir is safe.Why this is safe for mesh-llm's own build
In a normal mesh-llm build,
$LLAMA_WORKDIRis a real clone with its own.gitat the workdir — i.e. at/below the ceiling — so discovery still finds it and every operation behaves exactly as before. The ceiling only blocks discovery above the workdir's parent, which never happens on the happy path. The happy path is byte-for-byte unchanged; the only new behavior is that a missing/partial.gitfails fast instead of escaping.Verification
Local functional tests (not just review):
.gitinside an enclosing repo: without the ceiling,git -Cresolves to the enclosing.git; with the ceiling,rev-parse --git-dirfails as intended (guard re-clones), and aremote set-urlerrorsfatal: not a git repositoryinstead of touching the parent. Enclosingoriginunchanged before/after..gitat/below the ceiling:rev-parse --git-dirsucceeds,rev-parse --show-toplevelcorrectly resolves to the child workdir, andremote set-urlaffects only the child. Parent untouched.bash -n scripts/prepare-llama.shclean.Scope is limited to
scripts/prepare-llama.sh— no refactor, no reformat, no changes to the patch flow or CI.