Skip to content

Conversation

@benjaminJohnson2204
Copy link
Contributor

@benjaminJohnson2204 benjaminJohnson2204 commented Jan 15, 2024

This PR fixes a use-after-free error when a callback registered with atexit.register() calls atexit.unregister() or atexit._clear() in its __eq__ method, which is called by the atexit_register() C API.

I fixed the issue by increasing the refcounts of both arguments to the __eq__ check before the equality check, and decrementing them afterward.

I also added additional unit tests to test that these cases do not cause crashes.

I don't think this needs a news entry.

Issue: #112127

@ghost
Copy link

ghost commented Jan 15, 2024

All commit authors signed the Contributor License Agreement.
CLA signed

@bedevere-app
Copy link

bedevere-app bot commented Jan 15, 2024

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.

@bedevere-app
Copy link

bedevere-app bot commented Jan 15, 2024

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.

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

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

Thanks! I have several comments.

@bedevere-app
Copy link

bedevere-app bot commented Feb 13, 2024

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.

@bedevere-app
Copy link

bedevere-app bot commented Feb 13, 2024

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.

@bedevere-app
Copy link

bedevere-app bot commented Feb 13, 2024

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.

@bedevere-app
Copy link

bedevere-app bot commented Jul 17, 2024

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.

@bedevere-app
Copy link

bedevere-app bot commented Jul 18, 2024

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.

@benjaminJohnson2204
Copy link
Contributor Author

Thanks for the feedback @serhiy-storchaka ! I updated my PR based on your comments, could you take another look when you get a chance please?

@bedevere-app
Copy link

bedevere-app bot commented Jul 18, 2024

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.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

LGTM.

Copy link
Contributor

@kumaraditya303 kumaraditya303 left a comment

Choose a reason for hiding this comment

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

There are merge conflicts

@bedevere-app
Copy link

bedevere-app bot commented Jul 3, 2025

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@serhiy-storchaka
Copy link
Member

There is yet one issue here. Even if it no longer crashes, it can unregister wrong function if other function was unregistered during execution of __eq__().

After finding the callback, we should check that the tuple at the current position is the same, and if it is not, iterate back until we find it (items can only be removed, not inserted before the current position, unless we play with gc.get_objects()).

@serhiy-storchaka serhiy-storchaka self-requested a review December 17, 2025 09:45
Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

Also, currently the test is passed with unpatched code.

cnt += 1
if cnt == 1:
self.action(o)
return self.eq_ret_val(o)
Copy link
Member

Choose a reason for hiding this comment

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

self.eq_ret_val is not callable.

@serhiy-storchaka serhiy-storchaka self-assigned this Dec 17, 2025
@bedevere-app
Copy link

bedevere-app bot commented Dec 17, 2025

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.

@serhiy-storchaka serhiy-storchaka merged commit 2b466c4 into python:main Dec 17, 2025
49 of 50 checks passed
@serhiy-storchaka serhiy-storchaka removed their assignment Dec 17, 2025
@serhiy-storchaka serhiy-storchaka added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels Dec 17, 2025
@miss-islington-app
Copy link

Thanks @benjaminJohnson2204 for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖

@miss-islington-app
Copy link

Thanks @benjaminJohnson2204 for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

@miss-islington-app
Copy link

Sorry, @benjaminJohnson2204 and @serhiy-storchaka, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 2b466c47c333106dc9522ab77898e6972e25a2c6 3.13

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Dec 17, 2025
…ythonGH-114092)

(cherry picked from commit 2b466c4)

Co-authored-by: Benjamin Johnson <[email protected]>
Co-authored-by: Serhiy Storchaka <[email protected]>
@bedevere-app
Copy link

bedevere-app bot commented Dec 17, 2025

GH-142878 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 Dec 17, 2025
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this pull request Dec 17, 2025
…ter() (pythonGH-114092)

(cherry picked from commit 2b466c4)

Co-authored-by: Benjamin Johnson <[email protected]>
Co-authored-by: Serhiy Storchaka <[email protected]>
@bedevere-app
Copy link

bedevere-app bot commented Dec 17, 2025

GH-142880 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Dec 17, 2025
kumaraditya303 pushed a commit that referenced this pull request Dec 17, 2025
…H-114092) (#142878)

gh-112127: Fix possible use-after-free in atexit.unregister() (GH-114092)
(cherry picked from commit 2b466c4)

Co-authored-by: Benjamin Johnson <[email protected]>
Co-authored-by: Serhiy Storchaka <[email protected]>
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this pull request Dec 17, 2025
…ter() (pythonGH-114092)

(cherry picked from commit 2b466c4)

Co-authored-by: Benjamin Johnson <[email protected]>
Co-authored-by: Serhiy Storchaka <[email protected]>
serhiy-storchaka added a commit that referenced this pull request Dec 17, 2025
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