Skip to content

Commit 465b8d8

Browse files
committed
fix out-of-bounds read when packing ext data with overridden __len__
The ext packing paths took the body pointer from the bytes object but its length from len(), which a bytes subclass can override, so msgpack_pack_raw_body copied past the buffer. Size the body with Py_SIZE of the same object in _pack_inner and pack_ext_type.
1 parent 98f010e commit 465b8d8

2 files changed

Lines changed: 40 additions & 5 deletions

File tree

‎msgpack/_packer.pyx‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ cdef class Packer:
157157
cdef const char* rawval
158158
cdef Py_ssize_t L
159159
cdef Py_buffer view
160+
cdef object odata
160161
cdef bint strict = self.strict_types
161162

162163
if o is None:
@@ -213,8 +214,12 @@ cdef class Packer:
213214
self._pack(v, nest_limit)
214215
elif type(o) is ExtType if strict else isinstance(o, ExtType):
215216
# This should be before Tuple because ExtType is namedtuple.
216-
rawval = o.data
217-
L = len(o.data)
217+
# Size the body from the bytes object itself. len() can be
218+
# overridden on a bytes subclass so it disagrees with rawval,
219+
# which would make msgpack_pack_raw_body read past the buffer.
220+
odata = o.data
221+
rawval = odata
222+
L = Py_SIZE(odata)
218223
if L > ITEM_LIMIT:
219224
raise ValueError("EXT data is too large")
220225
msgpack_pack_ext(&self.pk, <long>o.code, L)
@@ -287,11 +292,18 @@ cdef class Packer:
287292

288293
@cython.critical_section
289294
def pack_ext_type(self, typecode, data):
295+
cdef const char *rawval
296+
cdef Py_ssize_t L
290297
self._check_exports()
291-
if len(data) > ITEM_LIMIT:
298+
# Take the body length from the bytes object, not len(): a bytes
299+
# subclass can override __len__ so it no longer matches rawval and
300+
# msgpack_pack_raw_body reads past the buffer.
301+
rawval = data
302+
L = Py_SIZE(data)
303+
if L > ITEM_LIMIT:
292304
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))
305+
msgpack_pack_ext(&self.pk, typecode, L)
306+
msgpack_pack_raw_body(&self.pk, rawval, L)
295307
if self.autoreset:
296308
buf = PyBytes_FromStringAndSize(self.pk.buf, self.pk.length)
297309
self.pk.length = 0

‎test/test_extension.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,29 @@ def test_pack_ext_type_autoreset():
5757
assert packer.bytes() == b""
5858

5959

60+
@pytest.mark.skipif(
61+
msgpack.Packer.__module__ == "msgpack.fallback",
62+
reason="out-of-bounds read is specific to the C extension",
63+
)
64+
def test_pack_ext_type_len_override():
65+
# A bytes subclass whose __len__ over-reports its size must not make the
66+
# packer copy more than the real buffer.
67+
class BadLen(bytes):
68+
def __len__(self):
69+
return 4096
70+
71+
data = BadLen(b"AB") # real length 2
72+
assert bytes.__len__(data) == 2
73+
74+
packed = msgpack.packb(ExtType(0x42, data))
75+
assert packed == b"\xd5\x42AB" # fixext 2, sized by the real buffer
76+
assert msgpack.unpackb(packed) == ExtType(0x42, b"AB")
77+
78+
packer = msgpack.Packer(autoreset=False)
79+
packer.pack_ext_type(0x42, data)
80+
assert packer.bytes() == b"\xd5\x42AB"
81+
82+
6083
def test_unpack_ext_type():
6184
def check(b, expected):
6285
assert msgpack.unpackb(b) == expected

0 commit comments

Comments
 (0)