Skip to content

gh-148660: Fix use-after-free in OrderedDict.copy() on reentrant mutation#151573

Open
gpshead wants to merge 1 commit into
python:mainfrom
gpshead:gh-148660-odict-copy-uaf-minimal
Open

gh-148660: Fix use-after-free in OrderedDict.copy() on reentrant mutation#151573
gpshead wants to merge 1 commit into
python:mainfrom
gpshead:gh-148660-odict-copy-uaf-minimal

Conversation

@gpshead

@gpshead gpshead commented Jun 17, 2026

Copy link
Copy Markdown
Member

OrderedDict.copy() walks the internal linked list while building the new dict. The loop body can run arbitrary Python (a key's __eq__/__hash__, or a subclass __getitem__/__setitem__) which can clear the source dict and free the nodes being iterated.

Detect this the same way OrderedDict.__eq__ already does (gh-119004): snapshot od_state before the loop, hold a strong reference to the key and read the hash before any reentrant call, and raise RuntimeError if the state changed before advancing to the next node.

…t mutation

OrderedDict.copy() walks the internal linked list while building the new
dict. The loop body can run arbitrary Python (a key's __eq__/__hash__, or
a subclass __getitem__/__setitem__) which can clear the source dict and
free the nodes being iterated.

Detect this the same way OrderedDict.__eq__ already does (pythongh-119004):
snapshot od_state before the loop, hold a strong reference to the key and
read the hash before any reentrant call, and raise RuntimeError if the
state changed before advancing to the next node.
@gpshead gpshead added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels Jun 17, 2026
@gpshead gpshead self-assigned this Jun 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant