diff options
author | Igor Pavlov <87184205+ip7z@users.noreply.github.com> | 2021-12-27 00:00:00 +0000 |
---|---|---|
committer | Igor Pavlov <87184205+ip7z@users.noreply.github.com> | 2022-03-18 15:35:13 +0500 |
commit | f19f813537c7aea1c20749c914e756b54a9c3cf5 (patch) | |
tree | 816ba62ca7c0fa19f2eb46d9e9d6f7dd7c3a744d /C/DllSecur.c | |
parent | 98e06a519b63b81986abe76d28887f6984a7732b (diff) | |
download | 7zip-21.07.tar.gz 7zip-21.07.tar.bz2 7zip-21.07.zip |
'21.07'21.07
Diffstat (limited to 'C/DllSecur.c')
-rw-r--r-- | C/DllSecur.c | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/C/DllSecur.c b/C/DllSecur.c new file mode 100644 index 0000000..d81508c --- /dev/null +++ b/C/DllSecur.c | |||
@@ -0,0 +1,110 @@ | |||
1 | /* DllSecur.c -- DLL loading security | ||
2 | 2021-12-25 : Igor Pavlov : Public domain */ | ||
3 | |||
4 | #include "Precomp.h" | ||
5 | |||
6 | #ifdef _WIN32 | ||
7 | |||
8 | #include <Windows.h> | ||
9 | |||
10 | #include "DllSecur.h" | ||
11 | |||
12 | #ifndef UNDER_CE | ||
13 | |||
14 | typedef BOOL (WINAPI *Func_SetDefaultDllDirectories)(DWORD DirectoryFlags); | ||
15 | |||
16 | #define MY_LOAD_LIBRARY_SEARCH_USER_DIRS 0x400 | ||
17 | #define MY_LOAD_LIBRARY_SEARCH_SYSTEM32 0x800 | ||
18 | |||
19 | static const char * const g_Dlls = | ||
20 | #ifndef _CONSOLE | ||
21 | "UXTHEME\0" | ||
22 | #endif | ||
23 | "USERENV\0" | ||
24 | "SETUPAPI\0" | ||
25 | "APPHELP\0" | ||
26 | "PROPSYS\0" | ||
27 | "DWMAPI\0" | ||
28 | "CRYPTBASE\0" | ||
29 | "OLEACC\0" | ||
30 | "CLBCATQ\0" | ||
31 | "VERSION\0" | ||
32 | ; | ||
33 | |||
34 | #endif | ||
35 | |||
36 | // #define MY_CAST_FUNC (void(*)()) | ||
37 | #define MY_CAST_FUNC | ||
38 | |||
39 | void My_SetDefaultDllDirectories() | ||
40 | { | ||
41 | #ifndef UNDER_CE | ||
42 | |||
43 | OSVERSIONINFO vi; | ||
44 | vi.dwOSVersionInfoSize = sizeof(vi); | ||
45 | if (!GetVersionEx(&vi) || vi.dwMajorVersion != 6 || vi.dwMinorVersion != 0) | ||
46 | { | ||
47 | Func_SetDefaultDllDirectories setDllDirs = (Func_SetDefaultDllDirectories) | ||
48 | MY_CAST_FUNC GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "SetDefaultDllDirectories"); | ||
49 | if (setDllDirs) | ||
50 | if (setDllDirs(MY_LOAD_LIBRARY_SEARCH_SYSTEM32 | MY_LOAD_LIBRARY_SEARCH_USER_DIRS)) | ||
51 | return; | ||
52 | } | ||
53 | |||
54 | #endif | ||
55 | } | ||
56 | |||
57 | |||
58 | void LoadSecurityDlls() | ||
59 | { | ||
60 | #ifndef UNDER_CE | ||
61 | |||
62 | wchar_t buf[MAX_PATH + 100]; | ||
63 | |||
64 | { | ||
65 | // at Vista (ver 6.0) : CoCreateInstance(CLSID_ShellLink, ...) doesn't work after SetDefaultDllDirectories() : Check it ??? | ||
66 | OSVERSIONINFO vi; | ||
67 | vi.dwOSVersionInfoSize = sizeof(vi); | ||
68 | if (!GetVersionEx(&vi) || vi.dwMajorVersion != 6 || vi.dwMinorVersion != 0) | ||
69 | { | ||
70 | Func_SetDefaultDllDirectories setDllDirs = (Func_SetDefaultDllDirectories) | ||
71 | MY_CAST_FUNC GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "SetDefaultDllDirectories"); | ||
72 | if (setDllDirs) | ||
73 | if (setDllDirs(MY_LOAD_LIBRARY_SEARCH_SYSTEM32 | MY_LOAD_LIBRARY_SEARCH_USER_DIRS)) | ||
74 | return; | ||
75 | } | ||
76 | } | ||
77 | |||
78 | { | ||
79 | unsigned len = GetSystemDirectoryW(buf, MAX_PATH + 2); | ||
80 | if (len == 0 || len > MAX_PATH) | ||
81 | return; | ||
82 | } | ||
83 | { | ||
84 | const char *dll; | ||
85 | unsigned pos = (unsigned)lstrlenW(buf); | ||
86 | |||
87 | if (buf[pos - 1] != '\\') | ||
88 | buf[pos++] = '\\'; | ||
89 | |||
90 | for (dll = g_Dlls; dll[0] != 0;) | ||
91 | { | ||
92 | unsigned k = 0; | ||
93 | for (;;) | ||
94 | { | ||
95 | char c = *dll++; | ||
96 | buf[pos + k] = (Byte)c; | ||
97 | k++; | ||
98 | if (c == 0) | ||
99 | break; | ||
100 | } | ||
101 | |||
102 | lstrcatW(buf, L".dll"); | ||
103 | LoadLibraryExW(buf, NULL, LOAD_WITH_ALTERED_SEARCH_PATH); | ||
104 | } | ||
105 | } | ||
106 | |||
107 | #endif | ||
108 | } | ||
109 | |||
110 | #endif | ||