Skip to content

[Python] ResizableBuffer.resize() invalidates exported buffer views (use-after-free on a public API) #55955

Backlog

Description

@nulltracer-maker

Describe the bug, including details regarding any error messages, version, and platform.

Summary

pyarrow.Buffer.__getbuffer__ fills the exported Py_buffer.buf/len once, at export time.
ResizableBuffer.resize() then reallocates the backing allocation without checking whether the
buffer is currently exported. Any Python code holding a memoryview / np.frombuffer view over
the buffer keeps reading and writing the freed allocation after resize().

This was reported privately first; the PMC asked for it to be filed publicly as a regular bug
rather than a security report (the trigger is API usage, not ingestion of untrusted IPC/Parquet/CSV
data, so it falls outside the documented threat model). Filing it here as requested — the defect
itself is still a memory-safety bug on a public API.

Reproduction (crash-free, no exploitation scaffolding)

import pyarrow as pa

b = pa.allocate_buffer(4096, resizable=True)
mv = memoryview(b)
mv[:] = b"A" * 4096

old = b.address
b.resize(1 << 20)                     # reallocates; old block freed
print("moved:", b.address != old)     # True

head = bytes(mv[:16])                 # reads the FREED block
print(head.hex())                     # not "A"*16 — allocator metadata / reused data

Observed on pyarrow 26.0.1, python 3.12, linux/amd64 (glibc 2.41):

moved: True
0000000000000000000000a0be6005010000000...

With pa.set_memory_pool(pa.system_memory_pool()) (for a glibc-visible allocation), the freed
block is immediately handed to the next same-size Python allocation; writes through the stale
view then land inside that live object:

ba = bytearray(b"A" * 4096)           # or any allocation in the same size class
mv[:] = b"Z" * 4096                   # write through the stale view after resize
# the "Z"s are observable in whatever reused the freed block

The same pattern applies to memoryview(b), np.frombuffer(b) and any Arrow-backed consumer that
holds a view while the buffer grows.

Root cause

python/pyarrow/io.pxi:1591 (Buffer.__getbuffer__):

buffer.buf = <char *>self.buffer.get().data()
buffer.len = self.size

set once at export; never refreshed.

python/pyarrow/io.pxi:1625 (ResizableBuffer.resize):

with nogil:
    check_status((<CResizableBuffer*> self.buffer.get())
                 .Resize(new_size, c_shrink_to_fit))

no export check.

cpp/src/arrow/memory_pool.cc:944-962 (PoolBuffer::Resize -> Reserve ->
MemoryPool::Reallocate): allocates a new block, copies, frees the old one — unconditionally.

CPython's buffer protocol requires the memory to remain valid until PyBuffer_Release. The
allocator may therefore free memory that Python still owns a pointer to.

Why this is a defect, not just a documented limitation

  • The repository already implements the correct guard in a sibling class:
    MemoryMap::Resize (cpp/src/arrow/io/file.cc:491) refuses to resize while
    region_.use_count() > 1.
  • numpy raises BufferError: Existing exports of data: object cannot be re-sized for the identical
    situation, i.e. this is the established behaviour in the Python ecosystem.
  • The docstring documents no such restriction: "Resize buffer to indicated size".

So the inconsistency is internal to the repository: one buffer class protects exported memory, the
other does not.

Suggested patch

Raise while exports exist (mirroring MemoryMap):

--- a/python/pyarrow/io.pxi
+++ b/python/pyarrow/io.pxi
@@ class ResizableBuffer(Buffer):
     def resize(self, int64_t new_size, shrink_to_fit=False):
         """
         ...
         """
+        if self.buffer.get().use_count() > 1:
+            raise BufferError(
+                "buffer is exported; release the memoryview before resizing")
         cdef c_bool c_shrink_to_fit = shrink_to_fit
         with nogil:
             check_status((<CResizableBuffer*> self.buffer.get())
                          .Resize(new_size, c_shrink_to_fit))

(or track buffer-protocol exports explicitly with a counter, as numpy does). Regression test:
mv = memoryview(buf); pytest.raises(BufferError): buf.resize(...).

Versions

pyarrow 26.0.1 (current PyPI release, 2026-11-03) and main as of 2026-11-29; all platforms.
Confirmed with the default memory pool (mimalloc) and with pa.system_memory_pool().

Related

  • The format-level security model explicitly excludes implementation-specific concerns such as
    API usage; the C++ model's untrusted-data section covers the IPC/Parquet/CSV readers only. This
    report is filed publicly on that basis, not as a claim against the threat model.
  • The private report contained a demonstration of the corruption escalating further; that detail is
    intentionally left out here and can be shared privately if it helps justify the fix.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions