Skip to content

Commit 58e7eca

Browse files
aiskvstinner
andauthored
gh-159237: Fix buffer type confusion in _winapi.CreateProcess audit event (#159238)
Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent 2123e52 commit 58e7eca

4 files changed

Lines changed: 43 additions & 2 deletions

File tree

‎Lib/test/audit-tests.py‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import os
1010
import sys
1111
import unittest.mock
12-
from test.support import swap_item
12+
from test.support import SHORT_TIMEOUT, swap_item
1313

1414

1515
class TestHook:
@@ -659,6 +659,29 @@ def hook(event, args):
659659
_winapi.CreateNamedPipe(pipe_name, _winapi.PIPE_ACCESS_DUPLEX, 8, 2, 0, 0, 0, 0)
660660

661661

662+
def test_winapi_createprocess(command_line):
663+
import _winapi
664+
import subprocess
665+
666+
def hook(event, args):
667+
if event == "_winapi.CreateProcess":
668+
print(event, args)
669+
670+
sys.addaudithook(hook)
671+
672+
hp, ht, _, _ = _winapi.CreateProcess(
673+
sys.executable, command_line, None, None, False, 0, None, None,
674+
subprocess.STARTUPINFO())
675+
_winapi.CloseHandle(ht)
676+
677+
timeout = SHORT_TIMEOUT
678+
timeout = int(timeout * 1000)
679+
result = _winapi.WaitForSingleObject(hp, timeout)
680+
_winapi.CloseHandle(hp)
681+
if result == _winapi.WAIT_TIMEOUT:
682+
raise TimeoutError(f"process still running after {timeout} ms")
683+
684+
662685
def test_assert_unicode():
663686
import sys
664687
sys.addaudithook(lambda *args: None)

‎Lib/test/test_audit.py‎

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

320320
self.assertEqual(actual, expected)
321321

322+
def test_winapi_createprocess(self):
323+
winapi = import_helper.import_module("_winapi")
324+
325+
command_line = "python -c pass"
326+
returncode, events, stderr = self.run_python("test_winapi_createprocess", command_line)
327+
if returncode:
328+
self.fail(stderr)
329+
330+
if support.verbose:
331+
print(*events, sep='\n')
332+
actual = [(ev[0], ev[2]) for ev in events]
333+
expected = [("_winapi.CreateProcess",
334+
f"({sys.executable!r}, {command_line!r}, None)")]
335+
336+
self.assertEqual(actual, expected)
337+
322338
def test_assert_unicode(self):
323339
# See gh-126018
324340
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
@@ -1368,7 +1368,7 @@ _winapi_CreateProcess_impl(PyObject *module, const wchar_t *application_name,
13681368
wchar_t *command_line_copy = NULL;
13691369
AttributeList attribute_list = {0};
13701370

1371-
if (PySys_Audit("_winapi.CreateProcess", "uuu", application_name,
1371+
if (PySys_Audit("_winapi.CreateProcess", "uOu", application_name,
13721372
command_line, current_directory) < 0) {
13731373
return NULL;
13741374
}

0 commit comments

Comments
 (0)