Skip to content

Out-of-bounds write in bytearray.extend() with a reentrant __buffer__ #153578

Closed

Description

@tonghuaroot

Bug report

Bug description:

The C implementation of bytearray.extend() acquires the argument's buffer
(running its __buffer__()) before clamping the append range, then clamps only
the high bound to the current size. A __buffer__ that shrinks the bytearray
leaves the low bound past the high bound, so extend runs a negative-size
memmove (an out-of-bounds write; SIGABRT on a debug build).

b = bytearray(b'x' * 100)
class Evil:
    def __buffer__(self, flags):
        b.clear()
        return memoryview(b'ABCDEFGH')
b.extend(Evil())   # out-of-bounds write

bytearray.__iadd__ re-reads the size after acquiring the buffer; extend()
does not.

CPython versions tested on:

3.13, 3.14, 3.15, 3.16

Operating systems tested on:

macOS

Linked PRs

Activity

  1. added
    3.13only security fixes
    3.14bugs and security fixes
    3.15bugs and security fixes
    3.16new features, bugs and security fixes
    type-crashA hard crash of the interpreter, possibly with a core dump
    on Aug 17, 2026
  2. added a commit that references this issue on Aug 18, 2026
  3. added 2 commits that reference this issue on Aug 18, 2026
  4. added a commit that references this issue on Oct 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.13only security fixes3.14bugs and security fixes3.15bugs and security fixes3.16new features, bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or errortype-crashA hard crash of the interpreter, possibly with a core dump

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions