VirtualBox

Ignore:
Timestamp:
Oct 6, 2022 8:23:59 AM (2 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
153956
Message:

SUP: Check inherited handles in bugging VM processes. bugref:10294

File:
1 edited

Legend:

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

    r96407 r97023  
    49434943
    49444944
     4945static const char *supdrvNtProtectHandleTypeIndexToName(ULONG idxType, char *pszName, size_t cbName)
     4946{
     4947    /*
     4948     * Query the object types.
     4949     */
     4950    uint32_t  cbBuf    = _8K;
     4951    uint8_t  *pbBuf    = (uint8_t *)RTMemAllocZ(_8K);
     4952    ULONG     cbNeeded = cbBuf;
     4953    NTSTATUS rcNt = NtQueryObject(NULL, ObjectTypesInformation, pbBuf, cbBuf, &cbNeeded);
     4954    while (rcNt == STATUS_INFO_LENGTH_MISMATCH)
     4955    {
     4956        cbBuf = RT_ALIGN_32(cbNeeded + 256, _64K);
     4957        RTMemFree(pbBuf);
     4958        pbBuf = (uint8_t *)RTMemAllocZ(cbBuf);
     4959        if (pbBuf)
     4960            rcNt = NtQueryObject(NULL, ObjectTypesInformation, pbBuf, cbBuf, &cbNeeded);
     4961        else
     4962            break;
     4963    }
     4964    if (NT_SUCCESS(rcNt))
     4965    {
     4966        Assert(cbNeeded <= cbBuf);
     4967
     4968        POBJECT_TYPES_INFORMATION pObjTypes = (OBJECT_TYPES_INFORMATION *)pbBuf;
     4969        POBJECT_TYPE_INFORMATION  pCurType  = &pObjTypes->FirstType;
     4970        ULONG cLeft = pObjTypes->NumberOfTypes;
     4971        while (cLeft-- > 0 && (uintptr_t)&pCurType[1] - (uintptr_t)pbBuf < cbNeeded)
     4972        {
     4973            if (pCurType->TypeIndex == idxType)
     4974            {
     4975                PCRTUTF16 const pwszSrc = pCurType->TypeName.Buffer;
     4976                AssertBreak(pwszSrc);
     4977                size_t          idxName = pCurType->TypeName.Length / sizeof(RTUTF16);
     4978                AssertBreak(idxName > 0);
     4979                AssertBreak(idxName < 128);
     4980                if (idxName >= cbName)
     4981                    idxName = cbName - 1;
     4982                pszName[idxName] = '\0';
     4983                while (idxName-- > 0)
     4984                    pszName[idxName] = (char )pwszSrc[idxName];
     4985                RTMemFree(pbBuf);
     4986                return pszName;
     4987            }
     4988
     4989            /* next */
     4990            pCurType = (POBJECT_TYPE_INFORMATION)(  (uintptr_t)pCurType->TypeName.Buffer
     4991                                                  + RT_ALIGN_32(pCurType->TypeName.MaximumLength, sizeof(uintptr_t)));
     4992        }
     4993    }
     4994
     4995    RTMemFree(pbBuf);
     4996    return "unknown";
     4997}
     4998
     4999
    49455000/**
    49465001 * Worker for supdrvNtProtectVerifyProcess that verifies the handles to a VM
     
    50095064    uint32_t cBenignThreadHandles  = 0;
    50105065
     5066    uint32_t cEvilInheritableHandles   = 0;
     5067    uint32_t cBenignInheritableHandles = 0;
     5068    char     szTmpName[32];
     5069
    50115070    SYSTEM_HANDLE_INFORMATION_EX const *pInfo = (SYSTEM_HANDLE_INFORMATION_EX const *)pbBuf;
    50125071    ULONG_PTR i = pInfo->NumberOfHandles;
     
    50705129            cEvilThreadHandles++;
    50715130            pszType = "thread";
     5131        }
     5132        else if (   (pHandleInfo->HandleAttributes & OBJ_INHERIT)
     5133                 && pHandleInfo->UniqueProcessId == hProtectedPid)
     5134        {
     5135            /* No handles should be marked inheritable, except files and two events.
     5136               Handles to NT 'directory' objects are especially evil, because of
     5137               KnownDlls faking. See bugref{10294} for details.
     5138
     5139               Correlating the ObjectTypeIndex to a type is complicated, so instead
     5140               we try referecing the handle and check the type that way.  So, only
     5141               file and events objects are allowed to be marked inheritable at the
     5142               moment. Add more in whitelist fashion if needed. */
     5143            void *pvObject = NULL;
     5144            rcNt = ObReferenceObjectByHandle(pHandleInfo->HandleValue, 0, *IoFileObjectType, KernelMode, &pvObject, NULL);
     5145            if (rcNt == STATUS_OBJECT_TYPE_MISMATCH)
     5146                rcNt = ObReferenceObjectByHandle(pHandleInfo->HandleValue, 0, *ExEventObjectType, KernelMode, &pvObject, NULL);
     5147            if (NT_SUCCESS(rcNt))
     5148            {
     5149                ObDereferenceObject(pvObject);
     5150                cBenignInheritableHandles++;
     5151                continue;
     5152            }
     5153
     5154            if (rcNt != STATUS_OBJECT_TYPE_MISMATCH)
     5155            {
     5156                cBenignInheritableHandles++;
     5157                continue;
     5158            }
     5159
     5160            cEvilInheritableHandles++;
     5161            pszType = supdrvNtProtectHandleTypeIndexToName(pHandleInfo->ObjectTypeIndex, szTmpName, sizeof(szTmpName));
    50725162        }
    50735163        else
     
    51005190            || g_pfnObRegisterCallbacks)
    51015191        {
    5102             LogRel(("vboxdrv: Found evil handle to budding VM process: pid=%p h=%p acc=%#x attr=%#x type=%s\n",
     5192            LogRel(("vboxdrv: Found evil handle to budding VM process: pid=%p h=%p acc=%#x attr=%#x type=%s (%u)\n",
    51035193                    pHandleInfo->UniqueProcessId, pHandleInfo->HandleValue,
    5104                     pHandleInfo->GrantedAccess, pHandleInfo->HandleAttributes, pszType));
     5194                    pHandleInfo->GrantedAccess, pHandleInfo->HandleAttributes, pszType, pHandleInfo->ObjectTypeIndex));
    51055195            rc = RTErrInfoAddF(pErrInfo, VERR_SUPDRV_HARDENING_EVIL_HANDLE,
    51065196                               *pErrInfo->pszMsg
    5107                                ? "\nFound evil handle to budding VM process: pid=%p h=%p acc=%#x attr=%#x type=%s"
    5108                                : "Found evil handle to budding VM process: pid=%p h=%p acc=%#x attr=%#x type=%s",
     5197                               ? "\nFound evil handle to budding VM process: pid=%p h=%p acc=%#x attr=%#x type=%s (%u)"
     5198                               : "Found evil handle to budding VM process: pid=%p h=%p acc=%#x attr=%#x type=%s (%u)",
    51095199                               pHandleInfo->UniqueProcessId, pHandleInfo->HandleValue,
    5110                                pHandleInfo->GrantedAccess, pHandleInfo->HandleAttributes, pszType);
     5200                               pHandleInfo->GrantedAccess, pHandleInfo->HandleAttributes, pszType, pHandleInfo->ObjectTypeIndex);
    51115201
    51125202            /* Try add the process name. */
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