# CVE-2026-87817: GitPython before 3.1.60 Remote Code Execution via Git Directory Impersonation

The Markdown version of <https://upatch.dev/cve/CVE-2026-87817>. Last updated 25 Nov 2026.

## At a glance

CVE-2026-87817 is rated High 8.7 (CVSS 4.0, CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:P/VC:H/VI:H/VA:H/SC:N/SI:N/SA:N/E:X/CR:X/IR:X/AR:X/MAV:X/MAC:X/MAT:X/MPR:X/MUI:X/MVC:X/MVI:X/MVA:X/MSC:X/MSI:X/MSA:X/S:X/AU:X/R:X/V:X/RE:X/U:X). It affects GitPython (gitpython-developers) and was published on 9 Sept 2026. It is not in CISA's Known Exploited Vulnerabilities catalog. Its EPSS score is 0.6%: the estimated chance it is exploited in the next 30 days, as of 25 Nov 2026. The upatch `cve-2026-87817-hook-block` mitigates it, and is vetted (merged into the registry).

A upatch is a signed BPF program that shirudo loads into a running Linux kernel to block the code path this CVE's exploit takes. It holds the line until the fix ships; it does not replace patching.

## The vulnerability

CVE-2026-87817 (GHSA-239g-whfq-7xj9), "Repository content can impersonate the git directory, leading to arbitrary code execution", was published on 26 August 2026 (CVE record published 9 September 2026). In GitPython through 3.1.59, `Repo.__init__` (`git/repo/base.py`) resolves the repository's `git_dir` by testing candidate paths in an order that checks for the real `.git` directory last. It first checks whether `gitdir`, `commondir` and `HEAD` exist as files in `curpath` (`git/repo/base.py:299`), and second whether `is_git_dir(curpath)` holds (`git/repo/base.py:320`), before checking `curpath/.git` (`git/repo/base.py:341`). Because Git reserves only `.git` and allows ordinary tracked files named `gitdir`, `commondir`, `HEAD` and `hooks/pre-commit` at the root of a repository, cloning or opening such a repository causes GitPython to resolve `git_dir` to the working-tree root while Git itself uses `<root>/.git`. When a service using GitPython calls `index.commit()` (`git/index/base.py:1201`), `run_commit_hook` (`git/index/fun.py:73`) resolves the hook path under `index.repo.git_dir` and executes the tracked `<root>/hooks/pre-commit` script with `safer_popen`.

### Official description

GitPython before 3.1.60 fails to properly validate the git directory location, allowing attackers to impersonate the git directory using tracked files like gitdir, commondir, and HEAD. Attackers can execute arbitrary code by placing a malicious pre-commit hook in the tracked hooks directory that executes when a victim calls index.commit() on a cloned or opened repository.

## What the upatch does

A BPF LSM program on `lsm/bprm_check_security` inspects the executable file being started (`bprm->file->f_path.dentry`) and returns `-EPERM` when all of the following hold:

- The file being executed is named `pre-commit`, `commit-msg` or `post-commit`, the three hooks `IndexFile.commit()` runs in `git/index/base.py`.
- Its parent directory is named `hooks`.
- Its grandparent directory is not named `.git`.
- The calling process's executable (`current->mm->exe_file->f_path.dentry`) starts with `python`.

The cheapest check (the executable's dentry name length and comparison against `pre-commit`, `commit-msg` and `post-commit`) runs first so that almost every `execve` on the machine returns immediately. A refusal from an earlier BPF LSM program on the same hook is preserved. Refusals are counted in the `refused` per-CPU array map.

The policy: [`cve-2026-87817-hook-block.bpf.c`](https://github.com/copper-helm/upatch-registry/tree/main/upatches/cve-2026-87817), 107 lines of BPF C attached at `lsm/bprm_check_security`, licensed GPL-2.0 OR BSD-2-Clause.

## Side effects

- A Python process cannot directly execute `hooks/pre-commit`, `hooks/commit-msg` or `hooks/post-commit` when the `hooks` directory is not directly inside `.git` (for example in a bare repository whose directory is not named `.git`, a linked worktree administrative directory, or a custom `core.hooksPath` directory named `hooks`).
- Standard repository hooks in `.git/hooks/` continue to work, as do hooks executed by the `git` binary itself or by any non-Python process.
- This upatch blocks the code execution vector (`index.commit()` running tracked hooks). It does not prevent `Repo.__init__` in vulnerable GitPython from misresolving `git_dir` in memory or reading a tracked `config` file; upgrade to GitPython 3.1.60 for the complete fix.

## Preemption model and cost

The program holds no state other than the `refused` per-CPU counter, which is updated with `__sync_fetch_and_add`, making it safe under all preemption models including preemptible RCU and `PREEMPT_RT`. On every `execve` of a binary whose name is not 10 or 11 bytes long, the program performs a single length check on `bprm->file->f_path.dentry->d_name.len` and returns.

## Affected versions

The vulnerability is in GitPython (`<= 3.1.59`), fixed in GitPython `3.1.60` by commit `c7cf4d13b1ed0a2e70f2a1f3c6b4fc6c2652cf0b`. The upatch applies to any Linux kernel supporting BPF LSM (`lsm/bprm_check_security`) and BTF.

## How it was tested

On a vulnerable GitPython 3.1.59 service, the loaded upatch refused execution of the impersonated working-tree hooks/pre-commit script with EPERM when index.commit() ran. Before loading and after unloading, the hook ran.

- **Tested:** 25 Nov 2026
- **Target:** GitPython 3.1.59 (gitdb 4.0.12, smmap 5.0.2) on Python 3, loopback HTTP service cloning a repository with tracked gitdir, commondir, HEAD and hooks/pre-commit and calling index.commit()
- **Kernel:** 7.4-shirudo-ci-20261123
- **Architecture:** x86_64
- **Distribution:** Ubuntu 26.04

### Results

| State           | Tracked hooks/pre-commit in working tree            |
| --------------- | --------------------------------------------------- |
| Before loading  | reached (hooks/pre-commit in working tree executed) |
| Upatch loaded   | blocked (hooks/pre-commit refused with EPERM)       |
| After unloading | reached (hooks/pre-commit in working tree executed) |

## How to apply

Build the upatch from source, sign it with your own shirudo key, and publish it to your own OCI registry. Machines that follow your upatch tree load it within about a minute.

Build and sign:

```sh
git clone https://github.com/copper-helm/upatch-registry
cd upatch-registry
tools/build <shirudo-checkout> <keys-dir> 1.0.0 upatches/cve-2026-87817
```

Publish and roll out:

```sh
shirudo bpm push build/packages/cve-2026-87817-1.0.0.bee oci://<registry>/<namespace>/cve-2026-87817 --keys-dir <keys-dir>
shirudo bpm tree append build/packages/cve-2026-87817-1.0.0.bee oci://<registry>/<namespace>/<tree> --tree-id <tree-id> --keys-dir <keys-dir>
```

## Review

- Contributors: romanlab.
- Reviewers: none recorded yet.

## Sources and links

- [CVE-2026-87817 record (CVE API)](https://cveawg.mitre.org/api/cve/CVE-2026-87817)
- [GitHub Security Advisory GHSA-239g-whfq-7xj9](https://github.com/gitpython-developers/GitPython/security/advisories/GHSA-239g-whfq-7xj9)
- [Fix commit c7cf4d13b1ed](https://github.com/gitpython-developers/GitPython/commit/c7cf4d13b1ed0a2e70f2a1f3c6b4fc6c2652cf0b)
- [GitPython 3.1.60 release](https://github.com/gitpython-developers/GitPython/releases/tag/3.1.60)

### Links

- [CVE-2026-87817 on upatch.dev](https://upatch.dev/cve/CVE-2026-87817) (this page, as HTML)
- [NVD](https://nvd.nist.gov/vuln/detail/CVE-2026-87817)
- [CVE.org](https://www.cve.org/CVERecord?id=CVE-2026-87817)
- [CWE-94](https://cwe.mitre.org/data/definitions/94.html)
- Upatch source: [upatches/cve-2026-87817](https://github.com/copper-helm/upatch-registry/tree/main/upatches/cve-2026-87817)

### More references from NVD (1)

- [vulncheck.com](https://www.vulncheck.com/advisories/gitpython-before-3.1.60-remote-code-execution-via-git-directory-impersonation) (Third Party Advisory)
