VirtualBox

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

Last change on this file since 85546 was 85546, checked in by vboxsync, 5 years ago

SUP: Logging adjustment in supLoadModuleCompileSegmentsCB. bugref:9801

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 40.6 KB
Line 
1/* $Id: SUPLibLdr.cpp 85546 2020-07-30 09:07:13Z 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.215389.xyz. 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 Log2(("supLoadModuleCompileSegmentsCB: %RTptr/%RTptr LB %RTptr/%RTptr prot %#x %s\n",
362 pSeg->LinkAddress, pSeg->RVA, pSeg->cbMapped, pSeg->cb, pSeg->fProt, pSeg->pszName));
363
364 /* Ignore segments not part of the loaded image. */
365 if (pSeg->RVA == NIL_RTLDRADDR || pSeg->cbMapped == 0)
366 {
367 Log2(("supLoadModuleCompileSegmentsCB: -> skipped\n"));
368 return VINF_SUCCESS;
369 }
370
371 /* We currently ASSUME that all relevant segments are in ascending RVA order. */
372 AssertReturn(pSeg->RVA >= pArgs->uEndRva,
373 RTERRINFO_LOG_REL_SET_F(pArgs->pErrInfo, VERR_BAD_EXE_FORMAT, "Out of order segment: %p LB %#zx #%.*s",
374 pSeg->RVA, pSeg->cb, pSeg->cchName, pSeg->pszName));
375
376 /* We ASSUME the cbMapped field is implemented. */
377 AssertReturn(pSeg->cbMapped != NIL_RTLDRADDR, VERR_INTERNAL_ERROR_2);
378 AssertReturn(pSeg->cbMapped < _1G, VERR_INTERNAL_ERROR_4);
379 uint32_t cbMapped = (uint32_t)pSeg->cbMapped;
380 AssertReturn(pSeg->RVA < _1G, VERR_INTERNAL_ERROR_3);
381 uint32_t uRvaSeg = (uint32_t)pSeg->RVA;
382
383 /*
384 * If the protection is the same as the previous segment,
385 * just update uEndRva and continue.
386 */
387 uint32_t fProt = pSeg->fProt;
388#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
389 if (fProt & RTMEM_PROT_EXEC)
390 fProt |= fProt & RTMEM_PROT_READ;
391#endif
392 if (pSeg->fProt == pArgs->fProt)
393 {
394 pArgs->uEndRva = uRvaSeg + cbMapped;
395 Log2(("supLoadModuleCompileSegmentsCB: -> merged\n"));
396 return VINF_SUCCESS;
397 }
398
399 /*
400 * The protection differs, so commit current segment and start a new one.
401 * However, if the new segment and old segment share a page, this becomes
402 * a little more complicated...
403 */
404 if (pArgs->uStartRva < pArgs->uEndRva)
405 {
406 if (((pArgs->uEndRva - 1) >> PAGE_SHIFT) != (uRvaSeg >> PAGE_SHIFT))
407 {
408 /* No common page, so make the new segment start on a page boundrary. */
409 cbMapped += uRvaSeg & PAGE_OFFSET_MASK;
410 uRvaSeg &= ~(uint32_t)PAGE_OFFSET_MASK;
411 Assert(pArgs->uEndRva <= uRvaSeg);
412 Log2(("supLoadModuleCompileSegmentsCB: -> new, no common\n"));
413 }
414 else if ((fProt & pArgs->fProt) == fProt)
415 {
416 /* The current segment includes the memory protections of the
417 previous, so include the common page in it: */
418 uint32_t const cbCommon = PAGE_SIZE - (uRvaSeg & PAGE_OFFSET_MASK);
419 if (cbCommon >= cbMapped)
420 {
421 pArgs->uEndRva = uRvaSeg + cbMapped;
422 Log2(("supLoadModuleCompileSegmentsCB: -> merge, %#x common, upgrading prot to %#x\n", cbCommon, pArgs->fProt));
423 return VINF_SUCCESS; /* New segment was smaller than a page. */
424 }
425 cbMapped -= cbCommon;
426 uRvaSeg += cbCommon;
427 Assert(pArgs->uEndRva <= uRvaSeg);
428 Log2(("supLoadModuleCompileSegmentsCB: -> new, %#x common into previous\n", cbCommon));
429 }
430 else if ((fProt & pArgs->fProt) == pArgs->fProt)
431 {
432 /* The new segment includes the memory protections of the
433 previous, so include the common page in it: */
434 cbMapped += uRvaSeg & PAGE_OFFSET_MASK;
435 uRvaSeg &= ~(uint32_t)PAGE_OFFSET_MASK;
436 if (uRvaSeg == pArgs->uStartRva)
437 {
438 pArgs->fProt = fProt;
439 pArgs->uEndRva = uRvaSeg + cbMapped;
440 Log2(("supLoadModuleCompileSegmentsCB: -> upgrade current protection\n"));
441 return VINF_SUCCESS; /* Current segment was smaller than a page. */
442 }
443 Log2(("supLoadModuleCompileSegmentsCB: -> new, %#x common into new\n", (uint32_t)(pSeg->RVA & PAGE_OFFSET_MASK)));
444 }
445 else
446 {
447 /* Create a new segment for the common page with the combined protection. */
448 Log2(("supLoadModuleCompileSegmentsCB: -> its complicated...\n"));
449 pArgs->uEndRva &= ~(uint32_t)PAGE_OFFSET_MASK;
450 if (pArgs->uEndRva > pArgs->uStartRva)
451 {
452 Log2(("supLoadModuleCompileSegmentsCB: SUP Seg #%u: %#x LB %#x prot %#x\n",
453 pArgs->iSegs, pArgs->uStartRva, pArgs->uEndRva - pArgs->uStartRva, pArgs->fProt));
454 if (pArgs->paSegs)
455 {
456 AssertReturn(pArgs->iSegs < pArgs->cSegsAlloc, VERR_INTERNAL_ERROR_5);
457 pArgs->paSegs[pArgs->iSegs].off = pArgs->uStartRva;
458 pArgs->paSegs[pArgs->iSegs].cb = pArgs->uEndRva - pArgs->uStartRva;
459 pArgs->paSegs[pArgs->iSegs].fProt = pArgs->fProt;
460 pArgs->paSegs[pArgs->iSegs].fUnused = 0;
461 }
462 pArgs->iSegs++;
463 pArgs->uStartRva = pArgs->uEndRva;
464 }
465 pArgs->fProt |= fProt;
466
467 uint32_t const cbCommon = PAGE_SIZE - (uRvaSeg & PAGE_OFFSET_MASK);
468 if (cbCommon <= cbMapped)
469 {
470 fProt |= pArgs->fProt;
471 pArgs->uEndRva = uRvaSeg + cbMapped;
472 return VINF_SUCCESS; /* New segment was smaller than a page. */
473 }
474 cbMapped -= cbCommon;
475 uRvaSeg += cbCommon;
476 Assert(uRvaSeg - pArgs->uStartRva == PAGE_SIZE);
477 }
478
479 /* The current segment should end where the new one starts, no gaps. */
480 pArgs->uEndRva = uRvaSeg;
481
482 /* Emit the current segment */
483 Log2(("supLoadModuleCompileSegmentsCB: SUP Seg #%u: %#x LB %#x prot %#x\n",
484 pArgs->iSegs, pArgs->uStartRva, pArgs->uEndRva - pArgs->uStartRva, pArgs->fProt));
485 if (pArgs->paSegs)
486 {
487 AssertReturn(pArgs->iSegs < pArgs->cSegsAlloc, VERR_INTERNAL_ERROR_5);
488 pArgs->paSegs[pArgs->iSegs].off = pArgs->uStartRva;
489 pArgs->paSegs[pArgs->iSegs].cb = pArgs->uEndRva - pArgs->uStartRva;
490 pArgs->paSegs[pArgs->iSegs].fProt = pArgs->fProt;
491 pArgs->paSegs[pArgs->iSegs].fUnused = 0;
492 }
493 pArgs->iSegs++;
494 }
495 /* else: current segment is empty */
496
497 /* Start the new segment. */
498 Assert(!(uRvaSeg & PAGE_OFFSET_MASK));
499 pArgs->fProt = fProt;
500 pArgs->uStartRva = uRvaSeg;
501 pArgs->uEndRva = uRvaSeg + cbMapped;
502 return VINF_SUCCESS;
503}
504
505
506/**
507 * Worker for supLoadModule().
508 */
509static int supLoadModuleInner(RTLDRMOD hLdrMod, PSUPLDRLOAD pLoadReq, uint32_t cbImageWithEverything,
510 RTR0PTR uImageBase, size_t cbImage, const char *pszModule, const char *pszFilename,
511 bool fNativeLoader, bool fIsVMMR0, const char *pszSrvReqHandler,
512 uint32_t offSymTab, uint32_t cSymbols,
513 uint32_t offStrTab, size_t cbStrTab,
514 uint32_t offSegTab, uint32_t cSegments,
515 PRTERRINFO pErrInfo)
516{
517 /*
518 * Get the image bits.
519 */
520 SUPLDRRESIMPARGS Args = { pszModule, pErrInfo };
521 int rc = RTLdrGetBits(hLdrMod, &pLoadReq->u.In.abImage[0], uImageBase, supLoadModuleResolveImport, &Args);
522 if (RT_FAILURE(rc))
523 {
524 LogRel(("SUP: RTLdrGetBits failed for %s (%s). rc=%Rrc\n", pszModule, pszFilename, rc));
525 if (!RTErrInfoIsSet(pErrInfo))
526 RTErrInfoSetF(pErrInfo, rc, "RTLdrGetBits failed");
527 return rc;
528 }
529
530 /*
531 * Get the entry points.
532 */
533 RTUINTPTR VMMR0EntryFast = 0;
534 RTUINTPTR VMMR0EntryEx = 0;
535 RTUINTPTR SrvReqHandler = 0;
536 RTUINTPTR ModuleInit = 0;
537 RTUINTPTR ModuleTerm = 0;
538 const char *pszEp = NULL;
539 if (fIsVMMR0)
540 {
541 rc = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.abImage[0], uImageBase,
542 UINT32_MAX, pszEp = "VMMR0EntryFast", &VMMR0EntryFast);
543 if (RT_SUCCESS(rc))
544 rc = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.abImage[0], uImageBase,
545 UINT32_MAX, pszEp = "VMMR0EntryEx", &VMMR0EntryEx);
546 }
547 else if (pszSrvReqHandler)
548 rc = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.abImage[0], uImageBase,
549 UINT32_MAX, pszEp = pszSrvReqHandler, &SrvReqHandler);
550 if (RT_SUCCESS(rc))
551 {
552 int rc2 = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.abImage[0], uImageBase,
553 UINT32_MAX, pszEp = "ModuleInit", &ModuleInit);
554 if (RT_FAILURE(rc2))
555 ModuleInit = 0;
556
557 rc2 = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.abImage[0], uImageBase,
558 UINT32_MAX, pszEp = "ModuleTerm", &ModuleTerm);
559 if (RT_FAILURE(rc2))
560 ModuleTerm = 0;
561 }
562 if (RT_FAILURE(rc))
563 {
564 LogRel(("SUP: Failed to get entry point '%s' for %s (%s) rc=%Rrc\n", pszEp, pszModule, pszFilename, rc));
565 return RTErrInfoSetF(pErrInfo, rc, "Failed to resolve entry point '%s'", pszEp);
566 }
567
568 /*
569 * Create the symbol and string tables.
570 */
571 SUPLDRCREATETABSARGS CreateArgs;
572 CreateArgs.cbImage = cbImage;
573 CreateArgs.pSym = (PSUPLDRSYM)&pLoadReq->u.In.abImage[offSymTab];
574 CreateArgs.pszBase = (char *)&pLoadReq->u.In.abImage[offStrTab];
575 CreateArgs.psz = CreateArgs.pszBase;
576 rc = RTLdrEnumSymbols(hLdrMod, 0, NULL, 0, supLoadModuleCreateTabsCB, &CreateArgs);
577 if (RT_FAILURE(rc))
578 {
579 LogRel(("SUP: RTLdrEnumSymbols failed for %s (%s) rc=%Rrc\n", pszModule, pszFilename, rc));
580 return RTErrInfoSetF(pErrInfo, rc, "RTLdrEnumSymbols #2 failed");
581 }
582 AssertRelease((size_t)(CreateArgs.psz - CreateArgs.pszBase) <= cbStrTab);
583 AssertRelease((size_t)(CreateArgs.pSym - (PSUPLDRSYM)&pLoadReq->u.In.abImage[offSymTab]) <= cSymbols);
584
585 /*
586 * Create the segment table.
587 */
588 SUPLDRCOMPSEGTABARGS SegArgs;
589 SegArgs.uStartRva = 0;
590 SegArgs.uEndRva = 0;
591 SegArgs.fProt = RTMEM_PROT_READ;
592 SegArgs.iSegs = 0;
593 SegArgs.cSegsAlloc = cSegments;
594 SegArgs.paSegs = (PSUPLDRSEG)&pLoadReq->u.In.abImage[offSegTab];
595 SegArgs.pErrInfo = pErrInfo;
596 rc = RTLdrEnumSegments(hLdrMod, supLoadModuleCompileSegmentsCB, &SegArgs);
597 if (RT_FAILURE(rc))
598 {
599 LogRel(("SUP: RTLdrEnumSegments failed for %s (%s) rc=%Rrc\n", pszModule, pszFilename, rc));
600 return RTErrInfoSetF(pErrInfo, rc, "RTLdrEnumSegments #2 failed");
601 }
602 SegArgs.uEndRva = (uint32_t)cbImage;
603 AssertReturn(SegArgs.uEndRva == cbImage, VERR_OUT_OF_RANGE);
604 if (SegArgs.uEndRva > SegArgs.uStartRva)
605 {
606 SegArgs.paSegs[SegArgs.iSegs].off = SegArgs.uStartRva;
607 SegArgs.paSegs[SegArgs.iSegs].cb = SegArgs.uEndRva - SegArgs.uStartRva;
608 SegArgs.paSegs[SegArgs.iSegs].fProt = SegArgs.fProt;
609 SegArgs.paSegs[SegArgs.iSegs].fUnused = 0;
610 SegArgs.iSegs++;
611 }
612 for (uint32_t i = 0; i < SegArgs.iSegs; i++)
613 LogRel(("SUP: seg #%u: %c%c%c %#010RX32 LB %#010RX32\n", i, /** @todo LogRel2 */
614 SegArgs.paSegs[i].fProt & SUPLDR_PROT_READ ? 'R' : ' ',
615 SegArgs.paSegs[i].fProt & SUPLDR_PROT_WRITE ? 'W' : ' ',
616 SegArgs.paSegs[i].fProt & SUPLDR_PROT_EXEC ? 'X' : ' ',
617 SegArgs.paSegs[i].off, SegArgs.paSegs[i].cb));
618 AssertRelease(SegArgs.iSegs == cSegments);
619 AssertRelease(SegArgs.cSegsAlloc == cSegments);
620
621 /*
622 * Upload the image.
623 */
624 pLoadReq->Hdr.u32Cookie = g_u32Cookie;
625 pLoadReq->Hdr.u32SessionCookie = g_u32SessionCookie;
626 pLoadReq->Hdr.cbIn = SUP_IOCTL_LDR_LOAD_SIZE_IN(cbImageWithEverything);
627 pLoadReq->Hdr.cbOut = SUP_IOCTL_LDR_LOAD_SIZE_OUT;
628 pLoadReq->Hdr.fFlags = SUPREQHDR_FLAGS_MAGIC | SUPREQHDR_FLAGS_EXTRA_IN;
629 pLoadReq->Hdr.rc = VERR_INTERNAL_ERROR;
630
631 pLoadReq->u.In.pfnModuleInit = (RTR0PTR)ModuleInit;
632 pLoadReq->u.In.pfnModuleTerm = (RTR0PTR)ModuleTerm;
633 if (fIsVMMR0)
634 {
635 pLoadReq->u.In.eEPType = SUPLDRLOADEP_VMMR0;
636 pLoadReq->u.In.EP.VMMR0.pvVMMR0 = uImageBase;
637 pLoadReq->u.In.EP.VMMR0.pvVMMR0EntryFast= (RTR0PTR)VMMR0EntryFast;
638 pLoadReq->u.In.EP.VMMR0.pvVMMR0EntryEx = (RTR0PTR)VMMR0EntryEx;
639 }
640 else if (pszSrvReqHandler)
641 {
642 pLoadReq->u.In.eEPType = SUPLDRLOADEP_SERVICE;
643 pLoadReq->u.In.EP.Service.pfnServiceReq = (RTR0PTR)SrvReqHandler;
644 pLoadReq->u.In.EP.Service.apvReserved[0] = NIL_RTR0PTR;
645 pLoadReq->u.In.EP.Service.apvReserved[1] = NIL_RTR0PTR;
646 pLoadReq->u.In.EP.Service.apvReserved[2] = NIL_RTR0PTR;
647 }
648 else
649 pLoadReq->u.In.eEPType = SUPLDRLOADEP_NOTHING;
650 pLoadReq->u.In.offStrTab = offStrTab;
651 pLoadReq->u.In.cbStrTab = (uint32_t)cbStrTab;
652 AssertRelease(pLoadReq->u.In.cbStrTab == cbStrTab);
653 pLoadReq->u.In.cbImageBits = (uint32_t)cbImage;
654 pLoadReq->u.In.offSymbols = offSymTab;
655 pLoadReq->u.In.cSymbols = cSymbols;
656 pLoadReq->u.In.offSegments = offSegTab;
657 pLoadReq->u.In.cSegments = cSegments;
658 pLoadReq->u.In.cbImageWithEverything = cbImageWithEverything;
659 pLoadReq->u.In.pvImageBase = uImageBase;
660 if (!g_uSupFakeMode)
661 {
662 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LDR_LOAD, pLoadReq, SUP_IOCTL_LDR_LOAD_SIZE(cbImageWithEverything));
663 if (RT_SUCCESS(rc))
664 rc = pLoadReq->Hdr.rc;
665 else
666 LogRel(("SUP: SUP_IOCTL_LDR_LOAD ioctl for %s (%s) failed rc=%Rrc\n", pszModule, pszFilename, rc));
667 }
668 else
669 rc = VINF_SUCCESS;
670 if ( RT_SUCCESS(rc)
671 || rc == VERR_ALREADY_LOADED /* A competing process. */
672 )
673 {
674 LogRel(("SUP: Loaded %s (%s) at %#RKv - ModuleInit at %RKv and ModuleTerm at %RKv%s\n",
675 pszModule, pszFilename, uImageBase, (RTR0PTR)ModuleInit, (RTR0PTR)ModuleTerm,
676 fNativeLoader ? " using the native ring-0 loader" : ""));
677 if (fIsVMMR0)
678 {
679 g_pvVMMR0 = uImageBase;
680 LogRel(("SUP: VMMR0EntryEx located at %RKv and VMMR0EntryFast at %RKv\n", (RTR0PTR)VMMR0EntryEx, (RTR0PTR)VMMR0EntryFast));
681 }
682#ifdef RT_OS_WINDOWS
683 LogRel(("SUP: windbg> .reload /f %s=%#RKv\n", pszFilename, uImageBase));
684#endif
685 return VINF_SUCCESS;
686 }
687
688 /*
689 * Failed, bail out.
690 */
691 LogRel(("SUP: Loading failed for %s (%s) rc=%Rrc\n", pszModule, pszFilename, rc));
692 if ( pLoadReq->u.Out.uErrorMagic == SUPLDRLOAD_ERROR_MAGIC
693 && pLoadReq->u.Out.szError[0] != '\0')
694 {
695 LogRel(("SUP: %s\n", pLoadReq->u.Out.szError));
696 return RTErrInfoSet(pErrInfo, rc, pLoadReq->u.Out.szError);
697 }
698 return RTErrInfoSet(pErrInfo, rc, "SUP_IOCTL_LDR_LOAD failed");
699}
700
701
702/**
703 * Worker for SUPR3LoadModule().
704 *
705 * @returns VBox status code.
706 * @param pszFilename Name of the VMMR0 image file
707 * @param pszModule The modulen name.
708 * @param pszSrvReqHandler The service request handler symbol name,
709 * optional.
710 * @param pErrInfo Where to store detailed error info. Optional.
711 * @param ppvImageBase Where to return the load address.
712 */
713static int supLoadModule(const char *pszFilename, const char *pszModule, const char *pszSrvReqHandler,
714 PRTERRINFO pErrInfo, void **ppvImageBase)
715{
716 int rc;
717
718 /*
719 * Validate input.
720 */
721 AssertPtrReturn(pszFilename, VERR_INVALID_PARAMETER);
722 AssertPtrReturn(pszModule, VERR_INVALID_PARAMETER);
723 AssertPtrReturn(ppvImageBase, VERR_INVALID_PARAMETER);
724 /** @todo abspath it right into SUPLDROPEN */
725 AssertReturn(strlen(pszModule) < RT_SIZEOFMEMB(SUPLDROPEN, u.In.szName), VERR_FILENAME_TOO_LONG);
726 char szAbsFilename[RT_SIZEOFMEMB(SUPLDROPEN, u.In.szFilename)];
727 rc = RTPathAbs(pszFilename, szAbsFilename, sizeof(szAbsFilename));
728 if (RT_FAILURE(rc))
729 return rc;
730 pszFilename = szAbsFilename;
731
732 const bool fIsVMMR0 = !strcmp(pszModule, "VMMR0.r0");
733 AssertReturn(!pszSrvReqHandler || !fIsVMMR0, VERR_INTERNAL_ERROR);
734 *ppvImageBase = NULL;
735
736 /*
737 * Open image file and figure its size.
738 */
739 RTLDRMOD hLdrMod;
740 rc = RTLdrOpenEx(pszFilename, 0 /*fFlags*/, RTLDRARCH_HOST, &hLdrMod, pErrInfo);
741 if (RT_FAILURE(rc))
742 {
743 LogRel(("SUP: RTLdrOpen failed for %s (%s) %Rrc\n", pszModule, pszFilename, rc));
744 return rc;
745 }
746
747 SUPLDRCALCSIZEARGS CalcArgs;
748 CalcArgs.cbStrings = 0;
749 CalcArgs.cSymbols = 0;
750 CalcArgs.cbImage = RTLdrSize(hLdrMod);
751 rc = RTLdrEnumSymbols(hLdrMod, 0, NULL, 0, supLoadModuleCalcSizeCB, &CalcArgs);
752 if (RT_SUCCESS(rc))
753 {
754 /*
755 * Figure out the number of segments needed first.
756 */
757 SUPLDRCOMPSEGTABARGS SegArgs;
758 SegArgs.uStartRva = 0;
759 SegArgs.uEndRva = 0;
760 SegArgs.fProt = RTMEM_PROT_READ;
761 SegArgs.iSegs = 0;
762 SegArgs.cSegsAlloc = 0;
763 SegArgs.paSegs = NULL;
764 SegArgs.pErrInfo = pErrInfo;
765 rc = RTLdrEnumSegments(hLdrMod, supLoadModuleCompileSegmentsCB, &SegArgs);
766 if (RT_SUCCESS(rc))
767 {
768 Assert(SegArgs.uEndRva <= RTLdrSize(hLdrMod));
769 SegArgs.uEndRva = (uint32_t)CalcArgs.cbImage; /* overflow is checked later */
770 if (SegArgs.uEndRva > SegArgs.uStartRva)
771 SegArgs.iSegs++;
772
773 const uint32_t offSymTab = RT_ALIGN_32(CalcArgs.cbImage, 8);
774 const uint32_t offStrTab = offSymTab + CalcArgs.cSymbols * sizeof(SUPLDRSYM);
775 const uint32_t offSegTab = RT_ALIGN_32(offStrTab + CalcArgs.cbStrings, 8);
776 const uint32_t cbImageWithEverything = RT_ALIGN_32(offSegTab + sizeof(SUPLDRSEG) * SegArgs.iSegs, 8);
777
778 /*
779 * Open the R0 image.
780 */
781 SUPLDROPEN OpenReq;
782 OpenReq.Hdr.u32Cookie = g_u32Cookie;
783 OpenReq.Hdr.u32SessionCookie = g_u32SessionCookie;
784 OpenReq.Hdr.cbIn = SUP_IOCTL_LDR_OPEN_SIZE_IN;
785 OpenReq.Hdr.cbOut = SUP_IOCTL_LDR_OPEN_SIZE_OUT;
786 OpenReq.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
787 OpenReq.Hdr.rc = VERR_INTERNAL_ERROR;
788 OpenReq.u.In.cbImageWithEverything = cbImageWithEverything;
789 OpenReq.u.In.cbImageBits = (uint32_t)CalcArgs.cbImage;
790 strcpy(OpenReq.u.In.szName, pszModule);
791 strcpy(OpenReq.u.In.szFilename, pszFilename);
792 if (!g_uSupFakeMode)
793 {
794 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LDR_OPEN, &OpenReq, SUP_IOCTL_LDR_OPEN_SIZE);
795 if (RT_SUCCESS(rc))
796 rc = OpenReq.Hdr.rc;
797 }
798 else
799 {
800 OpenReq.u.Out.fNeedsLoading = true;
801 OpenReq.u.Out.pvImageBase = 0xef423420;
802 }
803 *ppvImageBase = (void *)OpenReq.u.Out.pvImageBase;
804 if ( RT_SUCCESS(rc)
805 && OpenReq.u.Out.fNeedsLoading)
806 {
807 /*
808 * We need to load it.
809 *
810 * Allocate the request and pass it to an inner work function
811 * that populates it and sends it off to the driver.
812 */
813 const uint32_t cbLoadReq = SUP_IOCTL_LDR_LOAD_SIZE(cbImageWithEverything);
814 PSUPLDRLOAD pLoadReq = (PSUPLDRLOAD)RTMemTmpAlloc(cbLoadReq);
815 if (pLoadReq)
816 {
817 rc = supLoadModuleInner(hLdrMod, pLoadReq, cbImageWithEverything, OpenReq.u.Out.pvImageBase, CalcArgs.cbImage,
818 pszModule, pszFilename, OpenReq.u.Out.fNativeLoader, fIsVMMR0, pszSrvReqHandler,
819 offSymTab, CalcArgs.cSymbols,
820 offStrTab, CalcArgs.cbStrings,
821 offSegTab, SegArgs.iSegs,
822 pErrInfo);
823 RTMemTmpFree(pLoadReq);
824 }
825 else
826 {
827 AssertMsgFailed(("failed to allocated %u bytes for SUPLDRLOAD_IN structure!\n", SUP_IOCTL_LDR_LOAD_SIZE(cbImageWithEverything)));
828 rc = RTErrInfoSetF(pErrInfo, VERR_NO_TMP_MEMORY, "Failed to allocate %u bytes for the load request",
829 SUP_IOCTL_LDR_LOAD_SIZE(cbImageWithEverything));
830 }
831 }
832 /*
833 * Already loaded?
834 */
835 else if (RT_SUCCESS(rc))
836 {
837 if (fIsVMMR0)
838 g_pvVMMR0 = OpenReq.u.Out.pvImageBase;
839 LogRel(("SUP: Opened %s (%s) at %#RKv%s.\n", pszModule, pszFilename, OpenReq.u.Out.pvImageBase,
840 OpenReq.u.Out.fNativeLoader ? " loaded by the native ring-0 loader" : ""));
841#ifdef RT_OS_WINDOWS
842 LogRel(("SUP: windbg> .reload /f %s=%#RKv\n", pszFilename, OpenReq.u.Out.pvImageBase));
843#endif
844 }
845 /*
846 * No, failed.
847 */
848 else
849 RTErrInfoSet(pErrInfo, rc, "SUP_IOCTL_LDR_OPEN failed");
850 }
851 else if (!RTErrInfoIsSet(pErrInfo) && pErrInfo)
852 RTErrInfoSetF(pErrInfo, rc, "RTLdrEnumSegments #1 failed");
853 }
854 else
855 RTErrInfoSetF(pErrInfo, rc, "RTLdrEnumSymbols #1 failed");
856 RTLdrClose(hLdrMod);
857 return rc;
858}
859
860
861SUPR3DECL(int) SUPR3FreeModule(void *pvImageBase)
862{
863 /* fake */
864 if (RT_UNLIKELY(g_uSupFakeMode))
865 {
866 g_pvVMMR0 = NIL_RTR0PTR;
867 return VINF_SUCCESS;
868 }
869
870 /*
871 * Free the requested module.
872 */
873 SUPLDRFREE Req;
874 Req.Hdr.u32Cookie = g_u32Cookie;
875 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
876 Req.Hdr.cbIn = SUP_IOCTL_LDR_FREE_SIZE_IN;
877 Req.Hdr.cbOut = SUP_IOCTL_LDR_FREE_SIZE_OUT;
878 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
879 Req.Hdr.rc = VERR_INTERNAL_ERROR;
880 Req.u.In.pvImageBase = (RTR0PTR)pvImageBase;
881 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LDR_FREE, &Req, SUP_IOCTL_LDR_FREE_SIZE);
882 if (RT_SUCCESS(rc))
883 rc = Req.Hdr.rc;
884 if ( RT_SUCCESS(rc)
885 && (RTR0PTR)pvImageBase == g_pvVMMR0)
886 g_pvVMMR0 = NIL_RTR0PTR;
887 return rc;
888}
889
890
891SUPR3DECL(int) SUPR3GetSymbolR0(void *pvImageBase, const char *pszSymbol, void **ppvValue)
892{
893 *ppvValue = NULL;
894
895 /* fake */
896 if (RT_UNLIKELY(g_uSupFakeMode))
897 {
898 *ppvValue = (void *)(uintptr_t)0xdeadf00d;
899 return VINF_SUCCESS;
900 }
901
902 /*
903 * Do ioctl.
904 */
905 SUPLDRGETSYMBOL Req;
906 Req.Hdr.u32Cookie = g_u32Cookie;
907 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
908 Req.Hdr.cbIn = SUP_IOCTL_LDR_GET_SYMBOL_SIZE_IN;
909 Req.Hdr.cbOut = SUP_IOCTL_LDR_GET_SYMBOL_SIZE_OUT;
910 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
911 Req.Hdr.rc = VERR_INTERNAL_ERROR;
912 Req.u.In.pvImageBase = (RTR0PTR)pvImageBase;
913 size_t cchSymbol = strlen(pszSymbol);
914 if (cchSymbol >= sizeof(Req.u.In.szSymbol))
915 return VERR_SYMBOL_NOT_FOUND;
916 memcpy(Req.u.In.szSymbol, pszSymbol, cchSymbol + 1);
917 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LDR_GET_SYMBOL, &Req, SUP_IOCTL_LDR_GET_SYMBOL_SIZE);
918 if (RT_SUCCESS(rc))
919 rc = Req.Hdr.rc;
920 if (RT_SUCCESS(rc))
921 *ppvValue = (void *)Req.u.Out.pvSymbol;
922 return rc;
923}
924
925
926SUPR3DECL(int) SUPR3LoadVMM(const char *pszFilename, PRTERRINFO pErrInfo)
927{
928 void *pvImageBase;
929 return SUPR3LoadModule(pszFilename, "VMMR0.r0", &pvImageBase, pErrInfo);
930}
931
932
933SUPR3DECL(int) SUPR3UnloadVMM(void)
934{
935 return SUPR3FreeModule((void*)g_pvVMMR0);
936}
937
938
939/**
940 * Worker for SUPR3HardenedLdrLoad and SUPR3HardenedLdrLoadAppPriv.
941 *
942 * @returns iprt status code.
943 * @param pszFilename The full file name.
944 * @param phLdrMod Where to store the handle to the loaded module.
945 * @param fFlags See RTLDFLAGS_.
946 * @param pErrInfo Where to return extended error information.
947 * Optional.
948 *
949 */
950static int supR3HardenedLdrLoadIt(const char *pszFilename, PRTLDRMOD phLdrMod, uint32_t fFlags, PRTERRINFO pErrInfo)
951{
952#ifdef VBOX_WITH_HARDENING
953 /*
954 * Verify the image file.
955 */
956 int rc = SUPR3HardenedVerifyInit();
957 if (RT_FAILURE(rc))
958 rc = supR3HardenedVerifyFixedFile(pszFilename, false /* fFatal */);
959 if (RT_FAILURE(rc))
960 {
961 LogRel(("supR3HardenedLdrLoadIt: Verification of \"%s\" failed, rc=%Rrc\n", pszFilename, rc));
962 return RTErrInfoSet(pErrInfo, rc, "supR3HardenedVerifyFixedFile failed");
963 }
964#endif
965
966 /*
967 * Try load it.
968 */
969 return RTLdrLoadEx(pszFilename, phLdrMod, fFlags, pErrInfo);
970}
971
972
973SUPR3DECL(int) SUPR3HardenedLdrLoad(const char *pszFilename, PRTLDRMOD phLdrMod, uint32_t fFlags, PRTERRINFO pErrInfo)
974{
975 /*
976 * Validate input.
977 */
978 RTErrInfoClear(pErrInfo);
979 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
980 AssertPtrReturn(phLdrMod, VERR_INVALID_POINTER);
981 *phLdrMod = NIL_RTLDRMOD;
982 AssertReturn(RTPathHavePath(pszFilename), VERR_INVALID_PARAMETER);
983
984 /*
985 * Add the default extension if it's missing.
986 */
987 if (!RTPathHasSuffix(pszFilename))
988 {
989 const char *pszSuff = RTLdrGetSuff();
990 size_t cchSuff = strlen(pszSuff);
991 size_t cchFilename = strlen(pszFilename);
992 char *psz = (char *)alloca(cchFilename + cchSuff + 1);
993 AssertReturn(psz, VERR_NO_TMP_MEMORY);
994 memcpy(psz, pszFilename, cchFilename);
995 memcpy(psz + cchFilename, pszSuff, cchSuff + 1);
996 pszFilename = psz;
997 }
998
999 /*
1000 * Pass it on to the common library loader.
1001 */
1002 return supR3HardenedLdrLoadIt(pszFilename, phLdrMod, fFlags, pErrInfo);
1003}
1004
1005
1006SUPR3DECL(int) SUPR3HardenedLdrLoadAppPriv(const char *pszFilename, PRTLDRMOD phLdrMod, uint32_t fFlags, PRTERRINFO pErrInfo)
1007{
1008 LogFlow(("SUPR3HardenedLdrLoadAppPriv: pszFilename=%p:{%s} phLdrMod=%p fFlags=%08x pErrInfo=%p\n", pszFilename, pszFilename, phLdrMod, fFlags, pErrInfo));
1009
1010 /*
1011 * Validate input.
1012 */
1013 RTErrInfoClear(pErrInfo);
1014 AssertPtrReturn(phLdrMod, VERR_INVALID_PARAMETER);
1015 *phLdrMod = NIL_RTLDRMOD;
1016 AssertPtrReturn(pszFilename, VERR_INVALID_PARAMETER);
1017 AssertMsgReturn(!RTPathHavePath(pszFilename), ("%s\n", pszFilename), VERR_INVALID_PARAMETER);
1018
1019 /*
1020 * Check the filename.
1021 */
1022 size_t cchFilename = strlen(pszFilename);
1023 AssertMsgReturn(cchFilename < (RTPATH_MAX / 4) * 3, ("%zu\n", cchFilename), VERR_INVALID_PARAMETER);
1024
1025 const char *pszExt = "";
1026 size_t cchExt = 0;
1027 if (!RTPathHasSuffix(pszFilename))
1028 {
1029 pszExt = RTLdrGetSuff();
1030 cchExt = strlen(pszExt);
1031 }
1032
1033 /*
1034 * Construct the private arch path and check if the file exists.
1035 */
1036 char szPath[RTPATH_MAX];
1037 int rc = RTPathAppPrivateArch(szPath, sizeof(szPath) - 1 - cchExt - cchFilename);
1038 AssertRCReturn(rc, rc);
1039
1040 char *psz = strchr(szPath, '\0');
1041 *psz++ = RTPATH_SLASH;
1042 memcpy(psz, pszFilename, cchFilename);
1043 psz += cchFilename;
1044 memcpy(psz, pszExt, cchExt + 1);
1045
1046 if (!RTPathExists(szPath))
1047 {
1048 LogRel(("SUPR3HardenedLdrLoadAppPriv: \"%s\" not found\n", szPath));
1049 return VERR_FILE_NOT_FOUND;
1050 }
1051
1052 /*
1053 * Pass it on to SUPR3HardenedLdrLoad.
1054 */
1055 rc = SUPR3HardenedLdrLoad(szPath, phLdrMod, fFlags, pErrInfo);
1056
1057 LogFlow(("SUPR3HardenedLdrLoadAppPriv: returns %Rrc\n", rc));
1058 return rc;
1059}
1060
1061
1062SUPR3DECL(int) SUPR3HardenedLdrLoadPlugIn(const char *pszFilename, PRTLDRMOD phLdrMod, PRTERRINFO pErrInfo)
1063{
1064 /*
1065 * Validate input.
1066 */
1067 RTErrInfoClear(pErrInfo);
1068 AssertPtrReturn(phLdrMod, VERR_INVALID_PARAMETER);
1069 *phLdrMod = NIL_RTLDRMOD;
1070 AssertPtrReturn(pszFilename, VERR_INVALID_PARAMETER);
1071 AssertReturn(RTPathStartsWithRoot(pszFilename), VERR_INVALID_PARAMETER);
1072
1073#ifdef VBOX_WITH_HARDENING
1074 /*
1075 * Verify the image file.
1076 */
1077 int rc = supR3HardenedVerifyFile(pszFilename, RTHCUINTPTR_MAX, true /*fMaybe3rdParty*/, pErrInfo);
1078 if (RT_FAILURE(rc))
1079 {
1080 if (!RTErrInfoIsSet(pErrInfo))
1081 LogRel(("supR3HardenedVerifyFile: Verification of \"%s\" failed, rc=%Rrc\n", pszFilename, rc));
1082 return rc;
1083 }
1084#endif
1085
1086 /*
1087 * Try load it.
1088 */
1089 return RTLdrLoadEx(pszFilename, phLdrMod, RTLDRLOAD_FLAGS_LOCAL, pErrInfo);
1090}
1091
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