-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
gh-157335: Fix out-of-bounds write in mmap.mmap.__setitem__ #157438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Fix out-of-bounds write in :meth:`mmap.mmap.__setitem__` that could occur | ||
| when the assigned value's :meth:`~object.__index__` method resized the | ||
| mmap object during the assignment. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1687,6 +1687,13 @@ mmap_ass_subscript_lock_held(PyObject *op, PyObject *item, PyObject *value) | |
| return -1; | ||
| } | ||
| CHECK_VALID(-1); | ||
| /* 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; | ||
| } | ||
|
Comment on lines
+1690
to
+1696
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
|
||
| char v_char = (char) v; | ||
| if (safe_byte_copy(self->data + i, &v_char) < 0) { | ||
|
|
||
There was a problem hiding this comment.
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:
Something like that.