bytearray.strip() reads freed memory if the argument's __release_buffer__ resizes the bytearray
Les mainteneurs répondent en général sous 1 jour
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Accessibilité débutants
- 25/100
Piste de recherche
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.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
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
- Langage dominant
- Python
- Étoiles
- 78.2k
- Forks
- 36.6k
- Merge moyen
- 19 h 57 min
- PR mergées (30 j)
- 630
Préparer son environnement
Lance le conteneur de développement du projet dans votre navigateur, avec votre propre compte GitHub.
- Aucun Dockerfile ni fichier Docker Compose
- Propose un modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de python/cpython
-
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
Les mainteneurs répondent en général sous 1 jour
-
stdlib type-bug
Difficulté 2/5 1-3 heures Accessibilité débutants 75/100
Les mainteneurs répondent en général sous 1 jour
-
extension-modules stdlib topic-XML type-bug
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
python/cpython#161807 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour
-
stdlib type-feature
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
python/cpython#161788 · 7 commentaires ·
Les mainteneurs répondent en général sous 1 jour
-
extension-modules topic-IO type-bug
Difficulté 2/5 1-3 heures Accessibilité débutants 75/100
Les mainteneurs répondent en général sous 1 jour
Toutes les issues de python/cpython
Issues similaires
-
Difficulté 2/5 1-3 heures Accessibilité débutants 62/100
cpp-best-practices/project_validator#267 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
Project-N-E-K-O/N.E.K.O#3725 ·
Les mainteneurs répondent en général sous 1 jour
-
[Bug] Sending a Telegram message chain twice duplicates mentionsPeut-être pris @123XieC l’a pris aujourd’hui. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
AstrBotDevs/AstrBot#11125 ·
Les mainteneurs répondent en général sous 1 jour
-
backend bug priority: high
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
nooriegarg/ContribIQ#73 ·
Les mainteneurs répondent en général sous 1 jour
-
bug p3 security
Difficulté 2/5 1-3 heures Accessibilité débutants 68/100
amd/gaia#8197 · 2 commentaires ·
Les mainteneurs répondent en général sous 1 jour