VirtualBox

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

Last change on this file since 108195 was 107675, checked in by vboxsync, 6 weeks ago

HostDrivers/Support/SUPLibLdr.cpp: Remove unused assignment, bugref:3409

  • 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 107675 2025-01-10 15:51:54Z vboxsync $ */
2/** @file
3 * VirtualBox Support Library - Loader related bits.
4 */
5
6/*
7 * Copyright (C) 2006-2024 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 pArgs->uEndRva = uRvaSeg + cbMapped;
483 return VINF_SUCCESS; /* New segment was smaller than a page. */
484 }
485 cbMapped -= cbCommon;
486 uRvaSeg += cbCommon;
487 Assert(uRvaSeg - pArgs->uStartRva == PAGE_SIZE);
488 }
489
490 /* The current segment should end where the new one starts, no gaps. */
491 pArgs->uEndRva = uRvaSeg;
492
493 /* Emit the current segment */
494 Log2(("supLoadModuleCompileSegmentsCB: SUP Seg #%u: %#x LB %#x prot %#x\n",
495 pArgs->iSegs, pArgs->uStartRva, pArgs->uEndRva - pArgs->uStartRva, pArgs->fProt));
496 if (pArgs->paSegs)
497 {
498 AssertReturn(pArgs->iSegs < pArgs->cSegsAlloc, VERR_INTERNAL_ERROR_5);
499 pArgs->paSegs[pArgs->iSegs].off = pArgs->uStartRva;
500 pArgs->paSegs[pArgs->iSegs].cb = pArgs->uEndRva - pArgs->uStartRva;
501 pArgs->paSegs[pArgs->iSegs].fProt = pArgs->fProt;
502 pArgs->paSegs[pArgs->iSegs].fUnused = 0;
503 }
504 pArgs->iSegs++;
505 }
506 /* else: current segment is empty */
507
508 /* Start the new segment. */
509 Assert(!(uRvaSeg & PAGE_OFFSET_MASK));
510 pArgs->fProt = fProt;
511 pArgs->uStartRva = uRvaSeg;
512 pArgs->uEndRva = uRvaSeg + cbMapped;
513 return VINF_SUCCESS;
514}
515
516
517/**
518 * Worker for supLoadModule().
519 */
520static int supLoadModuleInner(RTLDRMOD hLdrMod, PSUPLDRLOAD pLoadReq, uint32_t cbImageWithEverything,
521 RTR0PTR uImageBase, size_t cbImage, const char *pszModule, const char *pszFilename,
522 bool fNativeLoader, bool fIsVMMR0, const char *pszSrvReqHandler,
523 uint32_t offSymTab, uint32_t cSymbols,
524 uint32_t offStrTab, size_t cbStrTab,
525 uint32_t offSegTab, uint32_t cSegments,
526 PRTERRINFO pErrInfo)
527{
528 /*
529 * Get the image bits.
530 */
531 SUPLDRRESIMPARGS Args = { pszModule, pErrInfo, 0 };
532 int rc = RTLdrGetBits(hLdrMod, &pLoadReq->u.In.abImage[0], uImageBase, supLoadModuleResolveImport, &Args);
533 if (RT_FAILURE(rc))
534 {
535 LogRel(("SUP: RTLdrGetBits failed for %s (%s). rc=%Rrc\n", pszModule, pszFilename, rc));
536 if (!RTErrInfoIsSet(pErrInfo))
537 RTErrInfoSetF(pErrInfo, rc, "RTLdrGetBits failed");
538 return rc;
539 }
540
541 /*
542 * Get the entry points.
543 */
544 RTUINTPTR VMMR0EntryFast = 0;
545 RTUINTPTR VMMR0EntryEx = 0;
546 RTUINTPTR SrvReqHandler = 0;
547 RTUINTPTR ModuleInit = 0;
548 RTUINTPTR ModuleTerm = 0;
549 const char *pszEp = NULL;
550 if (fIsVMMR0)
551 {
552 rc = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.abImage[0], uImageBase,
553 UINT32_MAX, pszEp = "VMMR0EntryFast", &VMMR0EntryFast);
554 if (RT_SUCCESS(rc))
555 rc = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.abImage[0], uImageBase,
556 UINT32_MAX, pszEp = "VMMR0EntryEx", &VMMR0EntryEx);
557 }
558 else if (pszSrvReqHandler)
559 rc = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.abImage[0], uImageBase,
560 UINT32_MAX, pszEp = pszSrvReqHandler, &SrvReqHandler);
561 if (RT_SUCCESS(rc))
562 {
563 int rc2 = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.abImage[0], uImageBase,
564 UINT32_MAX, pszEp = "ModuleInit", &ModuleInit);
565 if (RT_FAILURE(rc2))
566 ModuleInit = 0;
567
568 rc2 = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.abImage[0], uImageBase,
569 UINT32_MAX, pszEp = "ModuleTerm", &ModuleTerm);
570 if (RT_FAILURE(rc2))
571 ModuleTerm = 0;
572 }
573 if (RT_FAILURE(rc))
574 {
575 LogRel(("SUP: Failed to get entry point '%s' for %s (%s) rc=%Rrc\n", pszEp, pszModule, pszFilename, rc));
576 return RTErrInfoSetF(pErrInfo, rc, "Failed to resolve entry point '%s'", pszEp);
577 }
578
579 /*
580 * Create the symbol and string tables.
581 */
582 SUPLDRCREATETABSARGS CreateArgs;
583 CreateArgs.cbImage = cbImage;
584 CreateArgs.pSym = (PSUPLDRSYM)&pLoadReq->u.In.abImage[offSymTab];
585 CreateArgs.pszBase = (char *)&pLoadReq->u.In.abImage[offStrTab];
586 CreateArgs.psz = CreateArgs.pszBase;
587 rc = RTLdrEnumSymbols(hLdrMod, 0, NULL, 0, supLoadModuleCreateTabsCB, &CreateArgs);
588 if (RT_FAILURE(rc))
589 {
590 LogRel(("SUP: RTLdrEnumSymbols failed for %s (%s) rc=%Rrc\n", pszModule, pszFilename, rc));
591 return RTErrInfoSetF(pErrInfo, rc, "RTLdrEnumSymbols #2 failed");
592 }
593 AssertRelease((size_t)(CreateArgs.psz - CreateArgs.pszBase) <= cbStrTab);
594 AssertRelease((size_t)(CreateArgs.pSym - (PSUPLDRSYM)&pLoadReq->u.In.abImage[offSymTab]) <= cSymbols);
595
596 /*
597 * Create the segment table.
598 */
599 SUPLDRCOMPSEGTABARGS SegArgs;
600 SegArgs.uStartRva = 0;
601 SegArgs.uEndRva = 0;
602 SegArgs.fProt = RTMEM_PROT_READ;
603 SegArgs.iSegs = 0;
604 SegArgs.cSegsAlloc = cSegments;
605 SegArgs.paSegs = (PSUPLDRSEG)&pLoadReq->u.In.abImage[offSegTab];
606 SegArgs.pErrInfo = pErrInfo;
607 rc = RTLdrEnumSegments(hLdrMod, supLoadModuleCompileSegmentsCB, &SegArgs);
608 if (RT_FAILURE(rc))
609 {
610 LogRel(("SUP: RTLdrEnumSegments failed for %s (%s) rc=%Rrc\n", pszModule, pszFilename, rc));
611 return RTErrInfoSetF(pErrInfo, rc, "RTLdrEnumSegments #2 failed");
612 }
613 SegArgs.uEndRva = (uint32_t)cbImage;
614 AssertReturn(SegArgs.uEndRva == cbImage, VERR_OUT_OF_RANGE);
615 if (SegArgs.uEndRva > SegArgs.uStartRva)
616 {
617 SegArgs.paSegs[SegArgs.iSegs].off = SegArgs.uStartRva;
618 SegArgs.paSegs[SegArgs.iSegs].cb = SegArgs.uEndRva - SegArgs.uStartRva;
619 SegArgs.paSegs[SegArgs.iSegs].fProt = SegArgs.fProt;
620 SegArgs.paSegs[SegArgs.iSegs].fUnused = 0;
621 SegArgs.iSegs++;
622 }
623 for (uint32_t i = 0; i < SegArgs.iSegs; i++)
624 LogRel(("SUP: seg #%u: %c%c%c %#010RX32 LB %#010RX32\n", i, /** @todo LogRel2 */
625 SegArgs.paSegs[i].fProt & SUPLDR_PROT_READ ? 'R' : ' ',
626 SegArgs.paSegs[i].fProt & SUPLDR_PROT_WRITE ? 'W' : ' ',
627 SegArgs.paSegs[i].fProt & SUPLDR_PROT_EXEC ? 'X' : ' ',
628 SegArgs.paSegs[i].off, SegArgs.paSegs[i].cb));
629 AssertRelease(SegArgs.iSegs == cSegments);
630 AssertRelease(SegArgs.cSegsAlloc == cSegments);
631
632 /*
633 * Upload the image.
634 */
635 pLoadReq->Hdr.u32Cookie = g_u32Cookie;
636 pLoadReq->Hdr.u32SessionCookie = g_u32SessionCookie;
637 pLoadReq->Hdr.cbIn = SUP_IOCTL_LDR_LOAD_SIZE_IN(cbImageWithEverything);
638 pLoadReq->Hdr.cbOut = SUP_IOCTL_LDR_LOAD_SIZE_OUT;
639 pLoadReq->Hdr.fFlags = SUPREQHDR_FLAGS_MAGIC | SUPREQHDR_FLAGS_EXTRA_IN;
640 pLoadReq->Hdr.rc = VERR_INTERNAL_ERROR;
641
642 pLoadReq->u.In.pfnModuleInit = (RTR0PTR)ModuleInit;
643 pLoadReq->u.In.pfnModuleTerm = (RTR0PTR)ModuleTerm;
644 if (fIsVMMR0)
645 {
646 pLoadReq->u.In.eEPType = SUPLDRLOADEP_VMMR0;
647 pLoadReq->u.In.EP.VMMR0.pvVMMR0EntryFast = (RTR0PTR)VMMR0EntryFast;
648 pLoadReq->u.In.EP.VMMR0.pvVMMR0EntryEx = (RTR0PTR)VMMR0EntryEx;
649 }
650 else if (pszSrvReqHandler)
651 {
652 pLoadReq->u.In.eEPType = SUPLDRLOADEP_SERVICE;
653 pLoadReq->u.In.EP.Service.pfnServiceReq = (RTR0PTR)SrvReqHandler;
654 pLoadReq->u.In.EP.Service.apvReserved[0] = NIL_RTR0PTR;
655 pLoadReq->u.In.EP.Service.apvReserved[1] = NIL_RTR0PTR;
656 pLoadReq->u.In.EP.Service.apvReserved[2] = NIL_RTR0PTR;
657 }
658 else
659 pLoadReq->u.In.eEPType = SUPLDRLOADEP_NOTHING;
660 pLoadReq->u.In.offStrTab = offStrTab;
661 pLoadReq->u.In.cbStrTab = (uint32_t)cbStrTab;
662 AssertRelease(pLoadReq->u.In.cbStrTab == cbStrTab);
663 pLoadReq->u.In.cbImageBits = (uint32_t)cbImage;
664 pLoadReq->u.In.offSymbols = offSymTab;
665 pLoadReq->u.In.cSymbols = cSymbols;
666 pLoadReq->u.In.offSegments = offSegTab;
667 pLoadReq->u.In.cSegments = cSegments;
668 pLoadReq->u.In.cbImageWithEverything = cbImageWithEverything;
669 pLoadReq->u.In.pvImageBase = uImageBase;
670 pLoadReq->u.In.fFlags = Args.fLoadReq;
671 if (!g_uSupFakeMode)
672 {
673 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LDR_LOAD, pLoadReq, SUP_IOCTL_LDR_LOAD_SIZE(cbImageWithEverything));
674 if (RT_SUCCESS(rc))
675 rc = pLoadReq->Hdr.rc;
676 else
677 LogRel(("SUP: SUP_IOCTL_LDR_LOAD ioctl for %s (%s) failed rc=%Rrc\n", pszModule, pszFilename, rc));
678 }
679 else
680 rc = VINF_SUCCESS;
681 if ( RT_SUCCESS(rc)
682 || rc == VERR_ALREADY_LOADED /* A competing process. */
683 )
684 {
685 LogRel(("SUP: Loaded %s (%s) at %#RKv - ModuleInit at %RKv and ModuleTerm at %RKv%s\n",
686 pszModule, pszFilename, uImageBase, (RTR0PTR)ModuleInit, (RTR0PTR)ModuleTerm,
687 fNativeLoader ? " using the native ring-0 loader" : ""));
688 if (fIsVMMR0)
689 {
690 g_pvVMMR0 = uImageBase;
691 LogRel(("SUP: VMMR0EntryEx located at %RKv and VMMR0EntryFast at %RKv\n", (RTR0PTR)VMMR0EntryEx, (RTR0PTR)VMMR0EntryFast));
692 }
693#ifdef RT_OS_WINDOWS
694 LogRel(("SUP: windbg> .reload /f %s=%#RKv\n", pszFilename, uImageBase));
695#endif
696 return VINF_SUCCESS;
697 }
698
699 /*
700 * Failed, bail out.
701 */
702 LogRel(("SUP: Loading failed for %s (%s) rc=%Rrc\n", pszModule, pszFilename, rc));
703 if ( pLoadReq->u.Out.uErrorMagic == SUPLDRLOAD_ERROR_MAGIC
704 && pLoadReq->u.Out.szError[0] != '\0')
705 {
706 LogRel(("SUP: %s\n", pLoadReq->u.Out.szError));
707 return RTErrInfoSet(pErrInfo, rc, pLoadReq->u.Out.szError);
708 }
709 return RTErrInfoSet(pErrInfo, rc, "SUP_IOCTL_LDR_LOAD failed");
710}
711
712
713/**
714 * Worker for SUPR3LoadModule().
715 *
716 * @returns VBox status code.
717 * @param pszFilename Name of the VMMR0 image file
718 * @param pszModule The modulen name.
719 * @param pszSrvReqHandler The service request handler symbol name,
720 * optional.
721 * @param pErrInfo Where to store detailed error info. Optional.
722 * @param ppvImageBase Where to return the load address.
723 */
724static int supLoadModule(const char *pszFilename, const char *pszModule, const char *pszSrvReqHandler,
725 PRTERRINFO pErrInfo, void **ppvImageBase)
726{
727 SUPLDROPEN OpenReq;
728
729 /*
730 * Validate input.
731 */
732 AssertPtrReturn(pszFilename, VERR_INVALID_PARAMETER);
733 AssertPtrReturn(pszModule, VERR_INVALID_PARAMETER);
734 AssertPtrReturn(ppvImageBase, VERR_INVALID_PARAMETER);
735 AssertReturn(strlen(pszModule) < sizeof(OpenReq.u.In.szName), VERR_FILENAME_TOO_LONG);
736
737 const bool fIsVMMR0 = !strcmp(pszModule, "VMMR0.r0");
738 AssertReturn(!pszSrvReqHandler || !fIsVMMR0, VERR_INTERNAL_ERROR);
739 *ppvImageBase = NULL;
740
741 /*
742 * First try open it w/o preparing a binary for loading.
743 *
744 * This will be a lot faster if it's already loaded, and it will
745 * avoid fixup issues when using wrapped binaries. With wrapped
746 * ring-0 binaries not all binaries need to be wrapped, so trying
747 * to load it ourselves is not a bug, but intentional behaviour
748 * (even it it asserts in the loader code).
749 */
750 OpenReq.Hdr.u32Cookie = g_u32Cookie;
751 OpenReq.Hdr.u32SessionCookie = g_u32SessionCookie;
752 OpenReq.Hdr.cbIn = SUP_IOCTL_LDR_OPEN_SIZE_IN;
753 OpenReq.Hdr.cbOut = SUP_IOCTL_LDR_OPEN_SIZE_OUT;
754 OpenReq.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
755 OpenReq.Hdr.rc = VERR_INTERNAL_ERROR;
756 OpenReq.u.In.cbImageWithEverything = 0;
757 OpenReq.u.In.cbImageBits = 0;
758 strcpy(OpenReq.u.In.szName, pszModule);
759 int rc = RTPathAbs(pszFilename, OpenReq.u.In.szFilename, sizeof(OpenReq.u.In.szFilename));
760 if (RT_FAILURE(rc))
761 return rc;
762 if ( (SUPDRV_IOC_VERSION & 0xffff0000) != 0x00300000
763 || g_uSupSessionVersion >= 0x00300001)
764 {
765 if (!g_uSupFakeMode)
766 {
767 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LDR_OPEN, &OpenReq, SUP_IOCTL_LDR_OPEN_SIZE);
768 if (RT_SUCCESS(rc))
769 rc = OpenReq.Hdr.rc;
770 }
771 else
772 {
773 OpenReq.u.Out.fNeedsLoading = true;
774 OpenReq.u.Out.pvImageBase = 0xef423420;
775 }
776 *ppvImageBase = (void *)OpenReq.u.Out.pvImageBase;
777 if (rc != VERR_MODULE_NOT_FOUND)
778 {
779 if (fIsVMMR0)
780 g_pvVMMR0 = OpenReq.u.Out.pvImageBase;
781 LogRel(("SUP: Opened %s (%s) at %#RKv%s.\n", pszModule, pszFilename, OpenReq.u.Out.pvImageBase,
782 OpenReq.u.Out.fNativeLoader ? " loaded by the native ring-0 loader" : ""));
783#ifdef RT_OS_WINDOWS
784 LogRel(("SUP: windbg> .reload /f %s=%#RKv\n", pszFilename, OpenReq.u.Out.pvImageBase));
785#endif
786 return rc;
787 }
788 }
789
790 /*
791 * Open image file and figure its size.
792 */
793 RTLDRMOD hLdrMod;
794 rc = RTLdrOpenEx(OpenReq.u.In.szFilename, 0 /*fFlags*/, RTLDRARCH_HOST, &hLdrMod, pErrInfo);
795 if (RT_FAILURE(rc))
796 {
797 LogRel(("SUP: RTLdrOpen failed for %s (%s) %Rrc\n", pszModule, OpenReq.u.In.szFilename, rc));
798 return rc;
799 }
800
801 SUPLDRCALCSIZEARGS CalcArgs;
802 CalcArgs.cbStrings = 0;
803 CalcArgs.cSymbols = 0;
804 CalcArgs.cbImage = RTLdrSize(hLdrMod);
805 rc = RTLdrEnumSymbols(hLdrMod, 0, NULL, 0, supLoadModuleCalcSizeCB, &CalcArgs);
806 if (RT_SUCCESS(rc))
807 {
808 /*
809 * Figure out the number of segments needed first.
810 */
811 SUPLDRCOMPSEGTABARGS SegArgs;
812 SegArgs.uStartRva = 0;
813 SegArgs.uEndRva = 0;
814 SegArgs.fProt = RTMEM_PROT_READ;
815 SegArgs.iSegs = 0;
816 SegArgs.cSegsAlloc = 0;
817 SegArgs.paSegs = NULL;
818 SegArgs.pErrInfo = pErrInfo;
819 rc = RTLdrEnumSegments(hLdrMod, supLoadModuleCompileSegmentsCB, &SegArgs);
820 if (RT_SUCCESS(rc))
821 {
822 Assert(SegArgs.uEndRva <= RTLdrSize(hLdrMod));
823 SegArgs.uEndRva = (uint32_t)CalcArgs.cbImage; /* overflow is checked later */
824 if (SegArgs.uEndRva > SegArgs.uStartRva)
825 {
826 Log2(("supLoadModule: SUP Seg #%u: %#x LB %#x prot %#x\n",
827 SegArgs.iSegs, SegArgs.uStartRva, SegArgs.uEndRva - SegArgs.uStartRva, SegArgs.fProt));
828 SegArgs.iSegs++;
829 }
830
831 const uint32_t offSymTab = RT_ALIGN_32(CalcArgs.cbImage, 8);
832 const uint32_t offStrTab = offSymTab + CalcArgs.cSymbols * sizeof(SUPLDRSYM);
833 const uint32_t offSegTab = RT_ALIGN_32(offStrTab + CalcArgs.cbStrings, 8);
834 const uint32_t cbImageWithEverything = RT_ALIGN_32(offSegTab + sizeof(SUPLDRSEG) * SegArgs.iSegs, 8);
835
836 /*
837 * Open the R0 image.
838 */
839 OpenReq.Hdr.u32Cookie = g_u32Cookie;
840 OpenReq.Hdr.u32SessionCookie = g_u32SessionCookie;
841 OpenReq.Hdr.cbIn = SUP_IOCTL_LDR_OPEN_SIZE_IN;
842 OpenReq.Hdr.cbOut = SUP_IOCTL_LDR_OPEN_SIZE_OUT;
843 OpenReq.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
844 OpenReq.Hdr.rc = VERR_INTERNAL_ERROR;
845 OpenReq.u.In.cbImageWithEverything = cbImageWithEverything;
846 OpenReq.u.In.cbImageBits = (uint32_t)CalcArgs.cbImage;
847 strcpy(OpenReq.u.In.szName, pszModule);
848 rc = RTPathAbs(pszFilename, OpenReq.u.In.szFilename, sizeof(OpenReq.u.In.szFilename));
849 AssertRC(rc);
850 if (RT_SUCCESS(rc))
851 {
852 if (!g_uSupFakeMode)
853 {
854 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LDR_OPEN, &OpenReq, SUP_IOCTL_LDR_OPEN_SIZE);
855 if (RT_SUCCESS(rc))
856 rc = OpenReq.Hdr.rc;
857 }
858 else
859 {
860 OpenReq.u.Out.fNeedsLoading = true;
861 OpenReq.u.Out.pvImageBase = 0xef423420;
862 }
863 }
864 *ppvImageBase = (void *)OpenReq.u.Out.pvImageBase;
865 if ( RT_SUCCESS(rc)
866 && OpenReq.u.Out.fNeedsLoading)
867 {
868 /*
869 * We need to load it.
870 *
871 * Allocate the request and pass it to an inner work function
872 * that populates it and sends it off to the driver.
873 */
874 const uint32_t cbLoadReq = SUP_IOCTL_LDR_LOAD_SIZE(cbImageWithEverything);
875 PSUPLDRLOAD pLoadReq = (PSUPLDRLOAD)RTMemTmpAlloc(cbLoadReq);
876 if (pLoadReq)
877 {
878 rc = supLoadModuleInner(hLdrMod, pLoadReq, cbImageWithEverything, OpenReq.u.Out.pvImageBase, CalcArgs.cbImage,
879 pszModule, pszFilename, OpenReq.u.Out.fNativeLoader, fIsVMMR0, pszSrvReqHandler,
880 offSymTab, CalcArgs.cSymbols,
881 offStrTab, CalcArgs.cbStrings,
882 offSegTab, SegArgs.iSegs,
883 pErrInfo);
884 RTMemTmpFree(pLoadReq);
885 }
886 else
887 {
888 AssertMsgFailed(("failed to allocated %u bytes for SUPLDRLOAD_IN structure!\n", SUP_IOCTL_LDR_LOAD_SIZE(cbImageWithEverything)));
889 rc = RTErrInfoSetF(pErrInfo, VERR_NO_TMP_MEMORY, "Failed to allocate %u bytes for the load request",
890 SUP_IOCTL_LDR_LOAD_SIZE(cbImageWithEverything));
891 }
892 }
893 /*
894 * Already loaded?
895 */
896 else if (RT_SUCCESS(rc))
897 {
898 if (fIsVMMR0)
899 g_pvVMMR0 = OpenReq.u.Out.pvImageBase;
900 LogRel(("SUP: Opened %s (%s) at %#RKv%s.\n", pszModule, pszFilename, OpenReq.u.Out.pvImageBase,
901 OpenReq.u.Out.fNativeLoader ? " loaded by the native ring-0 loader" : ""));
902#ifdef RT_OS_WINDOWS
903 LogRel(("SUP: windbg> .reload /f %s=%#RKv\n", pszFilename, OpenReq.u.Out.pvImageBase));
904#endif
905 }
906 /*
907 * No, failed.
908 */
909 else
910 RTErrInfoSet(pErrInfo, rc, "SUP_IOCTL_LDR_OPEN failed");
911 }
912 else if (!RTErrInfoIsSet(pErrInfo) && pErrInfo)
913 RTErrInfoSetF(pErrInfo, rc, "RTLdrEnumSegments #1 failed");
914 }
915 else
916 RTErrInfoSetF(pErrInfo, rc, "RTLdrEnumSymbols #1 failed");
917 RTLdrClose(hLdrMod);
918 return rc;
919}
920
921
922SUPR3DECL(int) SUPR3FreeModule(void *pvImageBase)
923{
924 /* fake */
925 if (RT_UNLIKELY(g_uSupFakeMode))
926 {
927 g_pvVMMR0 = NIL_RTR0PTR;
928 return VINF_SUCCESS;
929 }
930
931 /*
932 * Free the requested module.
933 */
934 SUPLDRFREE Req;
935 Req.Hdr.u32Cookie = g_u32Cookie;
936 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
937 Req.Hdr.cbIn = SUP_IOCTL_LDR_FREE_SIZE_IN;
938 Req.Hdr.cbOut = SUP_IOCTL_LDR_FREE_SIZE_OUT;
939 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
940 Req.Hdr.rc = VERR_INTERNAL_ERROR;
941 Req.u.In.pvImageBase = (RTR0PTR)pvImageBase;
942 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LDR_FREE, &Req, SUP_IOCTL_LDR_FREE_SIZE);
943 if (RT_SUCCESS(rc))
944 rc = Req.Hdr.rc;
945 if ( RT_SUCCESS(rc)
946 && (RTR0PTR)pvImageBase == g_pvVMMR0)
947 g_pvVMMR0 = NIL_RTR0PTR;
948 return rc;
949}
950
951
952SUPR3DECL(int) SUPR3GetSymbolR0(void *pvImageBase, const char *pszSymbol, void **ppvValue)
953{
954 *ppvValue = NULL;
955
956 /* fake */
957 if (RT_UNLIKELY(g_uSupFakeMode))
958 {
959 *ppvValue = (void *)(uintptr_t)0xdeadf00d;
960 return VINF_SUCCESS;
961 }
962
963 /*
964 * Do ioctl.
965 */
966 SUPLDRGETSYMBOL Req;
967 Req.Hdr.u32Cookie = g_u32Cookie;
968 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
969 Req.Hdr.cbIn = SUP_IOCTL_LDR_GET_SYMBOL_SIZE_IN;
970 Req.Hdr.cbOut = SUP_IOCTL_LDR_GET_SYMBOL_SIZE_OUT;
971 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
972 Req.Hdr.rc = VERR_INTERNAL_ERROR;
973 Req.u.In.pvImageBase = (RTR0PTR)pvImageBase;
974 size_t cchSymbol = strlen(pszSymbol);
975 if (cchSymbol >= sizeof(Req.u.In.szSymbol))
976 return VERR_SYMBOL_NOT_FOUND;
977 memcpy(Req.u.In.szSymbol, pszSymbol, cchSymbol + 1);
978 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LDR_GET_SYMBOL, &Req, SUP_IOCTL_LDR_GET_SYMBOL_SIZE);
979 if (RT_SUCCESS(rc))
980 rc = Req.Hdr.rc;
981 if (RT_SUCCESS(rc))
982 *ppvValue = (void *)Req.u.Out.pvSymbol;
983 return rc;
984}
985
986
987SUPR3DECL(int) SUPR3LoadVMM(const char *pszFilename, PRTERRINFO pErrInfo)
988{
989 void *pvImageBase;
990 return SUPR3LoadModule(pszFilename, "VMMR0.r0", &pvImageBase, pErrInfo);
991}
992
993
994SUPR3DECL(int) SUPR3UnloadVMM(void)
995{
996 return SUPR3FreeModule((void*)g_pvVMMR0);
997}
998
999
1000/**
1001 * Worker for SUPR3HardenedLdrLoad and SUPR3HardenedLdrLoadAppPriv.
1002 *
1003 * @returns iprt status code.
1004 * @param pszFilename The full file name.
1005 * @param phLdrMod Where to store the handle to the loaded module.
1006 * @param fFlags See RTLDFLAGS_.
1007 * @param pErrInfo Where to return extended error information.
1008 * Optional.
1009 *
1010 */
1011static int supR3HardenedLdrLoadIt(const char *pszFilename, PRTLDRMOD phLdrMod, uint32_t fFlags, PRTERRINFO pErrInfo)
1012{
1013#ifdef VBOX_WITH_HARDENING
1014 /*
1015 * Verify the image file.
1016 */
1017 int rc = SUPR3HardenedVerifyInit();
1018 if (RT_FAILURE(rc))
1019 rc = supR3HardenedVerifyFixedFile(pszFilename, false /* fFatal */);
1020 if (RT_FAILURE(rc))
1021 {
1022 LogRel(("supR3HardenedLdrLoadIt: Verification of \"%s\" failed, rc=%Rrc\n", pszFilename, rc));
1023 return RTErrInfoSet(pErrInfo, rc, "supR3HardenedVerifyFixedFile failed");
1024 }
1025#endif
1026
1027 /*
1028 * Try load it.
1029 */
1030 return RTLdrLoadEx(pszFilename, phLdrMod, fFlags, pErrInfo);
1031}
1032
1033
1034SUPR3DECL(int) SUPR3HardenedLdrLoad(const char *pszFilename, PRTLDRMOD phLdrMod, uint32_t fFlags, PRTERRINFO pErrInfo)
1035{
1036 /*
1037 * Validate input.
1038 */
1039 RTErrInfoClear(pErrInfo);
1040 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
1041 AssertPtrReturn(phLdrMod, VERR_INVALID_POINTER);
1042 *phLdrMod = NIL_RTLDRMOD;
1043 AssertReturn(RTPathHavePath(pszFilename), VERR_INVALID_PARAMETER);
1044
1045 /*
1046 * Add the default extension if it's missing.
1047 */
1048 if (!RTPathHasSuffix(pszFilename))
1049 {
1050 const char *pszSuff = RTLdrGetSuff();
1051 size_t cchSuff = strlen(pszSuff);
1052 size_t cchFilename = strlen(pszFilename);
1053 char *psz = (char *)alloca(cchFilename + cchSuff + 1);
1054 AssertReturn(psz, VERR_NO_TMP_MEMORY);
1055 memcpy(psz, pszFilename, cchFilename);
1056 memcpy(psz + cchFilename, pszSuff, cchSuff + 1);
1057 pszFilename = psz;
1058 }
1059
1060 /*
1061 * Pass it on to the common library loader.
1062 */
1063 return supR3HardenedLdrLoadIt(pszFilename, phLdrMod, fFlags, pErrInfo);
1064}
1065
1066
1067SUPR3DECL(int) SUPR3HardenedLdrLoadAppPriv(const char *pszFilename, PRTLDRMOD phLdrMod, uint32_t fFlags, PRTERRINFO pErrInfo)
1068{
1069 LogFlow(("SUPR3HardenedLdrLoadAppPriv: pszFilename=%p:{%s} phLdrMod=%p fFlags=%08x pErrInfo=%p\n", pszFilename, pszFilename, phLdrMod, fFlags, pErrInfo));
1070
1071 /*
1072 * Validate input.
1073 */
1074 RTErrInfoClear(pErrInfo);
1075 AssertPtrReturn(phLdrMod, VERR_INVALID_PARAMETER);
1076 *phLdrMod = NIL_RTLDRMOD;
1077 AssertPtrReturn(pszFilename, VERR_INVALID_PARAMETER);
1078 AssertMsgReturn(!RTPathHavePath(pszFilename), ("%s\n", pszFilename), VERR_INVALID_PARAMETER);
1079
1080 /*
1081 * Check the filename.
1082 */
1083 size_t cchFilename = strlen(pszFilename);
1084 AssertMsgReturn(cchFilename < (RTPATH_MAX / 4) * 3, ("%zu\n", cchFilename), VERR_INVALID_PARAMETER);
1085
1086 const char *pszExt = "";
1087 size_t cchExt = 0;
1088 if (!RTPathHasSuffix(pszFilename))
1089 {
1090 pszExt = RTLdrGetSuff();
1091 cchExt = strlen(pszExt);
1092 }
1093
1094 /*
1095 * Construct the private arch path and check if the file exists.
1096 */
1097 char szPath[RTPATH_MAX];
1098 int rc = RTPathAppPrivateArch(szPath, sizeof(szPath) - 1 - cchExt - cchFilename);
1099 AssertRCReturn(rc, rc);
1100
1101 char *psz = strchr(szPath, '\0');
1102 *psz++ = RTPATH_SLASH;
1103 memcpy(psz, pszFilename, cchFilename);
1104 psz += cchFilename;
1105 memcpy(psz, pszExt, cchExt + 1);
1106
1107 if (!RTPathExists(szPath))
1108 {
1109 LogRel(("SUPR3HardenedLdrLoadAppPriv: \"%s\" not found\n", szPath));
1110 return VERR_FILE_NOT_FOUND;
1111 }
1112
1113 /*
1114 * Pass it on to SUPR3HardenedLdrLoad.
1115 */
1116 rc = SUPR3HardenedLdrLoad(szPath, phLdrMod, fFlags, pErrInfo);
1117
1118 LogFlow(("SUPR3HardenedLdrLoadAppPriv: returns %Rrc\n", rc));
1119 return rc;
1120}
1121
1122
1123SUPR3DECL(int) SUPR3HardenedLdrLoadPlugIn(const char *pszFilename, PRTLDRMOD phLdrMod, PRTERRINFO pErrInfo)
1124{
1125 /*
1126 * Validate input.
1127 */
1128 RTErrInfoClear(pErrInfo);
1129 AssertPtrReturn(phLdrMod, VERR_INVALID_PARAMETER);
1130 *phLdrMod = NIL_RTLDRMOD;
1131 AssertPtrReturn(pszFilename, VERR_INVALID_PARAMETER);
1132 AssertReturn(RTPathStartsWithRoot(pszFilename), VERR_INVALID_PARAMETER);
1133
1134#ifdef VBOX_WITH_HARDENING
1135 /*
1136 * Verify the image file.
1137 */
1138 int rc = supR3HardenedVerifyFile(pszFilename, RTHCUINTPTR_MAX, true /*fMaybe3rdParty*/, pErrInfo);
1139 if (RT_FAILURE(rc))
1140 {
1141 if (!RTErrInfoIsSet(pErrInfo))
1142 LogRel(("supR3HardenedVerifyFile: Verification of \"%s\" failed, rc=%Rrc\n", pszFilename, rc));
1143 return rc;
1144 }
1145#endif
1146
1147 /*
1148 * Try load it.
1149 */
1150 return RTLdrLoadEx(pszFilename, phLdrMod, RTLDRLOAD_FLAGS_LOCAL, pErrInfo);
1151}
1152
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