7zip/C/DllSecur.c

100 lines
2.3 KiB
C
Raw Permalink Normal View History

2021-12-27 01:00:00 +01:00
/* DllSecur.c -- DLL loading security
2024-05-14 02:00:00 +02:00
2023-12-03 : Igor Pavlov : Public domain */
2021-12-27 01:00:00 +01:00
#include "Precomp.h"
#ifdef _WIN32
2023-06-21 02:00:00 +02:00
#include "7zWindows.h"
2021-12-27 01:00:00 +01:00
#include "DllSecur.h"
#ifndef UNDER_CE
2024-05-14 02:00:00 +02:00
Z7_DIAGNOSTIC_IGNORE_CAST_FUNCTION
2022-07-16 02:00:00 +02:00
2021-12-27 01:00:00 +01:00
typedef BOOL (WINAPI *Func_SetDefaultDllDirectories)(DWORD DirectoryFlags);
#define MY_LOAD_LIBRARY_SEARCH_USER_DIRS 0x400
#define MY_LOAD_LIBRARY_SEARCH_SYSTEM32 0x800
2023-06-21 02:00:00 +02:00
#define DELIM "\0"
2021-12-27 01:00:00 +01:00
static const char * const g_Dlls =
2023-06-21 02:00:00 +02:00
"userenv"
DELIM "setupapi"
DELIM "apphelp"
DELIM "propsys"
DELIM "dwmapi"
DELIM "cryptbase"
DELIM "oleacc"
DELIM "clbcatq"
DELIM "version"
2021-12-27 01:00:00 +01:00
#ifndef _CONSOLE
2023-06-21 02:00:00 +02:00
DELIM "uxtheme"
2021-12-27 01:00:00 +01:00
#endif
2023-06-21 02:00:00 +02:00
DELIM;
2021-12-27 01:00:00 +01:00
#endif
2023-06-21 02:00:00 +02:00
#ifdef __clang__
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
#if defined (_MSC_VER) && _MSC_VER >= 1900
// sysinfoapi.h: kit10: GetVersion was declared deprecated
#pragma warning(disable : 4996)
#endif
#define IF_NON_VISTA_SET_DLL_DIRS_AND_RETURN \
if ((UInt16)GetVersion() != 6) { \
const \
Func_SetDefaultDllDirectories setDllDirs = \
2024-05-14 02:00:00 +02:00
(Func_SetDefaultDllDirectories) Z7_CAST_FUNC_C GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), \
2023-06-21 02:00:00 +02:00
"SetDefaultDllDirectories"); \
if (setDllDirs) if (setDllDirs(MY_LOAD_LIBRARY_SEARCH_SYSTEM32 | MY_LOAD_LIBRARY_SEARCH_USER_DIRS)) return; }
2021-12-27 01:00:00 +01:00
2023-06-21 02:00:00 +02:00
void My_SetDefaultDllDirectories(void)
2021-12-27 01:00:00 +01:00
{
#ifndef UNDER_CE
2023-06-21 02:00:00 +02:00
IF_NON_VISTA_SET_DLL_DIRS_AND_RETURN
2021-12-27 01:00:00 +01:00
#endif
}
2023-06-21 02:00:00 +02:00
void LoadSecurityDlls(void)
2021-12-27 01:00:00 +01:00
{
#ifndef UNDER_CE
2023-06-21 02:00:00 +02:00
// at Vista (ver 6.0) : CoCreateInstance(CLSID_ShellLink, ...) doesn't work after SetDefaultDllDirectories() : Check it ???
IF_NON_VISTA_SET_DLL_DIRS_AND_RETURN
2021-12-27 01:00:00 +01:00
{
2023-06-21 02:00:00 +02:00
wchar_t buf[MAX_PATH + 100];
2021-12-27 01:00:00 +01:00
const char *dll;
2023-06-21 02:00:00 +02:00
unsigned pos = GetSystemDirectoryW(buf, MAX_PATH + 2);
if (pos == 0 || pos > MAX_PATH)
return;
2021-12-27 01:00:00 +01:00
if (buf[pos - 1] != '\\')
buf[pos++] = '\\';
2023-06-21 02:00:00 +02:00
for (dll = g_Dlls; *dll != 0;)
2021-12-27 01:00:00 +01:00
{
2023-06-21 02:00:00 +02:00
wchar_t *dest = &buf[pos];
2021-12-27 01:00:00 +01:00
for (;;)
{
2023-06-21 02:00:00 +02:00
const char c = *dll++;
2021-12-27 01:00:00 +01:00
if (c == 0)
break;
2023-06-21 02:00:00 +02:00
*dest++ = (Byte)c;
2021-12-27 01:00:00 +01:00
}
2023-06-21 02:00:00 +02:00
dest[0] = '.';
dest[1] = 'd';
dest[2] = 'l';
dest[3] = 'l';
dest[4] = 0;
// lstrcatW(buf, L".dll");
2021-12-27 01:00:00 +01:00
LoadLibraryExW(buf, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
}
}
#endif
}
2023-06-21 02:00:00 +02:00
#endif // _WIN32