unpack_sha1_header(): detect malformed object header

When opening a loose object file, we often do this sequence: - prepare a short buffer for the object header (on stack) - call unpack_sha1_header() and have early part of the object data inflated, enough to fill the buffer - parse that data in the short buffer, assuming that the first part of the object is <typename> SP <length> NUL Because the parsing function parse_sha1_header_extended() is not given the number of bytes inflated into the header buffer, it you craft a file whose early part inflates a garbage sequence without SP or NUL, and replace a loose object with it, it will end up reading past the end of the inflated data. To correct this, do the following four things: - rename unpack_sha1_header() to unpack_sha1_short_header() and have unpack_sha1_header_to_strbuf() keep calling that as its helper function. This will detect and report zlib errors, but is not aware of the format of a loose object (as before). - introduce unpack_sha1_header() that calls the same helper function, and when zlib reports it inflated OK into the buffer, check if the inflated data has NUL. This would ensure that parsing function will terminate within the buffer that holds the inflated header. - update unpack_sha1_header_to_strbuf() to check if the resulting buffer has NUL for the same effect. - update parse_sha1_header_extended() to make sure that its loop to find the SP that terminates the <typename> stops at NUL. Essentially, this makes unpack_*() functions that are asked to unpack a loose object header to be a bit more strict and detect an input that cannot possibly be a valid object header, even before the parsing function kicks in. Reported-by: Gustavo Grieco <gustavo.grieco@imag.fr> Helped-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Sep 25, 2016 at 21:29 UTC d21f8426907e84465ab54df5b05bc81057f448d9
1 file changed +24 -2
sha1_file.c
+24 -2
@@ -1566,7 +1566,9 @@ unsigned long unpack_object_header_buffer(const unsigned char *buf,
1566 return used;
1567 }
1568
1569 -int unpack_sha1_header(git_zstream *stream, unsigned char *map, unsigned long mapsize, void *buffer, unsigned long bufsiz)
1569 +static int unpack_sha1_short_header(git_zstream *stream,
1570 + unsigned char *map, unsigned long mapsize,
1571 + void *buffer, unsigned long bufsiz)
1572 {
1573 /* Get the data stream */
1574 memset(stream, 0, sizeof(*stream));
@@ -1579,13 +1581,31 @@ int unpack_sha1_header(git_zstream *stream, unsigned char *map, unsigned long ma
1581 return git_inflate(stream, 0);
1582 }
1583
1584 +int unpack_sha1_header(git_zstream *stream,
1585 + unsigned char *map, unsigned long mapsize,
1586 + void *buffer, unsigned long bufsiz)
1587 +{
1588 + int status = unpack_sha1_short_header(stream, map, mapsize,
1589 + buffer, bufsiz);
1590 +
1591 + if (status < Z_OK)
1592 + return status;
1593 +
1594 + /* Make sure we have the terminating NUL */
1595 + if (!memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer))
1596 + return -1;
1597 + return 0;
1598 +}
1599 +
1600 static int unpack_sha1_header_to_strbuf(git_zstream *stream, unsigned char *map,
1601 unsigned long mapsize, void *buffer,
1602 unsigned long bufsiz, struct strbuf *header)
1603 {
1604 int status;
1605
1588 - status = unpack_sha1_header(stream, map, mapsize, buffer, bufsiz);
1606 + status = unpack_sha1_short_header(stream, map, mapsize, buffer, bufsiz);
1607 + if (status < Z_OK)
1608 + return -1;
1609
1610 /*
1611 * Check if entire header is unpacked in the first iteration.
@@ -1676,6 +1696,8 @@ static int parse_sha1_header_extended(const char *hdr, struct object_info *oi,
1696 */
1697 for (;;) {
1698 char c = *hdr++;
1699 + if (!c)
1700 + return -1;
1701 if (c == ' ')
1702 break;
1703 type_len++;