-
-
Notifications
You must be signed in to change notification settings - Fork 33.8k
gh-142884: Fix UAF in array.array.tofile with concurrent mutations
#143238
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 | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1367,6 +1367,23 @@ def test_frombytearray(self): | |||||||||||||||||||
| b = array.array(self.typecode, a) | ||||||||||||||||||||
| self.assertEqual(a, b) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| def test_tofile_concurrent_mutation(self): | ||||||||||||||||||||
| # Keep this test in sync with the implementation in | ||||||||||||||||||||
| # Modules/arraymodule.c:array_array_tofile_impl() | ||||||||||||||||||||
| BLOCKSIZE = 64 * 1024 | ||||||||||||||||||||
fatelei marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||
| victim = array.array('B', b'\0' * (BLOCKSIZE * 2)) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| class Writer: | ||||||||||||||||||||
| cleared = False | ||||||||||||||||||||
| def write(self, data): | ||||||||||||||||||||
| if not self.cleared: | ||||||||||||||||||||
| self.cleared = True | ||||||||||||||||||||
| victim.clear() | ||||||||||||||||||||
| return 0 | ||||||||||||||||||||
|
Comment on lines
+1377
to
+1382
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.
Suggested change
We actually don't care about calling clear() multiple times. |
||||||||||||||||||||
|
|
||||||||||||||||||||
| victim.tofile(Writer()) | ||||||||||||||||||||
|
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.
Suggested change
|
||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| class IntegerNumberTest(NumberTest): | ||||||||||||||||||||
| def test_type_error(self): | ||||||||||||||||||||
| a = array.array(self.typecode) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| :mod:`array`: fix a crash in :mod:`array.array.tofile` when the array is concurrently modified by the writer. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1587,27 +1587,41 @@ static PyObject * | |
| array_array_tofile_impl(arrayobject *self, PyTypeObject *cls, PyObject *f) | ||
| /*[clinic end generated code: output=4560c628d9c18bc2 input=5a24da7a7b407b52]*/ | ||
| { | ||
| Py_ssize_t nbytes = Py_SIZE(self) * self->ob_descr->itemsize; | ||
| /* Write 64K blocks at a time */ | ||
| /* XXX Make the block size settable */ | ||
| int BLOCKSIZE = 64*1024; | ||
| Py_ssize_t nblocks = (nbytes + BLOCKSIZE - 1) / BLOCKSIZE; | ||
| Py_ssize_t i; | ||
fatelei marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Py_ssize_t BLOCKSIZE = 64*1024; | ||
| Py_ssize_t max_items = PY_SSIZE_T_MAX / self->ob_descr->itemsize; | ||
|
|
||
| if (Py_SIZE(self) == 0) | ||
| goto done; | ||
|
|
||
|
|
||
| array_state *state = get_array_state_by_class(cls); | ||
| assert(state != NULL); | ||
|
|
||
| for (i = 0; i < nblocks; i++) { | ||
| char* ptr = self->ob_item + i*BLOCKSIZE; | ||
| Py_ssize_t size = BLOCKSIZE; | ||
| Py_ssize_t offset = 0; | ||
| while (1) { | ||
| Py_ssize_t total_size = Py_SIZE(self); | ||
| if (self->ob_item == NULL || total_size == 0) { | ||
|
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'd rather check nullity of |
||
| break; | ||
| } | ||
|
|
||
| if (total_size > max_items) { | ||
| return PyErr_NoMemory(); | ||
|
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. Why raise MemoryError? just break the loop. I think you can just do |
||
| } | ||
|
|
||
| Py_ssize_t current_nbytes = total_size * self->ob_descr->itemsize; | ||
| if (offset >= current_nbytes) { | ||
| break; | ||
| } | ||
|
|
||
| Py_ssize_t size = current_nbytes - offset; | ||
|
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'd prefer reverting this computation, that is, |
||
| if (size > BLOCKSIZE) { | ||
| size = BLOCKSIZE; | ||
| } | ||
|
|
||
| char* ptr = self->ob_item + offset; | ||
| PyObject *bytes, *res; | ||
|
|
||
| if (i*BLOCKSIZE + size > nbytes) | ||
| size = nbytes - i*BLOCKSIZE; | ||
| bytes = PyBytes_FromStringAndSize(ptr, size); | ||
| if (bytes == NULL) | ||
| return NULL; | ||
|
|
@@ -1616,6 +1630,8 @@ array_array_tofile_impl(arrayobject *self, PyTypeObject *cls, PyObject *f) | |
| if (res == NULL) | ||
| return NULL; | ||
| Py_DECREF(res); /* drop write result */ | ||
|
|
||
| offset += size; | ||
| } | ||
|
|
||
| done: | ||
|
|
||
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.