FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix: use-after-free in unpackb() ExtraData path for non-contiguous input by ThomasWaldmann · Pull Request #722 · msgpack/msgpack-python · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) .pyx  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
14 changes: 8 additions & 6 deletions msgpack/_unpacker.pyx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ def unpackb(object packed, *, object object_hook=None, object list_hook=None,
cdef char* buf = NULL
cdef Py_ssize_t buf_len
cdef const char* cerr = NULL
cdef object extra = None

if unicode_errors is not None:
cerr = unicode_errors
Expand All @@ -190,15 +191,16 @@ def unpackb(object packed, *, object object_hook=None, object list_hook=None,
use_list, raw, timestamp, strict_map_key, cerr,
max_str_len, max_bin_len, max_array_len, max_map_len, max_ext_len)
ret = unpack_construct(&ctx, buf, buf_len, &off)
if ret == 1:
obj = unpack_data(&ctx)
if off < buf_len:
# buf may point into a temporary contiguous copy owned by view,
# so the extra data must be copied out before releasing view.
raise ExtraData(obj, PyBytes_FromStringAndSize(buf+off, buf_len-off))
return obj
finally:
PyBuffer_Release(&view);

if ret == 1:
obj = unpack_data(&ctx)
if off < buf_len:
raise ExtraData(obj, PyBytes_FromStringAndSize(buf+off, buf_len-off))
return obj

unpack_clear(&ctx)
if ret == 0:
raise ValueError("Unpack failed: incomplete input")
Expand Down
21 changes: 20 additions & 1 deletion test/test_memoryview.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

from array import array

from msgpack import packb, unpackb
from pytest import raises

from msgpack import ExtraData, packb, unpackb


def make_array(f, data):
Expand Down Expand Up @@ -109,3 +111,20 @@ def test_unpack_noncontiguous_memoryview():
noncont = memoryview(bytes(padded))[::2]
assert not noncont.c_contiguous
assert unpackb(noncont) == 2**32


def test_unpack_noncontiguous_memoryview_extra_data():
# See https://github.com/msgpack/msgpack-python/issues/720
# ExtraData.extra must be copied out of the temporary contiguous copy
# before that copy is released.
packed = packb(0) + b"extra"
padded = bytearray()
for byte in packed:
padded.append(byte)
padded.append(0)
noncont = memoryview(bytes(padded))[::2]
assert not noncont.c_contiguous
with raises(ExtraData) as excinfo:
unpackb(noncont)
assert excinfo.value.unpacked == 0
assert excinfo.value.extra == b"extra"

Back | FazBrowse Home | New Git URL