Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ea9e00c

Browse files
committed
pack: make sure we don't go out of bounds for extended entries
A corrupt index might have data that tells us to go look past the end of the file for data. Catch these cases and return an appropriate error message.
1 parent 4395592 commit ea9e00c

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/pack.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1175,6 +1175,7 @@ int git_packfile_alloc(struct git_pack_file **pack_out, const char *path)
11751175
static git_off_t nth_packed_object_offset(const struct git_pack_file *p, uint32_t n)
11761176
{
11771177
const unsigned char *index = p->index_map.data;
1178+
const unsigned char *end = index + p->index_map.len;
11781179
index += 4 * 256;
11791180
if (p->index_version == 1) {
11801181
return ntohl(*((uint32_t *)(index + 24 * n)));
@@ -1185,6 +1186,11 @@ static git_off_t nth_packed_object_offset(const struct git_pack_file *p, uint32_
11851186
if (!(off & 0x80000000))
11861187
return off;
11871188
index += p->num_objects * 4 + (off & 0x7fffffff) * 8;
1189+
1190+
/* Make sure we're not being sent out of bounds */
1191+
if (index >= end - 8)
1192+
return -1;
1193+
11881194
return (((uint64_t)ntohl(*((uint32_t *)(index + 0)))) << 32) |
11891195
ntohl(*((uint32_t *)(index + 4)));
11901196
}
@@ -1264,6 +1270,7 @@ static int pack_entry_find_offset(
12641270
const unsigned char *index = p->index_map.data;
12651271
unsigned hi, lo, stride;
12661272
int pos, found = 0;
1273+
git_off_t offset;
12671274
const unsigned char *current = 0;
12681275

12691276
*offset_out = 0;
@@ -1336,7 +1343,12 @@ static int pack_entry_find_offset(
13361343
if (found > 1)
13371344
return git_odb__error_ambiguous("found multiple offsets for pack entry");
13381345

1339-
*offset_out = nth_packed_object_offset(p, pos);
1346+
if ((offset = nth_packed_object_offset(p, pos)) < 0) {
1347+
giterr_set(GITERR_ODB, "packfile index is corrupt");
1348+
return -1;
1349+
}
1350+
1351+
*offset_out = offset;
13401352
git_oid_fromraw(found_oid, current);
13411353

13421354
#ifdef INDEX_DEBUG_LOOKUP

0 commit comments

Comments
 (0)