VirtualBox

Changeset 52213 in vbox for trunk/src/VBox/HostDrivers


Ignore:
Timestamp:
Jul 28, 2014 5:52:58 PM (10 years ago)
Author:
vboxsync
Message:

SUP,IPRT: Implemented forwarder support in RTLdr and cleaned up some the ordinal mess. Resolved imports when doing the process verification/purification runs other than SUPHARDNTVPKIND_CHILD_PURIFICATION. This is necessary since 32-bit windows combine .text with .rdata, and we don't want to overwrite the import table after it has been snapped. Include read-only sections in the verfication runs.

Location:
trunk/src/VBox/HostDrivers/Support
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/HostDrivers/Support/SUPLibLdr.cpp

    r51770 r52213  
    429429                    if (fIsVMMR0)
    430430                    {
    431                         rc = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.abImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase, "VMMR0EntryInt", &VMMR0EntryInt);
     431                        rc = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.abImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase, UINT32_MAX, "VMMR0EntryInt", &VMMR0EntryInt);
    432432                        if (RT_SUCCESS(rc))
    433                             rc = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.abImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase, "VMMR0EntryFast", &VMMR0EntryFast);
     433                            rc = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.abImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase, UINT32_MAX, "VMMR0EntryFast", &VMMR0EntryFast);
    434434                        if (RT_SUCCESS(rc))
    435                             rc = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.abImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase, "VMMR0EntryEx", &VMMR0EntryEx);
     435                            rc = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.abImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase, UINT32_MAX, "VMMR0EntryEx", &VMMR0EntryEx);
    436436                    }
    437437                    else if (pszSrvReqHandler)
    438                         rc = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.abImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase, pszSrvReqHandler, &SrvReqHandler);
     438                        rc = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.abImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase, UINT32_MAX, pszSrvReqHandler, &SrvReqHandler);
    439439                    if (RT_SUCCESS(rc))
    440440                    {
    441                         int rc2 = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.abImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase, "ModuleInit", &ModuleInit);
     441                        int rc2 = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.abImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase, UINT32_MAX, "ModuleInit", &ModuleInit);
    442442                        if (RT_FAILURE(rc2))
    443443                            ModuleInit = 0;
    444444
    445                         rc2 = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.abImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase, "ModuleTerm", &ModuleTerm);
     445                        rc2 = RTLdrGetSymbolEx(hLdrMod, &pLoadReq->u.In.abImage[0], (uintptr_t)OpenReq.u.Out.pvImageBase, UINT32_MAX, "ModuleTerm", &ModuleTerm);
    446446                        if (RT_FAILURE(rc2))
    447447                            ModuleTerm = 0;
  • trunk/src/VBox/HostDrivers/Support/win/SUPHardenedVerifyImage-win.cpp

    r52207 r52213  
    16651665         */
    16661666# ifdef DEBUG_bird
    1667         __debugbreak();
     1667        if (hrc != CERT_E_CHAINING /* Un-updated vistas, XPs, ++ */)
     1668            __debugbreak();
    16681669# endif
    16691670        const char *pszErrConst = NULL;
     
    16851686            case TRUST_E_FAIL:                     pszErrConst = "TRUST_E_FAIL";                  break;
    16861687            case TRUST_E_EXPLICIT_DISTRUST:        pszErrConst = "TRUST_E_EXPLICIT_DISTRUST";     break;
     1688            case CERT_E_CHAINING:                  pszErrConst = "CERT_E_CHAINING";               break;
    16871689        }
    16881690        if (pszErrConst)
  • trunk/src/VBox/HostDrivers/Support/win/SUPHardenedVerifyProcess-win.cpp

    r52207 r52213  
    3838#include <VBox/sup.h>
    3939#include <VBox/err.h>
     40#include <iprt/alloca.h>
    4041#include <iprt/ctype.h>
     42#include <iprt/param.h>
     43#include <iprt/string.h>
    4144#include <iprt/zero.h>
    42 #include <iprt/param.h>
    4345
    4446#ifdef IN_RING0
     
    134136    /** Number of images in aImages. */
    135137    uint32_t                cImages;
     138    /** The index of the last image we looked up. */
     139    uint32_t                iImageHint;
    136140    /** The process handle. */
    137141    HANDLE                  hProcess;
     
    512516
    513517
     518static DECLINLINE(bool) supHardNtVpIsModuleNameMatch(PSUPHNTVPIMAGE pImage, const char *pszModule)
     519{
     520    if (pImage->fDll)
     521    {
     522        const char *pszImageNm = pImage->pszName;
     523        for (;;)
     524        {
     525            char chLeft  = *pszImageNm++;
     526            char chRight = *pszModule++;
     527            if (chLeft != chRight)
     528            {
     529                Assert(chLeft == RT_C_TO_LOWER(chLeft));
     530                if (chLeft != RT_C_TO_LOWER(chRight))
     531                {
     532                    if (   chRight == '\0'
     533                        && chLeft  == '.'
     534                        && pszImageNm[0] == 'd'
     535                        && pszImageNm[1] == 'l'
     536                        && pszImageNm[2] == 'l'
     537                        && pszImageNm[3] == '\0')
     538                        return true;
     539                    break;
     540                }
     541            }
     542
     543            if (chLeft == '\0')
     544                return true;
     545        }
     546    }
     547
     548    return false;
     549}
     550
     551
     552/**
     553 * Worker for supHardNtVpGetImport that looks up a module in the module table.
     554 *
     555 * @returns Pointer to the module if found, NULL if not found.
     556 * @param   pThis               The process validator instance.
     557 * @param   pszModule           The name of the module we're looking for.
     558 */
     559static PSUPHNTVPIMAGE supHardNtVpFindModule(PSUPHNTVPSTATE pThis, const char *pszModule)
     560{
     561    /*
     562     * Check out the hint first.
     563     */
     564    if (   pThis->iImageHint < pThis->cImages
     565        && supHardNtVpIsModuleNameMatch(&pThis->aImages[pThis->iImageHint], pszModule))
     566        return &pThis->aImages[pThis->iImageHint];
     567
     568    /*
     569     * Linear array search next.
     570     */
     571    uint32_t i = pThis->cImages;
     572    while (i-- > 0)
     573        if (supHardNtVpIsModuleNameMatch(&pThis->aImages[i], pszModule))
     574        {
     575            pThis->iImageHint = i;
     576            return &pThis->aImages[i];
     577        }
     578
     579    /* No cigar. */
     580    return NULL;
     581}
     582
     583
     584/**
     585 * @callback_method_impl{FNRTLDRIMPORT}
     586 */
     587static DECLCALLBACK(int) supHardNtVpGetImport(RTLDRMOD hLdrMod, const char *pszModule, const char *pszSymbol, unsigned uSymbol,
     588                                              PRTLDRADDR pValue, void *pvUser)
     589{
     590    /*SUP_DPRINTF(("supHardNtVpGetImport: %s / %#x / %s.\n", pszModule, uSymbol, pszSymbol));*/
     591    PSUPHNTVPSTATE pThis = (PSUPHNTVPSTATE)pvUser;
     592
     593    int rc = VERR_MODULE_NOT_FOUND;
     594    PSUPHNTVPIMAGE pImage = supHardNtVpFindModule(pThis, pszModule);
     595    if (pImage)
     596    {
     597        rc = RTLdrGetSymbolEx(pImage->hLdrMod, NULL, pImage->uImageBase, uSymbol, pszSymbol, pValue);
     598        if (RT_SUCCESS(rc))
     599            return rc;
     600    }
     601    /*
     602     * API set hacks.
     603     */
     604    else if (!RTStrNICmp(pszModule, RT_STR_TUPLE("api-ms-win-")))
     605    {
     606        static const char * const s_apszDlls[] = { "ntdll.dll", "kernelbase.dll", "kernel32.dll" };
     607        for (uint32_t i = 0; i < RT_ELEMENTS(s_apszDlls); i++)
     608        {
     609            pImage = supHardNtVpFindModule(pThis, s_apszDlls[i]);
     610            if (pImage)
     611            {
     612                rc = RTLdrGetSymbolEx(pImage->hLdrMod, NULL, pImage->uImageBase, uSymbol, pszSymbol, pValue);
     613                if (RT_SUCCESS(rc))
     614                    return rc;
     615                if (rc != VERR_SYMBOL_NOT_FOUND)
     616                    break;
     617            }
     618        }
     619    }
     620
     621    /*
     622     * Deal with forwarders.
     623     * ASSUMES no forwarders thru any api-ms-win-core-*.dll.
     624     * ASSUMES forwarders are resolved after one redirection.
     625     */
     626    if (rc == VERR_LDR_FORWARDER)
     627    {
     628        size_t           cbInfo = RT_MIN((uint32_t)*pValue, sizeof(RTLDRIMPORTINFO) + 32);
     629        PRTLDRIMPORTINFO pInfo  = (PRTLDRIMPORTINFO)alloca(cbInfo);
     630        rc = RTLdrQueryForwarderInfo(pImage->hLdrMod, NULL, uSymbol, pszSymbol, pInfo, cbInfo);
     631        if (RT_SUCCESS(rc))
     632        {
     633            rc = VERR_MODULE_NOT_FOUND;
     634            pImage = supHardNtVpFindModule(pThis, pInfo->szModule);
     635            if (pImage)
     636            {
     637                rc = RTLdrGetSymbolEx(pImage->hLdrMod, NULL, pImage->uImageBase, pInfo->iOrdinal, pInfo->pszSymbol, pValue);
     638                if (RT_SUCCESS(rc))
     639                    return rc;
     640
     641                SUP_DPRINTF(("supHardNtVpGetImport: Failed to find symbol '%s' in '%s' (forwarded from %s / %s): %Rrc\n",
     642                             pInfo->pszSymbol, pInfo->szModule, pszModule, pszSymbol, rc));
     643                if (rc == VERR_LDR_FORWARDER)
     644                    rc = VERR_LDR_FORWARDER_CHAIN_TOO_LONG;
     645            }
     646            else
     647                SUP_DPRINTF(("supHardNtVpGetImport: Failed to find forwarder module '%s' (%#x / %s; originally %s / %#x / %s): %Rrc\n",
     648                             pInfo->szModule, pInfo->iOrdinal, pInfo->pszSymbol, pszModule, uSymbol, pszSymbol, rc));
     649        }
     650        else
     651            SUP_DPRINTF(("supHardNtVpGetImport: RTLdrQueryForwarderInfo failed on symbol %#x/'%s' in '%s': %Rrc\n",
     652                         uSymbol, pszSymbol, pszModule, rc));
     653    }
     654    else
     655        SUP_DPRINTF(("supHardNtVpGetImport: Failed to find symbol %#x / '%s' in '%s': %Rrc\n",
     656                     uSymbol, pszSymbol, pszModule, rc));
     657    return rc;
     658}
     659
     660
    514661/**
    515662 * Compares process memory with the disk content.
     
    648795        return supHardNtVpSetInfo2(pThis, VERR_SUP_VP_NO_MEMORY,
    649796                                   "%s: Error allocating %#x bytes for fixed up image bits.", pImage->pszName, cbImage);
    650     rc = RTLdrGetBits(pImage->hLdrMod, pImage->pbBits, pImage->uImageBase, NULL /*pfnGetImport*/, pThis);
    651     /**@todo resolve import when not in SUPHARDNTVPKIND_CHILD_PURIFICATION mode. */
     797    if (pThis->enmKind == SUPHARDNTVPKIND_CHILD_PURIFICATION)
     798        rc = RTLdrGetBits(pImage->hLdrMod, pImage->pbBits, pImage->uImageBase, NULL /*pfnGetImport*/, pThis);
     799    else
     800        rc = RTLdrGetBits(pImage->hLdrMod, pImage->pbBits, pImage->uImageBase, supHardNtVpGetImport, pThis);
    652801    if (RT_FAILURE(rc))
    653802        return supHardNtVpSetInfo2(pThis, rc, "%s: RTLdrGetBits failed: %Rrc", pImage->pszName, rc);
     
    666815     */
    667816    uint32_t         cSkipAreas = 0;
    668     SUPHNTVPSKIPAREA aSkipAreas[2];
     817    SUPHNTVPSKIPAREA aSkipAreas[3];
    669818    if (pImage->fNtCreateSectionPatch)
    670819    {
     
    673822        {
    674823            /* Ignore our NtCreateSection hack. */
    675             rc = RTLdrGetSymbolEx(pImage->hLdrMod, pImage->pbBits, 0, "NtCreateSection", &uValue);
     824            rc = RTLdrGetSymbolEx(pImage->hLdrMod, pImage->pbBits, 0, UINT32_MAX, "NtCreateSection", &uValue);
    676825            if (RT_FAILURE(rc))
    677826                return supHardNtVpSetInfo2(pThis, rc, "%s: Failed to find 'NtCreateSection': %Rrc", pImage->pszName, rc);
     
    681830
    682831        /* LdrSystemDllInitBlock is filled in by the kernel. It mainly contains addresses of 32-bit ntdll method for wow64. */
    683         rc = RTLdrGetSymbolEx(pImage->hLdrMod, pImage->pbBits, 0, "LdrSystemDllInitBlock", &uValue);
     832        rc = RTLdrGetSymbolEx(pImage->hLdrMod, pImage->pbBits, 0, UINT32_MAX, "LdrSystemDllInitBlock", &uValue);
    684833        if (RT_SUCCESS(rc))
    685834        {
     
    758907            /* The section bits, only child purification verifies all bits . */
    759908            if (   pThis->enmKind == SUPHARDNTVPKIND_CHILD_PURIFICATION
    760                 || (pThis->aSecHdrs[i].Characteristics & (IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_CNT_CODE)) )
     909                || (pThis->aSecHdrs[i].Characteristics & (IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_CNT_CODE))
     910                || (pThis->aSecHdrs[i].Characteristics & (IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE)) == IMAGE_SCN_MEM_READ)
    761911            {
    762912                rc = VINF_SUCCESS;
     
    10071157    {
    10081158        /*
    1009          * Not a known DLL, executable?
     1159         * Not a known DLL, is it a known executable?
    10101160         */
    10111161        for (uint32_t i = 0; i < RT_ELEMENTS(g_apszSupNtVpAllowedVmExes); i++)
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette