GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||
GeezerVison is showing github.com — gh-157335: Fix out-of-bounds write in mmap.mmap.__setitem__ by Joekrry · Pull Request #157438 · python/cpython Conversation
Contributor
Assigning to a single index in This re-validates the index against the mmap's current size after the value conversion, right alongside the existing Fixes gh-157335.
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions…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.
|
There was an error while loading. Please reload this page.
| /* 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; | ||
| } |
There was a problem 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.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I fixed the slice path so PyObject_GetBuffer is called first, then computes bounds with PySlice_AdjustIndices against the size after that call. Added a test for this as well.
I didn't move the checks because the check still has to occur at the call.
Hopefully my understanding of this is correct
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Fix the same reentrancy issue in the slice-assignment path: acquiring the value's buffer (e.g. via a __buffer__ method) can also run arbitrary code that resizes the mmap, invalidating the previously computed slice bounds. Re-validate the bounds after acquiring the buffer, before copying into the mapping. Clarify why the single-item path still needs a bounds check both before and after converting the value: the earlier check preserves existing error precedence (IndexError before TypeError, per test_basic), while the later one is a narrow revalidation of just the upper bound.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Thanks, the PR looks better with checks moved after functions which can call arbitrary Python code. New review: you can now remove redundant CHECK_VALID() check.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Co-authored-by: Victor Stinner <[email protected]>
…faMah.rst Co-authored-by: Victor Stinner <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
@serhiy-storchaka: Do you want to double check this mmap fix?
@Joekrry: I'm not sure why you're merging the main branch into your branch so often, it's not need and it makes the review harder to follow :-(
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Sorry, @Joekrry and @vstinner, I could not cleanly backport this to |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
GH-158026 is a backport of this pull request to the 3.15 branch. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
GH-158027 is a backport of this pull request to the 3.14 branch. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
GH-158028 is a backport of this pull request to the 3.13 branch. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Merged. Thanks for the fix @Joekrry! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
…157438) (#158028) gh-157335: Fix out-of-bounds write in mmap.mmap.__setitem__ (#157438) Fix out-of-bounds write in mmap.mmap.__setitem__() that could occur when converting the index or the assigned value (via __index__() for a single item, or via the buffer protocol for a slice) resized or closed the mmap object during the assignment. (cherry picked from commit 09bf4c5) Co-authored-by: Joseph Kerry <[email protected]>
…H-157438) (#158027) gh-157335: Fix out-of-bounds write in mmap.mmap.__setitem__ (GH-157438) Fix out-of-bounds write in mmap.mmap.__setitem__() that could occur when converting the index or the assigned value (via __index__() for a single item, or via the buffer protocol for a slice) resized or closed the mmap object during the assignment. (cherry picked from commit 09bf4c5) Co-authored-by: Joseph Kerry <[email protected]> Co-authored-by: Victor Stinner <[email protected]>
…H-157438) (#158026) Co-authored-by: Joseph Kerry <[email protected]> Co-authored-by: Victor Stinner <[email protected]>
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
mmap.mmap.__setitem__ crashes when concurrently resized
There was an error while loading. Please reload this page.