`IndexEntry::from_raw` trusts the path length stored in `flags` (out-of-bounds read on a malformed index file)

Aperta Adatta ai principianti
#1,366 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
85/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
c, rust
Ambito
security

Direzione di ricerca

Inizia da IndexEntry::from_raw e dalla sua implementazione di Binding in src/index.rs, in particolare dalla gestione della lunghezza del percorso intorno alle righe 855–872. Confronta la lunghezza dei flag memorizzati con il percorso in memoria terminato con NUL prima di costruire lo slice. Fatto significa che le voci di indice malformate non possono causare una lettura fuori dai limiti, con un test di regressione che copra una lunghezza memorizzata maggiore del percorso effettivo.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

impl Binding for IndexEntry (src/index.rs:855-860) takes the path length from the low 12 bits of
git_index_entry::flags and builds slice::from_raw_parts(path, pathlen); only the saturated value
0xFFF falls back to the NUL terminator. For an entry read from an index file, libgit2 keeps the
flags value as stored in the file, while the in-memory path is allocated from the string's actual
length. When the two disagree, the slice extends past the allocation, and path.to_vec() copies
those bytes into IndexEntry::path. This is reachable from safe code through Index::get,
Index::iter, Index::get_path and the conflict iterators, on any index file that libgit2 accepts.

git2-rs main at b9d59b6 (git2 0.21.0) with libgit2 1.9.7, under AddressSanitizer, with a
one-entry index file whose stored length is larger than its path:

ERROR: AddressSanitizer: heap-buffer-overflow ... READ of size 4080
    #6 <git2::index::IndexEntry as git2::util::Binding>::from_raw src/index.rs:872:24
    #7 <git2::index::Index>::get src/index.rs:417:27
0x... is located 0 bytes after 82-byte region
allocated by thread T0 here:
    #4 index_entry_create libgit2/src/libgit2/index.c:964:10
    #6 read_entry libgit2/src/libgit2/index.c:2662:6

Possible fix

libgit2 NUL-terminates entry->path in memory, so from_raw can always use
CStr::from_ptr(path).to_bytes() and ignore the length bits, or clamp the length to it.

A standalone Cargo reproducer (safe code only) and the full sanitizer trace are available on request.

Found by Crustify, an
experimental UB/soundness auditing agent developed at UC Berkeley and running on
Claude Opus 4.5, then manually reviewed and independently reproduced.

Lingua principale
Rust
Stelle
2.1k
Fork
470
Merge medio
2g 17h
PR unite (30g)
18

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di rust-lang/git2-rs

Tutte le issue di rust-lang/git2-rs

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.