VirtualBox

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


Ignore:
Timestamp:
Aug 19, 2014 2:05:18 PM (10 years ago)
Author:
vboxsync
Message:

SUP: Need per thread recursion counters for WinVerifyTrust or we risk deadlocking. This is new after hooking LdrLoadDll.

Location:
trunk/src/VBox/HostDrivers/Support/win
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/HostDrivers/Support/win/SUPHardenedVerifyImage-win.cpp

    r52406 r52414  
    179179PFNCRYPTCATCATALOGINFOFROMCONTEXT       g_pfnCryptCATCatalogInfoFromContext;
    180180
    181 /** Indicates active WinVerifyTrust thread. */
     181/** Where we store the TLS entry for detecting WinVerifyTrustRecursion. */
     182static uint32_t                         g_iTlsWinVerifyTrustRecursion = UINT32_MAX;
     183/** Fallback WinVerifyTrust recursion protection. */
    182184static uint32_t volatile                g_idActiveThread = UINT32_MAX;
    183185
     
    18701872
    18711873    /*
     1874     * Allocate TLS entry for WinVerifyTrust recursion prevention.
     1875     */
     1876    DWORD iTls = TlsAlloc();
     1877    if (iTls != TLS_OUT_OF_INDEXES)
     1878        g_iTlsWinVerifyTrustRecursion = iTls;
     1879    else
     1880        supR3HardenedError(GetLastError(), false /*fFatal*/, "TlsAlloc failed");
     1881
     1882    /*
    18721883     * Resolve it.
    18731884     */
     
    23432354    /*
    23442355     * Call the windows verify trust API if we've resolved it and aren't in
    2345      * some obvious recursion.  Assume we won't be having too much
    2346      * concurrency, so a single global variable should suffice, right...
     2356     * some obvious recursion.
    23472357     */
    23482358    if (g_pfnWinVerifyTrust != NULL)
    23492359    {
    2350         uint32_t const idCurrentThread = GetCurrentThreadId();
    2351         if (g_idActiveThread != idCurrentThread)
     2360        /* Check for recursion. */
     2361        bool fNoRecursion;
     2362        if (g_iTlsWinVerifyTrustRecursion != UINT32_MAX)
    23522363        {
    2353             ASMAtomicCmpXchgU32(&g_idActiveThread, idCurrentThread, UINT32_MAX);
    2354 
     2364            fNoRecursion = TlsGetValue(g_iTlsWinVerifyTrustRecursion) == 0;
     2365            if (fNoRecursion)
     2366                TlsSetValue(g_iTlsWinVerifyTrustRecursion, (void *)1);
     2367        }
     2368        else
     2369        {
     2370            uint32_t const idCurrentThread = GetCurrentThreadId();
     2371            fNoRecursion = ASMAtomicCmpXchgU32(&g_idActiveThread, idCurrentThread, UINT32_MAX);
     2372        }
     2373        if (fNoRecursion)
     2374        {
     2375            /* We can call WinVerifyTrust. */
    23552376            if (pfWinVerifyTrust)
    23562377                *pfWinVerifyTrust = true;
     
    23852406            }
    23862407
    2387             ASMAtomicCmpXchgU32(&g_idActiveThread, UINT32_MAX, idCurrentThread);
     2408            /* Unwind recursion. */
     2409            if (g_iTlsWinVerifyTrustRecursion != UINT32_MAX)
     2410                TlsSetValue(g_iTlsWinVerifyTrustRecursion, (void *)0);
     2411            else
     2412                ASMAtomicWriteU32(&g_idActiveThread, UINT32_MAX);
    23882413        }
    23892414        else
     
    24052430{
    24062431    return g_pfnWinVerifyTrust != NULL
    2407         && g_idActiveThread != GetCurrentThreadId();
     2432        && (   g_iTlsWinVerifyTrustRecursion != UINT32_MAX
     2433            ?  (uintptr_t)TlsGetValue(g_iTlsWinVerifyTrustRecursion) == 0
     2434            : g_idActiveThread != GetCurrentThreadId() );
    24082435}
    24092436
  • trunk/src/VBox/HostDrivers/Support/win/import-template-kernel32.h

    r52403 r52414  
    2121SUPHARNT_IMPORT_STDCALL(SetLastError, 4)
    2222SUPHARNT_IMPORT_STDCALL(Sleep, 4)
     23SUPHARNT_IMPORT_STDCALL(TlsAlloc, 0)
     24SUPHARNT_IMPORT_STDCALL(TlsGetValue, 4)
     25SUPHARNT_IMPORT_STDCALL(TlsSetValue, 8)
    2326SUPHARNT_IMPORT_STDCALL(VirtualProtectEx, 20)
    2427SUPHARNT_IMPORT_STDCALL(WriteFile, 20)
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