]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-125966: fix use-after-free on `fut->fut_callback0` due to an evil callback's ...
authorBénédikt Tran <10796600+picnixz@users.noreply.github.com>
Sun, 27 Oct 2024 17:10:10 +0000 (18:10 +0100)
committerGitHub <noreply@github.com>
Sun, 27 Oct 2024 17:10:10 +0000 (22:40 +0530)
Lib/test/test_asyncio/test_futures.py
Misc/NEWS.d/next/Library/2024-10-25-10-53-56.gh-issue-125966.eOCYU_.rst [new file with mode: 0644]
Modules/_asynciomodule.c

index bac76b074981dfffdb56495d5ae654148ab5e8fa..3a4291e3a68ca6c4961a1111394dd505a3af9812 100644 (file)
@@ -999,6 +999,24 @@ class BaseFutureDoneCallbackTests():
             # 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
 
diff --git a/Misc/NEWS.d/next/Library/2024-10-25-10-53-56.gh-issue-125966.eOCYU_.rst b/Misc/NEWS.d/next/Library/2024-10-25-10-53-56.gh-issue-125966.eOCYU_.rst
new file mode 100644 (file)
index 0000000..9fe8795
--- /dev/null
@@ -0,0 +1,2 @@
+Fix a use-after-free crash in :meth:`asyncio.Future.remove_done_callback`.
+Patch by Bénédikt Tran.
index a4dab29ee9428d37e1d6c6d6913d2f84dceacaf3..d4135f04e56575b581b6a519c15917e66af0ec1a 100644 (file)
@@ -1019,7 +1019,12 @@ _asyncio_Future_remove_done_callback_impl(FutureObj *self, PyTypeObject *cls,
     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;
         }