1 | /* $Id: SUPHardenedVerify-win.h 85121 2020-07-08 19:33:26Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox Support Library/Driver - Hardened Verification, Windows.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2020 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 | #ifndef VBOX_INCLUDED_SRC_Support_win_SUPHardenedVerify_win_h
|
---|
28 | #define VBOX_INCLUDED_SRC_Support_win_SUPHardenedVerify_win_h
|
---|
29 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
30 | # pragma once
|
---|
31 | #endif
|
---|
32 |
|
---|
33 | #include <iprt/types.h>
|
---|
34 | #include <iprt/crypto/x509.h>
|
---|
35 | #ifndef SUP_CERTIFICATES_ONLY
|
---|
36 | # ifdef RT_OS_WINDOWS
|
---|
37 | # include <iprt/ldr.h>
|
---|
38 | # endif
|
---|
39 | #endif
|
---|
40 |
|
---|
41 |
|
---|
42 | RT_C_DECLS_BEGIN
|
---|
43 |
|
---|
44 | #ifndef SUP_CERTIFICATES_ONLY
|
---|
45 | # ifdef RT_OS_WINDOWS
|
---|
46 | DECL_HIDDEN_NOTHROW(int) supHardenedWinInitImageVerifier(PRTERRINFO pErrInfo);
|
---|
47 | DECL_HIDDEN_NOTHROW(void) supHardenedWinTermImageVerifier(void);
|
---|
48 | DECL_HIDDEN_NOTHROW(void) supR3HardenedWinVerifyCacheScheduleImports(RTLDRMOD hLdrMod, PCRTUTF16 pwszName);
|
---|
49 | DECL_HIDDEN_NOTHROW(void) supR3HardenedWinVerifyCachePreload(PCRTUTF16 pwszName);
|
---|
50 |
|
---|
51 |
|
---|
52 | typedef enum SUPHARDNTVPKIND
|
---|
53 | {
|
---|
54 | SUPHARDNTVPKIND_VERIFY_ONLY = 1,
|
---|
55 | SUPHARDNTVPKIND_CHILD_PURIFICATION,
|
---|
56 | SUPHARDNTVPKIND_SELF_PURIFICATION,
|
---|
57 | SUPHARDNTVPKIND_SELF_PURIFICATION_LIMITED,
|
---|
58 | SUPHARDNTVPKIND_32BIT_HACK = 0x7fffffff
|
---|
59 | } SUPHARDNTVPKIND;
|
---|
60 | /** @name SUPHARDNTVP_F_XXX - Flags for supHardenedWinVerifyProcess
|
---|
61 | * @{ */
|
---|
62 | /** Replace unwanted executable memory allocations with a new one that's filled
|
---|
63 | * with a safe read-write copy (default is just to free it).
|
---|
64 | *
|
---|
65 | * This is one way we attempt to work around buggy protection software that
|
---|
66 | * either result in host BSOD or VBox application malfunction. Here the current
|
---|
67 | * shit list:
|
---|
68 | * - Trend Micro's data protection software includes a buggy driver called
|
---|
69 | * sakfile.sys that has been observed crashing accessing user memory that we
|
---|
70 | * probably freed. I'd love to report this to Trend Micro, but unfortunately
|
---|
71 | * they doesn't advertise (or have?) an email address for reporting security
|
---|
72 | * vulnerabilities in the their software. Having wasted time looking and not
|
---|
73 | * very sorry for having to disclosing the bug here.
|
---|
74 | * - Maybe one more.
|
---|
75 | */
|
---|
76 | #define SUPHARDNTVP_F_EXEC_ALLOC_REPLACE_WITH_RW RT_BIT_32(0)
|
---|
77 | /** @} */
|
---|
78 | DECL_HIDDEN_NOTHROW(int) supHardenedWinVerifyProcess(HANDLE hProcess, HANDLE hThread, SUPHARDNTVPKIND enmKind,
|
---|
79 | uint32_t fFlags, uint32_t *pcFixes, PRTERRINFO pErrInfo);
|
---|
80 | DECL_HIDDEN_NOTHROW(int) supHardNtVpThread(HANDLE hProcess, HANDLE hThread, PRTERRINFO pErrInfo);
|
---|
81 | DECL_HIDDEN_NOTHROW(int) supHardNtVpDebugger(HANDLE hProcess, PRTERRINFO pErrInfo);
|
---|
82 |
|
---|
83 | DECL_HIDDEN_NOTHROW(bool) supHardViUtf16PathIsEqualEx(PCRTUTF16 pawcLeft, size_t cwcLeft, const char *pszRight);
|
---|
84 | DECL_HIDDEN_NOTHROW(bool) supHardViUniStrPathStartsWithUniStr(UNICODE_STRING const *pUniStrLeft,
|
---|
85 | UNICODE_STRING const *pUniStrRight, bool fCheckSlash);
|
---|
86 | DECL_HIDDEN_NOTHROW(bool) supHardViUtf16PathStartsWithEx(PCRTUTF16 pwszLeft, uint32_t cwcLeft,
|
---|
87 | PCRTUTF16 pwszRight, uint32_t cwcRight, bool fCheckSlash);
|
---|
88 | DECL_HIDDEN_NOTHROW(bool) supHardViIsAppPatchDir(PCRTUTF16 pwszPath, uint32_t cwcName);
|
---|
89 |
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * SUP image verifier loader reader instance.
|
---|
93 | */
|
---|
94 | typedef struct SUPHNTVIRDR
|
---|
95 | {
|
---|
96 | /** The core reader structure. */
|
---|
97 | RTLDRREADER Core;
|
---|
98 | /** The file handle. */
|
---|
99 | HANDLE hFile;
|
---|
100 | /** Handle to event sempahore in case we're force to deal with asynchronous
|
---|
101 | * I/O. */
|
---|
102 | HANDLE hEvent;
|
---|
103 | /** Current file offset. */
|
---|
104 | RTFOFF off;
|
---|
105 | /** The file size. */
|
---|
106 | uint64_t cbFile;
|
---|
107 | /** Flags for the verification callback, SUPHNTVI_F_XXX. */
|
---|
108 | uint32_t fFlags;
|
---|
109 | /** The executable timstamp in second since unix epoch. */
|
---|
110 | uint64_t uTimestamp;
|
---|
111 | /** Log name. */
|
---|
112 | char szFilename[1];
|
---|
113 | } SUPHNTVIRDR;
|
---|
114 | /** Pointer to an SUP image verifier loader reader instance. */
|
---|
115 | typedef SUPHNTVIRDR *PSUPHNTVIRDR;
|
---|
116 | DECL_HIDDEN_NOTHROW(int) supHardNtViRdrCreate(HANDLE hFile, PCRTUTF16 pwszName, uint32_t fFlags, PSUPHNTVIRDR *ppNtViRdr);
|
---|
117 | DECL_HIDDEN_NOTHROW(bool) supHardenedWinIsWinVerifyTrustCallable(void);
|
---|
118 | DECL_HIDDEN_NOTHROW(int) supHardenedWinVerifyImageTrust(HANDLE hFile, PCRTUTF16 pwszName, uint32_t fFlags, int rc,
|
---|
119 | bool *pfWinVerifyTrust, PRTERRINFO pErrInfo);
|
---|
120 | DECL_HIDDEN_NOTHROW(int) supHardenedWinVerifyImageByHandle(HANDLE hFile, PCRTUTF16 pwszName, uint32_t fFlags,
|
---|
121 | bool fAvoidWinVerifyTrust, bool *pfWinVerifyTrust, PRTERRINFO pErrInfo);
|
---|
122 | DECL_HIDDEN_NOTHROW(int) supHardenedWinVerifyImageByHandleNoName(HANDLE hFile, uint32_t fFlags, PRTERRINFO pErrInfo);
|
---|
123 | DECL_HIDDEN_NOTHROW(int) supHardenedWinVerifyImageByLdrMod(RTLDRMOD hLdrMod, PCRTUTF16 pwszName, PSUPHNTVIRDR pNtViRdr,
|
---|
124 | bool fAvoidWinVerifyTrust, bool *pfWinVerifyTrust, PRTERRINFO pErrInfo);
|
---|
125 | /** @name SUPHNTVI_F_XXX - Flags for supHardenedWinVerifyImageByHandle.
|
---|
126 | * @{ */
|
---|
127 | /** The signing certificate must be the same as the one the VirtualBox build
|
---|
128 | * was signed with. */
|
---|
129 | # define SUPHNTVI_F_REQUIRE_BUILD_CERT RT_BIT(0)
|
---|
130 | /** Require kernel code signing level. */
|
---|
131 | # define SUPHNTVI_F_REQUIRE_KERNEL_CODE_SIGNING RT_BIT(1)
|
---|
132 | /** Require the image to force the memory mapper to do signature checking. */
|
---|
133 | # define SUPHNTVI_F_REQUIRE_SIGNATURE_ENFORCEMENT RT_BIT(2)
|
---|
134 | /** Whether to allow image verification by catalog file. */
|
---|
135 | # define SUPHNTVI_F_ALLOW_CAT_FILE_VERIFICATION RT_BIT(3)
|
---|
136 | /** The file owner must be TrustedInstaller on Vista+. */
|
---|
137 | # define SUPHNTVI_F_TRUSTED_INSTALLER_OWNER RT_BIT(4)
|
---|
138 | /** Ignore the image architecture (otherwise it must match the verification
|
---|
139 | * code). Used with resource images and such. */
|
---|
140 | # define SUPHNTVI_F_IGNORE_ARCHITECTURE RT_BIT(30)
|
---|
141 | /** Raw-mode context image, always 32-bit. */
|
---|
142 | # define SUPHNTVI_F_RC_IMAGE RT_BIT(31)
|
---|
143 | /** @} */
|
---|
144 |
|
---|
145 | /* Array in SUPHardenedVerifyImage-win.cpp */
|
---|
146 | extern const RTSTRTUPLE g_aSupNtViBlacklistedDlls[];
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * Loader cache entry.
|
---|
150 | *
|
---|
151 | * This is for avoiding loading and signature checking a file multiple times,
|
---|
152 | * due to multiple passes thru the process validation code (and syscall import
|
---|
153 | * code of NTDLL).
|
---|
154 | */
|
---|
155 | typedef struct SUPHNTLDRCACHEENTRY
|
---|
156 | {
|
---|
157 | /** The file name (from g_apszSupNtVpAllowedDlls or
|
---|
158 | * g_apszSupNtVpAllowedVmExes). */
|
---|
159 | const char *pszName;
|
---|
160 | /** Load module associated with the image during content verfication. */
|
---|
161 | RTLDRMOD hLdrMod;
|
---|
162 | /** The file reader. */
|
---|
163 | PSUPHNTVIRDR pNtViRdr;
|
---|
164 | /** The module file handle, if we've opened it.
|
---|
165 | * (pNtviRdr does not close the file handle on destruction.) */
|
---|
166 | HANDLE hFile;
|
---|
167 | /** Bits buffer. */
|
---|
168 | uint8_t *pbBits;
|
---|
169 | /** Set if verified. */
|
---|
170 | bool fVerified;
|
---|
171 | /** Whether we've got valid cacheable image bits. */
|
---|
172 | bool fValidBits;
|
---|
173 | /** The image base address. */
|
---|
174 | uintptr_t uImageBase;
|
---|
175 | } SUPHNTLDRCACHEENTRY;
|
---|
176 | /** Pointer to a loader cache entry. */
|
---|
177 | typedef SUPHNTLDRCACHEENTRY *PSUPHNTLDRCACHEENTRY;
|
---|
178 | DECL_HIDDEN_NOTHROW(int) supHardNtLdrCacheOpen(const char *pszName, PSUPHNTLDRCACHEENTRY *ppEntry, PRTERRINFO pErrInfo);
|
---|
179 | DECL_HIDDEN_NOTHROW(int) supHardNtLdrCacheEntryVerify(PSUPHNTLDRCACHEENTRY pEntry, PCRTUTF16 pwszName, PRTERRINFO pErrInfo);
|
---|
180 | DECL_HIDDEN_NOTHROW(int) supHardNtLdrCacheEntryGetBits(PSUPHNTLDRCACHEENTRY pEntry, uint8_t **ppbBits, RTLDRADDR uBaseAddress,
|
---|
181 | PFNRTLDRIMPORT pfnGetImport, void *pvUser, PRTERRINFO pErrInfo);
|
---|
182 |
|
---|
183 |
|
---|
184 | /** Which directory under the system root to get. */
|
---|
185 | typedef enum SUPHARDNTSYSROOTDIR
|
---|
186 | {
|
---|
187 | kSupHardNtSysRootDir_System32 = 0,
|
---|
188 | kSupHardNtSysRootDir_WinSxS,
|
---|
189 | } SUPHARDNTSYSROOTDIR;
|
---|
190 |
|
---|
191 | DECL_HIDDEN_NOTHROW(int) supHardNtGetSystemRootDir(void *pvBuf, uint32_t cbBuf, SUPHARDNTSYSROOTDIR enmDir, PRTERRINFO pErrInfo);
|
---|
192 |
|
---|
193 | # ifndef SUPHNTVI_NO_NT_STUFF
|
---|
194 |
|
---|
195 | /** Typical system root directory buffer. */
|
---|
196 | typedef struct SUPSYSROOTDIRBUF
|
---|
197 | {
|
---|
198 | UNICODE_STRING UniStr;
|
---|
199 | WCHAR awcBuffer[260];
|
---|
200 | } SUPSYSROOTDIRBUF;
|
---|
201 | extern SUPSYSROOTDIRBUF g_System32NtPath;
|
---|
202 | extern SUPSYSROOTDIRBUF g_WinSxSNtPath;
|
---|
203 | #if defined(IN_RING3) && !defined(VBOX_PERMIT_EVEN_MORE)
|
---|
204 | extern SUPSYSROOTDIRBUF g_ProgramFilesNtPath;
|
---|
205 | extern SUPSYSROOTDIRBUF g_CommonFilesNtPath;
|
---|
206 | # if ARCH_BITS == 64
|
---|
207 | extern SUPSYSROOTDIRBUF g_ProgramFilesX86NtPath;
|
---|
208 | extern SUPSYSROOTDIRBUF g_CommonFilesX86NtPath;
|
---|
209 | # endif
|
---|
210 | #endif /* IN_RING3 && !VBOX_PERMIT_EVEN_MORE */
|
---|
211 | extern SUPSYSROOTDIRBUF g_SupLibHardenedExeNtPath;
|
---|
212 | extern SUPSYSROOTDIRBUF g_SupLibHardenedAppBinNtPath;
|
---|
213 |
|
---|
214 | # ifdef IN_RING0
|
---|
215 | /** Pointer to NtQueryVirtualMemory. */
|
---|
216 | typedef DECLCALLBACKPTR_EX(NTSTATUS, NTAPI, PFNNTQUERYVIRTUALMEMORY,(HANDLE, void const *, MEMORY_INFORMATION_CLASS,
|
---|
217 | PVOID, SIZE_T, PSIZE_T));
|
---|
218 | extern PFNNTQUERYVIRTUALMEMORY g_pfnNtQueryVirtualMemory;
|
---|
219 | # endif
|
---|
220 |
|
---|
221 | # endif /* SUPHNTVI_NO_NT_STUFF */
|
---|
222 |
|
---|
223 | /** Creates a combined NT version number for simple comparisons. */
|
---|
224 | #define SUP_MAKE_NT_VER_COMBINED(a_uMajor, a_uMinor, a_uBuild, a_uSpMajor, a_uSpMinor) \
|
---|
225 | ( ((uint32_t)((a_uMajor) & UINT32_C(0xf)) << 28) \
|
---|
226 | | ((uint32_t)((a_uMinor) & UINT32_C(0xf)) << 24) \
|
---|
227 | | ((uint32_t)((a_uBuild) & UINT32_C(0xffff)) << 8) \
|
---|
228 | | ((uint32_t)((a_uSpMajor) & UINT32_C(0xf)) << 4) \
|
---|
229 | | (uint32_t)((a_uSpMinor) & UINT32_C(0xf)) )
|
---|
230 | /** Simple version of SUP_MAKE_NT_VER_COMBINED. */
|
---|
231 | #define SUP_MAKE_NT_VER_SIMPLE(a_uMajor, a_uMinor) SUP_MAKE_NT_VER_COMBINED(a_uMajor, a_uMinor, 0, 0, 0)
|
---|
232 | extern uint32_t g_uNtVerCombined;
|
---|
233 |
|
---|
234 | /** @name NT version constants for less-than checks.
|
---|
235 | * @{ */
|
---|
236 | /** Combined NT version number for XP. */
|
---|
237 | #define SUP_NT_VER_XP SUP_MAKE_NT_VER_SIMPLE(5,1)
|
---|
238 | /** Combined NT version number for Windows server 2003 & XP64. */
|
---|
239 | #define SUP_NT_VER_W2K3 SUP_MAKE_NT_VER_SIMPLE(5,2)
|
---|
240 | /** Combined NT version number for Vista. */
|
---|
241 | #define SUP_NT_VER_VISTA SUP_MAKE_NT_VER_SIMPLE(6,0)
|
---|
242 | /** Combined NT version number for Vista with SP1. */
|
---|
243 | #define SUP_NT_VER_VISTA_SP1 SUP_MAKE_NT_VER_COMBINED(6,0,6001,1,0)
|
---|
244 | /** Combined NT version number for Windows 7. */
|
---|
245 | #define SUP_NT_VER_W70 SUP_MAKE_NT_VER_SIMPLE(6,1)
|
---|
246 | /** Combined NT version number for Windows 8.0. */
|
---|
247 | #define SUP_NT_VER_W80 SUP_MAKE_NT_VER_SIMPLE(6,2)
|
---|
248 | /** Combined NT version number for Windows 8.1. */
|
---|
249 | #define SUP_NT_VER_W81 SUP_MAKE_NT_VER_SIMPLE(6,3)
|
---|
250 | /** @} */
|
---|
251 |
|
---|
252 | # endif
|
---|
253 |
|
---|
254 | # ifndef IN_SUP_HARDENED_R3
|
---|
255 | # include <iprt/mem.h>
|
---|
256 | # include <iprt/string.h>
|
---|
257 |
|
---|
258 | # define suplibHardenedMemComp memcmp
|
---|
259 | # define suplibHardenedMemCopy memcpy
|
---|
260 | # define suplibHardenedMemSet memset
|
---|
261 | # define suplibHardenedStrCopy strcpy
|
---|
262 | # define suplibHardenedStrLen strlen
|
---|
263 | # define suplibHardenedStrCat strcat
|
---|
264 | # define suplibHardenedStrCmp strcmp
|
---|
265 | # define suplibHardenedStrNCmp strncmp
|
---|
266 | # else /* IN_SUP_HARDENED_R3 */
|
---|
267 | # include <iprt/mem.h>
|
---|
268 | # if 0
|
---|
269 | # define memcmp suplibHardenedMemComp
|
---|
270 | # define memcpy suplibHardenedMemCopy
|
---|
271 | # define memset suplibHardenedMemSet
|
---|
272 | # define strcpy suplibHardenedStrCopy
|
---|
273 | # define strlen suplibHardenedStrLen
|
---|
274 | # define strcat suplibHardenedStrCat
|
---|
275 | # define strcmp suplibHardenedStrCmp
|
---|
276 | # define strncmp suplibHardenedStrNCmp
|
---|
277 | # endif
|
---|
278 | # endif /* IN_SUP_HARDENED_R3 */
|
---|
279 |
|
---|
280 | #endif /* SUP_CERTIFICATES_ONLY */
|
---|
281 |
|
---|
282 | RT_C_DECLS_END
|
---|
283 |
|
---|
284 | #endif /* !VBOX_INCLUDED_SRC_Support_win_SUPHardenedVerify_win_h */
|
---|
285 |
|
---|