bytearray.strip() reads freed memory if the argument's __release_buffer__ resizes the bytearray

Aberta
#161,688 0 comentários 0 reações 0 responsáveis Ver no GitHub

Mantenedores costumam responder em até 1 dia

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
4/5
Tempo estimado
3-5 dias
Facilidade para iniciantes
25/100
Tipo de issue
Bug
Clareza
Claramente especificada
Status de atividade
Estagnada
Stack de tecnologia
c, python
Domínio
backend

Direção de pesquisa

Read the strip implementation at Objects/bytearrayobject.c:2460-2462 and inspect the existing bytearray strip tests. Check how buffer release can run Python code and resize the bytearray before the result is copied. Done when strip(), lstrip(), and rstrip() avoid the reported crash and return bytes consistent with the bounds they computed.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

interpreter-core type-crash
Crash report

bytearray.strip(chars) (and lstrip/rstrip) takes a pointer into the bytearray, finds the bounds, then releases the argument's buffer, and only then copies the bytes. If the argument's class defines __release_buffer__ (PEP 688), that release runs Python code, which can resize the bytearray and free the storage the pointer refers to.

ba = bytearray(b"x" * (1 << 22))

class S:
    def __buffer__(self, flags):
        return memoryview(b" ")
    def __release_buffer__(self, view):
        ba.clear()

ba.strip(S())   # segfault

Output: Segmentation fault, the same for lstrip and rstrip. With a small bytearray and an in-place write instead of clear(), the result is built from the modified bytes, not the ones the bounds were computed on.

Cause: Objects/bytearrayobject.c:2460-2462 calls PyBuffer_Release(&vbytes) before PyByteArray_FromStringAndSize(myptr + left, right - left). Copying first and releasing after would fix it.

CPython versions tested on:

3.15, CPython main branch

Operating systems tested on:

Linux

Output from running 'python -VV' on the command line:

Python 3.15.2 (tags/v3.15.2:5e58eaa, Nov 5 2026, 20:20:36) [GCC 11.4.0]


Found while building a Lean 4 model of CPython (https://broyojo.com/python-formalization/). AI tools (Claude and Codex) helped investigate the findings and prepare this report. The examples were rerun on CPython 3.15.2 and main, and the report received technical review. Record: bytesobj-bytearray-strip-release-uaf

Linked PRs
  • gh-161701
Linguagem predominante
Python
Estrelas
78.2k
Forks
36.6k
Merge médio
19h 57min
PRs com merge (30d)
630

Preparar o ambiente

Abrir no Codespaces

Inicia o contêiner de desenvolvimento do projeto no navegador, com a sua própria conta do GitHub.

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de python/cpython

Todas as issues de python/cpython

Issues semelhantes

Mais issues de Python

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.