← Winnitude! Search  ·  Reader Mode (1752 ms)
Links: Stay in Reader Retro Site Converter Retro Mode: OFF Original Page ↗
GITHUB · 11 LINKS

Free-threaded `OrderedDict` iterator: `odictiter_nextkey()` frees `di->di_odict` inside its own critical section -> UAF write in `Py_END_CRITICAL_SECTION()` · Issue #163208 · python/cpython

Repository navigation

Free-threaded OrderedDict iterator: odictiter_nextkey() frees di->di_odict inside its own critical section -> UAF write in Py_END_CRITICAL_SECTION()  #163208


New issue

New issue

Backlog

Backlog
Free-threaded OrderedDict iterator: odictiter_nextkey() frees di->di_odict inside its own critical section -> UAF write in Py_END_CRITICAL_SECTION() #163208 Labels
pending

type-bug

Description

@nevercodecorrect's profile nevercodecorrect opened on Dec 5, 2026

Bug report

Description

In Objects/odictobject.c:1794-1806 , odictiter_nextkey() enters a critical section on di->di_odict without holding a local strong reference:

done:
    Py_CLEAR(di->di_odict);
    return key;
}

static PyObject *
odictiter_nextkey(odictiterobject *di)
{
    _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(di);
    if (di->di_odict == NULL) {
        return NULL;
    }
    PyObject *res;
    Py_BEGIN_CRITICAL_SECTION(di->di_odict);
    res = odictiter_nextkey_lock_held(di);
    Py_END_CRITICAL_SECTION();
    return res;
}

When an OrderedDict iterator holds the last strong reference to its OrderedDict (e.g., list(iter(OrderedDict({1: 2}))), or any temporary OrderedDict iterated in traceback.py / stdlib), exhausting the iterator reaches done: (L1789) and calls Py_CLEAR(di->di_odict) while di->di_odict->ob_mutex is still locked . Py_CLEAR frees the OrderedDict immediately, and Py_END_CRITICAL_SECTION() at L1804 then executes _PyMutex_Unlock(&freed->ob_mutex) -- an atomic CAS write to freed heap memory.

Reproducer

Tested on python/cpython main (47a261dafc5b84bc1e639d017286e51c1a503183, --disable-gil):

Any case where an OrderedDict iterator holds the last reference, e.g.:

from collections import OrderedDict
list(iter(OrderedDict({1: 2})))      # anonymous OrderedDict, only the iterator refs it
list(OrderedDict({1: 2}).keys())     # same shape via a view's iterator

for k in od: is not affected, because od stays referenced by the caller, so Py_CLEAR is not the last reference.

Direct confirmation

Built at commit 47a261dafc (3.16.0a2+dev) with --disable-gil --with-pydebug --with-address-sanitizer (GIL confirmed off). Instrumenting odictiter_nextkey to read the odict's lock byte around the section:

odict freed inside CS (refcount before Py_CLEAR = 1);
mutex byte: after Py_CLEAR = 1 (LOCKED),  after Py_END_CRITICAL_SECTION = 0

i.e. END_CRITICAL_SECTION flips the LOCKED bit 1 → 0 on an object that Py_CLEAR already freed.

A plain or multi-threaded ASan run prints nothing and exits 0, which is misleading:

  • The free-threaded build allocates objects via mimalloc (and rejects PYTHONMALLOC=malloc/pymalloc), so freed objects never enter ASan's quarantine.
  • The unlock is an inline atomic on ob_mutex._bits, which ASan does not instrument.

The stray unlock therefore lands on freed-but-mapped memory and succeeds silently. It becomes observable (corruption or crash) only once another thread's allocator has recycled that block -- then the unlock flips a bit inside a live object.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels


    pending

    type-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions