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

オープン
#1,335 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
18/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
活発
技術スタック
git, rust
領域
backend

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
Rust
スター
2.1k
フォーク
470
平均マージ
2日 17時間
マージ済み PR(30日)
18

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

rust-lang/git2-rs のほかの issue

rust-lang/git2-rs の issue をすべて見る

似ている issue

Rust の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。