]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-112127: Fix possible use-after-free in atexit.unregister() (GH-114092)...
authorSerhiy Storchaka <storchaka@gmail.com>
Wed, 17 Dec 2025 17:15:21 +0000 (19:15 +0200)
committerGitHub <noreply@github.com>
Wed, 17 Dec 2025 17:15:21 +0000 (17:15 +0000)
(cherry picked from commit 2b466c47c333106dc9522ab77898e6972e25a2c6)

Co-authored-by: Benjamin Johnson <ben332004@gmail.com>
Lib/test/_test_atexit.py
Misc/ACKS
Misc/NEWS.d/next/Library/2025-12-17-14-41-09.gh-issue-112127.13OHQk.rst [new file with mode: 0644]
Modules/atexitmodule.c

index f618c1fcbca52b1edb30d8884123f355c013ce93..490b0686a0c1793240da3627d9817874ccb8f91b 100644 (file)
@@ -135,6 +135,19 @@ class GeneralTest(unittest.TestCase):
         finally:
             atexit.unregister(func)
 
+    def test_eq_unregister_clear(self):
+        # Issue #112127: callback's __eq__ may call unregister or _clear
+        class Evil:
+            def __eq__(self, other):
+                action(other)
+                return NotImplemented
+
+        for action in atexit.unregister, lambda o: atexit._clear():
+            with self.subTest(action=action):
+                atexit.register(lambda: None)
+                atexit.unregister(Evil())
+                atexit._clear()
+
 
 if __name__ == "__main__":
     unittest.main()
index 55713d3b2868357c8c7a264fe290c6de33113f22..85ac1d5feefb46e8fd5fb68042b70e0626245032 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -892,6 +892,7 @@ Jim Jewett
 Pedro Diaz Jimenez
 Orjan Johansen
 Fredrik Johansson
+Benjamin Johnson
 Gregory K. Johnson
 Kent Johnson
 Michael Johnson
diff --git a/Misc/NEWS.d/next/Library/2025-12-17-14-41-09.gh-issue-112127.13OHQk.rst b/Misc/NEWS.d/next/Library/2025-12-17-14-41-09.gh-issue-112127.13OHQk.rst
new file mode 100644 (file)
index 0000000..c983683
--- /dev/null
@@ -0,0 +1,2 @@
+Fix possible use-after-free in :func:`atexit.unregister` when the callback
+is unregistered during comparison.
index c009235b7a36c20b62b79f5ef280210f0866784e..93d4c4ede32513f293324ed35835cf6d68c7563b 100644 (file)
@@ -287,7 +287,9 @@ atexit_unregister(PyObject *module, PyObject *func)
             continue;
         }
 
-        int eq = PyObject_RichCompareBool(cb->func, func, Py_EQ);
+        PyObject *to_compare = Py_NewRef(cb->func);
+        int eq = PyObject_RichCompareBool(to_compare, func, Py_EQ);
+        Py_DECREF(to_compare);
         if (eq < 0) {
             return NULL;
         }