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

Skip to content

GH-97592: Fix crash in C remove_done_callback due to evil code #97660

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

Merged
merged 4 commits into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Lib/test/test_asyncio/test_futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,21 @@ def __eq__(self, other):

fut.remove_done_callback(evil())

def test_remove_done_callbacks_list_clear(self):
# see https://github.com/python/cpython/issues/97592 for details

fut = self._new_future()
fut.add_done_callback(str)

for _ in range(63):
fut.add_done_callback(id)

class evil:
def __eq__(self, other):
fut.remove_done_callback(other)

fut.remove_done_callback(evil())
Comment on lines +844 to +848
Copy link
Contributor

Choose a reason for hiding this comment

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

Class names should normally use the CapWords convention.

Suggested change
class evil:
def __eq__(self, other):
fut.remove_done_callback(other)
fut.remove_done_callback(evil())
class Evil:
def __eq__(self, other):
fut.remove_done_callback(other)
fut.remove_done_callback(Evil())

Copy link
Contributor

Choose a reason for hiding this comment

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

All callbacks added by fut.add_done_callback() will be cleared when calling fut.remove_done_callback(Evil()), but the return value will be the number of callbacks added last.

I'm not sure if we need to improve the return value of remove done callback further, the current changes are good enough to fix the crash. :)


def test_schedule_callbacks_list_mutation_1(self):
# see http://bugs.python.org/issue28963 for details

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Avoid a crash in the C version of :meth:`asyncio.Future.remove_done_callback` when an evil argument is passed.
9 changes: 7 additions & 2 deletions Modules/_asynciomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,11 @@ _asyncio_Future_remove_done_callback(FutureObj *self, PyObject *fn)
return NULL;
}

for (i = 0; i < PyList_GET_SIZE(self->fut_callbacks); i++) {
// Beware: PyObject_RichCompareBool below may change fut_callbacks.
// See GH-97592.
for (i = 0;
self->fut_callbacks != NULL && i < PyList_GET_SIZE(self->fut_callbacks);
i++) {
int ret;
PyObject *item = PyList_GET_ITEM(self->fut_callbacks, i);
Py_INCREF(item);
Expand All @@ -1071,7 +1075,8 @@ _asyncio_Future_remove_done_callback(FutureObj *self, PyObject *fn)
}
}

if (j == 0) {
// Note: fut_callbacks may have been cleared.
if (j == 0 || self->fut_callbacks == NULL) {
Py_CLEAR(self->fut_callbacks);
Py_DECREF(newlist);
return PyLong_FromSsize_t(len + cleared_callback0);
Expand Down