Skip to content

Commit 458b722

Browse files
vstinneraisk
andauthored
[3.14] gh-159237: Fix buffer type confusion in _winapi.CreateProcess audit event (#163159)
gh-159237: Fix buffer type confusion in `_winapi.CreateProcess` audit event (#159238) (cherry picked from commit 58e7eca) Co-authored-by: An Long <aisk@users.noreply.github.com>
1 parent 1b3341c commit 458b722

4 files changed

Lines changed: 43 additions & 1 deletion

File tree

‎Lib/test/audit-tests.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import contextlib
99
import os
1010
import sys
11+
from test.support import SHORT_TIMEOUT
1112

1213

1314
class TestHook:
@@ -633,6 +634,29 @@ def hook(event, args):
633634
_winapi.CreateNamedPipe(pipe_name, _winapi.PIPE_ACCESS_DUPLEX, 8, 2, 0, 0, 0, 0)
634635

635636

637+
def test_winapi_createprocess(command_line):
638+
import _winapi
639+
import subprocess
640+
641+
def hook(event, args):
642+
if event == "_winapi.CreateProcess":
643+
print(event, args)
644+
645+
sys.addaudithook(hook)
646+
647+
hp, ht, _, _ = _winapi.CreateProcess(
648+
sys.executable, command_line, None, None, False, 0, None, None,
649+
subprocess.STARTUPINFO())
650+
_winapi.CloseHandle(ht)
651+
652+
timeout = SHORT_TIMEOUT
653+
timeout = int(timeout * 1000)
654+
result = _winapi.WaitForSingleObject(hp, timeout)
655+
_winapi.CloseHandle(hp)
656+
if result == _winapi.WAIT_TIMEOUT:
657+
raise TimeoutError(f"process still running after {timeout} ms")
658+
659+
636660
def test_assert_unicode():
637661
import sys
638662
sys.addaudithook(lambda *args: None)

‎Lib/test/test_audit.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,22 @@ def test_winapi_createnamedpipe(self):
316316

317317
self.assertEqual(actual, expected)
318318

319+
def test_winapi_createprocess(self):
320+
winapi = import_helper.import_module("_winapi")
321+
322+
command_line = "python -c pass"
323+
returncode, events, stderr = self.run_python("test_winapi_createprocess", command_line)
324+
if returncode:
325+
self.fail(stderr)
326+
327+
if support.verbose:
328+
print(*events, sep='\n')
329+
actual = [(ev[0], ev[2]) for ev in events]
330+
expected = [("_winapi.CreateProcess",
331+
f"({sys.executable!r}, {command_line!r}, None)")]
332+
333+
self.assertEqual(actual, expected)
334+
319335
def test_assert_unicode(self):
320336
# See gh-126018
321337
returncode, _, stderr = self.run_python("test_assert_unicode")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix the ``_winapi.CreateProcess`` audit event passing garbage instead of the
2+
*command_line* argument to audit hooks.

‎Modules/_winapi.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1359,7 +1359,7 @@ _winapi_CreateProcess_impl(PyObject *module, const wchar_t *application_name,
13591359
wchar_t *command_line_copy = NULL;
13601360
AttributeList attribute_list = {0};
13611361

1362-
if (PySys_Audit("_winapi.CreateProcess", "uuu", application_name,
1362+
if (PySys_Audit("_winapi.CreateProcess", "uOu", application_name,
13631363
command_line, current_directory) < 0) {
13641364
return NULL;
13651365
}

0 commit comments

Comments
 (0)