VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/Support/win/SUPHardenedVerifyProcess-win.cpp@ 62688

Last change on this file since 62688 was 62677, checked in by vboxsync, 9 years ago

SUPHardNt: -Wall warnings.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 99.3 KB
Line 
1/* $Id: SUPHardenedVerifyProcess-win.cpp 62677 2016-07-29 12:39:44Z vboxsync $ */
2/** @file
3 * VirtualBox Support Library/Driver - Hardened Process Verification, Windows.
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#ifdef IN_RING0
32# define IPRT_NT_MAP_TO_ZW
33# include <iprt/nt/nt.h>
34# include <ntimage.h>
35#else
36# include <iprt/nt/nt-and-windows.h>
37#endif
38
39#include <VBox/sup.h>
40#include <VBox/err.h>
41#include <iprt/alloca.h>
42#include <iprt/ctype.h>
43#include <iprt/param.h>
44#include <iprt/string.h>
45#include <iprt/zero.h>
46
47#ifdef IN_RING0
48# include "SUPDrvInternal.h"
49#else
50# include "SUPLibInternal.h"
51#endif
52#include "win/SUPHardenedVerify-win.h"
53
54
55/*********************************************************************************************************************************
56* Structures and Typedefs *
57*********************************************************************************************************************************/
58/**
59 * Virtual address space region.
60 */
61typedef struct SUPHNTVPREGION
62{
63 /** The RVA of the region. */
64 uint32_t uRva;
65 /** The size of the region. */
66 uint32_t cb;
67 /** The protection of the region. */
68 uint32_t fProt;
69} SUPHNTVPREGION;
70/** Pointer to a virtual address space region. */
71typedef SUPHNTVPREGION *PSUPHNTVPREGION;
72
73/**
74 * Virtual address space image information.
75 */
76typedef struct SUPHNTVPIMAGE
77{
78 /** The base address of the image. */
79 uintptr_t uImageBase;
80 /** The size of the image mapping. */
81 uintptr_t cbImage;
82
83 /** The name from the allowed lists. */
84 const char *pszName;
85 /** Name structure for NtQueryVirtualMemory/MemorySectionName. */
86 struct
87 {
88 /** The full unicode name. */
89 UNICODE_STRING UniStr;
90 /** Buffer space. */
91 WCHAR awcBuffer[260];
92 } Name;
93
94 /** The number of mapping regions. */
95 uint32_t cRegions;
96 /** Mapping regions. */
97 SUPHNTVPREGION aRegions[16];
98
99 /** The image characteristics from the FileHeader. */
100 uint16_t fImageCharecteristics;
101 /** The DLL characteristics from the OptionalHeader. */
102 uint16_t fDllCharecteristics;
103
104 /** Set if this is the DLL. */
105 bool fDll;
106 /** Set if the image is NTDLL an the verficiation code needs to watch out for
107 * the NtCreateSection patch. */
108 bool fNtCreateSectionPatch;
109 /** Whether the API set schema hack needs to be applied when verifying memory
110 * content. The hack means that we only check if the 1st section is mapped. */
111 bool fApiSetSchemaOnlySection1;
112 /** This may be a 32-bit resource DLL. */
113 bool f32bitResourceDll;
114
115 /** Pointer to the loader cache entry for the image. */
116 PSUPHNTLDRCACHEENTRY pCacheEntry;
117#ifdef IN_RING0
118 /** In ring-0 we don't currently cache images, so put it here. */
119 SUPHNTLDRCACHEENTRY CacheEntry;
120#endif
121} SUPHNTVPIMAGE;
122/** Pointer to image info from the virtual address space scan. */
123typedef SUPHNTVPIMAGE *PSUPHNTVPIMAGE;
124
125/**
126 * Virtual address space scanning state.
127 */
128typedef struct SUPHNTVPSTATE
129{
130 /** Type of verification to perform. */
131 SUPHARDNTVPKIND enmKind;
132 /** Combination of SUPHARDNTVP_F_XXX. */
133 uint32_t fFlags;
134 /** The result. */
135 int rcResult;
136 /** Number of fixes we've done.
137 * Only applicable in the purification modes. */
138 uint32_t cFixes;
139 /** Number of images in aImages. */
140 uint32_t cImages;
141 /** The index of the last image we looked up. */
142 uint32_t iImageHint;
143 /** The process handle. */
144 HANDLE hProcess;
145 /** Images found in the process.
146 * The array is large enough to hold the executable, all allowed DLLs, and one
147 * more so we can get the image name of the first unwanted DLL. */
148 SUPHNTVPIMAGE aImages[1 + 6 + 1
149#ifdef VBOX_PERMIT_VERIFIER_DLL
150 + 1
151#endif
152#ifdef VBOX_PERMIT_MORE
153 + 5
154#endif
155#ifdef VBOX_PERMIT_VISUAL_STUDIO_PROFILING
156 + 16
157#endif
158 ];
159 /** Memory compare scratch buffer.*/
160 uint8_t abMemory[_4K];
161 /** File compare scratch buffer.*/
162 uint8_t abFile[_4K];
163 /** Section headers for use when comparing file and loaded image. */
164 IMAGE_SECTION_HEADER aSecHdrs[16];
165 /** Pointer to the error info. */
166 PRTERRINFO pErrInfo;
167} SUPHNTVPSTATE;
168/** Pointer to stat information of a virtual address space scan. */
169typedef SUPHNTVPSTATE *PSUPHNTVPSTATE;
170
171
172/*********************************************************************************************************************************
173* Global Variables *
174*********************************************************************************************************************************/
175/**
176 * System DLLs allowed to be loaded into the process.
177 * @remarks supHardNtVpCheckDlls assumes these are lower case.
178 */
179static const char *g_apszSupNtVpAllowedDlls[] =
180{
181 "ntdll.dll",
182 "kernel32.dll",
183 "kernelbase.dll",
184 "apphelp.dll",
185 "apisetschema.dll",
186#ifdef VBOX_PERMIT_VERIFIER_DLL
187 "verifier.dll",
188#endif
189#ifdef VBOX_PERMIT_MORE
190# define VBOX_PERMIT_MORE_FIRST_IDX 5
191 "sfc.dll",
192 "sfc_os.dll",
193 "user32.dll",
194 "acres.dll",
195 "acgenral.dll",
196#endif
197#ifdef VBOX_PERMIT_VISUAL_STUDIO_PROFILING
198 "psapi.dll",
199 "msvcrt.dll",
200 "advapi32.dll",
201 "sechost.dll",
202 "rpcrt4.dll",
203 "SamplingRuntime.dll",
204#endif
205};
206
207/**
208 * VBox executables allowed to start VMs.
209 * @remarks Remember to keep in sync with g_aSupInstallFiles in
210 * SUPR3HardenedVerify.cpp.
211 */
212static const char *g_apszSupNtVpAllowedVmExes[] =
213{
214 "VBoxHeadless.exe",
215 "VirtualBox.exe",
216 "VBoxSDL.exe",
217 "VBoxNetDHCP.exe",
218 "VBoxNetNAT.exe",
219 "VBoxVMMPreload.exe",
220
221 "tstMicro.exe",
222 "tstPDMAsyncCompletion.exe",
223 "tstPDMAsyncCompletionStress.exe",
224 "tstVMM.exe",
225 "tstVMREQ.exe",
226 "tstCFGM.exe",
227 "tstGIP-2.exe",
228 "tstIntNet-1.exe",
229 "tstMMHyperHeap.exe",
230 "tstRTR0ThreadPreemptionDriver.exe",
231 "tstRTR0MemUserKernelDriver.exe",
232 "tstRTR0SemMutexDriver.exe",
233 "tstRTR0TimerDriver.exe",
234 "tstSSM.exe",
235};
236
237/** Pointer to NtQueryVirtualMemory. Initialized by SUPDrv-win.cpp in
238 * ring-0, in ring-3 it's just a slightly confusing define. */
239#ifdef IN_RING0
240PFNNTQUERYVIRTUALMEMORY g_pfnNtQueryVirtualMemory = NULL;
241#else
242# define g_pfnNtQueryVirtualMemory NtQueryVirtualMemory
243#endif
244
245#ifdef IN_RING3
246/** The number of valid entries in the loader cache. */
247static uint32_t g_cSupNtVpLdrCacheEntries = 0;
248/** The loader cache entries. */
249static SUPHNTLDRCACHEENTRY g_aSupNtVpLdrCacheEntries[RT_ELEMENTS(g_apszSupNtVpAllowedDlls) + 1 + 3];
250#endif
251
252
253/**
254 * Fills in error information.
255 *
256 * @returns @a rc.
257 * @param pErrInfo Pointer to the extended error info structure.
258 * Can be NULL.
259 * @param rc The status to return.
260 * @param pszMsg The format string for the message.
261 * @param ... The arguments for the format string.
262 */
263static int supHardNtVpSetInfo1(PRTERRINFO pErrInfo, int rc, const char *pszMsg, ...)
264{
265 va_list va;
266#ifdef IN_RING3
267 va_start(va, pszMsg);
268 supR3HardenedError(rc, false /*fFatal*/, "%N\n", pszMsg, &va);
269 va_end(va);
270#endif
271
272 va_start(va, pszMsg);
273 RTErrInfoSetV(pErrInfo, rc, pszMsg, va);
274 va_end(va);
275
276 return rc;
277}
278
279
280/**
281 * Adds error information.
282 *
283 * @returns @a rc.
284 * @param pErrInfo Pointer to the extended error info structure
285 * which may contain some details already. Can be
286 * NULL.
287 * @param rc The status to return.
288 * @param pszMsg The format string for the message.
289 * @param ... The arguments for the format string.
290 */
291static int supHardNtVpAddInfo1(PRTERRINFO pErrInfo, int rc, const char *pszMsg, ...)
292{
293 va_list va;
294#ifdef IN_RING3
295 va_start(va, pszMsg);
296 if (pErrInfo && pErrInfo->pszMsg)
297 supR3HardenedError(rc, false /*fFatal*/, "%N - %s\n", pszMsg, &va, pErrInfo->pszMsg);
298 else
299 supR3HardenedError(rc, false /*fFatal*/, "%N\n", pszMsg, &va);
300 va_end(va);
301#endif
302
303 va_start(va, pszMsg);
304 RTErrInfoAddV(pErrInfo, rc, pszMsg, va);
305 va_end(va);
306
307 return rc;
308}
309
310
311/**
312 * Fills in error information.
313 *
314 * @returns @a rc.
315 * @param pThis The process validator instance.
316 * @param rc The status to return.
317 * @param pszMsg The format string for the message.
318 * @param ... The arguments for the format string.
319 */
320static int supHardNtVpSetInfo2(PSUPHNTVPSTATE pThis, int rc, const char *pszMsg, ...)
321{
322 va_list va;
323#ifdef IN_RING3
324 va_start(va, pszMsg);
325 supR3HardenedError(rc, false /*fFatal*/, "%N\n", pszMsg, &va);
326 va_end(va);
327#endif
328
329 va_start(va, pszMsg);
330#ifdef IN_RING0
331 RTErrInfoSetV(pThis->pErrInfo, rc, pszMsg, va);
332 pThis->rcResult = rc;
333#else
334 if (RT_SUCCESS(pThis->rcResult))
335 {
336 RTErrInfoSetV(pThis->pErrInfo, rc, pszMsg, va);
337 pThis->rcResult = rc;
338 }
339 else
340 {
341 RTErrInfoAddF(pThis->pErrInfo, rc, " \n[rc=%d] ", rc);
342 RTErrInfoAddV(pThis->pErrInfo, rc, pszMsg, va);
343 }
344#endif
345 va_end(va);
346
347 return pThis->rcResult;
348}
349
350
351static int supHardNtVpReadImage(PSUPHNTVPIMAGE pImage, uint64_t off, void *pvBuf, size_t cbRead)
352{
353 return pImage->pCacheEntry->pNtViRdr->Core.pfnRead(&pImage->pCacheEntry->pNtViRdr->Core, pvBuf, cbRead, off);
354}
355
356
357static NTSTATUS supHardNtVpReadMem(HANDLE hProcess, uintptr_t uPtr, void *pvBuf, size_t cbRead)
358{
359#ifdef IN_RING0
360 /* ASSUMES hProcess is the current process. */
361 RT_NOREF1(hProcess);
362 /** @todo use MmCopyVirtualMemory where available! */
363 int rc = RTR0MemUserCopyFrom(pvBuf, uPtr, cbRead);
364 if (RT_SUCCESS(rc))
365 return STATUS_SUCCESS;
366 return STATUS_ACCESS_DENIED;
367#else
368 SIZE_T cbIgn;
369 NTSTATUS rcNt = NtReadVirtualMemory(hProcess, (PVOID)uPtr, pvBuf, cbRead, &cbIgn);
370 if (NT_SUCCESS(rcNt) && cbIgn != cbRead)
371 rcNt = STATUS_IO_DEVICE_ERROR;
372 return rcNt;
373#endif
374}
375
376
377#ifdef IN_RING3
378static NTSTATUS supHardNtVpFileMemRestore(PSUPHNTVPSTATE pThis, PVOID pvRestoreAddr, uint8_t const *pbFile, uint32_t cbToRestore,
379 uint32_t fCorrectProtection)
380{
381 PVOID pvProt = pvRestoreAddr;
382 SIZE_T cbProt = cbToRestore;
383 ULONG fOldProt = 0;
384 NTSTATUS rcNt = NtProtectVirtualMemory(pThis->hProcess, &pvProt, &cbProt, PAGE_READWRITE, &fOldProt);
385 if (NT_SUCCESS(rcNt))
386 {
387 SIZE_T cbIgnored;
388 rcNt = NtWriteVirtualMemory(pThis->hProcess, pvRestoreAddr, pbFile, cbToRestore, &cbIgnored);
389
390 pvProt = pvRestoreAddr;
391 cbProt = cbToRestore;
392 NTSTATUS rcNt2 = NtProtectVirtualMemory(pThis->hProcess, &pvProt, &cbProt, fCorrectProtection, &fOldProt);
393 if (NT_SUCCESS(rcNt))
394 rcNt = rcNt2;
395 }
396 pThis->cFixes++;
397 return rcNt;
398}
399#endif /* IN_RING3 */
400
401
402typedef struct SUPHNTVPSKIPAREA
403{
404 uint32_t uRva;
405 uint32_t cb;
406} SUPHNTVPSKIPAREA;
407typedef SUPHNTVPSKIPAREA *PSUPHNTVPSKIPAREA;
408
409static int supHardNtVpFileMemCompareSection(PSUPHNTVPSTATE pThis, PSUPHNTVPIMAGE pImage,
410 uint32_t uRva, uint32_t cb, const uint8_t *pbFile,
411 int32_t iSh, PSUPHNTVPSKIPAREA paSkipAreas, uint32_t cSkipAreas,
412 uint32_t fCorrectProtection)
413{
414#ifndef IN_RING3
415 RT_NOREF1(fCorrectProtection);
416#endif
417 AssertCompileAdjacentMembers(SUPHNTVPSTATE, abMemory, abFile); /* Use both the memory and file buffers here. Parfait might hate me for this... */
418 uint32_t const cbMemory = sizeof(pThis->abMemory) + sizeof(pThis->abFile);
419 uint8_t * const pbMemory = &pThis->abMemory[0];
420
421 while (cb > 0)
422 {
423 uint32_t cbThis = RT_MIN(cb, cbMemory);
424
425 /* Clipping. */
426 uint32_t uNextRva = uRva + cbThis;
427 if (cSkipAreas)
428 {
429 uint32_t uRvaEnd = uNextRva;
430 uint32_t i = cSkipAreas;
431 while (i-- > 0)
432 {
433 uint32_t uSkipEnd = paSkipAreas[i].uRva + paSkipAreas[i].cb;
434 if ( uRva < uSkipEnd
435 && uRvaEnd > paSkipAreas[i].uRva)
436 {
437 if (uRva < paSkipAreas[i].uRva)
438 {
439 cbThis = paSkipAreas[i].uRva - uRva;
440 uRvaEnd = paSkipAreas[i].uRva;
441 uNextRva = uSkipEnd;
442 }
443 else if (uRvaEnd >= uSkipEnd)
444 {
445 cbThis -= uSkipEnd - uRva;
446 pbFile += uSkipEnd - uRva;
447 uRva = uSkipEnd;
448 }
449 else
450 {
451 uNextRva = uSkipEnd;
452 cbThis = 0;
453 break;
454 }
455 }
456 }
457 }
458
459 /* Read the memory. */
460 NTSTATUS rcNt = supHardNtVpReadMem(pThis->hProcess, pImage->uImageBase + uRva, pbMemory, cbThis);
461 if (!NT_SUCCESS(rcNt))
462 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_MEMORY_READ_ERROR,
463 "%s: Error reading %#x bytes at %p (rva %#x, #%u, %.8s) from memory: %#x",
464 pImage->pszName, cbThis, pImage->uImageBase + uRva, uRva, iSh + 1,
465 iSh >= 0 ? (char *)pThis->aSecHdrs[iSh].Name : "headers", rcNt);
466
467 /* Do the compare. */
468 if (memcmp(pbFile, pbMemory, cbThis) != 0)
469 {
470 const char *pachSectNm = iSh >= 0 ? (char *)pThis->aSecHdrs[iSh].Name : "headers";
471 SUP_DPRINTF(("%s: Differences in section #%u (%s) between file and memory:\n", pImage->pszName, iSh + 1, pachSectNm));
472
473 uint32_t off = 0;
474 while (off < cbThis && pbFile[off] == pbMemory[off])
475 off++;
476 SUP_DPRINTF((" %p / %#09x: %02x != %02x\n",
477 pImage->uImageBase + uRva + off, uRva + off, pbFile[off], pbMemory[off]));
478 uint32_t offLast = off;
479 uint32_t cDiffs = 1;
480 for (uint32_t off2 = off + 1; off2 < cbThis; off2++)
481 if (pbFile[off2] != pbMemory[off2])
482 {
483 SUP_DPRINTF((" %p / %#09x: %02x != %02x\n",
484 pImage->uImageBase + uRva + off2, uRva + off2, pbFile[off2], pbMemory[off2]));
485 cDiffs++;
486 offLast = off2;
487 }
488
489#ifdef IN_RING3
490 if ( pThis->enmKind == SUPHARDNTVPKIND_CHILD_PURIFICATION
491 || pThis->enmKind == SUPHARDNTVPKIND_SELF_PURIFICATION)
492 {
493 PVOID pvRestoreAddr = (uint8_t *)pImage->uImageBase + uRva;
494 rcNt = supHardNtVpFileMemRestore(pThis, pvRestoreAddr, pbFile, cbThis, fCorrectProtection);
495 if (NT_SUCCESS(rcNt))
496 SUP_DPRINTF((" Restored %#x bytes of original file content at %p\n", cbThis, pvRestoreAddr));
497 else
498 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_MEMORY_VS_FILE_MISMATCH,
499 "%s: Failed to restore %#x bytes at %p (%#x, #%u, %s): %#x (cDiffs=%#x, first=%#x)",
500 pImage->pszName, cbThis, pvRestoreAddr, uRva, iSh + 1, pachSectNm, rcNt,
501 cDiffs, uRva + off);
502 }
503 else
504#endif /* IN_RING3 */
505 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_MEMORY_VS_FILE_MISMATCH,
506 "%s: %u differences between %#x and %#x in #%u (%.8s), first: %02x != %02x",
507 pImage->pszName, cDiffs, uRva + off, uRva + offLast, iSh + 1,
508 pachSectNm, pbFile[off], pbMemory[off]);
509 }
510
511 /* Advance. The clipping makes it a little bit complicated. */
512 cbThis = uNextRva - uRva;
513 if (cbThis >= cb)
514 break;
515 cb -= cbThis;
516 pbFile += cbThis;
517 uRva = uNextRva;
518 }
519 return VINF_SUCCESS;
520}
521
522
523
524static int supHardNtVpCheckSectionProtection(PSUPHNTVPSTATE pThis, PSUPHNTVPIMAGE pImage,
525 uint32_t uRva, uint32_t cb, uint32_t fProt)
526{
527 uint32_t const cbOrg = cb;
528 if (!cb)
529 return VINF_SUCCESS;
530 if ( pThis->enmKind == SUPHARDNTVPKIND_CHILD_PURIFICATION
531 || pThis->enmKind == SUPHARDNTVPKIND_SELF_PURIFICATION)
532 return VINF_SUCCESS;
533
534 for (uint32_t i = 0; i < pImage->cRegions; i++)
535 {
536 uint32_t offRegion = uRva - pImage->aRegions[i].uRva;
537 if (offRegion < pImage->aRegions[i].cb)
538 {
539 uint32_t cbLeft = pImage->aRegions[i].cb - offRegion;
540 if ( pImage->aRegions[i].fProt != fProt
541 && ( fProt != PAGE_READWRITE
542 || pImage->aRegions[i].fProt != PAGE_WRITECOPY))
543 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_SECTION_PROTECTION_MISMATCH,
544 "%s: RVA range %#x-%#x protection is %#x, expected %#x. (cb=%#x)",
545 pImage->pszName, uRva, uRva + cbLeft - 1, pImage->aRegions[i].fProt, fProt, cb);
546 if (cbLeft >= cb)
547 return VINF_SUCCESS;
548 cb -= cbLeft;
549 uRva += cbLeft;
550
551#if 0 /* This shouldn't ever be necessary. */
552 if ( i + 1 < pImage->cRegions
553 && uRva < pImage->aRegions[i + 1].uRva)
554 {
555 cbLeft = pImage->aRegions[i + 1].uRva - uRva;
556 if (cbLeft >= cb)
557 return VINF_SUCCESS;
558 cb -= cbLeft;
559 uRva += cbLeft;
560 }
561#endif
562 }
563 }
564
565 return supHardNtVpSetInfo2(pThis, cbOrg == cb ? VERR_SUP_VP_SECTION_NOT_MAPPED : VERR_SUP_VP_SECTION_NOT_FULLY_MAPPED,
566 "%s: RVA range %#x-%#x is not mapped?", pImage->pszName, uRva, uRva + cb - 1);
567}
568
569
570DECLINLINE(bool) supHardNtVpIsModuleNameMatch(PSUPHNTVPIMAGE pImage, const char *pszModule)
571{
572 if (pImage->fDll)
573 {
574 const char *pszImageNm = pImage->pszName;
575 for (;;)
576 {
577 char chLeft = *pszImageNm++;
578 char chRight = *pszModule++;
579 if (chLeft != chRight)
580 {
581 Assert(chLeft == RT_C_TO_LOWER(chLeft));
582 if (chLeft != RT_C_TO_LOWER(chRight))
583 {
584 if ( chRight == '\0'
585 && chLeft == '.'
586 && pszImageNm[0] == 'd'
587 && pszImageNm[1] == 'l'
588 && pszImageNm[2] == 'l'
589 && pszImageNm[3] == '\0')
590 return true;
591 break;
592 }
593 }
594
595 if (chLeft == '\0')
596 return true;
597 }
598 }
599
600 return false;
601}
602
603
604/**
605 * Worker for supHardNtVpGetImport that looks up a module in the module table.
606 *
607 * @returns Pointer to the module if found, NULL if not found.
608 * @param pThis The process validator instance.
609 * @param pszModule The name of the module we're looking for.
610 */
611static PSUPHNTVPIMAGE supHardNtVpFindModule(PSUPHNTVPSTATE pThis, const char *pszModule)
612{
613 /*
614 * Check out the hint first.
615 */
616 if ( pThis->iImageHint < pThis->cImages
617 && supHardNtVpIsModuleNameMatch(&pThis->aImages[pThis->iImageHint], pszModule))
618 return &pThis->aImages[pThis->iImageHint];
619
620 /*
621 * Linear array search next.
622 */
623 uint32_t i = pThis->cImages;
624 while (i-- > 0)
625 if (supHardNtVpIsModuleNameMatch(&pThis->aImages[i], pszModule))
626 {
627 pThis->iImageHint = i;
628 return &pThis->aImages[i];
629 }
630
631 /* No cigar. */
632 return NULL;
633}
634
635
636/**
637 * @callback_method_impl{FNRTLDRIMPORT}
638 */
639static DECLCALLBACK(int) supHardNtVpGetImport(RTLDRMOD hLdrMod, const char *pszModule, const char *pszSymbol, unsigned uSymbol,
640 PRTLDRADDR pValue, void *pvUser)
641{
642 RT_NOREF1(hLdrMod);
643 /*SUP_DPRINTF(("supHardNtVpGetImport: %s / %#x / %s.\n", pszModule, uSymbol, pszSymbol));*/
644 PSUPHNTVPSTATE pThis = (PSUPHNTVPSTATE)pvUser;
645
646 int rc = VERR_MODULE_NOT_FOUND;
647 PSUPHNTVPIMAGE pImage = supHardNtVpFindModule(pThis, pszModule);
648 if (pImage)
649 {
650 rc = RTLdrGetSymbolEx(pImage->pCacheEntry->hLdrMod, pImage->pCacheEntry->pbBits,
651 pImage->uImageBase, uSymbol, pszSymbol, pValue);
652 if (RT_SUCCESS(rc))
653 return rc;
654 }
655 /*
656 * API set hacks.
657 */
658 else if (!RTStrNICmp(pszModule, RT_STR_TUPLE("api-ms-win-")))
659 {
660 static const char * const s_apszDlls[] = { "ntdll.dll", "kernelbase.dll", "kernel32.dll" };
661 for (uint32_t i = 0; i < RT_ELEMENTS(s_apszDlls); i++)
662 {
663 pImage = supHardNtVpFindModule(pThis, s_apszDlls[i]);
664 if (pImage)
665 {
666 rc = RTLdrGetSymbolEx(pImage->pCacheEntry->hLdrMod, pImage->pCacheEntry->pbBits,
667 pImage->uImageBase, uSymbol, pszSymbol, pValue);
668 if (RT_SUCCESS(rc))
669 return rc;
670 if (rc != VERR_SYMBOL_NOT_FOUND)
671 break;
672 }
673 }
674 }
675
676 /*
677 * Deal with forwarders.
678 * ASSUMES no forwarders thru any api-ms-win-core-*.dll.
679 * ASSUMES forwarders are resolved after one redirection.
680 */
681 if (rc == VERR_LDR_FORWARDER)
682 {
683 size_t cbInfo = RT_MIN((uint32_t)*pValue, sizeof(RTLDRIMPORTINFO) + 32);
684 PRTLDRIMPORTINFO pInfo = (PRTLDRIMPORTINFO)alloca(cbInfo);
685 rc = RTLdrQueryForwarderInfo(pImage->pCacheEntry->hLdrMod, pImage->pCacheEntry->pbBits,
686 uSymbol, pszSymbol, pInfo, cbInfo);
687 if (RT_SUCCESS(rc))
688 {
689 rc = VERR_MODULE_NOT_FOUND;
690 pImage = supHardNtVpFindModule(pThis, pInfo->szModule);
691 if (pImage)
692 {
693 rc = RTLdrGetSymbolEx(pImage->pCacheEntry->hLdrMod, pImage->pCacheEntry->pbBits,
694 pImage->uImageBase, pInfo->iOrdinal, pInfo->pszSymbol, pValue);
695 if (RT_SUCCESS(rc))
696 return rc;
697
698 SUP_DPRINTF(("supHardNtVpGetImport: Failed to find symbol '%s' in '%s' (forwarded from %s / %s): %Rrc\n",
699 pInfo->pszSymbol, pInfo->szModule, pszModule, pszSymbol, rc));
700 if (rc == VERR_LDR_FORWARDER)
701 rc = VERR_LDR_FORWARDER_CHAIN_TOO_LONG;
702 }
703 else
704 SUP_DPRINTF(("supHardNtVpGetImport: Failed to find forwarder module '%s' (%#x / %s; originally %s / %#x / %s): %Rrc\n",
705 pInfo->szModule, pInfo->iOrdinal, pInfo->pszSymbol, pszModule, uSymbol, pszSymbol, rc));
706 }
707 else
708 SUP_DPRINTF(("supHardNtVpGetImport: RTLdrQueryForwarderInfo failed on symbol %#x/'%s' in '%s': %Rrc\n",
709 uSymbol, pszSymbol, pszModule, rc));
710 }
711 else
712 SUP_DPRINTF(("supHardNtVpGetImport: Failed to find symbol %#x / '%s' in '%s': %Rrc\n",
713 uSymbol, pszSymbol, pszModule, rc));
714 return rc;
715}
716
717
718/**
719 * Compares process memory with the disk content.
720 *
721 * @returns VBox status code.
722 * @param pThis The process scanning state structure (for the
723 * two scratch buffers).
724 * @param pImage The image data collected during the address
725 * space scan.
726 * @param hProcess Handle to the process.
727 */
728static int supHardNtVpVerifyImageMemoryCompare(PSUPHNTVPSTATE pThis, PSUPHNTVPIMAGE pImage)
729{
730
731 /*
732 * Read and find the file headers.
733 */
734 int rc = supHardNtVpReadImage(pImage, 0 /*off*/, pThis->abFile, sizeof(pThis->abFile));
735 if (RT_FAILURE(rc))
736 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_IMAGE_HDR_READ_ERROR,
737 "%s: Error reading image header: %Rrc", pImage->pszName, rc);
738
739 uint32_t offNtHdrs = 0;
740 PIMAGE_DOS_HEADER pDosHdr = (PIMAGE_DOS_HEADER)&pThis->abFile[0];
741 if (pDosHdr->e_magic == IMAGE_DOS_SIGNATURE)
742 {
743 offNtHdrs = pDosHdr->e_lfanew;
744 if (offNtHdrs > 512 || offNtHdrs < sizeof(*pDosHdr))
745 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_BAD_MZ_OFFSET,
746 "%s: Unexpected e_lfanew value: %#x", pImage->pszName, offNtHdrs);
747 }
748 PIMAGE_NT_HEADERS pNtHdrs = (PIMAGE_NT_HEADERS)&pThis->abFile[offNtHdrs];
749 PIMAGE_NT_HEADERS32 pNtHdrs32 = (PIMAGE_NT_HEADERS32)pNtHdrs;
750 if (pNtHdrs->Signature != IMAGE_NT_SIGNATURE)
751 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_BAD_IMAGE_SIGNATURE,
752 "%s: No PE signature at %#x: %#x", pImage->pszName, offNtHdrs, pNtHdrs->Signature);
753
754 /*
755 * Do basic header validation.
756 */
757#ifdef RT_ARCH_AMD64
758 if (pNtHdrs->FileHeader.Machine != IMAGE_FILE_MACHINE_AMD64 && !pImage->f32bitResourceDll)
759#else
760 if (pNtHdrs->FileHeader.Machine != IMAGE_FILE_MACHINE_I386)
761#endif
762 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_UNEXPECTED_IMAGE_MACHINE,
763 "%s: Unexpected machine: %#x", pImage->pszName, pNtHdrs->FileHeader.Machine);
764 bool const fIs32Bit = pNtHdrs->FileHeader.Machine == IMAGE_FILE_MACHINE_I386;
765
766 if (pNtHdrs->FileHeader.SizeOfOptionalHeader != (fIs32Bit ? sizeof(IMAGE_OPTIONAL_HEADER32) : sizeof(IMAGE_OPTIONAL_HEADER64)))
767 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_BAD_OPTIONAL_HEADER,
768 "%s: Unexpected optional header size: %#x",
769 pImage->pszName, pNtHdrs->FileHeader.SizeOfOptionalHeader);
770
771 if (pNtHdrs->OptionalHeader.Magic != (fIs32Bit ? IMAGE_NT_OPTIONAL_HDR32_MAGIC : IMAGE_NT_OPTIONAL_HDR64_MAGIC))
772 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_BAD_OPTIONAL_HEADER,
773 "%s: Unexpected optional header magic: %#x", pImage->pszName, pNtHdrs->OptionalHeader.Magic);
774
775 uint32_t cDirs = (fIs32Bit ? pNtHdrs32->OptionalHeader.NumberOfRvaAndSizes : pNtHdrs->OptionalHeader.NumberOfRvaAndSizes);
776 if (cDirs != IMAGE_NUMBEROF_DIRECTORY_ENTRIES)
777 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_BAD_OPTIONAL_HEADER,
778 "%s: Unexpected data dirs: %#x", pImage->pszName, cDirs);
779
780 /*
781 * Before we start comparing things, store what we need to know from the headers.
782 */
783 uint32_t const cSections = pNtHdrs->FileHeader.NumberOfSections;
784 if (cSections > RT_ELEMENTS(pThis->aSecHdrs))
785 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_TOO_MANY_SECTIONS,
786 "%s: Too many section headers: %#x", pImage->pszName, cSections);
787 suplibHardenedMemCopy(pThis->aSecHdrs, (fIs32Bit ? (void *)(pNtHdrs32 + 1) : (void *)(pNtHdrs + 1)),
788 cSections * sizeof(IMAGE_SECTION_HEADER));
789
790 uintptr_t const uImageBase = fIs32Bit ? pNtHdrs32->OptionalHeader.ImageBase : pNtHdrs->OptionalHeader.ImageBase;
791 if (uImageBase & PAGE_OFFSET_MASK)
792 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_BAD_IMAGE_BASE,
793 "%s: Invalid image base: %p", pImage->pszName, uImageBase);
794
795 uint32_t const cbImage = fIs32Bit ? pNtHdrs32->OptionalHeader.SizeOfImage : pNtHdrs->OptionalHeader.SizeOfImage;
796 if (RT_ALIGN_32(pImage->cbImage, PAGE_SIZE) != RT_ALIGN_32(cbImage, PAGE_SIZE) && !pImage->fApiSetSchemaOnlySection1)
797 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_BAD_IMAGE_SIZE,
798 "%s: SizeOfImage (%#x) isn't close enough to the mapping size (%#x)",
799 pImage->pszName, cbImage, pImage->cbImage);
800 if (cbImage != RTLdrSize(pImage->pCacheEntry->hLdrMod))
801 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_BAD_IMAGE_SIZE,
802 "%s: SizeOfImage (%#x) differs from what RTLdrSize returns (%#zx)",
803 pImage->pszName, cbImage, RTLdrSize(pImage->pCacheEntry->hLdrMod));
804
805 uint32_t const cbSectAlign = fIs32Bit ? pNtHdrs32->OptionalHeader.SectionAlignment : pNtHdrs->OptionalHeader.SectionAlignment;
806 if ( !RT_IS_POWER_OF_TWO(cbSectAlign)
807 || cbSectAlign < PAGE_SIZE
808 || cbSectAlign > (pImage->fApiSetSchemaOnlySection1 ? _64K : (uint32_t)PAGE_SIZE) )
809 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_BAD_SECTION_ALIGNMENT_VALUE,
810 "%s: Unexpected SectionAlignment value: %#x", pImage->pszName, cbSectAlign);
811
812 uint32_t const cbFileAlign = fIs32Bit ? pNtHdrs32->OptionalHeader.FileAlignment : pNtHdrs->OptionalHeader.FileAlignment;
813 if (!RT_IS_POWER_OF_TWO(cbFileAlign) || cbFileAlign < 512 || cbFileAlign > PAGE_SIZE || cbFileAlign > cbSectAlign)
814 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_BAD_FILE_ALIGNMENT_VALUE,
815 "%s: Unexpected FileAlignment value: %#x (cbSectAlign=%#x)",
816 pImage->pszName, cbFileAlign, cbSectAlign);
817
818 uint32_t const cbHeaders = fIs32Bit ? pNtHdrs32->OptionalHeader.SizeOfHeaders : pNtHdrs->OptionalHeader.SizeOfHeaders;
819 uint32_t const cbMinHdrs = offNtHdrs + (fIs32Bit ? sizeof(*pNtHdrs32) : sizeof(*pNtHdrs) )
820 + sizeof(IMAGE_SECTION_HEADER) * cSections;
821 if (cbHeaders < cbMinHdrs)
822 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_BAD_SIZE_OF_HEADERS,
823 "%s: Headers are too small: %#x < %#x (cSections=%#x)",
824 pImage->pszName, cbHeaders, cbMinHdrs, cSections);
825 uint32_t const cbHdrsFile = RT_ALIGN_32(cbHeaders, cbFileAlign);
826 if (cbHdrsFile > sizeof(pThis->abFile))
827 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_BAD_SIZE_OF_HEADERS,
828 "%s: Headers are larger than expected: %#x/%#x (expected max %zx)",
829 pImage->pszName, cbHeaders, cbHdrsFile, sizeof(pThis->abFile));
830
831 /*
832 * Save some header fields we might be using later on.
833 */
834 pImage->fImageCharecteristics = pNtHdrs->FileHeader.Characteristics;
835 pImage->fDllCharecteristics = fIs32Bit ? pNtHdrs32->OptionalHeader.DllCharacteristics : pNtHdrs->OptionalHeader.DllCharacteristics;
836
837 /*
838 * Correct the apisetschema image base, size and region rva.
839 */
840 if (pImage->fApiSetSchemaOnlySection1)
841 {
842 pImage->uImageBase -= pThis->aSecHdrs[0].VirtualAddress;
843 pImage->cbImage += pThis->aSecHdrs[0].VirtualAddress;
844 pImage->aRegions[0].uRva = pThis->aSecHdrs[0].VirtualAddress;
845 }
846
847 /*
848 * Get relocated bits.
849 */
850 uint8_t *pbBits;
851 if (pThis->enmKind == SUPHARDNTVPKIND_CHILD_PURIFICATION)
852 rc = supHardNtLdrCacheEntryGetBits(pImage->pCacheEntry, &pbBits, pImage->uImageBase, NULL /*pfnGetImport*/, pThis,
853 pThis->pErrInfo);
854 else
855 rc = supHardNtLdrCacheEntryGetBits(pImage->pCacheEntry, &pbBits, pImage->uImageBase, supHardNtVpGetImport, pThis,
856 pThis->pErrInfo);
857 if (RT_FAILURE(rc))
858 return rc;
859
860 /* XP SP3 does not set ImageBase to load address. It fixes up the image on load time though. */
861 if (g_uNtVerCombined >= SUP_NT_VER_VISTA)
862 {
863 if (fIs32Bit)
864 ((PIMAGE_NT_HEADERS32)&pbBits[offNtHdrs])->OptionalHeader.ImageBase = (uint32_t)pImage->uImageBase;
865 else
866 ((PIMAGE_NT_HEADERS)&pbBits[offNtHdrs])->OptionalHeader.ImageBase = pImage->uImageBase;
867 }
868
869 /*
870 * Figure out areas we should skip during comparison.
871 */
872 uint32_t cSkipAreas = 0;
873 SUPHNTVPSKIPAREA aSkipAreas[5];
874 if (pImage->fNtCreateSectionPatch)
875 {
876 RTLDRADDR uValue;
877 if (pThis->enmKind == SUPHARDNTVPKIND_VERIFY_ONLY)
878 {
879 /* Ignore our NtCreateSection hack. */
880 rc = RTLdrGetSymbolEx(pImage->pCacheEntry->hLdrMod, pbBits, 0, UINT32_MAX, "NtCreateSection", &uValue);
881 if (RT_FAILURE(rc))
882 return supHardNtVpSetInfo2(pThis, rc, "%s: Failed to find 'NtCreateSection': %Rrc", pImage->pszName, rc);
883 aSkipAreas[cSkipAreas].uRva = (uint32_t)uValue;
884 aSkipAreas[cSkipAreas++].cb = ARCH_BITS == 32 ? 5 : 12;
885
886 /* Ignore our LdrLoadDll hack. */
887 rc = RTLdrGetSymbolEx(pImage->pCacheEntry->hLdrMod, pbBits, 0, UINT32_MAX, "LdrLoadDll", &uValue);
888 if (RT_FAILURE(rc))
889 return supHardNtVpSetInfo2(pThis, rc, "%s: Failed to find 'LdrLoadDll': %Rrc", pImage->pszName, rc);
890 aSkipAreas[cSkipAreas].uRva = (uint32_t)uValue;
891 aSkipAreas[cSkipAreas++].cb = ARCH_BITS == 32 ? 5 : 12;
892 }
893
894 /* Ignore our patched LdrInitializeThunk hack. */
895 rc = RTLdrGetSymbolEx(pImage->pCacheEntry->hLdrMod, pbBits, 0, UINT32_MAX, "LdrInitializeThunk", &uValue);
896 if (RT_FAILURE(rc))
897 return supHardNtVpSetInfo2(pThis, rc, "%s: Failed to find 'LdrInitializeThunk': %Rrc", pImage->pszName, rc);
898 aSkipAreas[cSkipAreas].uRva = (uint32_t)uValue;
899 aSkipAreas[cSkipAreas++].cb = 14;
900
901 /* LdrSystemDllInitBlock is filled in by the kernel. It mainly contains addresses of 32-bit ntdll method for wow64. */
902 rc = RTLdrGetSymbolEx(pImage->pCacheEntry->hLdrMod, pbBits, 0, UINT32_MAX, "LdrSystemDllInitBlock", &uValue);
903 if (RT_SUCCESS(rc))
904 {
905 aSkipAreas[cSkipAreas].uRva = (uint32_t)uValue;
906 aSkipAreas[cSkipAreas++].cb = RT_MAX(pbBits[(uint32_t)uValue], 0x50);
907 }
908
909 Assert(cSkipAreas <= RT_ELEMENTS(aSkipAreas));
910 }
911
912 /*
913 * Compare the file header with the loaded bits. The loader will fiddle
914 * with image base, changing it to the actual load address.
915 */
916 if (!pImage->fApiSetSchemaOnlySection1)
917 {
918 rc = supHardNtVpFileMemCompareSection(pThis, pImage, 0 /*uRva*/, cbHdrsFile, pbBits, -1, NULL, 0, PAGE_READONLY);
919 if (RT_FAILURE(rc))
920 return rc;
921
922 rc = supHardNtVpCheckSectionProtection(pThis, pImage, 0 /*uRva*/, cbHdrsFile, PAGE_READONLY);
923 if (RT_FAILURE(rc))
924 return rc;
925 }
926
927 /*
928 * Validate sections:
929 * - Check them against the mapping regions.
930 * - Check section bits according to enmKind.
931 */
932 uint32_t fPrevProt = PAGE_READONLY;
933 uint32_t uRva = cbHdrsFile;
934 for (uint32_t i = 0; i < cSections; i++)
935 {
936 /* Validate the section. */
937 uint32_t uSectRva = pThis->aSecHdrs[i].VirtualAddress;
938 if (uSectRva < uRva || uSectRva > cbImage || RT_ALIGN_32(uSectRva, cbSectAlign) != uSectRva)
939 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_BAD_SECTION_RVA,
940 "%s: Section %u: Invalid virtual address: %#x (uRva=%#x, cbImage=%#x, cbSectAlign=%#x)",
941 pImage->pszName, i, uSectRva, uRva, cbImage, cbSectAlign);
942 uint32_t cbMap = pThis->aSecHdrs[i].Misc.VirtualSize;
943 if (cbMap > cbImage || uRva + cbMap > cbImage)
944 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_BAD_SECTION_VIRTUAL_SIZE,
945 "%s: Section %u: Invalid virtual size: %#x (uSectRva=%#x, uRva=%#x, cbImage=%#x)",
946 pImage->pszName, i, cbMap, uSectRva, uRva, cbImage);
947 uint32_t cbFile = pThis->aSecHdrs[i].SizeOfRawData;
948 if (cbFile != RT_ALIGN_32(cbFile, cbFileAlign) || cbFile > RT_ALIGN_32(cbMap, cbSectAlign))
949 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_BAD_SECTION_FILE_SIZE,
950 "%s: Section %u: Invalid file size: %#x (cbMap=%#x, uSectRva=%#x)",
951 pImage->pszName, i, cbFile, cbMap, uSectRva);
952
953 /* Validate the protection and bits. */
954 if (!pImage->fApiSetSchemaOnlySection1 || i == 0)
955 {
956 uint32_t fProt;
957 switch (pThis->aSecHdrs[i].Characteristics & (IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE))
958 {
959 case IMAGE_SCN_MEM_READ:
960 fProt = PAGE_READONLY;
961 break;
962 case IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE:
963 fProt = PAGE_READWRITE;
964 if ( pThis->enmKind != SUPHARDNTVPKIND_VERIFY_ONLY
965 && pThis->enmKind != SUPHARDNTVPKIND_CHILD_PURIFICATION
966 && !suplibHardenedMemComp(pThis->aSecHdrs[i].Name, ".mrdata", 8)) /* w8.1, ntdll. Changed by proc init. */
967 fProt = PAGE_READONLY;
968 break;
969 case IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_EXECUTE:
970 fProt = PAGE_EXECUTE_READ;
971 break;
972 case IMAGE_SCN_MEM_EXECUTE:
973 fProt = PAGE_EXECUTE;
974 break;
975 case IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE:
976 /* Only the executable is allowed to have this section,
977 and it's protected after we're done patching. */
978 if (!pImage->fDll)
979 {
980 if (pThis->enmKind == SUPHARDNTVPKIND_CHILD_PURIFICATION)
981 fProt = PAGE_EXECUTE_READWRITE;
982 else
983 fProt = PAGE_EXECUTE_READ;
984 break;
985 }
986 default:
987 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_UNEXPECTED_SECTION_FLAGS,
988 "%s: Section %u: Unexpected characteristics: %#x (uSectRva=%#x, cbMap=%#x)",
989 pImage->pszName, i, pThis->aSecHdrs[i].Characteristics, uSectRva, cbMap);
990 }
991
992 /* The section bits. Child purification verifies all, normal
993 verification verifies all except where the executable is
994 concerned (due to opening vboxdrv during early process init). */
995 if ( ( (pThis->aSecHdrs[i].Characteristics & (IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_CNT_CODE))
996 && !(pThis->aSecHdrs[i].Characteristics & IMAGE_SCN_MEM_WRITE))
997 || (pThis->aSecHdrs[i].Characteristics & (IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE)) == IMAGE_SCN_MEM_READ
998 || (pThis->enmKind == SUPHARDNTVPKIND_VERIFY_ONLY && pImage->fDll)
999 || pThis->enmKind == SUPHARDNTVPKIND_CHILD_PURIFICATION)
1000 {
1001 rc = VINF_SUCCESS;
1002 if (uRva < uSectRva && !pImage->fApiSetSchemaOnlySection1) /* Any gap worth checking? */
1003 rc = supHardNtVpFileMemCompareSection(pThis, pImage, uRva, uSectRva - uRva, pbBits + uRva,
1004 i - 1, NULL, 0, fPrevProt);
1005 if (RT_SUCCESS(rc))
1006 rc = supHardNtVpFileMemCompareSection(pThis, pImage, uSectRva, cbMap, pbBits + uSectRva,
1007 i, aSkipAreas, cSkipAreas, fProt);
1008 if (RT_SUCCESS(rc))
1009 {
1010 uint32_t cbMapAligned = i + 1 < cSections && !pImage->fApiSetSchemaOnlySection1
1011 ? RT_ALIGN_32(cbMap, cbSectAlign) : RT_ALIGN_32(cbMap, PAGE_SIZE);
1012 if (cbMapAligned > cbMap)
1013 rc = supHardNtVpFileMemCompareSection(pThis, pImage, uSectRva + cbMap, cbMapAligned - cbMap,
1014 g_abRTZeroPage, i, NULL, 0, fProt);
1015 }
1016 if (RT_FAILURE(rc))
1017 return rc;
1018 }
1019
1020 /* The protection (must be checked afterwards!). */
1021 rc = supHardNtVpCheckSectionProtection(pThis, pImage, uSectRva, RT_ALIGN_32(cbMap, PAGE_SIZE), fProt);
1022 if (RT_FAILURE(rc))
1023 return rc;
1024
1025 fPrevProt = fProt;
1026 }
1027
1028 /* Advance the RVA. */
1029 uRva = uSectRva + RT_ALIGN_32(cbMap, cbSectAlign);
1030 }
1031
1032 return VINF_SUCCESS;
1033}
1034
1035
1036/**
1037 * Verifies the signature of the given image on disk, then checks if the memory
1038 * mapping matches what we verified.
1039 *
1040 * @returns VBox status code.
1041 * @param pThis The process scanning state structure (for the
1042 * two scratch buffers).
1043 * @param pImage The image data collected during the address
1044 * space scan.
1045 */
1046static int supHardNtVpVerifyImage(PSUPHNTVPSTATE pThis, PSUPHNTVPIMAGE pImage)
1047{
1048 /*
1049 * Validate the file signature first, then do the memory compare.
1050 */
1051 int rc;
1052 if ( pImage->pCacheEntry != NULL
1053 && pImage->pCacheEntry->hLdrMod != NIL_RTLDRMOD)
1054 {
1055 rc = supHardNtLdrCacheEntryVerify(pImage->pCacheEntry, pImage->Name.UniStr.Buffer, pThis->pErrInfo);
1056 if (RT_SUCCESS(rc))
1057 rc = supHardNtVpVerifyImageMemoryCompare(pThis, pImage);
1058 }
1059 else
1060 rc = supHardNtVpSetInfo2(pThis, VERR_OPEN_FAILED, "pCacheEntry/hLdrMod is NIL! Impossible!");
1061 return rc;
1062}
1063
1064
1065/**
1066 * Verifies that there is only one thread in the process.
1067 *
1068 * @returns VBox status code.
1069 * @param hProcess The process.
1070 * @param hThread The thread.
1071 * @param pErrInfo Pointer to error info structure. Optional.
1072 */
1073DECLHIDDEN(int) supHardNtVpThread(HANDLE hProcess, HANDLE hThread, PRTERRINFO pErrInfo)
1074{
1075 RT_NOREF1(hProcess);
1076
1077 /*
1078 * Use the ThreadAmILastThread request to check that there is only one
1079 * thread in the process.
1080 * Seems this isn't entirely reliable when hThread isn't the current thread?
1081 */
1082 ULONG cbIgn = 0;
1083 ULONG fAmI = 0;
1084 NTSTATUS rcNt = NtQueryInformationThread(hThread, ThreadAmILastThread, &fAmI, sizeof(fAmI), &cbIgn);
1085 if (!NT_SUCCESS(rcNt))
1086 return supHardNtVpSetInfo1(pErrInfo, VERR_SUP_VP_NT_QI_THREAD_ERROR,
1087 "NtQueryInformationThread/ThreadAmILastThread -> %#x", rcNt);
1088 if (!fAmI)
1089 return supHardNtVpSetInfo1(pErrInfo, VERR_SUP_VP_THREAD_NOT_ALONE,
1090 "More than one thread in process");
1091
1092 /** @todo Would be nice to verify the relation ship between hProcess and hThread
1093 * as well... */
1094 return VINF_SUCCESS;
1095}
1096
1097
1098/**
1099 * Verifies that there isn't a debugger attached to the process.
1100 *
1101 * @returns VBox status code.
1102 * @param hProcess The process.
1103 * @param pErrInfo Pointer to error info structure. Optional.
1104 */
1105DECLHIDDEN(int) supHardNtVpDebugger(HANDLE hProcess, PRTERRINFO pErrInfo)
1106{
1107#ifndef VBOX_WITHOUT_DEBUGGER_CHECKS
1108 /*
1109 * Use the ProcessDebugPort request to check there is no debugger
1110 * currently attached to the process.
1111 */
1112 ULONG cbIgn = 0;
1113 uintptr_t uPtr = ~(uintptr_t)0;
1114 NTSTATUS rcNt = NtQueryInformationProcess(hProcess,
1115 ProcessDebugPort,
1116 &uPtr, sizeof(uPtr), &cbIgn);
1117 if (!NT_SUCCESS(rcNt))
1118 return supHardNtVpSetInfo1(pErrInfo, VERR_SUP_VP_NT_QI_PROCESS_DBG_PORT_ERROR,
1119 "NtQueryInformationProcess/ProcessDebugPort -> %#x", rcNt);
1120 if (uPtr != 0)
1121 return supHardNtVpSetInfo1(pErrInfo, VERR_SUP_VP_DEBUGGED,
1122 "Debugger attached (%#zx)", uPtr);
1123#else
1124 RT_NOREF2(hProcess, pErrInfo);
1125#endif /* !VBOX_WITHOUT_DEBUGGER_CHECKS */
1126 return VINF_SUCCESS;
1127}
1128
1129
1130/**
1131 * Matches two UNICODE_STRING structures in a case sensitive fashion.
1132 *
1133 * @returns true if equal, false if not.
1134 * @param pUniStr1 The first unicode string.
1135 * @param pUniStr2 The first unicode string.
1136 */
1137static bool supHardNtVpAreUniStringsEqual(PCUNICODE_STRING pUniStr1, PCUNICODE_STRING pUniStr2)
1138{
1139 if (pUniStr1->Length != pUniStr2->Length)
1140 return false;
1141 return suplibHardenedMemComp(pUniStr1->Buffer, pUniStr2->Buffer, pUniStr1->Length) == 0;
1142}
1143
1144
1145/**
1146 * Performs a case insensitive comparison of an ASCII and an UTF-16 file name.
1147 *
1148 * @returns true / false
1149 * @param pszName1 The ASCII name.
1150 * @param pwszName2 The UTF-16 name.
1151 */
1152static bool supHardNtVpAreNamesEqual(const char *pszName1, PCRTUTF16 pwszName2)
1153{
1154 for (;;)
1155 {
1156 char ch1 = *pszName1++;
1157 RTUTF16 wc2 = *pwszName2++;
1158 if (ch1 != wc2)
1159 {
1160 ch1 = RT_C_TO_LOWER(ch1);
1161 wc2 = wc2 < 0x80 ? RT_C_TO_LOWER(wc2) : wc2;
1162 if (ch1 != wc2)
1163 return false;
1164 }
1165 if (!ch1)
1166 return true;
1167 }
1168}
1169
1170
1171/**
1172 * Records an additional memory region for an image.
1173 *
1174 * May trash pThis->abMemory.
1175 *
1176 * @returns VBox status code.
1177 * @retval VINF_OBJECT_DESTROYED if we've unmapped the image (child
1178 * purification only).
1179 * @param pThis The process scanning state structure.
1180 * @param pImage The new image structure. Only the unicode name
1181 * buffer is valid (it's zero-terminated).
1182 * @param pMemInfo The memory information for the image.
1183 */
1184static int supHardNtVpNewImage(PSUPHNTVPSTATE pThis, PSUPHNTVPIMAGE pImage, PMEMORY_BASIC_INFORMATION pMemInfo)
1185{
1186 /*
1187 * If the filename or path contains short names, we have to get the long
1188 * path so that we will recognize the DLLs and their location.
1189 */
1190 PUNICODE_STRING pLongName = &pImage->Name.UniStr;
1191 if (RTNtPathFindPossible8dot3Name(pLongName->Buffer))
1192 {
1193 AssertCompile(sizeof(pThis->abMemory) > sizeof(pImage->Name));
1194 PUNICODE_STRING pTmp = (PUNICODE_STRING)pThis->abMemory;
1195 pTmp->MaximumLength = (USHORT)RT_MIN(_64K - 1, sizeof(pThis->abMemory) - sizeof(*pTmp)) - sizeof(RTUTF16);
1196 pTmp->Length = pImage->Name.UniStr.Length;
1197 pTmp->Buffer = (PRTUTF16)(pTmp + 1);
1198 memcpy(pTmp->Buffer, pLongName->Buffer, pLongName->Length + sizeof(RTUTF16));
1199
1200 RTNtPathExpand8dot3Path(pTmp, false /*fPathOnly*/);
1201 Assert(pTmp->Buffer[pTmp->Length / sizeof(RTUTF16)] == '\0');
1202
1203 pLongName = pTmp;
1204 }
1205
1206 /*
1207 * Extract the final component.
1208 */
1209 RTUTF16 wc;
1210 unsigned cwcDirName = pLongName->Length / sizeof(WCHAR);
1211 PCRTUTF16 pwszFilename = &pLongName->Buffer[cwcDirName];
1212 while ( cwcDirName > 0
1213 && (wc = pwszFilename[-1]) != '\\'
1214 && wc != '/'
1215 && wc != ':')
1216 {
1217 pwszFilename--;
1218 cwcDirName--;
1219 }
1220 if (!*pwszFilename)
1221 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_NO_IMAGE_MAPPING_NAME,
1222 "Empty filename (len=%u) for image at %p.", pLongName->Length, pMemInfo->BaseAddress);
1223
1224 /*
1225 * Drop trailing slashes from the directory name.
1226 */
1227 while ( cwcDirName > 0
1228 && ( pLongName->Buffer[cwcDirName - 1] == '\\'
1229 || pLongName->Buffer[cwcDirName - 1] == '/'))
1230 cwcDirName--;
1231
1232 /*
1233 * Match it against known DLLs.
1234 */
1235 pImage->pszName = NULL;
1236 for (uint32_t i = 0; i < RT_ELEMENTS(g_apszSupNtVpAllowedDlls); i++)
1237 if (supHardNtVpAreNamesEqual(g_apszSupNtVpAllowedDlls[i], pwszFilename))
1238 {
1239 pImage->pszName = g_apszSupNtVpAllowedDlls[i];
1240 pImage->fDll = true;
1241
1242#ifndef VBOX_PERMIT_VISUAL_STUDIO_PROFILING
1243 /* The directory name must match the one we've got for System32. */
1244 if ( ( cwcDirName * sizeof(WCHAR) != g_System32NtPath.UniStr.Length
1245 || suplibHardenedMemComp(pLongName->Buffer, g_System32NtPath.UniStr.Buffer, cwcDirName * sizeof(WCHAR)) )
1246# ifdef VBOX_PERMIT_MORE
1247 && ( pImage->pszName[0] != 'a'
1248 || pImage->pszName[1] != 'c'
1249 || !supHardViIsAppPatchDir(pLongName->Buffer, pLongName->Length / sizeof(WCHAR)) )
1250# endif
1251 )
1252 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_NON_SYSTEM32_DLL,
1253 "Expected %ls to be loaded from %ls.",
1254 pLongName->Buffer, g_System32NtPath.UniStr.Buffer);
1255# ifdef VBOX_PERMIT_MORE
1256 if (g_uNtVerCombined < SUP_NT_VER_W70 && i >= VBOX_PERMIT_MORE_FIRST_IDX)
1257 pImage->pszName = NULL; /* hard limit: user32.dll is unwanted prior to w7. */
1258# endif
1259
1260#endif /* VBOX_PERMIT_VISUAL_STUDIO_PROFILING */
1261 break;
1262 }
1263 if (!pImage->pszName)
1264 {
1265 /*
1266 * Not a known DLL, is it a known executable?
1267 */
1268 for (uint32_t i = 0; i < RT_ELEMENTS(g_apszSupNtVpAllowedVmExes); i++)
1269 if (supHardNtVpAreNamesEqual(g_apszSupNtVpAllowedVmExes[i], pwszFilename))
1270 {
1271 pImage->pszName = g_apszSupNtVpAllowedVmExes[i];
1272 pImage->fDll = false;
1273 break;
1274 }
1275 }
1276 if (!pImage->pszName)
1277 {
1278 /*
1279 * Unknown image.
1280 *
1281 * If we're cleaning up a child process, we can unmap the offending
1282 * DLL... Might have interesting side effects, or at least interesting
1283 * as in "may you live in interesting times".
1284 */
1285#ifdef IN_RING3
1286 if ( pMemInfo->AllocationBase == pMemInfo->BaseAddress
1287 && pThis->enmKind == SUPHARDNTVPKIND_CHILD_PURIFICATION)
1288 {
1289 SUP_DPRINTF(("supHardNtVpScanVirtualMemory: Unmapping image mem at %p (%p LB %#zx) - '%ls'\n",
1290 pMemInfo->AllocationBase, pMemInfo->BaseAddress, pMemInfo->RegionSize, pwszFilename));
1291 NTSTATUS rcNt = NtUnmapViewOfSection(pThis->hProcess, pMemInfo->AllocationBase);
1292 if (NT_SUCCESS(rcNt))
1293 return VINF_OBJECT_DESTROYED;
1294 pThis->cFixes++;
1295 SUP_DPRINTF(("supHardNtVpScanVirtualMemory: NtUnmapViewOfSection(,%p) failed: %#x\n", pMemInfo->AllocationBase, rcNt));
1296 }
1297#endif
1298 /*
1299 * Special error message if we can.
1300 */
1301 if ( pMemInfo->AllocationBase == pMemInfo->BaseAddress
1302 && ( supHardNtVpAreNamesEqual("sysfer.dll", pwszFilename)
1303 || supHardNtVpAreNamesEqual("sysfer32.dll", pwszFilename)
1304 || supHardNtVpAreNamesEqual("sysfer64.dll", pwszFilename)
1305 || supHardNtVpAreNamesEqual("sysfrethunk.dll", pwszFilename)) )
1306 {
1307 supHardNtVpSetInfo2(pThis, VERR_SUP_VP_SYSFER_DLL,
1308 "Found %ls at %p - This is probably part of Symantec Endpoint Protection. \n"
1309 "You or your admin need to add and exception to the Application and Device Control (ADC) "
1310 "component (or disable it) to prevent ADC from injecting itself into the VirtualBox VM processes. "
1311 "See http://www.symantec.com/connect/articles/creating-application-control-exclusions-symantec-endpoint-protection-121"
1312 , pLongName->Buffer, pMemInfo->BaseAddress);
1313 return pThis->rcResult = VERR_SUP_VP_SYSFER_DLL; /* Try make sure this is what the user sees first! */
1314 }
1315 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_NOT_KNOWN_DLL_OR_EXE,
1316 "Unknown image file %ls at %p.", pLongName->Buffer, pMemInfo->BaseAddress);
1317 }
1318
1319 /*
1320 * Checks for multiple mappings of the same DLL but with different image file paths.
1321 */
1322 uint32_t i = pThis->cImages;
1323 while (i-- > 1)
1324 if (pImage->pszName == pThis->aImages[i].pszName)
1325 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_DUPLICATE_DLL_MAPPING,
1326 "Duplicate image entries for %s: %ls and %ls",
1327 pImage->pszName, pImage->Name.UniStr.Buffer, pThis->aImages[i].Name.UniStr.Buffer);
1328
1329 /*
1330 * Since it's a new image, we expect to be at the start of the mapping now.
1331 */
1332 if (pMemInfo->AllocationBase != pMemInfo->BaseAddress)
1333 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_IMAGE_MAPPING_BASE_ERROR,
1334 "Invalid AllocationBase/BaseAddress for %s: %p vs %p.",
1335 pImage->pszName, pMemInfo->AllocationBase, pMemInfo->BaseAddress);
1336
1337 /*
1338 * Check for size/rva overflow.
1339 */
1340 if (pMemInfo->RegionSize >= _2G)
1341 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_TOO_LARGE_REGION,
1342 "Region 0 of image %s is too large: %p.", pImage->pszName, pMemInfo->RegionSize);
1343
1344 /*
1345 * Fill in details from the memory info.
1346 */
1347 pImage->uImageBase = (uintptr_t)pMemInfo->AllocationBase;
1348 pImage->cbImage = pMemInfo->RegionSize;
1349 pImage->pCacheEntry= NULL;
1350 pImage->cRegions = 1;
1351 pImage->aRegions[0].uRva = 0;
1352 pImage->aRegions[0].cb = (uint32_t)pMemInfo->RegionSize;
1353 pImage->aRegions[0].fProt = pMemInfo->Protect;
1354
1355 if (suplibHardenedStrCmp(pImage->pszName, "ntdll.dll") == 0)
1356 pImage->fNtCreateSectionPatch = true;
1357 else if (suplibHardenedStrCmp(pImage->pszName, "apisetschema.dll") == 0)
1358 pImage->fApiSetSchemaOnlySection1 = true; /** @todo Check the ApiSetMap field in the PEB. */
1359#ifdef VBOX_PERMIT_MORE
1360 else if (suplibHardenedStrCmp(pImage->pszName, "acres.dll") == 0)
1361 pImage->f32bitResourceDll = true;
1362#endif
1363
1364 return VINF_SUCCESS;
1365}
1366
1367
1368/**
1369 * Records an additional memory region for an image.
1370 *
1371 * @returns VBox status code.
1372 * @param pThis The process scanning state structure.
1373 * @param pImage The image.
1374 * @param pMemInfo The memory information for the region.
1375 */
1376static int supHardNtVpAddRegion(PSUPHNTVPSTATE pThis, PSUPHNTVPIMAGE pImage, PMEMORY_BASIC_INFORMATION pMemInfo)
1377{
1378 /*
1379 * Make sure the base address matches.
1380 */
1381 if (pImage->uImageBase != (uintptr_t)pMemInfo->AllocationBase)
1382 return supHardNtVpSetInfo2(pThis, VERR_SUPLIB_NT_PROCESS_UNTRUSTED_3,
1383 "Base address mismatch for %s: have %p, found %p for region %p LB %#zx.",
1384 pImage->pszName, pImage->uImageBase, pMemInfo->AllocationBase,
1385 pMemInfo->BaseAddress, pMemInfo->RegionSize);
1386
1387 /*
1388 * Check for size and rva overflows.
1389 */
1390 uintptr_t uRva = (uintptr_t)pMemInfo->BaseAddress - pImage->uImageBase;
1391 if (pMemInfo->RegionSize >= _2G)
1392 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_TOO_LARGE_REGION,
1393 "Region %u of image %s is too large: %p/%p.", pImage->pszName, pMemInfo->RegionSize, uRva);
1394 if (uRva >= _2G)
1395 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_TOO_HIGH_REGION_RVA,
1396 "Region %u of image %s is too high: %p/%p.", pImage->pszName, pMemInfo->RegionSize, uRva);
1397
1398
1399 /*
1400 * Record the region.
1401 */
1402 uint32_t iRegion = pImage->cRegions;
1403 if (iRegion + 1 >= RT_ELEMENTS(pImage->aRegions))
1404 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_TOO_MANY_IMAGE_REGIONS,
1405 "Too many regions for %s.", pImage->pszName);
1406 pImage->aRegions[iRegion].uRva = (uint32_t)uRva;
1407 pImage->aRegions[iRegion].cb = (uint32_t)pMemInfo->RegionSize;
1408 pImage->aRegions[iRegion].fProt = pMemInfo->Protect;
1409 pImage->cbImage = pImage->aRegions[iRegion].uRva + pImage->aRegions[iRegion].cb;
1410 pImage->cRegions++;
1411 pImage->fApiSetSchemaOnlySection1 = false;
1412
1413 return VINF_SUCCESS;
1414}
1415
1416
1417#ifdef IN_RING3
1418/**
1419 * Frees (or replaces) executable memory of allocation type private.
1420 *
1421 * @returns True if nothing really bad happen, false if to quit ASAP because we
1422 * killed the process being scanned.
1423 * @param pThis The process scanning state structure. Details
1424 * about images are added to this.
1425 * @param hProcess The process to verify.
1426 * @param pMemInfo The information we've got on this private
1427 * executable memory.
1428 */
1429static bool supHardNtVpFreeOrReplacePrivateExecMemory(PSUPHNTVPSTATE pThis, HANDLE hProcess,
1430 MEMORY_BASIC_INFORMATION const *pMemInfo)
1431{
1432 NTSTATUS rcNt;
1433
1434 /*
1435 * Try figure the entire allocation size. Free/Alloc may fail otherwise.
1436 */
1437 PVOID pvFree = pMemInfo->AllocationBase;
1438 SIZE_T cbFree = pMemInfo->RegionSize + ((uintptr_t)pMemInfo->BaseAddress - (uintptr_t)pMemInfo->AllocationBase);
1439 for (;;)
1440 {
1441 SIZE_T cbActual = 0;
1442 MEMORY_BASIC_INFORMATION MemInfo2 = { 0, 0, 0, 0, 0, 0, 0 };
1443 uintptr_t uPtrNext = (uintptr_t)pvFree + cbFree;
1444 rcNt = g_pfnNtQueryVirtualMemory(hProcess,
1445 (void const *)uPtrNext,
1446 MemoryBasicInformation,
1447 &MemInfo2,
1448 sizeof(MemInfo2),
1449 &cbActual);
1450 if (!NT_SUCCESS(rcNt))
1451 break;
1452 if (pMemInfo->AllocationBase != MemInfo2.AllocationBase)
1453 break;
1454 if (MemInfo2.RegionSize == 0)
1455 break;
1456 cbFree += MemInfo2.RegionSize;
1457 }
1458 SUP_DPRINTF(("supHardNtVpFreeOrReplacePrivateExecMemory: %s exec mem at %p (LB %#zx, %p LB %#zx)\n",
1459 pThis->fFlags & SUPHARDNTVP_F_EXEC_ALLOC_REPLACE_WITH_RW ? "Replacing" : "Freeing",
1460 pvFree, cbFree, pMemInfo->BaseAddress, pMemInfo->RegionSize));
1461
1462 /*
1463 * In the BSOD workaround mode, we need to make a copy of the memory before
1464 * freeing it.
1465 */
1466 uintptr_t uCopySrc = (uintptr_t)pvFree;
1467 size_t cbCopy = 0;
1468 void *pvCopy = NULL;
1469 if (pThis->fFlags & SUPHARDNTVP_F_EXEC_ALLOC_REPLACE_WITH_RW)
1470 {
1471 cbCopy = cbFree;
1472 pvCopy = RTMemAllocZ(cbCopy);
1473 if (!pvCopy)
1474 {
1475 supHardNtVpSetInfo2(pThis, VERR_SUP_VP_REPLACE_VIRTUAL_MEMORY_FAILED, "RTMemAllocZ(%#zx) failed", cbCopy);
1476 return true;
1477 }
1478
1479 rcNt = supHardNtVpReadMem(hProcess, uCopySrc, pvCopy, cbCopy);
1480 if (!NT_SUCCESS(rcNt))
1481 supHardNtVpSetInfo2(pThis, VERR_SUP_VP_REPLACE_VIRTUAL_MEMORY_FAILED,
1482 "Error reading data from original alloc: %#x (%p LB %#zx)", rcNt, uCopySrc, cbCopy, rcNt);
1483 supR3HardenedLogFlush();
1484 }
1485
1486 /*
1487 * Free the memory.
1488 */
1489 for (uint32_t i = 0; i < 10; i++)
1490 {
1491 PVOID pvFreeInOut = pvFree;
1492 SIZE_T cbFreeInOut = 0;
1493 rcNt = NtFreeVirtualMemory(hProcess, &pvFreeInOut, &cbFreeInOut, MEM_RELEASE);
1494 if (NT_SUCCESS(rcNt))
1495 {
1496 SUP_DPRINTF(("supHardNtVpFreeOrReplacePrivateExecMemory: Free attempt #1 succeeded: %#x [%p/%p LB 0/%#zx]\n",
1497 rcNt, pvFree, pvFreeInOut, cbFreeInOut));
1498 supR3HardenedLogFlush();
1499 }
1500 else
1501 {
1502 SUP_DPRINTF(("supHardNtVpFreeOrReplacePrivateExecMemory: Free attempt #1 failed: %#x [%p LB 0]\n", rcNt, pvFree));
1503 supR3HardenedLogFlush();
1504 pvFreeInOut = pvFree;
1505 cbFreeInOut = cbFree;
1506 rcNt = NtFreeVirtualMemory(hProcess, &pvFreeInOut, &cbFreeInOut, MEM_RELEASE);
1507 if (NT_SUCCESS(rcNt))
1508 {
1509 SUP_DPRINTF(("supHardNtVpFreeOrReplacePrivateExecMemory: Free attempt #2 succeeded: %#x [%p/%p LB %#zx/%#zx]\n",
1510 rcNt, pvFree, pvFreeInOut, cbFree, cbFreeInOut));
1511 supR3HardenedLogFlush();
1512 }
1513 else
1514 {
1515 SUP_DPRINTF(("supHardNtVpFreeOrReplacePrivateExecMemory: Free attempt #2 failed: %#x [%p LB %#zx]\n",
1516 rcNt, pvFree, cbFree));
1517 supR3HardenedLogFlush();
1518 pvFreeInOut = pMemInfo->BaseAddress;
1519 cbFreeInOut = pMemInfo->RegionSize;
1520 rcNt = NtFreeVirtualMemory(hProcess, &pvFreeInOut, &cbFreeInOut, MEM_RELEASE);
1521 if (NT_SUCCESS(rcNt))
1522 {
1523 pvFree = pMemInfo->BaseAddress;
1524 cbFree = pMemInfo->RegionSize;
1525 SUP_DPRINTF(("supHardNtVpFreeOrReplacePrivateExecMemory: Free attempt #3 succeeded [%p LB %#zx]\n",
1526 pvFree, cbFree));
1527 supR3HardenedLogFlush();
1528 }
1529 else
1530 supHardNtVpSetInfo2(pThis, VERR_SUP_VP_FREE_VIRTUAL_MEMORY_FAILED,
1531 "NtFreeVirtualMemory [%p LB %#zx and %p LB %#zx] failed: %#x",
1532 pvFree, cbFree, pMemInfo->BaseAddress, pMemInfo->RegionSize, rcNt);
1533 }
1534 }
1535
1536 /*
1537 * Query the region again, redo the free operation if there's still memory there.
1538 */
1539 if (!NT_SUCCESS(rcNt))
1540 break;
1541 SIZE_T cbActual = 0;
1542 MEMORY_BASIC_INFORMATION MemInfo3 = { 0, 0, 0, 0, 0, 0, 0 };
1543 NTSTATUS rcNt2 = g_pfnNtQueryVirtualMemory(hProcess, pvFree, MemoryBasicInformation,
1544 &MemInfo3, sizeof(MemInfo3), &cbActual);
1545 if (!NT_SUCCESS(rcNt2))
1546 break;
1547 SUP_DPRINTF(("supHardNtVpFreeOrReplacePrivateExecMemory: QVM after free %u: [%p]/%p LB %#zx s=%#x ap=%#x rp=%#p\n",
1548 i, MemInfo3.AllocationBase, MemInfo3.BaseAddress, MemInfo3.RegionSize, MemInfo3.State,
1549 MemInfo3.AllocationProtect, MemInfo3.Protect));
1550 supR3HardenedLogFlush();
1551 if (MemInfo3.State == MEM_FREE || !(pThis->fFlags & SUPHARDNTVP_F_EXEC_ALLOC_REPLACE_WITH_RW))
1552 break;
1553 NtYieldExecution();
1554 SUP_DPRINTF(("supHardNtVpFreeOrReplacePrivateExecMemory: Retrying free...\n"));
1555 supR3HardenedLogFlush();
1556 }
1557
1558 /*
1559 * Restore memory as non-executable - Kludge for Trend Micro sakfile.sys
1560 * and Digital Guardian dgmaster.sys BSODs.
1561 */
1562 if (NT_SUCCESS(rcNt) && (pThis->fFlags & SUPHARDNTVP_F_EXEC_ALLOC_REPLACE_WITH_RW))
1563 {
1564 PVOID pvAlloc = pvFree;
1565 SIZE_T cbAlloc = cbFree;
1566 rcNt = NtAllocateVirtualMemory(hProcess, &pvAlloc, 0, &cbAlloc, MEM_COMMIT, PAGE_READWRITE);
1567 if (!NT_SUCCESS(rcNt))
1568 {
1569 supHardNtVpSetInfo2(pThis, VERR_SUP_VP_REPLACE_VIRTUAL_MEMORY_FAILED,
1570 "NtAllocateVirtualMemory (%p LB %#zx) failed with rcNt=%#x allocating "
1571 "replacement memory for working around buggy protection software. "
1572 "See VBoxStartup.log for more details",
1573 pvAlloc, cbFree, rcNt);
1574 supR3HardenedLogFlush();
1575 NtTerminateProcess(hProcess, VERR_SUP_VP_REPLACE_VIRTUAL_MEMORY_FAILED);
1576 return false;
1577 }
1578
1579 if ( (uintptr_t)pvFree < (uintptr_t)pvAlloc
1580 || (uintptr_t)pvFree + cbFree > (uintptr_t)pvAlloc + cbFree)
1581 {
1582 supHardNtVpSetInfo2(pThis, VERR_SUP_VP_REPLACE_VIRTUAL_MEMORY_FAILED,
1583 "We wanted NtAllocateVirtualMemory to get us %p LB %#zx, but it returned %p LB %#zx.",
1584 pMemInfo->BaseAddress, pMemInfo->RegionSize, pvFree, cbFree, rcNt);
1585 supR3HardenedLogFlush();
1586 NtTerminateProcess(hProcess, VERR_SUP_VP_REPLACE_VIRTUAL_MEMORY_FAILED);
1587 return false;
1588 }
1589
1590 /*
1591 * Copy what we can, considering the 2nd free attempt.
1592 */
1593 uint8_t *pbDst = (uint8_t *)pvFree;
1594 size_t cbDst = cbFree;
1595 uint8_t *pbSrc = (uint8_t *)pvCopy;
1596 size_t cbSrc = cbCopy;
1597 if ((uintptr_t)pbDst != uCopySrc)
1598 {
1599 if ((uintptr_t)pbDst > uCopySrc)
1600 {
1601 uintptr_t cbAdj = (uintptr_t)pbDst - uCopySrc;
1602 pbSrc += cbAdj;
1603 cbSrc -= cbAdj;
1604 }
1605 else
1606 {
1607 uintptr_t cbAdj = uCopySrc - (uintptr_t)pbDst;
1608 pbDst += cbAdj;
1609 cbDst -= cbAdj;
1610 }
1611 }
1612 if (cbSrc > cbDst)
1613 cbSrc = cbDst;
1614
1615 SIZE_T cbWritten;
1616 rcNt = NtWriteVirtualMemory(hProcess, pbDst, pbSrc, cbSrc, &cbWritten);
1617 if (NT_SUCCESS(rcNt))
1618 {
1619 SUP_DPRINTF(("supHardNtVpFreeOrReplacePrivateExecMemory: Restored the exec memory as non-exec.\n"));
1620 supR3HardenedLogFlush();
1621 }
1622 else
1623 {
1624 supHardNtVpSetInfo2(pThis, VERR_SUP_VP_FREE_VIRTUAL_MEMORY_FAILED,
1625 "NtWriteVirtualMemory (%p LB %#zx) failed: %#x",
1626 pMemInfo->BaseAddress, pMemInfo->RegionSize, rcNt);
1627 supR3HardenedLogFlush();
1628 NtTerminateProcess(hProcess, VERR_SUP_VP_REPLACE_VIRTUAL_MEMORY_FAILED);
1629 return false;
1630 }
1631 }
1632 if (pvCopy)
1633 RTMemFree(pvCopy);
1634 return true;
1635}
1636#endif /* IN_RING3 */
1637
1638
1639/**
1640 * Scans the virtual memory of the process.
1641 *
1642 * This collects the locations of DLLs and the EXE, and verifies that executable
1643 * memory is only associated with these. May trash pThis->abMemory.
1644 *
1645 * @returns VBox status code.
1646 * @param pThis The process scanning state structure. Details
1647 * about images are added to this.
1648 * @param hProcess The process to verify.
1649 */
1650static int supHardNtVpScanVirtualMemory(PSUPHNTVPSTATE pThis, HANDLE hProcess)
1651{
1652 SUP_DPRINTF(("supHardNtVpScanVirtualMemory: enmKind=%s\n",
1653 pThis->enmKind == SUPHARDNTVPKIND_VERIFY_ONLY ? "VERIFY_ONLY" :
1654 pThis->enmKind == SUPHARDNTVPKIND_CHILD_PURIFICATION ? "CHILD_PURIFICATION" : "SELF_PURIFICATION"));
1655
1656 uint32_t cXpExceptions = 0;
1657 uintptr_t cbAdvance = 0;
1658 uintptr_t uPtrWhere = 0;
1659#ifdef VBOX_PERMIT_VERIFIER_DLL
1660 for (uint32_t i = 0; i < 10240; i++)
1661#else
1662 for (uint32_t i = 0; i < 1024; i++)
1663#endif
1664 {
1665 SIZE_T cbActual = 0;
1666 MEMORY_BASIC_INFORMATION MemInfo = { 0, 0, 0, 0, 0, 0, 0 };
1667 NTSTATUS rcNt = g_pfnNtQueryVirtualMemory(hProcess,
1668 (void const *)uPtrWhere,
1669 MemoryBasicInformation,
1670 &MemInfo,
1671 sizeof(MemInfo),
1672 &cbActual);
1673 if (!NT_SUCCESS(rcNt))
1674 {
1675 if (rcNt == STATUS_INVALID_PARAMETER)
1676 return pThis->rcResult;
1677 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_NT_QI_VIRTUAL_MEMORY_ERROR,
1678 "NtQueryVirtualMemory failed for %p: %#x", uPtrWhere, rcNt);
1679 }
1680
1681 /*
1682 * Record images.
1683 */
1684 if ( MemInfo.Type == SEC_IMAGE
1685 || MemInfo.Type == SEC_PROTECTED_IMAGE
1686 || MemInfo.Type == (SEC_IMAGE | SEC_PROTECTED_IMAGE))
1687 {
1688 uint32_t iImg = pThis->cImages;
1689 rcNt = g_pfnNtQueryVirtualMemory(hProcess,
1690 (void const *)uPtrWhere,
1691 MemorySectionName,
1692 &pThis->aImages[iImg].Name,
1693 sizeof(pThis->aImages[iImg].Name) - sizeof(WCHAR),
1694 &cbActual);
1695 if (!NT_SUCCESS(rcNt))
1696 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_NT_QI_VIRTUAL_MEMORY_NM_ERROR,
1697 "NtQueryVirtualMemory/MemorySectionName failed for %p: %#x", uPtrWhere, rcNt);
1698 pThis->aImages[iImg].Name.UniStr.Buffer[pThis->aImages[iImg].Name.UniStr.Length / sizeof(WCHAR)] = '\0';
1699 SUP_DPRINTF((MemInfo.AllocationBase == MemInfo.BaseAddress
1700 ? " *%p-%p %#06x/%#06x %#09x %ls\n"
1701 : " %p-%p %#06x/%#06x %#09x %ls\n",
1702 MemInfo.BaseAddress, (uintptr_t)MemInfo.BaseAddress + MemInfo.RegionSize - 1, MemInfo.Protect,
1703 MemInfo.AllocationProtect, MemInfo.Type, pThis->aImages[iImg].Name.UniStr.Buffer));
1704
1705 /* New or existing image? */
1706 bool fNew = true;
1707 uint32_t iSearch = iImg;
1708 while (iSearch-- > 0)
1709 if (supHardNtVpAreUniStringsEqual(&pThis->aImages[iSearch].Name.UniStr, &pThis->aImages[iImg].Name.UniStr))
1710 {
1711 int rc = supHardNtVpAddRegion(pThis, &pThis->aImages[iSearch], &MemInfo);
1712 if (RT_FAILURE(rc))
1713 return rc;
1714 fNew = false;
1715 break;
1716 }
1717 else if (pThis->aImages[iSearch].uImageBase == (uintptr_t)MemInfo.AllocationBase)
1718 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_NT_MAPPING_NAME_CHANGED,
1719 "Unexpected base address match");
1720
1721 if (fNew)
1722 {
1723 int rc = supHardNtVpNewImage(pThis, &pThis->aImages[iImg], &MemInfo);
1724 if (RT_SUCCESS(rc))
1725 {
1726 if (rc != VINF_OBJECT_DESTROYED)
1727 {
1728 pThis->cImages++;
1729 if (pThis->cImages >= RT_ELEMENTS(pThis->aImages))
1730 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_TOO_MANY_DLLS_LOADED,
1731 "Internal error: aImages is full.\n");
1732 }
1733 }
1734#ifdef IN_RING3 /* Continue and add more information if unknown DLLs are found. */
1735 else if (rc != VERR_SUP_VP_NOT_KNOWN_DLL_OR_EXE && rc != VERR_SUP_VP_NON_SYSTEM32_DLL)
1736 return rc;
1737#else
1738 else
1739 return rc;
1740#endif
1741 }
1742 }
1743 /*
1744 * XP, W2K3: Ignore the CSRSS read-only region as best we can.
1745 */
1746 else if ( (MemInfo.Protect & (PAGE_EXECUTE | PAGE_EXECUTE_READ | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY))
1747 == PAGE_EXECUTE_READ
1748 && cXpExceptions == 0
1749 && (uintptr_t)MemInfo.BaseAddress >= UINT32_C(0x78000000)
1750 /* && MemInfo.BaseAddress == pPeb->ReadOnlySharedMemoryBase */
1751 && g_uNtVerCombined < SUP_MAKE_NT_VER_SIMPLE(6, 0) )
1752 {
1753 cXpExceptions++;
1754 SUP_DPRINTF((" %p-%p %#06x/%#06x %#09x XP CSRSS read-only region\n", MemInfo.BaseAddress,
1755 (uintptr_t)MemInfo.BaseAddress - MemInfo.RegionSize - 1, MemInfo.Protect,
1756 MemInfo.AllocationProtect, MemInfo.Type));
1757 }
1758 /*
1759 * Executable memory?
1760 */
1761#ifndef VBOX_PERMIT_VISUAL_STUDIO_PROFILING
1762 else if (MemInfo.Protect & (PAGE_EXECUTE | PAGE_EXECUTE_READ | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY))
1763 {
1764 SUP_DPRINTF((MemInfo.AllocationBase == MemInfo.BaseAddress
1765 ? " *%p-%p %#06x/%#06x %#09x !!\n"
1766 : " %p-%p %#06x/%#06x %#09x !!\n",
1767 MemInfo.BaseAddress, (uintptr_t)MemInfo.BaseAddress - MemInfo.RegionSize - 1,
1768 MemInfo.Protect, MemInfo.AllocationProtect, MemInfo.Type));
1769# ifdef IN_RING3
1770 if (pThis->enmKind == SUPHARDNTVPKIND_CHILD_PURIFICATION)
1771 {
1772 /*
1773 * Free any private executable memory (sysplant.sys allocates executable memory).
1774 */
1775 if (MemInfo.Type == MEM_PRIVATE)
1776 {
1777 if (!supHardNtVpFreeOrReplacePrivateExecMemory(pThis, hProcess, &MemInfo))
1778 break;
1779 }
1780 /*
1781 * Unmap mapped memory, failing that, drop exec privileges.
1782 */
1783 else if (MemInfo.Type == MEM_MAPPED)
1784 {
1785 SUP_DPRINTF(("supHardNtVpScanVirtualMemory: Unmapping exec mem at %p (%p/%p LB %#zx)\n",
1786 uPtrWhere, MemInfo.AllocationBase, MemInfo.BaseAddress, MemInfo.RegionSize));
1787 rcNt = NtUnmapViewOfSection(hProcess, MemInfo.AllocationBase);
1788 if (!NT_SUCCESS(rcNt))
1789 {
1790 PVOID pvCopy = MemInfo.BaseAddress;
1791 SIZE_T cbCopy = MemInfo.RegionSize;
1792 NTSTATUS rcNt2 = NtProtectVirtualMemory(hProcess, &pvCopy, &cbCopy, PAGE_NOACCESS, NULL);
1793 if (!NT_SUCCESS(rcNt2))
1794 rcNt2 = NtProtectVirtualMemory(hProcess, &pvCopy, &cbCopy, PAGE_READONLY, NULL);
1795 if (!NT_SUCCESS(rcNt2))
1796 supHardNtVpSetInfo2(pThis, VERR_SUP_VP_UNMAP_AND_PROTECT_FAILED,
1797 "NtUnmapViewOfSection (%p/%p LB %#zx) failed: %#x (%#x)",
1798 MemInfo.AllocationBase, MemInfo.BaseAddress, MemInfo.RegionSize, rcNt, rcNt2);
1799 }
1800 }
1801 else
1802 supHardNtVpSetInfo2(pThis, VERR_SUP_VP_UNKOWN_MEM_TYPE,
1803 "Unknown executable memory type %#x at %p/%p LB %#zx",
1804 MemInfo.Type, MemInfo.AllocationBase, MemInfo.BaseAddress, MemInfo.RegionSize);
1805 pThis->cFixes++;
1806 }
1807 else
1808# endif /* IN_RING3 */
1809 supHardNtVpSetInfo2(pThis, VERR_SUP_VP_FOUND_EXEC_MEMORY,
1810 "Found executable memory at %p (%p LB %#zx): type=%#x prot=%#x state=%#x aprot=%#x abase=%p",
1811 uPtrWhere, MemInfo.BaseAddress, MemInfo.RegionSize, MemInfo.Type, MemInfo.Protect,
1812 MemInfo.State, MemInfo.AllocationBase, MemInfo.AllocationProtect);
1813
1814# ifndef IN_RING3
1815 if (RT_FAILURE(pThis->rcResult))
1816 return pThis->rcResult;
1817# endif
1818 /* Continue add more information about the problematic process. */
1819 }
1820#endif /* VBOX_PERMIT_VISUAL_STUDIO_PROFILING */
1821 else
1822 SUP_DPRINTF((MemInfo.AllocationBase == MemInfo.BaseAddress
1823 ? " *%p-%p %#06x/%#06x %#09x\n"
1824 : " %p-%p %#06x/%#06x %#09x\n",
1825 MemInfo.BaseAddress, (uintptr_t)MemInfo.BaseAddress - MemInfo.RegionSize - 1,
1826 MemInfo.Protect, MemInfo.AllocationProtect, MemInfo.Type));
1827
1828 /*
1829 * Advance.
1830 */
1831 cbAdvance = MemInfo.RegionSize;
1832 if (uPtrWhere + cbAdvance <= uPtrWhere)
1833 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_EMPTY_REGION_TOO_LARGE,
1834 "Empty region at %p.", uPtrWhere);
1835 uPtrWhere += MemInfo.RegionSize;
1836 }
1837
1838 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_TOO_MANY_MEMORY_REGIONS,
1839 "Too many virtual memory regions.\n");
1840}
1841
1842
1843/**
1844 * Verifies the loader image, i.e. check cryptographic signatures if present.
1845 *
1846 * @returns VBox status code.
1847 * @param pEntry The loader cache entry.
1848 * @param pwszName The filename to use in error messages.
1849 * @param pErrInfo Where to return extened error information.
1850 */
1851DECLHIDDEN(int) supHardNtLdrCacheEntryVerify(PSUPHNTLDRCACHEENTRY pEntry, PCRTUTF16 pwszName, PRTERRINFO pErrInfo)
1852{
1853 int rc = VINF_SUCCESS;
1854 if (!pEntry->fVerified)
1855 {
1856 rc = supHardenedWinVerifyImageByLdrMod(pEntry->hLdrMod, pwszName, pEntry->pNtViRdr,
1857 false /*fAvoidWinVerifyTrust*/, NULL /*pfWinVerifyTrust*/, pErrInfo);
1858 pEntry->fVerified = RT_SUCCESS(rc);
1859 }
1860 return rc;
1861}
1862
1863
1864/**
1865 * Allocates a image bits buffer and calls RTLdrGetBits on them.
1866 *
1867 * An assumption here is that there won't ever be concurrent use of the cache.
1868 * It's currently 104% single threaded, non-reentrant. Thus, we can't reuse the
1869 * pbBits allocation.
1870 *
1871 * @returns VBox status code
1872 * @param pEntry The loader cache entry.
1873 * @param ppbBits Where to return the pointer to the allocation.
1874 * @param uBaseAddress The image base address, see RTLdrGetBits.
1875 * @param pfnGetImport Import getter, see RTLdrGetBits.
1876 * @param pvUser The user argument for @a pfnGetImport.
1877 * @param pErrInfo Where to return extened error information.
1878 */
1879DECLHIDDEN(int) supHardNtLdrCacheEntryGetBits(PSUPHNTLDRCACHEENTRY pEntry, uint8_t **ppbBits,
1880 RTLDRADDR uBaseAddress, PFNRTLDRIMPORT pfnGetImport, void *pvUser,
1881 PRTERRINFO pErrInfo)
1882{
1883 int rc;
1884
1885 /*
1886 * First time around we have to allocate memory before we can get the image bits.
1887 */
1888 if (!pEntry->pbBits)
1889 {
1890 size_t cbBits = RTLdrSize(pEntry->hLdrMod);
1891 if (cbBits >= _1M*32U)
1892 return supHardNtVpSetInfo1(pErrInfo, VERR_SUP_VP_IMAGE_TOO_BIG, "Image %s is too large: %zu bytes (%#zx).",
1893 pEntry->pszName, cbBits, cbBits);
1894
1895 pEntry->pbBits = (uint8_t *)RTMemAllocZ(cbBits);
1896 if (!pEntry->pbBits)
1897 return supHardNtVpSetInfo1(pErrInfo, VERR_SUP_VP_NO_MEMORY, "Failed to allocate %zu bytes for image %s.",
1898 cbBits, pEntry->pszName);
1899
1900 pEntry->fValidBits = false; /* paranoia */
1901
1902 rc = RTLdrGetBits(pEntry->hLdrMod, pEntry->pbBits, uBaseAddress, pfnGetImport, pvUser);
1903 if (RT_FAILURE(rc))
1904 return supHardNtVpSetInfo1(pErrInfo, VERR_SUP_VP_NO_MEMORY, "RTLdrGetBits failed on image %s: %Rrc",
1905 pEntry->pszName, rc);
1906 pEntry->uImageBase = uBaseAddress;
1907 pEntry->fValidBits = pfnGetImport == NULL;
1908
1909 }
1910 /*
1911 * Cache hit? No?
1912 *
1913 * Note! We cannot currently cache image bits for images with imports as we
1914 * don't control the way they're resolved. Fortunately, NTDLL and
1915 * the VM process images all have no imports.
1916 */
1917 else if ( !pEntry->fValidBits
1918 || pEntry->uImageBase != uBaseAddress
1919 || pfnGetImport)
1920 {
1921 pEntry->fValidBits = false;
1922
1923 rc = RTLdrGetBits(pEntry->hLdrMod, pEntry->pbBits, uBaseAddress, pfnGetImport, pvUser);
1924 if (RT_FAILURE(rc))
1925 return supHardNtVpSetInfo1(pErrInfo, VERR_SUP_VP_NO_MEMORY, "RTLdrGetBits failed on image %s: %Rrc",
1926 pEntry->pszName, rc);
1927 pEntry->uImageBase = uBaseAddress;
1928 pEntry->fValidBits = pfnGetImport == NULL;
1929 }
1930
1931 *ppbBits = pEntry->pbBits;
1932 return VINF_SUCCESS;
1933}
1934
1935
1936/**
1937 * Frees all resources associated with a cache entry and wipes the members
1938 * clean.
1939 *
1940 * @param pEntry The entry to delete.
1941 */
1942static void supHardNTLdrCacheDeleteEntry(PSUPHNTLDRCACHEENTRY pEntry)
1943{
1944 if (pEntry->pbBits)
1945 {
1946 RTMemFree(pEntry->pbBits);
1947 pEntry->pbBits = NULL;
1948 }
1949
1950 if (pEntry->hLdrMod != NIL_RTLDRMOD)
1951 {
1952 RTLdrClose(pEntry->hLdrMod);
1953 pEntry->hLdrMod = NIL_RTLDRMOD;
1954 pEntry->pNtViRdr = NULL;
1955 }
1956 else if (pEntry->pNtViRdr)
1957 {
1958 pEntry->pNtViRdr->Core.pfnDestroy(&pEntry->pNtViRdr->Core);
1959 pEntry->pNtViRdr = NULL;
1960 }
1961
1962 if (pEntry->hFile)
1963 {
1964 NtClose(pEntry->hFile);
1965 pEntry->hFile = NULL;
1966 }
1967
1968 pEntry->pszName = NULL;
1969 pEntry->fVerified = false;
1970 pEntry->fValidBits = false;
1971 pEntry->uImageBase = 0;
1972}
1973
1974#ifdef IN_RING3
1975
1976/**
1977 * Flushes the cache.
1978 *
1979 * This is called from one of two points in the hardened main code, first is
1980 * after respawning and the second is when we open the vboxdrv device for
1981 * unrestricted access.
1982 */
1983DECLHIDDEN(void) supR3HardenedWinFlushLoaderCache(void)
1984{
1985 uint32_t i = g_cSupNtVpLdrCacheEntries;
1986 while (i-- > 0)
1987 supHardNTLdrCacheDeleteEntry(&g_aSupNtVpLdrCacheEntries[i]);
1988 g_cSupNtVpLdrCacheEntries = 0;
1989}
1990
1991
1992/**
1993 * Searches the cache for a loader image.
1994 *
1995 * @returns Pointer to the cache entry if found, NULL if not.
1996 * @param pszName The name (from g_apszSupNtVpAllowedVmExes or
1997 * g_apszSupNtVpAllowedDlls).
1998 */
1999static PSUPHNTLDRCACHEENTRY supHardNtLdrCacheLookupEntry(const char *pszName)
2000{
2001 /*
2002 * Since the caller is supplying us a pszName from one of the two tables,
2003 * we can dispense with string compare and simply compare string pointers.
2004 */
2005 uint32_t i = g_cSupNtVpLdrCacheEntries;
2006 while (i-- > 0)
2007 if (g_aSupNtVpLdrCacheEntries[i].pszName == pszName)
2008 return &g_aSupNtVpLdrCacheEntries[i];
2009 return NULL;
2010}
2011
2012#endif /* IN_RING3 */
2013
2014static int supHardNtLdrCacheNewEntry(PSUPHNTLDRCACHEENTRY pEntry, const char *pszName, PUNICODE_STRING pUniStrPath,
2015 bool fDll, bool f32bitResourceDll, PRTERRINFO pErrInfo)
2016{
2017 /*
2018 * Open the image file.
2019 */
2020 HANDLE hFile = RTNT_INVALID_HANDLE_VALUE;
2021 IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
2022
2023 OBJECT_ATTRIBUTES ObjAttr;
2024 InitializeObjectAttributes(&ObjAttr, pUniStrPath, OBJ_CASE_INSENSITIVE, NULL /*hRootDir*/, NULL /*pSecDesc*/);
2025#ifdef IN_RING0
2026 ObjAttr.Attributes |= OBJ_KERNEL_HANDLE;
2027#endif
2028
2029 NTSTATUS rcNt = NtCreateFile(&hFile,
2030 GENERIC_READ | SYNCHRONIZE,
2031 &ObjAttr,
2032 &Ios,
2033 NULL /* Allocation Size*/,
2034 FILE_ATTRIBUTE_NORMAL,
2035 FILE_SHARE_READ,
2036 FILE_OPEN,
2037 FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT,
2038 NULL /*EaBuffer*/,
2039 0 /*EaLength*/);
2040 if (NT_SUCCESS(rcNt))
2041 rcNt = Ios.Status;
2042 if (!NT_SUCCESS(rcNt))
2043 return supHardNtVpSetInfo1(pErrInfo, VERR_SUP_VP_IMAGE_FILE_OPEN_ERROR,
2044 "Error opening image for scanning: %#x (name %ls)", rcNt, pUniStrPath->Buffer);
2045
2046 /*
2047 * Figure out validation flags we'll be using and create the reader
2048 * for this image.
2049 */
2050 uint32_t fFlags = fDll
2051 ? SUPHNTVI_F_TRUSTED_INSTALLER_OWNER | SUPHNTVI_F_ALLOW_CAT_FILE_VERIFICATION
2052 : SUPHNTVI_F_REQUIRE_BUILD_CERT;
2053 if (f32bitResourceDll)
2054 fFlags |= SUPHNTVI_F_IGNORE_ARCHITECTURE;
2055
2056 PSUPHNTVIRDR pNtViRdr;
2057 int rc = supHardNtViRdrCreate(hFile, pUniStrPath->Buffer, fFlags, &pNtViRdr);
2058 if (RT_FAILURE(rc))
2059 {
2060 NtClose(hFile);
2061 return rc;
2062 }
2063
2064 /*
2065 * Finally, open the image with the loader
2066 */
2067 RTLDRMOD hLdrMod;
2068 RTLDRARCH enmArch = fFlags & SUPHNTVI_F_RC_IMAGE ? RTLDRARCH_X86_32 : RTLDRARCH_HOST;
2069 if (fFlags & SUPHNTVI_F_IGNORE_ARCHITECTURE)
2070 enmArch = RTLDRARCH_WHATEVER;
2071 rc = RTLdrOpenWithReader(&pNtViRdr->Core, RTLDR_O_FOR_VALIDATION, enmArch, &hLdrMod, pErrInfo);
2072 if (RT_FAILURE(rc))
2073 return supHardNtVpAddInfo1(pErrInfo, rc, "RTLdrOpenWithReader failed: %Rrc (Image='%ls').",
2074 rc, pUniStrPath->Buffer);
2075
2076 /*
2077 * Fill in the cache entry.
2078 */
2079 pEntry->pszName = pszName;
2080 pEntry->hLdrMod = hLdrMod;
2081 pEntry->pNtViRdr = pNtViRdr;
2082 pEntry->hFile = hFile;
2083 pEntry->pbBits = NULL;
2084 pEntry->fVerified = false;
2085 pEntry->fValidBits = false;
2086 pEntry->uImageBase = ~(uintptr_t)0;
2087
2088#ifdef IN_SUP_HARDENED_R3
2089 /*
2090 * Log the image timestamp when in the hardened exe.
2091 */
2092 uint64_t uTimestamp = 0;
2093 rc = RTLdrQueryProp(hLdrMod, RTLDRPROP_TIMESTAMP_SECONDS, &uTimestamp, sizeof(uint64_t));
2094 SUP_DPRINTF(("%s: timestamp %#llx (rc=%Rrc)\n", pszName, uTimestamp, rc));
2095#endif
2096
2097 return VINF_SUCCESS;
2098}
2099
2100#ifdef IN_RING3
2101/**
2102 * Opens a loader cache entry.
2103 *
2104 * Currently this is only used by the import code for getting NTDLL.
2105 *
2106 * @returns VBox status code.
2107 * @param pszName The DLL name. Must be one from the
2108 * g_apszSupNtVpAllowedDlls array.
2109 * @param ppEntry Where to return the entry we've opened/found.
2110 * @param pErrInfo Optional buffer where to return additional error
2111 * information.
2112 */
2113DECLHIDDEN(int) supHardNtLdrCacheOpen(const char *pszName, PSUPHNTLDRCACHEENTRY *ppEntry, PRTERRINFO pErrInfo)
2114{
2115 /*
2116 * Locate the dll.
2117 */
2118 uint32_t i = 0;
2119 while ( i < RT_ELEMENTS(g_apszSupNtVpAllowedDlls)
2120 && strcmp(pszName, g_apszSupNtVpAllowedDlls[i]))
2121 i++;
2122 if (i >= RT_ELEMENTS(g_apszSupNtVpAllowedDlls))
2123 return VERR_FILE_NOT_FOUND;
2124 pszName = g_apszSupNtVpAllowedDlls[i];
2125
2126 /*
2127 * Try the cache.
2128 */
2129 *ppEntry = supHardNtLdrCacheLookupEntry(pszName);
2130 if (*ppEntry)
2131 return VINF_SUCCESS;
2132
2133 /*
2134 * Not in the cache, so open it.
2135 * Note! We cannot assume that g_System32NtPath has been initialized at this point.
2136 */
2137 if (g_cSupNtVpLdrCacheEntries >= RT_ELEMENTS(g_aSupNtVpLdrCacheEntries))
2138 return VERR_INTERNAL_ERROR_3;
2139
2140 static WCHAR s_wszSystem32[] = L"\\SystemRoot\\System32\\";
2141 WCHAR wszPath[64];
2142 memcpy(wszPath, s_wszSystem32, sizeof(s_wszSystem32));
2143 RTUtf16CatAscii(wszPath, sizeof(wszPath), pszName);
2144
2145 UNICODE_STRING UniStr;
2146 UniStr.Buffer = wszPath;
2147 UniStr.Length = (USHORT)(RTUtf16Len(wszPath) * sizeof(WCHAR));
2148 UniStr.MaximumLength = UniStr.Length + sizeof(WCHAR);
2149
2150 int rc = supHardNtLdrCacheNewEntry(&g_aSupNtVpLdrCacheEntries[g_cSupNtVpLdrCacheEntries], pszName, &UniStr,
2151 true /*fDll*/, false /*f32bitResourceDll*/, pErrInfo);
2152 if (RT_SUCCESS(rc))
2153 {
2154 *ppEntry = &g_aSupNtVpLdrCacheEntries[g_cSupNtVpLdrCacheEntries];
2155 g_cSupNtVpLdrCacheEntries++;
2156 return VINF_SUCCESS;
2157 }
2158 return rc;
2159}
2160#endif /* IN_RING3 */
2161
2162
2163/**
2164 * Opens all the images with the IPRT loader, setting both, hFile, pNtViRdr and
2165 * hLdrMod for each image.
2166 *
2167 * @returns VBox status code.
2168 * @param pThis The process scanning state structure.
2169 */
2170static int supHardNtVpOpenImages(PSUPHNTVPSTATE pThis)
2171{
2172 unsigned i = pThis->cImages;
2173 while (i-- > 0)
2174 {
2175 PSUPHNTVPIMAGE pImage = &pThis->aImages[i];
2176
2177#ifdef IN_RING3
2178 /*
2179 * Try the cache first.
2180 */
2181 pImage->pCacheEntry = supHardNtLdrCacheLookupEntry(pImage->pszName);
2182 if (pImage->pCacheEntry)
2183 continue;
2184
2185 /*
2186 * Not in the cache, so load it into the cache.
2187 */
2188 if (g_cSupNtVpLdrCacheEntries >= RT_ELEMENTS(g_aSupNtVpLdrCacheEntries))
2189 return supHardNtVpSetInfo2(pThis, VERR_INTERNAL_ERROR_3, "Loader cache overflow.");
2190 pImage->pCacheEntry = &g_aSupNtVpLdrCacheEntries[g_cSupNtVpLdrCacheEntries];
2191#else
2192 /*
2193 * In ring-0 we don't have a cache at the moment (resource reasons), so
2194 * we have a static cache entry in each image structure that we use instead.
2195 */
2196 pImage->pCacheEntry = &pImage->CacheEntry;
2197#endif
2198
2199 int rc = supHardNtLdrCacheNewEntry(pImage->pCacheEntry, pImage->pszName, &pImage->Name.UniStr,
2200 pImage->fDll, pImage->f32bitResourceDll, pThis->pErrInfo);
2201 if (RT_FAILURE(rc))
2202 return rc;
2203#ifdef IN_RING3
2204 g_cSupNtVpLdrCacheEntries++;
2205#endif
2206 }
2207
2208 return VINF_SUCCESS;
2209}
2210
2211
2212/**
2213 * Check the integrity of the executable of the process.
2214 *
2215 * @returns VBox status code.
2216 * @param pThis The process scanning state structure. Details
2217 * about images are added to this. The hProcess
2218 * member holds the handle to the process that is
2219 * to be verified.
2220 */
2221static int supHardNtVpCheckExe(PSUPHNTVPSTATE pThis)
2222{
2223 /*
2224 * Make sure there is exactly one executable image.
2225 */
2226 unsigned cExecs = 0;
2227 unsigned iExe = ~0U;
2228 unsigned i = pThis->cImages;
2229 while (i-- > 0)
2230 {
2231 if (!pThis->aImages[i].fDll)
2232 {
2233 cExecs++;
2234 iExe = i;
2235 }
2236 }
2237 if (cExecs == 0)
2238 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_NO_FOUND_NO_EXE_MAPPING,
2239 "No executable mapping found in the virtual address space.");
2240 if (cExecs != 1)
2241 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_FOUND_MORE_THAN_ONE_EXE_MAPPING,
2242 "Found more than one executable mapping in the virtual address space.");
2243 PSUPHNTVPIMAGE pImage = &pThis->aImages[iExe];
2244
2245 /*
2246 * Check that it matches the executable image of the process.
2247 */
2248 int rc;
2249 ULONG cbUniStr = sizeof(UNICODE_STRING) + RTPATH_MAX * sizeof(RTUTF16);
2250 PUNICODE_STRING pUniStr = (PUNICODE_STRING)RTMemAllocZ(cbUniStr);
2251 if (!pUniStr)
2252 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_NO_MEMORY,
2253 "Error allocating %zu bytes for process name.", cbUniStr);
2254 ULONG cbIgn = 0;
2255 NTSTATUS rcNt = NtQueryInformationProcess(pThis->hProcess, ProcessImageFileName, pUniStr, cbUniStr - sizeof(WCHAR), &cbIgn);
2256 if (NT_SUCCESS(rcNt))
2257 {
2258 if (supHardNtVpAreUniStringsEqual(pUniStr, &pImage->Name.UniStr))
2259 rc = VINF_SUCCESS;
2260 else
2261 {
2262 pUniStr->Buffer[pUniStr->Length / sizeof(WCHAR)] = '\0';
2263 rc = supHardNtVpSetInfo2(pThis, VERR_SUP_VP_EXE_VS_PROC_NAME_MISMATCH,
2264 "Process image name does not match the exectuable we found: %ls vs %ls.",
2265 pUniStr->Buffer, pImage->Name.UniStr.Buffer);
2266 }
2267 }
2268 else
2269 rc = supHardNtVpSetInfo2(pThis, VERR_SUP_VP_NT_QI_PROCESS_NM_ERROR,
2270 "NtQueryInformationProcess/ProcessImageFileName failed: %#x", rcNt);
2271 RTMemFree(pUniStr);
2272 if (RT_FAILURE(rc))
2273 return rc;
2274
2275 /*
2276 * Validate the signing of the executable image.
2277 * This will load the fDllCharecteristics and fImageCharecteristics members we use below.
2278 */
2279 rc = supHardNtVpVerifyImage(pThis, pImage);
2280 if (RT_FAILURE(rc))
2281 return rc;
2282
2283 /*
2284 * Check linking requirements.
2285 * This query is only available using the current process pseudo handle on
2286 * older windows versions. The cut-off seems to be Vista.
2287 */
2288 SECTION_IMAGE_INFORMATION ImageInfo;
2289 rcNt = NtQueryInformationProcess(pThis->hProcess, ProcessImageInformation, &ImageInfo, sizeof(ImageInfo), NULL);
2290 if (!NT_SUCCESS(rcNt))
2291 {
2292 if ( rcNt == STATUS_INVALID_PARAMETER
2293 && g_uNtVerCombined < SUP_NT_VER_VISTA
2294 && pThis->hProcess != NtCurrentProcess() )
2295 return VINF_SUCCESS;
2296 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_NT_QI_PROCESS_IMG_INFO_ERROR,
2297 "NtQueryInformationProcess/ProcessImageInformation failed: %#x hProcess=%#x",
2298 rcNt, pThis->hProcess);
2299 }
2300 if ( !(ImageInfo.DllCharacteristics & IMAGE_DLLCHARACTERISTICS_FORCE_INTEGRITY))
2301 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_EXE_MISSING_FORCE_INTEGRITY,
2302 "EXE DllCharacteristics=%#x, expected IMAGE_DLLCHARACTERISTICS_FORCE_INTEGRITY to be set.",
2303 ImageInfo.DllCharacteristics);
2304 if (!(ImageInfo.DllCharacteristics & IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE))
2305 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_EXE_MISSING_DYNAMIC_BASE,
2306 "EXE DllCharacteristics=%#x, expected IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE to be set.",
2307 ImageInfo.DllCharacteristics);
2308 if (!(ImageInfo.DllCharacteristics & IMAGE_DLLCHARACTERISTICS_NX_COMPAT))
2309 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_EXE_MISSING_NX_COMPAT,
2310 "EXE DllCharacteristics=%#x, expected IMAGE_DLLCHARACTERISTICS_NX_COMPAT to be set.",
2311 ImageInfo.DllCharacteristics);
2312
2313 if (pImage->fDllCharecteristics != ImageInfo.DllCharacteristics)
2314 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_DLL_CHARECTERISTICS_MISMATCH,
2315 "EXE Info.DllCharacteristics=%#x fDllCharecteristics=%#x.",
2316 ImageInfo.DllCharacteristics, pImage->fDllCharecteristics);
2317
2318 if (pImage->fImageCharecteristics != ImageInfo.ImageCharacteristics)
2319 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_DLL_CHARECTERISTICS_MISMATCH,
2320 "EXE Info.ImageCharacteristics=%#x fImageCharecteristics=%#x.",
2321 ImageInfo.ImageCharacteristics, pImage->fImageCharecteristics);
2322
2323 return VINF_SUCCESS;
2324}
2325
2326
2327/**
2328 * Check the integrity of the DLLs found in the process.
2329 *
2330 * @returns VBox status code.
2331 * @param pThis The process scanning state structure. Details
2332 * about images are added to this. The hProcess
2333 * member holds the handle to the process that is
2334 * to be verified.
2335 */
2336static int supHardNtVpCheckDlls(PSUPHNTVPSTATE pThis)
2337{
2338 /*
2339 * Check for duplicate entries (paranoia).
2340 */
2341 uint32_t i = pThis->cImages;
2342 while (i-- > 1)
2343 {
2344 const char *pszName = pThis->aImages[i].pszName;
2345 uint32_t j = i;
2346 while (j-- > 0)
2347 if (pThis->aImages[j].pszName == pszName)
2348 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_DUPLICATE_DLL_MAPPING,
2349 "Duplicate image entries for %s: %ls and %ls",
2350 pszName, pThis->aImages[i].Name.UniStr.Buffer, pThis->aImages[j].Name.UniStr.Buffer);
2351 }
2352
2353 /*
2354 * Check that both ntdll and kernel32 are present.
2355 * ASSUMES the entries in g_apszSupNtVpAllowedDlls are all lower case.
2356 */
2357 uint32_t iNtDll = UINT32_MAX;
2358 uint32_t iKernel32 = UINT32_MAX;
2359 i = pThis->cImages;
2360 while (i-- > 0)
2361 if (suplibHardenedStrCmp(pThis->aImages[i].pszName, "ntdll.dll") == 0)
2362 iNtDll = i;
2363 else if (suplibHardenedStrCmp(pThis->aImages[i].pszName, "kernel32.dll") == 0)
2364 iKernel32 = i;
2365 if (iNtDll == UINT32_MAX)
2366 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_NO_NTDLL_MAPPING,
2367 "The process has no NTDLL.DLL.");
2368 if (iKernel32 == UINT32_MAX && pThis->enmKind == SUPHARDNTVPKIND_SELF_PURIFICATION)
2369 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_NO_KERNEL32_MAPPING,
2370 "The process has no KERNEL32.DLL.");
2371 else if (iKernel32 != UINT32_MAX && pThis->enmKind == SUPHARDNTVPKIND_CHILD_PURIFICATION)
2372 return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_KERNEL32_ALREADY_MAPPED,
2373 "The process already has KERNEL32.DLL loaded.");
2374
2375 /*
2376 * Verify that the DLLs are correctly signed (by MS).
2377 */
2378 i = pThis->cImages;
2379 while (i-- > 0)
2380 {
2381 int rc = supHardNtVpVerifyImage(pThis, &pThis->aImages[i]);
2382 if (RT_FAILURE(rc))
2383 return rc;
2384 }
2385
2386 return VINF_SUCCESS;
2387}
2388
2389
2390/**
2391 * Verifies the given process.
2392 *
2393 * The following requirements are checked:
2394 * - The process only has one thread, the calling thread.
2395 * - The process has no debugger attached.
2396 * - The executable image of the process is verified to be signed with
2397 * certificate known to this code at build time.
2398 * - The executable image is one of a predefined set.
2399 * - The process has only a very limited set of system DLLs loaded.
2400 * - The system DLLs signatures check out fine.
2401 * - The only executable memory in the process belongs to the system DLLs and
2402 * the executable image.
2403 *
2404 * @returns VBox status code.
2405 * @param hProcess The process to verify.
2406 * @param hThread A thread in the process (the caller).
2407 * @param enmKind The kind of process verification to perform.
2408 * @param fFlags Valid combination of SUPHARDNTVP_F_XXX flags.
2409 * @param pErrInfo Pointer to error info structure. Optional.
2410 * @param pcFixes Where to return the number of fixes made during
2411 * purification. Optional.
2412 */
2413DECLHIDDEN(int) supHardenedWinVerifyProcess(HANDLE hProcess, HANDLE hThread, SUPHARDNTVPKIND enmKind, uint32_t fFlags,
2414 uint32_t *pcFixes, PRTERRINFO pErrInfo)
2415{
2416 if (pcFixes)
2417 *pcFixes = 0;
2418
2419 /*
2420 * Some basic checks regarding threads and debuggers. We don't need
2421 * allocate any state memory for these.
2422 */
2423 int rc = VINF_SUCCESS;
2424 if (enmKind != SUPHARDNTVPKIND_CHILD_PURIFICATION)
2425 rc = supHardNtVpThread(hProcess, hThread, pErrInfo);
2426 if (RT_SUCCESS(rc))
2427 rc = supHardNtVpDebugger(hProcess, pErrInfo);
2428 if (RT_SUCCESS(rc))
2429 {
2430 /*
2431 * Allocate and initialize memory for the state.
2432 */
2433 PSUPHNTVPSTATE pThis = (PSUPHNTVPSTATE)RTMemAllocZ(sizeof(*pThis));
2434 if (pThis)
2435 {
2436 pThis->enmKind = enmKind;
2437 pThis->fFlags = fFlags;
2438 pThis->rcResult = VINF_SUCCESS;
2439 pThis->hProcess = hProcess;
2440 pThis->pErrInfo = pErrInfo;
2441
2442 /*
2443 * Perform the verification.
2444 */
2445 rc = supHardNtVpScanVirtualMemory(pThis, hProcess);
2446 if (RT_SUCCESS(rc))
2447 rc = supHardNtVpOpenImages(pThis);
2448 if (RT_SUCCESS(rc))
2449 rc = supHardNtVpCheckExe(pThis);
2450 if (RT_SUCCESS(rc))
2451 rc = supHardNtVpCheckDlls(pThis);
2452
2453 if (pcFixes)
2454 *pcFixes = pThis->cFixes;
2455
2456 /*
2457 * Clean up the state.
2458 */
2459#ifdef IN_RING0
2460 for (uint32_t i = 0; i < pThis->cImages; i++)
2461 supHardNTLdrCacheDeleteEntry(&pThis->aImages[i].CacheEntry);
2462#endif
2463 RTMemFree(pThis);
2464 }
2465 else
2466 rc = supHardNtVpSetInfo1(pErrInfo, VERR_SUP_VP_NO_MEMORY_STATE,
2467 "Failed to allocate %zu bytes for state structures.", sizeof(*pThis));
2468 }
2469 return rc;
2470}
2471
Note: See TracBrowser for help on using the repository browser.

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