diff --git a/deps/zlib/contrib/minizip/README.chromium b/deps/zlib/contrib/minizip/README.chromium index 98a7a72a4299..b09f973dc9e7 100644 --- a/deps/zlib/contrib/minizip/README.chromium +++ b/deps/zlib/contrib/minizip/README.chromium @@ -39,7 +39,3 @@ Local Modifications: - Added support for zip64 archives that have extra bytes on front (for example, large CRX files). 0018-support-prefixed-zip64.patch - -- Added stricter parsing for zip64 candidates to avoid an issue where a zip64 - could be embedded in a non-zip64 zip. - 0019-fix-zip64-in-zip.patch diff --git a/deps/zlib/contrib/minizip/crypt.h b/deps/zlib/contrib/minizip/crypt.h index dd548938a4fd..d23cc6a0b78c 100644 --- a/deps/zlib/contrib/minizip/crypt.h +++ b/deps/zlib/contrib/minizip/crypt.h @@ -77,11 +77,10 @@ static void init_keys(const char* passwd, unsigned long* pkeys, const z_crc_t* p #ifdef INCLUDECRYPTINGCODE_IFCRYPTALLOWED #define RAND_HEAD_LEN 12 - /* "last resort" source for second part of crypt seed pattern */ -# ifndef ZCR_SEED2 -# define ZCR_SEED2 3141592654UL /* use PI as default pattern */ -# endif +/* The random header bytes come from the PCG32 generator in skipset.h, which + must be included before this file when INCLUDECRYPTINGCODE_IFCRYPTALLOWED is + defined. */ static unsigned crypthead(const char* passwd, /* password string */ unsigned char* buf, /* where to write header */ int bufSize, @@ -92,23 +91,25 @@ static unsigned crypthead(const char* passwd, /* password string */ int t; /* temporary */ int c; /* random byte */ unsigned char header[RAND_HEAD_LEN-2]; /* random header */ - static unsigned calls = 0; /* ensure different random header each time */ + static int seeded = 0; /* true once gen has been seeded */ + static set_rand_t gen; /* random header generator, persists so that + each header is different */ if (bufSize> 7) & 0xff; + c = (int)(set_rand(&gen) >> 24); header[n] = (unsigned char)zencode(pkeys, pcrc_32_tab, c, t); } /* Encrypt random header (last two bytes is high word of crc) */ diff --git a/deps/zlib/contrib/minizip/ioapi.h b/deps/zlib/contrib/minizip/ioapi.h index a5211239ea2a..8dc78ae6eed9 100644 --- a/deps/zlib/contrib/minizip/ioapi.h +++ b/deps/zlib/contrib/minizip/ioapi.h @@ -21,7 +21,7 @@ #ifndef ZLIBIOAPI64_H #define ZLIBIOAPI64_H -#if (!defined(_WIN32)) && (!defined(WIN32)) && (!defined(__APPLE__)) && (!(defined(__ANDROID_API__) || __ANDROID_API__ >= 24)) +#if (!defined(_WIN32)) && (!defined(WIN32)) && (!defined(__APPLE__)) && (!defined(__ANDROID_API__) || __ANDROID_API__ >= 24) /* Linux needs this to support file operation on files larger then 4+GB */ /* But might need better if/def to select just the platforms that needs them.*/ diff --git a/deps/zlib/contrib/minizip/miniunz.c b/deps/zlib/contrib/minizip/miniunz.c index e8a5c726c4c7..07a11a11b13d 100644 --- a/deps/zlib/contrib/minizip/miniunz.c +++ b/deps/zlib/contrib/minizip/miniunz.c @@ -184,6 +184,59 @@ static int makedir(const char *newdir) { return 1; } +/* Copy to out a version of the path name that can only be created under the + extraction directory. Both '/' and '\\' are taken as separators. Empty and + "." path components are removed, and a ".." component is removed along with + the component before it, if any. On Windows, drive specifiers at the start + and components of only dots and spaces are also removed. The result is + always a relative path, and is no longer than name. out must have room for + strlen(name) + 1 characters. */ +static void safe_path(char* out, const char* name) { + char* start = out; + while (*name != '\0') + { + const char* comp = name; + size_t len; + while (*name != '\0' && *name != '/' && *name != '\\') + name++; + len = (size_t)(name - comp); +#ifdef _WIN32 + while (out == start && len >= 2 && comp[1] == ':') + { + comp += 2; /* remove drive specifiers, e.g. "C:" */ + len -= 2; + } + { + /* Windows drops trailing dots and spaces from a name, so remove + any other component made of only those, such as "..." */ + size_t i = 0; + while (i < len && (comp[i] == '.' || comp[i] == ' ')) + i++; + if (i == len && !(len == 2 && comp[0] == '.' && comp[1] == '.')) + len = 0; + } +#endif + if (len == 2 && comp[0] == '.' && comp[1] == '.') + { + /* remove the previous component and its separator, if any */ + if (out > start) + out--; + while (out > start && out[-1] != '/' && out[-1] != '\\') + out--; + } + else if (len != 0 && !(len == 1 && comp[0] == '.')) + { + memcpy(out, comp, len); + out += len; + if (*name != '\0') + *out++ = *name; /* keep the separator */ + } + if (*name != '\0') + name++; + } + *out = '\0'; +} + static void do_banner(void) { printf("MiniUnz 1.1, demo of zLib + Unz package written by Gilles Vollant\n"); printf("more info at https://www.winimage.com/zLibDll/unzip.html\n\n"); @@ -267,7 +320,7 @@ static int do_list(unzFile uf) { string_method="Defl:N"; else if (iLevel==1) string_method="Defl:X"; - else if ((iLevel==2) || (iLevel==3)) + else /* iLevel==2 || iLevel==3 */ string_method="Defl:F"; /* 2:fast , 3 : extra fast*/ } else @@ -305,6 +358,7 @@ static int do_list(unzFile uf) { static int do_extract_currentfile(unzFile uf, const int* popt_extract_without_path, int* popt_overwrite, const char* password) { char filename_inzip[65536+1]; + char write_name[65536+1]; char* filename_withoutpath; char* p; int err=UNZ_OK; @@ -339,36 +393,28 @@ static int do_extract_currentfile(unzFile uf, const int* popt_extract_without_pa if ((*filename_withoutpath)=='\0') { - if ((*popt_extract_without_path)==0) + safe_path(write_name, filename_inzip); + if (((*popt_extract_without_path)==0) && (write_name[0]!='\0')) { - printf("creating directory: %s\n",filename_inzip); - mymkdir(filename_inzip); + printf("creating directory: %s\n",write_name); + mymkdir(write_name); } } else { - const char* write_filename; + const char* write_filename = write_name; int skip=0; if ((*popt_extract_without_path)==0) - write_filename = filename_inzip; + safe_path(write_name, filename_inzip); else - write_filename = filename_withoutpath; - - if (write_filename[0]!='\0') + safe_path(write_name, filename_withoutpath); + if (write_name[0]=='\0') { - const char* relative_check = write_filename; - while (relative_check[1]!='\0') - { - if (relative_check[0]=='.' && relative_check[1]=='.') - write_filename = relative_check; - relative_check++; - } + printf("skipping %s: no name left to extract to\n",filename_inzip); + skip = 1; } - while (write_filename[0]=='/' || write_filename[0]=='.') - write_filename++; - err = unzOpenCurrentFilePassword(uf,password); if (err!=UNZ_OK) { @@ -412,14 +458,22 @@ static int do_extract_currentfile(unzFile uf, const int* popt_extract_without_pa { fout=FOPEN_FUNC(write_filename,"wb"); /* some zipfile don't contain directory alone before file */ - if ((fout==NULL) && ((*popt_extract_without_path)==0) && - (filename_withoutpath!=(char*)filename_inzip)) + if ((fout==NULL) && ((*popt_extract_without_path)==0)) { - char c=*(filename_withoutpath-1); - *(filename_withoutpath-1)='\0'; - makedir(write_filename); - *(filename_withoutpath-1)=c; - fout=FOPEN_FUNC(write_filename,"wb"); + /* make the directories leading up to the file */ + char* sep = NULL; + char* q; + for (q = write_name; *q != '\0'; q++) + if (*q == '/' || *q == '\\') + sep = q; + if (sep != NULL) + { + char c = *sep; + *sep = '\0'; + makedir(write_name); + *sep = c; + fout=FOPEN_FUNC(write_filename,"wb"); + } } if (fout==NULL) diff --git a/deps/zlib/contrib/minizip/mztools.c b/deps/zlib/contrib/minizip/mztools.c index 1e2bb56853a7..f88060f847ec 100644 --- a/deps/zlib/contrib/minizip/mztools.c +++ b/deps/zlib/contrib/minizip/mztools.c @@ -16,7 +16,7 @@ #define READ_8(adr) ((unsigned char)*(adr)) #define READ_16(adr) ( READ_8(adr) | (READ_8(adr+1) << 8) ) -#define READ_32(adr) ( READ_16(adr) | (READ_16((adr)+2) << 16) ) +#define READ_32(adr) ( (uLong)READ_16(adr) | ((uLong)READ_16((adr)+2) << 16) ) #define WRITE_8(buff, n) do { \ *((unsigned char*)(buff)) = (unsigned char) ((n) & 0xff); \ diff --git a/deps/zlib/contrib/minizip/unzip.c b/deps/zlib/contrib/minizip/unzip.c index c5e69721a8f7..fc921557d661 100644 --- a/deps/zlib/contrib/minizip/unzip.c +++ b/deps/zlib/contrib/minizip/unzip.c @@ -347,7 +347,7 @@ local ZPOS64_T unz64local_SearchCentralDir(const zlib_filefunc64_32_def* pzlib_f unsigned char* buf; ZPOS64_T uSizeFile; ZPOS64_T uBackRead; - ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */ + ZPOS64_T uMaxBack=0xffff+22; /* maximum comment and end-of-central-directory record */ ZPOS64_T uPosFound=CENTRALDIRINVALID; if (ZSEEK64(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0) @@ -383,7 +383,7 @@ local ZPOS64_T unz64local_SearchCentralDir(const zlib_filefunc64_32_def* pzlib_f if (ZREAD64(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize) break; - for (i=(int)uReadSize-3; (i--)>0;) + for (i=(int)uReadSize-4; i>=0; --i) if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) && ((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06)) { @@ -400,91 +400,29 @@ local ZPOS64_T unz64local_SearchCentralDir(const zlib_filefunc64_32_def* pzlib_f /* - Locate the Central directory 64 of a zipfile (at the end, just before - the global comment) + Locate the ZIP64 end of central directory record using the locator immediately + preceding the classic end of central directory record. */ local ZPOS64_T unz64local_SearchCentralDir64(const zlib_filefunc64_32_def* pzlib_filefunc_def, - voidpf filestream) { - unsigned char* buf; - ZPOS64_T uSizeFile; - ZPOS64_T uBackRead; - ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */ - ZPOS64_T uPosFound=CENTRALDIRINVALID; + voidpf filestream, + ZPOS64_T centralDirPos) { + ZPOS64_T uPosFound; uLong uL; - ZPOS64_T relativeOffset; - - if (ZSEEK64(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0) - return CENTRALDIRINVALID; - - - uSizeFile = ZTELL64(*pzlib_filefunc_def,filestream); - - if (uMaxBack>uSizeFile) - uMaxBack = uSizeFile; - - buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4); - if (buf==NULL) - return CENTRALDIRINVALID; - - uBackRead = 4; - while (uBackReaduMaxBack) - uBackRead = uMaxBack; - else - uBackRead+=BUFREADCOMMENT; - uReadPos = uSizeFile-uBackRead ; - - uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ? - (BUFREADCOMMENT+4) : (uLong)(uSizeFile-uReadPos); - if (ZSEEK64(*pzlib_filefunc_def,filestream,uReadPos,ZLIB_FILEFUNC_SEEK_SET)!=0) - break; - - if (ZREAD64(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize) - break; - - /* Search for the non-zip64 EoCDR and confirm zip64 EoCDL is 20 bytes - earlier. This avoids false positives if the file is a non-zip64 zip - but contains an uncompressed zip64 near its end. Note: zip64 EoCDL is - 20 bytes long. */ - for (i=(int)uReadSize-3; (i--)>20;) - // End of central directory record signature (PK\5\6) - if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) && - ((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06)) - { - // Zip64 end of central directory locator signature (PK\6\7) - if (((*(buf+i-20))==0x50) && ((*(buf+i+1-20))==0x4b) && - ((*(buf+i+2-20))==0x06) && ((*(buf+i+3-20))==0x07)) - { - uPosFound = uReadPos+(unsigned)i-20; - break; - } - else - { - /* This is a non-zip64 zip; abandon the search. */ - free(buf); - return CENTRALDIRINVALID; - } - } + ZPOS64_T relativeOffset; - if (uPosFound!=CENTRALDIRINVALID) - break; - } - free(buf); - if (uPosFound == CENTRALDIRINVALID) + /* The ZIP64 locator immediately precedes the classic EOCD. Checking only + this position prevents a locator signature in file data from overriding + the central directory referenced by the actual EOCD. */ + if (centralDirPos == CENTRALDIRINVALID || centralDirPos < 20) return CENTRALDIRINVALID; - /* Zip64 end of central directory locator */ + uPosFound = centralDirPos - 20; if (ZSEEK64(*pzlib_filefunc_def,filestream, uPosFound,ZLIB_FILEFUNC_SEEK_SET)!=0) return CENTRALDIRINVALID; - /* the signature, already checked */ - if (unz64local_getLong(pzlib_filefunc_def,filestream,&uL)!=UNZ_OK) + if (unz64local_getLong(pzlib_filefunc_def,filestream,&uL)!=UNZ_OK || + uL != 0x07064b50) return CENTRALDIRINVALID; - /* number of the disk with the start of the zip64 end of central directory */ if (unz64local_getLong(pzlib_filefunc_def,filestream,&uL)!=UNZ_OK) return CENTRALDIRINVALID; @@ -507,7 +445,7 @@ local ZPOS64_T unz64local_SearchCentralDir64(const zlib_filefunc64_32_def* pzlib if (relativeOffset > uPosFound) return CENTRALDIRINVALID; const int BUFSIZE = 1024 * 4; - buf = (unsigned char*)ALLOC(BUFSIZE); + unsigned char* buf = (unsigned char*)ALLOC(BUFSIZE); if (buf==NULL) return CENTRALDIRINVALID; // Zip64 EOCDR is at least 48 bytes long. @@ -570,6 +508,7 @@ local unzFile unzOpenInternal(const void *path, unz64_s us; unz64_s *s; ZPOS64_T central_pos; + ZPOS64_T central_pos64; uLong uL; uLong number_disk; /* number of the current disk, used for @@ -602,12 +541,16 @@ local unzFile unzOpenInternal(const void *path, if (us.filestream==NULL) return NULL; - central_pos = unz64local_SearchCentralDir64(&us.z_filefunc,us.filestream); - if (central_pos!=CENTRALDIRINVALID) + central_pos = unz64local_SearchCentralDir(&us.z_filefunc,us.filestream); + central_pos64 = unz64local_SearchCentralDir64(&us.z_filefunc, + us.filestream, + central_pos); + if (central_pos64!=CENTRALDIRINVALID) { uLong uS; ZPOS64_T uL64; + central_pos = central_pos64; us.isZip64 = 1; if (ZSEEK64(us.z_filefunc, us.filestream, @@ -664,7 +607,6 @@ local unzFile unzOpenInternal(const void *path, } else { - central_pos = unz64local_SearchCentralDir(&us.z_filefunc,us.filestream); if (central_pos==CENTRALDIRINVALID) err=UNZ_ERRNO; @@ -857,6 +799,8 @@ local int unz64local_GetCurrentFileInfoInternal(unzFile file, uLong uMagic; long lSeek=0; uLong uL; + unsigned zip64_required; + unsigned zip64_consumed = 0; uLong uFileNameCrc; file_info.size_utf8_filename = 0; @@ -931,6 +875,17 @@ local int unz64local_GetCurrentFileInfoInternal(unzFile file, err=UNZ_ERRNO; file_info_internal.offset_curfile = uL; + + /* Remember ZIP64 requirements before replacing any values. */ + if (err != UNZ_OK) + return err; + + zip64_required = + (file_info.uncompressed_size == MAXU32 ? 1U : 0U) | + (file_info.compressed_size == MAXU32 ? 2U : 0U) | + (file_info_internal.offset_curfile == MAXU32 ? 4U : 0U) | + (file_info.disk_num_start == 0xffff ? 8U : 0U); + lSeek+=file_info.size_filename; if (err==UNZ_OK) { @@ -1007,109 +962,116 @@ local int unz64local_GetCurrentFileInfoInternal(unzFile file, while(acc < file_info.size_file_extra) { uLong headerId; - uLong dataSize; - - if (unz64local_getShort(&s->z_filefunc, s->filestream,&headerId) != UNZ_OK) - err=UNZ_ERRNO; + uLong dataSize; + uLong used = 0; + uLong remaining = file_info.size_file_extra - acc; + + /* Every extra-field record starts with four bytes. */ + if (remaining < 4) + return UNZ_BADZIPFILE; + + if (unz64local_getShort(&s->z_filefunc, s->filestream, + &headerId) != UNZ_OK || + unz64local_getShort(&s->z_filefunc, s->filestream, + &dataSize) != UNZ_OK) + return UNZ_ERRNO; - if (unz64local_getShort(&s->z_filefunc, s->filestream,&dataSize) != UNZ_OK) - err=UNZ_ERRNO; + remaining -= 4; + if (dataSize > remaining) + return UNZ_BADZIPFILE; - /* ZIP64 extra fields */ + /* ZIP64 values must fit within this record. */ if (headerId == 0x0001) { - if(file_info.uncompressed_size == MAXU32) + if ((zip64_required & 1U) && !(zip64_consumed & 1U)) { - if (unz64local_getLong64(&s->z_filefunc, s->filestream,&file_info.uncompressed_size) != UNZ_OK) - err=UNZ_ERRNO; + if (dataSize - used < 8) + return UNZ_BADZIPFILE; + if (unz64local_getLong64(&s->z_filefunc, s->filestream, + &file_info.uncompressed_size) != UNZ_OK) + return UNZ_ERRNO; + used += 8; + zip64_consumed |= 1U; } - if(file_info.compressed_size == MAXU32) + if ((zip64_required & 2U) && !(zip64_consumed & 2U)) { - if (unz64local_getLong64(&s->z_filefunc, s->filestream,&file_info.compressed_size) != UNZ_OK) - err=UNZ_ERRNO; + if (dataSize - used < 8) + return UNZ_BADZIPFILE; + if (unz64local_getLong64(&s->z_filefunc, s->filestream, + &file_info.compressed_size) != UNZ_OK) + return UNZ_ERRNO; + used += 8; + zip64_consumed |= 2U; } - if(file_info_internal.offset_curfile == MAXU32) + if ((zip64_required & 4U) && !(zip64_consumed & 4U)) { - /* Relative Header offset */ - if (unz64local_getLong64(&s->z_filefunc, s->filestream,&file_info_internal.offset_curfile) != UNZ_OK) - err=UNZ_ERRNO; + if (dataSize - used < 8) + return UNZ_BADZIPFILE; + if (unz64local_getLong64(&s->z_filefunc, s->filestream, + &file_info_internal.offset_curfile) != UNZ_OK) + return UNZ_ERRNO; + used += 8; + zip64_consumed |= 4U; } - if(file_info.disk_num_start == 0xffff) + if ((zip64_required & 8U) && !(zip64_consumed & 8U)) { - /* Disk Start Number */ - if (unz64local_getLong(&s->z_filefunc, s->filestream,&file_info.disk_num_start) != UNZ_OK) - err=UNZ_ERRNO; + if (dataSize - used < 4) + return UNZ_BADZIPFILE; + if (unz64local_getLong(&s->z_filefunc, s->filestream, + &file_info.disk_num_start) != UNZ_OK) + return UNZ_ERRNO; + used += 4; + zip64_consumed |= 8U; } - } else if (headerId == 0x7075) /* Info-ZIP Unicode Path Extra Field */ { int version = 0; + uLong uCrc; - if (dataSize < 1 + 4) + /* dataSize includes version (1 byte), uCrc (4 bytes), and + * the filename data. If it's too small, the filename size + * below would overflow. */ + if (dataSize >= 1 + 4) { - /* dataSize includes version (1 byte), uCrc (4 bytes), and - * the filename data. If it's too small, fileNameSize below - * would overflow. */ - if (ZSEEK64(s->z_filefunc, s->filestream, dataSize, ZLIB_FILEFUNC_SEEK_CUR) != 0) - { - err = UNZ_ERRNO; - } - } - else if (unz64local_getByte(&s->z_filefunc, s->filestream, &version) != UNZ_OK) - { - err = UNZ_ERRNO; - } - else if (version != 1) - { - if (ZSEEK64(s->z_filefunc, s->filestream,dataSize - 1, ZLIB_FILEFUNC_SEEK_CUR) != 0) - { - err = UNZ_ERRNO; - } + if (unz64local_getByte(&s->z_filefunc, s->filestream, &version) != UNZ_OK) + return UNZ_ERRNO; + used += 1; } - else - { - uLong uCrc, utf8FileNameSize; + if (version == 1) + { if (unz64local_getLong(&s->z_filefunc, s->filestream, &uCrc) != UNZ_OK) - { - err = UNZ_ERRNO; - } - utf8FileNameSize = dataSize - (1 + 4); /* 1 for version, 4 for uCrc */ + return UNZ_ERRNO; + used += 4; /* Check CRC against file name in the header. */ - if (uCrc != uFileNameCrc) - { - if (ZSEEK64(s->z_filefunc, s->filestream, utf8FileNameSize, ZLIB_FILEFUNC_SEEK_CUR) != 0) - { - err = UNZ_ERRNO; - } - } - else + if (uCrc == uFileNameCrc) { - file_info.size_utf8_filename = utf8FileNameSize; + file_info.size_utf8_filename = dataSize - used; if (file_info.size_utf8_filename > 0) { if (ZREAD64(s->z_filefunc, s->filestream, file_info.utf8_filename, file_info.size_utf8_filename) != file_info.size_utf8_filename) - { - err = UNZ_ERRNO; - } + return UNZ_ERRNO; } file_info.utf8_filename[file_info.size_utf8_filename] = '\0'; + used += file_info.size_utf8_filename; } } } - else - { - if (ZSEEK64(s->z_filefunc, s->filestream,dataSize,ZLIB_FILEFUNC_SEEK_CUR)!=0) - err=UNZ_ERRNO; - } - acc += 2 + 2 + dataSize; + /* Skip remaining bytes, but never leave this record. */ + if (used < dataSize && + ZSEEK64(s->z_filefunc, s->filestream, + dataSize - used, + ZLIB_FILEFUNC_SEEK_CUR) != 0) + return UNZ_ERRNO; + + acc += 4 + dataSize; } } diff --git a/deps/zlib/contrib/minizip/unzip.h b/deps/zlib/contrib/minizip/unzip.h index 05efa64ea477..5b5e820a4403 100644 --- a/deps/zlib/contrib/minizip/unzip.h +++ b/deps/zlib/contrib/minizip/unzip.h @@ -325,9 +325,10 @@ extern int ZEXPORT unzGetCurrentFileInfo(unzFile file, if szComment!=NULL, the comment string of the file will be copied in szComment (commentBufferSize is the size of the buffer) The file name and comment will be zero-terminated if there is room in the - provided buffer. Otherwise the buffer will contain as much as will fit. If at - least 65537 bytes of room is provided, then the result will always be - complete and zero-terminated. + provided buffer. Otherwise the buffer will contain as much as will fit. It is + up to the application to zero-terminate the string in that case, if it plans + to use the truncated result. If at least 65537 bytes of room is provided, then + the result will always be complete and zero-terminated. */ diff --git a/deps/zlib/contrib/minizip/zip.c b/deps/zlib/contrib/minizip/zip.c index 0bbdf77e5fc0..b263680bf60b 100644 --- a/deps/zlib/contrib/minizip/zip.c +++ b/deps/zlib/contrib/minizip/zip.c @@ -309,6 +309,7 @@ local void block_init(block_t *block, linkedlist_data *list) { data is added to the datablocks. This is invoked if the central directory is invalid, so there is no longer any point in attempting to interpret it. */ local void block_stop(block_t *block) { + block->node = NULL; block->left = 0; block->next = NULL; } @@ -447,6 +448,7 @@ local char *block_central_name(block_t *block, set_t *set) { name. (Who the heck is putting nuls inside their zip file entry names anyway?) */ set_free(set, name); + name = NULL; continue; } @@ -518,7 +520,6 @@ extern int ZEXPORT zipAlreadyThere(zipFile file, char const *name) { /****************************************************************************/ -#ifndef NO_ADDFILEINEXISTINGZIP /* =========================================================================== Inputs a long in LSB order to the given file nbByte == 1, 2 ,4 or 8 (byte, short or long, ZPOS64_T) @@ -580,6 +581,7 @@ local uLong zip64local_TmzDateToDosDate(const tm_zip* ptm) { /****************************************************************************/ +#ifndef NO_ADDFILEINEXISTINGZIP local int zip64local_getByte(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, int* pi) { unsigned char c; int err = (int)ZREAD64(*pzlib_filefunc_def,filestream,&c,1); @@ -703,7 +705,7 @@ local ZPOS64_T zip64local_SearchCentralDir(const zlib_filefunc64_32_def* pzlib_f unsigned char* buf; ZPOS64_T uSizeFile; ZPOS64_T uBackRead; - ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */ + ZPOS64_T uMaxBack=0xffff+22; /* maximum comment and end-of-central-directory record */ ZPOS64_T uPosFound=0; if (ZSEEK64(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0) @@ -739,7 +741,7 @@ local ZPOS64_T zip64local_SearchCentralDir(const zlib_filefunc64_32_def* pzlib_f if (ZREAD64(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize) break; - for (i=(int)uReadSize-3; (i--)>0;) + for (i=(int)uReadSize-4; i>=0; --i) if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) && ((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06)) { @@ -755,74 +757,28 @@ local ZPOS64_T zip64local_SearchCentralDir(const zlib_filefunc64_32_def* pzlib_f } /* -Locate the End of Zip64 Central directory locator and from there find the CD of a zipfile (at the end, just before -the global comment) + Locate the Zip64 end of central directory record using the locator + immediately preceding the end of central directory record at + centralDirPos. */ -local ZPOS64_T zip64local_SearchCentralDir64(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream) { - unsigned char* buf; - ZPOS64_T uSizeFile; - ZPOS64_T uBackRead; - ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */ - ZPOS64_T uPosFound=0; +local ZPOS64_T zip64local_SearchCentralDir64(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, + ZPOS64_T centralDirPos) { + ZPOS64_T uPosFound; uLong uL; ZPOS64_T relativeOffset; - if (ZSEEK64(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0) + /* The Zip64 locator immediately precedes the end of central directory + record. Checking only there prevents a locator signature in the file + data from overriding the central directory that record refers to. */ + if (centralDirPos < 20) return 0; - uSizeFile = ZTELL64(*pzlib_filefunc_def,filestream); - - if (uMaxBack>uSizeFile) - uMaxBack = uSizeFile; - - buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4); - if (buf==NULL) - return 0; - - uBackRead = 4; - while (uBackReaduMaxBack) - uBackRead = uMaxBack; - else - uBackRead+=BUFREADCOMMENT; - uReadPos = uSizeFile-uBackRead ; - - uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ? - (BUFREADCOMMENT+4) : (uLong)(uSizeFile-uReadPos); - if (ZSEEK64(*pzlib_filefunc_def,filestream,uReadPos,ZLIB_FILEFUNC_SEEK_SET)!=0) - break; - - if (ZREAD64(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize) - break; - - for (i=(int)uReadSize-3; (i--)>0;) - { - /* Signature "0x07064b50" Zip64 end of central directory locator */ - if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) && ((*(buf+i+2))==0x06) && ((*(buf+i+3))==0x07)) - { - uPosFound = uReadPos+(unsigned)i; - break; - } - } - - if (uPosFound!=0) - break; - } - - free(buf); - if (uPosFound == 0) - return 0; - - /* Zip64 end of central directory locator */ + uPosFound = centralDirPos - 20; if (ZSEEK64(*pzlib_filefunc_def,filestream, uPosFound,ZLIB_FILEFUNC_SEEK_SET)!=0) return 0; - /* the signature, already checked */ - if (zip64local_getLong(pzlib_filefunc_def,filestream,&uL)!=ZIP_OK) + if (zip64local_getLong(pzlib_filefunc_def,filestream,&uL)!=ZIP_OK || + uL != 0x07064b50) return 0; /* number of the disk with the start of the zip64 end of central directory */ @@ -862,6 +818,7 @@ local int LoadCentralDirectoryRecord(zip64_internal* pziinit) { ZPOS64_T size_central_dir; /* size of the central directory */ ZPOS64_T offset_central_dir; /* offset of start of central directory */ ZPOS64_T central_pos; + ZPOS64_T central_pos64; uLong uL; uLong number_disk; /* number of the current disk, used for @@ -878,15 +835,15 @@ local int LoadCentralDirectoryRecord(zip64_internal* pziinit) { int hasZIP64Record = 0; - /* check first if we find a ZIP64 record */ - central_pos = zip64local_SearchCentralDir64(&pziinit->z_filefunc,pziinit->filestream); - if(central_pos > 0) + /* find the end of central directory record, and then a ZIP64 record if + there is one immediately before it */ + central_pos = zip64local_SearchCentralDir(&pziinit->z_filefunc,pziinit->filestream); + central_pos64 = zip64local_SearchCentralDir64(&pziinit->z_filefunc,pziinit->filestream, + central_pos); + if (central_pos64 > 0) { hasZIP64Record = 1; - } - else if(central_pos == 0) - { - central_pos = zip64local_SearchCentralDir(&pziinit->z_filefunc,pziinit->filestream); + central_pos = central_pos64; } /* disable to allow appending to empty ZIP archive @@ -1030,7 +987,9 @@ local int LoadCentralDirectoryRecord(zip64_internal* pziinit) { ZPOS64_T size_central_dir_to_read = size_central_dir; size_t buf_size = SIZEDATA_INDATABLOCK; void* buf_read = (void*)ALLOC(buf_size); - if (ZSEEK64(pziinit->z_filefunc, pziinit->filestream, offset_central_dir + byte_before_the_zipfile, ZLIB_FILEFUNC_SEEK_SET) != 0) + if (buf_read == NULL) + err=ZIP_INTERNALERROR; + else if (ZSEEK64(pziinit->z_filefunc, pziinit->filestream, offset_central_dir + byte_before_the_zipfile, ZLIB_FILEFUNC_SEEK_SET) != 0) err=ZIP_ERRNO; while ((size_central_dir_to_read>0) && (err==ZIP_OK)) @@ -1055,6 +1014,11 @@ local int LoadCentralDirectoryRecord(zip64_internal* pziinit) { if (ZSEEK64(pziinit->z_filefunc, pziinit->filestream, offset_central_dir+byte_before_the_zipfile,ZLIB_FILEFUNC_SEEK_SET) != 0) err=ZIP_ERRNO; + if (err != ZIP_OK) + { + ZCLOSE64(pziinit->z_filefunc, pziinit->filestream); + } + return err; } @@ -1113,22 +1077,25 @@ extern zipFile ZEXPORT zipOpen3(const void *pathname, int append, zipcharpc* glo err = LoadCentralDirectoryRecord(&ziinit); } - if (globalcomment) - { - *globalcomment = ziinit.globalcomment; - } # endif /* !NO_ADDFILEINEXISTINGZIP*/ if (err != ZIP_OK) { # ifndef NO_ADDFILEINEXISTINGZIP free(ziinit.globalcomment); + free_linkedlist(&(ziinit.central_dir)); # endif /* !NO_ADDFILEINEXISTINGZIP*/ free(zi); + if (globalcomment) + *globalcomment = NULL; return NULL; } else { +# ifndef NO_ADDFILEINEXISTINGZIP + if (globalcomment) + *globalcomment = ziinit.globalcomment; +# endif /* !NO_ADDFILEINEXISTINGZIP*/ *zi = ziinit; return (zipFile)zi; } @@ -1339,10 +1306,12 @@ extern int ZEXPORT zipOpenNewFileInZip4_64(zipFile file, const char* filename, c return ZIP_PARAMERROR; if ((comment!=NULL) && (strlen(comment)>0xffff)) return ZIP_PARAMERROR; - /* The extra field length must fit in 16 bits. If the member also requires - a Zip64 extra block, that will also need to fit within that 16-bit - length, but that will be checked for later. */ - if ((size_extrafield_local>0xffff) || (size_extrafield_global>0xffff)) + /* The extra field lengths must fit in 16 bits, including a 20-byte + Zip64 sub-field in the local header if zip64 is true. There might + be a Zip64 sub-field needed in the central directory header as + well, but that won't be known until later. */ + if ((size_extrafield_local>(0xffffu-(zip64?20u:0u))) || + (size_extrafield_global>0xffffu)) return ZIP_PARAMERROR; zi = (zip64_internal*)file; @@ -1399,6 +1368,8 @@ extern int ZEXPORT zipOpenNewFileInZip4_64(zipFile file, const char* filename, c zi->ci.size_centralExtraFree = 32; /* Extra space we have reserved in case we need to add ZIP64 extra info data */ zi->ci.central_header = (char*)ALLOC((uInt)zi->ci.size_centralheader + zi->ci.size_centralExtraFree); + if (zi->ci.central_header == NULL) + return ZIP_INTERNALERROR; zi->ci.size_centralExtra = size_extrafield_global; zip64local_putValue_inmemory(zi->ci.central_header,(uLong)CENTRALHEADERMAGIC,4); @@ -1441,8 +1412,6 @@ extern int ZEXPORT zipOpenNewFileInZip4_64(zipFile file, const char* filename, c for (i=0;ici.central_header+SIZECENTRALHEADER+size_filename+ size_extrafield_global+i) = *(comment+i); - if (zi->ci.central_header == NULL) - return ZIP_INTERNALERROR; zi->ci.zip64 = zip64; zi->ci.totalCompressedData = 0; @@ -1899,14 +1868,25 @@ extern int ZEXPORT zipCloseFileInZipRaw64(zipFile file, ZPOS64_T uncompressed_si if(datasize > 0) { char* p = NULL; + uLong size_comment; - if((uLong)(datasize + 4) > zi->ci.size_centralExtraFree) + if (zi->ci.size_centralExtra + datasize + 4 > 0xffff) { - /* we cannot write more data to the buffer that we have room for. */ + /* the extra field is now too big with the Zip64 sub-field -- leave + this entry out of the central directory, so that the zip file can be + completed with the remaining entries, if desired */ + free(zi->ci.central_header); + zi->ci.central_header = NULL; + zi->in_opened_file_inzip = 0; return ZIP_BADZIPFILE; } - p = zi->ci.central_header + zi->ci.size_centralheader; + /* Insert the ZIP64 field at the end of the extra field, which is ahead + of the file comment, so move the comment up to make room. */ + size_comment = (unsigned char)zi->ci.central_header[32] | + ((uLong)(unsigned char)zi->ci.central_header[33] << 8); + p = zi->ci.central_header + zi->ci.size_centralheader - size_comment; + memmove(p + datasize + 4, p, size_comment); /* Add Extra Information Header for 'ZIP64 information' */ zip64local_putValue_inmemory(p, 0x0001, 2); /* HeaderID */ @@ -2197,25 +2177,45 @@ extern int ZEXPORT zipClose(zipFile file, const char* global_comment) { extern int ZEXPORT zipRemoveExtraInfoBlock(char* pData, int* dataLen, short sHeader) { char* p = pData; int size = 0; + int remaining; char* pNewHeader; char* pTmp; - short header; - short dataSize; + unsigned header; + unsigned dataSize; + unsigned remove = (unsigned short)sHeader; int retVal = ZIP_OK; if(pData == NULL || dataLen == NULL || *dataLen < 4) return ZIP_PARAMERROR; + remaining = *dataLen; pNewHeader = (char*)ALLOC((unsigned)*dataLen); + if (pNewHeader == NULL) + return ZIP_INTERNALERROR; + pTmp = pNewHeader; - while(p < (pData + *dataLen)) + while(remaining > 0) { - header = *(short*)p; - dataSize = *(((short*)p)+1); + if(remaining < 4) + { + retVal = ZIP_BADZIPFILE; + break; + } + + header = (unsigned)(unsigned char)p[0] | + ((unsigned)(unsigned char)p[1] << 8); + dataSize = (unsigned)(unsigned char)p[2] | + ((unsigned)(unsigned char)p[3] << 8); - if( header == sHeader ) /* Header found. */ + if((int)dataSize > remaining - 4) + { + retVal = ZIP_BADZIPFILE; + break; + } + + if( header == remove ) /* Header found. */ { p += dataSize + 4; /* skip it. do not copy to temp buffer */ } @@ -2223,10 +2223,18 @@ extern int ZEXPORT zipRemoveExtraInfoBlock(char* pData, int* dataLen, short sHea { /* Extra Info block should not be removed, So copy it to the temp buffer. */ memcpy(pTmp, p, dataSize + 4); + pTmp += dataSize + 4; p += dataSize + 4; size += dataSize + 4; } + remaining -= dataSize + 4; + } + + if(retVal != ZIP_OK) + { + free(pNewHeader); + return retVal; } if(size < *dataLen) diff --git a/deps/zlib/contrib/minizip/zip.h b/deps/zlib/contrib/minizip/zip.h index 9962440781d4..d4f31112f918 100644 --- a/deps/zlib/contrib/minizip/zip.h +++ b/deps/zlib/contrib/minizip/zip.h @@ -181,9 +181,14 @@ extern int ZEXPORT zipOpenNewFileInZip64(zipFile file, contains the extrafield data for the local header if extrafield_global!=NULL and size_extrafield_global>0, extrafield_global contains the extrafield data for the global header - if comment != NULL, comment contain the comment string - method contain the compression method (0 for store, Z_DEFLATED for deflate) - level contain the level of compression (can be Z_DEFAULT_COMPRESSION) + size_extrafield_local and size_extrafield_global are limited to 65535 by + the format, or less depending on the presence of Zip64 subfields -- + size_extrafield_local needs to leave room for a 20-byte Zip64 subfield if + zip64 is true, and size_extrafield_global needs to leave room for a 12 to + 28 byte Zip64 subfield in case the offset or length exceeds 2^32-1 + if comment != NULL, comment contains the comment string + method contains the compression method (0 for store, Z_DEFLATED for deflate) + level contains the level of compression (can be Z_DEFAULT_COMPRESSION) zip64 is set to 1 if a zip64 extended information block should be added to the local file header. this MUST be '1' if the uncompressed size is >= 0xffffffff. diff --git a/deps/zlib/contrib/optimizations/inflate.c b/deps/zlib/contrib/optimizations/inflate.c index 5c5938d3e522..c4ac1c339f8a 100644 --- a/deps/zlib/contrib/optimizations/inflate.c +++ b/deps/zlib/contrib/optimizations/inflate.c @@ -139,6 +139,10 @@ int ZEXPORT inflateReset(z_streamp strm) { state->window = Z_NULL; } state->wbits = state->reqbits; + } else if (state->window != Z_NULL) { + /* Clear the window before reusing it, to avoid "chunked copies" + * copying bytes from a previous session. */ + zmemzero(state->window, (1U << state->wbits) + CHUNKCOPY_CHUNK_SIZE); } state->wrap = state->reqwrap; state->wsize = 0; @@ -388,15 +392,10 @@ local int updatewindow(z_streamp strm, const Bytef *end, unsigned copy) { ZALLOC(strm, wsize + CHUNKCOPY_CHUNK_SIZE, sizeof(unsigned char)); if (state->window == Z_NULL) return 1; -#ifdef INFLATE_CLEAR_UNUSED_UNDEFINED - /* Copies from the overflow portion of this buffer are undefined and - may cause analysis tools to raise a warning if we don't initialize - it. However, this undefined data overwrites other undefined data - and is subsequently either overwritten or left deliberately - undefined at the end of decode; so there's really no point. - */ - zmemzero(state->window + wsize, CHUNKCOPY_CHUNK_SIZE); -#endif + + /* Clear the window to avoid "chunked copies" writing uninitialized + * bytes from the window to the output buffer (crbug.com/575038806). */ + zmemzero(state->window, wsize + CHUNKCOPY_CHUNK_SIZE); } /* if window not in use yet, initialize */ @@ -1262,29 +1261,6 @@ int ZEXPORT inflate(z_streamp strm, int flush) { Note: a memory error from inflate() is non-recoverable. */ inf_leave: -#if defined(ZLIB_DEBUG) - /* XXX(cavalcantii): I put this in place back in 2017 to help debug faulty - * client code relying on undefined behavior when chunk_copy first landed. - * - * It is save to say after all these years that Chromium code is well - * behaved and works fine with the optimization, therefore we can enable - * this only for DEBUG builds. - * - * We write a defined value in the unused space to help mark - * where the stream has ended. We don't use zeros as that can - * mislead clients relying on undefined behavior (i.e. assuming - * that the data is over when the buffer has a zero/null value). - * - * The basic idea is that if client code is not relying on the zlib context - * to inform the amount of decompressed data, but instead reads the output - * buffer until a zero/null is found, it will fail faster and harder - * when the remaining of the buffer is marked with a symbol (e.g. 0x55). - */ - if (left >= CHUNKCOPY_CHUNK_SIZE) - memset(put, 0x55, CHUNKCOPY_CHUNK_SIZE); - else - memset(put, 0x55, left); -#endif RESTORE(); if (state->wsize || (out != strm->avail_out && state->mode < BAD && (state->mode < CHECK || flush != Z_FINISH))) @@ -1537,7 +1513,7 @@ int ZEXPORT inflateCopy(z_streamp dest, z_streamp source) { copy->next = copy->codes + (state->next - state->codes); if (window != Z_NULL) { wsize = 1U << state->wbits; - zmemcpy(window, state->window, wsize); + zmemcpy(window, state->window, wsize + CHUNKCOPY_CHUNK_SIZE); } copy->window = window; dest->state = (struct internal_state FAR *)copy; diff --git a/deps/zlib/contrib/tests/utils_unittest.cc b/deps/zlib/contrib/tests/utils_unittest.cc index a9a40a755b6d..1cd0831a78da 100644 --- a/deps/zlib/contrib/tests/utils_unittest.cc +++ b/deps/zlib/contrib/tests/utils_unittest.cc @@ -1297,6 +1297,149 @@ TEST(ZlibTest, InflateChunkRefillLiteralBitBudget) { EXPECT_EQ('b', output[5]); } +static voidpf debug_alloc(voidpf opaque, uInt items, uInt size) { + size_t n = items * size; + void *p = malloc(n); + if (!p) + return p; + memset(p, 255, n); + return p; +} + +TEST(ZlibTest, InflateWindowZeroInit) { + // $ echo "0hello123hello" | gzip | xxd -i + static const uint8_t kCompressed[] = { + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x33, 0xc8, + 0x48, 0xcd, 0xc9, 0xc9, 0x37, 0x34, 0x32, 0x06, 0xd3, 0x5c, 0x00, 0x32, + 0x8e, 0x87, 0x00, 0x0f, 0x00, 0x00, 0x00 + }; + + unsigned char out[256] = {0}; + z_stream stream = {}; + // Use an allocator that initializes memory with 255-bytes. + stream.zalloc = debug_alloc; + stream.next_in = const_cast(kCompressed); + stream.avail_in = sizeof(kCompressed); + + // Initialize inflation (+32 for gzip header). + ASSERT_EQ(Z_OK, inflateInit2(&stream, 15 + 32)); + + // Extract the first part, forcing zlib to save it in the window for later. + stream.next_out = out; + stream.avail_out = 6; + ASSERT_EQ(Z_OK, inflate(&stream, Z_SYNC_FLUSH)); + + // Extract the rest, performing a wide copy (for the backref) from the window. + stream.avail_out = sizeof(out) - stream.total_out; + ASSERT_EQ(Z_STREAM_END, inflate(&stream, Z_FINISH)); + EXPECT_EQ(std::string(out, out + stream.total_out), "0hello123hello\n"); + for (unsigned char c : out) { + EXPECT_NE(255, c); // No debug bytes from the window in out. + } + + ASSERT_EQ(Z_OK, inflateEnd(&stream)); +} + +TEST(ZlibTest, InflateCopyWindowInit) { + // A window-sized (32768) byte sequence ending with "hello", followed by + // another "hello": + // $ printf "%32768shello" hello | gzip | xxd -i + static const uint8_t kCompressed[] = { + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xed, 0xc4, + 0x31, 0x09, 0x00, 0x00, 0x08, 0x00, 0xb0, 0x2a, 0x96, 0x13, 0x3c, 0x04, + 0xfb, 0x7f, 0x82, 0x35, 0xdc, 0x8e, 0x45, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0xc0, 0x6b, 0x95, 0xdd, 0x73, 0x2d, 0x6f, 0xc6, 0x49, 0x63, + 0x05, 0x80, 0x00, 0x00 + }; + + unsigned char out[65536] = {0}; + z_stream stream = {}; + // Use an allocator that initializes memory with 255-bytes. + stream.zalloc = debug_alloc; + stream.next_in = const_cast(kCompressed); + stream.avail_in = sizeof(kCompressed); + + // Initialize inflation (+32 for gzip header). + ASSERT_EQ(Z_OK, inflateInit2(&stream, 15 + 32)); + + // Extract the first part, forcing zlib to save it in the window for later. + stream.next_out = out; + stream.avail_out = 32768; + ASSERT_EQ(Z_OK, inflate(&stream, Z_SYNC_FLUSH)); + + // The stream copy should have a fully initialized window. + z_stream stream_cpy = {}; + ASSERT_EQ(Z_OK, inflateCopy(&stream_cpy, &stream)); + ASSERT_EQ(Z_OK, inflateEnd(&stream)); + + // Extract the rest, performing a wide copy (for the backref) from the end of + // the window. + stream_cpy.avail_out = sizeof(out) - stream_cpy.total_out; + ASSERT_EQ(Z_STREAM_END, inflate(&stream_cpy, Z_FINISH)); + EXPECT_EQ(std::string(out + 32768 - 5, out + stream_cpy.total_out), + "hellohello"); + for (unsigned char c : out) { + EXPECT_NE(255, c); // No debug bytes from the window in out. + } + ASSERT_EQ(Z_OK, inflateEnd(&stream_cpy)); +} + +TEST(ZlibTest, InflateResetWindowInit) { + // A long (larger than a window) sequence of x's: + // $ perl -e 'print "x" x 50000' | gzip | xxd -i + static const uint8_t kCompressed1[] = { + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xed, 0xc1, + 0x01, 0x0d, 0x00, 0x00, 0x00, 0xc2, 0xa0, 0xda, 0x8f, 0x6f, 0x0f, 0x07, + 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x4f, 0x06, 0xda, 0x1f, 0xee, 0xb5, 0x50, 0xc3, 0x00, 0x00 + }; + + // $ echo "0hello123hello" | gzip | xxd -i + static const uint8_t kCompressed2[] = { + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x33, 0xc8, + 0x48, 0xcd, 0xc9, 0xc9, 0x37, 0x34, 0x32, 0x06, 0xd3, 0x5c, 0x00, 0x32, + 0x8e, 0x87, 0x00, 0x0f, 0x00, 0x00, 0x00 + }; + + unsigned char out[65536] = {0}; + z_stream stream = {}; + + // Fill the window with x's. + stream.next_in = const_cast(kCompressed1); + stream.avail_in = sizeof(kCompressed1); + ASSERT_EQ(Z_OK, inflateInit2(&stream, 15 + 32)); + stream.next_out = out; + stream.avail_out = 32768; + ASSERT_EQ(Z_OK, inflate(&stream, Z_SYNC_FLUSH)); + + // Resetting should clear the window. + ASSERT_EQ(Z_OK, inflateReset(&stream)); + memset(out, 0, sizeof(out)); + + // Extract the first part of kCompressed2, forcing zlib to save it in the + // window for later. + stream.next_in = const_cast(kCompressed2); + stream.avail_in = sizeof(kCompressed2); + stream.next_out = out; + stream.avail_out = 6; + ASSERT_EQ(Z_OK, inflate(&stream, Z_SYNC_FLUSH)); + + // Extract the rest, performing a wide copy (for the backref) from the window. + stream.avail_out = sizeof(out) - stream.total_out; + ASSERT_EQ(Z_STREAM_END, inflate(&stream, Z_FINISH)); + EXPECT_EQ(std::string(out, out + stream.total_out), "0hello123hello\n"); + for (unsigned char c : out) { + EXPECT_NE('x', c); // No x's from the first inflation window in out. + } + + ASSERT_EQ(Z_OK, inflateEnd(&stream)); +} + // TODO(gustavoa): make these tests run standalone. #ifndef CMAKE_STANDALONE_UNITTESTS @@ -1434,7 +1577,11 @@ static base::FilePath TestDataDir() { TEST(ZlibTest, ZipUnicodePathExtraSizeFilenameOverflow) { // This is based on components/test/data/unzip_service/bug953599.zip (added // in https://crrev.com/1004132), with the Unicode Path Extra Field's - // dataSize hex edited to four. + // dataSize hex edited to four. The 15 bytes left after that field form a + // record of their own (its dataSize at offset 0x75 hex edited to 11), + // because minizip rejects an extra field whose records overrun it. That + // record starts with 0xc8, the last byte of the name CRC, so a parser that + // ignores the short dataSize would find a matching CRC. base::FilePath zip_file = TestDataDir().AppendASCII("unicode_path_extra_overflow.zip"); unzFile uzf = unzOpen(zip_file.AsUTF8Unsafe().c_str()); ASSERT_NE(uzf, nullptr); diff --git a/deps/zlib/google/test/data/unicode_path_extra_overflow.zip b/deps/zlib/google/test/data/unicode_path_extra_overflow.zip index 36d525b99b90..32c7e9e81ace 100644 Binary files a/deps/zlib/google/test/data/unicode_path_extra_overflow.zip and b/deps/zlib/google/test/data/unicode_path_extra_overflow.zip differ diff --git a/deps/zlib/patches/0016-minizip-parse-unicode-path-extra-field.patch b/deps/zlib/patches/0016-minizip-parse-unicode-path-extra-field.patch index 2b2eb3375e52..a4e2b7bcbb43 100644 --- a/deps/zlib/patches/0016-minizip-parse-unicode-path-extra-field.patch +++ b/deps/zlib/patches/0016-minizip-parse-unicode-path-extra-field.patch @@ -103,7 +103,7 @@ Date: Wed Sep 16 15:45:40 2026 +0200 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/8418328 diff --git a/third_party/zlib/contrib/minizip/unzip.c b/third_party/zlib/contrib/minizip/unzip.c -index fb5eb0bf0f852..c15f4484f75f7 100644 +index 838092494056a..d6ac9c5d06d03 100644 --- a/third_party/zlib/contrib/minizip/unzip.c +++ b/third_party/zlib/contrib/minizip/unzip.c @@ -63,6 +63,7 @@ @@ -114,7 +114,7 @@ index fb5eb0bf0f852..c15f4484f75f7 100644 #include #include #include -@@ -195,6 +196,26 @@ +@@ -195,6 +196,26 @@ typedef struct Reads a long in LSB order from the given gz_stream. Sets */ @@ -141,10 +141,10 @@ index fb5eb0bf0f852..c15f4484f75f7 100644 local int unz64local_getShort(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, uLong *pX) { -@@ -836,6 +857,10 @@ - uLong uMagic; - long lSeek=0; +@@ -740,6 +761,10 @@ local int unz64local_GetCurrentFileInfoInternal(unzFile file, uLong uL; + unsigned zip64_required; + unsigned zip64_consumed = 0; + uLong uFileNameCrc; + + file_info.size_utf8_filename = 0; @@ -152,8 +152,8 @@ index fb5eb0bf0f852..c15f4484f75f7 100644 if (file==NULL) return UNZ_PARAMERROR; -@@ -907,21 +932,34 @@ - file_info_internal.offset_curfile = uL; +@@ -822,21 +847,34 @@ local int unz64local_GetCurrentFileInfoInternal(unzFile file, + (file_info.disk_num_start == 0xffff ? 8U : 0U); lSeek+=file_info.size_filename; - if ((err==UNZ_OK) && (szFileName!=NULL)) @@ -198,72 +198,50 @@ index fb5eb0bf0f852..c15f4484f75f7 100644 } /* Read extrafield */ -@@ -1007,6 +1045,64 @@ +@@ -949,6 +987,42 @@ local int unz64local_GetCurrentFileInfoInternal(unzFile file, + zip64_consumed |= 8U; } - } + else if (headerId == 0x7075) /* Info-ZIP Unicode Path Extra Field */ + { + int version = 0; ++ uLong uCrc; + -+ if (dataSize < 1 + 4) -+ { -+ /* dataSize includes version (1 byte), uCrc (4 bytes), and -+ * the filename data. If it's too small, fileNameSize below -+ * would overflow. */ -+ if (ZSEEK64(s->z_filefunc, s->filestream, dataSize, ZLIB_FILEFUNC_SEEK_CUR) != 0) -+ { -+ err = UNZ_ERRNO; -+ } -+ } -+ else if (unz64local_getByte(&s->z_filefunc, s->filestream, &version) != UNZ_OK) ++ /* dataSize includes version (1 byte), uCrc (4 bytes), and ++ * the filename data. If it's too small, the filename size ++ * below would overflow. */ ++ if (dataSize >= 1 + 4) + { -+ err = UNZ_ERRNO; -+ } -+ else if (version != 1) -+ { -+ if (ZSEEK64(s->z_filefunc, s->filestream,dataSize - 1, ZLIB_FILEFUNC_SEEK_CUR) != 0) -+ { -+ err = UNZ_ERRNO; -+ } ++ if (unz64local_getByte(&s->z_filefunc, s->filestream, &version) != UNZ_OK) ++ return UNZ_ERRNO; ++ used += 1; + } -+ else -+ { -+ uLong uCrc, utf8FileNameSize; + ++ if (version == 1) ++ { + if (unz64local_getLong(&s->z_filefunc, s->filestream, &uCrc) != UNZ_OK) -+ { -+ err = UNZ_ERRNO; -+ } -+ utf8FileNameSize = dataSize - (1 + 4); /* 1 for version, 4 for uCrc */ ++ return UNZ_ERRNO; ++ used += 4; + + /* Check CRC against file name in the header. */ -+ if (uCrc != uFileNameCrc) ++ if (uCrc == uFileNameCrc) + { -+ if (ZSEEK64(s->z_filefunc, s->filestream, utf8FileNameSize, ZLIB_FILEFUNC_SEEK_CUR) != 0) -+ { -+ err = UNZ_ERRNO; -+ } -+ } -+ else -+ { -+ file_info.size_utf8_filename = utf8FileNameSize; ++ file_info.size_utf8_filename = dataSize - used; + + if (file_info.size_utf8_filename > 0) + { + if (ZREAD64(s->z_filefunc, s->filestream, file_info.utf8_filename, file_info.size_utf8_filename) != file_info.size_utf8_filename) -+ { -+ err = UNZ_ERRNO; -+ } ++ return UNZ_ERRNO; + } + file_info.utf8_filename[file_info.size_utf8_filename] = '\0'; ++ used += file_info.size_utf8_filename; + } + } + } - else - { - if (ZSEEK64(s->z_filefunc, s->filestream,dataSize,ZLIB_FILEFUNC_SEEK_CUR)!=0) -@@ -1106,6 +1202,9 @@ + + /* Skip remaining bytes, but never leave this record. */ + if (used < dataSize && +@@ -1050,6 +1124,9 @@ extern int ZEXPORT unzGetCurrentFileInfo(unzFile file, pfile_info->compressed_size = (uLong)file_info64.compressed_size; pfile_info->uncompressed_size = (uLong)file_info64.uncompressed_size; @@ -274,7 +252,7 @@ index fb5eb0bf0f852..c15f4484f75f7 100644 return err; } diff --git a/third_party/zlib/contrib/minizip/unzip.h b/third_party/zlib/contrib/minizip/unzip.h -index 9c98b608c7342..05efa64ea477e 100644 +index c85c65e56c2a1..5b5e820a44032 100644 --- a/third_party/zlib/contrib/minizip/unzip.h +++ b/third_party/zlib/contrib/minizip/unzip.h @@ -42,6 +42,8 @@ diff --git a/deps/zlib/patches/0018-support-prefixed-zip64.patch b/deps/zlib/patches/0018-support-prefixed-zip64.patch index 8dc483f6e1da..dfec0a5b29d6 100644 --- a/deps/zlib/patches/0018-support-prefixed-zip64.patch +++ b/deps/zlib/patches/0018-support-prefixed-zip64.patch @@ -30,10 +30,10 @@ Date: Fri May 16 15:35:36 2025 +0200 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6554217 diff --git a/third_party/zlib/contrib/minizip/unzip.c b/third_party/zlib/contrib/minizip/unzip.c -index c15f4484f75f7..2ec74e0ff2af8 100644 +index d6ac9c5d06d03..fc921557d6619 100644 --- a/third_party/zlib/contrib/minizip/unzip.c +++ b/third_party/zlib/contrib/minizip/unzip.c -@@ -485,6 +485,46 @@ local ZPOS64_T unz64local_SearchCentralDir64(const zlib_filefunc64_32_def* pzlib +@@ -439,6 +439,46 @@ local ZPOS64_T unz64local_SearchCentralDir64(const zlib_filefunc64_32_def* pzlib if (uL != 1) return CENTRALDIRINVALID; @@ -43,7 +43,7 @@ index c15f4484f75f7..2ec74e0ff2af8 100644 + if (relativeOffset > uPosFound) + return CENTRALDIRINVALID; + const int BUFSIZE = 1024 * 4; -+ buf = (unsigned char*)ALLOC(BUFSIZE); ++ unsigned char* buf = (unsigned char*)ALLOC(BUFSIZE); + if (buf==NULL) + return CENTRALDIRINVALID; + // Zip64 EOCDR is at least 48 bytes long. diff --git a/deps/zlib/patches/0019-fix-zip64-in-zip.patch b/deps/zlib/patches/0019-fix-zip64-in-zip.patch deleted file mode 100644 index 26b8528645af..000000000000 --- a/deps/zlib/patches/0019-fix-zip64-in-zip.patch +++ /dev/null @@ -1,37 +0,0 @@ -diff --git a/third_party/zlib/contrib/minizip/unzip.c b/third_party/zlib/contrib/minizip/unzip.c -index b77b787ccc0db..78663ec2d53a8 100644 ---- a/third_party/zlib/contrib/minizip/unzip.c -+++ b/third_party/zlib/contrib/minizip/unzip.c -@@ -444,12 +444,28 @@ local ZPOS64_T unz64local_SearchCentralDir64(const zlib_filefunc64_32_def* pzlib - if (ZREAD64(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize) - break; - -- for (i=(int)uReadSize-3; (i--)>0;) -+ /* Search for the non-zip64 EoCDR and confirm zip64 EoCDL is 20 bytes -+ earlier. This avoids false positives if the file is a non-zip64 zip -+ but contains an uncompressed zip64 near its end. Note: zip64 EoCDL is -+ 20 bytes long. */ -+ for (i=(int)uReadSize-3; (i--)>20;) -+ // End of central directory record signature (PK\5\6) - if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) && -- ((*(buf+i+2))==0x06) && ((*(buf+i+3))==0x07)) -+ ((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06)) - { -- uPosFound = uReadPos+(unsigned)i; -- break; -+ // Zip64 end of central directory locator signature (PK\6\7) -+ if (((*(buf+i-20))==0x50) && ((*(buf+i+1-20))==0x4b) && -+ ((*(buf+i+2-20))==0x06) && ((*(buf+i+3-20))==0x07)) -+ { -+ uPosFound = uReadPos+(unsigned)i-20; -+ break; -+ } -+ else -+ { -+ /* This is a non-zip64 zip; abandon the search. */ -+ free(buf); -+ return CENTRALDIRINVALID; -+ } - } - - if (uPosFound!=CENTRALDIRINVALID) diff --git a/src/zlib_version.h b/src/zlib_version.h index 696df9f6dff0..64ec49a44c2f 100644 --- a/src/zlib_version.h +++ b/src/zlib_version.h @@ -2,5 +2,5 @@ // Refer to tools/dep_updaters/update-zlib.sh #ifndef SRC_ZLIB_VERSION_H_ #define SRC_ZLIB_VERSION_H_ -#define ZLIB_VERSION "1.4-401d185" +#define ZLIB_VERSION "1.4-02826db" #endif // SRC_ZLIB_VERSION_H_