Repository navigation
gh-163185: Fix use-after-free in functools.cmp_to_key - #163212
Open
abedalawieh wants to merge 1 commit into
Open
abedalawieh wants to merge 1 commit into
abedalawieh wants to merge 1 commit into
Conversation
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.
keyobject_richcompare()passed the borrowedlhs->objectandrhs->objecttoPyObject_Vectorcall(). If the comparison replaces theobjattribute of a key, the wrapped object can be freed while it is still in use. Withoperator.ltas the comparison function, the reflected check indo_richcompare()then reads freed memory.The fix takes strong references to both objects for the duration of the call. The reads are done inside
Py_BEGIN_CRITICAL_SECTION2(self, other), because theobjmember setter stores under a critical section on free-threaded builds. On default builds the critical section compiles to nothing.The new test in
test_functools(it runs for both the C and Python implementations) reports a heap-use-after-free under ASan without the fix.test_functoolsandtest_itertoolspass on a--with-pydebug --with-address-sanitizerbuild.The same code is on 3.14 and 3.15, so this probably needs backports there.
functools.cmp_to_key: Use-after-free inkeyobject_richcompare()due to borrowedkey->objarguments passed toPyObject_Vectorcall#163185