Skip to content

gh-162593: Fix use-after-free in FrameLocalsProxy item assignment - #162615

Open
TINGyu123644 wants to merge 1 commit into
python:mainfrom
TINGyu123644:fix/framelocalsproxy-relocation-uaf
Open

TINGyu123644 wants to merge 1 commit into
python:mainfrom
TINGyu123644:fix/framelocalsproxy-relocation-uaf

Conversation

@TINGyu123644

@TINGyu123644 TINGyu123644 commented Nov 25, 2026 •

Copy link
Copy Markdown

Fixes #162593

FrameLocalsProxy.__setitem__ (framelocalsproxy_setitem() in Objects/frameobject.c) caches a pointer to the frame's locals array before framelocalsproxy_getkeyindex() performs the key lookup. That lookup calls PyObject_Hash() / PyObject_RichCompareBool(), which run arbitrary Python code.

If that code closes the generator owning the frame, _PyFrame_ClearExceptCode() calls take_ownership() and relocates the _PyInterpreterFrame into the frame object, repointing frame->f_frame. The cached fast pointer then refers to the abandoned copy: the new value is written there (lost), and PyStackRef_CLOSE(fast[i]) releases a reference now owned by the relocated frame. The result is a premature deallocation and a deterministic interpreter-shutdown segfault.

The read path does not have this problem because framelocalsproxy_getval() receives frame->f_frame afresh. The fix applies the same discipline to the write path: move the fast / co fetch to after the key lookup, so it always reflects the (possibly relocated) current frame.

Verification performed:

  • Reproduced the reported crash locally on CPython 3.13.14 (5/5 runs, exit 139), with the documented control (g.close() before the assignment) exiting cleanly.
  • Confirmed the root cause line-by-line against the current Objects/frameobject.c and Python/frame.c (take_ownership() / _PyFrame_ClearExceptCode()).
  • Built a standalone C model of the refcount ownership transfer: the cached-pointer version raises a use-after-free (release of an already-freed object), while re-fetching the locals array after the lookup is clean.
  • Added a regression test (test_setitem_after_frame_relocation) asserting the assignment reaches the relocated frame and the generator is closed.
  • Added a NEWS entry under Core_and_Builtins.

No existing tests were modified or removed; the test change is purely additive.

Re-fetch the frame locals array after the key lookup, which may run
arbitrary code that relocates the frame.

Signed-off-by: TINGyu123644 <tingyu123644@users.noreply.github.com>
@python-cla-bot

Copy link
Copy Markdown

The following commit authors need to sign the Contributor License Agreement:

CLA not signed

@bedevere-app

bedevere-app Bot commented Nov 25, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

segfault in FrameLocalsProxy

1 participant