diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 63e109c6..4b200d3c 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -18,6 +18,10 @@ version 1.8.1-dev by standard zlib, including builds linked against older ISA-L releases. + Fix backward seeking in igzip files by discarding buffered compressed data when rewinding the underlying file. ++ Extraordinarily large gzip headers with either a very long comment or + filename will now cause an ``OverflowError`` rather than use more and more + memory. The size is limited to the internal buffer size which is currently + set at 512 KiB. version 1.8.0 ----------------- diff --git a/src/isal/isal_zlibmodule.c b/src/isal/isal_zlibmodule.c index 25224161..ea9457fd 100644 --- a/src/isal/isal_zlibmodule.c +++ b/src/isal/isal_zlibmodule.c @@ -1626,17 +1626,15 @@ GzipReader_read_from_file(GzipReader *self) uint8_t *buffer_end = self->buffer_end; size_t remaining = buffer_end - current_pos; if (remaining == self->buffer_size) { - /* Buffer is full but a new read request was issued. This will be due - to the header being bigger than the header. Enlarge the buffer - to accommodate the hzip header. */ - size_t new_buffer_size = self->buffer_size * 2; - uint8_t *tmp_buffer = PyMem_Realloc(self->input_buffer, new_buffer_size); - if (tmp_buffer == NULL) { - PyErr_NoMemory(); - return -1; - } - self->input_buffer = tmp_buffer; - self->buffer_size = new_buffer_size; + /* Buffer is full but a new read request was issued. This will be due + to the header being bigger than the buffer. To prevent adverserial + inputs with infinite header sizes an error is thrown here. */ + PyErr_Format( + PyExc_OverflowError, + "Gzip header exceeds input buffer size of %lu", + self->buffer_size + ); + return -1; } else if (remaining > 0) { memmove(self->input_buffer, current_pos, remaining); } diff --git a/tests/test_igzip.py b/tests/test_igzip.py index 7b32b363..c0661197 100644 --- a/tests/test_igzip.py +++ b/tests/test_igzip.py @@ -411,7 +411,7 @@ def test_very_long_header_in_data(): def test_very_long_header_in_file(): # header with a very long filename. header = (b"\x1f\x8b\x08\x08\x00\x00\x00\x00\x00\xff" + - igzip.READ_BUFFER_SIZE * 2 * b"A" + + (igzip.READ_BUFFER_SIZE - 11) * b"A" + b"\x00") compressed = header + isal_zlib.compress(b"", 3, -15) + 8 * b"\00" f = io.BytesIO(compressed) @@ -473,3 +473,27 @@ def test_bgzip(): with igzip.open(gzip_file, "rb") as gz: gz_data = gz.read() assert bgz_data == gz_data + + +def test_adverserial_header(): + magic = b"\x1f\x8b" + method = 8 + mtime = 0 + flags = FCOMMENT + _os = 255 + xfl = 0 + header = struct.pack("<2sBBIBB", magic, method, flags, mtime, xfl, _os) + comment = b"A" * 10 * 1024 * 1024 + b"\00" + compressed = isal_zlib.compress(b"", wbits=-15) + crc = isal_zlib.crc32(b"") + length = 0 + trailer = struct.pack("