]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-87604: Avoid publishing list of active per-interpreter audit hooks via the gc...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 15 Nov 2022 20:52:14 +0000 (12:52 -0800)
committerGitHub <noreply@github.com>
Tue, 15 Nov 2022 20:52:14 +0000 (12:52 -0800)
(cherry picked from commit 4e4b13e8f6211abbc0d53056da11357756daa314)

Co-authored-by: Steve Dower <steve.dower@python.org>
Lib/test/audit-tests.py
Lib/test/test_audit.py
Misc/NEWS.d/next/Security/2022-11-11-12-50-28.gh-issue-87604.OtwH5L.rst [new file with mode: 0644]
Python/sysmodule.c

index b781b9940465c717894b4fc8e7279627cc1f2f87..481aedd6b4b2c790c60914940a18c4ba50c6a6df 100644 (file)
@@ -429,6 +429,17 @@ def test_syslog():
     syslog.closelog()
 
 
+def test_not_in_gc():
+    import gc
+
+    hook = lambda *a: None
+    sys.addaudithook(hook)
+
+    for o in gc.get_objects():
+        if isinstance(o, list):
+            assert hook not in o
+
+
 if __name__ == "__main__":
     from test.support import suppress_msvcrt_asserts
 
index cea452ddce51ba649f0ad55af9db7ec3ef4f79f3..10a61c60b57e88bcdc55bf18f4bce4364abb7dfc 100644 (file)
@@ -195,6 +195,11 @@ class AuditTest(unittest.TestCase):
             ('syslog.closelog', '', '')]
         )
 
+    def test_not_in_gc(self):
+        returncode, _, stderr = self.run_python("test_not_in_gc")
+        if returncode:
+            self.fail(stderr)
+
 
 if __name__ == "__main__":
     unittest.main()
diff --git a/Misc/NEWS.d/next/Security/2022-11-11-12-50-28.gh-issue-87604.OtwH5L.rst b/Misc/NEWS.d/next/Security/2022-11-11-12-50-28.gh-issue-87604.OtwH5L.rst
new file mode 100644 (file)
index 0000000..c931409
--- /dev/null
@@ -0,0 +1,2 @@
+Avoid publishing list of active per-interpreter audit hooks via the
+:mod:`gc` module
index 1d5a06a6b4787eaeff7e9ed4d42a2bf0f320f964..2b5c9d3ebbe86b064f4ec9e07e4347f3669da117 100644 (file)
@@ -462,6 +462,8 @@ sys_addaudithook_impl(PyObject *module, PyObject *hook)
         if (interp->audit_hooks == NULL) {
             return NULL;
         }
+        /* Avoid having our list of hooks show up in the GC module */
+        PyObject_GC_UnTrack(interp->audit_hooks);
     }
 
     if (PyList_Append(interp->audit_hooks, hook) < 0) {