Use a safer way to load system libraries.

This commit is contained in:
John Preston 2020-07-02 21:01:25 +04:00
parent 822c0434e8
commit 7f55fd2cad
5 changed files with 58 additions and 58 deletions

View file

@ -7,26 +7,43 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/ */
#include "platform/win/windows_dlls.h" #include "platform/win/windows_dlls.h"
#include "base/platform/win/base_windows_safe_library.h"
#include <VersionHelpers.h> #include <VersionHelpers.h>
#include <QtCore/QSysInfo> #include <QtCore/QSysInfo>
namespace Platform { namespace Platform {
namespace Dlls { namespace Dlls {
f_SetDllDirectory SetDllDirectory; using base::Platform::SafeLoadLibrary;
using base::Platform::LoadMethod;
HINSTANCE LibKernel32;
void init() { void init() {
static bool inited = false; static bool inited = false;
if (inited) return; if (inited) return;
inited = true; inited = true;
LibKernel32 = LoadLibrary(L"KERNEL32.DLL"); // Remove the current directory from the DLL search order.
load(LibKernel32, "SetDllDirectoryW", SetDllDirectory); ::SetDllDirectory(L"");
if (SetDllDirectory) {
// Remove the current directory from the DLL search order. const auto list = {
SetDllDirectory(L""); u"dbghelp.dll"_q,
u"dbgcore.dll"_q,
u"propsys.dll"_q,
u"winsta.dll"_q,
u"textinputframework.dll"_q,
u"uxtheme.dll"_q,
u"igdumdim32.dll"_q,
u"amdhdl32.dll"_q,
u"wtsapi32.dll"_q,
u"propsys.dll"_q,
u"combase.dll"_q,
u"dwmapi.dll"_q,
u"rstrtmgr.dll"_q,
u"psapi.dll"_q,
};
for (const auto &lib : list) {
SafeLoadLibrary(lib);
} }
} }
@ -53,59 +70,50 @@ f_RmShutdown RmShutdown;
f_RmEndSession RmEndSession; f_RmEndSession RmEndSession;
f_GetProcessMemoryInfo GetProcessMemoryInfo; f_GetProcessMemoryInfo GetProcessMemoryInfo;
HINSTANCE LibUxTheme;
HINSTANCE LibShell32;
HINSTANCE LibWtsApi32;
HINSTANCE LibPropSys;
HINSTANCE LibComBase;
HINSTANCE LibDwmApi;
HINSTANCE LibRstrtMgr;
HINSTANCE LibPsApi;
void start() { void start() {
init(); init();
LibShell32 = LoadLibrary(L"SHELL32.DLL"); const auto LibShell32 = SafeLoadLibrary(u"shell32.dll"_q);
load(LibShell32, "SHAssocEnumHandlers", SHAssocEnumHandlers); LoadMethod(LibShell32, "SHAssocEnumHandlers", SHAssocEnumHandlers);
load(LibShell32, "SHCreateItemFromParsingName", SHCreateItemFromParsingName); LoadMethod(LibShell32, "SHCreateItemFromParsingName", SHCreateItemFromParsingName);
load(LibShell32, "SHOpenWithDialog", SHOpenWithDialog); LoadMethod(LibShell32, "SHOpenWithDialog", SHOpenWithDialog);
load(LibShell32, "OpenAs_RunDLLW", OpenAs_RunDLL); LoadMethod(LibShell32, "OpenAs_RunDLLW", OpenAs_RunDLL);
load(LibShell32, "SHQueryUserNotificationState", SHQueryUserNotificationState); LoadMethod(LibShell32, "SHQueryUserNotificationState", SHQueryUserNotificationState);
load(LibShell32, "SHChangeNotify", SHChangeNotify); LoadMethod(LibShell32, "SHChangeNotify", SHChangeNotify);
load(LibShell32, "SetCurrentProcessExplicitAppUserModelID", SetCurrentProcessExplicitAppUserModelID); LoadMethod(LibShell32, "SetCurrentProcessExplicitAppUserModelID", SetCurrentProcessExplicitAppUserModelID);
LibUxTheme = LoadLibrary(L"UXTHEME.DLL"); const auto LibUxTheme = SafeLoadLibrary(u"uxtheme.dll"_q);
load(LibUxTheme, "SetWindowTheme", SetWindowTheme); LoadMethod(LibUxTheme, "SetWindowTheme", SetWindowTheme);
if (IsWindowsVistaOrGreater()) { if (IsWindowsVistaOrGreater()) {
LibWtsApi32 = LoadLibrary(L"WTSAPI32.DLL"); const auto LibWtsApi32 = SafeLoadLibrary(u"wtsapi32.dll"_q);
load(LibWtsApi32, "WTSRegisterSessionNotification", WTSRegisterSessionNotification); LoadMethod(LibWtsApi32, "WTSRegisterSessionNotification", WTSRegisterSessionNotification);
load(LibWtsApi32, "WTSUnRegisterSessionNotification", WTSUnRegisterSessionNotification); LoadMethod(LibWtsApi32, "WTSUnRegisterSessionNotification", WTSUnRegisterSessionNotification);
LibPropSys = LoadLibrary(L"PROPSYS.DLL"); const auto LibPropSys = SafeLoadLibrary(u"propsys.dll"_q);
load(LibPropSys, "PropVariantToString", PropVariantToString); LoadMethod(LibPropSys, "PropVariantToString", PropVariantToString);
load(LibPropSys, "PSStringFromPropertyKey", PSStringFromPropertyKey); LoadMethod(LibPropSys, "PSStringFromPropertyKey", PSStringFromPropertyKey);
if (IsWindows8OrGreater()) { if (IsWindows8OrGreater()) {
LibComBase = LoadLibrary(L"COMBASE.DLL"); const auto LibComBase = SafeLoadLibrary(u"combase.dll"_q);
load(LibComBase, "RoGetActivationFactory", RoGetActivationFactory); LoadMethod(LibComBase, "RoGetActivationFactory", RoGetActivationFactory);
load(LibComBase, "WindowsCreateStringReference", WindowsCreateStringReference); LoadMethod(LibComBase, "WindowsCreateStringReference", WindowsCreateStringReference);
load(LibComBase, "WindowsDeleteString", WindowsDeleteString); LoadMethod(LibComBase, "WindowsDeleteString", WindowsDeleteString);
} }
LibDwmApi = LoadLibrary(L"DWMAPI.DLL"); const auto LibDwmApi = SafeLoadLibrary(u"dwmapi.dll"_q);
load(LibDwmApi, "DwmIsCompositionEnabled", DwmIsCompositionEnabled); LoadMethod(LibDwmApi, "DwmIsCompositionEnabled", DwmIsCompositionEnabled);
LibRstrtMgr = LoadLibrary(L"RSTRTMGR.DLL"); const auto LibRstrtMgr = SafeLoadLibrary(u"rstrtmgr.dll"_q);
load(LibRstrtMgr, "RmStartSession", RmStartSession); LoadMethod(LibRstrtMgr, "RmStartSession", RmStartSession);
load(LibRstrtMgr, "RmRegisterResources", RmRegisterResources); LoadMethod(LibRstrtMgr, "RmRegisterResources", RmRegisterResources);
load(LibRstrtMgr, "RmGetList", RmGetList); LoadMethod(LibRstrtMgr, "RmGetList", RmGetList);
load(LibRstrtMgr, "RmShutdown", RmShutdown); LoadMethod(LibRstrtMgr, "RmShutdown", RmShutdown);
load(LibRstrtMgr, "RmEndSession", RmEndSession); LoadMethod(LibRstrtMgr, "RmEndSession", RmEndSession);
} }
LibPsApi = LoadLibrary(L"PSAPI.DLL"); const auto LibPsApi = SafeLoadLibrary(u"psapi.dll"_q);
load(LibPsApi, "GetProcessMemoryInfo", GetProcessMemoryInfo); LoadMethod(LibPsApi, "GetProcessMemoryInfo", GetProcessMemoryInfo);
} }
} // namespace Dlls } // namespace Dlls

View file

@ -30,14 +30,6 @@ extern f_SetDllDirectory SetDllDirectory;
void start(); void start();
template <typename Function>
bool load(HINSTANCE library, LPCSTR name, Function &func) {
if (!library) return false;
func = reinterpret_cast<Function>(GetProcAddress(library, name));
return (func != nullptr);
}
// UXTHEME.DLL // UXTHEME.DLL
using f_SetWindowTheme = HRESULT(FAR STDAPICALLTYPE*)( using f_SetWindowTheme = HRESULT(FAR STDAPICALLTYPE*)(
HWND hWnd, HWND hWnd,

@ -1 +1 @@
Subproject commit 3017da83c15e5e27244ab66526fea8cc3bddb7cf Subproject commit 01ca681ab3aecda8c372b1bb77999f3b7b7a52c6

@ -1 +1 @@
Subproject commit 262b3eb33593c638c46a30423c3774d9fe8171fc Subproject commit 09918f0133ce0070000b9ca2a798057e6ad8bd5d

2
cmake

@ -1 +1 @@
Subproject commit 6b36a649dc0061138cf60bac65d9a40a5c9faea3 Subproject commit 9346d5f89510c4f9e340a0e26a1f8c2244e7de29