# returns an empty list but the C implementation returns None.
self.assertIn(fut._callbacks, (None, []))
+ def test_use_after_free_on_fut_callback_0_with_evil__eq__(self):
+ # Special thanks to Nico-Posada for the original PoC.
+ # See https://github.com/python/cpython/issues/125966.
+
+ fut = self._new_future()
+
+ class cb_pad:
+ def __eq__(self, other):
+ return True
+
+ class evil(cb_pad):
+ def __eq__(self, other):
+ fut.remove_done_callback(None)
+ return NotImplemented
+
+ fut.add_done_callback(cb_pad())
+ fut.remove_done_callback(evil())
+
def test_use_after_free_on_fut_callback_0_with_evil__getattribute__(self):
# see: https://github.com/python/cpython/issues/125984
ENSURE_FUTURE_ALIVE(state, self)
if (self->fut_callback0 != NULL) {
- int cmp = PyObject_RichCompareBool(self->fut_callback0, fn, Py_EQ);
+ // Beware: An evil PyObject_RichCompareBool could free fut_callback0
+ // before a recursive call is made with that same arg. For details, see
+ // https://github.com/python/cpython/pull/125967#discussion_r1816593340.
+ PyObject *fut_callback0 = Py_NewRef(self->fut_callback0);
+ int cmp = PyObject_RichCompareBool(fut_callback0, fn, Py_EQ);
+ Py_DECREF(fut_callback0);
if (cmp == -1) {
return NULL;
}