itertools.count use-after-free via re-entrant step.__radd__
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 77.2k
- Fork
- 35.9k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Bug description
itertools.count has a use-after-free when the step object's __radd__ re-enters the iterator.
count_nextlong() borrows lz->long_cnt (the running total) without Py_INCREF, then calls PyNumber_Add(result, step). If step.__radd__ calls next() on the same count, the inner call moves lz->long_cnt to the new value and hands the old total's only reference back, which is then dropped and freed. The outer call still returns that freed total as a dangling pointer.
The step needs __index__ so count() accepts it as a number, and the returned value has to be used to hit the freed memory:
from itertools import count
class Step:
armed = True
def __index__(self): # so count() accepts it as a number
return 1
def __radd__(self, other):
if Step.armed:
Step.armed = False
inner = next(c) # re-enter; steals the running total's ref
f"{inner!r}" # churn the heap so the freed slot is reused
return other + 1
c = count(1 << 100, Step())
val = next(c)
val + 1 # use the returned (dangling) total
Run with PYTHONMALLOC=debug python repro.py -> SIGSEGV. With the fix it prints the correct value.
CPython versions tested on
main
Operating systems tested on
macOS
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia tracciando il percorso di count_nextlong() descritto nel report ed esegui il reproducer fornito con PYTHONMALLOC=debug. Analizza la chiamata rientrante a next() e la durata di vita del riferimento attorno al totale progressivo; il lavoro è completato quando il reproducer stampa il valore corretto senza arrestarsi in modo anomalo.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- c, python
- Ambito
- backend
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Tranquilla
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 52/100