itertools.count use-after-free via re-entrant step.__radd__
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Python
- Estrellas
- 77.2k
- Forks
- 35.9k
- Métricas de merge de PR
- Métricas de PR pendientes
Descripción
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
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza siguiendo la ruta de count_nextlong() descrita en el informe y ejecuta el reproducer proporcionado con PYTHONMALLOC=debug. Investiga la llamada reentrante a next() y el tiempo de vida de la referencia alrededor del total acumulado; el trabajo estará terminado cuando el reproducer imprima el valor correcto sin bloquearse.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- c, python
- Área
- backend
- Tipo de issue
- Error
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Tranquilo
- Claridad
- Bastante claro
- Aptitud para principiantes
- 52/100