Skip to content

Commit

Permalink
Merge pull request #439: sparse index: fix use-after-free bug in cach…
Browse files Browse the repository at this point in the history
…e_tree_verify()

This is a copy of [the v2 patch sent by Philip Wood](https://lore.kernel.org/git/pull.1053.v2.git.1633600244854.gitgitgadget@gmail.com/T/#u). This fixes a possible segfault when a `git rebase --apply` creates a full cache tree in a sparse index.

There is a slight change from Philip's patch that makes the test more robust to verbose output.
  • Loading branch information
derrickstolee authored Oct 7, 2021
2 parents 6df9339 + e9317e5 commit 7e17ee3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
37 changes: 29 additions & 8 deletions cache-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -923,32 +923,48 @@ static void verify_one_sparse(struct repository *r,
path->buf);
}

static void verify_one(struct repository *r,
struct index_state *istate,
struct cache_tree *it,
struct strbuf *path)
/*
* Returns:
* 0 - Verification completed.
* 1 - Restart verification - a call to ensure_full_index() freed the cache
* tree that is being verified and verification needs to be restarted from
* the new toplevel cache tree.
*/
static int verify_one(struct repository *r,
struct index_state *istate,
struct cache_tree *it,
struct strbuf *path)
{
int i, pos, len = path->len;
struct strbuf tree_buf = STRBUF_INIT;
struct object_id new_oid;

for (i = 0; i < it->subtree_nr; i++) {
strbuf_addf(path, "%s/", it->down[i]->name);
verify_one(r, istate, it->down[i]->cache_tree, path);
if (verify_one(r, istate, it->down[i]->cache_tree, path))
return 1;
strbuf_setlen(path, len);
}

if (it->entry_count < 0 ||
/* no verification on tests (t7003) that replace trees */
lookup_replace_object(r, &it->oid) != &it->oid)
return;
return 0;

if (path->len) {
/*
* If the index is sparse and the cache tree is not
* index_name_pos() may trigger ensure_full_index() which will
* free the tree that is being verified.
*/
int is_sparse = istate->sparse_index;
pos = index_name_pos(istate, path->buf, path->len);
if (is_sparse && !istate->sparse_index)
return 1;

if (pos >= 0) {
verify_one_sparse(r, istate, it, path, pos);
return;
return 0;
}

pos = -pos - 1;
Expand Down Expand Up @@ -996,6 +1012,7 @@ static void verify_one(struct repository *r,
oid_to_hex(&new_oid), oid_to_hex(&it->oid));
strbuf_setlen(path, len);
strbuf_release(&tree_buf);
return 0;
}

void cache_tree_verify(struct repository *r, struct index_state *istate)
Expand All @@ -1004,6 +1021,10 @@ void cache_tree_verify(struct repository *r, struct index_state *istate)

if (!istate->cache_tree)
return;
verify_one(r, istate, istate->cache_tree, &path);
if (verify_one(r, istate, istate->cache_tree, &path)) {
strbuf_reset(&path);
if (verify_one(r, istate, istate->cache_tree, &path))
BUG("ensure_full_index() called twice while verifying cache tree");
}
strbuf_release(&path);
}
2 changes: 1 addition & 1 deletion t/t1092-sparse-checkout-compatibility.sh
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,7 @@ test_expect_success 'read-tree --merge with directory-file conflicts' '
test_expect_success 'merge, cherry-pick, and rebase' '
init_repos &&
for OPERATION in "merge -m merge" cherry-pick rebase
for OPERATION in "merge -m merge" cherry-pick "rebase -q --apply" "rebase --merge"
do
test_all_match git checkout -B temp update-deep &&
test_all_match git $OPERATION update-folder1 &&
Expand Down

0 comments on commit 7e17ee3

Please sign in to comment.