Skip to content

prepare-llama.sh: prevent git -C from escaping into an enclosing repo - #780

Merged
i386 merged 1 commit into
Mesh-LLM:mainfrom
wesbillman:fix/prepare-llama-git-c-escape
Jun 2, 2026
Merged

i386 merged 1 commit into
Mesh-LLM:mainfrom
wesbillman:fix/prepare-llama-git-c-escape

Conversation

@wesbillman

Copy link
Copy Markdown
Contributor

Problem

scripts/prepare-llama.sh runs all of its git operations as git -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 .git at 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-targets that built mesh-llm:

  1. Origin rewritten — git -C "$LLAMA_WORKDIR" remote set-url origin "$LLAMA_UPSTREAM_URL" resolved against the consuming repo and rewrote its remote.origin.url to https://github.com/ggml-org/llama.cpp.git. A later git push then attempted to push the consuming repo's commits to ggml-org/llama.cpp (it 403'd for us — no write access — so nothing leaked, but it was a wrong-destination push attempt).
  2. HEAD clobbered — the git am --3way "${PATCHES[@]}" series applied onto the consuming repo's worktree HEAD: HEAD detached, several Mesh-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 .git is entirely absent. A partial/corrupt checkout (dir present, .git missing or incomplete) passes straight through to the git -C operations, and discovery then escapes upward.

Fix

Two minimal, behavior-preserving guards in scripts/prepare-llama.sh:

  1. Pin discovery so it can't escape — export GIT_CEILING_DIRECTORIES to the absolute parent of $LLAMA_WORKDIR. Git discovery can no longer walk above that ceiling, so a missing/partial .git now errors loudly here instead of corrupting the parent repo. This closes every git -C call site at once (including the rev-parse HEAD in the "already prepared" fast-path).
  2. Harden the clone guard — replace the bare [[ ! -d .../.git ]] with git rev-parse --git-dir, so a partial/corrupt checkout is re-cloned rather than passed through. clone_llama_workdir already rm -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_WORKDIR is a real clone with its own .git at 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 .git fails fast instead of escaping.

Verification

Local functional tests (not just review):

  • Bug path — workdir with no valid .git inside an enclosing repo: without the ceiling, git -C resolves to the enclosing .git; with the ceiling, rev-parse --git-dir fails as intended (guard re-clones), and a remote set-url errors fatal: not a git repository instead of touching the parent. Enclosing origin unchanged before/after.
  • Happy path — workdir with its own .git at/below the ceiling: rev-parse --git-dir succeeds, rev-parse --show-toplevel correctly resolves to the child workdir, and remote set-url affects only the child. Parent untouched.
  • bash -n scripts/prepare-llama.sh clean.

Scope is limited to scripts/prepare-llama.sh — no refactor, no reformat, no changes to the patch flow or CI.

Copilot AI review requested due to automatic review settings June 2, 2026 15:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_DIRECTORIES to prevent Git discovery from walking above $LLAMA_WORKDIR’s parent.
  • Replace a .git directory existence check with git rev-parse --git-dir to detect partial/corrupt checkouts and force a safe re-clone.

Comment thread scripts/prepare-llama.sh Outdated
# 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)"
Comment thread scripts/prepare-llama.sh Outdated
# 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).
@michaelneale

Copy link
Copy Markdown
Collaborator

I think ok @i386 build maven - this ok?

@i386
i386 merged commit f72d11a into Mesh-LLM:main Jun 2, 2026
28 checks passed
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
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.

4 participants