VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/Support/SUPLibLdr.cpp@ 85526

Last change on this file since 85526 was 85526, checked in by vboxsync, 4 years ago

SUP: Forgot to initialize SUPLDRSEG::fUnused to zero. bugref:9801

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 40.5 KB
Line 
1/* $Id: SUPLibLdr.cpp 85526 2020-07-29 15:50:36Z vboxsync $ */
2/** @file
3 * VirtualBox Support Library - Loader related bits.
4 */
5
6/*
7 * Copyright (C) 2006-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#define LOG_GROUP LOG_GROUP_SUP
32#include <VBox/sup.h>
33#include <VBox/err.h>
34#include <VBox/param.h>
35#include <VBox/log.h>
36#include <VBox/VBoxTpG.h>
37
38#include <iprt/assert.h>
39#include <iprt/alloc.h>
40#include <iprt/alloca.h>
41#include <iprt/ldr.h>
42#include <iprt/asm.h>
43#include <iprt/mp.h>
44#include <iprt/cpuset.h>
45#include <iprt/thread.h>
46#include <iprt/process.h>
47#include <iprt/path.h>
48#include <iprt/string.h>
49#include <iprt/env.h>
50#include <iprt/rand.h>
51#include <iprt/x86.h>
52
53#include "SUPDrvIOC.h"
54#include "SUPLibInternal.h"
55
56
57/*********************************************************************************************************************************
58* Defined Constants And Macros *
59*********************************************************************************************************************************/
60/** R0 VMM module name. */
61#define VMMR0_NAME "VMMR0"
62
63
64/*********************************************************************************************************************************
65* Structures and Typedefs *
66*********************************************************************************************************************************/
67typedef DECLCALLBACKTYPE(int, FNCALLVMMR0,(PVMR0 pVMR0, unsigned uOperation, void *pvArg));
68typedef FNCALLVMMR0 *PFNCALLVMMR0;
69
70
71/*********************************************************************************************************************************
72* Global Variables *
73*********************************************************************************************************************************/
74/** VMMR0 Load Address. */
75static RTR0PTR g_pvVMMR0 = NIL_RTR0PTR;
76
77
78/*********************************************************************************************************************************
79* Internal Functions *
80*********************************************************************************************************************************/
81static int supLoadModule(const char *pszFilename, const char *pszModule, const char *pszSrvReqHandler,
82 PRTERRINFO pErrInfo, void **ppvImageBase);
83static DECLCALLBACK(int) supLoadModuleResolveImport(RTLDRMOD hLdrMod, const char *pszModule, const char *pszSymbol,
84 unsigned uSymbol, RTUINTPTR *pValue, void *pvUser);
85
86
87SUPR3DECL(int) SUPR3LoadModule(const char *pszFilename, const char *pszModule, void **ppvImageBase, PRTERRINFO pErrInfo)
88{
89 /*
90 * Check that the module can be trusted.
91 */
92 int rc = SUPR3HardenedVerifyPlugIn(pszFilename, pErrInfo);
93 if (RT_SUCCESS(rc))
94 {
95 rc = supLoadModule(pszFilename, pszModule, NULL, pErrInfo, ppvImageBase);
96 if (RT_FAILURE(rc) && !RTErrInfoIsSet(pErrInfo))
97 RTErrInfoSetF(pErrInfo, rc, "SUPR3LoadModule: supLoadModule returned %Rrc", rc);
98 }
99 return rc;
100}
101
102
103SUPR3DECL(int) SUPR3LoadServiceModule(const char *pszFilename, const char *pszModule,
104 const char *pszSrvReqHandler, void **ppvImageBase)
105{
106 AssertPtrReturn(pszSrvReqHandler, VERR_INVALID_PARAMETER);
107
108 /*
109 * Check that the module can be trusted.
110 */
111 int rc = SUPR3HardenedVerifyPlugIn(pszFilename, NULL /*pErrInfo*/);
112 if (RT_SUCCESS(rc))
113 rc = supLoadModule(pszFilename, pszModule, pszSrvReqHandler, NULL /*pErrInfo*/, ppvImageBase);
114 else
115 LogRel(("SUPR3LoadServiceModule: Verification of \"%s\" failed, rc=%Rrc\n", pszFilename, rc));
116 return rc;
117}
118
119
120/**
121 * Argument package for supLoadModuleResolveImport.
122 */
123typedef struct SUPLDRRESIMPARGS
124{
125 const char *pszModule;
126 PRTERRINFO pErrInfo;
127} SUPLDRRESIMPARGS, *PSUPLDRRESIMPARGS;
128
129/**
130 * Resolve an external symbol during RTLdrGetBits().
131 *
132 * @returns VBox status code.
133 * @param hLdrMod The loader module handle.
134 * @param pszModule Module name.
135 * @param pszSymbol Symbol name, NULL if uSymbol should be used.
136 * @param uSymbol Symbol ordinal, ~0 if pszSymbol should be used.
137 * @param pValue Where to store the symbol value (address).
138 * @param pvUser User argument.
139 */
140static DECLCALLBACK(int) supLoadModuleResolveImport(RTLDRMOD hLdrMod, const char *pszModule,
141 const char *pszSymbol, unsigned uSymbol, RTUINTPTR *pValue, void *pvUser)
142{
143 NOREF(hLdrMod); NOREF(uSymbol);
144 AssertPtr(pValue);
145 AssertPtr(pvUser);
146 PSUPLDRRESIMPARGS pArgs = (PSUPLDRRESIMPARGS)pvUser;
147
148 /*
149 * Only SUPR0 and VMMR0.r0
150 */
151 if ( pszModule
152 && *pszModule
153 && strcmp(pszModule, "VBoxDrv.sys")
154 && strcmp(pszModule, "VMMR0.r0"))
155 {
156#if defined(RT_OS_WINDOWS) && 0 /* Useful for VMMR0 hacking, not for production use. See also SUPDrv-win.cpp */
157 if (strcmp(pszModule, "ntoskrnl.exe") == 0)
158 {
159 *pValue = 42; /* Non-zero so ring-0 can find the end of the IAT and exclude it when comparing. */
160 return VINF_SUCCESS;
161 }
162#endif
163 AssertMsgFailed(("%s is importing from %s! (expected 'SUPR0.dll' or 'VMMR0.r0', case-sensitive)\n", pArgs->pszModule, pszModule));
164 return RTErrInfoSetF(pArgs->pErrInfo, VERR_SYMBOL_NOT_FOUND,
165 "Unexpected import module '%s' in '%s'", pszModule, pArgs->pszModule);
166 }
167
168 /*
169 * No ordinals.
170 */
171 if (uSymbol != ~0U)
172 {
173 AssertMsgFailed(("%s is importing by ordinal (ord=%d)\n", pArgs->pszModule, uSymbol));
174 return RTErrInfoSetF(pArgs->pErrInfo, VERR_SYMBOL_NOT_FOUND,
175 "Unexpected ordinal import (%#x) in '%s'", uSymbol, pArgs->pszModule);
176 }
177
178 /*
179 * Lookup symbol.
180 */
181 /* Skip the 64-bit ELF import prefix first. */
182 /** @todo is this actually used??? */
183 if (!strncmp(pszSymbol, RT_STR_TUPLE("SUPR0$")))
184 pszSymbol += sizeof("SUPR0$") - 1;
185
186 /*
187 * Check the VMMR0.r0 module if loaded.
188 */
189 /** @todo call the SUPR3LoadModule caller.... */
190 /** @todo proper reference counting and such. */
191 if (g_pvVMMR0 != NIL_RTR0PTR)
192 {
193 void *pvValue;
194 if (!SUPR3GetSymbolR0((void *)g_pvVMMR0, pszSymbol, &pvValue))
195 {
196 *pValue = (uintptr_t)pvValue;
197 return VINF_SUCCESS;
198 }
199 }
200
201 /* iterate the function table. */
202 int c = g_pSupFunctions->u.Out.cFunctions;
203 PSUPFUNC pFunc = &g_pSupFunctions->u.Out.aFunctions[0];
204 while (c-- > 0)
205 {
206 if (!strcmp(pFunc->szName, pszSymbol))
207 {
208 *pValue = (uintptr_t)pFunc->pfn;
209 return VINF_SUCCESS;
210 }
211 pFunc++;
212 }
213
214 /*
215 * The GIP.
216 */
217 if ( pszSymbol
218 && g_pSUPGlobalInfoPage
219 && g_pSUPGlobalInfoPageR0
220 && !strcmp(pszSymbol, "g_SUPGlobalInfoPage")
221 )
222 {
223 *pValue = (uintptr_t)g_pSUPGlobalInfoPageR0;
224 return VINF_SUCCESS;
225 }
226
227 /*
228 * Symbols that are undefined by convention.
229 */
230#ifdef RT_OS_SOLARIS
231 static const char * const s_apszConvSyms[] =
232 {
233 "", "mod_getctl",
234 "", "mod_install",
235 "", "mod_remove",
236 "", "mod_info",
237 "", "mod_miscops",
238 };
239 for (unsigned i = 0; i < RT_ELEMENTS(s_apszConvSyms); i += 2)
240 {
241 if ( !RTStrCmp(s_apszConvSyms[i], pszModule)
242 && !RTStrCmp(s_apszConvSyms[i + 1], pszSymbol))
243 {
244 *pValue = ~(uintptr_t)0;
245 return VINF_SUCCESS;
246 }
247 }
248#endif
249
250 /*
251 * Despair.
252 */
253 c = g_pSupFunctions->u.Out.cFunctions;
254 pFunc = &g_pSupFunctions->u.Out.aFunctions[0];
255 while (c-- > 0)
256 {
257 RTAssertMsg2Weak("%d: %s\n", g_pSupFunctions->u.Out.cFunctions - c, pFunc->szName);
258 pFunc++;
259 }
260 RTAssertMsg2Weak("%s is importing %s which we couldn't find\n", pArgs->pszModule, pszSymbol);
261
262 AssertLogRelMsgFailed(("%s is importing %s which we couldn't find\n", pArgs->pszModule, pszSymbol));
263 if (g_uSupFakeMode)
264 {
265 *pValue = 0xdeadbeef;
266 return VINF_SUCCESS;
267 }
268 return RTErrInfoSetF(pArgs->pErrInfo, VERR_SYMBOL_NOT_FOUND,
269 "Unable to locate imported symbol '%s%s%s' for module '%s'",
270 pszModule ? pszModule : "",
271 pszModule && *pszModule ? "." : "",
272 pszSymbol,
273 pArgs->pszModule);
274}
275
276
277/** Argument package for supLoadModuleCalcSizeCB. */
278typedef struct SUPLDRCALCSIZEARGS
279{
280 size_t cbStrings;
281 uint32_t cSymbols;
282 size_t cbImage;
283} SUPLDRCALCSIZEARGS, *PSUPLDRCALCSIZEARGS;
284
285/**
286 * Callback used to calculate the image size.
287 * @return VINF_SUCCESS
288 */
289static DECLCALLBACK(int) supLoadModuleCalcSizeCB(RTLDRMOD hLdrMod, const char *pszSymbol, unsigned uSymbol, RTUINTPTR Value, void *pvUser)
290{
291 PSUPLDRCALCSIZEARGS pArgs = (PSUPLDRCALCSIZEARGS)pvUser;
292 if ( pszSymbol != NULL
293 && *pszSymbol
294 && Value <= pArgs->cbImage)
295 {
296 pArgs->cSymbols++;
297 pArgs->cbStrings += strlen(pszSymbol) + 1;
298 }
299 NOREF(hLdrMod); NOREF(uSymbol);
300 return VINF_SUCCESS;
301}
302
303
304/** Argument package for supLoadModuleCreateTabsCB. */
305typedef struct SUPLDRCREATETABSARGS
306{
307 size_t cbImage;
308 PSUPLDRSYM pSym;
309 char *pszBase;
310 char *psz;
311} SUPLDRCREATETABSARGS, *PSUPLDRCREATETABSARGS;
312
313/**
314 * Callback used to calculate the image size.
315 * @return VINF_SUCCESS
316 */
317static DECLCALLBACK(int) supLoadModuleCreateTabsCB(RTLDRMOD hLdrMod, const char *pszSymbol, unsigned uSymbol, RTUINTPTR Value, void *pvUser)
318{
319 PSUPLDRCREATETABSARGS pArgs = (PSUPLDRCREATETABSARGS)pvUser;
320 if ( pszSymbol != NULL
321 && *pszSymbol
322 && Value <= pArgs->cbImage)
323 {
324 pArgs->pSym->offSymbol = (uint32_t)Value;
325 pArgs->pSym->offName = pArgs->psz - pArgs->pszBase;
326 pArgs->pSym++;
327
328 size_t cbCopy = strlen(pszSymbol) + 1;
329 memcpy(pArgs->psz, pszSymbol, cbCopy);
330 pArgs->psz += cbCopy;
331 }
332 NOREF(hLdrMod); NOREF(uSymbol);
333 return VINF_SUCCESS;
334}
335
336
337/** Argument package for supLoadModuleCompileSegmentsCB. */
338typedef struct SUPLDRCOMPSEGTABARGS
339{
340 uint32_t uStartRva;
341 uint32_t uEndRva;
342 uint32_t fProt;
343 uint32_t iSegs;
344 uint32_t cSegsAlloc;
345 PSUPLDRSEG paSegs;
346 PRTERRINFO pErrInfo;
347} SUPLDRCOMPSEGTABARGS, *PSUPLDRCOMPSEGTABARGS;
348
349/**
350 * @callback_method_impl{FNRTLDRENUMSEGS,
351 * Compile list of segments with the same memory protection.}
352 */
353static DECLCALLBACK(int) supLoadModuleCompileSegmentsCB(RTLDRMOD hLdrMod, PCRTLDRSEG pSeg, void *pvUser)
354{
355 PSUPLDRCOMPSEGTABARGS pArgs = (PSUPLDRCOMPSEGTABARGS)pvUser;
356 AssertCompile(RTMEM_PROT_READ == SUPLDR_PROT_READ);
357 AssertCompile(RTMEM_PROT_WRITE == SUPLDR_PROT_WRITE);
358 AssertCompile(RTMEM_PROT_EXEC == SUPLDR_PROT_EXEC);
359 RT_NOREF(hLdrMod);
360
361 /* Ignore segments not part of the loaded image. */
362 if (pSeg->RVA == NIL_RTLDRADDR || pSeg->cbMapped == 0)
363 return VINF_SUCCESS;
364
365 /* We currently ASSUME that all relevant segments are in ascending RVA order. */
366 AssertReturn(pSeg->RVA >= pArgs->uEndRva,
367 RTERRINFO_LOG_REL_SET_F(pArgs->pErrInfo, VERR_BAD_EXE_FORMAT, "Out of order segment: %p LB %#zx #%.*s",
368 pSeg->RVA, pSeg->cb, pSeg->cchName, pSeg->pszName));
369
370 /* We ASSUME the cbMapped field is implemented. */
371 AssertReturn(pSeg->cbMapped != NIL_RTLDRADDR, VERR_INTERNAL_ERROR_2);
372 AssertReturn(pSeg->cbMapped < _1G, VERR_INTERNAL_ERROR_4);
373 uint32_t cbMapped = (uint32_t)pSeg->cbMapped;
374 AssertReturn(pSeg->RVA < _1G, VERR_INTERNAL_ERROR_3);
375 uint32_t uRvaSeg = (uint32_t)pSeg->RVA;
376 Log2(("supLoadModuleCompileSegmentsCB: %RTptr/%RTptr LB %RTptr/%RTptr prot %#x %s\n",
377 pSeg->LinkAddress, pSeg->RVA, pSeg->cbMapped, pSeg->cb, pSeg->fProt, pSeg->pszName));
378
379 /*
380 * If the protection is the same as the previous segment,
381 * just update uEndRva and continue.
382 */
383 uint32_t fProt = pSeg->fProt;
384#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
385 if (fProt & RTMEM_PROT_EXEC)
386 fProt |= fProt & RTMEM_PROT_READ;
387#endif
388 if (pSeg->fProt == pArgs->fProt)
389 {
390 pArgs->uEndRva = uRvaSeg + cbMapped;
391 Log2(("supLoadModuleCompileSegmentsCB: -> merged\n"));
392 return VINF_SUCCESS;
393 }
394
395 /*
396 * The protection differs, so commit current segment and start a new one.
397 * However, if the new segment and old segment share a page, this becomes
398 * a little more complicated...
399 */
400 if (pArgs->uStartRva < pArgs->uEndRva)
401 {
402 if (((pArgs->uEndRva - 1) >> PAGE_SHIFT) != (uRvaSeg >> PAGE_SHIFT))
403 {
404 /* No common page, so make the new segment start on a page boundrary. */
405 cbMapped += uRvaSeg & PAGE_OFFSET_MASK;
406 uRvaSeg &= ~(uint32_t)PAGE_OFFSET_MASK;
407 Assert(pArgs->uEndRva <= uRvaSeg);
408 Log2(("supLoadModuleCompileSegmentsCB: -> new, no common\n"));
409 }
410 else if ((fProt & pArgs->fProt) == fProt)
411 {
412 /* The current segment includes the memory protections of the
413 previous, so include the common page in it: */
414 uint32_t const cbCommon = PAGE_SIZE - (uRvaSeg & PAGE_OFFSET_MASK);
415 if (cbCommon >= cbMapped)
416 {
417 pArgs->uEndRva = uRvaSeg + cbMapped;
418 Log2(("supLoadModuleCompileSegmentsCB: -> merge, %#x common, upgrading prot to %#x\n", cbCommon, pArgs->fProt));
419 return VINF_SUCCESS; /* New segment was smaller than a page. */
420 }
421 cbMapped -= cbCommon;
422 uRvaSeg += cbCommon;
423 Assert(pArgs->uEndRva <= uRvaSeg);
424 Log2(("supLoadModuleCompileSegmentsCB: -> new, %#x common into previous\n", cbCommon));
425 }
426 else if ((fProt & pArgs->fProt) == pArgs->fProt)
427 {
428 /* The new segment includes the memory protections of the
429 previous, so include the common page in it: */
430 cbMapped += uRvaSeg & PAGE_OFFSET_MASK;
431 uRvaSeg &= ~(uint32_t)PAGE_OFFSET_MASK;
432 if (uRvaSeg == pArgs->uStartRva)
433 {
434 pArgs->fProt = fProt;
435 pArgs->uEndRva = uRvaSeg + cbMapped;
436 Log2(("supLoadModuleCompileSegmentsCB: -> upgrade current protection\n"));
437 return VINF_SUCCESS; /* Current segment was smaller than a page. */
438 }
439 Log2(("supLoadModuleCompileSegmentsCB: -> new, %#x common into new\n", (uint32_t)(pSeg->RVA & PAGE_OFFSET_MASK)));
440 }
441 else
442 {
443 /* Create a new segment for the common page with the combined protection. */
444 Log2(("supLoadModuleCompileSegmentsCB: -> its complicated...\n"));
445 pArgs->uEndRva &= ~(uint32_t)PAGE_OFFSET_MASK;
446 if (pArgs->uEndRva > pArgs->uStartRva)
447 {
448 Log2(("supLoadModuleCompileSegmentsCB: SUP Seg #%u: %#x LB %#x prot %#x\n",
449 pArgs->iSegs, pArgs->uStartRva, pArgs->uEndRva - pArgs->uStartRva, pArgs->fProt));
450 if (pArgs->paSegs)
451 {
452 AssertReturn(pArgs->iSegs < pArgs->cSegsAlloc, VERR_INTERNAL_ERROR_5);
453 pArgs->paSegs[pArgs->iSegs].off = pArgs->uStartRva;
454 pArgs->paSegs[pArgs->iSegs].cb = pArgs->uEndRva - pArgs->uStartRva;
455 pArgs->paSegs[pArgs->iSegs].fProt = pArgs->fProt;
456 pArgs->paSegs[pArgs->iSegs].fUnused = 0;
457 }
458 pArgs->iSegs++;
459 pArgs->uStartRva = pArgs->uEndRva;
460 }
461 pArgs->fProt |= fProt;
462
463 uint32_t const cbCommon = PAGE_SIZE - (uRvaSeg & PAGE_OFFSET_MASK);
464 if (cbCommon <= cbMapped)
465 {
466 fProt |= pArgs->fProt;
467 pArgs->uEndRva = uRvaSeg + cbMapped;
468 return VINF_SUCCESS; /* New segment was smaller than a page. */
469 }
470 cbMapped -= cbCommon;
471 uRvaSeg += cbCommon;
472 Assert(uRvaSeg - pArgs->uStartRva == PAGE_SIZE);
473 }
474
475 /* The current segment should end where the new one starts, no gaps. */
476 pArgs->uEndRva = uRvaSeg;
477
478 /* Emit the current segment */
479 Log2(("supLoadModuleCompileSegmentsCB: SUP Seg #%u: %#x LB %#x prot %#x\n",
480 pArgs->iSegs, pArgs->uStartRva, pArgs->uEndRva - pArgs->uStartRva, pArgs->fProt));
481 if (pArgs->paSegs)
482 {
483 AssertReturn(pArgs->iSegs < pArgs->cSegsAlloc, VERR_INTERNAL_ERROR_5);
484 pArgs->paSegs[pArgs->iSegs].off = pArgs->uStartRva;
485 pArgs->paSegs[pArgs->iSegs].cb = pArgs->uEndRva - pArgs->uStartRva;
486 pArgs->paSegs[pArgs->iSegs].fProt = pArgs->fProt;
487 pArgs->paSegs[pArgs->iSegs].fUnused = 0;
488 }
489 pArgs->iSegs++;
490 }
491 /* else: current segment is empty */
492
493 /* Start the new segment. */
494 Assert(!(uRvaSeg & PAGE_OFFSET_MASK));
495 pArgs->fProt = fProt;
496 pArgs->uStartRva = uRvaSeg;
497 pArgs->uEndRva = uRvaSeg + cbMapped;
498 return VINF_SUCCESS;
499}
500
501
502/**
503 * Worker for supLoadModule().
504 */
505static int supLoadModuleInner(RTLDRMOD hLdrMod, PSUPLDRLOAD pLoadReq, uint32_t cbImageWithEverything,
506 RTR0PTR uImageBase, size_t cbImage, const char *pszModule, const char *pszFilename,
507 bool fNativeLoader, bool fIsVMMR0, const char *pszSrvReqHandler,
508 uint32_t offSymTab, uint32_t cSymbols,
509 uint32_t offStrTab, size_t cbStrTab,
510 uint32_t offSegTab, uint32_t cSegments,
511 PRTERRINFO pErrInfo)
512{
513 /*
514 * Get the image bits.
515 */
516 SUPLDRRESIMPARGS Args = { pszModule, pErrInfo };
517 int rc = RTLdrGetBits(hLdrMod, &pLoadReq->u.In.abImage[0], uImageBase, supLoadModuleResolveImport, &Args);
518 if (RT_FAILURE(rc))
519 {
520 LogRel(("SUP: RTLdrGetBits failed for %s (%s). rc=%Rrc\n", pszModule, pszFilename, rc));
521 if (!RTErrInfoIsSet(pErrInfo))
522 RTErrInfoSetF(pErrInfo, rc, "RTLdrGetBits failed");
523 return rc;
524 }
525
526 /*
527 * Get the entry points.
528 */
529 RTUINTPTR VMMR0EntryFast = 0;
530 RTUINTPTR VMMR0EntryEx = 0;
531 RTUINTPTR SrvReqHandler = 0;
532 RTUINTPTR ModuleInit = 0;
533 RTUINTPTR ModuleTerm = 0;
534 const char *pszEp = NULL;
535 if (fIsVMMR0)
536 {
537 rc = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.abImage[0], uImageBase,
538 UINT32_MAX, pszEp = "VMMR0EntryFast", &VMMR0EntryFast);
539 if (RT_SUCCESS(rc))
540 rc = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.abImage[0], uImageBase,
541 UINT32_MAX, pszEp = "VMMR0EntryEx", &VMMR0EntryEx);
542 }
543 else if (pszSrvReqHandler)
544 rc = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.abImage[0], uImageBase,
545 UINT32_MAX, pszEp = pszSrvReqHandler, &SrvReqHandler);
546 if (RT_SUCCESS(rc))
547 {
548 int rc2 = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.abImage[0], uImageBase,
549 UINT32_MAX, pszEp = "ModuleInit", &ModuleInit);
550 if (RT_FAILURE(rc2))
551 ModuleInit = 0;
552
553 rc2 = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.abImage[0], uImageBase,
554 UINT32_MAX, pszEp = "ModuleTerm", &ModuleTerm);
555 if (RT_FAILURE(rc2))
556 ModuleTerm = 0;
557 }
558 if (RT_FAILURE(rc))
559 {
560 LogRel(("SUP: Failed to get entry point '%s' for %s (%s) rc=%Rrc\n", pszEp, pszModule, pszFilename, rc));
561 return RTErrInfoSetF(pErrInfo, rc, "Failed to resolve entry point '%s'", pszEp);
562 }
563
564 /*
565 * Create the symbol and string tables.
566 */
567 SUPLDRCREATETABSARGS CreateArgs;
568 CreateArgs.cbImage = cbImage;
569 CreateArgs.pSym = (PSUPLDRSYM)&pLoadReq->u.In.abImage[offSymTab];
570 CreateArgs.pszBase = (char *)&pLoadReq->u.In.abImage[offStrTab];
571 CreateArgs.psz = CreateArgs.pszBase;
572 rc = RTLdrEnumSymbols(hLdrMod, 0, NULL, 0, supLoadModuleCreateTabsCB, &CreateArgs);
573 if (RT_FAILURE(rc))
574 {
575 LogRel(("SUP: RTLdrEnumSymbols failed for %s (%s) rc=%Rrc\n", pszModule, pszFilename, rc));
576 return RTErrInfoSetF(pErrInfo, rc, "RTLdrEnumSymbols #2 failed");
577 }
578 AssertRelease((size_t)(CreateArgs.psz - CreateArgs.pszBase) <= cbStrTab);
579 AssertRelease((size_t)(CreateArgs.pSym - (PSUPLDRSYM)&pLoadReq->u.In.abImage[offSymTab]) <= cSymbols);
580
581 /*
582 * Create the segment table.
583 */
584 SUPLDRCOMPSEGTABARGS SegArgs;
585 SegArgs.uStartRva = 0;
586 SegArgs.uEndRva = 0;
587 SegArgs.fProt = RTMEM_PROT_READ;
588 SegArgs.iSegs = 0;
589 SegArgs.cSegsAlloc = cSegments;
590 SegArgs.paSegs = (PSUPLDRSEG)&pLoadReq->u.In.abImage[offSegTab];
591 SegArgs.pErrInfo = pErrInfo;
592 rc = RTLdrEnumSegments(hLdrMod, supLoadModuleCompileSegmentsCB, &SegArgs);
593 if (RT_FAILURE(rc))
594 {
595 LogRel(("SUP: RTLdrEnumSegments failed for %s (%s) rc=%Rrc\n", pszModule, pszFilename, rc));
596 return RTErrInfoSetF(pErrInfo, rc, "RTLdrEnumSegments #2 failed");
597 }
598 SegArgs.uEndRva = (uint32_t)cbImage;
599 AssertReturn(SegArgs.uEndRva == cbImage, VERR_OUT_OF_RANGE);
600 if (SegArgs.uEndRva > SegArgs.uStartRva)
601 {
602 SegArgs.paSegs[SegArgs.iSegs].off = SegArgs.uStartRva;
603 SegArgs.paSegs[SegArgs.iSegs].cb = SegArgs.uEndRva - SegArgs.uStartRva;
604 SegArgs.paSegs[SegArgs.iSegs].fProt = SegArgs.fProt;
605 SegArgs.paSegs[SegArgs.iSegs].fUnused = 0;
606 SegArgs.iSegs++;
607 }
608 for (uint32_t i = 0; i < SegArgs.iSegs; i++)
609 LogRel(("SUP: seg #%u: %c%c%c %#010RX32 LB %#010RX32\n", i, /** @todo LogRel2 */
610 SegArgs.paSegs[i].fProt & SUPLDR_PROT_READ ? 'R' : ' ',
611 SegArgs.paSegs[i].fProt & SUPLDR_PROT_WRITE ? 'W' : ' ',
612 SegArgs.paSegs[i].fProt & SUPLDR_PROT_EXEC ? 'X' : ' ',
613 SegArgs.paSegs[i].off, SegArgs.paSegs[i].cb));
614 AssertRelease(SegArgs.iSegs == cSegments);
615 AssertRelease(SegArgs.cSegsAlloc == cSegments);
616
617 /*
618 * Upload the image.
619 */
620 pLoadReq->Hdr.u32Cookie = g_u32Cookie;
621 pLoadReq->Hdr.u32SessionCookie = g_u32SessionCookie;
622 pLoadReq->Hdr.cbIn = SUP_IOCTL_LDR_LOAD_SIZE_IN(cbImageWithEverything);
623 pLoadReq->Hdr.cbOut = SUP_IOCTL_LDR_LOAD_SIZE_OUT;
624 pLoadReq->Hdr.fFlags = SUPREQHDR_FLAGS_MAGIC | SUPREQHDR_FLAGS_EXTRA_IN;
625 pLoadReq->Hdr.rc = VERR_INTERNAL_ERROR;
626
627 pLoadReq->u.In.pfnModuleInit = (RTR0PTR)ModuleInit;
628 pLoadReq->u.In.pfnModuleTerm = (RTR0PTR)ModuleTerm;
629 if (fIsVMMR0)
630 {
631 pLoadReq->u.In.eEPType = SUPLDRLOADEP_VMMR0;
632 pLoadReq->u.In.EP.VMMR0.pvVMMR0 = uImageBase;
633 pLoadReq->u.In.EP.VMMR0.pvVMMR0EntryFast= (RTR0PTR)VMMR0EntryFast;
634 pLoadReq->u.In.EP.VMMR0.pvVMMR0EntryEx = (RTR0PTR)VMMR0EntryEx;
635 }
636 else if (pszSrvReqHandler)
637 {
638 pLoadReq->u.In.eEPType = SUPLDRLOADEP_SERVICE;
639 pLoadReq->u.In.EP.Service.pfnServiceReq = (RTR0PTR)SrvReqHandler;
640 pLoadReq->u.In.EP.Service.apvReserved[0] = NIL_RTR0PTR;
641 pLoadReq->u.In.EP.Service.apvReserved[1] = NIL_RTR0PTR;
642 pLoadReq->u.In.EP.Service.apvReserved[2] = NIL_RTR0PTR;
643 }
644 else
645 pLoadReq->u.In.eEPType = SUPLDRLOADEP_NOTHING;
646 pLoadReq->u.In.offStrTab = offStrTab;
647 pLoadReq->u.In.cbStrTab = (uint32_t)cbStrTab;
648 AssertRelease(pLoadReq->u.In.cbStrTab == cbStrTab);
649 pLoadReq->u.In.cbImageBits = (uint32_t)cbImage;
650 pLoadReq->u.In.offSymbols = offSymTab;
651 pLoadReq->u.In.cSymbols = cSymbols;
652 pLoadReq->u.In.offSegments = offSegTab;
653 pLoadReq->u.In.cSegments = cSegments;
654 pLoadReq->u.In.cbImageWithEverything = cbImageWithEverything;
655 pLoadReq->u.In.pvImageBase = uImageBase;
656 if (!g_uSupFakeMode)
657 {
658 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LDR_LOAD, pLoadReq, SUP_IOCTL_LDR_LOAD_SIZE(cbImageWithEverything));
659 if (RT_SUCCESS(rc))
660 rc = pLoadReq->Hdr.rc;
661 else
662 LogRel(("SUP: SUP_IOCTL_LDR_LOAD ioctl for %s (%s) failed rc=%Rrc\n", pszModule, pszFilename, rc));
663 }
664 else
665 rc = VINF_SUCCESS;
666 if ( RT_SUCCESS(rc)
667 || rc == VERR_ALREADY_LOADED /* A competing process. */
668 )
669 {
670 LogRel(("SUP: Loaded %s (%s) at %#RKv - ModuleInit at %RKv and ModuleTerm at %RKv%s\n",
671 pszModule, pszFilename, uImageBase, (RTR0PTR)ModuleInit, (RTR0PTR)ModuleTerm,
672 fNativeLoader ? " using the native ring-0 loader" : ""));
673 if (fIsVMMR0)
674 {
675 g_pvVMMR0 = uImageBase;
676 LogRel(("SUP: VMMR0EntryEx located at %RKv and VMMR0EntryFast at %RKv\n", (RTR0PTR)VMMR0EntryEx, (RTR0PTR)VMMR0EntryFast));
677 }
678#ifdef RT_OS_WINDOWS
679 LogRel(("SUP: windbg> .reload /f %s=%#RKv\n", pszFilename, uImageBase));
680#endif
681 return VINF_SUCCESS;
682 }
683
684 /*
685 * Failed, bail out.
686 */
687 LogRel(("SUP: Loading failed for %s (%s) rc=%Rrc\n", pszModule, pszFilename, rc));
688 if ( pLoadReq->u.Out.uErrorMagic == SUPLDRLOAD_ERROR_MAGIC
689 && pLoadReq->u.Out.szError[0] != '\0')
690 {
691 LogRel(("SUP: %s\n", pLoadReq->u.Out.szError));
692 return RTErrInfoSet(pErrInfo, rc, pLoadReq->u.Out.szError);
693 }
694 return RTErrInfoSet(pErrInfo, rc, "SUP_IOCTL_LDR_LOAD failed");
695}
696
697
698/**
699 * Worker for SUPR3LoadModule().
700 *
701 * @returns VBox status code.
702 * @param pszFilename Name of the VMMR0 image file
703 * @param pszModule The modulen name.
704 * @param pszSrvReqHandler The service request handler symbol name,
705 * optional.
706 * @param pErrInfo Where to store detailed error info. Optional.
707 * @param ppvImageBase Where to return the load address.
708 */
709static int supLoadModule(const char *pszFilename, const char *pszModule, const char *pszSrvReqHandler,
710 PRTERRINFO pErrInfo, void **ppvImageBase)
711{
712 int rc;
713
714 /*
715 * Validate input.
716 */
717 AssertPtrReturn(pszFilename, VERR_INVALID_PARAMETER);
718 AssertPtrReturn(pszModule, VERR_INVALID_PARAMETER);
719 AssertPtrReturn(ppvImageBase, VERR_INVALID_PARAMETER);
720 /** @todo abspath it right into SUPLDROPEN */
721 AssertReturn(strlen(pszModule) < RT_SIZEOFMEMB(SUPLDROPEN, u.In.szName), VERR_FILENAME_TOO_LONG);
722 char szAbsFilename[RT_SIZEOFMEMB(SUPLDROPEN, u.In.szFilename)];
723 rc = RTPathAbs(pszFilename, szAbsFilename, sizeof(szAbsFilename));
724 if (RT_FAILURE(rc))
725 return rc;
726 pszFilename = szAbsFilename;
727
728 const bool fIsVMMR0 = !strcmp(pszModule, "VMMR0.r0");
729 AssertReturn(!pszSrvReqHandler || !fIsVMMR0, VERR_INTERNAL_ERROR);
730 *ppvImageBase = NULL;
731
732 /*
733 * Open image file and figure its size.
734 */
735 RTLDRMOD hLdrMod;
736 rc = RTLdrOpenEx(pszFilename, 0 /*fFlags*/, RTLDRARCH_HOST, &hLdrMod, pErrInfo);
737 if (RT_FAILURE(rc))
738 {
739 LogRel(("SUP: RTLdrOpen failed for %s (%s) %Rrc\n", pszModule, pszFilename, rc));
740 return rc;
741 }
742
743 SUPLDRCALCSIZEARGS CalcArgs;
744 CalcArgs.cbStrings = 0;
745 CalcArgs.cSymbols = 0;
746 CalcArgs.cbImage = RTLdrSize(hLdrMod);
747 rc = RTLdrEnumSymbols(hLdrMod, 0, NULL, 0, supLoadModuleCalcSizeCB, &CalcArgs);
748 if (RT_SUCCESS(rc))
749 {
750 /*
751 * Figure out the number of segments needed first.
752 */
753 SUPLDRCOMPSEGTABARGS SegArgs;
754 SegArgs.uStartRva = 0;
755 SegArgs.uEndRva = 0;
756 SegArgs.fProt = RTMEM_PROT_READ;
757 SegArgs.iSegs = 0;
758 SegArgs.cSegsAlloc = 0;
759 SegArgs.paSegs = NULL;
760 SegArgs.pErrInfo = pErrInfo;
761 rc = RTLdrEnumSegments(hLdrMod, supLoadModuleCompileSegmentsCB, &SegArgs);
762 if (RT_SUCCESS(rc))
763 {
764 Assert(SegArgs.uEndRva <= RTLdrSize(hLdrMod));
765 SegArgs.uEndRva = (uint32_t)CalcArgs.cbImage; /* overflow is checked later */
766 if (SegArgs.uEndRva > SegArgs.uStartRva)
767 SegArgs.iSegs++;
768
769 const uint32_t offSymTab = RT_ALIGN_32(CalcArgs.cbImage, 8);
770 const uint32_t offStrTab = offSymTab + CalcArgs.cSymbols * sizeof(SUPLDRSYM);
771 const uint32_t offSegTab = RT_ALIGN_32(offStrTab + CalcArgs.cbStrings, 8);
772 const uint32_t cbImageWithEverything = RT_ALIGN_32(offSegTab + sizeof(SUPLDRSEG) * SegArgs.iSegs, 8);
773
774 /*
775 * Open the R0 image.
776 */
777 SUPLDROPEN OpenReq;
778 OpenReq.Hdr.u32Cookie = g_u32Cookie;
779 OpenReq.Hdr.u32SessionCookie = g_u32SessionCookie;
780 OpenReq.Hdr.cbIn = SUP_IOCTL_LDR_OPEN_SIZE_IN;
781 OpenReq.Hdr.cbOut = SUP_IOCTL_LDR_OPEN_SIZE_OUT;
782 OpenReq.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
783 OpenReq.Hdr.rc = VERR_INTERNAL_ERROR;
784 OpenReq.u.In.cbImageWithEverything = cbImageWithEverything;
785 OpenReq.u.In.cbImageBits = (uint32_t)CalcArgs.cbImage;
786 strcpy(OpenReq.u.In.szName, pszModule);
787 strcpy(OpenReq.u.In.szFilename, pszFilename);
788 if (!g_uSupFakeMode)
789 {
790 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LDR_OPEN, &OpenReq, SUP_IOCTL_LDR_OPEN_SIZE);
791 if (RT_SUCCESS(rc))
792 rc = OpenReq.Hdr.rc;
793 }
794 else
795 {
796 OpenReq.u.Out.fNeedsLoading = true;
797 OpenReq.u.Out.pvImageBase = 0xef423420;
798 }
799 *ppvImageBase = (void *)OpenReq.u.Out.pvImageBase;
800 if ( RT_SUCCESS(rc)
801 && OpenReq.u.Out.fNeedsLoading)
802 {
803 /*
804 * We need to load it.
805 *
806 * Allocate the request and pass it to an inner work function
807 * that populates it and sends it off to the driver.
808 */
809 const uint32_t cbLoadReq = SUP_IOCTL_LDR_LOAD_SIZE(cbImageWithEverything);
810 PSUPLDRLOAD pLoadReq = (PSUPLDRLOAD)RTMemTmpAlloc(cbLoadReq);
811 if (pLoadReq)
812 {
813 rc = supLoadModuleInner(hLdrMod, pLoadReq, cbImageWithEverything, OpenReq.u.Out.pvImageBase, CalcArgs.cbImage,
814 pszModule, pszFilename, OpenReq.u.Out.fNativeLoader, fIsVMMR0, pszSrvReqHandler,
815 offSymTab, CalcArgs.cSymbols,
816 offStrTab, CalcArgs.cbStrings,
817 offSegTab, SegArgs.iSegs,
818 pErrInfo);
819 RTMemTmpFree(pLoadReq);
820 }
821 else
822 {
823 AssertMsgFailed(("failed to allocated %u bytes for SUPLDRLOAD_IN structure!\n", SUP_IOCTL_LDR_LOAD_SIZE(cbImageWithEverything)));
824 rc = RTErrInfoSetF(pErrInfo, VERR_NO_TMP_MEMORY, "Failed to allocate %u bytes for the load request",
825 SUP_IOCTL_LDR_LOAD_SIZE(cbImageWithEverything));
826 }
827 }
828 /*
829 * Already loaded?
830 */
831 else if (RT_SUCCESS(rc))
832 {
833 if (fIsVMMR0)
834 g_pvVMMR0 = OpenReq.u.Out.pvImageBase;
835 LogRel(("SUP: Opened %s (%s) at %#RKv%s.\n", pszModule, pszFilename, OpenReq.u.Out.pvImageBase,
836 OpenReq.u.Out.fNativeLoader ? " loaded by the native ring-0 loader" : ""));
837#ifdef RT_OS_WINDOWS
838 LogRel(("SUP: windbg> .reload /f %s=%#RKv\n", pszFilename, OpenReq.u.Out.pvImageBase));
839#endif
840 }
841 /*
842 * No, failed.
843 */
844 else
845 RTErrInfoSet(pErrInfo, rc, "SUP_IOCTL_LDR_OPEN failed");
846 }
847 else if (!RTErrInfoIsSet(pErrInfo) && pErrInfo)
848 RTErrInfoSetF(pErrInfo, rc, "RTLdrEnumSegments #1 failed");
849 }
850 else
851 RTErrInfoSetF(pErrInfo, rc, "RTLdrEnumSymbols #1 failed");
852 RTLdrClose(hLdrMod);
853 return rc;
854}
855
856
857SUPR3DECL(int) SUPR3FreeModule(void *pvImageBase)
858{
859 /* fake */
860 if (RT_UNLIKELY(g_uSupFakeMode))
861 {
862 g_pvVMMR0 = NIL_RTR0PTR;
863 return VINF_SUCCESS;
864 }
865
866 /*
867 * Free the requested module.
868 */
869 SUPLDRFREE Req;
870 Req.Hdr.u32Cookie = g_u32Cookie;
871 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
872 Req.Hdr.cbIn = SUP_IOCTL_LDR_FREE_SIZE_IN;
873 Req.Hdr.cbOut = SUP_IOCTL_LDR_FREE_SIZE_OUT;
874 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
875 Req.Hdr.rc = VERR_INTERNAL_ERROR;
876 Req.u.In.pvImageBase = (RTR0PTR)pvImageBase;
877 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LDR_FREE, &Req, SUP_IOCTL_LDR_FREE_SIZE);
878 if (RT_SUCCESS(rc))
879 rc = Req.Hdr.rc;
880 if ( RT_SUCCESS(rc)
881 && (RTR0PTR)pvImageBase == g_pvVMMR0)
882 g_pvVMMR0 = NIL_RTR0PTR;
883 return rc;
884}
885
886
887SUPR3DECL(int) SUPR3GetSymbolR0(void *pvImageBase, const char *pszSymbol, void **ppvValue)
888{
889 *ppvValue = NULL;
890
891 /* fake */
892 if (RT_UNLIKELY(g_uSupFakeMode))
893 {
894 *ppvValue = (void *)(uintptr_t)0xdeadf00d;
895 return VINF_SUCCESS;
896 }
897
898 /*
899 * Do ioctl.
900 */
901 SUPLDRGETSYMBOL Req;
902 Req.Hdr.u32Cookie = g_u32Cookie;
903 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
904 Req.Hdr.cbIn = SUP_IOCTL_LDR_GET_SYMBOL_SIZE_IN;
905 Req.Hdr.cbOut = SUP_IOCTL_LDR_GET_SYMBOL_SIZE_OUT;
906 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
907 Req.Hdr.rc = VERR_INTERNAL_ERROR;
908 Req.u.In.pvImageBase = (RTR0PTR)pvImageBase;
909 size_t cchSymbol = strlen(pszSymbol);
910 if (cchSymbol >= sizeof(Req.u.In.szSymbol))
911 return VERR_SYMBOL_NOT_FOUND;
912 memcpy(Req.u.In.szSymbol, pszSymbol, cchSymbol + 1);
913 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LDR_GET_SYMBOL, &Req, SUP_IOCTL_LDR_GET_SYMBOL_SIZE);
914 if (RT_SUCCESS(rc))
915 rc = Req.Hdr.rc;
916 if (RT_SUCCESS(rc))
917 *ppvValue = (void *)Req.u.Out.pvSymbol;
918 return rc;
919}
920
921
922SUPR3DECL(int) SUPR3LoadVMM(const char *pszFilename, PRTERRINFO pErrInfo)
923{
924 void *pvImageBase;
925 return SUPR3LoadModule(pszFilename, "VMMR0.r0", &pvImageBase, pErrInfo);
926}
927
928
929SUPR3DECL(int) SUPR3UnloadVMM(void)
930{
931 return SUPR3FreeModule((void*)g_pvVMMR0);
932}
933
934
935/**
936 * Worker for SUPR3HardenedLdrLoad and SUPR3HardenedLdrLoadAppPriv.
937 *
938 * @returns iprt status code.
939 * @param pszFilename The full file name.
940 * @param phLdrMod Where to store the handle to the loaded module.
941 * @param fFlags See RTLDFLAGS_.
942 * @param pErrInfo Where to return extended error information.
943 * Optional.
944 *
945 */
946static int supR3HardenedLdrLoadIt(const char *pszFilename, PRTLDRMOD phLdrMod, uint32_t fFlags, PRTERRINFO pErrInfo)
947{
948#ifdef VBOX_WITH_HARDENING
949 /*
950 * Verify the image file.
951 */
952 int rc = SUPR3HardenedVerifyInit();
953 if (RT_FAILURE(rc))
954 rc = supR3HardenedVerifyFixedFile(pszFilename, false /* fFatal */);
955 if (RT_FAILURE(rc))
956 {
957 LogRel(("supR3HardenedLdrLoadIt: Verification of \"%s\" failed, rc=%Rrc\n", pszFilename, rc));
958 return RTErrInfoSet(pErrInfo, rc, "supR3HardenedVerifyFixedFile failed");
959 }
960#endif
961
962 /*
963 * Try load it.
964 */
965 return RTLdrLoadEx(pszFilename, phLdrMod, fFlags, pErrInfo);
966}
967
968
969SUPR3DECL(int) SUPR3HardenedLdrLoad(const char *pszFilename, PRTLDRMOD phLdrMod, uint32_t fFlags, PRTERRINFO pErrInfo)
970{
971 /*
972 * Validate input.
973 */
974 RTErrInfoClear(pErrInfo);
975 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
976 AssertPtrReturn(phLdrMod, VERR_INVALID_POINTER);
977 *phLdrMod = NIL_RTLDRMOD;
978 AssertReturn(RTPathHavePath(pszFilename), VERR_INVALID_PARAMETER);
979
980 /*
981 * Add the default extension if it's missing.
982 */
983 if (!RTPathHasSuffix(pszFilename))
984 {
985 const char *pszSuff = RTLdrGetSuff();
986 size_t cchSuff = strlen(pszSuff);
987 size_t cchFilename = strlen(pszFilename);
988 char *psz = (char *)alloca(cchFilename + cchSuff + 1);
989 AssertReturn(psz, VERR_NO_TMP_MEMORY);
990 memcpy(psz, pszFilename, cchFilename);
991 memcpy(psz + cchFilename, pszSuff, cchSuff + 1);
992 pszFilename = psz;
993 }
994
995 /*
996 * Pass it on to the common library loader.
997 */
998 return supR3HardenedLdrLoadIt(pszFilename, phLdrMod, fFlags, pErrInfo);
999}
1000
1001
1002SUPR3DECL(int) SUPR3HardenedLdrLoadAppPriv(const char *pszFilename, PRTLDRMOD phLdrMod, uint32_t fFlags, PRTERRINFO pErrInfo)
1003{
1004 LogFlow(("SUPR3HardenedLdrLoadAppPriv: pszFilename=%p:{%s} phLdrMod=%p fFlags=%08x pErrInfo=%p\n", pszFilename, pszFilename, phLdrMod, fFlags, pErrInfo));
1005
1006 /*
1007 * Validate input.
1008 */
1009 RTErrInfoClear(pErrInfo);
1010 AssertPtrReturn(phLdrMod, VERR_INVALID_PARAMETER);
1011 *phLdrMod = NIL_RTLDRMOD;
1012 AssertPtrReturn(pszFilename, VERR_INVALID_PARAMETER);
1013 AssertMsgReturn(!RTPathHavePath(pszFilename), ("%s\n", pszFilename), VERR_INVALID_PARAMETER);
1014
1015 /*
1016 * Check the filename.
1017 */
1018 size_t cchFilename = strlen(pszFilename);
1019 AssertMsgReturn(cchFilename < (RTPATH_MAX / 4) * 3, ("%zu\n", cchFilename), VERR_INVALID_PARAMETER);
1020
1021 const char *pszExt = "";
1022 size_t cchExt = 0;
1023 if (!RTPathHasSuffix(pszFilename))
1024 {
1025 pszExt = RTLdrGetSuff();
1026 cchExt = strlen(pszExt);
1027 }
1028
1029 /*
1030 * Construct the private arch path and check if the file exists.
1031 */
1032 char szPath[RTPATH_MAX];
1033 int rc = RTPathAppPrivateArch(szPath, sizeof(szPath) - 1 - cchExt - cchFilename);
1034 AssertRCReturn(rc, rc);
1035
1036 char *psz = strchr(szPath, '\0');
1037 *psz++ = RTPATH_SLASH;
1038 memcpy(psz, pszFilename, cchFilename);
1039 psz += cchFilename;
1040 memcpy(psz, pszExt, cchExt + 1);
1041
1042 if (!RTPathExists(szPath))
1043 {
1044 LogRel(("SUPR3HardenedLdrLoadAppPriv: \"%s\" not found\n", szPath));
1045 return VERR_FILE_NOT_FOUND;
1046 }
1047
1048 /*
1049 * Pass it on to SUPR3HardenedLdrLoad.
1050 */
1051 rc = SUPR3HardenedLdrLoad(szPath, phLdrMod, fFlags, pErrInfo);
1052
1053 LogFlow(("SUPR3HardenedLdrLoadAppPriv: returns %Rrc\n", rc));
1054 return rc;
1055}
1056
1057
1058SUPR3DECL(int) SUPR3HardenedLdrLoadPlugIn(const char *pszFilename, PRTLDRMOD phLdrMod, PRTERRINFO pErrInfo)
1059{
1060 /*
1061 * Validate input.
1062 */
1063 RTErrInfoClear(pErrInfo);
1064 AssertPtrReturn(phLdrMod, VERR_INVALID_PARAMETER);
1065 *phLdrMod = NIL_RTLDRMOD;
1066 AssertPtrReturn(pszFilename, VERR_INVALID_PARAMETER);
1067 AssertReturn(RTPathStartsWithRoot(pszFilename), VERR_INVALID_PARAMETER);
1068
1069#ifdef VBOX_WITH_HARDENING
1070 /*
1071 * Verify the image file.
1072 */
1073 int rc = supR3HardenedVerifyFile(pszFilename, RTHCUINTPTR_MAX, true /*fMaybe3rdParty*/, pErrInfo);
1074 if (RT_FAILURE(rc))
1075 {
1076 if (!RTErrInfoIsSet(pErrInfo))
1077 LogRel(("supR3HardenedVerifyFile: Verification of \"%s\" failed, rc=%Rrc\n", pszFilename, rc));
1078 return rc;
1079 }
1080#endif
1081
1082 /*
1083 * Try load it.
1084 */
1085 return RTLdrLoadEx(pszFilename, phLdrMod, RTLDRLOAD_FLAGS_LOCAL, pErrInfo);
1086}
1087
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