Skip to content

unpackb() may return corrupted ExtraData.extra for non-contiguous input #720

Description

@marinelay

I found an issue in unpackb() while fuzzing Python C extension modules with Atheris and ASan/UBSan enabled.

The sanitizer initially reported memcpy-param-overlap, but the problem is not limited to sanitizer builds. The same input also produces incorrect output with the PyPI wheel, without crashing or emitting a warning.

Environment

  • msgpack 1.2.1
  • CPython 3.12
  • Linux x86_64

Reproducer

import msgpack

# A strided (non-contiguous) view whose logical content is b"\x00abc":
#   \x00 -> the integer 0
#   abc  -> trailing bytes, so unpackb raises ExtraData
buf = bytearray(b"\x00\xffa\xffb\xffc\xff")
view = memoryview(buf)[::2]
assert bytes(view) == b"\x00abc"

try:
    msgpack.unpackb(view)
except msgpack.ExtraData as e:
    print(e.unpacked)   # 0        -- correct
    print(e.extra)      # b'ab\x00' -- expected b'abc'

ASan report

AddressSanitizer: memcpy-param-overlap: memory ranges
  [0x7a2549f75340,0x7a2549f75343) and [0x7a2549f75341,0x7a2549f75344) overlap
    #1 PyBytes_FromStringAndSize
    #2 __pyx_pf_7msgpack_9_cmsgpack_2unpackb  msgpack/_unpacker.pyx:199

This looks like a bug to me, but I don't know the codebase well enough to rule out that it's expected for non-contiguous input.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions