VirtualBox

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

Last change on this file since 78351 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette