VirtualBox

Changeset 52169 in vbox


Ignore:
Timestamp:
Jul 24, 2014 2:12:57 PM (11 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
95213
Message:

SUP: Added logging capabilities to the hardened stub.

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

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/HostDrivers/Support/Makefile.kmk

    r52030 r52169  
    363363endif
    364364
     365SUPR3HardenedMain.cpp_DEFS = VBOX_SVN_REV=$(VBOX_SVN_REV)
     366
    365367
    366368#
  • trunk/src/VBox/HostDrivers/Support/SUPLibInternal.h

    r52160 r52169  
    8989# define supR3HardenedErrorV               supR3HardenedStaticErrorV
    9090# define supR3HardenedError                supR3HardenedStaticError
     91# define supR3HardenedOpenLog              supR3HardenedStaticOpenLog
     92# define supR3HardenedLogV                 supR3HardenedStaticLogV
     93# define supR3HardenedLog                  supR3HardenedStaticLog
    9194# define supR3HardenedVerifyAll            supR3HardenedStaticVerifyAll
    9295# define supR3HardenedVerifyFixedDir       supR3HardenedStaticVerifyFixedDir
     
    131134
    132135/** Debug output macro. */
    133 #ifdef DEBUG_bird
    134 # ifdef IN_SUP_HARDENED_R3
    135 #  define SUP_DPRINTF(a)    suplibHardenedPrintF a
     136#ifdef IN_SUP_HARDENED_R3
     137# if defined(DEBUG_bird) && defined(RT_OS_WINDOWS)
     138#  define SUP_DPRINTF(a)    do { supR3HardenedStaticLog a; suplibHardenedPrintF a; } while (0)
    136139# else
     140#  define SUP_DPRINTF(a)    do { supR3HardenedStaticLog a; } while (0)
     141# endif
     142#else
     143# if defined(DEBUG_bird) && defined(RT_OS_WINDOWS)
    137144#  define SUP_DPRINTF(a)    RTLogPrintf a
     145# else
     146#  define SUP_DPRINTF(a)    do { } while (0)
    138147# endif
    139 #else
    140 # define SUP_DPRINTF(a)     do { } while (0)
    141148#endif
    142149
     
    401408DECLHIDDEN(int)     supR3HardenedError(int rc, bool fFatal, const char *pszFormat, ...);
    402409
     410/**
     411 * Open any startup log file specified in the argument.
     412 */
     413DECLHIDDEN(void)    supR3HardenedOpenLog(int *pcArgs, char **papszArgs);
     414
     415/**
     416 * Write to the startup log file.
     417 */
     418DECLHIDDEN(void)    supR3HardenedLogV(const char *pszFormat, va_list va);
     419
     420/**
     421 * Write to the startup log file.
     422 */
     423DECLHIDDEN(void)    supR3HardenedLog(const char *pszFormat, ...);
     424
     425
    403426DECLHIDDEN(int)     supR3HardenedVerifyAll(bool fFatal, const char *pszProgName);
    404427DECLHIDDEN(int)     supR3HardenedVerifyFixedDir(SUPINSTDIR enmDir, bool fFatal);
  • trunk/src/VBox/HostDrivers/Support/SUPR3HardenedMain.cpp

    r52163 r52169  
    7878#include <VBox/sup.h>
    7979#include <VBox/err.h>
     80#ifdef RT_OS_WINDOWS
     81# include <VBox/version.h>
     82#endif
    8083#include <iprt/ctype.h>
    8184#include <iprt/string.h>
     
    141144static uint32_t         g_uCaps;
    142145# endif
     146#endif
     147
     148/** The startup log file. */
     149#ifdef RT_OS_WINDOWS
     150static HANDLE           g_hStartupLog = NULL;
     151#else
     152static int              g_hStartupLog = -1;
    143153#endif
    144154
     
    235245        IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
    236246        NtWriteFile(hStdOut, NULL /*Event*/, NULL /*ApcRoutine*/, NULL /*ApcContext*/,
    237                     &Ios, (PVOID)pch, cch, NULL /*ByteOffset*/, NULL /*Key*/);
     247                    &Ios, (PVOID)pch, (ULONG)cch, NULL /*ByteOffset*/, NULL /*Key*/);
    238248    }
    239249#else
     
    920930
    921931
     932#ifdef RT_OS_WINDOWS
     933extern "C" uint32_t g_uNtVerCombined;
     934#endif
     935
     936DECLHIDDEN(void) supR3HardenedOpenLog(int *pcArgs, char **papszArgs)
     937{
     938    static const char s_szLogOption[] = "--sup-startup-log=";
     939
     940    /*
     941     * Scan the argument vector.
     942     */
     943    int cArgs = *pcArgs;
     944    for (int iArg = 1; iArg < cArgs; iArg++)
     945        if (strncmp(papszArgs[iArg], s_szLogOption, sizeof(s_szLogOption) - 1) == 0)
     946        {
     947            const char *pszLogFile = &papszArgs[iArg][sizeof(s_szLogOption) - 1];
     948
     949            /*
     950             * Drop the argument from the vector (has trailing NULL entry).
     951             */
     952            memmove(&papszArgs[iArg], &papszArgs[iArg + 1], (cArgs - iArg) * sizeof(papszArgs[0]));
     953            *pcArgs -= 1;
     954            cArgs   -= 1;
     955
     956            /*
     957             * Open the log file, unless we've already opened one.
     958             * First argument takes precedence
     959             */
     960#ifdef RT_OS_WINDOWS
     961            if (g_hStartupLog == NULL)
     962            {
     963                PRTUTF16 pwszPath;
     964                int rc = RTStrToUtf16(pszLogFile, &pwszPath);
     965                if (RT_SUCCESS(rc))
     966                {
     967                    g_hStartupLog = CreateFileW(pwszPath,
     968                                                GENERIC_WRITE,
     969                                                FILE_SHARE_READ | FILE_SHARE_WRITE,
     970                                                NULL,
     971                                                OPEN_ALWAYS,
     972                                                FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH,
     973                                                NULL);
     974                    RTUtf16Free(pwszPath);
     975                }
     976                SUP_DPRINTF(("Log file opened: " VBOX_VERSION_STRING "r%u g_hStartupLog=%p g_uNtVerCombined=%#x\n",
     977                             VBOX_SVN_REV, g_hStartupLog, g_uNtVerCombined));
     978            }
     979#else
     980            //g_hStartupLog = open()
     981#endif
     982        }
     983}
     984
     985
     986DECLHIDDEN(void) supR3HardenedLogV(const char *pszFormat, va_list va)
     987{
     988#ifdef RT_OS_WINDOWS
     989    if (g_hStartupLog)
     990    {
     991        char szBuf[5120];
     992        PCLIENT_ID pSelfId = &((PTEB)NtCurrentTeb())->ClientId;
     993        size_t cchPrefix = RTStrPrintf(szBuf, sizeof(szBuf), "%x.%x: ", pSelfId->UniqueProcess, pSelfId->UniqueThread);
     994        size_t cch = RTStrPrintfV(&szBuf[cchPrefix], sizeof(szBuf) - cchPrefix, pszFormat, va) + cchPrefix;
     995
     996        if ((size_t)cch >= sizeof(szBuf))
     997            cch = sizeof(szBuf) - 1;
     998
     999        if (!cch || szBuf[cch - 1] != '\n')
     1000            szBuf[cch++] = '\n';
     1001
     1002        IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
     1003        LARGE_INTEGER   Offset;
     1004        Offset.QuadPart = -1; /* Write to end of file. */
     1005        NtWriteFile(g_hStartupLog, NULL /*Event*/, NULL /*ApcRoutine*/, NULL /*ApcContext*/,
     1006                    &Ios, szBuf, (ULONG)cch, &Offset, NULL /*Key*/);
     1007    }
     1008#else
     1009    /* later */
     1010#endif
     1011}
     1012
     1013
     1014DECLHIDDEN(void) supR3HardenedLog(const char *pszFormat,  ...)
     1015{
     1016    va_list va;
     1017    va_start(va, pszFormat);
     1018    supR3HardenedLogV(pszFormat, va);
     1019    va_end(va);
     1020}
     1021
     1022
    9221023/**
    9231024 * Prints the message prefix.
     
    9341035{
    9351036    /*
    936      * To the console first, like supR3HardenedFatalV.
     1037     * First to the log.
     1038     */
     1039    supR3HardenedLog("Error %d in %s! (enmWhat=%d)\n", rc, pszWhere, enmWhat);
     1040    va_list vaCopy;
     1041    va_copy(vaCopy, va);
     1042    supR3HardenedLogV(pszMsgFmt, vaCopy);
     1043    va_end(vaCopy);
     1044
     1045    /*
     1046     * Then to the console.
    9371047     */
    9381048    suplibHardenedPrintPrefix();
     
    9401050
    9411051    suplibHardenedPrintPrefix();
    942     va_list vaCopy;
    9431052    va_copy(vaCopy, va);
    9441053    suplibHardenedPrintFV(pszMsgFmt, vaCopy);
     
    10181127DECLHIDDEN(void) supR3HardenedFatalV(const char *pszFormat, va_list va)
    10191128{
     1129    supR3HardenedLog("Fatal error:\n");
     1130    va_list vaCopy;
     1131    va_copy(vaCopy, va);
     1132    supR3HardenedLogV(pszFormat, vaCopy);
     1133    va_end(vaCopy);
     1134
    10201135    suplibHardenedPrintPrefix();
    10211136    suplibHardenedPrintFV(pszFormat, va);
     
    10371152    if (fFatal)
    10381153        supR3HardenedFatalV(pszFormat, va);
     1154
     1155    supR3HardenedLog("Error (rc=%d):\n", rc);
     1156    va_list vaCopy;
     1157    va_copy(vaCopy, va);
     1158    supR3HardenedLogV(pszFormat, vaCopy);
     1159    va_end(vaCopy);
    10391160
    10401161    suplibHardenedPrintPrefix();
     
    10521173    return rc;
    10531174}
     1175
    10541176
    10551177
     
    15121634DECLHIDDEN(int) SUPR3HardenedMain(const char *pszProgName, uint32_t fFlags, int argc, char **argv, char **envp)
    15131635{
     1636    SUP_DPRINTF(("SUPR3HardenedMain: pszProgName=%s fFlags=%#x\n", pszProgName, fFlags));
     1637
    15141638    /*
    15151639     * Note! At this point there is no IPRT, so we will have to stick
     
    15561680        && supR3HardenedWinIsReSpawnNeeded(1 /*iWhich*/, argc, argv))
    15571681    {
     1682        SUP_DPRINTF(("SUPR3HardenedMain: Respawn #1\n"));
    15581683        supR3HardenedWinInit(SUPSECMAIN_FLAGS_DONT_OPEN_DEV);
    15591684        supR3HardenedVerifyAll(true /* fFatal */, pszProgName);
     
    15911716         */
    15921717        if (supR3HardenedWinIsReSpawnNeeded(2 /* iWhich*/, argc, argv))
     1718        {
     1719            SUP_DPRINTF(("SUPR3HardenedMain: Respawn #2\n"));
    15931720            return supR3HardenedWinReSpawn(2 /* iWhich*/);
     1721        }
     1722        SUP_DPRINTF(("SUPR3HardenedMain: Final process, opening VBoxDrv...\n"));
    15941723#endif /* RT_OS_WINDOWS */
    15951724
     
    16241753     * call RTR3InitEx.
    16251754     */
     1755    SUP_DPRINTF(("SUPR3HardenedMain: Load Runtime...\n"));
    16261756    g_enmSupR3HardenedMainState = SUPR3HARDENEDMAINSTATE_INIT_RUNTIME;
    16271757    supR3HardenedMainInitRuntime(fFlags);
     
    16311761     * and pass control to it.
    16321762     */
     1763    SUP_DPRINTF(("SUPR3HardenedMain: Load TrustedMain...\n"));
    16331764    g_enmSupR3HardenedMainState = SUPR3HARDENEDMAINSTATE_GET_TRUSTED_MAIN;
    16341765    PFNSUPTRUSTEDMAIN pfnTrustedMain = supR3HardenedMainGetTrustedMain(pszProgName);
     1766
     1767    SUP_DPRINTF(("SUPR3HardenedMain: Calling TrustedMain (%p)...\n", pfnTrustedMain));
    16351768    g_enmSupR3HardenedMainState = SUPR3HARDENEDMAINSTATE_CALLED_TRUSTED_MAIN;
    16361769    return pfnTrustedMain(argc, argv, envp);
  • trunk/src/VBox/HostDrivers/Support/win/SUPHardenedVerifyProcess-win.cpp

    r52030 r52169  
    998998static int supHardNtVpScanVirtualMemory(PSUPHNTVPSTATE pThis, HANDLE hProcess)
    999999{
     1000    SUP_DPRINTF(("supHardNtVpScanVirtualMemory:\n"));
     1001
    10001002    uint32_t    cXpExceptions = 0;
    10011003    uintptr_t   cbAdvance = 0;
     
    10371039                                           "NtQueryVirtualMemory/MemorySectionName failed for %p: %#x", uPtrWhere, rcNt);
    10381040            pThis->aImages[iImg].Name.UniStr.Buffer[pThis->aImages[iImg].Name.UniStr.Length / sizeof(WCHAR)] = '\0';
     1041            SUP_DPRINTF((MemInfo.AllocationBase == MemInfo.BaseAddress
     1042                         ? " *%p-%p %#06x/%#06x %#09x  %ls\n"
     1043                         : "  %p-%p %#06x/%#06x %#09x  %ls\n",
     1044                         MemInfo.BaseAddress, (uintptr_t)MemInfo.BaseAddress - MemInfo.RegionSize - 1, MemInfo.Protect,
     1045                         MemInfo.AllocationProtect, MemInfo.Type, pThis->aImages[iImg].Name.UniStr.Buffer));
    10391046
    10401047            /* New or existing image? */
     
    10821089                 /* && MemInfo.BaseAddress == pPeb->ReadOnlySharedMemoryBase */
    10831090                 && g_uNtVerCombined < SUP_MAKE_NT_VER_SIMPLE(6, 0) )
     1091        {
    10841092            cXpExceptions++;
     1093            SUP_DPRINTF(("  %p-%p %#06x/%#06x %#09x  XP CSRSS read-only region\n", MemInfo.BaseAddress,
     1094                         (uintptr_t)MemInfo.BaseAddress - MemInfo.RegionSize - 1, MemInfo.Protect,
     1095                         MemInfo.AllocationProtect, MemInfo.Type));
     1096        }
    10851097        /*
    10861098         * Executable memory?
     
    11061118        }
    11071119#endif
     1120        else
     1121            SUP_DPRINTF((MemInfo.AllocationBase == MemInfo.BaseAddress
     1122                         ? " *%p-%p %#06x/%#06x %#09x\n"
     1123                         : "  %p-%p %#06x/%#06x %#09x\n",
     1124                         MemInfo.BaseAddress, (uintptr_t)MemInfo.BaseAddress - MemInfo.RegionSize - 1,
     1125                         MemInfo.Protect, MemInfo.AllocationProtect, MemInfo.Type));
    11081126
    11091127        /*
  • trunk/src/VBox/HostDrivers/Support/win/SUPR3HardenedMain-win.cpp

    r52163 r52169  
    15431543    PVOID pvLdrInitThunk      = (PVOID)((uintptr_t)LdrInitializeThunk + pThis->uNtDllAddr - pThis->uNtDllParentAddr);
    15441544    PVOID pvNtTerminateThread = (PVOID)((uintptr_t)NtTerminateThread  + pThis->uNtDllAddr - pThis->uNtDllParentAddr);
     1545    SUP_DPRINTF(("supR3HardNtPuChTriggerInitialImageEvents: pvLdrInitThunk=%p pvNtTerminateThread=%p\n",
     1546                 pvLdrInitThunk, pvNtTerminateThread));
    15451547
    15461548    /*
     
    17871789                             "NtQueryVirtualMemory/MemorySectionName failed for %p: %#x", pMemInfo->BaseAddress, rcNt);
    17881790    uBuf.UniStr.Buffer[uBuf.UniStr.Length / sizeof(WCHAR)] = '\0';
     1791    SUP_DPRINTF(("supR3HardNtPuChSanitizeImage: %p '%ls'\n", pMemInfo->BaseAddress, uBuf.UniStr.Buffer));
     1792
    17891793
    17901794    /*
     
    20422046                        {
    20432047                            pThis->uNtDllAddr = (uintptr_t)MemInfo.AllocationBase;
     2048                            SUP_DPRINTF(("supR3HardNtPuChFindNtdll: uNtDllParentAddr=%p uNtDllChildAddr=%p\n",
     2049                                         pThis->uNtDllParentAddr, pThis->uNtDllAddr));
    20442050                            return;
    20452051                        }
     
    20942100    else
    20952101        This.cbPeb = PEB_SIZE_W81;
     2102
     2103    SUP_DPRINTF(("supR3HardenedWinPurifyChild: PebBaseAddress=%p cbPeb=%#x\n", This.BasicInfo.PebBaseAddress, This.cbPeb));
    20962104
    20972105    SIZE_T cbActualMem;
     
    21892197                              GetLastError(), pwszCmdLine);
    21902198
     2199    SUP_DPRINTF(("supR3HardenedWinDoReSpawn(%d): New child %x.%x [kernel32].\n",
     2200                 iWhich, ProcessInfoW32.dwProcessId, ProcessInfoW32.dwThreadId));
    21912201    HANDLE hProcess = ProcessInfoW32.hProcess;
    21922202    HANDLE hThread  = ProcessInfoW32.hThread;
     
    22422252                              rcNt, CmdLine.Buffer);
    22432253
     2254    SUP_DPRINTF(("supR3HardenedWinDoReSpawn(%d): New child %x.%x [ntdll].\n",
     2255                 iWhich, ProcessInfo.ClientId.UniqueProcess, ProcessInfo.ClientId.UniqueThread));
    22442256    RtlDestroyProcessParameters(pProcParams);
    22452257
     
    22472259    HANDLE hThread  = ProcessInfoNt.ThreadHandle;
    22482260#endif
     2261
    22492262
    22502263    /*
     
    22882301    {
    22892302        /* Failure is unacceptable, kill the process. */
    2290         DWORD dwErr = GetLastError();
    22912303        NtTerminateProcess(hProcess, RTEXITCODE_FAILURE);
    2292         supR3HardenedError(dwErr, false /*fFatal*/, "NtDuplicateObject failed on child process handle: %u\n", dwErr);
     2304        supR3HardenedError(rcNt, false /*fFatal*/, "NtDuplicateObject failed on child process handle: %#x\n", rcNt);
    22932305
    22942306        NTSTATUS rcNtExit = NtQueryInformationProcess(hProcess, ProcessBasicInformation, &BasicInfo, sizeof(BasicInfo), NULL);
     
    23142326                     && GetTickCount() - dwStartTick < 60 * 1000);
    23152327            if (fExitOk)
    2316                 supR3HardenedError(dwErr, false /*fFatal*/,
     2328                supR3HardenedError(rcNt, false /*fFatal*/,
    23172329                                   "NtDuplicateObject failed and we failed to kill child: rcNt=%u rcNtWait=%u hProcess=%p\n",
    23182330                                   rcNt, rcNtWait, hProcess);
     
    23872399     * Proxy the termination code of the child, if it exited already.
    23882400     */
    2389     rcNt = NtQueryInformationProcess(hProcWait, ProcessBasicInformation, &BasicInfo, sizeof(BasicInfo), NULL);
    2390     if (   !NT_SUCCESS(rcNt)
     2401    NTSTATUS rcNt2 = NtQueryInformationProcess(hProcWait, ProcessBasicInformation, &BasicInfo, sizeof(BasicInfo), NULL);
     2402    if (   !NT_SUCCESS(rcNt2)
    23912403        || BasicInfo.ExitStatus == STATUS_PENDING)
    23922404        BasicInfo.ExitStatus = RTEXITCODE_FAILURE;
    23932405
    23942406    NtClose(hProcWait);
     2407    SUP_DPRINTF(("supR3HardenedWinDoReSpawn(%d): Quitting: ExitCode=%#x rcNt=%#x\n", BasicInfo.ExitStatus, rcNt));
    23952408    suplibHardenedExit((RTEXITCODE)BasicInfo.ExitStatus);
    23962409}
     
    27492762extern "C" void __stdcall suplibHardenedWindowsMain(void)
    27502763{
    2751     RTEXITCODE  rcExit = RTEXITCODE_FAILURE;
     2764    RTEXITCODE rcExit = RTEXITCODE_FAILURE;
    27522765
    27532766    g_cSuplibHardenedWindowsMainCalls++;
     
    27582771     */
    27592772    supR3HardenedWinInitVersion();
     2773
     2774    /*
     2775     * Convert the arguments to UTF-8 and open the log file if specified.
     2776     * This must be done as early as possible since the code below may fail.
     2777     */
     2778    PUNICODE_STRING pCmdLineStr = &NtCurrentPeb()->ProcessParameters->CommandLine;
     2779    int    cArgs;
     2780    char **papszArgs = suplibCommandLineToArgvWStub(pCmdLineStr->Buffer, pCmdLineStr->Length / sizeof(WCHAR), &cArgs);
     2781
     2782    supR3HardenedOpenLog(&cArgs, papszArgs);
    27602783
    27612784    /*
     
    27902813
    27912814    /*
    2792      * Convert the arguments to UTF-8 and call the C/C++ main function.
    2793      */
    2794     PUNICODE_STRING pCmdLineStr = &NtCurrentPeb()->ProcessParameters->CommandLine;
    2795     int             cArgs;
    2796     char **papszArgs = suplibCommandLineToArgvWStub(pCmdLineStr->Buffer, pCmdLineStr->Length / sizeof(WCHAR), &cArgs);
    2797 
     2815     * Call the C/C++ main function.
     2816     */
     2817    SUP_DPRINTF(("Calling main()\n"));
    27982818    rcExit = (RTEXITCODE)main(cArgs, papszArgs, NULL);
    27992819
     
    28012821     * Exit the process (never return).
    28022822     */
     2823    SUP_DPRINTF(("Terminating the normal way: rcExit=%d\n", rcExit));
    28032824    suplibHardenedExit(rcExit);
    28042825}
Note: See TracChangeset for help on using the changeset viewer.

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