Skip to content

Commit 2be7fc0

Browse files
pks-tgitster
authored andcommitted
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]>
1 parent 9f11959 commit 2be7fc0

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

cache-tree.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,11 @@ static int verify_one(struct repository *r,
933933
pos = 0;
934934
}
935935

936+
if (it->entry_count + pos > istate->cache_nr) {
937+
ret = error(_("corrupted cache-tree has entries not present in index"));
938+
goto out;
939+
}
940+
936941
i = 0;
937942
while (i < it->entry_count) {
938943
struct cache_entry *ce = istate->cache[pos + i];

t/t4058-diff-duplicates.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,15 @@ test_expect_success 'create a few commits' '
132132
rm commit_id up final
133133
'
134134

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

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

146146
test_expect_failure 'git diff HEAD does not segfault' '

0 commit comments

Comments
 (0)