Skip to content

Commit 3207ddb

Browse files
committed
index: fix out-of-bounds read with invalid index entry prefix length
The index format in version 4 has prefix-compressed entries, where every index entry can compress its path by using a path prefix of the previous entry. Since implmenting support for this index format version in commit 5625d86 (index: support index v4, 2016-05-17), though, we do not correctly verify that the prefix length that we want to reuse is actually smaller or equal to the amount of characters than the length of the previous index entry's path. This can lead to a an integer underflow and subsequently to an out-of-bounds read. Fix this by verifying that the prefix is actually smaller than the previous entry's path length. Reported-by: Krishna Ram Prakash R <krp@gtux.in> Reported-by: Vivek Parikh <viv0411.parikh@gmail.com>
1 parent 58a6fe9 commit 3207ddb

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/index.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2365,17 +2365,18 @@ static int read_entry(
23652365
entry_size = index_entry_size(path_length, 0, entry.flags);
23662366
entry.path = (char *)path_ptr;
23672367
} else {
2368-
size_t varint_len;
2369-
size_t strip_len = git_decode_varint((const unsigned char *)path_ptr,
2370-
&varint_len);
2371-
size_t last_len = strlen(last);
2372-
size_t prefix_len = last_len - strip_len;
2373-
size_t suffix_len = strlen(path_ptr + varint_len);
2374-
size_t path_len;
2375-
2376-
if (varint_len == 0)
2368+
size_t varint_len, last_len, prefix_len, suffix_len, path_len;
2369+
uintmax_t strip_len;
2370+
2371+
strip_len = git_decode_varint((const unsigned char *)path_ptr, &varint_len);
2372+
last_len = strlen(last);
2373+
2374+
if (varint_len == 0 || last_len < strip_len)
23772375
return index_error_invalid("incorrect prefix length");
23782376

2377+
prefix_len = last_len - strip_len;
2378+
suffix_len = strlen(path_ptr + varint_len);
2379+
23792380
GITERR_CHECK_ALLOC_ADD(&path_len, prefix_len, suffix_len);
23802381
GITERR_CHECK_ALLOC_ADD(&path_len, path_len, 1);
23812382
tmp_path = git__malloc(path_len);

0 commit comments

Comments
 (0)