• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* DllSecur.c -- DLL loading security
2 2023-04-02 : Igor Pavlov : Public domain */
3 
4 #include "Precomp.h"
5 
6 #ifdef _WIN32
7 
8 #include "7zWindows.h"
9 
10 #include "DllSecur.h"
11 
12 #ifndef UNDER_CE
13 
14 #if (defined(__GNUC__) && (__GNUC__ >= 8)) || defined(__clang__)
15   // #pragma GCC diagnostic ignored "-Wcast-function-type"
16 #endif
17 
18 #if defined(__clang__) || defined(__GNUC__)
19 typedef void (*Z7_voidFunction)(void);
20 #define MY_CAST_FUNC (Z7_voidFunction)
21 #elif defined(_MSC_VER) && _MSC_VER > 1920
22 #define MY_CAST_FUNC  (void *)
23 // #pragma warning(disable : 4191) // 'type cast': unsafe conversion from 'FARPROC' to 'void (__cdecl *)()'
24 #else
25 #define MY_CAST_FUNC
26 #endif
27 
28 typedef BOOL (WINAPI *Func_SetDefaultDllDirectories)(DWORD DirectoryFlags);
29 
30 #define MY_LOAD_LIBRARY_SEARCH_USER_DIRS 0x400
31 #define MY_LOAD_LIBRARY_SEARCH_SYSTEM32  0x800
32 
33 #define DELIM "\0"
34 
35 static const char * const g_Dlls =
36          "userenv"
37   DELIM  "setupapi"
38   DELIM  "apphelp"
39   DELIM  "propsys"
40   DELIM  "dwmapi"
41   DELIM  "cryptbase"
42   DELIM  "oleacc"
43   DELIM  "clbcatq"
44   DELIM  "version"
45   #ifndef _CONSOLE
46   DELIM  "uxtheme"
47   #endif
48   DELIM;
49 
50 #endif
51 
52 #ifdef __clang__
53   #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
54 #endif
55 #if defined (_MSC_VER) && _MSC_VER >= 1900
56 // sysinfoapi.h: kit10: GetVersion was declared deprecated
57 #pragma warning(disable : 4996)
58 #endif
59 
60 #define IF_NON_VISTA_SET_DLL_DIRS_AND_RETURN \
61     if ((UInt16)GetVersion() != 6) { \
62       const \
63        Func_SetDefaultDllDirectories setDllDirs = \
64       (Func_SetDefaultDllDirectories) MY_CAST_FUNC GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), \
65            "SetDefaultDllDirectories"); \
66       if (setDllDirs) if (setDllDirs(MY_LOAD_LIBRARY_SEARCH_SYSTEM32 | MY_LOAD_LIBRARY_SEARCH_USER_DIRS)) return; }
67 
My_SetDefaultDllDirectories(void)68 void My_SetDefaultDllDirectories(void)
69 {
70   #ifndef UNDER_CE
71   IF_NON_VISTA_SET_DLL_DIRS_AND_RETURN
72   #endif
73 }
74 
75 
LoadSecurityDlls(void)76 void LoadSecurityDlls(void)
77 {
78   #ifndef UNDER_CE
79   // at Vista (ver 6.0) : CoCreateInstance(CLSID_ShellLink, ...) doesn't work after SetDefaultDllDirectories() : Check it ???
80   IF_NON_VISTA_SET_DLL_DIRS_AND_RETURN
81   {
82     wchar_t buf[MAX_PATH + 100];
83     const char *dll;
84     unsigned pos = GetSystemDirectoryW(buf, MAX_PATH + 2);
85     if (pos == 0 || pos > MAX_PATH)
86       return;
87     if (buf[pos - 1] != '\\')
88       buf[pos++] = '\\';
89     for (dll = g_Dlls; *dll != 0;)
90     {
91       wchar_t *dest = &buf[pos];
92       for (;;)
93       {
94         const char c = *dll++;
95         if (c == 0)
96           break;
97         *dest++ = (Byte)c;
98       }
99       dest[0] = '.';
100       dest[1] = 'd';
101       dest[2] = 'l';
102       dest[3] = 'l';
103       dest[4] = 0;
104       // lstrcatW(buf, L".dll");
105       LoadLibraryExW(buf, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
106     }
107   }
108   #endif
109 }
110 
111 #endif // _WIN32
112