Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-157335: Fix out-of-bounds write in mmap.mmap.__setitem__ - #157438

Open
Joekrry wants to merge 2 commits into
python:mainfrom
Joekrry:fix-mmap-setitem-resize-reentrancy
Open

gh-157335: Fix out-of-bounds write in mmap.mmap.__setitem__#157438
Joekrry wants to merge 2 commits into
python:mainfrom
Joekrry:fix-mmap-setitem-resize-reentrancy

Conversation

@Joekrry

@Joekrry Joekrry commented Sep 13, 2026

Copy link
Copy Markdown

Assigning to a single index in mmap.mmap.__setitem__ validates the index against the object's size, then converts the assigned value via PyNumber_AsSsize_t(). That conversion can invoke arbitrary Python code through __index__(), and if that code calls mmap.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.

…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.
@python-cla-bot

python-cla-bot Bot commented Sep 13, 2026

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

Comment thread Modules/mmapmodule.c
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;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread Modules/mmapmodule.c
CHECK_VALID(-1);
/* value's __index__ may have resized the mmap, invalidating
* the earlier bounds check on i. */
if (i >= self->size) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to not check the bounds twice. You can reorganize the code instead:

  • call PyNumber_AsSsize_t(item) + error check
  • call PyNumber_AsSsize_t(value) + error check
  • adjust i and check bounds
  • check v bounds
  • call safe_byte_copy()

Something like that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mmap.mmap.__setitem__ crashes when concurrently resized

3 participants