diff --git a/deps/python3/0001-winapi-createprocess-audit-format.patch b/deps/python3/0001-winapi-createprocess-audit-format.patch new file mode 100644 index 0000000000..a081e51164 --- /dev/null +++ b/deps/python3/0001-winapi-createprocess-audit-format.patch @@ -0,0 +1,11 @@ +--- a/Modules/_winapi.c ++++ b/Modules/_winapi.c +@@ -1252,7 +1252,7 @@ _winapi_CreateProcess_impl(PyObject *module, const Py_UNICODE *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; + } diff --git a/deps/python3/patch_winapi_audit.cmake b/deps/python3/patch_winapi_audit.cmake new file mode 100644 index 0000000000..85610c0eb3 --- /dev/null +++ b/deps/python3/patch_winapi_audit.cmake @@ -0,0 +1,38 @@ +# Applied as PATCH_COMMAND for CPython on Windows, from the extracted source directory. +# +# CPython 3.12.13 raises the _winapi.CreateProcess audit event with PySys_Audit format "uuu", +# although command_line is a Python object: audit hooks receive garbage instead of the command +# line. PluginAuditManager turns that argument into a process grant's target, so the prompt +# named the wrong executable and the grant it saved could not cover a real call. The patch +# changes the format to "uOu", as upstream does (python/cpython#159237), until the pinned +# CPython release carries the fix. +# +# Applying is idempotent: ExternalProject re-runs the patch step when PATCH_COMMAND changes or +# the patch stamp is missing, which can happen on a source tree that already carries the patch. +# A reverse check recognises that state instead of failing the build on an already patched file. +# When the pinned CPython release carries the fix, drop this script, its patch and PATCH_COMMAND. + +set(_patch "${CMAKE_CURRENT_LIST_DIR}/0001-winapi-createprocess-audit-format.patch") + +if(NOT GIT_EXECUTABLE) + message(FATAL_ERROR "GIT_EXECUTABLE is required") +endif() + +execute_process( + COMMAND "${GIT_EXECUTABLE}" apply --check --reverse --ignore-space-change "${_patch}" + RESULT_VARIABLE _already_applied + OUTPUT_QUIET + ERROR_QUIET +) +if(_already_applied EQUAL 0) + message(STATUS "CPython: _winapi.CreateProcess audit format is already patched") + return() +endif() + +execute_process( + COMMAND "${GIT_EXECUTABLE}" apply --verbose --ignore-space-change --whitespace=fix "${_patch}" + RESULT_VARIABLE _apply_result +) +if(NOT _apply_result EQUAL 0) + message(FATAL_ERROR "CPython: failed to apply ${_patch} (exit ${_apply_result})") +endif() diff --git a/deps/python3/python3.cmake b/deps/python3/python3.cmake index 4f60bb1b14..7e5745af79 100644 --- a/deps/python3/python3.cmake +++ b/deps/python3/python3.cmake @@ -181,6 +181,16 @@ if(WIN32) -DPYTHON_LAYOUT_ARCH=${_python_layout_arch} -P ${CMAKE_CURRENT_LIST_DIR}/stage_windows.cmake ) + # CPython 3.12.13 raises the _winapi.CreateProcess audit event with PySys_Audit's "u" format + # for command_line although it is a Python object, so audit hooks received garbage instead of + # the command line and the plugin audit prompt named the wrong executable. Apply upstream's + # "uOu" fix (python/cpython#159237) until the pinned release carries it. The step tolerates + # an already patched tree, which a re-run after a stamp change can meet. + set(_python_patch_cmd + ${CMAKE_COMMAND} + "-DGIT_EXECUTABLE=${GIT_EXECUTABLE}" + -P "${CMAKE_CURRENT_LIST_DIR}/patch_winapi_audit.cmake" + ) elseif(APPLE) # macOS configuration if(CMAKE_OSX_ARCHITECTURES) @@ -351,6 +361,7 @@ ExternalProject_Add(dep_python3 URL_HASH SHA256=${_python_sha256} DOWNLOAD_DIR ${DEP_DOWNLOAD_DIR}/python3 BUILD_IN_SOURCE ON + PATCH_COMMAND ${_python_patch_cmd} CONFIGURE_COMMAND ${_conf_cmd} BUILD_COMMAND ${_build_cmd} INSTALL_COMMAND ${_install_cmd} diff --git a/tests/slic3rutils/test_plugin_audit.cpp b/tests/slic3rutils/test_plugin_audit.cpp index c130b08748..151949176b 100644 --- a/tests/slic3rutils/test_plugin_audit.cpp +++ b/tests/slic3rutils/test_plugin_audit.cpp @@ -1426,6 +1426,8 @@ import http.client, http.server, os, pathlib, shutil, socket, subprocess, sys, t import urllib.request, zipfile if os.name != "nt": import pty +else: + import _winapi APPROVED = os.path.join(OUTSIDE, "approved.txt") PRIVATE = os.path.join(OUTSIDE, "private.txt") @@ -2706,6 +2708,58 @@ site_packages_outcome = attempt(lambda: open(SITE_PACKAGE_FILE).close()) CHECK(describe(harness.prompts) == write_prompt + site_packages_prompt); } +#ifdef _WIN32 +TEST_CASE("A real _winapi.CreateProcess audit event reports its command line as text", "[audit][Python]") +{ + AuditHarness harness; + if (!harness.initialized) + SKIP("Bundled Python interpreter unavailable: " + PythonInterpreter::instance().last_error()); + harness.add_plugin(PLUGIN); + py::gil_scoped_acquire gil; + PythonSide python(harness); + + // subprocess reaches this event on Windows. CPython 3.12.13 raised it with PySys_Audit's "uuu" + // format, reading the command-line object as a wide string, so audit hooks received garbage + // instead of the text subprocess passed. The deps patch raises "uOu" (as upstream does, + // python/cpython#159237) until the pinned release carries the fix. + // + // The event is raised before the arguments are used, so a rejected environment mapping ends + // the call after the hook saw the event, without reaching CreateProcessW. Run outside a + // plugin scope, so the hook below is reached: the plugin audit hook aborts the event when it + // denies, and a real prompt is asserted separately at the end. + python.exec(R"PY( +WINAPI_AUDIT_CALLS = [] + +def record_winapi_audit(event, args): + if event == "_winapi.CreateProcess": + WINAPI_AUDIT_CALLS.append(args) + +sys.addaudithook(record_winapi_audit) + +DENIED_EXECUTABLE = os.path.join(OUTSIDE, "Program Files", "audit-denied.exe") +COMMAND_LINE = '"' + DENIED_EXECUTABLE + '" --flag' +try: + _winapi.CreateProcess(None, COMMAND_LINE, None, None, False, 0, object(), None, + subprocess.STARTUPINFO()) + audit_arguments_outcome = "started" +except TypeError: + audit_arguments_outcome = "stopped" +)PY"); + CHECK(python.str("audit_arguments_outcome") == "stopped"); + INFO("recorded audit arguments: " << python.str("WINAPI_AUDIT_CALLS")); + CHECK(python.eval("WINAPI_AUDIT_CALLS == [(None, COMMAND_LINE, None)]") == "True"); + + // In a plugin scope the same call is prompted and denied, and the prompt names the command + // line exactly as the caller passed it, which an approval saves as the process grant. + python.run_as(PLUGIN, R"PY( +create_process_outcome = attempt(lambda: _winapi.CreateProcess( + None, COMMAND_LINE, None, None, False, 0, None, None, subprocess.STARTUPINFO())) +)PY"); + CHECK(python.str("create_process_outcome") == "denied"); + CHECK(describe(harness.prompts) == "_winapi.CreateProcess: " + python.str("COMMAND_LINE") + "\n"); +} +#endif + TEST_CASE("A filesystem wildcard grant covers the folder and its descendants at any depth", "[audit][Python]") { AuditHarness harness;