From 6b226005298a7f7eb2a29089acba81e03addf2bd Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Fri, 26 Aug 2022 00:11:35 +0100 Subject: [PATCH 01/15] gh-89545: Adds internal _wmi module on Windows for directly querying OS properties --- ...2-08-26-00-11-18.gh-issue-89545.zmJMY_.rst | 1 + PC/_wmimodule.cpp | 277 ++++++++++++++++++ PC/clinic/_wmimodule.cpp.h | 75 +++++ PCbuild/_wmi.vcxproj | 118 ++++++++ PCbuild/_wmi.vcxproj.filters | 22 ++ PCbuild/pcbuild.proj | 2 +- PCbuild/pcbuild.sln | 34 +++ Tools/msi/lib/lib_files.wxs | 2 +- 8 files changed, 529 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Windows/2022-08-26-00-11-18.gh-issue-89545.zmJMY_.rst create mode 100644 PC/_wmimodule.cpp create mode 100644 PC/clinic/_wmimodule.cpp.h create mode 100644 PCbuild/_wmi.vcxproj create mode 100644 PCbuild/_wmi.vcxproj.filters diff --git a/Misc/NEWS.d/next/Windows/2022-08-26-00-11-18.gh-issue-89545.zmJMY_.rst b/Misc/NEWS.d/next/Windows/2022-08-26-00-11-18.gh-issue-89545.zmJMY_.rst new file mode 100644 index 00000000000000..0527ca7d0490fa --- /dev/null +++ b/Misc/NEWS.d/next/Windows/2022-08-26-00-11-18.gh-issue-89545.zmJMY_.rst @@ -0,0 +1 @@ +Adds internal ``_wmi`` module for directly querying OS properties diff --git a/PC/_wmimodule.cpp b/PC/_wmimodule.cpp new file mode 100644 index 00000000000000..f76a4178405a66 --- /dev/null +++ b/PC/_wmimodule.cpp @@ -0,0 +1,277 @@ +// +// Helper library for querying WMI using its COM-based query API. +// +// Copyright (c) Microsoft Corporation +// Licensed to PSF under a contributor agreement +// + +// Version history +// 2022-08: Initial contribution (Steve Dower) + +#define _WIN32_DCOM +#include +#include +#include +#include + +#include +#include "clinic/_wmimodule.cpp.h" + + +/*[clinic input] +module _wmi +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=7ca95dad1453d10d]*/ + + + +struct _query_data { + LPCWSTR query; + HANDLE writePipe; + HANDLE readPipe; +}; + + +static DWORD WINAPI +_query_thread(LPVOID param) +{ + IWbemLocator *locator = NULL; + IWbemServices *services = NULL; + IEnumWbemClassObject* enumerator = NULL; + BSTR bstrQuery = NULL; + struct _query_data *data = (struct _query_data*)param; + + HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED); + if (FAILED(hr)) { + CloseHandle(data->writePipe); + return (DWORD)hr; + } + + hr = CoInitializeSecurity( + NULL, -1, NULL, NULL, + RPC_C_AUTHN_LEVEL_DEFAULT, + RPC_C_IMP_LEVEL_IMPERSONATE, + NULL, EOAC_NONE, NULL + ); + if (SUCCEEDED(hr)) { + hr = CoCreateInstance( + CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, + IID_IWbemLocator, (LPVOID *)&locator + ); + } + if (SUCCEEDED(hr)) { + hr = locator->ConnectServer( + bstr_t(L"ROOT\\CIMV2"), + NULL, NULL, 0, NULL, 0, 0, &services + ); + } + if (SUCCEEDED(hr)) { + hr = CoSetProxyBlanket( + services, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL, + RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, + NULL, EOAC_NONE + ); + } + if (SUCCEEDED(hr)) { + bstrQuery = SysAllocString(data->query); + if (!bstrQuery) { + hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY); + } + } + if (SUCCEEDED(hr)) { + hr = services->ExecQuery( + bstr_t("WQL"), + bstrQuery, + WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, + NULL, + &enumerator + ); + } + + // Okay, after all that, at this stage we should have an enumerator + // to the query results and can start writing them to the pipe! + IWbemClassObject *value = NULL; + int endOfEnum = FALSE; + while (SUCCEEDED(hr) && !endOfEnum) { + ULONG got = 0; + hr = enumerator->Next(WBEM_INFINITE, 1, &value, &got); + if (hr == WBEM_S_FALSE) { + // Could be at the end, but still got a result this time + endOfEnum = TRUE; + hr = 0; + break; + } + if (FAILED(hr) || got != 1 || !value) { + continue; + } + // Okay, now we have each resulting object it's time to + // enumerate its members + hr = value->BeginEnumeration(0); + while (SUCCEEDED(hr)) { + BSTR propName; + VARIANT propValue; + long flavor; + hr = value->Next(0, &propName, &propValue, NULL, &flavor); + if (hr == WBEM_S_NO_MORE_DATA) { + hr = 0; + break; + } + if (SUCCEEDED(hr) && (flavor & WBEM_FLAVOR_MASK_ORIGIN) != WBEM_FLAVOR_ORIGIN_SYSTEM) { + WCHAR propStr[8192]; + hr = VariantToString(propValue, propStr, sizeof(propStr) / sizeof(propStr[0])); + if (SUCCEEDED(hr)) { + DWORD cbStr; + DWORD written; + cbStr = (DWORD)(wcslen(propName) * sizeof(propName[0])); + WriteFile(data->writePipe, propName, cbStr, &written, NULL); + WriteFile(data->writePipe, (LPVOID)L"=", 2, &written, NULL); + cbStr = (DWORD)(wcslen(propStr) * sizeof(propStr[0])); + WriteFile(data->writePipe, propStr, cbStr, &written, NULL); + WriteFile(data->writePipe, (LPVOID)L"\0", 2, &written, NULL); + } + VariantClear(&propValue); + SysFreeString(propName); + } + } + value->EndEnumeration(); + value->Release(); + } + + if (bstrQuery) { + SysFreeString(bstrQuery); + } + if (enumerator) { + enumerator->Release(); + } + if (services) { + services->Release(); + } + if (locator) { + locator->Release(); + } + CoUninitialize(); + CloseHandle(data->writePipe); + return (DWORD)hr; +} + + +/*[clinic input] +_wmi.exec_query + + query: unicode + +Runs a WMI query against the local machine. + +This returns a single string with 'name=value' pairs in a flat array separated +by null characters. +[clinic start generated code]*/ + +static PyObject * +_wmi_exec_query_impl(PyObject *module, PyObject *query) +/*[clinic end generated code: output=a62303d5bb5e003f input=48d2d0a1e1a7e3c2]*/ + +/*[clinic end generated code]*/ +{ + PyObject *result = NULL; + HANDLE hThread = NULL; + int err = 0; + WCHAR buffer[16384]; + DWORD offset = 0; + DWORD bytesRead; + struct _query_data data = {0}; + + if (PySys_Audit("_wmi.exec_query", "O", query) < 0) { + return NULL; + } + + data.query = PyUnicode_AsWideCharString(query, NULL); + if (!data.query) { + return NULL; + } + + Py_BEGIN_ALLOW_THREADS + + if (!CreatePipe(&data.readPipe, &data.writePipe, NULL, 0) || + !(hThread = CreateThread(NULL, 0, _query_thread, (LPVOID*)&data, 0, NULL))) { + err = GetLastError(); + } + + while (!err) { + if (ReadFile( + data.readPipe, + (LPVOID)&buffer[offset / sizeof(buffer[0])], + sizeof(buffer) - offset, + &bytesRead, + NULL + )) { + offset += bytesRead; + if (offset >= sizeof(buffer)) { + err = ERROR_MORE_DATA; + } + } else { + err = GetLastError(); + } + } + + if (data.readPipe) { + CloseHandle(data.readPipe); + } + if (data.writePipe) { + CloseHandle(data.writePipe); + } + + if (err == ERROR_BROKEN_PIPE) { + // broken pipe indicates some kind of failure, but the real error + // code will come as the thread exit + if (WaitForSingleObject(hThread, 1000) != WAIT_OBJECT_0 || + !GetExitCodeThread(hThread, (LPDWORD)&err)) { + err = GetLastError(); + } + } + + CloseHandle(hThread); + hThread = NULL; + + Py_END_ALLOW_THREADS + + PyMem_Free((void *)data.query); + + return PyUnicode_FromWideChar(buffer, offset / sizeof(buffer[0]) - 1); +} + + +static PyMethodDef wmi_functions[] = { + _WMI_EXEC_QUERY_METHODDEF + { NULL, NULL, 0, NULL } +}; + +static int exec_wmi(PyObject *module) +{ + PyModule_AddFunctions(module, wmi_functions); + + return 0; // success +} + +static PyModuleDef_Slot wmi_slots[] = { + { Py_mod_exec, exec_wmi }, + { 0, NULL } +}; + +static PyModuleDef wmi_def = { + PyModuleDef_HEAD_INIT, + "_wmi", + NULL, // doc + 0, // m_size + NULL, // m_methods + wmi_slots, + NULL, // m_traverse + NULL, // m_clear + NULL, // m_free +}; + +extern "C" { + PyMODINIT_FUNC PyInit__wmi(void) + { + return PyModuleDef_Init(&wmi_def); + } +} diff --git a/PC/clinic/_wmimodule.cpp.h b/PC/clinic/_wmimodule.cpp.h new file mode 100644 index 00000000000000..e2b947f339da60 --- /dev/null +++ b/PC/clinic/_wmimodule.cpp.h @@ -0,0 +1,75 @@ +/*[clinic input] +preserve +[clinic start generated code]*/ + +#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) +# include "pycore_gc.h" // PyGC_Head +# include "pycore_runtime.h" // _Py_ID() +#endif + + +PyDoc_STRVAR(_wmi_exec_query__doc__, +"exec_query($module, /, query)\n" +"--\n" +"\n" +"Runs a WMI query against the local machine.\n" +"\n" +"This returns a single string with \'name=value\' pairs in a flat array separated\n" +"by null characters."); + +#define _WMI_EXEC_QUERY_METHODDEF \ + {"exec_query", _PyCFunction_CAST(_wmi_exec_query), METH_FASTCALL|METH_KEYWORDS, _wmi_exec_query__doc__}, + +static PyObject * +_wmi_exec_query_impl(PyObject *module, PyObject *query); + +static PyObject * +_wmi_exec_query(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) +{ + PyObject *return_value = NULL; + #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) + + #define NUM_KEYWORDS 1 + static struct { + PyGC_Head _this_is_not_used; + PyObject_VAR_HEAD + PyObject *ob_item[NUM_KEYWORDS]; + } _kwtuple = { + .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) + .ob_item = { &_Py_ID(query), }, + }; + #undef NUM_KEYWORDS + #define KWTUPLE (&_kwtuple.ob_base.ob_base) + + #else // !Py_BUILD_CORE + # define KWTUPLE NULL + #endif // !Py_BUILD_CORE + + static const char * const _keywords[] = {"query", NULL}; + static _PyArg_Parser _parser = { + .keywords = _keywords, + .fname = "exec_query", + .kwtuple = KWTUPLE, + }; + #undef KWTUPLE + PyObject *argsbuf[1]; + PyObject *query; + + args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf); + if (!args) { + goto exit; + } + if (!PyUnicode_Check(args[0])) { + _PyArg_BadArgument("exec_query", "argument 'query'", "str", args[0]); + goto exit; + } + if (PyUnicode_READY(args[0]) == -1) { + goto exit; + } + query = args[0]; + return_value = _wmi_exec_query_impl(module, query); + +exit: + return return_value; +} +/*[clinic end generated code: output=7fdf0c0579ddb566 input=a9049054013a1b77]*/ diff --git a/PCbuild/_wmi.vcxproj b/PCbuild/_wmi.vcxproj new file mode 100644 index 00000000000000..c0fc04cbcd195e --- /dev/null +++ b/PCbuild/_wmi.vcxproj @@ -0,0 +1,118 @@ + + + + + Debug + ARM + + + Debug + ARM64 + + + Debug + Win32 + + + Debug + x64 + + + PGInstrument + ARM + + + PGInstrument + ARM64 + + + PGInstrument + Win32 + + + PGInstrument + x64 + + + PGUpdate + ARM + + + PGUpdate + ARM64 + + + PGUpdate + Win32 + + + PGUpdate + x64 + + + Release + ARM + + + Release + ARM64 + + + Release + Win32 + + + Release + x64 + + + + {54B1431F-B86B-4ACB-B28C-88BCF93191D8} + _wmi + Win32Proj + false + + + + + DynamicLibrary + NotSet + + + + .pyd + + + + + + + + + + <_ProjectFileVersion>10.0.30319.1 + + + + /std:c++20 %(AdditionalOptions) + + + wbemuuid.lib;propsys.lib;%(AdditionalDependencies) + + + + + + + + + + + {cf7ac3d1-e2df-41d2-bea6-1e2556cdea26} + false + + + + + + \ No newline at end of file diff --git a/PCbuild/_wmi.vcxproj.filters b/PCbuild/_wmi.vcxproj.filters new file mode 100644 index 00000000000000..705902ff50c3a6 --- /dev/null +++ b/PCbuild/_wmi.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {4fa4dbfa-e069-4ab4-86a6-ad389b2ec407} + + + + + Source Files + + + + + Resource Files + + + \ No newline at end of file diff --git a/PCbuild/pcbuild.proj b/PCbuild/pcbuild.proj index 2ba0627b833695..8d143a4f604ae6 100644 --- a/PCbuild/pcbuild.proj +++ b/PCbuild/pcbuild.proj @@ -64,7 +64,7 @@ - + diff --git a/PCbuild/pcbuild.sln b/PCbuild/pcbuild.sln index 3629a8508a3a60..9f374abd152b17 100644 --- a/PCbuild/pcbuild.sln +++ b/PCbuild/pcbuild.sln @@ -108,6 +108,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "pythonw_uwp", "pythonw_uwp. EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "_uuid", "_uuid.vcxproj", "{CB435430-EBB1-478B-8F4E-C256F6838F55}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "_wmi", "_wmi.vcxproj", "{54B1431F-B86B-4ACB-B28C-88BCF93191D8}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|ARM = Debug|ARM @@ -1503,6 +1505,38 @@ Global {CB435430-EBB1-478B-8F4E-C256F6838F55}.Release|Win32.Build.0 = Release|Win32 {CB435430-EBB1-478B-8F4E-C256F6838F55}.Release|x64.ActiveCfg = Release|x64 {CB435430-EBB1-478B-8F4E-C256F6838F55}.Release|x64.Build.0 = Release|x64 + {54B1431F-B86B-4ACB-B28C-88BCF93191D8}.Debug|ARM.ActiveCfg = Debug|ARM + {54B1431F-B86B-4ACB-B28C-88BCF93191D8}.Debug|ARM.Build.0 = Debug|ARM + {54B1431F-B86B-4ACB-B28C-88BCF93191D8}.Debug|ARM64.ActiveCfg = Debug|ARM64 + {54B1431F-B86B-4ACB-B28C-88BCF93191D8}.Debug|ARM64.Build.0 = Debug|ARM64 + {54B1431F-B86B-4ACB-B28C-88BCF93191D8}.Debug|Win32.ActiveCfg = Debug|Win32 + {54B1431F-B86B-4ACB-B28C-88BCF93191D8}.Debug|Win32.Build.0 = Debug|Win32 + {54B1431F-B86B-4ACB-B28C-88BCF93191D8}.Debug|x64.ActiveCfg = Debug|x64 + {54B1431F-B86B-4ACB-B28C-88BCF93191D8}.Debug|x64.Build.0 = Debug|x64 + {54B1431F-B86B-4ACB-B28C-88BCF93191D8}.PGInstrument|ARM.ActiveCfg = PGInstrument|ARM + {54B1431F-B86B-4ACB-B28C-88BCF93191D8}.PGInstrument|ARM.Build.0 = PGInstrument|ARM + {54B1431F-B86B-4ACB-B28C-88BCF93191D8}.PGInstrument|ARM64.ActiveCfg = PGInstrument|ARM64 + {54B1431F-B86B-4ACB-B28C-88BCF93191D8}.PGInstrument|ARM64.Build.0 = PGInstrument|ARM64 + {54B1431F-B86B-4ACB-B28C-88BCF93191D8}.PGInstrument|Win32.ActiveCfg = PGInstrument|Win32 + {54B1431F-B86B-4ACB-B28C-88BCF93191D8}.PGInstrument|Win32.Build.0 = PGInstrument|Win32 + {54B1431F-B86B-4ACB-B28C-88BCF93191D8}.PGInstrument|x64.ActiveCfg = PGInstrument|x64 + {54B1431F-B86B-4ACB-B28C-88BCF93191D8}.PGInstrument|x64.Build.0 = PGInstrument|x64 + {54B1431F-B86B-4ACB-B28C-88BCF93191D8}.PGUpdate|ARM.ActiveCfg = PGUpdate|ARM + {54B1431F-B86B-4ACB-B28C-88BCF93191D8}.PGUpdate|ARM.Build.0 = PGUpdate|ARM + {54B1431F-B86B-4ACB-B28C-88BCF93191D8}.PGUpdate|ARM64.ActiveCfg = PGUpdate|ARM64 + {54B1431F-B86B-4ACB-B28C-88BCF93191D8}.PGUpdate|ARM64.Build.0 = PGUpdate|ARM64 + {54B1431F-B86B-4ACB-B28C-88BCF93191D8}.PGUpdate|Win32.ActiveCfg = PGUpdate|Win32 + {54B1431F-B86B-4ACB-B28C-88BCF93191D8}.PGUpdate|Win32.Build.0 = PGUpdate|Win32 + {54B1431F-B86B-4ACB-B28C-88BCF93191D8}.PGUpdate|x64.ActiveCfg = PGUpdate|x64 + {54B1431F-B86B-4ACB-B28C-88BCF93191D8}.PGUpdate|x64.Build.0 = PGUpdate|x64 + {54B1431F-B86B-4ACB-B28C-88BCF93191D8}.Release|ARM.ActiveCfg = Release|ARM + {54B1431F-B86B-4ACB-B28C-88BCF93191D8}.Release|ARM.Build.0 = Release|ARM + {54B1431F-B86B-4ACB-B28C-88BCF93191D8}.Release|ARM64.ActiveCfg = Release|ARM64 + {54B1431F-B86B-4ACB-B28C-88BCF93191D8}.Release|ARM64.Build.0 = Release|ARM64 + {54B1431F-B86B-4ACB-B28C-88BCF93191D8}.Release|Win32.ActiveCfg = Release|Win32 + {54B1431F-B86B-4ACB-B28C-88BCF93191D8}.Release|Win32.Build.0 = Release|Win32 + {54B1431F-B86B-4ACB-B28C-88BCF93191D8}.Release|x64.ActiveCfg = Release|x64 + {54B1431F-B86B-4ACB-B28C-88BCF93191D8}.Release|x64.Build.0 = Release|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Tools/msi/lib/lib_files.wxs b/Tools/msi/lib/lib_files.wxs index 64c046e6dd9108..73c0231352f35c 100644 --- a/Tools/msi/lib/lib_files.wxs +++ b/Tools/msi/lib/lib_files.wxs @@ -1,6 +1,6 @@  - + From ba6ad6917949d55dea93d79462ace0582b7198ea Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Tue, 30 Aug 2022 19:11:43 +0100 Subject: [PATCH 02/15] Regenerate global objects --- Include/internal/pycore_global_strings.h | 1 + Include/internal/pycore_runtime_init_generated.h | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/Include/internal/pycore_global_strings.h b/Include/internal/pycore_global_strings.h index c736bfecd077fd..2301dec8f9d00d 100644 --- a/Include/internal/pycore_global_strings.h +++ b/Include/internal/pycore_global_strings.h @@ -526,6 +526,7 @@ struct _Py_global_strings { STRUCT_FOR_ID(protocol) STRUCT_FOR_ID(ps1) STRUCT_FOR_ID(ps2) + STRUCT_FOR_ID(query) STRUCT_FOR_ID(quotetabs) STRUCT_FOR_ID(r) STRUCT_FOR_ID(raw) diff --git a/Include/internal/pycore_runtime_init_generated.h b/Include/internal/pycore_runtime_init_generated.h index 58d9e934b96c19..59f8fa2bc17dbc 100644 --- a/Include/internal/pycore_runtime_init_generated.h +++ b/Include/internal/pycore_runtime_init_generated.h @@ -1035,6 +1035,7 @@ extern "C" { INIT_ID(protocol), \ INIT_ID(ps1), \ INIT_ID(ps2), \ + INIT_ID(query), \ INIT_ID(quotetabs), \ INIT_ID(r), \ INIT_ID(raw), \ @@ -2374,6 +2375,8 @@ _PyUnicode_InitStaticStrings(void) { PyUnicode_InternInPlace(&string); string = &_Py_ID(ps2); PyUnicode_InternInPlace(&string); + string = &_Py_ID(query); + PyUnicode_InternInPlace(&string); string = &_Py_ID(quotetabs); PyUnicode_InternInPlace(&string); string = &_Py_ID(r); @@ -6673,6 +6676,10 @@ _PyStaticObjects_CheckRefcnt(void) { _PyObject_Dump((PyObject *)&_Py_ID(ps2)); Py_FatalError("immortal object has less refcnt than expected _PyObject_IMMORTAL_REFCNT"); }; + if (Py_REFCNT((PyObject *)&_Py_ID(query)) < _PyObject_IMMORTAL_REFCNT) { + _PyObject_Dump((PyObject *)&_Py_ID(query)); + Py_FatalError("immortal object has less refcnt than expected _PyObject_IMMORTAL_REFCNT"); + }; if (Py_REFCNT((PyObject *)&_Py_ID(quotetabs)) < _PyObject_IMMORTAL_REFCNT) { _PyObject_Dump((PyObject *)&_Py_ID(quotetabs)); Py_FatalError("immortal object has less refcnt than expected _PyObject_IMMORTAL_REFCNT"); From 39a85552d8973f8cf6cfea44cee356ade23b781b Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Tue, 30 Aug 2022 20:04:27 +0100 Subject: [PATCH 03/15] Improve error handling and block non-SELECT queries --- PC/_wmimodule.cpp | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/PC/_wmimodule.cpp b/PC/_wmimodule.cpp index f76a4178405a66..ee7416397b1516 100644 --- a/PC/_wmimodule.cpp +++ b/PC/_wmimodule.cpp @@ -41,7 +41,7 @@ _query_thread(LPVOID param) BSTR bstrQuery = NULL; struct _query_data *data = (struct _query_data*)param; - HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED); + HRESULT hr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED); if (FAILED(hr)) { CloseHandle(data->writePipe); return (DWORD)hr; @@ -120,14 +120,17 @@ _query_thread(LPVOID param) WCHAR propStr[8192]; hr = VariantToString(propValue, propStr, sizeof(propStr) / sizeof(propStr[0])); if (SUCCEEDED(hr)) { - DWORD cbStr; + DWORD cbStr1, cbStr2; DWORD written; - cbStr = (DWORD)(wcslen(propName) * sizeof(propName[0])); - WriteFile(data->writePipe, propName, cbStr, &written, NULL); - WriteFile(data->writePipe, (LPVOID)L"=", 2, &written, NULL); - cbStr = (DWORD)(wcslen(propStr) * sizeof(propStr[0])); - WriteFile(data->writePipe, propStr, cbStr, &written, NULL); - WriteFile(data->writePipe, (LPVOID)L"\0", 2, &written, NULL); + cbStr1 = (DWORD)(wcslen(propName) * sizeof(propName[0])); + cbStr2 = (DWORD)(wcslen(propStr) * sizeof(propStr[0])); + if (!WriteFile(data->writePipe, propName, cbStr1, &written, NULL) || + !WriteFile(data->writePipe, (LPVOID)L"=", 2, &written, NULL) || + !WriteFile(data->writePipe, propStr, cbStr2, &written, NULL) || + !WriteFile(data->writePipe, (LPVOID)L"\0", 2, &written, NULL) + ) { + hr = HRESULT_FROM_WIN32(GetLastError()); + } } VariantClear(&propValue); SysFreeString(propName); @@ -175,7 +178,7 @@ _wmi_exec_query_impl(PyObject *module, PyObject *query) PyObject *result = NULL; HANDLE hThread = NULL; int err = 0; - WCHAR buffer[16384]; + WCHAR buffer[8192]; DWORD offset = 0; DWORD bytesRead; struct _query_data data = {0}; @@ -189,6 +192,12 @@ _wmi_exec_query_impl(PyObject *module, PyObject *query) return NULL; } + if (0 != _wcsnicmp(data.query, L"select ", 7)) { + PyMem_Free((void *)data.query); + PyErr_SetString(PyExc_ValueError, "only SELECT queries are supported"); + return NULL; + } + Py_BEGIN_ALLOW_THREADS if (!CreatePipe(&data.readPipe, &data.writePipe, NULL, 0) || @@ -236,6 +245,14 @@ _wmi_exec_query_impl(PyObject *module, PyObject *query) PyMem_Free((void *)data.query); + if (err == ERROR_MORE_DATA) { + PyErr_SetString(PyExc_OSError, "Query returns too much data"); + return NULL; + } else if (err) { + PyErr_SetFromWindowsErr(err); + return NULL; + } + return PyUnicode_FromWideChar(buffer, offset / sizeof(buffer[0]) - 1); } From 7db086b9fcf3d579fcf4deadbe8408e51139f982 Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Tue, 30 Aug 2022 22:34:20 +0100 Subject: [PATCH 04/15] Improved error handling and audit tests --- Lib/test/audit-tests.py | 11 +++++++++ Lib/test/test_audit.py | 15 ++++++++++++ Lib/test/test_wmi.py | 54 +++++++++++++++++++++++++++++++++++++++++ PC/_wmimodule.cpp | 36 +++++++++++++++++++-------- 4 files changed, 106 insertions(+), 10 deletions(-) create mode 100644 Lib/test/test_wmi.py diff --git a/Lib/test/audit-tests.py b/Lib/test/audit-tests.py index 00333cc9036a3c..66c08f7f2ff8d5 100644 --- a/Lib/test/audit-tests.py +++ b/Lib/test/audit-tests.py @@ -419,6 +419,17 @@ def hook(event, args): sys._getframe() +def test_wmi_exec_query(): + import _wmi + + def hook(event, args): + if event.startswith("_wmi."): + print(event, args[0]) + + sys.addaudithook(hook) + _wmi.exec_query("SELECT * FROM Win32_OperatingSystem") + + if __name__ == "__main__": from test.support import suppress_msvcrt_asserts diff --git a/Lib/test/test_audit.py b/Lib/test/test_audit.py index 18426f27a2e32f..09b3333afe184f 100644 --- a/Lib/test/test_audit.py +++ b/Lib/test/test_audit.py @@ -185,5 +185,20 @@ def test_sys_getframe(self): self.assertEqual(actual, expected) + + def test_wmi_exec_query(self): + import_helper.import_module("_wmi") + returncode, events, stderr = self.run_python("test_wmi_exec_query") + if returncode: + self.fail(stderr) + + if support.verbose: + print(*events, sep='\n') + actual = [(ev[0], ev[2]) for ev in events] + expected = [("_wmi.exec_query", "SELECT * FROM Win32_OperatingSystem")] + + self.assertEqual(actual, expected) + + if __name__ == "__main__": unittest.main() diff --git a/Lib/test/test_wmi.py b/Lib/test/test_wmi.py new file mode 100644 index 00000000000000..5d9f19f297aad6 --- /dev/null +++ b/Lib/test/test_wmi.py @@ -0,0 +1,54 @@ +# Test the internal _wmi module on Windows +# This is used + +import re +import sys +import unittest +from test.support import import_helper + + +# Do this first so test will be skipped if module doesn't exist +_wmi = import_helper.import_module('_wmi', required_on=['win']) + + +class WmiTests(unittest.TestCase): + def test_wmi_query_os_version(self): + r = _wmi.exec_query("SELECT Version FROM Win32_OperatingSystem").split("\0") + self.assertEqual(1, len(r)) + k, eq, v = r[0].partition("=") + self.assertEqual("=", eq, r[0]) + self.assertEqual("Version", k, r[0]) + # Best we can check for the version is that it's digits, dot, digits, anything + # Otherwise, we are likely checking the result of the query against itself + self.assertTrue(re.match(r"\d+\.\d+.+$", v), r[0]) + + def test_wmi_query_repeated(self): + # Repeated queries should not break + for _ in range(10): + self.test_wmi_query_os_version() + + def test_wmi_query_error(self): + # Invalid queries fail with OSError + try: + _wmi.exec_query("SELECT InvalidColumnName FROM InvalidTableName") + except OSError as ex: + if ex.winerror & 0xFFFFFFFF == 0x80041010: + # This is the expected error code. All others should fail the test + return + self.fail("Expected OSError") + + def test_wmi_query_repeated_error(self): + for _ in range(10): + self.test_wmi_query_error() + + def test_wmi_query_not_select(self): + # Queries other than SELECT are blocked to avoid potential exploits + with self.assertRaises(ValueError): + _wmi.exec_query("not select, just in case someone tries something") + + def test_wmi_query_overflow(self): + # Ensure very big queries fail + # Test multiple times to ensure consistency + for _ in range(2): + with self.assertRaises(OSError): + _wmi.exec_query("SELECT * FROM CIM_DataFile") diff --git a/PC/_wmimodule.cpp b/PC/_wmimodule.cpp index ee7416397b1516..038636a94e3178 100644 --- a/PC/_wmimodule.cpp +++ b/PC/_wmimodule.cpp @@ -200,9 +200,16 @@ _wmi_exec_query_impl(PyObject *module, PyObject *query) Py_BEGIN_ALLOW_THREADS - if (!CreatePipe(&data.readPipe, &data.writePipe, NULL, 0) || - !(hThread = CreateThread(NULL, 0, _query_thread, (LPVOID*)&data, 0, NULL))) { + if (!CreatePipe(&data.readPipe, &data.writePipe, NULL, 0)) { err = GetLastError(); + } else { + hThread = CreateThread(NULL, 0, _query_thread, (LPVOID*)&data, 0, NULL); + if (!hThread) { + err = GetLastError(); + // Normally the thread proc closes this handle, but since we never started + // we need to close it here. + CloseHandle(data.writePipe); + } } while (!err) { @@ -225,17 +232,26 @@ _wmi_exec_query_impl(PyObject *module, PyObject *query) if (data.readPipe) { CloseHandle(data.readPipe); } - if (data.writePipe) { - CloseHandle(data.writePipe); - } - if (err == ERROR_BROKEN_PIPE) { - // broken pipe indicates some kind of failure, but the real error - // code will come as the thread exit - if (WaitForSingleObject(hThread, 1000) != WAIT_OBJECT_0 || - !GetExitCodeThread(hThread, (LPDWORD)&err)) { + // Allow the thread some time to clean up + switch (WaitForSingleObject(hThread, 1000)) { + case WAIT_OBJECT_0: + // Thread ended cleanly + if (!GetExitCodeThread(hThread, (LPDWORD)&err)) { + err = GetLastError(); + } + break; + case WAIT_TIMEOUT: + // Probably stuck - there's not much we can do, unfortunately + if (err == 0 || err == ERROR_BROKEN_PIPE) { + err = WAIT_TIMEOUT; + } + break; + default: + if (err == 0 || err == ERROR_BROKEN_PIPE) { err = GetLastError(); } + break; } CloseHandle(hThread); From da37548d250f6be028ac26b8d42ad91a51c67881 Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Wed, 31 Aug 2022 00:16:29 +0100 Subject: [PATCH 05/15] Update platform module to use _wmi --- Lib/platform.py | 147 +++++++++++++++++++++++++++----------- Lib/test/test_platform.py | 21 ++++++ Lib/test/test_wmi.py | 2 +- 3 files changed, 126 insertions(+), 44 deletions(-) diff --git a/Lib/platform.py b/Lib/platform.py index df8faac88ca88a..0096ea0664b503 100755 --- a/Lib/platform.py +++ b/Lib/platform.py @@ -309,34 +309,52 @@ def _syscmd_ver(system='', release='', version='', version = _norm_version(version) return system, release, version -_WIN32_CLIENT_RELEASES = { - (5, 0): "2000", - (5, 1): "XP", - # Strictly, 5.2 client is XP 64-bit, but platform.py historically - # has always called it 2003 Server - (5, 2): "2003Server", - (5, None): "post2003", - - (6, 0): "Vista", - (6, 1): "7", - (6, 2): "8", - (6, 3): "8.1", - (6, None): "post8.1", - - (10, 0): "10", - (10, None): "post10", -} - -# Server release name lookup will default to client names if necessary -_WIN32_SERVER_RELEASES = { - (5, 2): "2003Server", - - (6, 0): "2008Server", - (6, 1): "2008ServerR2", - (6, 2): "2012Server", - (6, 3): "2012ServerR2", - (6, None): "post2012ServerR2", -} +try: + import _wmi +except ImportError: + def _wmi_query(*keys): + raise OSError("not supported") +else: + def _wmi_query(table, *keys): + table = { + "OS": "Win32_OperatingSystem", + "CPU": "Win32_Processor", + }[table] + data = _wmi.exec_query("SELECT {} FROM {}".format( + ",".join(keys), + table, + )).split("\0") + split_data = (i.partition("=") for i in data) + dict_data = {i[0]: i[2] for i in split_data} + return (dict_data[k] for k in keys) + + +_WIN32_CLIENT_RELEASES = [ + ((10, 1, 0), "post11"), + ((10, 0, 22000), "11"), + ((6, 4, 0), "10"), + ((6, 3, 0), "8.1"), + ((6, 2, 0), "8"), + ((6, 1, 0), "7"), + ((6, 0, 0), "Vista"), + ((5, 2, 3790), "XP64"), + ((5, 2, 0), "XPMedia"), + ((5, 1, 0), "XP"), + ((5, 0, 0), "2000"), +] + +_WIN32_SERVER_RELEASES = [ + ((10, 1, 0), "post2022Server"), + ((10, 0, 20348), "2022Server"), + ((10, 0, 17763), "2019Server"), + ((6, 4, 0), "2016Server"), + ((6, 3, 0), "2012ServerR2"), + ((6, 2, 0), "2012Server"), + ((6, 1, 0), "2008ServerR2"), + ((6, 0, 0), "2008Server"), + ((5, 2, 0), "2003Server"), + ((5, 0, 0), "2000Server"), +] def win32_is_iot(): return win32_edition() in ('IoTUAP', 'NanoServer', 'WindowsCoreHeadless', 'IoTEdgeOS') @@ -359,22 +377,40 @@ def win32_edition(): return None -def win32_ver(release='', version='', csd='', ptype=''): +def _win32_ver(version, csd, ptype): + # Try using WMI first, as this is the canonical source of data + try: + (version, product_type, ptype, spmajor, spminor) = _wmi_query( + 'OS', + 'Version', + 'ProductType', + 'BuildType', + 'ServicePackMajorVersion', + 'ServicePackMinorVersion', + ) + is_client = (int(product_type) == 1) + if spminor and spminor != '0': + csd = f'SP{spmajor}.{spminor}' + else: + csd = f'SP{spmajor}' + return version, csd, ptype, is_client + except OSError: + pass + + # Fall back to a combination of sys.getwindowsversion and "ver" try: from sys import getwindowsversion except ImportError: - return release, version, csd, ptype + return version, csd, ptype, True winver = getwindowsversion() + is_client = (getattr(winver, 'product_type', 1) == 1) try: - major, minor, build = map(int, _syscmd_ver()[2].split('.')) + version = _syscmd_ver()[2] + major, minor, build = map(int, version.split('.')) except ValueError: major, minor, build = winver.platform_version or winver[:3] - version = '{0}.{1}.{2}'.format(major, minor, build) - - release = (_WIN32_CLIENT_RELEASES.get((major, minor)) or - _WIN32_CLIENT_RELEASES.get((major, None)) or - release) + version = '{0}.{1}.{2}'.format(major, minor, build) # getwindowsversion() reflect the compatibility mode Python is # running under, and so the service pack value is only going to be @@ -386,12 +422,6 @@ def win32_ver(release='', version='', csd='', ptype=''): if csd[:13] == 'Service Pack ': csd = 'SP' + csd[13:] - # VER_NT_SERVER = 3 - if getattr(winver, 'product_type', None) == 3: - release = (_WIN32_SERVER_RELEASES.get((major, minor)) or - _WIN32_SERVER_RELEASES.get((major, None)) or - release) - try: try: import winreg @@ -407,6 +437,17 @@ def win32_ver(release='', version='', csd='', ptype=''): except OSError: pass + return version, csd, ptype, is_client + +def win32_ver(release='', version='', csd='', ptype=''): + is_client = False + + version, csd, ptype, is_client = _win32_ver(version, csd, ptype) + + intversion = tuple(map(int, version.split('.'))) + releases = _WIN32_CLIENT_RELEASES if is_client else _WIN32_SERVER_RELEASES + release = next((r for v, r in releases if v <= intversion), release) + return release, version, csd, ptype @@ -725,6 +766,21 @@ def _get_machine_win32(): # http://www.geocities.com/rick_lively/MANUALS/ENV/MSWIN/PROCESSI.HTM # WOW64 processes mask the native architecture + try: + arch, = _wmi_query('CPU', 'Architecture') + except OSError: + pass + else: + try: + arch = ['x86', 'MIPS', 'Alpha', 'PowerPC', None, + 'ARM', 'ia64', None, None, + 'AMD64', None, None, 'ARM64', + ][int(arch)] + except (ValueError, IndexError): + pass + else: + if arch: + return arch return ( os.environ.get('PROCESSOR_ARCHITEW6432', '') or os.environ.get('PROCESSOR_ARCHITECTURE', '') @@ -738,7 +794,12 @@ def get(cls): return func() or '' def get_win32(): - return os.environ.get('PROCESSOR_IDENTIFIER', _get_machine_win32()) + try: + manufacturer, caption = _wmi_query('CPU', 'Manufacturer', 'Caption') + except OSError: + return os.environ.get('PROCESSOR_IDENTIFIER', _get_machine_win32()) + else: + return f'{caption}, {manufacturer}' def get_OpenVMS(): try: diff --git a/Lib/test/test_platform.py b/Lib/test/test_platform.py index 9b2cd201f3c2fe..0d3100129ca21f 100644 --- a/Lib/test/test_platform.py +++ b/Lib/test/test_platform.py @@ -229,6 +229,18 @@ def test_uname(self): self.assertEqual(res[-1], res.processor) self.assertEqual(len(res), 6) + @unittest.skipUnless(sys.platform.startswith('win'), "windows only test") + def test_uname_win32_without_wmi(self): + old_wmi_query = platform._wmi_query + def raises_oserror(*a): + raise OSError() + platform._wmi_query = raises_oserror + + try: + self.test_uname() + finally: + platform._wmi_query = old_wmi_query + def test_uname_cast_to_tuple(self): res = platform.uname() expected = ( @@ -289,6 +301,14 @@ def test_uname_win32_ARCHITEW6432(self): # on 64 bit Windows: if PROCESSOR_ARCHITEW6432 exists we should be # using it, per # http://blogs.msdn.com/david.wang/archive/2006/03/26/HOWTO-Detect-Process-Bitness.aspx + + # We also need to suppress WMI checks, as those are reliable and + # overrule the environment variables + old_wmi_query = platform._wmi_query + def raises_oserror(*a): + raise OSError() + platform._wmi_query = raises_oserror + try: with os_helper.EnvironmentVarGuard() as environ: if 'PROCESSOR_ARCHITEW6432' in environ: @@ -303,6 +323,7 @@ def test_uname_win32_ARCHITEW6432(self): self.assertEqual(machine, 'bar') finally: platform._uname_cache = None + platform._wmi_query = old_wmi_query def test_java_ver(self): res = platform.java_ver() diff --git a/Lib/test/test_wmi.py b/Lib/test/test_wmi.py index 5d9f19f297aad6..2fb789eb5f60fd 100644 --- a/Lib/test/test_wmi.py +++ b/Lib/test/test_wmi.py @@ -1,5 +1,5 @@ # Test the internal _wmi module on Windows -# This is used +# This is used by the platform module, and potentially others import re import sys From 1264330e3ed1d381abf5cf9392c7e49b43b4a8c5 Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Wed, 31 Aug 2022 00:17:29 +0100 Subject: [PATCH 06/15] Update NEWS --- .../next/Windows/2022-08-26-00-11-18.gh-issue-89545.zmJMY_.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Windows/2022-08-26-00-11-18.gh-issue-89545.zmJMY_.rst b/Misc/NEWS.d/next/Windows/2022-08-26-00-11-18.gh-issue-89545.zmJMY_.rst index 0527ca7d0490fa..60e3b4024d407c 100644 --- a/Misc/NEWS.d/next/Windows/2022-08-26-00-11-18.gh-issue-89545.zmJMY_.rst +++ b/Misc/NEWS.d/next/Windows/2022-08-26-00-11-18.gh-issue-89545.zmJMY_.rst @@ -1 +1 @@ -Adds internal ``_wmi`` module for directly querying OS properties +Updates :mod:`platform` to use native WMI queries to determine OS version, type, and architecture. From 8a8e950f7487d96281dd1852961f6e47f79b9add Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Wed, 31 Aug 2022 00:48:28 +0100 Subject: [PATCH 07/15] Only use version when it's not empty --- Lib/platform.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Lib/platform.py b/Lib/platform.py index 0096ea0664b503..1083b2220ca924 100755 --- a/Lib/platform.py +++ b/Lib/platform.py @@ -444,9 +444,10 @@ def win32_ver(release='', version='', csd='', ptype=''): version, csd, ptype, is_client = _win32_ver(version, csd, ptype) - intversion = tuple(map(int, version.split('.'))) - releases = _WIN32_CLIENT_RELEASES if is_client else _WIN32_SERVER_RELEASES - release = next((r for v, r in releases if v <= intversion), release) + if version: + intversion = tuple(map(int, version.split('.'))) + releases = _WIN32_CLIENT_RELEASES if is_client else _WIN32_SERVER_RELEASES + release = next((r for v, r in releases if v <= intversion), release) return release, version, csd, ptype From a77841fa055131f40e8b9ffb06cfa771de102fe8 Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Wed, 31 Aug 2022 16:47:31 +0100 Subject: [PATCH 08/15] Apply suggestions from code review Co-authored-by: Victor Stinner --- ...2022-08-26-00-11-18.gh-issue-89545.zmJMY_.rst | 2 +- PC/_wmimodule.cpp | 16 +++++++--------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/Misc/NEWS.d/next/Windows/2022-08-26-00-11-18.gh-issue-89545.zmJMY_.rst b/Misc/NEWS.d/next/Windows/2022-08-26-00-11-18.gh-issue-89545.zmJMY_.rst index 60e3b4024d407c..eeedbf9d6fa8f6 100644 --- a/Misc/NEWS.d/next/Windows/2022-08-26-00-11-18.gh-issue-89545.zmJMY_.rst +++ b/Misc/NEWS.d/next/Windows/2022-08-26-00-11-18.gh-issue-89545.zmJMY_.rst @@ -1 +1 @@ -Updates :mod:`platform` to use native WMI queries to determine OS version, type, and architecture. +Updates :mod:`platform` code getting the Windows version to use native Windows Management Instrumentation (WMI) queries to determine OS version, type, and architecture. diff --git a/PC/_wmimodule.cpp b/PC/_wmimodule.cpp index 038636a94e3178..0886b6a2c36e1b 100644 --- a/PC/_wmimodule.cpp +++ b/PC/_wmimodule.cpp @@ -262,13 +262,16 @@ _wmi_exec_query_impl(PyObject *module, PyObject *query) PyMem_Free((void *)data.query); if (err == ERROR_MORE_DATA) { - PyErr_SetString(PyExc_OSError, "Query returns too much data"); + PyErr_Format(PyExc_OSError, "Query returns more than %zd characters", Py_ARRAY_LENGTH(buffer)); return NULL; } else if (err) { PyErr_SetFromWindowsErr(err); return NULL; } + if (!offset) { + return PyUnicode_FromStringAndSize(NULL, 0); + } return PyUnicode_FromWideChar(buffer, offset / sizeof(buffer[0]) - 1); } @@ -292,14 +295,9 @@ static PyModuleDef_Slot wmi_slots[] = { static PyModuleDef wmi_def = { PyModuleDef_HEAD_INIT, - "_wmi", - NULL, // doc - 0, // m_size - NULL, // m_methods - wmi_slots, - NULL, // m_traverse - NULL, // m_clear - NULL, // m_free + .m_name = "_wmi", + .m_size = 0, + .m_slots = wmi_slots, }; extern "C" { From 9002d8ef8a7c24be18c036beb147c942708b204e Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Wed, 31 Aug 2022 16:57:48 +0100 Subject: [PATCH 09/15] Additional PR feedback --- Lib/platform.py | 2 +- Lib/test/test_platform.py | 36 +++++++++++++++--------------------- PC/_wmimodule.cpp | 19 ++++--------------- 3 files changed, 20 insertions(+), 37 deletions(-) diff --git a/Lib/platform.py b/Lib/platform.py index 1083b2220ca924..9f5b317287530b 100755 --- a/Lib/platform.py +++ b/Lib/platform.py @@ -768,7 +768,7 @@ def _get_machine_win32(): # WOW64 processes mask the native architecture try: - arch, = _wmi_query('CPU', 'Architecture') + [arch, *_] = _wmi_query('CPU', 'Architecture') except OSError: pass else: diff --git a/Lib/test/test_platform.py b/Lib/test/test_platform.py index 0d3100129ca21f..9c03a89fd57d07 100644 --- a/Lib/test/test_platform.py +++ b/Lib/test/test_platform.py @@ -231,15 +231,11 @@ def test_uname(self): @unittest.skipUnless(sys.platform.startswith('win'), "windows only test") def test_uname_win32_without_wmi(self): - old_wmi_query = platform._wmi_query def raises_oserror(*a): raise OSError() - platform._wmi_query = raises_oserror - try: + with support.swap_attr(platform, '_wmi_query', raises_oserror): self.test_uname() - finally: - platform._wmi_query = old_wmi_query def test_uname_cast_to_tuple(self): res = platform.uname() @@ -304,26 +300,24 @@ def test_uname_win32_ARCHITEW6432(self): # We also need to suppress WMI checks, as those are reliable and # overrule the environment variables - old_wmi_query = platform._wmi_query def raises_oserror(*a): raise OSError() - platform._wmi_query = raises_oserror - try: + with support.swap_attr(platform, '_wmi_query', raises_oserror): with os_helper.EnvironmentVarGuard() as environ: - if 'PROCESSOR_ARCHITEW6432' in environ: - del environ['PROCESSOR_ARCHITEW6432'] - environ['PROCESSOR_ARCHITECTURE'] = 'foo' - platform._uname_cache = None - system, node, release, version, machine, processor = platform.uname() - self.assertEqual(machine, 'foo') - environ['PROCESSOR_ARCHITEW6432'] = 'bar' - platform._uname_cache = None - system, node, release, version, machine, processor = platform.uname() - self.assertEqual(machine, 'bar') - finally: - platform._uname_cache = None - platform._wmi_query = old_wmi_query + try: + if 'PROCESSOR_ARCHITEW6432' in environ: + del environ['PROCESSOR_ARCHITEW6432'] + environ['PROCESSOR_ARCHITECTURE'] = 'foo' + platform._uname_cache = None + system, node, release, version, machine, processor = platform.uname() + self.assertEqual(machine, 'foo') + environ['PROCESSOR_ARCHITEW6432'] = 'bar' + platform._uname_cache = None + system, node, release, version, machine, processor = platform.uname() + self.assertEqual(machine, 'bar') + finally: + platform._uname_cache = None def test_java_ver(self): res = platform.java_ver() diff --git a/PC/_wmimodule.cpp b/PC/_wmimodule.cpp index 0886b6a2c36e1b..6e1ff155e03e68 100644 --- a/PC/_wmimodule.cpp +++ b/PC/_wmimodule.cpp @@ -281,23 +281,12 @@ static PyMethodDef wmi_functions[] = { { NULL, NULL, 0, NULL } }; -static int exec_wmi(PyObject *module) -{ - PyModule_AddFunctions(module, wmi_functions); - - return 0; // success -} - -static PyModuleDef_Slot wmi_slots[] = { - { Py_mod_exec, exec_wmi }, - { 0, NULL } -}; - static PyModuleDef wmi_def = { PyModuleDef_HEAD_INIT, - .m_name = "_wmi", - .m_size = 0, - .m_slots = wmi_slots, + "_wmi", + NULL, // doc + 0, // m_size + wmi_functions }; extern "C" { From aef3a4d78710bade3bf080fce905d6e2dd38ff39 Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Wed, 31 Aug 2022 19:35:30 +0100 Subject: [PATCH 10/15] Update sys.getwindowsversion to use better calculation --- Lib/ntpath.py | 5 +- Python/sysmodule.c | 160 +++++++++++++++++++++++++++++++++++++-------- 2 files changed, 133 insertions(+), 32 deletions(-) diff --git a/Lib/ntpath.py b/Lib/ntpath.py index 959bcd09831186..d9582f4087433e 100644 --- a/Lib/ntpath.py +++ b/Lib/ntpath.py @@ -732,9 +732,8 @@ def realpath(path, *, strict=False): return path -# Win9x family and earlier have no Unicode filename support. -supports_unicode_filenames = (hasattr(sys, "getwindowsversion") and - sys.getwindowsversion()[3] >= 2) +# All supported version have Unicode filename support. +supports_unicode_filenames = True def relpath(path, start=None): """Return a relative version of a path""" diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 75e64553d88c9f..54b6aef359fa66 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -1486,6 +1486,108 @@ static PyStructSequence_Desc windows_version_desc = { via indexing, the rest are name only */ }; +static PyObject * +_sys_getwindowsversion_from_wmi() +{ + PyObject *wmi_module = PyImport_ImportModule("_wmi"); + if (!wmi_module) { + return NULL; + } + + PyObject *queryResult = PyObject_CallMethod(wmi_module, "exec_query", "s", + "SELECT Version FROM Win32_OperatingSystem" + ); + Py_DECREF(wmi_module); + + if (!queryResult) { + return NULL; + } + + char versionString[256]; + const char *queryResultString = PyUnicode_AsUTF8(queryResult); + if (!queryResultString) { + return NULL; + } + if (strcpy_s(versionString, sizeof(versionString), queryResultString)) { + PyErr_SetFromErrno(0); + Py_DECREF(queryResultString); + return NULL; + } + if (0 != _strnicmp(versionString, "version=", 8)) { + PyErr_Format(PyExc_SystemError, "invalid WMI result: %s", versionString); + Py_DECREF(queryResult); + return NULL; + } + char *majorStart = strchr(versionString, '='); + if (!majorStart || !majorStart[1]) { + PyErr_Format(PyExc_SystemError, "invalid WMI result: %s", versionString); + Py_DECREF(queryResult); + return NULL; + } + majorStart += 1; + char *minorStart = strchr(majorStart, '.'); + if (!minorStart || !minorStart[1]) { + PyErr_Format(PyExc_SystemError, "invalid WMI result: %s", versionString); + Py_DECREF(queryResult); + return NULL; + } + *minorStart = '\0'; + minorStart += 1; + char *buildStart = strchr(minorStart, '.'); + if (!buildStart || !buildStart[1]) { + PyErr_Format(PyExc_SystemError, "invalid WMI result: %s", versionString); + Py_DECREF(queryResult); + return NULL; + } + *buildStart = '\0'; + buildStart += 1; + PyObject *result = Py_BuildValue("(NNN)", + PyLong_FromString(majorStart, NULL, 10), + PyLong_FromString(minorStart, NULL, 10), + PyLong_FromString(buildStart, NULL, 10) + ); + Py_DECREF(queryResult); + return result; +} + +static PyObject * +_sys_getwindowsversion_from_kernel32() +{ + HANDLE hKernel32; + wchar_t kernel32_path[MAX_PATH]; + LPVOID verblock; + DWORD verblock_size; + VS_FIXEDFILEINFO *ffi; + UINT ffi_len; + DWORD realMajor, realMinor, realBuild; + + Py_BEGIN_ALLOW_THREADS + hKernel32 = GetModuleHandleW(L"kernel32.dll"); + Py_END_ALLOW_THREADS + if (!hKernel32 || !GetModuleFileNameW(hKernel32, kernel32_path, MAX_PATH)) { + PyErr_SetFromWindowsErr(0); + return NULL; + } + verblock_size = GetFileVersionInfoSizeW(kernel32_path, NULL); + if (!verblock_size) { + PyErr_SetFromWindowsErr(0); + return NULL; + } + verblock = PyMem_RawMalloc(verblock_size); + if (!verblock || + !GetFileVersionInfoW(kernel32_path, 0, verblock_size, verblock) || + !VerQueryValueW(verblock, L"", (LPVOID)&ffi, &ffi_len)) { + PyErr_SetFromWindowsErr(0); + return NULL; + } + + realMajor = HIWORD(ffi->dwProductVersionMS); + realMinor = LOWORD(ffi->dwProductVersionMS); + realBuild = HIWORD(ffi->dwProductVersionLS); + PyMem_RawFree(verblock); + return Py_BuildValue("(kkk)", realMajor, realMinor, realBuild); +} + /* Disable deprecation warnings about GetVersionEx as the result is being passed straight through to the caller, who is responsible for using it correctly. */ @@ -1515,11 +1617,13 @@ sys_getwindowsversion_impl(PyObject *module) PyObject *version; int pos = 0; OSVERSIONINFOEXW ver; - DWORD realMajor, realMinor, realBuild; - HANDLE hKernel32; - wchar_t kernel32_path[MAX_PATH]; - LPVOID verblock; - DWORD verblock_size; + + version = PyObject_GetAttrString(module, "_cached_windows_version"); + if (version && PyObject_TypeCheck(version, &WindowsVersionType)) { + return version; + } + Py_XDECREF(version); + PyErr_Clear(); ver.dwOSVersionInfoSize = sizeof(ver); if (!GetVersionExW((OSVERSIONINFOW*) &ver)) @@ -1539,41 +1643,39 @@ sys_getwindowsversion_impl(PyObject *module) PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wSuiteMask)); PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wProductType)); - realMajor = ver.dwMajorVersion; - realMinor = ver.dwMinorVersion; - realBuild = ver.dwBuildNumber; - // GetVersion will lie if we are running in a compatibility mode. // We need to read the version info from a system file resource // to accurately identify the OS version. If we fail for any reason, // just return whatever GetVersion said. - Py_BEGIN_ALLOW_THREADS - hKernel32 = GetModuleHandleW(L"kernel32.dll"); - Py_END_ALLOW_THREADS - if (hKernel32 && GetModuleFileNameW(hKernel32, kernel32_path, MAX_PATH) && - (verblock_size = GetFileVersionInfoSizeW(kernel32_path, NULL)) && - (verblock = PyMem_RawMalloc(verblock_size))) { - VS_FIXEDFILEINFO *ffi; - UINT ffi_len; - - if (GetFileVersionInfoW(kernel32_path, 0, verblock_size, verblock) && - VerQueryValueW(verblock, L"", (LPVOID)&ffi, &ffi_len)) { - realMajor = HIWORD(ffi->dwProductVersionMS); - realMinor = LOWORD(ffi->dwProductVersionMS); - realBuild = HIWORD(ffi->dwProductVersionLS); + PyObject *realVersion = _sys_getwindowsversion_from_wmi(); + if (!realVersion) { + return NULL; + PyErr_Clear(); + realVersion = _sys_getwindowsversion_from_kernel32(); + if (!realVersion) { + PyErr_Clear(); + realVersion = Py_BuildValue("(kkk)", + ver.dwMajorVersion, + ver.dwMinorVersion, + ver.dwBuildNumber + ); } - PyMem_RawFree(verblock); } - PyStructSequence_SET_ITEM(version, pos++, Py_BuildValue("(kkk)", - realMajor, - realMinor, - realBuild - )); + + if (realVersion) { + PyStructSequence_SET_ITEM(version, pos++, realVersion); + } if (PyErr_Occurred()) { Py_DECREF(version); return NULL; } + + if (PyObject_SetAttrString(module, "_cached_windows_version", version) < 0) { + Py_DECREF(version); + return NULL; + } + return version; } From 6b32d88c772744bd63f95c3aca0494aa5cbf3a33 Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Wed, 31 Aug 2022 19:50:38 +0100 Subject: [PATCH 11/15] Add extra null separator between instances --- Lib/test/test_wmi.py | 13 +++++++++++++ PC/_wmimodule.cpp | 7 ++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_wmi.py b/Lib/test/test_wmi.py index 2fb789eb5f60fd..af2a453dcb5216 100644 --- a/Lib/test/test_wmi.py +++ b/Lib/test/test_wmi.py @@ -52,3 +52,16 @@ def test_wmi_query_overflow(self): for _ in range(2): with self.assertRaises(OSError): _wmi.exec_query("SELECT * FROM CIM_DataFile") + + def test_wmi_query_multiple_rows(self): + # Multiple instances should have an extra null separator + r = _wmi.exec_query("SELECT ProcessId FROM Win32_Process WHERE ProcessId < 1000") + self.assertFalse(r.startswith("\0"), r) + self.assertFalse(r.endswith("\0"), r) + it = iter(r.split("\0")) + try: + while True: + self.assertTrue(re.match(r"ProcessId=\d+", next(it))) + self.assertEqual("", next(it)) + except StopIteration: + pass diff --git a/PC/_wmimodule.cpp b/PC/_wmimodule.cpp index 6e1ff155e03e68..d9b7f2bd6d3501 100644 --- a/PC/_wmimodule.cpp +++ b/PC/_wmimodule.cpp @@ -91,9 +91,11 @@ _query_thread(LPVOID param) // Okay, after all that, at this stage we should have an enumerator // to the query results and can start writing them to the pipe! IWbemClassObject *value = NULL; + int startOfEnum = TRUE; int endOfEnum = FALSE; while (SUCCEEDED(hr) && !endOfEnum) { ULONG got = 0; + DWORD written; hr = enumerator->Next(WBEM_INFINITE, 1, &value, &got); if (hr == WBEM_S_FALSE) { // Could be at the end, but still got a result this time @@ -104,6 +106,10 @@ _query_thread(LPVOID param) if (FAILED(hr) || got != 1 || !value) { continue; } + if (!startOfEnum && !WriteFile(data->writePipe, (LPVOID)L"\0", 2, &written, NULL)) { + hr = HRESULT_FROM_WIN32(GetLastError()); + } + startOfEnum = FALSE; // Okay, now we have each resulting object it's time to // enumerate its members hr = value->BeginEnumeration(0); @@ -121,7 +127,6 @@ _query_thread(LPVOID param) hr = VariantToString(propValue, propStr, sizeof(propStr) / sizeof(propStr[0])); if (SUCCEEDED(hr)) { DWORD cbStr1, cbStr2; - DWORD written; cbStr1 = (DWORD)(wcslen(propName) * sizeof(propName[0])); cbStr2 = (DWORD)(wcslen(propStr) * sizeof(propStr[0])); if (!WriteFile(data->writePipe, propName, cbStr1, &written, NULL) || From 04330f825a3759c900b26e3086678f870ac01535 Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Thu, 1 Sep 2022 21:04:01 +0100 Subject: [PATCH 12/15] Update PC/_wmimodule.cpp Co-authored-by: Eryk Sun --- PC/_wmimodule.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/PC/_wmimodule.cpp b/PC/_wmimodule.cpp index d9b7f2bd6d3501..3df56b5857b885 100644 --- a/PC/_wmimodule.cpp +++ b/PC/_wmimodule.cpp @@ -108,6 +108,7 @@ _query_thread(LPVOID param) } if (!startOfEnum && !WriteFile(data->writePipe, (LPVOID)L"\0", 2, &written, NULL)) { hr = HRESULT_FROM_WIN32(GetLastError()); + break; } startOfEnum = FALSE; // Okay, now we have each resulting object it's time to From c3f9e9c856e72440a0a834aecad2d80cbcc08720 Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Thu, 1 Sep 2022 21:14:29 +0100 Subject: [PATCH 13/15] Add missing error handling --- PC/_wmimodule.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/PC/_wmimodule.cpp b/PC/_wmimodule.cpp index 3df56b5857b885..a9f7836172ba2c 100644 --- a/PC/_wmimodule.cpp +++ b/PC/_wmimodule.cpp @@ -114,6 +114,10 @@ _query_thread(LPVOID param) // Okay, now we have each resulting object it's time to // enumerate its members hr = value->BeginEnumeration(0); + if (FAILED(hr)) { + value->Release(); + break; + } while (SUCCEEDED(hr)) { BSTR propName; VARIANT propValue; From 70b251811d9900f2aea56cb8a4b62cb0f370d004 Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Mon, 5 Sep 2022 18:24:39 +0100 Subject: [PATCH 14/15] Remove WMI usage from sys.getwindowsversion --- Python/sysmodule.c | 81 ++++------------------------------------------ 1 file changed, 6 insertions(+), 75 deletions(-) diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 465a532d13fec7..653b5a55e885e5 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -1487,70 +1487,6 @@ static PyStructSequence_Desc windows_version_desc = { via indexing, the rest are name only */ }; -static PyObject * -_sys_getwindowsversion_from_wmi() -{ - PyObject *wmi_module = PyImport_ImportModule("_wmi"); - if (!wmi_module) { - return NULL; - } - - PyObject *queryResult = PyObject_CallMethod(wmi_module, "exec_query", "s", - "SELECT Version FROM Win32_OperatingSystem" - ); - Py_DECREF(wmi_module); - - if (!queryResult) { - return NULL; - } - - char versionString[256]; - const char *queryResultString = PyUnicode_AsUTF8(queryResult); - if (!queryResultString) { - return NULL; - } - if (strcpy_s(versionString, sizeof(versionString), queryResultString)) { - PyErr_SetFromErrno(0); - Py_DECREF(queryResultString); - return NULL; - } - if (0 != _strnicmp(versionString, "version=", 8)) { - PyErr_Format(PyExc_SystemError, "invalid WMI result: %s", versionString); - Py_DECREF(queryResult); - return NULL; - } - char *majorStart = strchr(versionString, '='); - if (!majorStart || !majorStart[1]) { - PyErr_Format(PyExc_SystemError, "invalid WMI result: %s", versionString); - Py_DECREF(queryResult); - return NULL; - } - majorStart += 1; - char *minorStart = strchr(majorStart, '.'); - if (!minorStart || !minorStart[1]) { - PyErr_Format(PyExc_SystemError, "invalid WMI result: %s", versionString); - Py_DECREF(queryResult); - return NULL; - } - *minorStart = '\0'; - minorStart += 1; - char *buildStart = strchr(minorStart, '.'); - if (!buildStart || !buildStart[1]) { - PyErr_Format(PyExc_SystemError, "invalid WMI result: %s", versionString); - Py_DECREF(queryResult); - return NULL; - } - *buildStart = '\0'; - buildStart += 1; - PyObject *result = Py_BuildValue("(NNN)", - PyLong_FromString(majorStart, NULL, 10), - PyLong_FromString(minorStart, NULL, 10), - PyLong_FromString(buildStart, NULL, 10) - ); - Py_DECREF(queryResult); - return result; -} - static PyObject * _sys_getwindowsversion_from_kernel32() { @@ -1648,19 +1584,14 @@ sys_getwindowsversion_impl(PyObject *module) // We need to read the version info from a system file resource // to accurately identify the OS version. If we fail for any reason, // just return whatever GetVersion said. - PyObject *realVersion = _sys_getwindowsversion_from_wmi(); + PyObject *realVersion = _sys_getwindowsversion_from_kernel32(); if (!realVersion) { - return NULL; PyErr_Clear(); - realVersion = _sys_getwindowsversion_from_kernel32(); - if (!realVersion) { - PyErr_Clear(); - realVersion = Py_BuildValue("(kkk)", - ver.dwMajorVersion, - ver.dwMinorVersion, - ver.dwBuildNumber - ); - } + realVersion = Py_BuildValue("(kkk)", + ver.dwMajorVersion, + ver.dwMinorVersion, + ver.dwBuildNumber + ); } if (realVersion) { From 9d77094673406733301a6c457f0247dcdaaa21f0 Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Tue, 6 Sep 2022 15:03:15 +0100 Subject: [PATCH 15/15] Apply suggestions from code review Co-authored-by: Eryk Sun --- PCbuild/_wmi.vcxproj | 1 + PCbuild/_wmi.vcxproj.filters | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/PCbuild/_wmi.vcxproj b/PCbuild/_wmi.vcxproj index c0fc04cbcd195e..c1914a3fa5a1bf 100644 --- a/PCbuild/_wmi.vcxproj +++ b/PCbuild/_wmi.vcxproj @@ -98,6 +98,7 @@ wbemuuid.lib;propsys.lib;%(AdditionalDependencies) + ole32.dll diff --git a/PCbuild/_wmi.vcxproj.filters b/PCbuild/_wmi.vcxproj.filters index 705902ff50c3a6..fa8046237a0b71 100644 --- a/PCbuild/_wmi.vcxproj.filters +++ b/PCbuild/_wmi.vcxproj.filters @@ -10,7 +10,7 @@ - + Source Files