1 | /* $Id: SUPHardenedVerify-win.h 52406 2014-08-19 03:03:28Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox Support Library/Driver - Hardened Verification, Windows.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2014 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 ___win_SUPHardenedVerify_win_h
|
---|
28 | #define ___win_SUPHardenedVerify_win_h
|
---|
29 |
|
---|
30 | #include <iprt/types.h>
|
---|
31 | #include <iprt/crypto/x509.h>
|
---|
32 | #ifndef SUP_CERTIFICATES_ONLY
|
---|
33 | # ifdef RT_OS_WINDOWS
|
---|
34 | # include <iprt/ldr.h>
|
---|
35 | # endif
|
---|
36 | #endif
|
---|
37 |
|
---|
38 |
|
---|
39 | RT_C_DECLS_BEGIN
|
---|
40 |
|
---|
41 | #ifndef SUP_CERTIFICATES_ONLY
|
---|
42 | # ifdef RT_OS_WINDOWS
|
---|
43 | DECLHIDDEN(int) supHardenedWinInitImageVerifier(PRTERRINFO pErrInfo);
|
---|
44 | DECLHIDDEN(void) supHardenedWinTermImageVerifier(void);
|
---|
45 | DECLHIDDEN(void) supR3HardenedWinVerifyCacheScheduleImports(RTLDRMOD hLdrMod, PCRTUTF16 pwszName);
|
---|
46 | DECLHIDDEN(void) supR3HardenedWinVerifyCachePreload(PCRTUTF16 pwszName);
|
---|
47 |
|
---|
48 |
|
---|
49 | typedef enum SUPHARDNTVPKIND
|
---|
50 | {
|
---|
51 | SUPHARDNTVPKIND_VERIFY_ONLY = 1,
|
---|
52 | SUPHARDNTVPKIND_CHILD_PURIFICATION,
|
---|
53 | SUPHARDNTVPKIND_SELF_PURIFICATION,
|
---|
54 | SUPHARDNTVPKIND_32BIT_HACK = 0x7fffffff
|
---|
55 | } SUPHARDNTVPKIND;
|
---|
56 | DECLHIDDEN(int) supHardenedWinVerifyProcess(HANDLE hProcess, HANDLE hThread, SUPHARDNTVPKIND enmKind, PRTERRINFO pErrInfo);
|
---|
57 |
|
---|
58 | DECLHIDDEN(bool) supHardViUniStrPathStartsWithUniStr(UNICODE_STRING const *pUniStrLeft,
|
---|
59 | UNICODE_STRING const *pUniStrRight, bool fCheckSlash);
|
---|
60 | DECLHIDDEN(bool) supHardViUtf16PathStartsWithEx(PCRTUTF16 pwszLeft, uint32_t cwcLeft,
|
---|
61 | PCRTUTF16 pwszRight, uint32_t cwcRight, bool fCheckSlash);
|
---|
62 | DECLHIDDEN(bool) supHardViIsAppPatchDir(PCRTUTF16 pwszPath, uint32_t cwcName);
|
---|
63 |
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * SUP image verifier loader reader instance.
|
---|
67 | */
|
---|
68 | typedef struct SUPHNTVIRDR
|
---|
69 | {
|
---|
70 | /** The core reader structure. */
|
---|
71 | RTLDRREADER Core;
|
---|
72 | /** The file handle . */
|
---|
73 | HANDLE hFile;
|
---|
74 | /** Current file offset. */
|
---|
75 | RTFOFF off;
|
---|
76 | /** The file size. */
|
---|
77 | RTFOFF cbFile;
|
---|
78 | /** Flags for the verification callback, SUPHNTVI_F_XXX. */
|
---|
79 | uint32_t fFlags;
|
---|
80 | /** The executable timstamp in second since unix epoch. */
|
---|
81 | uint64_t uTimestamp;
|
---|
82 | /** Log name. */
|
---|
83 | char szFilename[1];
|
---|
84 | } SUPHNTVIRDR;
|
---|
85 | /** Pointer to an SUP image verifier loader reader instance. */
|
---|
86 | typedef SUPHNTVIRDR *PSUPHNTVIRDR;
|
---|
87 | DECLHIDDEN(int) supHardNtViRdrCreate(HANDLE hFile, PCRTUTF16 pwszName, uint32_t fFlags, PSUPHNTVIRDR *ppNtViRdr);
|
---|
88 | DECLHIDDEN(bool) supHardenedWinIsWinVerifyTrustCallable(void);
|
---|
89 | DECLHIDDEN(int) supHardenedWinVerifyImageTrust(HANDLE hFile, PCRTUTF16 pwszName, uint32_t fFlags, int rc,
|
---|
90 | bool *pfWinVerifyTrust, PRTERRINFO pErrInfo);
|
---|
91 | DECLHIDDEN(int) supHardenedWinVerifyImageByHandle(HANDLE hFile, PCRTUTF16 pwszName, uint32_t fFlags,
|
---|
92 | bool *pfWinVerifyTrust, PRTERRINFO pErrInfo);
|
---|
93 | DECLHIDDEN(int) supHardenedWinVerifyImageByHandleNoName(HANDLE hFile, uint32_t fFlags, PRTERRINFO pErrInfo);
|
---|
94 | DECLHIDDEN(int) supHardenedWinVerifyImageByLdrMod(RTLDRMOD hLdrMod, PCRTUTF16 pwszName, PSUPHNTVIRDR pNtViRdr,
|
---|
95 | bool *pfWinVerifyTrust, PRTERRINFO pErrInfo);
|
---|
96 | /** @name SUPHNTVI_F_XXX - Flags for supHardenedWinVerifyImageByHandle.
|
---|
97 | * @{ */
|
---|
98 | /** The signing certificate must be the same as the one the VirtualBox build
|
---|
99 | * was signed with. */
|
---|
100 | # define SUPHNTVI_F_REQUIRE_BUILD_CERT RT_BIT(0)
|
---|
101 | /** Require kernel code signing level. */
|
---|
102 | # define SUPHNTVI_F_REQUIRE_KERNEL_CODE_SIGNING RT_BIT(1)
|
---|
103 | /** Require the image to force the memory mapper to do signature checking. */
|
---|
104 | # define SUPHNTVI_F_REQUIRE_SIGNATURE_ENFORCEMENT RT_BIT(2)
|
---|
105 | /** Whether to allow image verification by catalog file. */
|
---|
106 | # define SUPHNTVI_F_ALLOW_CAT_FILE_VERIFICATION RT_BIT(3)
|
---|
107 | /** The file owner must be TrustedInstaller on Vista+. */
|
---|
108 | # define SUPHNTVI_F_TRUSTED_INSTALLER_OWNER RT_BIT(4)
|
---|
109 | /** Resource image, could be any bitness. */
|
---|
110 | # define SUPHNTVI_F_RESOURCE_IMAGE RT_BIT(30)
|
---|
111 | /** Raw-mode context image, always 32-bit. */
|
---|
112 | # define SUPHNTVI_F_RC_IMAGE RT_BIT(31)
|
---|
113 | /** @} */
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Loader cache entry.
|
---|
117 | *
|
---|
118 | * This is for avoiding loading and signature checking a file multiple times,
|
---|
119 | * due to multiple passes thru the process validation code (and syscall import
|
---|
120 | * code of NTDLL).
|
---|
121 | */
|
---|
122 | typedef struct SUPHNTLDRCACHEENTRY
|
---|
123 | {
|
---|
124 | /** The file name (from g_apszSupNtVpAllowedDlls or
|
---|
125 | * g_apszSupNtVpAllowedVmExes). */
|
---|
126 | const char *pszName;
|
---|
127 | /** Load module associated with the image during content verfication. */
|
---|
128 | RTLDRMOD hLdrMod;
|
---|
129 | /** The file reader. */
|
---|
130 | PSUPHNTVIRDR pNtViRdr;
|
---|
131 | /** The module file handle, if we've opened it.
|
---|
132 | * (pNtviRdr does not close the file handle on destruction.) */
|
---|
133 | HANDLE hFile;
|
---|
134 | /** Bits buffer. */
|
---|
135 | uint8_t *pbBits;
|
---|
136 | /** Set if verified. */
|
---|
137 | bool fVerified;
|
---|
138 | } SUPHNTLDRCACHEENTRY;
|
---|
139 | /** Pointer to a loader cache entry. */
|
---|
140 | typedef SUPHNTLDRCACHEENTRY *PSUPHNTLDRCACHEENTRY;
|
---|
141 | DECLHIDDEN(int) supHardNtLdrCacheOpen(const char *pszName, PSUPHNTLDRCACHEENTRY *ppEntry);
|
---|
142 | DECLHIDDEN(int) supHardNtLdrCacheEntryVerify(PSUPHNTLDRCACHEENTRY pEntry, PCRTUTF16 pwszName, PRTERRINFO pErrInfo);
|
---|
143 | DECLHIDDEN(int) supHardNtLdrCacheEntryAllocBits(PSUPHNTLDRCACHEENTRY pEntry, uint8_t **ppbBits, PRTERRINFO pErrInfo);
|
---|
144 |
|
---|
145 |
|
---|
146 | /** Which directory under the system root to get. */
|
---|
147 | typedef enum SUPHARDNTSYSROOTDIR
|
---|
148 | {
|
---|
149 | kSupHardNtSysRootDir_System32 = 0,
|
---|
150 | kSupHardNtSysRootDir_WinSxS,
|
---|
151 | } SUPHARDNTSYSROOTDIR;
|
---|
152 |
|
---|
153 | DECLHIDDEN(int) supHardNtGetSystemRootDir(void *pvBuf, uint32_t cbBuf, SUPHARDNTSYSROOTDIR enmDir, PRTERRINFO pErrInfo);
|
---|
154 |
|
---|
155 | # ifndef SUPHNTVI_NO_NT_STUFF
|
---|
156 |
|
---|
157 | /** Typical system root directory buffer. */
|
---|
158 | typedef struct SUPSYSROOTDIRBUF
|
---|
159 | {
|
---|
160 | UNICODE_STRING UniStr;
|
---|
161 | WCHAR awcBuffer[260];
|
---|
162 | } SUPSYSROOTDIRBUF;
|
---|
163 | extern SUPSYSROOTDIRBUF g_System32NtPath;
|
---|
164 | extern SUPSYSROOTDIRBUF g_WinSxSNtPath;
|
---|
165 | #ifdef IN_RING3
|
---|
166 | extern SUPSYSROOTDIRBUF g_ProgramFilesNtPath;
|
---|
167 | extern SUPSYSROOTDIRBUF g_CommonFilesNtPath;
|
---|
168 | # if ARCH_BITS == 64
|
---|
169 | extern SUPSYSROOTDIRBUF g_ProgramFilesX86NtPath;
|
---|
170 | extern SUPSYSROOTDIRBUF g_CommonFilesX86NtPath;
|
---|
171 | # endif
|
---|
172 | #endif
|
---|
173 | extern SUPSYSROOTDIRBUF g_SupLibHardenedExeNtPath;
|
---|
174 | extern uint32_t g_offSupLibHardenedExeNtName;
|
---|
175 |
|
---|
176 | # ifdef IN_RING0
|
---|
177 | /** Pointer to NtQueryVirtualMemory. */
|
---|
178 | typedef NTSTATUS (NTAPI *PFNNTQUERYVIRTUALMEMORY)(HANDLE, void const *, MEMORY_INFORMATION_CLASS, PVOID, SIZE_T, PSIZE_T);
|
---|
179 | extern PFNNTQUERYVIRTUALMEMORY g_pfnNtQueryVirtualMemory;
|
---|
180 | # endif
|
---|
181 |
|
---|
182 | # endif /* SUPHNTVI_NO_NT_STUFF */
|
---|
183 |
|
---|
184 | /** Creates a combined NT version number for simple comparisons. */
|
---|
185 | #define SUP_MAKE_NT_VER_COMBINED(a_uMajor, a_uMinor, a_uBuild, a_uSpMajor, a_uSpMinor) \
|
---|
186 | ( ((uint32_t)((a_uMajor) & UINT32_C(0xf)) << 28) \
|
---|
187 | | ((uint32_t)((a_uMinor) & UINT32_C(0xf)) << 24) \
|
---|
188 | | ((uint32_t)((a_uBuild) & UINT32_C(0xffff)) << 8) \
|
---|
189 | | ((uint32_t)((a_uSpMajor) & UINT32_C(0xf)) << 4) \
|
---|
190 | | RT_MIN((uint32_t)(a_uSpMinor), UINT32_C(0xf)) )
|
---|
191 | /** Simple version of SUP_MAKE_NT_VER_COMBINED. */
|
---|
192 | #define SUP_MAKE_NT_VER_SIMPLE(a_uMajor, a_uMinor) SUP_MAKE_NT_VER_COMBINED(a_uMajor, a_uMinor, 0, 0, 0)
|
---|
193 | extern uint32_t g_uNtVerCombined;
|
---|
194 |
|
---|
195 | /** Combined NT version number for XP. */
|
---|
196 | #define SUP_NT_VER_XP SUP_MAKE_NT_VER_SIMPLE(5,1)
|
---|
197 | /** Combined NT version number for Windows server 2003 & XP64. */
|
---|
198 | #define SUP_NT_VER_W2K3 SUP_MAKE_NT_VER_SIMPLE(5,2)
|
---|
199 | /** Combined NT version number for Vista. */
|
---|
200 | #define SUP_NT_VER_VISTA SUP_MAKE_NT_VER_SIMPLE(6,0)
|
---|
201 | /** Combined NT version number for Windows 7. */
|
---|
202 | #define SUP_NT_VER_W70 SUP_MAKE_NT_VER_SIMPLE(6,1)
|
---|
203 | /** Combined NT version number for Windows 8.0. */
|
---|
204 | #define SUP_NT_VER_W80 SUP_MAKE_NT_VER_SIMPLE(6,2)
|
---|
205 | /** Combined NT version number for Windows 8.1. */
|
---|
206 | #define SUP_NT_VER_W81 SUP_MAKE_NT_VER_SIMPLE(6,3)
|
---|
207 |
|
---|
208 | # endif
|
---|
209 |
|
---|
210 | # ifndef IN_SUP_HARDENED_R3
|
---|
211 | # include <iprt/mem.h>
|
---|
212 | # include <iprt/string.h>
|
---|
213 |
|
---|
214 | # define suplibHardenedAllocZ RTMemAllocZ
|
---|
215 | # define suplibHardenedReAlloc RTMemRealloc
|
---|
216 | # define suplibHardenedFree RTMemFree
|
---|
217 | # define suplibHardenedMemComp memcmp
|
---|
218 | # define suplibHardenedMemCopy memcpy
|
---|
219 | # define suplibHardenedMemSet memset
|
---|
220 | # define suplibHardenedStrCopy strcpy
|
---|
221 | # define suplibHardenedStrLen strlen
|
---|
222 | # define suplibHardenedStrCat strcat
|
---|
223 | # define suplibHardenedStrCmp strcmp
|
---|
224 | # define suplibHardenedStrNCmp strncmp
|
---|
225 | # else /* IN_SUP_HARDENED_R3 */
|
---|
226 | # include <iprt/mem.h>
|
---|
227 | #if 0
|
---|
228 | # define memcmp suplibHardenedMemComp
|
---|
229 | # define memcpy suplibHardenedMemCopy
|
---|
230 | # define memset suplibHardenedMemSet
|
---|
231 | # define strcpy suplibHardenedStrCopy
|
---|
232 | # define strlen suplibHardenedStrLen
|
---|
233 | # define strcat suplibHardenedStrCat
|
---|
234 | # define strcmp suplibHardenedStrCmp
|
---|
235 | # define strncmp suplibHardenedStrNCmp
|
---|
236 | #endif
|
---|
237 | DECLHIDDEN(void *) suplibHardenedAllocZ(size_t cb);
|
---|
238 | DECLHIDDEN(void *) suplibHardenedReAlloc(void *pvOld, size_t cbNew);
|
---|
239 | DECLHIDDEN(void) suplibHardenedFree(void *pv);
|
---|
240 | # endif /* IN_SUP_HARDENED_R3 */
|
---|
241 |
|
---|
242 | #endif /* SUP_CERTIFICATES_ONLY */
|
---|
243 |
|
---|
244 | RT_C_DECLS_END
|
---|
245 |
|
---|
246 | #endif
|
---|
247 |
|
---|