VirtualBox

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

Last change on this file since 67954 was 67904, checked in by vboxsync, 7 years ago

SUPLibLdr.cpp: log typo

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 32.8 KB
Line 
1/* $Id: SUPLibLdr.cpp 67904 2017-07-11 11:54:41Z vboxsync $ */
2/** @file
3 * VirtualBox Support Library - Loader related bits.
4 */
5
6/*
7 * Copyright (C) 2006-2016 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27/** @page pg_sup SUP - The Support Library
28 *
29 * The support library is responsible for providing facilities to load
30 * VMM Host Ring-0 code, to call Host VMM Ring-0 code from Ring-3 Host
31 * code, to pin down physical memory, and more.
32 *
33 * The VMM Host Ring-0 code can be combined in the support driver if
34 * permitted by kernel module license policies. If it is not combined
35 * it will be externalized in a .r0 module that will be loaded using
36 * the IPRT loader.
37 *
38 * The Ring-0 calling is done thru a generic SUP interface which will
39 * transfer an argument set and call a predefined entry point in the Host
40 * VMM Ring-0 code.
41 *
42 * See @ref grp_sup "SUP - Support APIs" for API details.
43 */
44
45
46/*********************************************************************************************************************************
47* Header Files *
48*********************************************************************************************************************************/
49#define LOG_GROUP LOG_GROUP_SUP
50#include <VBox/sup.h>
51#include <VBox/err.h>
52#include <VBox/param.h>
53#include <VBox/log.h>
54#include <VBox/VBoxTpG.h>
55
56#include <iprt/assert.h>
57#include <iprt/alloc.h>
58#include <iprt/alloca.h>
59#include <iprt/ldr.h>
60#include <iprt/asm.h>
61#include <iprt/mp.h>
62#include <iprt/cpuset.h>
63#include <iprt/thread.h>
64#include <iprt/process.h>
65#include <iprt/path.h>
66#include <iprt/string.h>
67#include <iprt/env.h>
68#include <iprt/rand.h>
69#include <iprt/x86.h>
70
71#include "SUPDrvIOC.h"
72#include "SUPLibInternal.h"
73
74
75/*********************************************************************************************************************************
76* Defined Constants And Macros *
77*********************************************************************************************************************************/
78/** R0 VMM module name. */
79#define VMMR0_NAME "VMMR0"
80
81
82/*********************************************************************************************************************************
83* Structures and Typedefs *
84*********************************************************************************************************************************/
85typedef DECLCALLBACK(int) FNCALLVMMR0(PVMR0 pVMR0, unsigned uOperation, void *pvArg);
86typedef FNCALLVMMR0 *PFNCALLVMMR0;
87
88
89/*********************************************************************************************************************************
90* Global Variables *
91*********************************************************************************************************************************/
92/** VMMR0 Load Address. */
93static RTR0PTR g_pvVMMR0 = NIL_RTR0PTR;
94
95
96/*********************************************************************************************************************************
97* Internal Functions *
98*********************************************************************************************************************************/
99static int supLoadModule(const char *pszFilename, const char *pszModule, const char *pszSrvReqHandler,
100 PRTERRINFO pErrInfo, void **ppvImageBase);
101static DECLCALLBACK(int) supLoadModuleResolveImport(RTLDRMOD hLdrMod, const char *pszModule, const char *pszSymbol,
102 unsigned uSymbol, RTUINTPTR *pValue, void *pvUser);
103
104
105SUPR3DECL(int) SUPR3LoadModule(const char *pszFilename, const char *pszModule, void **ppvImageBase, PRTERRINFO pErrInfo)
106{
107 /*
108 * Check that the module can be trusted.
109 */
110 int rc = SUPR3HardenedVerifyPlugIn(pszFilename, pErrInfo);
111 if (RT_SUCCESS(rc))
112 {
113 rc = supLoadModule(pszFilename, pszModule, NULL, pErrInfo, ppvImageBase);
114 if (RT_FAILURE(rc) && !RTErrInfoIsSet(pErrInfo))
115 RTErrInfoSetF(pErrInfo, rc, "SUPR3LoadModule: supLoadModule returned %Rrc", rc);
116 }
117 return rc;
118}
119
120
121SUPR3DECL(int) SUPR3LoadServiceModule(const char *pszFilename, const char *pszModule,
122 const char *pszSrvReqHandler, void **ppvImageBase)
123{
124 AssertPtrReturn(pszSrvReqHandler, VERR_INVALID_PARAMETER);
125
126 /*
127 * Check that the module can be trusted.
128 */
129 int rc = SUPR3HardenedVerifyPlugIn(pszFilename, NULL /*pErrInfo*/);
130 if (RT_SUCCESS(rc))
131 rc = supLoadModule(pszFilename, pszModule, pszSrvReqHandler, NULL /*pErrInfo*/, ppvImageBase);
132 else
133 LogRel(("SUPR3LoadServiceModule: Verification of \"%s\" failed, rc=%Rrc\n", pszFilename, rc));
134 return rc;
135}
136
137
138/**
139 * Argument package for supLoadModuleResolveImport.
140 */
141typedef struct SUPLDRRESIMPARGS
142{
143 const char *pszModule;
144 PRTERRINFO pErrInfo;
145} SUPLDRRESIMPARGS, *PSUPLDRRESIMPARGS;
146
147/**
148 * Resolve an external symbol during RTLdrGetBits().
149 *
150 * @returns VBox status code.
151 * @param hLdrMod The loader module handle.
152 * @param pszModule Module name.
153 * @param pszSymbol Symbol name, NULL if uSymbol should be used.
154 * @param uSymbol Symbol ordinal, ~0 if pszSymbol should be used.
155 * @param pValue Where to store the symbol value (address).
156 * @param pvUser User argument.
157 */
158static DECLCALLBACK(int) supLoadModuleResolveImport(RTLDRMOD hLdrMod, const char *pszModule,
159 const char *pszSymbol, unsigned uSymbol, RTUINTPTR *pValue, void *pvUser)
160{
161 NOREF(hLdrMod); NOREF(uSymbol);
162 AssertPtr(pValue);
163 AssertPtr(pvUser);
164 PSUPLDRRESIMPARGS pArgs = (PSUPLDRRESIMPARGS)pvUser;
165
166 /*
167 * Only SUPR0 and VMMR0.r0
168 */
169 if ( pszModule
170 && *pszModule
171 && strcmp(pszModule, "VBoxDrv.sys")
172 && strcmp(pszModule, "VMMR0.r0"))
173 {
174 AssertMsgFailed(("%s is importing from %s! (expected 'SUPR0.dll' or 'VMMR0.r0', case-sensitive)\n", pArgs->pszModule, pszModule));
175 return RTErrInfoSetF(pArgs->pErrInfo, VERR_SYMBOL_NOT_FOUND,
176 "Unexpected import module '%s' in '%s'", pszModule, pArgs->pszModule);
177 }
178
179 /*
180 * No ordinals.
181 */
182 if (uSymbol != ~0U)
183 {
184 AssertMsgFailed(("%s is importing by ordinal (ord=%d)\n", pArgs->pszModule, uSymbol));
185 return RTErrInfoSetF(pArgs->pErrInfo, VERR_SYMBOL_NOT_FOUND,
186 "Unexpected ordinal import (%#x) in '%s'", uSymbol, pArgs->pszModule);
187 }
188
189 /*
190 * Lookup symbol.
191 */
192 /** @todo is this actually used??? */
193 /* skip the 64-bit ELF import prefix first. */
194 if (!strncmp(pszSymbol, RT_STR_TUPLE("SUPR0$")))
195 pszSymbol += sizeof("SUPR0$") - 1;
196
197 /*
198 * Check the VMMR0.r0 module if loaded.
199 */
200 /** @todo call the SUPR3LoadModule caller.... */
201 /** @todo proper reference counting and such. */
202 if (g_pvVMMR0 != NIL_RTR0PTR)
203 {
204 void *pvValue;
205 if (!SUPR3GetSymbolR0((void *)g_pvVMMR0, pszSymbol, &pvValue))
206 {
207 *pValue = (uintptr_t)pvValue;
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/**
349 * Worker for SUPR3LoadModule().
350 *
351 * @returns VBox status code.
352 * @param pszFilename Name of the VMMR0 image file
353 * @param pszModule The modulen name.
354 * @param pszSrvReqHandler The service request handler symbol name,
355 * optional.
356 * @param pErrInfo Where to store detailed error info. Optional.
357 * @param ppvImageBase Where to return the load address.
358 */
359static int supLoadModule(const char *pszFilename, const char *pszModule, const char *pszSrvReqHandler,
360 PRTERRINFO pErrInfo, void **ppvImageBase)
361{
362 int rc;
363
364 /*
365 * Validate input.
366 */
367 AssertPtrReturn(pszFilename, VERR_INVALID_PARAMETER);
368 AssertPtrReturn(pszModule, VERR_INVALID_PARAMETER);
369 AssertPtrReturn(ppvImageBase, VERR_INVALID_PARAMETER);
370 AssertReturn(strlen(pszModule) < RT_SIZEOFMEMB(SUPLDROPEN, u.In.szName), VERR_FILENAME_TOO_LONG);
371 char szAbsFilename[RT_SIZEOFMEMB(SUPLDROPEN, u.In.szFilename)];
372 rc = RTPathAbs(pszFilename, szAbsFilename, sizeof(szAbsFilename));
373 if (RT_FAILURE(rc))
374 return rc;
375 pszFilename = szAbsFilename;
376
377 const bool fIsVMMR0 = !strcmp(pszModule, "VMMR0.r0");
378 AssertReturn(!pszSrvReqHandler || !fIsVMMR0, VERR_INTERNAL_ERROR);
379 *ppvImageBase = NULL;
380
381 /*
382 * Open image file and figure its size.
383 */
384 RTLDRMOD hLdrMod;
385 rc = RTLdrOpen(pszFilename, 0, RTLDRARCH_HOST, &hLdrMod);
386 if (!RT_SUCCESS(rc))
387 {
388 LogRel(("SUP: RTLdrOpen failed for %s (%s) %Rrc\n", pszModule, pszFilename, rc));
389 return rc;
390 }
391
392 SUPLDRCALCSIZEARGS CalcArgs;
393 CalcArgs.cbStrings = 0;
394 CalcArgs.cSymbols = 0;
395 CalcArgs.cbImage = RTLdrSize(hLdrMod);
396 rc = RTLdrEnumSymbols(hLdrMod, 0, NULL, 0, supLoadModuleCalcSizeCB, &CalcArgs);
397 if (RT_SUCCESS(rc))
398 {
399 const uint32_t offSymTab = RT_ALIGN_32(CalcArgs.cbImage, 8);
400 const uint32_t offStrTab = offSymTab + CalcArgs.cSymbols * sizeof(SUPLDRSYM);
401 const uint32_t cbImageWithTabs = RT_ALIGN_32(offStrTab + CalcArgs.cbStrings, 8);
402
403 /*
404 * Open the R0 image.
405 */
406 SUPLDROPEN OpenReq;
407 OpenReq.Hdr.u32Cookie = g_u32Cookie;
408 OpenReq.Hdr.u32SessionCookie = g_u32SessionCookie;
409 OpenReq.Hdr.cbIn = SUP_IOCTL_LDR_OPEN_SIZE_IN;
410 OpenReq.Hdr.cbOut = SUP_IOCTL_LDR_OPEN_SIZE_OUT;
411 OpenReq.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
412 OpenReq.Hdr.rc = VERR_INTERNAL_ERROR;
413 OpenReq.u.In.cbImageWithTabs = cbImageWithTabs;
414 OpenReq.u.In.cbImageBits = (uint32_t)CalcArgs.cbImage;
415 strcpy(OpenReq.u.In.szName, pszModule);
416 strcpy(OpenReq.u.In.szFilename, pszFilename);
417 if (!g_uSupFakeMode)
418 {
419 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LDR_OPEN, &OpenReq, SUP_IOCTL_LDR_OPEN_SIZE);
420 if (RT_SUCCESS(rc))
421 rc = OpenReq.Hdr.rc;
422 }
423 else
424 {
425 OpenReq.u.Out.fNeedsLoading = true;
426 OpenReq.u.Out.pvImageBase = 0xef423420;
427 }
428 *ppvImageBase = (void *)OpenReq.u.Out.pvImageBase;
429 if ( RT_SUCCESS(rc)
430 && OpenReq.u.Out.fNeedsLoading)
431 {
432 /*
433 * We need to load it.
434 * Allocate memory for the image bits.
435 */
436 PSUPLDRLOAD pLoadReq = (PSUPLDRLOAD)RTMemTmpAlloc(SUP_IOCTL_LDR_LOAD_SIZE(cbImageWithTabs));
437 if (pLoadReq)
438 {
439 /*
440 * Get the image bits.
441 */
442
443 SUPLDRRESIMPARGS Args = { pszModule, pErrInfo };
444 rc = RTLdrGetBits(hLdrMod, &pLoadReq->u.In.abImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase,
445 supLoadModuleResolveImport, &Args);
446
447 if (RT_SUCCESS(rc))
448 {
449 /*
450 * Get the entry points.
451 */
452 RTUINTPTR VMMR0EntryFast = 0;
453 RTUINTPTR VMMR0EntryEx = 0;
454 RTUINTPTR SrvReqHandler = 0;
455 RTUINTPTR ModuleInit = 0;
456 RTUINTPTR ModuleTerm = 0;
457 const char *pszEp = NULL;
458 if (fIsVMMR0)
459 {
460 rc = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.abImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase,
461 UINT32_MAX, pszEp = "VMMR0EntryFast", &VMMR0EntryFast);
462 if (RT_SUCCESS(rc))
463 rc = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.abImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase,
464 UINT32_MAX, pszEp = "VMMR0EntryEx", &VMMR0EntryEx);
465 }
466 else if (pszSrvReqHandler)
467 rc = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.abImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase,
468 UINT32_MAX, pszEp = pszSrvReqHandler, &SrvReqHandler);
469 if (RT_SUCCESS(rc))
470 {
471 int rc2 = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.abImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase,
472 UINT32_MAX, pszEp = "ModuleInit", &ModuleInit);
473 if (RT_FAILURE(rc2))
474 ModuleInit = 0;
475
476 rc2 = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.abImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase,
477 UINT32_MAX, pszEp = "ModuleTerm", &ModuleTerm);
478 if (RT_FAILURE(rc2))
479 ModuleTerm = 0;
480 }
481 if (RT_SUCCESS(rc))
482 {
483 /*
484 * Create the symbol and string tables.
485 */
486 SUPLDRCREATETABSARGS CreateArgs;
487 CreateArgs.cbImage = CalcArgs.cbImage;
488 CreateArgs.pSym = (PSUPLDRSYM)&pLoadReq->u.In.abImage[offSymTab];
489 CreateArgs.pszBase = (char *)&pLoadReq->u.In.abImage[offStrTab];
490 CreateArgs.psz = CreateArgs.pszBase;
491 rc = RTLdrEnumSymbols(hLdrMod, 0, NULL, 0, supLoadModuleCreateTabsCB, &CreateArgs);
492 if (RT_SUCCESS(rc))
493 {
494 AssertRelease((size_t)(CreateArgs.psz - CreateArgs.pszBase) <= CalcArgs.cbStrings);
495 AssertRelease((size_t)(CreateArgs.pSym - (PSUPLDRSYM)&pLoadReq->u.In.abImage[offSymTab]) <= CalcArgs.cSymbols);
496
497 /*
498 * Upload the image.
499 */
500 pLoadReq->Hdr.u32Cookie = g_u32Cookie;
501 pLoadReq->Hdr.u32SessionCookie = g_u32SessionCookie;
502 pLoadReq->Hdr.cbIn = SUP_IOCTL_LDR_LOAD_SIZE_IN(cbImageWithTabs);
503 pLoadReq->Hdr.cbOut = SUP_IOCTL_LDR_LOAD_SIZE_OUT;
504 pLoadReq->Hdr.fFlags = SUPREQHDR_FLAGS_MAGIC | SUPREQHDR_FLAGS_EXTRA_IN;
505 pLoadReq->Hdr.rc = VERR_INTERNAL_ERROR;
506
507 pLoadReq->u.In.pfnModuleInit = (RTR0PTR)ModuleInit;
508 pLoadReq->u.In.pfnModuleTerm = (RTR0PTR)ModuleTerm;
509 if (fIsVMMR0)
510 {
511 pLoadReq->u.In.eEPType = SUPLDRLOADEP_VMMR0;
512 pLoadReq->u.In.EP.VMMR0.pvVMMR0 = OpenReq.u.Out.pvImageBase;
513 pLoadReq->u.In.EP.VMMR0.pvVMMR0EntryFast= (RTR0PTR)VMMR0EntryFast;
514 pLoadReq->u.In.EP.VMMR0.pvVMMR0EntryEx = (RTR0PTR)VMMR0EntryEx;
515 }
516 else if (pszSrvReqHandler)
517 {
518 pLoadReq->u.In.eEPType = SUPLDRLOADEP_SERVICE;
519 pLoadReq->u.In.EP.Service.pfnServiceReq = (RTR0PTR)SrvReqHandler;
520 pLoadReq->u.In.EP.Service.apvReserved[0] = NIL_RTR0PTR;
521 pLoadReq->u.In.EP.Service.apvReserved[1] = NIL_RTR0PTR;
522 pLoadReq->u.In.EP.Service.apvReserved[2] = NIL_RTR0PTR;
523 }
524 else
525 pLoadReq->u.In.eEPType = SUPLDRLOADEP_NOTHING;
526 pLoadReq->u.In.offStrTab = offStrTab;
527 pLoadReq->u.In.cbStrTab = (uint32_t)CalcArgs.cbStrings;
528 AssertRelease(pLoadReq->u.In.cbStrTab == CalcArgs.cbStrings);
529 pLoadReq->u.In.cbImageBits = (uint32_t)CalcArgs.cbImage;
530 pLoadReq->u.In.offSymbols = offSymTab;
531 pLoadReq->u.In.cSymbols = CalcArgs.cSymbols;
532 pLoadReq->u.In.cbImageWithTabs = cbImageWithTabs;
533 pLoadReq->u.In.pvImageBase = OpenReq.u.Out.pvImageBase;
534 if (!g_uSupFakeMode)
535 {
536 rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LDR_LOAD, pLoadReq, SUP_IOCTL_LDR_LOAD_SIZE(cbImageWithTabs));
537 if (RT_SUCCESS(rc))
538 rc = pLoadReq->Hdr.rc;
539 else
540 LogRel(("SUP: SUP_IOCTL_LDR_LOAD ioctl for %s (%s) failed rc=%Rrc\n", pszModule, pszFilename, rc));
541 }
542 else
543 rc = VINF_SUCCESS;
544 if ( RT_SUCCESS(rc)
545 || rc == VERR_ALREADY_LOADED /* A competing process. */
546 )
547 {
548 LogRel(("SUP: Loaded %s (%s) at %#RKv - ModuleInit at %RKv and ModuleTerm at %RKv%s\n",
549 pszModule, pszFilename, OpenReq.u.Out.pvImageBase, (RTR0PTR)ModuleInit, (RTR0PTR)ModuleTerm,
550 OpenReq.u.Out.fNativeLoader ? " using the native ring-0 loader" : ""));
551 if (fIsVMMR0)
552 {
553 g_pvVMMR0 = OpenReq.u.Out.pvImageBase;
554 LogRel(("SUP: VMMR0EntryEx located at %RKv and VMMR0EntryFast at %RKv\n", (RTR0PTR)VMMR0EntryEx, (RTR0PTR)VMMR0EntryFast));
555 }
556#ifdef RT_OS_WINDOWS
557 LogRel(("SUP: windbg> .reload /f %s=%#RKv\n", pszFilename, OpenReq.u.Out.pvImageBase));
558#endif
559
560 RTMemTmpFree(pLoadReq);
561 RTLdrClose(hLdrMod);
562 return VINF_SUCCESS;
563 }
564
565 /*
566 * Failed, bail out.
567 */
568 LogRel(("SUP: Loading failed for %s (%s) rc=%Rrc\n", pszModule, pszFilename, rc));
569 if ( pLoadReq->u.Out.uErrorMagic == SUPLDRLOAD_ERROR_MAGIC
570 && pLoadReq->u.Out.szError[0] != '\0')
571 {
572 LogRel(("SUP: %s\n", pLoadReq->u.Out.szError));
573 RTErrInfoSet(pErrInfo, rc, pLoadReq->u.Out.szError);
574 }
575 else
576 RTErrInfoSet(pErrInfo, rc, "SUP_IOCTL_LDR_LOAD failed");
577 }
578 else
579 {
580 LogRel(("SUP: RTLdrEnumSymbols failed for %s (%s) rc=%Rrc\n", pszModule, pszFilename, rc));
581 RTErrInfoSetF(pErrInfo, rc, "RTLdrEnumSymbols #2 failed");
582 }
583 }
584 else
585 {
586 LogRel(("SUP: Failed to get entry point '%s' for %s (%s) rc=%Rrc\n", pszEp, pszModule, pszFilename, rc));
587 RTErrInfoSetF(pErrInfo, rc, "Failed to resolve entry point '%s'", pszEp);
588 }
589 }
590 else
591 {
592 LogRel(("SUP: RTLdrGetBits failed for %s (%s). rc=%Rrc\n", pszModule, pszFilename, rc));
593 if (!RTErrInfoIsSet(pErrInfo))
594 RTErrInfoSetF(pErrInfo, rc, "RTLdrGetBits failed");
595 }
596 RTMemTmpFree(pLoadReq);
597 }
598 else
599 {
600 AssertMsgFailed(("failed to allocated %u bytes for SUPLDRLOAD_IN structure!\n", SUP_IOCTL_LDR_LOAD_SIZE(cbImageWithTabs)));
601 rc = VERR_NO_TMP_MEMORY;
602 RTErrInfoSetF(pErrInfo, rc, "Failed to allocate %u bytes for the load request", SUP_IOCTL_LDR_LOAD_SIZE(cbImageWithTabs));
603 }
604 }
605 /*
606 * Already loaded?
607 */
608 else if (RT_SUCCESS(rc))
609 {
610 if (fIsVMMR0)
611 g_pvVMMR0 = OpenReq.u.Out.pvImageBase;
612 LogRel(("SUP: Opened %s (%s) at %#RKv%s.\n", pszModule, pszFilename, OpenReq.u.Out.pvImageBase,
613 OpenReq.u.Out.fNativeLoader ? " loaded by the native ring-0 loader" : ""));
614#ifdef RT_OS_WINDOWS
615 LogRel(("SUP: windbg> .reload /f %s=%#RKv\n", pszFilename, OpenReq.u.Out.pvImageBase));
616#endif
617 }
618 /*
619 * No, failed.
620 */
621 else
622 RTErrInfoSet(pErrInfo, rc, "SUP_IOCTL_LDR_OPEN failed");
623 }
624 else
625 RTErrInfoSetF(pErrInfo, rc, "RTLdrEnumSymbols #1 failed");
626 RTLdrClose(hLdrMod);
627 return rc;
628}
629
630
631SUPR3DECL(int) SUPR3FreeModule(void *pvImageBase)
632{
633 /* fake */
634 if (RT_UNLIKELY(g_uSupFakeMode))
635 {
636 g_pvVMMR0 = NIL_RTR0PTR;
637 return VINF_SUCCESS;
638 }
639
640 /*
641 * Free the requested module.
642 */
643 SUPLDRFREE Req;
644 Req.Hdr.u32Cookie = g_u32Cookie;
645 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
646 Req.Hdr.cbIn = SUP_IOCTL_LDR_FREE_SIZE_IN;
647 Req.Hdr.cbOut = SUP_IOCTL_LDR_FREE_SIZE_OUT;
648 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
649 Req.Hdr.rc = VERR_INTERNAL_ERROR;
650 Req.u.In.pvImageBase = (RTR0PTR)pvImageBase;
651 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LDR_FREE, &Req, SUP_IOCTL_LDR_FREE_SIZE);
652 if (RT_SUCCESS(rc))
653 rc = Req.Hdr.rc;
654 if ( RT_SUCCESS(rc)
655 && (RTR0PTR)pvImageBase == g_pvVMMR0)
656 g_pvVMMR0 = NIL_RTR0PTR;
657 return rc;
658}
659
660
661SUPR3DECL(int) SUPR3GetSymbolR0(void *pvImageBase, const char *pszSymbol, void **ppvValue)
662{
663 *ppvValue = NULL;
664
665 /* fake */
666 if (RT_UNLIKELY(g_uSupFakeMode))
667 {
668 *ppvValue = (void *)(uintptr_t)0xdeadf00d;
669 return VINF_SUCCESS;
670 }
671
672 /*
673 * Do ioctl.
674 */
675 SUPLDRGETSYMBOL Req;
676 Req.Hdr.u32Cookie = g_u32Cookie;
677 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
678 Req.Hdr.cbIn = SUP_IOCTL_LDR_GET_SYMBOL_SIZE_IN;
679 Req.Hdr.cbOut = SUP_IOCTL_LDR_GET_SYMBOL_SIZE_OUT;
680 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
681 Req.Hdr.rc = VERR_INTERNAL_ERROR;
682 Req.u.In.pvImageBase = (RTR0PTR)pvImageBase;
683 size_t cchSymbol = strlen(pszSymbol);
684 if (cchSymbol >= sizeof(Req.u.In.szSymbol))
685 return VERR_SYMBOL_NOT_FOUND;
686 memcpy(Req.u.In.szSymbol, pszSymbol, cchSymbol + 1);
687 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_LDR_GET_SYMBOL, &Req, SUP_IOCTL_LDR_GET_SYMBOL_SIZE);
688 if (RT_SUCCESS(rc))
689 rc = Req.Hdr.rc;
690 if (RT_SUCCESS(rc))
691 *ppvValue = (void *)Req.u.Out.pvSymbol;
692 return rc;
693}
694
695
696SUPR3DECL(int) SUPR3LoadVMM(const char *pszFilename)
697{
698 void *pvImageBase;
699 return SUPR3LoadModule(pszFilename, "VMMR0.r0", &pvImageBase, NULL /*pErrInfo*/);
700}
701
702
703SUPR3DECL(int) SUPR3UnloadVMM(void)
704{
705 return SUPR3FreeModule((void*)g_pvVMMR0);
706}
707
708
709/**
710 * Worker for SUPR3HardenedLdrLoad and SUPR3HardenedLdrLoadAppPriv.
711 *
712 * @returns iprt status code.
713 * @param pszFilename The full file name.
714 * @param phLdrMod Where to store the handle to the loaded module.
715 * @param fFlags See RTLDFLAGS_.
716 * @param pErrInfo Where to return extended error information.
717 * Optional.
718 *
719 */
720static int supR3HardenedLdrLoadIt(const char *pszFilename, PRTLDRMOD phLdrMod, uint32_t fFlags, PRTERRINFO pErrInfo)
721{
722#ifdef VBOX_WITH_HARDENING
723 /*
724 * Verify the image file.
725 */
726 int rc = SUPR3HardenedVerifyInit();
727 if (RT_FAILURE(rc))
728 rc = supR3HardenedVerifyFixedFile(pszFilename, false /* fFatal */);
729 if (RT_FAILURE(rc))
730 {
731 LogRel(("supR3HardenedLdrLoadIt: Verification of \"%s\" failed, rc=%Rrc\n", pszFilename, rc));
732 return RTErrInfoSet(pErrInfo, rc, "supR3HardenedVerifyFixedFile failed");
733 }
734#endif
735
736 /*
737 * Try load it.
738 */
739 return RTLdrLoadEx(pszFilename, phLdrMod, fFlags, pErrInfo);
740}
741
742
743SUPR3DECL(int) SUPR3HardenedLdrLoad(const char *pszFilename, PRTLDRMOD phLdrMod, uint32_t fFlags, PRTERRINFO pErrInfo)
744{
745 /*
746 * Validate input.
747 */
748 RTErrInfoClear(pErrInfo);
749 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
750 AssertPtrReturn(phLdrMod, VERR_INVALID_POINTER);
751 *phLdrMod = NIL_RTLDRMOD;
752 AssertReturn(RTPathHavePath(pszFilename), VERR_INVALID_PARAMETER);
753
754 /*
755 * Add the default extension if it's missing.
756 */
757 if (!RTPathHasSuffix(pszFilename))
758 {
759 const char *pszSuff = RTLdrGetSuff();
760 size_t cchSuff = strlen(pszSuff);
761 size_t cchFilename = strlen(pszFilename);
762 char *psz = (char *)alloca(cchFilename + cchSuff + 1);
763 AssertReturn(psz, VERR_NO_TMP_MEMORY);
764 memcpy(psz, pszFilename, cchFilename);
765 memcpy(psz + cchFilename, pszSuff, cchSuff + 1);
766 pszFilename = psz;
767 }
768
769 /*
770 * Pass it on to the common library loader.
771 */
772 return supR3HardenedLdrLoadIt(pszFilename, phLdrMod, fFlags, pErrInfo);
773}
774
775
776SUPR3DECL(int) SUPR3HardenedLdrLoadAppPriv(const char *pszFilename, PRTLDRMOD phLdrMod, uint32_t fFlags, PRTERRINFO pErrInfo)
777{
778 LogFlow(("SUPR3HardenedLdrLoadAppPriv: pszFilename=%p:{%s} phLdrMod=%p fFlags=%08x pErrInfo=%p\n", pszFilename, pszFilename, phLdrMod, fFlags, pErrInfo));
779
780 /*
781 * Validate input.
782 */
783 RTErrInfoClear(pErrInfo);
784 AssertPtrReturn(phLdrMod, VERR_INVALID_PARAMETER);
785 *phLdrMod = NIL_RTLDRMOD;
786 AssertPtrReturn(pszFilename, VERR_INVALID_PARAMETER);
787 AssertMsgReturn(!RTPathHavePath(pszFilename), ("%s\n", pszFilename), VERR_INVALID_PARAMETER);
788
789 /*
790 * Check the filename.
791 */
792 size_t cchFilename = strlen(pszFilename);
793 AssertMsgReturn(cchFilename < (RTPATH_MAX / 4) * 3, ("%zu\n", cchFilename), VERR_INVALID_PARAMETER);
794
795 const char *pszExt = "";
796 size_t cchExt = 0;
797 if (!RTPathHasSuffix(pszFilename))
798 {
799 pszExt = RTLdrGetSuff();
800 cchExt = strlen(pszExt);
801 }
802
803 /*
804 * Construct the private arch path and check if the file exists.
805 */
806 char szPath[RTPATH_MAX];
807 int rc = RTPathAppPrivateArch(szPath, sizeof(szPath) - 1 - cchExt - cchFilename);
808 AssertRCReturn(rc, rc);
809
810 char *psz = strchr(szPath, '\0');
811 *psz++ = RTPATH_SLASH;
812 memcpy(psz, pszFilename, cchFilename);
813 psz += cchFilename;
814 memcpy(psz, pszExt, cchExt + 1);
815
816 if (!RTPathExists(szPath))
817 {
818 LogRel(("SUPR3HardenedLdrLoadAppPriv: \"%s\" not found\n", szPath));
819 return VERR_FILE_NOT_FOUND;
820 }
821
822 /*
823 * Pass it on to SUPR3HardenedLdrLoad.
824 */
825 rc = SUPR3HardenedLdrLoad(szPath, phLdrMod, fFlags, pErrInfo);
826
827 LogFlow(("SUPR3HardenedLdrLoadAppPriv: returns %Rrc\n", rc));
828 return rc;
829}
830
831
832SUPR3DECL(int) SUPR3HardenedLdrLoadPlugIn(const char *pszFilename, PRTLDRMOD phLdrMod, PRTERRINFO pErrInfo)
833{
834 /*
835 * Validate input.
836 */
837 RTErrInfoClear(pErrInfo);
838 AssertPtrReturn(phLdrMod, VERR_INVALID_PARAMETER);
839 *phLdrMod = NIL_RTLDRMOD;
840 AssertPtrReturn(pszFilename, VERR_INVALID_PARAMETER);
841 AssertReturn(RTPathStartsWithRoot(pszFilename), VERR_INVALID_PARAMETER);
842
843#ifdef VBOX_WITH_HARDENING
844 /*
845 * Verify the image file.
846 */
847 int rc = supR3HardenedVerifyFile(pszFilename, RTHCUINTPTR_MAX, true /*fMaybe3rdParty*/, pErrInfo);
848 if (RT_FAILURE(rc))
849 {
850 if (!RTErrInfoIsSet(pErrInfo))
851 LogRel(("supR3HardenedVerifyFile: Verification of \"%s\" failed, rc=%Rrc\n", pszFilename, rc));
852 return rc;
853 }
854#endif
855
856 /*
857 * Try load it.
858 */
859 return RTLdrLoadEx(pszFilename, phLdrMod, RTLDRLOAD_FLAGS_LOCAL, pErrInfo);
860}
861
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use