[3.12] gh-157947: Keep exception fields alive during suggestion lookups - #157973
Closed
nvnzno-a11y wants to merge 2 commits into
Closed
nvnzno-a11y wants to merge 2 commits into
nvnzno-a11y wants to merge 2 commits into
Conversation
… lookups get_suggestions_for_attribute_error(), offer_suggestions_for_name_error() and offer_suggestions_for_import_error() used borrowed references to exc->name/exc->obj/exc->name_from and the traceback frame across calls that can invoke arbitrary Python code (PyObject_Dir, PyImport_GetModule, _PyObject_LookupAttr). That code can rebind or clear the exception's attributes and free the referenced objects, leaving dangling pointers that calculate_suggestions() then dereferences -> use-after-free. Take strong references for the duration of the calls that may run arbitrary code.
|
The following commit authors need to sign the Contributor License Agreement: |
Member
|
The CLA is not signed, and please don't open PRs before a conclusion is reached on the issue. |
Author
|
Understood — apologies for opening this before the discussion on the issue had reached a conclusion. I'll follow #157947 and wait for the maintainers' decision there. Thanks for the heads-up. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
get_suggestions_for_attribute_error(),offer_suggestions_for_name_error()andoffer_suggestions_for_import_error()inPython/suggestions.cuse borrowedreferences to
exc->name/exc->obj/exc->name_fromand to the tracebackframe across calls that can run arbitrary Python code:
PyObject_Dir(obj)/PyObject_Dir(mod)→__dir__PyImport_GetModule(mod_name)→sys.modulesoverrides_PyObject_LookupAttr(self, name, ...)→__getattr__/__getattribute__That code can rebind or clear the exception's attributes (
exc.name = ...,exc.obj = None,exc.__traceback__ = None) and free the referenced objects.calculate_suggestions()then dereferences the dangling pointers →use-after-free (deterministic SIGSEGV when the freed
nameis a large,mmap-backed string; reproducer in gh-157947).
This only affects 3.12 — the AttributeError suggestion path was refactored
away on 3.13/main.
The fix takes strong references to
name,obj,mod_name,name_fromandframefor the duration of the calls that may execute arbitrary code.Reproducer
On the 3.12 branch this segfaults during exception printing
(
unpatched_exit=139); with the patch the exception prints normally.Tests
test_attribute_error_name_cleared_in_dirinLib/test/test_traceback.py(runs in both the pure-Python and the_testcapiexception-printing variants).{Pure,CPython}SuggestionFormattingTestspass.