gh-157335: Fix out-of-bounds write in mmap.mmap.__setitem__ - #157438
Open
Joekrry wants to merge 2 commits into
Open
gh-157335: Fix out-of-bounds write in mmap.mmap.__setitem__#157438Joekrry wants to merge 2 commits into
Joekrry wants to merge 2 commits into
Conversation
…Assigning to a single index causes mmap.mmap.__setitem__ to validate the index against the object's size, which then converts the assigned value via PyNumber_AsSsize_t(). This invokes arbitrary python code via __index__(). mmap.resize(), which shrinks mapping could point past the end fo the new buffer causing an out of bounds error write.
picnixz
reviewed
Sep 13, 2026
Comment on lines
+1690
to
+1696
| /* value's __index__ may have resized the mmap, invalidating | ||
| * the earlier bounds check on i. */ | ||
| if (i >= self->size) { | ||
| PyErr_SetString(PyExc_IndexError, | ||
| "mmap index out of range"); | ||
| return -1; | ||
| } |
Member
There was a problem hiding this comment.
I think we can have the same issue in the slice-branch when doing PyObject_GetBuffer(). So instead, we could make the checks inside the safe_byte_copy and safe_memcpy functions. Though I don't know if it's an overkill. Can you verify that the slice pah is also not affected by adding tests.
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.
Assigning to a single index in
mmap.mmap.__setitem__validates the index against the object's size, then converts the assigned value viaPyNumber_AsSsize_t(). That conversion can invoke arbitrary Python code through__index__(), and if that code callsmmap.resize()to shrink the mapping, the previously validated index can point past the end of the new, smaller buffer — causing an out-of-bounds write.This re-validates the index against the mmap's current size after the value conversion, right alongside the existing
CHECK_VALID()check that already accounts for reentrancy at that point.Fixes gh-157335.