Skip to content

Commit 71a57ab

Browse files
committed
Merge branch 'jc/verify-loose-object-header'
Codepaths that read from an on-disk loose object were too loose in validating what they are reading is a proper object file and sometimes read past the data they read from the disk, which has been corrected. H/t to Gustavo Grieco for reporting. * jc/verify-loose-object-header: unpack_sha1_header(): detect malformed object header streaming: make sure to notice corrupt object
2 parents 53eb85e + d21f842 commit 71a57ab

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

sha1_file.c

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,7 +1646,9 @@ unsigned long unpack_object_header_buffer(const unsigned char *buf,
16461646
return used;
16471647
}
16481648

1649-
int unpack_sha1_header(git_zstream *stream, unsigned char *map, unsigned long mapsize, void *buffer, unsigned long bufsiz)
1649+
static int unpack_sha1_short_header(git_zstream *stream,
1650+
unsigned char *map, unsigned long mapsize,
1651+
void *buffer, unsigned long bufsiz)
16501652
{
16511653
/* Get the data stream */
16521654
memset(stream, 0, sizeof(*stream));
@@ -1659,13 +1661,31 @@ int unpack_sha1_header(git_zstream *stream, unsigned char *map, unsigned long ma
16591661
return git_inflate(stream, 0);
16601662
}
16611663

1664+
int unpack_sha1_header(git_zstream *stream,
1665+
unsigned char *map, unsigned long mapsize,
1666+
void *buffer, unsigned long bufsiz)
1667+
{
1668+
int status = unpack_sha1_short_header(stream, map, mapsize,
1669+
buffer, bufsiz);
1670+
1671+
if (status < Z_OK)
1672+
return status;
1673+
1674+
/* Make sure we have the terminating NUL */
1675+
if (!memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer))
1676+
return -1;
1677+
return 0;
1678+
}
1679+
16621680
static int unpack_sha1_header_to_strbuf(git_zstream *stream, unsigned char *map,
16631681
unsigned long mapsize, void *buffer,
16641682
unsigned long bufsiz, struct strbuf *header)
16651683
{
16661684
int status;
16671685

1668-
status = unpack_sha1_header(stream, map, mapsize, buffer, bufsiz);
1686+
status = unpack_sha1_short_header(stream, map, mapsize, buffer, bufsiz);
1687+
if (status < Z_OK)
1688+
return -1;
16691689

16701690
/*
16711691
* Check if entire header is unpacked in the first iteration.
@@ -1756,6 +1776,8 @@ static int parse_sha1_header_extended(const char *hdr, struct object_info *oi,
17561776
*/
17571777
for (;;) {
17581778
char c = *hdr++;
1779+
if (!c)
1780+
return -1;
17591781
if (c == ' ')
17601782
break;
17611783
type_len++;

streaming.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -337,17 +337,17 @@ static open_method_decl(loose)
337337
st->u.loose.mapped = map_sha1_file(sha1, &st->u.loose.mapsize);
338338
if (!st->u.loose.mapped)
339339
return -1;
340-
if (unpack_sha1_header(&st->z,
341-
st->u.loose.mapped,
342-
st->u.loose.mapsize,
343-
st->u.loose.hdr,
344-
sizeof(st->u.loose.hdr)) < 0) {
340+
if ((unpack_sha1_header(&st->z,
341+
st->u.loose.mapped,
342+
st->u.loose.mapsize,
343+
st->u.loose.hdr,
344+
sizeof(st->u.loose.hdr)) < 0) ||
345+
(parse_sha1_header(st->u.loose.hdr, &st->size) < 0)) {
345346
git_inflate_end(&st->z);
346347
munmap(st->u.loose.mapped, st->u.loose.mapsize);
347348
return -1;
348349
}
349350

350-
parse_sha1_header(st->u.loose.hdr, &st->size);
351351
st->u.loose.hdr_used = strlen(st->u.loose.hdr) + 1;
352352
st->u.loose.hdr_avail = st->z.total_out;
353353
st->z_state = z_used;

0 commit comments

Comments
 (0)