Skip to content

Commit 9c6cda5

Browse files
miss-islingtonpicnixzasvetlov
authored
[3.13] gh-125969: fix OOB in future_schedule_callbacks due to an evil call_soon (GH-125970) (#125991)
gh-125969: fix OOB in `future_schedule_callbacks` due to an evil `call_soon` (GH-125970) (cherry picked from commit c5b99f5) Co-authored-by: Bénédikt Tran <[email protected]> Co-authored-by: Andrew Svetlov <[email protected]>
1 parent 7b46ae9 commit 9c6cda5

File tree

3 files changed

+47
-17
lines changed

3 files changed

+47
-17
lines changed

Lib/test/test_asyncio/test_futures.py

+33
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,39 @@ def __eq__(self, other):
955955

956956
fut.remove_done_callback(evil())
957957

958+
def test_evil_call_soon_list_mutation(self):
959+
called_on_fut_callback0 = False
960+
961+
pad = lambda: ...
962+
963+
def evil_call_soon(*args, **kwargs):
964+
nonlocal called_on_fut_callback0
965+
if called_on_fut_callback0:
966+
# Called when handling fut->fut_callbacks[0]
967+
# and mutates the length fut->fut_callbacks.
968+
fut.remove_done_callback(int)
969+
fut.remove_done_callback(pad)
970+
else:
971+
called_on_fut_callback0 = True
972+
973+
fake_event_loop = lambda: ...
974+
fake_event_loop.call_soon = evil_call_soon
975+
fake_event_loop.get_debug = lambda: False # suppress traceback
976+
977+
with mock.patch.object(self, 'loop', fake_event_loop):
978+
fut = self._new_future()
979+
self.assertIs(fut.get_loop(), fake_event_loop)
980+
981+
fut.add_done_callback(str) # sets fut->fut_callback0
982+
fut.add_done_callback(int) # sets fut->fut_callbacks[0]
983+
fut.add_done_callback(pad) # sets fut->fut_callbacks[1]
984+
fut.add_done_callback(pad) # sets fut->fut_callbacks[2]
985+
fut.set_result("boom")
986+
987+
# When there are no more callbacks, the Python implementation
988+
# returns an empty list but the C implementation returns None.
989+
self.assertIn(fut._callbacks, (None, []))
990+
958991

959992
@unittest.skipUnless(hasattr(futures, '_CFuture'),
960993
'requires the C _asyncio module')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix an out-of-bounds crash when an evil :meth:`asyncio.loop.call_soon`
2+
mutates the length of the internal callbacks list. Patch by Bénédikt Tran.

Modules/_asynciomodule.c

+12-17
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,6 @@ future_ensure_alive(FutureObj *fut)
352352
static int
353353
future_schedule_callbacks(asyncio_state *state, FutureObj *fut)
354354
{
355-
Py_ssize_t len;
356-
Py_ssize_t i;
357-
358355
if (fut->fut_callback0 != NULL) {
359356
/* There's a 1st callback */
360357

@@ -380,27 +377,25 @@ future_schedule_callbacks(asyncio_state *state, FutureObj *fut)
380377
return 0;
381378
}
382379

383-
len = PyList_GET_SIZE(fut->fut_callbacks);
384-
if (len == 0) {
385-
/* The list of callbacks was empty; clear it and return. */
386-
Py_CLEAR(fut->fut_callbacks);
387-
return 0;
388-
}
389-
390-
for (i = 0; i < len; i++) {
391-
PyObject *cb_tup = PyList_GET_ITEM(fut->fut_callbacks, i);
380+
// Beware: An evil call_soon could change fut->fut_callbacks.
381+
// The idea is to transfer the ownership of the callbacks list
382+
// so that external code is not able to mutate the list during
383+
// the iteration.
384+
PyObject *callbacks = fut->fut_callbacks;
385+
fut->fut_callbacks = NULL;
386+
Py_ssize_t n = PyList_GET_SIZE(callbacks);
387+
for (Py_ssize_t i = 0; i < n; i++) {
388+
assert(PyList_GET_SIZE(callbacks) == n);
389+
PyObject *cb_tup = PyList_GET_ITEM(callbacks, i);
392390
PyObject *cb = PyTuple_GET_ITEM(cb_tup, 0);
393391
PyObject *ctx = PyTuple_GET_ITEM(cb_tup, 1);
394392

395393
if (call_soon(state, fut->fut_loop, cb, (PyObject *)fut, ctx)) {
396-
/* If an error occurs in pure-Python implementation,
397-
all callbacks are cleared. */
398-
Py_CLEAR(fut->fut_callbacks);
394+
Py_DECREF(callbacks);
399395
return -1;
400396
}
401397
}
402-
403-
Py_CLEAR(fut->fut_callbacks);
398+
Py_DECREF(callbacks);
404399
return 0;
405400
}
406401

0 commit comments

Comments
 (0)