From c2327520d2c7c363a3bfcd56d6ce8a9725a71f2e Mon Sep 17 00:00:00 2001 From: zkonge Date: Tue, 30 Jun 2020 03:42:14 +0800 Subject: [PATCH 1/5] bpo-41162: Clear audit hooks after destructors --- Python/pylifecycle.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index cd993ea13418ff..64add4983f571a 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -1290,6 +1290,13 @@ finalize_interp_clear(PyThreadState *tstate) _PyGC_CollectNoFail(); } + /* Clear all loghooks */ + /* Both _PySys_ClearAuditHooks function and users still need PyObject, + such as tuple. */ + if (is_main_interp) { + _PySys_ClearAuditHooks(tstate); + } + _PyGC_Fini(tstate); if (is_main_interp) { @@ -1404,9 +1411,6 @@ Py_FinalizeEx(void) */ _PyGC_CollectIfEnabled(); - /* Clear all loghooks */ - _PySys_ClearAuditHooks(tstate); - /* Destroy all modules */ _PyImport_Cleanup(tstate); From 76350ccd6ff72f7a8025097bf59fac9be7791c4a Mon Sep 17 00:00:00 2001 From: zkonge Date: Sat, 4 Jul 2020 01:03:37 +0800 Subject: [PATCH 2/5] Improve the description of comment --- Python/pylifecycle.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 64add4983f571a..37f7d337c94279 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -1291,8 +1291,8 @@ finalize_interp_clear(PyThreadState *tstate) } /* Clear all loghooks */ - /* Both _PySys_ClearAuditHooks function and users still need PyObject, - such as tuple. */ + /* Both _PySys_Audit function and users still need PyObject, such as tuple. + Call _PySys_ClearAuditHooks when PyObject available. */ if (is_main_interp) { _PySys_ClearAuditHooks(tstate); } From 2ba1352facf8492f3fb8467fd90b2f03194ca7c8 Mon Sep 17 00:00:00 2001 From: zkonge Date: Sat, 4 Jul 2020 01:38:11 +0800 Subject: [PATCH 3/5] bpo-41162: Apply test patch by @zooba --- Programs/_testembed.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Programs/_testembed.c b/Programs/_testembed.c index b60d70be5f71e6..5aad16a6f7c47d 100644 --- a/Programs/_testembed.c +++ b/Programs/_testembed.c @@ -1112,8 +1112,11 @@ static int test_open_code_hook(void) return result; } +static int _audit_hook_clear_count = 0; + static int _audit_hook(const char *event, PyObject *args, void *userdata) { + assert(args && PyTuple_CheckExact(args)); if (strcmp(event, "_testembed.raise") == 0) { PyErr_SetString(PyExc_RuntimeError, "Intentional error"); return -1; @@ -1122,6 +1125,8 @@ static int _audit_hook(const char *event, PyObject *args, void *userdata) return -1; } return 0; + } else if (strcmp(event, "cpython._PySys_ClearAuditHooks") == 0) { + _audit_hook_clear_count += 1; } return 0; } @@ -1167,6 +1172,9 @@ static int test_audit(void) { int result = _test_audit(42); Py_Finalize(); + if (_audit_hook_clear_count != 1) { + return 0x1000 | _audit_hook_clear_count; + } return result; } From 3e8202d14e753adf6320f7ab512d0fe160b28f29 Mon Sep 17 00:00:00 2001 From: zkonge Date: Sat, 4 Jul 2020 02:02:17 +0800 Subject: [PATCH 4/5] bpo-41162: Delete useless tests Test for ClearAudithooks has been moved to Programs/_testembed.c --- Lib/test/audit-tests.py | 26 -------------------------- Lib/test/test_audit.py | 16 ---------------- 2 files changed, 42 deletions(-) diff --git a/Lib/test/audit-tests.py b/Lib/test/audit-tests.py index a58395b068b395..ee6fc93351b753 100644 --- a/Lib/test/audit-tests.py +++ b/Lib/test/audit-tests.py @@ -44,28 +44,6 @@ def __call__(self, event, args): raise self.exc_type("saw event " + event) -class TestFinalizeHook: - """Used in the test_finalize_hooks function to ensure that hooks - are correctly cleaned up, that they are notified about the cleanup, - and are unable to prevent it. - """ - - def __init__(self): - print("Created", id(self), file=sys.stdout, flush=True) - - def __call__(self, event, args): - # Avoid recursion when we call id() below - if event == "builtins.id": - return - - print(event, id(self), file=sys.stdout, flush=True) - - if event == "cpython._PySys_ClearAuditHooks": - raise RuntimeError("Should be ignored") - elif event == "cpython.PyInterpreterState_Clear": - raise RuntimeError("Should be ignored") - - # Simple helpers, since we are not in unittest here def assertEqual(x, y): if x != y: @@ -128,10 +106,6 @@ def test_block_add_hook_baseexception(): pass -def test_finalize_hooks(): - sys.addaudithook(TestFinalizeHook()) - - def test_pickle(): import pickle diff --git a/Lib/test/test_audit.py b/Lib/test/test_audit.py index f405c6923979ca..f79edbc4bd0d9f 100644 --- a/Lib/test/test_audit.py +++ b/Lib/test/test_audit.py @@ -51,22 +51,6 @@ def test_block_add_hook(self): def test_block_add_hook_baseexception(self): self.do_test("test_block_add_hook_baseexception") - def test_finalize_hooks(self): - returncode, events, stderr = self.run_python("test_finalize_hooks") - if stderr: - print(stderr, file=sys.stderr) - if returncode: - self.fail(stderr) - - firstId = events[0][2] - self.assertSequenceEqual( - [ - ("Created", " ", firstId), - ("cpython._PySys_ClearAuditHooks", " ", firstId), - ], - events, - ) - def test_pickle(self): support.import_module("pickle") From da16861bc9ae10ee764ddd58f6432433bf891246 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 3 Jul 2020 20:41:29 +0000 Subject: [PATCH 5/5] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Security/2020-07-03-20-41-29.bpo-41162.tb8pVj.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Security/2020-07-03-20-41-29.bpo-41162.tb8pVj.rst diff --git a/Misc/NEWS.d/next/Security/2020-07-03-20-41-29.bpo-41162.tb8pVj.rst b/Misc/NEWS.d/next/Security/2020-07-03-20-41-29.bpo-41162.tb8pVj.rst new file mode 100644 index 00000000000000..f0333ac4a7bb32 --- /dev/null +++ b/Misc/NEWS.d/next/Security/2020-07-03-20-41-29.bpo-41162.tb8pVj.rst @@ -0,0 +1 @@ +Audit hooks are now cleared later during finalization to avoid missing events. \ No newline at end of file