# `Repository::rebase` does not keep `RebaseOptions` borrowed, but libgit2 keeps pointers into them (use-after-free in safe code)

Abierto
#1,335 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
18/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
git, rust
Área
backend

Línea de trabajo

The bug is in src/repo.rs (Repository::rebase and open_rebase, around lines 2410-2444), which borrow RebaseOptions only for the call, and in src/build.rs (CheckoutBuilder::configure and progress_cb, around lines 567-611), which hands libgit2 pointers into the options. Start by reproducing the use-after-free with the snippet, ideally under AddressSanitizer. Done means the signature ties the Rebase lifetime to the options (or Rebase owns the checkout state) so the snippet no longer compiles unsafely, with existing rebase tests passing.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

git_rebase_init and git_rebase_open store a shallow copy of the git_rebase_options they are
given (rebase_alloc, rebase.c:292) and use its checkout options again in git_rebase_next
(rebase.c:764) and git_rebase_abort (:1206). RebaseOptions::raw fills those through
CheckoutBuilder::configure (src/build.rs:567-600), so the callback payloads point at the
CheckoutBuilder inside the RebaseOptions, and the path, target directory and label pointers
point at its strings. Repository::rebase and open_rebase (src/repo.rs:2410-2444) borrow the
options only for the call, so safe code can drop or move them while the Rebase is alive:

#![forbid(unsafe_code)]
use git2::build::CheckoutBuilder;
use git2::{RebaseOptions, Repository, Signature};

fn main() {
    let dir = std::env::temp_dir().join(format!("rebase-opts-uaf-{}", std::process::id()));
    let repo = Repository::init(&dir).unwrap();
    let sig = Signature::now("a", "[email protected]").unwrap();
    let tree = repo.find_tree(repo.index().unwrap().write_tree().unwrap()).unwrap();
    let base = repo.commit(Some("HEAD"), &sig, &sig, "base", &tree, &[]).unwrap();
    let base = repo.find_commit(base).unwrap();
    let tip = repo.commit(Some("HEAD"), &sig, &sig, "tip", &tree, &[&base]).unwrap();
    let branch = repo.find_annotated_commit(tip).unwrap();
    let upstream = repo.find_annotated_commit(base.id()).unwrap();

    let mut checkout = CheckoutBuilder::new();
    checkout.progress(|_path, cur, total| println!("checkout progress {cur}/{total}"));
    let mut opts = Box::new(RebaseOptions::new());
    opts.checkout_options(checkout);

    let mut rebase = repo.rebase(Some(&branch), Some(&upstream), None, Some(&mut opts)).unwrap();
    drop(opts);
    rebase.next().unwrap().unwrap(); // progress_cb runs with the freed CheckoutBuilder as payload
}

With git2-rs at b9d59b6 (main), vendored libgit2 1.9.7, under AddressSanitizer (trimmed):

ERROR: AddressSanitizer: heap-use-after-free on address 0x6ee7561f70b8
READ of size 8 at 0x6ee7561f70b8 thread T0
    #0 git2::build::progress_cb::{closure#0} src/build.rs:611:30
    #7 report_progress libgit2/src/libgit2/checkout.c:1722:3
    #9 git_checkout_index libgit2/src/libgit2/checkout.c:2734:11
    #10 rebase_next_merge libgit2/src/libgit2/rebase.c:836:12
    #11 git_rebase_next libgit2/src/libgit2/rebase.c:924:11
0x6ee7561f70b8 is located 184 bytes inside of 488-byte region [0x6ee7561f7000,0x6ee7561f71e8)
freed by thread T0 here:
    #5 main src/main.rs:34:5

Without a sanitizer the program exits normally and the callback still runs through the dangling
payload.

Possible fix

Keep the options borrowed for as long as the rebase lives, e.g.
pub fn rebase<'a>(&'a self, .., opts: Option<&'a mut RebaseOptions<'_>>) -> Result<Rebase<'a>, Error>
and the same for open_rebase, or have Rebase own the checkout state that libgit2 points at.

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

Lenguaje dominante
Rust
Estrellas
2.1k
Forks
470
Merge medio
2 d 17 h
PR fusionados (30 d)
18

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de rust-lang/git2-rs

Todos los issues de rust-lang/git2-rs

Issues similares

Más issues de Rust

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.