From 59142408c496d1d9ec346480a772eaac0c02a333 Mon Sep 17 00:00:00 2001 From: An Long Date: Sat, 5 Dec 2026 03:31:47 +0900 Subject: [PATCH] gh-159237: Fix buffer type confusion in `_winapi.CreateProcess` audit event (#159238) Co-authored-by: Victor Stinner (cherry picked from commit 58e7eca17a58596559ba3bfc8468c664e51b0c8e) --- Lib/test/audit-tests.py | 24 +++++++++++++++++++ Lib/test/test_audit.py | 16 +++++++++++++ ...-10-13-01-22-44.gh-issue-159237.JYm7N8.rst | 2 ++ Modules/_winapi.c | 2 +- 4 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Windows/2026-10-13-01-22-44.gh-issue-159237.JYm7N8.rst diff --git a/Lib/test/audit-tests.py b/Lib/test/audit-tests.py index 6884ac0dbe6ff0..788cd7cff5cb45 100644 --- a/Lib/test/audit-tests.py +++ b/Lib/test/audit-tests.py @@ -8,6 +8,7 @@ import contextlib import os import sys +from test.support import SHORT_TIMEOUT class TestHook: @@ -633,6 +634,29 @@ def hook(event, args): _winapi.CreateNamedPipe(pipe_name, _winapi.PIPE_ACCESS_DUPLEX, 8, 2, 0, 0, 0, 0) +def test_winapi_createprocess(command_line): + import _winapi + import subprocess + + def hook(event, args): + if event == "_winapi.CreateProcess": + print(event, args) + + sys.addaudithook(hook) + + hp, ht, _, _ = _winapi.CreateProcess( + sys.executable, command_line, None, None, False, 0, None, None, + subprocess.STARTUPINFO()) + _winapi.CloseHandle(ht) + + timeout = SHORT_TIMEOUT + timeout = int(timeout * 1000) + result = _winapi.WaitForSingleObject(hp, timeout) + _winapi.CloseHandle(hp) + if result == _winapi.WAIT_TIMEOUT: + raise TimeoutError(f"process still running after {timeout} ms") + + def test_assert_unicode(): import sys sys.addaudithook(lambda *args: None) diff --git a/Lib/test/test_audit.py b/Lib/test/test_audit.py index 077765fcda210a..ab15cba6705c7d 100644 --- a/Lib/test/test_audit.py +++ b/Lib/test/test_audit.py @@ -316,6 +316,22 @@ def test_winapi_createnamedpipe(self): self.assertEqual(actual, expected) + def test_winapi_createprocess(self): + winapi = import_helper.import_module("_winapi") + + command_line = "python -c pass" + returncode, events, stderr = self.run_python("test_winapi_createprocess", command_line) + if returncode: + self.fail(stderr) + + if support.verbose: + print(*events, sep='\n') + actual = [(ev[0], ev[2]) for ev in events] + expected = [("_winapi.CreateProcess", + f"({sys.executable!r}, {command_line!r}, None)")] + + self.assertEqual(actual, expected) + def test_assert_unicode(self): # See gh-126018 returncode, _, stderr = self.run_python("test_assert_unicode") diff --git a/Misc/NEWS.d/next/Windows/2026-10-13-01-22-44.gh-issue-159237.JYm7N8.rst b/Misc/NEWS.d/next/Windows/2026-10-13-01-22-44.gh-issue-159237.JYm7N8.rst new file mode 100644 index 00000000000000..0d118e4b6490ff --- /dev/null +++ b/Misc/NEWS.d/next/Windows/2026-10-13-01-22-44.gh-issue-159237.JYm7N8.rst @@ -0,0 +1,2 @@ +Fix the ``_winapi.CreateProcess`` audit event passing garbage instead of the +*command_line* argument to audit hooks. diff --git a/Modules/_winapi.c b/Modules/_winapi.c index 8ae89219fb05eb..e72b93dd316170 100644 --- a/Modules/_winapi.c +++ b/Modules/_winapi.c @@ -1359,7 +1359,7 @@ _winapi_CreateProcess_impl(PyObject *module, const wchar_t *application_name, wchar_t *command_line_copy = NULL; AttributeList attribute_list = {0}; - if (PySys_Audit("_winapi.CreateProcess", "uuu", application_name, + if (PySys_Audit("_winapi.CreateProcess", "uOu", application_name, command_line, current_directory) < 0) { return NULL; }