VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/Support/win/SUPR3HardenedNoCrt-win.cpp@ 62490

Last change on this file since 62490 was 62490, checked in by vboxsync, 8 years ago

(C) 2016

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.4 KB
Line 
1/* $Id: SUPR3HardenedNoCrt-win.cpp 62490 2016-07-22 18:41:49Z vboxsync $ */
2/** @file
3 * VirtualBox Support Library - Hardened main(), windows bits.
4 */
5
6/*
7 * Copyright (C) 2006-2016 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
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <iprt/nt/nt-and-windows.h>
32#include <AccCtrl.h>
33#include <AclApi.h>
34#ifndef PROCESS_SET_LIMITED_INFORMATION
35# define PROCESS_SET_LIMITED_INFORMATION 0x2000
36#endif
37
38#include <VBox/sup.h>
39#include <VBox/err.h>
40#include <iprt/assert.h>
41#include <iprt/ctype.h>
42#include <iprt/heap.h>
43#include <iprt/string.h>
44#include <iprt/initterm.h>
45#include <iprt/param.h>
46#include <iprt/path.h>
47#include <iprt/mem.h>
48
49#include "SUPLibInternal.h"
50#include "win/SUPHardenedVerify-win.h"
51
52
53/*
54 * assert.cpp
55 */
56
57RTDATADECL(char) g_szRTAssertMsg1[1024];
58RTDATADECL(char) g_szRTAssertMsg2[4096];
59RTDATADECL(const char * volatile) g_pszRTAssertExpr;
60RTDATADECL(const char * volatile) g_pszRTAssertFile;
61RTDATADECL(uint32_t volatile) g_u32RTAssertLine;
62RTDATADECL(const char * volatile) g_pszRTAssertFunction;
63
64RTDECL(bool) RTAssertMayPanic(void)
65{
66 return true;
67}
68
69
70RTDECL(void) RTAssertMsg1(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
71{
72 /*
73 * Fill in the globals.
74 */
75 g_pszRTAssertExpr = pszExpr;
76 g_pszRTAssertFile = pszFile;
77 g_pszRTAssertFunction = pszFunction;
78 g_u32RTAssertLine = uLine;
79 RTStrPrintf(g_szRTAssertMsg1, sizeof(g_szRTAssertMsg1),
80 "\n!!Assertion Failed!!\n"
81 "Expression: %s\n"
82 "Location : %s(%d) %s\n",
83 pszExpr, pszFile, uLine, pszFunction);
84}
85
86
87RTDECL(void) RTAssertMsg2V(const char *pszFormat, va_list va)
88{
89 RTStrPrintfV(g_szRTAssertMsg2, sizeof(g_szRTAssertMsg2), pszFormat, va);
90 if (g_enmSupR3HardenedMainState < SUPR3HARDENEDMAINSTATE_CALLED_TRUSTED_MAIN)
91 supR3HardenedFatalMsg(g_pszRTAssertExpr, kSupInitOp_Misc, VERR_INTERNAL_ERROR,
92 "%s%s", g_szRTAssertMsg1, g_szRTAssertMsg2);
93 else
94 supR3HardenedError(VERR_INTERNAL_ERROR, false/*fFatal*/, "%s%s", g_szRTAssertMsg1, g_szRTAssertMsg2);
95}
96
97
98/*
99 * Memory allocator.
100 */
101
102/** The handle of the heap we're using. */
103static HANDLE g_hSupR3HardenedHeap = NULL;
104/** Number of heaps used during early process init. */
105static uint32_t g_cSupR3HardenedEarlyHeaps = 0;
106/** Early process init heaps. */
107static struct
108{
109 /** The heap handle. */
110 RTHEAPSIMPLE hHeap;
111 /** The heap block pointer. */
112 void *pvBlock;
113 /** The size of the heap block. */
114 size_t cbBlock;
115 /** Number of active allocations on this heap. */
116 size_t cAllocations;
117} g_aSupR3HardenedEarlyHeaps[8];
118
119
120static uint32_t supR3HardenedEarlyFind(void *pv)
121{
122 uint32_t iHeap = g_cSupR3HardenedEarlyHeaps;
123 while (iHeap-- > 0)
124 if ((uintptr_t)pv - (uintptr_t)g_aSupR3HardenedEarlyHeaps[iHeap].pvBlock < g_aSupR3HardenedEarlyHeaps[iHeap].cbBlock)
125 return iHeap;
126 return UINT32_MAX;
127}
128
129
130static void supR3HardenedEarlyCompact(void)
131{
132 uint32_t iHeap = g_cSupR3HardenedEarlyHeaps;
133 while (iHeap-- > 0)
134 if (g_aSupR3HardenedEarlyHeaps[iHeap].cAllocations == 0)
135 {
136 PVOID pvMem = g_aSupR3HardenedEarlyHeaps[iHeap].pvBlock;
137 SIZE_T cbMem = g_aSupR3HardenedEarlyHeaps[iHeap].cbBlock;
138 if (iHeap + 1 < g_cSupR3HardenedEarlyHeaps)
139 g_aSupR3HardenedEarlyHeaps[iHeap] = g_aSupR3HardenedEarlyHeaps[g_cSupR3HardenedEarlyHeaps - 1];
140 g_cSupR3HardenedEarlyHeaps--;
141
142 NTSTATUS rcNt = NtFreeVirtualMemory(NtCurrentProcess(), &pvMem, &cbMem, MEM_RELEASE);
143 Assert(NT_SUCCESS(rcNt));
144 SUP_DPRINTF(("supR3HardenedEarlyCompact: Removed heap %#u (%#p LB %#zx)\n", iHeap, pvMem, cbMem));
145 }
146}
147
148
149static void *supR3HardenedEarlyAlloc(size_t cb, bool fZero)
150{
151 /*
152 * Try allocate on existing heaps.
153 */
154 void *pv;
155 uint32_t iHeap = 0;
156 while (iHeap < g_cSupR3HardenedEarlyHeaps)
157 {
158 if (fZero)
159 pv = RTHeapSimpleAllocZ(g_aSupR3HardenedEarlyHeaps[iHeap].hHeap, cb, 0);
160 else
161 pv = RTHeapSimpleAlloc(g_aSupR3HardenedEarlyHeaps[iHeap].hHeap, cb, 0);
162 if (pv)
163 {
164 g_aSupR3HardenedEarlyHeaps[iHeap].cAllocations++;
165#ifdef SUPR3HARDENED_EARLY_HEAP_TRACE
166 SUP_DPRINTF(("Early heap: %p LB %#zx - alloc\n", pv, cb));
167#endif
168 return pv;
169 }
170 iHeap++;
171 }
172
173 /*
174 * Add another heap.
175 */
176 if (iHeap == RT_ELEMENTS(g_aSupR3HardenedEarlyHeaps))
177 supR3HardenedFatal("Early heap table is full (cb=%#zx).\n", cb);
178 SIZE_T cbBlock = iHeap == 0 ? _1M : g_aSupR3HardenedEarlyHeaps[iHeap - 1].cbBlock * 2;
179 while (cbBlock <= cb * 2)
180 cbBlock *= 2;
181
182 PVOID pvBlock = NULL;
183 NTSTATUS rcNt = NtAllocateVirtualMemory(NtCurrentProcess(), &pvBlock, 0 /*ZeroBits*/, &cbBlock, MEM_COMMIT, PAGE_READWRITE);
184 if (!NT_SUCCESS(rcNt))
185 supR3HardenedFatal("NtAllocateVirtualMemory(,,,%#zx,,) failed: rcNt=%#x\n", cbBlock, rcNt);
186 SUP_DPRINTF(("New simple heap: #%u %p LB %#zx (for %zu allocation)\n", iHeap, pvBlock, cbBlock, cb));
187
188 RTHEAPSIMPLE hHeap;
189 int rc = RTHeapSimpleInit(&hHeap, pvBlock, cbBlock);
190 if (RT_FAILURE(rc))
191 supR3HardenedFatal("RTHeapSimpleInit(,%p,%#zx) failed: rc=%#x\n", pvBlock, cbBlock, rc);
192
193 if (fZero)
194 pv = RTHeapSimpleAllocZ(hHeap, cb, 0);
195 else
196 pv = RTHeapSimpleAlloc(hHeap, cb, 0);
197 if (!pv)
198 supR3HardenedFatal("RTHeapSimpleAlloc[Z] failed allocating %#zx bytes on a %#zu heap.\n", cb, cbBlock);
199
200 g_aSupR3HardenedEarlyHeaps[iHeap].pvBlock = pvBlock;
201 g_aSupR3HardenedEarlyHeaps[iHeap].cbBlock = cbBlock;
202 g_aSupR3HardenedEarlyHeaps[iHeap].cAllocations = 1;
203 g_aSupR3HardenedEarlyHeaps[iHeap].hHeap = hHeap;
204
205 Assert(g_cSupR3HardenedEarlyHeaps == iHeap);
206 g_cSupR3HardenedEarlyHeaps = iHeap + 1;
207
208#ifdef SUPR3HARDENED_EARLY_HEAP_TRACE
209 SUP_DPRINTF(("Early heap: %p LB %#zx - alloc\n", pv, cb));
210#endif
211 return pv;
212}
213
214
215/**
216 * Lazy heap initialization function.
217 *
218 * @returns Heap handle.
219 */
220static HANDLE supR3HardenedHeapInit(void)
221{
222 Assert(g_enmSupR3HardenedMainState >= SUPR3HARDENEDMAINSTATE_WIN_EP_CALLED);
223 HANDLE hHeap = RtlCreateHeap(HEAP_GROWABLE | HEAP_CLASS_PRIVATE, NULL /*HeapBase*/,
224 0 /*ReserveSize*/, 0 /*CommitSize*/, NULL /*Lock*/, NULL /*Parameters*/);
225 if (hHeap)
226 {
227 g_hSupR3HardenedHeap = hHeap;
228 return hHeap;
229 }
230
231 supR3HardenedFatal("RtlCreateHeap failed.\n");
232 return NULL;
233}
234
235
236/**
237 * Compacts the heaps before enter wait for parent/child.
238 */
239DECLHIDDEN(void) supR3HardenedWinCompactHeaps(void)
240{
241 if (g_hSupR3HardenedHeap)
242 RtlCompactHeap(g_hSupR3HardenedHeap, 0 /*dwFlags*/);
243 RtlCompactHeap(GetProcessHeap(), 0 /*dwFlags*/);
244 supR3HardenedEarlyCompact();
245}
246
247
248
249RTDECL(void *) RTMemTmpAllocTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
250{
251 return RTMemAllocTag(cb, pszTag);
252}
253
254
255RTDECL(void *) RTMemTmpAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
256{
257 return RTMemAllocZTag(cb, pszTag);
258}
259
260
261RTDECL(void) RTMemTmpFree(void *pv) RT_NO_THROW_DEF
262{
263 RTMemFree(pv);
264}
265
266
267RTDECL(void *) RTMemAllocTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
268{
269 HANDLE hHeap = g_hSupR3HardenedHeap;
270 if (!hHeap)
271 {
272 if ( g_fSupEarlyProcessInit
273 && g_enmSupR3HardenedMainState <= SUPR3HARDENEDMAINSTATE_WIN_EP_CALLED)
274 return supR3HardenedEarlyAlloc(cb, false /*fZero*/);
275 hHeap = supR3HardenedHeapInit();
276 }
277
278 void *pv = RtlAllocateHeap(hHeap, 0 /*fFlags*/, cb);
279 if (!pv)
280 supR3HardenedFatal("RtlAllocateHeap failed to allocate %zu bytes.\n", cb);
281 return pv;
282}
283
284
285RTDECL(void *) RTMemAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
286{
287 HANDLE hHeap = g_hSupR3HardenedHeap;
288 if (!hHeap)
289 {
290 if ( g_fSupEarlyProcessInit
291 && g_enmSupR3HardenedMainState <= SUPR3HARDENEDMAINSTATE_WIN_EP_CALLED)
292 return supR3HardenedEarlyAlloc(cb, true /*fZero*/);
293 hHeap = supR3HardenedHeapInit();
294 }
295
296 void *pv = RtlAllocateHeap(hHeap, HEAP_ZERO_MEMORY, cb);
297 if (!pv)
298 supR3HardenedFatal("RtlAllocateHeap failed to allocate %zu bytes.\n", cb);
299 return pv;
300}
301
302
303RTDECL(void *) RTMemAllocVarTag(size_t cbUnaligned, const char *pszTag) RT_NO_THROW_DEF
304{
305 size_t cbAligned;
306 if (cbUnaligned >= 16)
307 cbAligned = RT_ALIGN_Z(cbUnaligned, 16);
308 else
309 cbAligned = RT_ALIGN_Z(cbUnaligned, sizeof(void *));
310 return RTMemAllocTag(cbAligned, pszTag);
311}
312
313
314RTDECL(void *) RTMemAllocZVarTag(size_t cbUnaligned, const char *pszTag) RT_NO_THROW_DEF
315{
316 size_t cbAligned;
317 if (cbUnaligned >= 16)
318 cbAligned = RT_ALIGN_Z(cbUnaligned, 16);
319 else
320 cbAligned = RT_ALIGN_Z(cbUnaligned, sizeof(void *));
321 return RTMemAllocZTag(cbAligned, pszTag);
322}
323
324
325RTDECL(void *) RTMemReallocTag(void *pvOld, size_t cbNew, const char *pszTag) RT_NO_THROW_DEF
326{
327 if (!pvOld)
328 return RTMemAllocZTag(cbNew, pszTag);
329
330 void *pv;
331 if (g_fSupEarlyProcessInit)
332 {
333 uint32_t iHeap = supR3HardenedEarlyFind(pvOld);
334 if (iHeap != UINT32_MAX)
335 {
336#if 0 /* RTHeapSimpleRealloc is not implemented */
337 /* If this is before we can use a regular heap, we try resize
338 within the simple heap. (There are a lot of array growing in
339 the ASN.1 code.) */
340 if (g_enmSupR3HardenedMainState < SUPR3HARDENEDMAINSTATE_WIN_IMPORTS_RESOLVED)
341 {
342 pv = RTHeapSimpleRealloc(g_aSupR3HardenedEarlyHeaps[iHeap].hHeap, pvOld, cbNew, 0);
343 if (pv)
344 {
345# ifdef SUPR3HARDENED_EARLY_HEAP_TRACE
346 SUP_DPRINTF(("Early heap: %p LB %#zx, was %p - realloc\n", pvNew, cbNew, pvOld));
347# endif
348 return pv;
349 }
350 }
351#endif
352
353 /* Either we can't reallocate it on the same simple heap, or we're
354 past hardened main and wish to migrate everything over on the
355 real heap. */
356 size_t cbOld = RTHeapSimpleSize(g_aSupR3HardenedEarlyHeaps[iHeap].hHeap, pvOld);
357 pv = RTMemAllocTag(cbNew, pszTag);
358 if (pv)
359 {
360 memcpy(pv, pvOld, RT_MIN(cbOld, cbNew));
361 RTHeapSimpleFree(g_aSupR3HardenedEarlyHeaps[iHeap].hHeap, pvOld);
362 if (g_aSupR3HardenedEarlyHeaps[iHeap].cAllocations)
363 g_aSupR3HardenedEarlyHeaps[iHeap].cAllocations--;
364 if ( !g_aSupR3HardenedEarlyHeaps[iHeap].cAllocations
365 && g_enmSupR3HardenedMainState >= SUPR3HARDENEDMAINSTATE_WIN_IMPORTS_RESOLVED)
366 supR3HardenedEarlyCompact();
367 }
368# ifdef SUPR3HARDENED_EARLY_HEAP_TRACE
369 SUP_DPRINTF(("Early heap: %p LB %#zx, was %p %LB %#zx - realloc\n", pv, cbNew, pvOld, cbOld));
370# endif
371 return pv;
372 }
373 Assert(g_enmSupR3HardenedMainState >= SUPR3HARDENEDMAINSTATE_WIN_IMPORTS_RESOLVED);
374 }
375
376 /* Allocate from the regular heap. */
377 HANDLE hHeap = g_hSupR3HardenedHeap;
378 Assert(hHeap != NULL);
379 pv = RtlReAllocateHeap(hHeap, 0 /*dwFlags*/, pvOld, cbNew);
380 if (!pv)
381 supR3HardenedFatal("RtlReAllocateHeap failed to allocate %zu bytes.\n", cbNew);
382 return pv;
383}
384
385
386RTDECL(void) RTMemFree(void *pv) RT_NO_THROW_DEF
387{
388 if (pv)
389 {
390 if (g_fSupEarlyProcessInit)
391 {
392 uint32_t iHeap = supR3HardenedEarlyFind(pv);
393 if (iHeap != UINT32_MAX)
394 {
395#ifdef SUPR3HARDENED_EARLY_HEAP_TRACE
396 SUP_DPRINTF(("Early heap: %p - free\n", pv));
397#endif
398 RTHeapSimpleFree(g_aSupR3HardenedEarlyHeaps[iHeap].hHeap, pv);
399 if (g_aSupR3HardenedEarlyHeaps[iHeap].cAllocations)
400 g_aSupR3HardenedEarlyHeaps[iHeap].cAllocations--;
401 if ( !g_aSupR3HardenedEarlyHeaps[iHeap].cAllocations
402 && g_enmSupR3HardenedMainState >= SUPR3HARDENEDMAINSTATE_WIN_IMPORTS_RESOLVED)
403 supR3HardenedEarlyCompact();
404 return;
405 }
406 Assert(g_enmSupR3HardenedMainState >= SUPR3HARDENEDMAINSTATE_WIN_IMPORTS_RESOLVED);
407 }
408
409 HANDLE hHeap = g_hSupR3HardenedHeap;
410 Assert(hHeap != NULL);
411 RtlFreeHeap(hHeap, 0 /* dwFlags*/, pv);
412 }
413}
414
415
416/*
417 * Simplified version of RTMemWipeThoroughly that avoids dragging in the
418 * random number code.
419 */
420
421RTDECL(void) RTMemWipeThoroughly(void *pv, size_t cb, size_t cMinPasses) RT_NO_THROW_DEF
422{
423 size_t cPasses = RT_MIN(cMinPasses, 6);
424 static const uint32_t s_aPatterns[] = { 0x00, 0xaa, 0x55, 0xff, 0xf0, 0x0f, 0xcc, 0x3c, 0xc3 };
425 uint32_t iPattern = 0;
426 do
427 {
428 memset(pv, s_aPatterns[iPattern], cb);
429 iPattern = (iPattern + 1) % RT_ELEMENTS(s_aPatterns);
430 ASMMemoryFence();
431
432 memset(pv, s_aPatterns[iPattern], cb);
433 iPattern = (iPattern + 1) % RT_ELEMENTS(s_aPatterns);
434 ASMMemoryFence();
435
436 memset(pv, s_aPatterns[iPattern], cb);
437 iPattern = (iPattern + 1) % RT_ELEMENTS(s_aPatterns);
438 ASMMemoryFence();
439 } while (cPasses-- > 0);
440
441 memset(pv, 0xff, cb);
442 ASMMemoryFence();
443}
444
445
446
447/*
448 * path-win.cpp
449 */
450
451RTDECL(int) RTPathGetCurrent(char *pszPath, size_t cbPath)
452{
453 int rc;
454 if (g_enmSupR3HardenedMainState < SUPR3HARDENEDMAINSTATE_WIN_IMPORTS_RESOLVED)
455/** @todo Rainy day: improve this by checking the process parameter block
456 * (needs to be normalized). */
457 rc = RTStrCopy(pszPath, cbPath, "C:\\");
458 else
459 {
460 /*
461 * GetCurrentDirectory may in some cases omit the drive letter, according
462 * to MSDN, thus the GetFullPathName call.
463 */
464 RTUTF16 wszCurPath[RTPATH_MAX];
465 if (GetCurrentDirectoryW(RTPATH_MAX, wszCurPath))
466 {
467 RTUTF16 wszFullPath[RTPATH_MAX];
468 if (GetFullPathNameW(wszCurPath, RTPATH_MAX, wszFullPath, NULL))
469 rc = RTUtf16ToUtf8Ex(&wszFullPath[0], RTSTR_MAX, &pszPath, cbPath, NULL);
470 else
471 rc = RTErrConvertFromWin32(RtlGetLastWin32Error());
472 }
473 else
474 rc = RTErrConvertFromWin32(RtlGetLastWin32Error());
475 }
476 return rc;
477}
478
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use