Skip to content

GH-159101: Fix UAF when tp_finalize changes the base class - #159888

Merged
sergey-miryanov merged 7 commits into
python:mainfrom
sergey-miryanov:bug/159101-uaf-after-finalize
Nov 7, 2026
Merged

sergey-miryanov merged 7 commits into
python:mainfrom
sergey-miryanov:bug/159101-uaf-after-finalize

Conversation

@sergey-miryanov

@sergey-miryanov sergey-miryanov commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

There are two issues to address.

First, we fix the UAF in PyObject_CallFinalizer. We set the FINALIZED flag before calling tp_finalize, so we don't need to call _PyType_IS_GC again. Both gc.c and gc_free_threading.c do the same.

Second, after calling PyObject_CallFinalizerFromDealloc, we extract the type of self again to get the updated type, and the rest works correctly. We do the same for tp_del later in the code.

@sergey-miryanov

Copy link
Copy Markdown
Contributor Author

@nascheme @pablogsal Could you please take a look?

@nascheme nascheme left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@sergey-miryanov sergey-miryanov added needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes labels Nov 7, 2026
@sergey-miryanov
sergey-miryanov enabled auto-merge (squash) November 7, 2026 05:45
@sergey-miryanov
sergey-miryanov merged commit 6c5596c into python:main Nov 7, 2026
56 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @sergey-miryanov for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented Nov 7, 2026

Copy link
Copy Markdown

GH-161534 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 bugs and security fixes label Nov 7, 2026
@bedevere-app

bedevere-app Bot commented Nov 7, 2026

Copy link
Copy Markdown

GH-161535 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Nov 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants