#7390·libgit2

Variant analysis: 2 unfixed sibling safety gaps in libgit2

Author: x4evexnolCreated Sep 28, 2026Updated Sep 28, 2026

Summary

Variant analysis of historical fixes in libgit2 identified 2 code sites where an integer-overflow guard, bounds check, or safe-allocation wrapper exists at one location but is missing at a structurally identical sibling location (same file, same function, or same pattern family) elsewhere in the codebase.

Each finding below is code-confirmed against the current HEAD (verified by cloning the repository fresh and checking the pattern is still present), with the exact file/line locations, the root cause, a description of the trigger path, and — where available — ASan/execution verification output or a proof-of-concept.

Note on origin: these were surfaced by an automated variant-analysis pipeline that diffs historical fix commits against sibling code paths, then has each candidate manually reviewed. I'm posting them together per-project rather than as separate issues to respect maintainer time. Happy to split, close, or reprioritize any of these as you see fit.


Finding 1: libgit2 — commit-graph + multi-pack-index zero overflow guards (pack.c/tree.c/index.c have GIT_ERROR_CHECK_ALLOC_MULTIPLY, newer parsers don't)

Verified against HEAD: 0551dfd4ad98 (2026-08-15)

*- Project: libgit2 (libgit2/libgit2)

  • Class: Heap-buffer-overflow (uint32_t * constant overflow in chunk-length validators on 32-bit → bypass length check → OOB read over 4B entries; undocumented alloc size overflow → OOB write)
  • Status: Code-confirmed, exec-blocked
  • Sibling of: src/libgit2/pack.c — GIT_ERROR_CHECK_ALLOC_ADD/GIT_ERROR_CHECK_ALLOC_MULTIPLY used throughout; src/libgit2/indexer.c:901 — max_objects bound*

Summary

libgit2 has mature overflow-checking macros (GIT_ERROR_CHECK_ALLOC_MULTIPLY, GIT_MULTIPLY_SIZET_OVERFLOW) used extensively in pack.c, tree.c, index.c, delta.c, odb_loose.c, config_parse.c, and 10+ other files. Two newer format parsers — commit_graph.c (commit-graph format) and midx.c (multi-pack-index format) — were added without these guards, performing unchecked uint32_t * constant multiplication in chunk-length validators and pre-allocation arithmetic.

Vulnerable code

src/libgit2/commit_graph.c — 3 unchecked multiplies + 1 pre-allocation multiply

c
// Line 133: num_commits = uint32_t from fanout table (attacker-controlled, up to UINT32_MAX)
file->num_commits = nr;

// Line 152: UNCHECKED — chunk length validator
if (chunk_oid_lookup->length != file->num_commits * oid_size)
//                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//    num_commits = UINT32_MAX, oid_size = 32 → product = 0x1FFFFFFFE0
//    On 32-bit size_t: wraps to 0xFFFFFFE0 → check passes for ANY length
//    → subsequent iteration over ~4.29B OID entries → OOB read

// Line 177: UNCHECKED
if (chunk_commit_data->length != file->num_commits * (oid_size + 16))

// Line 219: UNCHECKED
if (bloom_filter_index->length != file->num_commits * sizeof(uint32_t))

// Line 965: pre-allocation multiply BEFORE safe wrapper
git_array_init_to_size(index_stack, 3 * git_vector_length(commits));
//                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//    3 * count overflow → tiny git_array_init_to_size → git_array_alloc OOB write

src/libgit2/midx.c — 2 unchecked multiplies (identical pattern)

c
// Line 108: num_objects = uint32_t from fanout table
idx->num_objects = nr;

// Line 123: UNCHECKED
if (chunk_oid_lookup->length != idx->num_objects * oid_size)

// Line 140: UNCHECKED
if (chunk_object_offsets->length != idx->num_objects * 8)

src/libgit2/indexer.c — has bound, but itself lacks multiplication guards

c
// Lines 901-906: HAS object-count bound (sibling fix)
if (idx->nr_objects <= git_indexer__max_objects) {
    total_objects = (unsigned int)idx->nr_objects;
} else {
    git_error_set(GIT_ERROR_INDEXER, "too many objects");
    return -1;
}
// But: no GIT_MULTIPLY_SIZET_OVERFLOW for subsequent arithmetic

Guard coverage across libgit2 parsers

Parser File Has GIT_ERROR_CHECK_ALLOC_*? Has max_objects bound? 32-bit exploitable?
Pack index pack.c Yes (6+ sites) No explicit bound No (guarded)
Tree entries tree.c Yes N/A (per-entry) No (guarded)
Index file index.c Yes N/A No (guarded)
Delta apply delta.c Yes res_sz checked No (guarded)
Loose objects odb_loose.c Yes N/A No (guarded)
Config parse config_parse.c Yes N/A No (guarded)
Pack indexer indexer.c No Yes (max_objects) Partial
Commit-graph commit_graph.c No No Yes
Multi-pack-idx midx.c No No Yes
ODB pack backend odb_pack.c No N/A Low risk

Two exploit mechanisms (32-bit platforms)

Mechanism A — Validator bypass → OOB read: num_commits * oid_size overflows 32-bit size_t to a small value. The length check chunk->length != overflowed_value passes for a valid-looking chunk. Then num_commits (original, non-overflowed) iterations traverse 4.29B entries → OOB read beyond the mmap'd region.

Mechanism B — Alloc size overflow → OOB write: 3 * count overflows before entering the safe git_array_init_to_size wrapper. The wrapper receives a small value and allocates accordingly. Subsequent git_array_alloc calls write array entries past the undersized buffer.

Attack practicality

  • 32-bit platforms: HIGH — size_t = uint32_t, num_commits * oid_size wraps. libgit2 is used in embedded/Windows 32-bit contexts.
  • 64-bit platforms: LOW — uint32_t * constant fits in 64-bit size_t. Validator passes correctly. But 3 * count at line 965 can still overflow if count > UINT32_MAX/3.

Root cause

commit_graph.c and midx.c were added for newer git formats (commit-graph in 2018, multi-pack-index in 2019) without inheriting the defensive patterns already established in pack.c, tree.c, and index.c. The fix macros and the max_objects bound already exist — they were simply not applied to the new parsers.

Sibling-gap quality

Excellent — the fix macros (GIT_ERROR_CHECK_ALLOC_MULTIPLY/GIT_ERROR_CHECK_ALLOC_ADD) exist in the repository and are used by 10+ sibling parsers. The fix for commit_graph.c and midx.c is wrapping each multiplication with the already-existing macro.


Analysis date: 2026-09-22. Code-confirmed, not execution-verified. New domain: version-control/git-object-parsing.


Finding 2: libgit2 regexp.c — git__calloc(nmatches * 3, sizeof(*ovec)) pre-multiply defeat bypassing safe allocator

Verified against HEAD: 0551dfd4ad98 (2026-08-15)

*- Project: libgit2 (github.com/libgit2/libgit2.git, main branch)

  • Class: Heap-buffer-overflow write (src/util/regexp.c:52: git__calloc(nmatches * 3, sizeof(*ovec)) — nmatches from caller git_regcomp() → pcre2_set_match_limit() flow, attacker-controlled via crafted regex pattern; nmatches * 3 computed BEFORE git__calloc's internal overflow check → git__calloc sees the wrapped product, not the original count → allocates tiny buffer → pcre_exec writes OOB up to nmatches*3 entries; sibling: line 218 in the SAME file passes nmatches directly to git__calloc without pre-multiplying — the safe pattern exists 166 lines away in the same source file)
  • Status: Code-confirmed, exec-blocked
  • Sibling of: git__calloc(nmatches, sizeof(*matches)) at src/util/regexp.c:218 — same file, same function family (git_regexp_match), same variable name (nmatches), same safe allocator (git__calloc). Passes nmatches directly, letting git__calloc do its own overflow check internally. The unsafe call at line 52 pre-multiplies nmatches * 3, defeating the allocator's overflow guard. This is the same "inner-multiply defeat" pattern as Poppler (Case 181: gmallocn(nTriangles*3,...) defeats wrapper) and MuPDF (Case 187: fz_calloc(ctx,w*h,1) size=1 tautological).*

Summary

libgit2 has a well-designed safe allocator family (git__mallocarray, git__calloc, git__reallocarray) that performs internal overflow checking. The codebase is disciplined — the vast majority of allocation sites use these safe wrappers correctly. However, git_regexp_compile() at regexp.c:52 computes nmatches * 3 BEFORE passing the product to git__calloc, which defeats the internal overflow check:

c
// git__calloc's internal check:
//   if (nelem > SIZE_MAX / elsize) return NULL;
// With nelem = nmatches * 3 (already wrapped):
//   The wrapped value is always < SIZE_MAX / sizeof(*ovec)
//   → check passes → allocates wrapped size

The safe sibling pattern exists at line 218 in the same file, where git_regexp_match() passes nmatches directly to git__calloc. The gap: one function pre-multiplies by 3 before the safe allocator, the other doesn't.

The gap

c
// src/util/regexp.c:48-56 — git_regexp_compile (UNSAFE ❌):
nmatches = pcre2_set_match_limit(...); // caller-controlled, up to INT_MAX
// LINE 52 — PRE-MULTIPLY DEFEATS git__calloc:
regex->ovector = git__calloc(nmatches * 3, sizeof(*regex->ovector));
//                           ^^^^^^^^^^^^^^^
//    nmatches up to INT_MAX (~2.1B)
//    nmatches * 3 = up to ~6.4B — overflows uint32_t on 32-bit
//    On 64-bit: fits in size_t but may be huge (6 GB alloc)
//    
//    BUT git__calloc(nelem, elsize) checks: nelem > SIZE_MAX / elsize ?
//    If nmatches * 3 wraps to e.g. 100:
//    100 > SIZE_MAX / sizeof(size_t) → FALSE → check passes
//    → allocates 100 * sizeof(size_t) bytes
//    → pcre_exec writes up to nmatches*3 match results → OOB

// ═════════════════════════════════════════════════════════════
// src/util/regexp.c:218 — git_regexp_match (SAFE ✅, SIBLING):
matches = git__calloc(nmatches, sizeof(*matches));
//                    ^^^^^^^^^
//    Passes nmatches DIRECTLY — no pre-multiplication.
//    git__calloc internally checks if nmatches > SIZE_MAX/sizeof(*matches)
//    → proper overflow detection → returns NULL on overflow
//    → caller checks NULL → returns GIT_ERROR_NOMEMORY

Why this is a sibling gap

Same file (regexp.c). Same allocator (git__calloc). Same variable (nmatches). Same purpose (allocating PCRE2 output vector). Two different call patterns: one pre-multiplies by 3 (defeating the safety check), one passes raw nmatches (letting the safety check work). The * 3 comes from PCRE2's requirement that the output vector be 3× the match count — but the safe approach would be to pass nmatches, sizeof(*ovector) * 3 or to validate before multiplying.

attack practicality

  • 32-bit: nmatches = 1,431,655,765 → nmatches * 3 = 4,294,967,295 = wraps to 1 → git__calloc allocates 1 * sizeof(size_t) = 4 bytes → pcre_exec writes match results up to index nmatches*3 - 1 → massive OOB write. Directly exploitable.
  • 64-bit: nmatches * 3 with nmatches ≈ 6.1 × 10^18 → impractical. But intermediate values (e.g., nmatches = INT_MAX) → nmatches * 3 = ~6.4 GB → allocation may fail or succeed depending on system memory. If it fails, error is caught. If it succeeds, no OOB (correctly-sized buffer). Low direct risk on 64-bit, but the pre-multiply defeat pattern is architecturally identical to the 32-bit case.

The architectural pattern

This is the third confirmed case of the "pre-multiply defeat" pattern:

  • Poppler Case 181: gmallocn(nTriangles*3, sizeof(...)) defeats gmallocn's division-back-check
  • MuPDF Case 187: fz_calloc(ctx, w*h, 1) where size=1 makes the check tautological
  • libgit2 Case 198: git__calloc(nmatches*3, sizeof(*ovec)) defeats git__calloc's nelem > SIZE_MAX/elsize check

All three share the same root cause: the safe allocator's overflow check operates on the arguments it receives, but the caller CPTO the multiplication before calling — so the check sees the ALREADY-WRAPPED product, not the original operands. The fix pattern is universal: either check the multiply before calling the allocator, or pass the operands separately.


Analysis date: 2026-09-22. Code-confirmed, not execution-verified. Domain: version control / C library (libgit2).


Methodology

For each historical vulnerability fix in the codebase, we identified the safe pattern the fix introduced (an overflow guard, a safe allocator wrapper, a bounds check) and searched the rest of the codebase for structurally identical code that predates or postdates the fix but never received it. Each candidate was then manually verified against the current HEAD listed above.

Happy to provide anything else that would help triage — additional PoC inputs, a minimal patch following the existing safe-sibling pattern, or a written reproduction script.