Skip to content

Commit 31980e8

Browse files
miss-islingtonencukoutonghuaroot
authored
[3.14] gh-156002: Bound zipfile decompression for bzip2/LZMA/Zstandard (GH-156003) (GH-156362) (GH-156737)
[3.15] gh-156002: Bound zipfile decompression for bzip2/LZMA/Zstandard (GH-156003) (GH-156362) Patch by @tonghuaroot. zipfile.ZipExtFile._read1() bounds the output of each decompress() call for DEFLATE members by passing a max_length to zlib, but for bzip2, LZMA, and Zstandard members it called decompress() with no bound. A whole compressed chunk was therefore expanded into a single allocation before the data[:self._left] clip ran, so a consumer that deliberately reads in small chunks to limit memory (for example zf.open(name).read(8192)) was silently unprotected for non-DEFLATE members. A small, spec-conformant archive member declaring a large uncompressed size could drive multi-GB peak memory. _read1() now passes a per-call bound to the non-DEFLATE decompress() (mirroring the DEFLATE branch) and drains the decompressor's internal buffer across calls by checking needs_input before reading more compressed input. zipfile's LZMADecompressor wrapper forwards max_length and exposes needs_input so the bound also holds for LZMA members. (cherry picked from commit f897dbf) (cherry picked from commit 1b424c0) Co-authored-by: Petr Viktorin <encukou@gmail.com> Co-authored-by: tonghuaroot <tonghuaroot@gmail.com>
1 parent e3c7518 commit 31980e8

3 files changed

Lines changed: 79 additions & 5 deletions

File tree

Lib/test/test_zipfile/test_core.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2723,6 +2723,48 @@ def tearDown(self):
27232723
unlink(TESTFN2)
27242724

27252725

2726+
class AbstractBoundedDecompressTests:
2727+
# ZipExtFile._read1() bounds the output of each decompress() call so that a
2728+
# small member declaring a large uncompressed size cannot expand into one
2729+
# unbounded read.
2730+
def test_read1_output_is_bounded(self):
2731+
buf = io.BytesIO()
2732+
with zipfile.ZipFile(buf, "w", compression=self.compression) as zf:
2733+
zf.writestr("big", b"\0" * (4 * 1024 * 1024))
2734+
with zipfile.ZipFile(io.BytesIO(buf.getvalue())) as zf:
2735+
with zf.open("big") as f:
2736+
self.assertLessEqual(len(f._read1(100)), f.MIN_READ_SIZE)
2737+
2738+
2739+
class StoredBoundedDecompressTests(AbstractBoundedDecompressTests,
2740+
unittest.TestCase):
2741+
compression = zipfile.ZIP_STORED
2742+
2743+
2744+
@requires_zlib()
2745+
class DeflateBoundedDecompressTests(AbstractBoundedDecompressTests,
2746+
unittest.TestCase):
2747+
compression = zipfile.ZIP_DEFLATED
2748+
2749+
2750+
@requires_bz2()
2751+
class Bzip2BoundedDecompressTests(AbstractBoundedDecompressTests,
2752+
unittest.TestCase):
2753+
compression = zipfile.ZIP_BZIP2
2754+
2755+
2756+
@requires_lzma()
2757+
class LzmaBoundedDecompressTests(AbstractBoundedDecompressTests,
2758+
unittest.TestCase):
2759+
compression = zipfile.ZIP_LZMA
2760+
2761+
2762+
@requires_zstd()
2763+
class ZstdBoundedDecompressTests(AbstractBoundedDecompressTests,
2764+
unittest.TestCase):
2765+
compression = zipfile.ZIP_ZSTANDARD
2766+
2767+
27262768
class AbstractBadCrcTests:
27272769
def test_testzip_with_bad_crc(self):
27282770
"""Tests that files with bad CRCs return their name from testzip."""

Lib/zipfile/__init__.py

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,16 @@ def __init__(self):
786786
self._unconsumed = b''
787787
self.eof = False
788788

789-
def decompress(self, data):
789+
@property
790+
def _needs_input(self):
791+
# While the LZMA properties header is still being buffered, more input
792+
# is required; afterwards defer to the wrapped decompressor so a bounded
793+
# decompress() call can be drained across reads.
794+
if self._decomp is None:
795+
return True
796+
return self._decomp.needs_input
797+
798+
def decompress(self, data, max_length=-1):
790799
if self._decomp is None:
791800
self._unconsumed += data
792801
if len(self._unconsumed) <= 4:
@@ -802,7 +811,7 @@ def decompress(self, data):
802811
data = self._unconsumed[4 + psize:]
803812
del self._unconsumed
804813

805-
result = self._decomp.decompress(data)
814+
result = self._decomp.decompress(data, max_length)
806815
self.eof = self._decomp.eof
807816
return result
808817

@@ -869,6 +878,13 @@ def _get_compressor(compress_type, compresslevel=None):
869878
return None
870879

871880

881+
def _decompressor_needs_input(decompressor):
882+
# bz2/zstd expose the stdlib decompressor's public needs_input; the LZMA
883+
# wrapper keeps it private (_needs_input) to avoid adding public API.
884+
needs_input = getattr(decompressor, "needs_input", None)
885+
return decompressor._needs_input if needs_input is None else needs_input
886+
887+
872888
def _get_decompressor(compress_type):
873889
_check_compression(compress_type)
874890
if compress_type == ZIP_STORED:
@@ -1171,8 +1187,15 @@ def _read1(self, n):
11711187
data = self._decompressor.unconsumed_tail
11721188
if n > len(data):
11731189
data += self._read2(n - len(data))
1174-
else:
1190+
elif self._compress_type == ZIP_STORED:
11751191
data = self._read2(n)
1192+
else:
1193+
# bzip2/lzma/zstd: a bounded decompress() call may leave input
1194+
# buffered inside the decompressor; drain that before reading more.
1195+
if _decompressor_needs_input(self._decompressor):
1196+
data = self._read2(n)
1197+
else:
1198+
data = b''
11761199

11771200
if self._compress_type == ZIP_STORED:
11781201
self._eof = self._compress_left <= 0
@@ -1185,8 +1208,13 @@ def _read1(self, n):
11851208
if self._eof:
11861209
data += self._decompressor.flush()
11871210
else:
1188-
data = self._decompressor.decompress(data)
1189-
self._eof = self._decompressor.eof or self._compress_left <= 0
1211+
# Bound the output of a single decompress() call (mirroring the
1212+
# DEFLATE path above) so that a small compressed member cannot
1213+
# expand into one unbounded read.
1214+
data = self._decompressor.decompress(data, max(n, self.MIN_READ_SIZE))
1215+
self._eof = (self._decompressor.eof or
1216+
self._compress_left <= 0 and
1217+
_decompressor_needs_input(self._decompressor))
11901218

11911219
data = data[:self._left]
11921220
self._left -= len(data)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Bound the amount of data :mod:`zipfile` decompresses per read for members
2+
compressed with bzip2, LZMA, or Zstandard, matching the existing limit for
3+
deflate. A small archive member could previously expand into an unbounded
4+
allocation even when read in small chunks.

0 commit comments

Comments
 (0)