VirtualBox

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

Last change on this file since 97655 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

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