gh-143546: Fix heap buffer overflow in set_clear_internal #143628
+38
−1
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.
This PR fixes a heap buffer overflow vulnerability in set_clear_internal (Issue gh-143546).
The Issue
The function set_clear_internal could read past the end of the allocated table buffer if the set's
usedcount became inconsistent with the actual table state. This inconsistency occurs due to re-entrancy: if eq is invoked on a set element during an intersection operation (e.g., set_iand), user code can mutate the set (clearing or resizing it) while the interpreter is still holding pointers to the old table.The Fix
I added a bounds check to the clearing loop in set_clear_internal. The loop condition now strictly enforces
entry < table + oldsizein addition to checkingused > 0. This guarantees that the loop terminates safely before accessing invalid memory, even ifso->usedis corrupted or inconsistent with the current table size.Testing
Added a new regression test test_reentrant_clear_in_iand in Lib/test/test_set.py. This test reproduces the crash scenario by defining a custom object with a re-entrant eq method that clears the set during a
set_iand(&=) operation.