mirror of
https://github.com/python/cpython
synced 2024-10-14 09:01:41 +00:00
bpo-39401: Avoid unsafe DLL load on Windows 7 and earlier (GH-18231)
As Windows 7 is not supported by Python 3.9, we just replace the dynamic load with a static import. Backports will have a different fix to ensure they continue to behave the same.
This commit is contained in:
parent
0cd5bff6b7
commit
6a65eba44b
|
@ -0,0 +1 @@
|
||||||
|
Avoid unsafe DLL load at startup on Windows 7 and earlier.
|
|
@ -91,6 +91,7 @@
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
|
#include <pathcch.h>
|
||||||
#include <shlwapi.h>
|
#include <shlwapi.h>
|
||||||
|
|
||||||
#ifdef HAVE_SYS_TYPES_H
|
#ifdef HAVE_SYS_TYPES_H
|
||||||
|
@ -242,42 +243,14 @@ ismodule(wchar_t *filename, int update_filename)
|
||||||
stuff as fits will be appended.
|
stuff as fits will be appended.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static int _PathCchCombineEx_Initialized = 0;
|
|
||||||
typedef HRESULT(__stdcall *PPathCchCombineEx) (PWSTR pszPathOut, size_t cchPathOut,
|
|
||||||
PCWSTR pszPathIn, PCWSTR pszMore,
|
|
||||||
unsigned long dwFlags);
|
|
||||||
static PPathCchCombineEx _PathCchCombineEx;
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
join(wchar_t *buffer, const wchar_t *stuff)
|
join(wchar_t *buffer, const wchar_t *stuff)
|
||||||
{
|
{
|
||||||
if (_PathCchCombineEx_Initialized == 0) {
|
if (FAILED(PathCchCombineEx(buffer, MAXPATHLEN+1, buffer, stuff, 0))) {
|
||||||
HMODULE pathapi = LoadLibraryW(L"api-ms-win-core-path-l1-1-0.dll");
|
Py_FatalError("buffer overflow in getpathp.c's join()");
|
||||||
if (pathapi) {
|
|
||||||
_PathCchCombineEx = (PPathCchCombineEx)GetProcAddress(pathapi, "PathCchCombineEx");
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
_PathCchCombineEx = NULL;
|
|
||||||
}
|
|
||||||
_PathCchCombineEx_Initialized = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_PathCchCombineEx) {
|
|
||||||
if (FAILED(_PathCchCombineEx(buffer, MAXPATHLEN+1, buffer, stuff, 0))) {
|
|
||||||
Py_FatalError("buffer overflow in getpathp.c's join()");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (!PathCombineW(buffer, buffer, stuff)) {
|
|
||||||
Py_FatalError("buffer overflow in getpathp.c's join()");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int _PathCchCanonicalizeEx_Initialized = 0;
|
|
||||||
typedef HRESULT(__stdcall *PPathCchCanonicalizeEx) (PWSTR pszPathOut, size_t cchPathOut,
|
|
||||||
PCWSTR pszPathIn, unsigned long dwFlags);
|
|
||||||
static PPathCchCanonicalizeEx _PathCchCanonicalizeEx;
|
|
||||||
|
|
||||||
/* Call PathCchCanonicalizeEx(path): remove navigation elements such as "."
|
/* Call PathCchCanonicalizeEx(path): remove navigation elements such as "."
|
||||||
and ".." to produce a direct, well-formed path. */
|
and ".." to produce a direct, well-formed path. */
|
||||||
static PyStatus
|
static PyStatus
|
||||||
|
@ -287,26 +260,8 @@ canonicalize(wchar_t *buffer, const wchar_t *path)
|
||||||
return _PyStatus_NO_MEMORY();
|
return _PyStatus_NO_MEMORY();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_PathCchCanonicalizeEx_Initialized == 0) {
|
if (FAILED(PathCchCanonicalizeEx(buffer, MAXPATHLEN + 1, path, 0))) {
|
||||||
HMODULE pathapi = LoadLibraryW(L"api-ms-win-core-path-l1-1-0.dll");
|
return INIT_ERR_BUFFER_OVERFLOW();
|
||||||
if (pathapi) {
|
|
||||||
_PathCchCanonicalizeEx = (PPathCchCanonicalizeEx)GetProcAddress(pathapi, "PathCchCanonicalizeEx");
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
_PathCchCanonicalizeEx = NULL;
|
|
||||||
}
|
|
||||||
_PathCchCanonicalizeEx_Initialized = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_PathCchCanonicalizeEx) {
|
|
||||||
if (FAILED(_PathCchCanonicalizeEx(buffer, MAXPATHLEN + 1, path, 0))) {
|
|
||||||
return INIT_ERR_BUFFER_OVERFLOW();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if (!PathCanonicalizeW(buffer, path)) {
|
|
||||||
return INIT_ERR_BUFFER_OVERFLOW();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return _PyStatus_OK();
|
return _PyStatus_OK();
|
||||||
}
|
}
|
||||||
|
|
|
@ -106,7 +106,7 @@
|
||||||
<PreprocessorDefinitions Condition="$(IncludeExternals)">_Py_HAVE_ZLIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
<PreprocessorDefinitions Condition="$(IncludeExternals)">_Py_HAVE_ZLIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<Link>
|
<Link>
|
||||||
<AdditionalDependencies>version.lib;shlwapi.lib;ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
<AdditionalDependencies>version.lib;shlwapi.lib;ws2_32.lib;pathcch.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||||
</Link>
|
</Link>
|
||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
Loading…
Reference in a new issue