py: Fix integer overflow in sequence repeat that corrupts memory.#19315
Open
P4P3R-HAK wants to merge 1 commit into
Open
py: Fix integer overflow in sequence repeat that corrupts memory.#19315P4P3R-HAK wants to merge 1 commit into
P4P3R-HAK wants to merge 1 commit into
Conversation
The repeat operator (seq * n) computed the result size as len(seq) * n using unchecked size_t arithmetic, allocated a buffer of that (wrapped) size, and then wrote the full len(seq) * n elements via mp_seq_multiply(). When the multiplication overflowed size_t the buffer was under-allocated, causing a heap buffer overflow reachable from pure Python for str, bytes, list and tuple (e.g. b"a" * (n) with a suitable len and n). Check for the overflow before allocating and raise OverflowError, matching CPython, instead of corrupting the heap. Add a regression test. Signed-off-by: P4P3R-HAK <kbfanta@naver.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #19315 +/- ##
=======================================
Coverage 98.47% 98.47%
=======================================
Files 176 176
Lines 22845 22851 +6
=======================================
+ Hits 22497 22503 +6
Misses 348 348 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Code size report: |
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
Fixes #19314.
The sequence-repeat operator (
seq * n) computed the result size aslen(seq) * nusing uncheckedsize_tarithmetic, allocated a buffer of that(wrapped) size, and then wrote the full
len(seq) * nelements viamp_seq_multiply(). When the multiplication overflowedsize_tthe buffer wasunder-allocated, causing a heap buffer overflow reachable from pure Python
for
str,bytes,listandtuple.Example (64-bit):
(b"A" * 65536) * (2**48)wrapslen*nto0, allocates atiny buffer, then copies far past it → crash / memory corruption. On 32-bit
targets the threshold is only
2**32, so an ordinary repeat count is enough(e.g.
(b"x" * 64) * 67108864).Fix
Check for the overflow before allocating and raise
OverflowError(matchingCPython, which raises
OverflowError/MemoryErrorhere), instead of corruptingthe heap. The same guard is added to the three callers of
mp_seq_multiply():py/objstr.c(str/bytes/bytearray)py/objlist.cpy/objtuple.cTesting
tests/basics/sequence_repeat_overflow.py(portable across32/64-bit; output matches CPython).
tests/run-tests.py basics→ 545/545 passed (16926 testcases), no regressions.b"ab"*3,[1,2]*3,(1,2)*3,b"A"*1000000) still work.OverflowErroron the standardunix build, and that an ASAN build reports a
heap-buffer-overflowinmp_seq_multiplybefore the fix.