Skip to content

Commit e15f90e

Browse files
committed
Reject Packer buffer exports while packing in the C extension
Packer only checked for live buffer exports on entry to pack(), pack_map_pairs() and pack_ext_type(). Python code that runs during packing (a default callback, the pairs iterable, a list or dict subclass) could call memoryview(packer), after which msgpack_pack_write() reallocated pk.buf and left the export pointing at freed memory. Refuse exports from __getbuffer__ while one of those methods is running. The pure Python Packer already raises BufferError in this case because BytesIO cannot be resized while exported.
1 parent 8a88e3d commit e15f90e

2 files changed

Lines changed: 41 additions & 11 deletions

File tree

‎msgpack/_packer.pyx‎

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ cdef class Packer:
106106
cdef object _berrors
107107
cdef const char *unicode_errors
108108
cdef size_t exports # number of exported buffers
109+
cdef size_t packing # nesting depth of pack calls that can run Python code
109110
cdef bint strict_types
110111
cdef bint use_float
111112
cdef bint autoreset
@@ -118,6 +119,7 @@ cdef class Packer:
118119
self.pk.buf_size = buf_size
119120
self.pk.length = 0
120121
self.exports = 0
122+
self.packing = 0
121123

122124
def __dealloc__(self):
123125
PyMem_Free(self.pk.buf)
@@ -273,11 +275,14 @@ cdef class Packer:
273275
def pack(self, object obj):
274276
cdef int ret
275277
self._check_exports()
278+
self.packing += 1
276279
try:
277280
ret = self._pack(obj, DEFAULT_RECURSE_LIMIT)
278281
except:
279282
self.pk.length = 0
280283
raise
284+
finally:
285+
self.packing -= 1
281286
if ret: # should not happen.
282287
raise RuntimeError("internal error")
283288
if self.autoreset:
@@ -288,10 +293,14 @@ cdef class Packer:
288293
@cython.critical_section
289294
def pack_ext_type(self, typecode, data):
290295
self._check_exports()
291-
if len(data) > ITEM_LIMIT:
292-
raise ValueError("ext data too large")
293-
msgpack_pack_ext(&self.pk, typecode, len(data))
294-
msgpack_pack_raw_body(&self.pk, data, len(data))
296+
self.packing += 1
297+
try:
298+
if len(data) > ITEM_LIMIT:
299+
raise ValueError("ext data too large")
300+
msgpack_pack_ext(&self.pk, typecode, len(data))
301+
msgpack_pack_raw_body(&self.pk, data, len(data))
302+
finally:
303+
self.packing -= 1
295304
if self.autoreset:
296305
buf = PyBytes_FromStringAndSize(self.pk.buf, self.pk.length)
297306
self.pk.length = 0
@@ -332,13 +341,17 @@ cdef class Packer:
332341
(`len(pairs)` and `for k, v in pairs:` should be supported.)
333342
"""
334343
self._check_exports()
335-
size = len(pairs)
336-
if size > ITEM_LIMIT:
337-
raise ValueError("map too large")
338-
msgpack_pack_map(&self.pk, size)
339-
for k, v in pairs:
340-
self._pack(k)
341-
self._pack(v)
344+
self.packing += 1
345+
try:
346+
size = len(pairs)
347+
if size > ITEM_LIMIT:
348+
raise ValueError("map too large")
349+
msgpack_pack_map(&self.pk, size)
350+
for k, v in pairs:
351+
self._pack(k)
352+
self._pack(v)
353+
finally:
354+
self.packing -= 1
342355
if self.autoreset:
343356
buf = PyBytes_FromStringAndSize(self.pk.buf, self.pk.length)
344357
self.pk.length = 0
@@ -367,6 +380,9 @@ cdef class Packer:
367380

368381
@cython.critical_section
369382
def __getbuffer__(self, Py_buffer *buffer, int flags):
383+
# Packing may reallocate pk.buf, which would leave the export dangling.
384+
if self.packing:
385+
raise BufferError("Packer cannot export its buffer while packing")
370386
PyBuffer_FillInfo(buffer, self, self.pk.buf, self.pk.length, 1, flags)
371387
self.exports += 1
372388

‎test/test_buffer.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,17 @@ def test_packer_getbuffer():
4747
buffer.release()
4848
packer.pack(42)
4949
assert bytes(packer) == b"\x92*\xa5hello*"
50+
51+
52+
def test_packer_getbuffer_while_packing():
53+
views = []
54+
55+
def default(obj):
56+
views.append(packer.getbuffer())
57+
return b"x" * 1024 # large enough to grow the internal buffer
58+
59+
packer = Packer(autoreset=False, buf_size=16, default=default)
60+
with raises(BufferError):
61+
packer.pack([object()])
62+
with raises(BufferError):
63+
packer.pack_map_pairs([(object(), 1)])

0 commit comments

Comments
 (0)