Skip to content

Prepare for v2.47.0.windows.2 #5221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
cache-tree: detect mismatching number of index entries
In t4058 we have some tests that exercise git-read-tree(1) when used
with a tree that contains duplicate entries. While the expectation is
that we fail, we ideally should fail gracefully without a segfault.

But that is not the case: we never check that the number of entries in
the cache-tree is less than or equal to the number of entries in the
index. This can lead to an out-of-bounds read as we unconditionally
access `istate->cache[idx]`, where `idx` is controlled by the number of
cache-tree entries and the current position therein. The result is a
segfault.

Fix this segfault by adding a sanity check for the number of index
entries before dereferencing them.

Signed-off-by: Patrick Steinhardt <[email protected]>
Signed-off-by: Junio C Hamano <[email protected]>
  • Loading branch information
pks-t authored and dscho committed Oct 21, 2024
commit ce5ee8d2b1354ea60b68a31c01dfba610304d561
5 changes: 5 additions & 0 deletions cache-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,11 @@ static int verify_one(struct repository *r,
pos = 0;
}

if (it->entry_count + pos > istate->cache_nr) {
ret = error(_("corrupted cache-tree has entries not present in index"));
goto out;
}

i = 0;
while (i < it->entry_count) {
struct cache_entry *ce = istate->cache[pos + i];
Expand Down
12 changes: 6 additions & 6 deletions t/t4058-diff-duplicates.sh
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,15 @@ test_expect_success 'create a few commits' '
rm commit_id up final
'

test_expect_failure 'git read-tree does not segfault' '
test_when_finished rm .git/index.lock &&
test_might_fail git read-tree --reset base
test_expect_success 'git read-tree does not segfault' '
test_must_fail git read-tree --reset base 2>err &&
test_grep "error: corrupted cache-tree has entries not present in index" err
'

test_expect_failure 'reset --hard does not segfault' '
test_when_finished rm .git/index.lock &&
test_expect_success 'reset --hard does not segfault' '
git checkout base &&
test_might_fail git reset --hard
test_must_fail git reset --hard 2>err &&
test_grep "error: corrupted cache-tree has entries not present in index" err
'

test_expect_failure 'git diff HEAD does not segfault' '
Expand Down