Skip to content

Commit 7a2b738

Browse files
encukoutonghuaroot
authored andcommitted
[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 a371ba8 commit 7a2b738

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
@@ -2619,6 +2619,48 @@ def tearDown(self):
26192619
unlink(TESTFN2)
26202620

26212621

2622+
class AbstractBoundedDecompressTests:
2623+
# ZipExtFile._read1() bounds the output of each decompress() call so that a
2624+
# small member declaring a large uncompressed size cannot expand into one
2625+
# unbounded read.
2626+
def test_read1_output_is_bounded(self):
2627+
buf = io.BytesIO()
2628+
with zipfile.ZipFile(buf, "w", compression=self.compression) as zf:
2629+
zf.writestr("big", b"\0" * (4 * 1024 * 1024))
2630+
with zipfile.ZipFile(io.BytesIO(buf.getvalue())) as zf:
2631+
with zf.open("big") as f:
2632+
self.assertLessEqual(len(f._read1(100)), f.MIN_READ_SIZE)
2633+
2634+
2635+
class StoredBoundedDecompressTests(AbstractBoundedDecompressTests,
2636+
unittest.TestCase):
2637+
compression = zipfile.ZIP_STORED
2638+
2639+
2640+
@requires_zlib()
2641+
class DeflateBoundedDecompressTests(AbstractBoundedDecompressTests,
2642+
unittest.TestCase):
2643+
compression = zipfile.ZIP_DEFLATED
2644+
2645+
2646+
@requires_bz2()
2647+
class Bzip2BoundedDecompressTests(AbstractBoundedDecompressTests,
2648+
unittest.TestCase):
2649+
compression = zipfile.ZIP_BZIP2
2650+
2651+
2652+
@requires_lzma()
2653+
class LzmaBoundedDecompressTests(AbstractBoundedDecompressTests,
2654+
unittest.TestCase):
2655+
compression = zipfile.ZIP_LZMA
2656+
2657+
2658+
@requires_zstd()
2659+
class ZstdBoundedDecompressTests(AbstractBoundedDecompressTests,
2660+
unittest.TestCase):
2661+
compression = zipfile.ZIP_ZSTANDARD
2662+
2663+
26222664
class AbstractBadCrcTests:
26232665
def test_testzip_with_bad_crc(self):
26242666
"""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
@@ -726,7 +726,16 @@ def __init__(self):
726726
self._unconsumed = b''
727727
self.eof = False
728728

729-
def decompress(self, data):
729+
@property
730+
def _needs_input(self):
731+
# While the LZMA properties header is still being buffered, more input
732+
# is required; afterwards defer to the wrapped decompressor so a bounded
733+
# decompress() call can be drained across reads.
734+
if self._decomp is None:
735+
return True
736+
return self._decomp.needs_input
737+
738+
def decompress(self, data, max_length=-1):
730739
if self._decomp is None:
731740
self._unconsumed += data
732741
if len(self._unconsumed) <= 4:
@@ -742,7 +751,7 @@ def decompress(self, data):
742751
data = self._unconsumed[4 + psize:]
743752
del self._unconsumed
744753

745-
result = self._decomp.decompress(data)
754+
result = self._decomp.decompress(data, max_length)
746755
self.eof = self._decomp.eof
747756
return result
748757

@@ -802,6 +811,13 @@ def _get_compressor(compress_type, compresslevel=None):
802811
return None
803812

804813

814+
def _decompressor_needs_input(decompressor):
815+
# bz2/zstd expose the stdlib decompressor's public needs_input; the LZMA
816+
# wrapper keeps it private (_needs_input) to avoid adding public API.
817+
needs_input = getattr(decompressor, "needs_input", None)
818+
return decompressor._needs_input if needs_input is None else needs_input
819+
820+
805821
def _get_decompressor(compress_type):
806822
_check_compression(compress_type)
807823
if compress_type == ZIP_STORED:
@@ -1102,8 +1118,15 @@ def _read1(self, n):
11021118
data = self._decompressor.unconsumed_tail
11031119
if n > len(data):
11041120
data += self._read2(n - len(data))
1105-
else:
1121+
elif self._compress_type == ZIP_STORED:
11061122
data = self._read2(n)
1123+
else:
1124+
# bzip2/lzma/zstd: a bounded decompress() call may leave input
1125+
# buffered inside the decompressor; drain that before reading more.
1126+
if _decompressor_needs_input(self._decompressor):
1127+
data = self._read2(n)
1128+
else:
1129+
data = b''
11071130

11081131
if self._compress_type == ZIP_STORED:
11091132
self._eof = self._compress_left <= 0
@@ -1116,8 +1139,13 @@ def _read1(self, n):
11161139
if self._eof:
11171140
data += self._decompressor.flush()
11181141
else:
1119-
data = self._decompressor.decompress(data)
1120-
self._eof = self._decompressor.eof or self._compress_left <= 0
1142+
# Bound the output of a single decompress() call (mirroring the
1143+
# DEFLATE path above) so that a small compressed member cannot
1144+
# expand into one unbounded read.
1145+
data = self._decompressor.decompress(data, max(n, self.MIN_READ_SIZE))
1146+
self._eof = (self._decompressor.eof or
1147+
self._compress_left <= 0 and
1148+
_decompressor_needs_input(self._decompressor))
11211149

11221150
data = data[:self._left]
11231151
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)