VirtualBox

Ignore:
Timestamp:
Aug 13, 2019 2:02:58 AM (5 years ago)
Author:
vboxsync
Message:

SUPHardNt: Initial thread context validation adjustments.

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

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/HostDrivers/Support/win/SUPHardenedVerify-win.h

    r80216 r80242  
    169169    /** Set if verified. */
    170170    bool                fVerified;
    171     /** Whether we've got valid cacheable image bit.s */
     171    /** Whether we've got valid cacheable image bits. */
    172172    bool                fValidBits;
    173173    /** The image base address. */
  • trunk/src/VBox/HostDrivers/Support/win/SUPR3HardenedMain-win.cpp

    r80218 r80242  
    41554155# error "Unsupported arch."
    41564156#endif
    4157         size_t    const cbNtDll    = RTLdrSize(pLdrEntry->hLdrMod);
     4157        /* Entrypoint for the executable: */
    41584158        uintptr_t const uChildMain = uChildExeAddr + (  (uintptr_t)&suplibHardenedWindowsMain
    41594159                                                      - (uintptr_t)NtCurrentPeb()->ImageBaseAddress);
    4160         RTLDRADDR uLdrRtlUserThreadStart;
     4160
     4161        /* NtDll size and the more recent default thread start entrypoint (Vista+?): */
     4162        RTLDRADDR uSystemThreadStart;
    41614163        rc = RTLdrGetSymbolEx(pLdrEntry->hLdrMod, pbChildNtDllBits, pThis->uNtDllAddr, UINT32_MAX,
    4162                               "RtlUserThreadStart", &uLdrRtlUserThreadStart);
     4164                              "RtlUserThreadStart", &uSystemThreadStart);
    41634165        if (RT_FAILURE(rc))
    4164             uLdrRtlUserThreadStart = 0;
     4166            uSystemThreadStart = 0;
     4167
     4168        /* Kernel32 for thread start of older windows version, only XP64/W2K3-64 has an actual
     4169           export for it.  Unfortunately, it is not yet loaded into the child, so we have to
     4170           assume same location as in the parent (safe): */
     4171        PSUPHNTLDRCACHEENTRY pLdrEntryKernel32;
     4172        int rc = supHardNtLdrCacheOpen("kernel32.dll", &pLdrEntryKernel32, NULL /*pErrInfo*/);
     4173        if (RT_FAILURE(rc))
     4174            supR3HardenedWinKillChild(pThis, "supR3HardenedWinSetupChildInit", rc,
     4175                                      "supHardNtLdrCacheOpen failed on KERNEL32: %Rrc\n", rc);
     4176        size_t const cbKernel32 = RTLdrSize(pLdrEntryKernel32->hLdrMod);
     4177
     4178#ifdef RT_ARCH_AMD64
     4179        if (!uSystemThreadStart)
     4180        {
     4181            rc = RTLdrGetSymbolEx(pLdrEntry->hLdrMod, pbChildNtDllBits, pLdrEntryKernel32->uImageBase, UINT32_MAX,
     4182                                  "BaseProcessStart", &uSystemThreadStart);
     4183            if (RT_FAILURE(rc))
     4184                uSystemThreadStart = 0;
     4185        }
     4186#endif
    41654187
    41664188        bool fUpdateContext = false;
    41674189
    4168         /* Check if the RIP looks half sane, correct it if it isn't.
     4190        /* Check if the RIP looks half sane, try correct it if it isn't.
    41694191           It should point to RtlUserThreadStart (Vista and later it seem), though only
    41704192           tested on win10.  The first parameter is the executable entrypoint, the 2nd
    4171            is probably the PEB. */
    4172         if (   (  uLdrRtlUserThreadStart
    4173                 ? *pPC == uLdrRtlUserThreadStart
    4174                 : *pPC - pThis->uNtDllAddr <= cbNtDll)
     4193           is probably the PEB.  Before Vista it should point to Kernel32!BaseProcessStart,
     4194           though the symbol is only exported in 5.2/AMD64. */
     4195        if (   (  uSystemThreadStart
     4196                ? *pPC == uSystemThreadStart
     4197                : *pPC - (  pLdrEntryKernel32->uImageBase != ~(uintptr_t)0 ? pLdrEntryKernel32->uImageBase
     4198                          : (uintptr_t)GetModuleHandleW(L"kernel32.dll")) <= cbKernel32)
    41754199            || *pPC == uChildMain)
    41764200        { }
    41774201        else
    41784202        {
    4179             SUP_DPRINTF(("Warning! Bogus RIP: %016RX64\n", *pPC));
    4180             if (uLdrRtlUserThreadStart)
     4203            SUP_DPRINTF(("Warning! Bogus RIP: %p (uSystemThreadStart=%p; kernel32 %p LB %p; uChildMain=%p)\n",
     4204                         *pPC, uSystemThreadStart, pLdrEntryKernel32->uImageBase, cbKernel32, uChildMain));
     4205            if (uSystemThreadStart)
    41814206            {
    4182                 SUP_DPRINTF(("Correcting RIP from to %016RX64 hoping that it might work...\n", (uintptr_t)uLdrRtlUserThreadStart));
    4183                 *pPC = uLdrRtlUserThreadStart;
     4207                SUP_DPRINTF(("Correcting RIP from to %p hoping that it might work...\n", (uintptr_t)uSystemThreadStart));
     4208                *pPC = uSystemThreadStart;
    41844209                fUpdateContext = true;
    41854210            }
    41864211        }
    41874212#ifdef RT_ARCH_AMD64
    4188         if (Ctx.SegDs != 0)
    4189             SUP_DPRINTF(("Warning! Bogus DS: %04x, expected zero\n", Ctx.SegDs));
    4190         if (Ctx.SegEs != 0)
    4191             SUP_DPRINTF(("Warning! Bogus ES: %04x, expected zero\n", Ctx.SegEs));
    4192         if (Ctx.SegFs != 0)
    4193             SUP_DPRINTF(("Warning! Bogus FS: %04x, expected zero\n", Ctx.SegFs));
    4194         if (Ctx.SegGs != 0)
    4195             SUP_DPRINTF(("Warning! Bogus GS: %04x, expected zero\n", Ctx.SegGs));
     4213        if (g_uNtVerCombined >= SUP_MAKE_NT_VER_SIMPLE(10, 0)) /* W2K3: CS=33 SS=DS=ES=GS=2b FS=53 */
     4214        {
     4215            if (Ctx.SegDs != 0)
     4216                SUP_DPRINTF(("Warning! Bogus DS: %04x, expected zero\n", Ctx.SegDs));
     4217            if (Ctx.SegEs != 0)
     4218                SUP_DPRINTF(("Warning! Bogus ES: %04x, expected zero\n", Ctx.SegEs));
     4219            if (Ctx.SegFs != 0)
     4220                SUP_DPRINTF(("Warning! Bogus FS: %04x, expected zero\n", Ctx.SegFs));
     4221            if (Ctx.SegGs != 0)
     4222                SUP_DPRINTF(("Warning! Bogus GS: %04x, expected zero\n", Ctx.SegGs));
     4223        }
    41964224        if (Ctx.Rcx != uChildMain)
    41974225            SUP_DPRINTF(("Warning! Bogus RCX: %016RX64, expected %016RX64\n", Ctx.Rcx, uChildMain));
     4226        if (Ctx.Rdx & PAGE_OFFSET_MASK)
     4227            SUP_DPRINTF(("Warning! Bogus RDX: %016RX64, expected page aligned\n", Ctx.Rdx)); /* PEB */
    41984228        if ((Ctx.Rsp & 15) != 8)
    41994229            SUP_DPRINTF(("Warning! Misaligned RSP: %016RX64\n", Ctx.Rsp));
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