Summary
If the default callback obtains a memoryview through getbuffer() and then returns a sufficiently large value, Packer.pack() reallocates its internal buffer while the memoryview still references the old storage.
Accessing the memoryview afterward causes a segmentation fault, and an ASan build reports a heap-use-after-free.
Versions
msgpack 1.2.2, CPython 3.12.3, Ubuntu 24.04 x86_64, glibc 2.39.
Reproducer
import msgpack
packer = None
exported = None
class Unsupported:
pass
def default(obj):
global exported
exported = packer.getbuffer()
return b"A" * (2 * 1024 * 1024)
packer = msgpack.Packer(default=default, autoreset=False)
packer.pack([Unsupported()])
msgpack.unpackb(exported)
Running it produces:
Segmentation fault (core dumped)
Sanitizer result
An ASan build reports a heap-use-after-free in unpack_execute.
The allocation was freed by msgpack_pack_write after the callback exported a memoryview of the buffer.
ERROR: AddressSanitizer: heap-use-after-free
READ of size 1
#0 unpack_execute msgpack/unpack_template.h:161
freed by:
#1 msgpack_pack_write msgpack/pack.h:47
I found this while fuzzing Python C extension modules.
It looks like a bug to me, but I may be missing an intended constraint, so I would appreciate confirmation.
Summary
If the
defaultcallback obtains a memoryview throughgetbuffer()and then returns a sufficiently large value,Packer.pack()reallocates its internal buffer while the memoryview still references the old storage.Accessing the memoryview afterward causes a segmentation fault, and an ASan build reports a heap-use-after-free.
Versions
msgpack 1.2.2, CPython 3.12.3, Ubuntu 24.04 x86_64, glibc 2.39.
Reproducer
Running it produces:
Segmentation fault (core dumped)Sanitizer result
An ASan build reports a heap-use-after-free in
unpack_execute.The allocation was freed by
msgpack_pack_writeafter the callback exported a memoryview of the buffer.I found this while fuzzing Python C extension modules.
It looks like a bug to me, but I may be missing an intended constraint, so I would appreciate confirmation.