commit c7b1723265fdbbef35d01135261fab2ca7d52f88 from: Stefan Sperling date: Fri Jan 28 18:07:47 2022 UTC fix loose object file header parser for zero-length headers ok millert tracey commit - a47330a24a002793ebfd33553aeb80a98f940755 commit + c7b1723265fdbbef35d01135261fab2ca7d52f88 blob - b4f415940ee578ecafa1e8ceca60032674013904 blob + 3b225ca9384c228bc4e2ed56af6e1fbb67ff45d7 --- lib/object_parse.c +++ lib/object_parse.c @@ -196,13 +196,14 @@ got_object_parse_header(struct got_object **obj, char GOT_OBJ_TYPE_TAG, }; int type = 0; - size_t size = 0, hdrlen = 0; + size_t size = 0; size_t i; + char *end; *obj = NULL; - hdrlen = strnlen(buf, len) + 1 /* '\0' */; - if (hdrlen > len) + end = memchr(buf, '\0', len); + if (end == NULL) return got_error(GOT_ERR_BAD_OBJ_HDR); for (i = 0; i < nitems(obj_labels); i++) { @@ -210,12 +211,11 @@ got_object_parse_header(struct got_object **obj, char size_t label_len = strlen(label); const char *errstr; - if (strncmp(buf, label, label_len) != 0) + if (len <= label_len || buf + label_len >= end || + strncmp(buf, label, label_len) != 0) continue; type = obj_types[i]; - if (len <= label_len) - return got_error(GOT_ERR_BAD_OBJ_HDR); size = strtonum(buf + label_len, 0, LONG_MAX, &errstr); if (errstr != NULL) return got_error(GOT_ERR_BAD_OBJ_HDR); @@ -229,7 +229,7 @@ got_object_parse_header(struct got_object **obj, char if (*obj == NULL) return got_error_from_errno("calloc"); (*obj)->type = type; - (*obj)->hdrlen = hdrlen; + (*obj)->hdrlen = end - buf + 1; (*obj)->size = size; return NULL; } @@ -249,6 +249,7 @@ got_object_read_header(struct got_object **obj, int fd buf = malloc(zbsize); if (buf == NULL) return got_error_from_errno("malloc"); + buf[0] = '\0'; err = got_inflate_init(&zb, buf, zbsize, NULL); if (err)