VirtualBox

Changeset 52365 in vbox


Ignore:
Timestamp:
Aug 13, 2014 6:11:50 AM (11 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
95497
Message:

sup: Check for TrustedInstaller; accept ProgramFiles and CommonFiles.

Location:
trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/VBox/err.h

    r52205 r52365  
    25052505/** Process Purification Failure: Unknown memory type of executable memory.   */
    25062506#define VERR_SUP_VP_UNKOWN_MEM_TYPE                 (-5666)
     2507/** The image file is not owned by TrustedInstaller is it should be. */
     2508#define VERR_SUP_VP_NOT_OWNED_BY_TRUSTED_INSTALLER  (-5667)
    25072509
    25082510/** @} */
  • trunk/src/VBox/HostDrivers/Support/win/SUPHardenedVerify-win.h

    r52356 r52365  
    9797/** Whether to allow image verification by catalog file. */
    9898#  define SUPHNTVI_F_ALLOW_CAT_FILE_VERIFICATION    RT_BIT(3)
     99/** The file owner must be TrustedInstaller on Vista+. */
     100#  define SUPHNTVI_F_TRUSTED_INSTALLER_OWNER        RT_BIT(4)
    99101/** Resource image, could be any bitness. */
    100102#  define SUPHNTVI_F_RESOURCE_IMAGE                 RT_BIT(30)
     
    122124extern SUPSYSROOTDIRBUF g_System32NtPath;
    123125extern SUPSYSROOTDIRBUF g_WinSxSNtPath;
     126#ifdef IN_RING3
     127extern SUPSYSROOTDIRBUF g_ProgramFilesNtPath;
     128extern SUPSYSROOTDIRBUF g_CommonFilesNtPath;
     129# if ARCH_BITS == 64
     130extern SUPSYSROOTDIRBUF g_ProgramFilesX86NtPath;
     131extern SUPSYSROOTDIRBUF g_CommonFilesX86NtPath;
     132# endif
     133#endif
    124134extern SUPSYSROOTDIRBUF g_SupLibHardenedExeNtPath;
    125135extern uint32_t         g_offSupLibHardenedExeNtName;
  • trunk/src/VBox/HostDrivers/Support/win/SUPHardenedVerifyImage-win.cpp

    r52356 r52365  
    117117/** The full \\SystemRoot\\WinSxS path. */
    118118SUPSYSROOTDIRBUF            g_WinSxSNtPath;
     119#ifdef IN_RING3
     120/** The full 'Program Files' path. */
     121SUPSYSROOTDIRBUF            g_ProgramFilesNtPath;
     122# ifdef RT_ARCH_AMD64
     123/** The full 'Program Files (x86)' path. */
     124SUPSYSROOTDIRBUF            g_ProgramFilesX86NtPath;
     125# endif
     126/** The full 'Common Files' path. */
     127SUPSYSROOTDIRBUF            g_CommonFilesNtPath;
     128# ifdef RT_ARCH_AMD64
     129/** The full 'Common Files (x86)' path. */
     130SUPSYSROOTDIRBUF            g_CommonFilesX86NtPath;
     131# endif
     132#endif /* IN_RING3 */
     133
     134/** The TrustedInstaller SID (Vista+). */
     135static union
     136{
     137    SID                     Sid;
     138    uint8_t                 abPadding[SECURITY_MAX_SID_SIZE];
     139}                           g_TrustedInstallerSid;
    119140
    120141/** Set after we've retrived other SPC root certificates from the system. */
     
    379400
    380401/**
     402 * Checks if the file is owned by TrustedInstaller on Vista and later.
     403 *
     404 * @returns true if owned by TrustedInstaller of pre-Vista, false if not.
     405 *
     406 * @param   hFile               The handle to the file.
     407 * @param   pwszName            The name of the file.
     408 */
     409static bool supHardNtViCheckIsOwnedByTrustedInstaller(HANDLE hFile, PCRTUTF16 pwszName)
     410{
     411    if (g_uNtVerCombined < SUP_NT_VER_VISTA)
     412        return true;
     413
     414    /*
     415     * Get the ownership information.
     416     */
     417    union
     418    {
     419        SECURITY_DESCRIPTOR_RELATIVE    Rel;
     420        SECURITY_DESCRIPTOR             Abs;
     421        uint8_t                         abView[256];
     422    } uBuf;
     423    ULONG cbActual;
     424    NTSTATUS rcNt = NtQuerySecurityObject(hFile, OWNER_SECURITY_INFORMATION, &uBuf.Abs, sizeof(uBuf), &cbActual);
     425SUP_DPRINTF(("NtQuerySecurityObject: rcNt=%#x on '%ls'\n", rcNt, pwszName));
     426    if (!NT_SUCCESS(rcNt))
     427    {
     428        SUP_DPRINTF(("NtQuerySecurityObject failed with rcNt=%#x on '%ls'\n", rcNt, pwszName));
     429        return false;
     430    }
     431
     432    /*
     433     * Check the owner.
     434     */
     435    PSID pOwner = uBuf.Rel.Control & SE_SELF_RELATIVE ? &uBuf.abView[uBuf.Rel.Owner] : uBuf.Abs.Owner;
     436    Assert((uintptr_t)pOwner - (uintptr_t)&uBuf < sizeof(uBuf) - sizeof(SID));
     437    if (RtlEqualSid(pOwner, &g_TrustedInstallerSid))
     438        return true;
     439
     440    SUP_DPRINTF(("%ls: Owner is not trusted installer (%.*Rhxs)\n",
     441                 pwszName, ((uint8_t *)pOwner)[1] /*SubAuthorityCount*/ * sizeof(ULONG) + 8, pOwner));
     442    return false;
     443}
     444
     445
     446/**
    381447 * Simple case insensitive UTF-16 / ASCII path compare.
    382448 *
     
    484550                                                PCRTUTF16 pwszRight, uint32_t cwcRight, bool fCheckSlash)
    485551{
    486     if (cwcLeft < cwcRight)
     552    if (cwcLeft < cwcRight || !cwcRight || !pwszRight)
    487553        return false;
    488554
     
    586652 * @param   pwszName            The NT name of the DLL/EXE.
    587653 * @param   fFlags              Flags.
     654 * @param   hFile               The file handle.
    588655 * @param   rc                  The status code..
    589656 */
    590 static int supHardNtViCheckIfNotSignedOk(RTLDRMOD hLdrMod, PCRTUTF16 pwszName, uint32_t fFlags, int rc)
     657static int supHardNtViCheckIfNotSignedOk(RTLDRMOD hLdrMod, PCRTUTF16 pwszName, uint32_t fFlags, HANDLE hFile, int rc)
    591658{
    592659    if (fFlags & (SUPHNTVI_F_REQUIRE_BUILD_CERT | SUPHNTVI_F_REQUIRE_KERNEL_CODE_SIGNING))
     
    625692    {
    626693        pwsz = pwszName + cwcOther + 1;
     694
     695        /* Must be owned by trusted installer. */
     696        if (   !(fFlags & SUPHNTVI_F_TRUSTED_INSTALLER_OWNER)
     697            && !supHardNtViCheckIsOwnedByTrustedInstaller(hFile, pwszName))
     698            return rc;
    627699
    628700        /* Core DLLs. */
     
    650722
    651723#ifndef IN_RING0
    652 # if 0 /* Allow anything below System32 that WinVerifyTrust thinks is fine. */
    653         /* The ATI drivers load system drivers into the process, allow this,
    654            but reject anything else from a subdirectory. */
    655         uint32_t cSlashes = supHardViUtf16PathCountSlashes(pwsz);
    656         if (cSlashes > 0)
    657         {
    658             if (   cSlashes == 1
    659                 && supHardViUtf16PathStartsWithAscii(pwsz, "drivers\\ati")
    660                 && (   supHardViUtf16PathEndsWith(pwsz, ".sys")
    661                     || supHardViUtf16PathEndsWith(pwsz, ".dll") ) )
    662                 return VINF_LDRVI_NOT_SIGNED;
    663             return rc;
    664         }
    665 # endif
    666 
    667724        /* Check that this DLL isn't supposed to be signed on this windows
    668725           version.  If it should, it's likely to be a fake. */
    669726        /** @todo list of signed dlls for various windows versions.  */
    670 
    671         /** @todo check file permissions? TrustedInstaller is supposed to be involved
    672          *        with all of them. */
     727SUP_DPRINTF(("supHardNtViCheckIfNotSignedOk: VINF_LDRVI_NOT_SIGNED\n"));
    673728        return VINF_LDRVI_NOT_SIGNED;
    674729#else
    675730        return rc;
    676 #endif
     731#endif /* IN_RING0 */
    677732    }
    678733
     
    682737     *
    683738     * Just like with System32 there are potentially a number of DLLs that
    684      * could be required from WinSxS.  However, so far only comctl32.dll
    685      * variations have been required.  So, we limit ourselves to explicit
    686      * whitelisting of unsigned families of DLLs.
     739     * could be required from WinSxS.
    687740     */
    688741    cwcOther = g_WinSxSNtPath.UniStr.Length / sizeof(WCHAR);
     
    697750            return rc;
    698751
    699 # if 0 /* See below */
    700         /* The common controls mess. */
    701 # ifdef RT_ARCH_AMD64
    702         if (supHardViUtf16PathStartsWithAscii(pwsz, "amd64_microsoft.windows.common-controls_"))
    703 # elif defined(RT_ARCH_X86)
    704         if (supHardViUtf16PathStartsWithAscii(pwsz, "x86_microsoft.windows.common-controls_"))
    705 # else
    706 #  error "Unsupported architecture"
    707 # endif
    708         {
    709             if (supHardViUtf16PathEndsWith(pwsz, "\\comctl32.dll"))
    710                 return VINF_LDRVI_NOT_SIGNED;
    711         }
    712 # endif
    713 
    714         /* Allow anything slightly microsoftish from WinSxS. W2K3 wanted winhttp.dll early on... */
    715 # ifdef RT_ARCH_AMD64
    716         if (supHardViUtf16PathStartsWithAscii(pwsz, "amd64_microsoft."))
    717 # elif defined(RT_ARCH_X86)
    718         if (supHardViUtf16PathStartsWithAscii(pwsz, "x86_microsoft."))
    719 # else
    720 #  error "Unsupported architecture"
    721 # endif
    722         {
     752        if (   (fFlags & SUPHNTVI_F_TRUSTED_INSTALLER_OWNER)
     753            && supHardNtViCheckIsOwnedByTrustedInstaller(hFile, pwszName))
    723754            return VINF_LDRVI_NOT_SIGNED;
    724         }
    725 
    726755        return rc;
    727756    }
    728 #endif
     757#endif /* !IN_RING0 */
    729758
    730759#ifdef VBOX_PERMIT_MORE
     
    736765        cwcOther = g_System32NtPath.UniStr.Length / sizeof(WCHAR); /* ASSUMES System32 is called System32. */
    737766        pwsz = pwszName + cwcOther + 1;
     767
     768        if (   !(fFlags & SUPHNTVI_F_TRUSTED_INSTALLER_OWNER)
     769            && !supHardNtViCheckIsOwnedByTrustedInstaller(hFile, pwszName))
     770            return rc;
    738771
    739772        if (supHardViUtf16PathIsEqual(pwsz, "acres.dll"))
     
    748781# endif
    749782
     783# ifndef IN_RING0
     784        return VINF_LDRVI_NOT_SIGNED;
     785# else
    750786        return rc;
    751     }
    752 #else
    753 # error should not be here...
    754 #endif
     787# endif
     788    }
     789#endif /* VBOX_PERMIT_MORE */
     790
     791#if !defined(IN_RING0) && defined(VBOX_PERMIT_MORE)
     792    /*
     793     * Program files and common files.
     794     * Permit anything that's signed and correctly installed.
     795     */
     796    if (   supHardViUtf16PathStartsWithEx(pwszName, cwcName,
     797                                          g_ProgramFilesNtPath.UniStr.Buffer, g_ProgramFilesNtPath.UniStr.Length,
     798                                          true /*fCheckSlash*/)
     799        || supHardViUtf16PathStartsWithEx(pwszName, cwcName,
     800                                          g_CommonFilesNtPath.UniStr.Buffer, g_CommonFilesNtPath.UniStr.Length,
     801                                          true /*fCheckSlash*/)
     802# ifdef RT_ARCH_AMD64
     803        || supHardViUtf16PathStartsWithEx(pwszName, cwcName,
     804                                          g_ProgramFilesX86NtPath.UniStr.Buffer, g_ProgramFilesX86NtPath.UniStr.Length,
     805                                          true /*fCheckSlash*/)
     806        || supHardViUtf16PathStartsWithEx(pwszName, cwcName,
     807                                          g_CommonFilesX86NtPath.UniStr.Buffer, g_CommonFilesX86NtPath.UniStr.Length,
     808                                          true /*fCheckSlash*/)
     809# endif
     810       )
     811    {
     812        if (   (fFlags & SUPHNTVI_F_TRUSTED_INSTALLER_OWNER)
     813            && supHardNtViCheckIsOwnedByTrustedInstaller(hFile, pwszName))
     814            return VINF_LDRVI_NOT_SIGNED;
     815        return rc;
     816    }
     817#endif /* !IN_RING0 && VBOX_PERMIT_MORE*/
    755818
    756819    return rc;
     
    9401003
    9411004    /*
     1005     * Check the trusted installer bit first, if requested as it's somewhat
     1006     * cheaper than the rest.
     1007     */
     1008    if (   (pNtViRdr->fFlags & SUPHNTVI_F_TRUSTED_INSTALLER_OWNER)
     1009        && !supHardNtViCheckIsOwnedByTrustedInstaller(pNtViRdr->hFile, pwszName))
     1010        return RTErrInfoSetF(pErrInfo, VERR_SUP_VP_NOT_OWNED_BY_TRUSTED_INSTALLER,
     1011                             "supHardenedWinVerifyImageByHandle: TrustedInstaller is not the owner of '%ls'.", pwszName);
     1012
     1013    /*
    9421014     * Verify it.
    9431015     *
     
    9761048         */
    9771049        if (rc == VERR_LDRVI_NOT_SIGNED)
    978             rc = supHardNtViCheckIfNotSignedOk(hLdrMod, pwszName, pNtViRdr->fFlags, rc);
     1050            rc = supHardNtViCheckIfNotSignedOk(hLdrMod, pwszName, pNtViRdr->fFlags, pNtViRdr->hFile, rc);
    9791051        if (RT_FAILURE(rc))
    9801052            RTErrInfoAddF(pErrInfo, rc, ": %ls", pwszName);
     
    13071379
    13081380
     1381
     1382#ifdef IN_RING3
     1383/**
     1384 * Initializes the windows paths.
     1385 */
     1386static void supHardenedWinInitImageVerifierWinPaths(void)
     1387{
     1388    /*
     1389     * Windows paths that we're interested in.
     1390     */
     1391    static const struct
     1392    {
     1393        SUPSYSROOTDIRBUF   *pNtPath;
     1394        WCHAR const        *pwszRegValue;
     1395        const char         *pszLogName;
     1396    } s_aPaths[] =
     1397    {
     1398        { &g_ProgramFilesNtPath,    L"ProgramFilesDir",         "ProgDir" },
     1399        { &g_CommonFilesNtPath,     L"CommonFilesDir",          "ComDir" },
     1400# ifdef RT_ARCH_AMD64
     1401        { &g_ProgramFilesX86NtPath, L"ProgramFilesDir (x86)",   "ProgDir32" },
     1402        { &g_CommonFilesX86NtPath,  L"CommonFilesDir (x86)",    "ComDir32" },
     1403# endif
     1404    };
     1405
     1406    /*
     1407     * Open the registry key containing the paths.
     1408     */
     1409    UNICODE_STRING NtName = RTNT_CONSTANT_UNISTR(L"\\Registry\\Machine\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion");
     1410    OBJECT_ATTRIBUTES ObjAttr;
     1411    InitializeObjectAttributes(&ObjAttr, &NtName, OBJ_CASE_INSENSITIVE, NULL /*hRootDir*/, NULL /*pSecDesc*/);
     1412    HANDLE hKey;
     1413    NTSTATUS rcNt = NtOpenKey(&hKey, KEY_QUERY_VALUE, &ObjAttr);
     1414    if (NT_SUCCESS(rcNt))
     1415    {
     1416        /*
     1417         * Loop over the paths and resolve their NT paths.
     1418         */
     1419        for (uint32_t i = 0; i < RT_ELEMENTS(s_aPaths); i++)
     1420        {
     1421            /*
     1422             * Query the value first.
     1423             */
     1424            UNICODE_STRING ValueName;
     1425            ValueName.Buffer = (WCHAR *)s_aPaths[i].pwszRegValue;
     1426            ValueName.Length = (USHORT)(RTUtf16Len(s_aPaths[i].pwszRegValue) * sizeof(WCHAR));
     1427            ValueName.MaximumLength = ValueName.Length + sizeof(WCHAR);
     1428
     1429            union
     1430            {
     1431                KEY_VALUE_PARTIAL_INFORMATION   PartialInfo;
     1432                uint8_t                         abPadding[sizeof(KEY_VALUE_PARTIAL_INFORMATION) + sizeof(WCHAR) * 128];
     1433                uint64_t                        uAlign;
     1434            } uBuf;
     1435
     1436            ULONG cbActual = 0;
     1437            rcNt = NtQueryValueKey(hKey, &ValueName, KeyValuePartialInformation, &uBuf, sizeof(uBuf) - sizeof(WCHAR), &cbActual);
     1438            if (NT_SUCCESS(rcNt))
     1439            {
     1440                /*
     1441                 * Must be a simple string value, terminate it.
     1442                 */
     1443                if (   uBuf.PartialInfo.Type == REG_EXPAND_SZ
     1444                    || uBuf.PartialInfo.Type == REG_SZ)
     1445                {
     1446                    /*
     1447                     * Expand any environment variable references before opening it.
     1448                     * We use the result buffer as storage for the expaneded path,
     1449                     * reserving space for the windows name space prefix.
     1450                     */
     1451                    UNICODE_STRING Src;
     1452                    Src.Buffer = (WCHAR *)uBuf.PartialInfo.Data;
     1453                    Src.Length = uBuf.PartialInfo.DataLength;
     1454                    if (Src.Length >= sizeof(WCHAR) && Src.Buffer[Src.Length / sizeof(WCHAR) - 1] == '\0')
     1455                        Src.Length -= sizeof(WCHAR);
     1456                    Src.MaximumLength = Src.Length + sizeof(WCHAR);
     1457                    Src.Buffer[uBuf.PartialInfo.DataLength / sizeof(WCHAR)] = '\0';
     1458
     1459                    s_aPaths[i].pNtPath->awcBuffer[0] = '\\';
     1460                    s_aPaths[i].pNtPath->awcBuffer[1] = '?';
     1461                    s_aPaths[i].pNtPath->awcBuffer[2] = '?';
     1462                    s_aPaths[i].pNtPath->awcBuffer[3] = '\\';
     1463                    UNICODE_STRING Dst;
     1464                    Dst.Buffer = &s_aPaths[i].pNtPath->awcBuffer[4];
     1465                    Dst.MaximumLength = sizeof(s_aPaths[i].pNtPath->awcBuffer) - sizeof(WCHAR) * 5;
     1466                    Dst.Length = Dst.MaximumLength;
     1467
     1468                    if (uBuf.PartialInfo.Type == REG_EXPAND_SZ)
     1469                        rcNt = RtlExpandEnvironmentStrings_U(NULL, &Src, &Dst, NULL);
     1470                    else
     1471                    {
     1472                        memcpy(Dst.Buffer, Src.Buffer, Src.Length);
     1473                        Dst.Length = Src.Length;
     1474                    }
     1475                    if (NT_SUCCESS(rcNt))
     1476                    {
     1477                        Dst.Buffer[Dst.Length / sizeof(WCHAR)] = '\0';
     1478
     1479                        /*
     1480                         * Include the \\??\\ prefix in the result and open the path.
     1481                         */
     1482                        Dst.Buffer        -= 4;
     1483                        Dst.Length        += 4 * sizeof(WCHAR);
     1484                        Dst.MaximumLength += 4 * sizeof(WCHAR);
     1485                        InitializeObjectAttributes(&ObjAttr, &Dst, OBJ_CASE_INSENSITIVE, NULL /*hRootDir*/, NULL /*pSecDesc*/);
     1486                        HANDLE          hFile = INVALID_HANDLE_VALUE;
     1487                        IO_STATUS_BLOCK Ios   = RTNT_IO_STATUS_BLOCK_INITIALIZER;
     1488                        NTSTATUS rcNt = NtCreateFile(&hFile,
     1489                                                     FILE_READ_DATA | SYNCHRONIZE,
     1490                                                     &ObjAttr,
     1491                                                     &Ios,
     1492                                                     NULL /* Allocation Size*/,
     1493                                                     FILE_ATTRIBUTE_NORMAL,
     1494                                                     FILE_SHARE_READ | FILE_SHARE_WRITE,
     1495                                                     FILE_OPEN,
     1496                                                     FILE_DIRECTORY_FILE | FILE_OPEN_FOR_BACKUP_INTENT
     1497                                                     | FILE_SYNCHRONOUS_IO_NONALERT,
     1498                                                     NULL /*EaBuffer*/,
     1499                                                     0 /*EaLength*/);
     1500                        if (NT_SUCCESS(rcNt))
     1501                            rcNt = Ios.Status;
     1502                        if (NT_SUCCESS(rcNt))
     1503                        {
     1504                            /*
     1505                             * Query the real NT name.
     1506                             */
     1507                            ULONG cbIgn;
     1508                            rcNt = NtQueryObject(hFile,
     1509                                                 ObjectNameInformation,
     1510                                                 s_aPaths[i].pNtPath,
     1511                                                 sizeof(*s_aPaths[i].pNtPath) - sizeof(WCHAR),
     1512                                                 &cbIgn);
     1513                            if (NT_SUCCESS(rcNt))
     1514                            {
     1515                                if (s_aPaths[i].pNtPath->UniStr.Length > 0)
     1516                                {
     1517                                    /* Make sure it's terminated.*/
     1518                                    s_aPaths[i].pNtPath->UniStr.Buffer[s_aPaths[i].pNtPath->UniStr.Length / sizeof(WCHAR)] = '\0';
     1519                                    SUP_DPRINTF(("%s:%*s %ls\n", s_aPaths[i].pszLogName, 9 - strlen(s_aPaths[i].pszLogName), "",
     1520                                                 s_aPaths[i].pNtPath->UniStr.Buffer));
     1521                                }
     1522                                else
     1523                                {
     1524                                    SUP_DPRINTF(("%s: NtQueryObject returned empty string\n", s_aPaths[i].pszLogName));
     1525                                    rcNt = STATUS_INVALID_PARAMETER;
     1526                                }
     1527                            }
     1528                            else
     1529                                SUP_DPRINTF(("%s: NtQueryObject failed: %#x\n", s_aPaths[i].pszLogName, rcNt));
     1530                            NtClose(hFile);
     1531                        }
     1532                        else
     1533                            SUP_DPRINTF(("%s: NtCreateFile failed: %#x (%ls)\n",
     1534                                         s_aPaths[i].pszLogName, rcNt, Dst.Buffer));
     1535                    }
     1536                    else
     1537                        SUP_DPRINTF(("%s: RtlExpandEnvironmentStrings_U failed: %#x (%ls)\n",
     1538                                     s_aPaths[i].pszLogName, rcNt, Src.Buffer));
     1539                }
     1540                else
     1541                {
     1542                    SUP_DPRINTF(("%s: type mismatch: %#x\n", s_aPaths[i].pszLogName, uBuf.PartialInfo.Type));
     1543                    rcNt = STATUS_INVALID_PARAMETER;
     1544                }
     1545            }
     1546            else
     1547                SUP_DPRINTF(("%s: NtQueryValueKey failed: %#x\n", s_aPaths[i].pszLogName, rcNt));
     1548
     1549            /* Stub the entry on failure. */
     1550            if (!NT_SUCCESS(rcNt))
     1551            {
     1552                s_aPaths[i].pNtPath->UniStr.Length = 0;
     1553                s_aPaths[i].pNtPath->UniStr.Buffer = NULL;
     1554            }
     1555        }
     1556        NtClose(hKey);
     1557    }
     1558    else
     1559    {
     1560        SUP_DPRINTF(("NtOpenKey(%ls) failed: %#x\n", NtName.Buffer, rcNt));
     1561
     1562        /* Stub all the entries on failure. */
     1563        for (uint32_t i = 0; i < RT_ELEMENTS(s_aPaths); i++)
     1564        {
     1565            s_aPaths[i].pNtPath->UniStr.Length = 0;
     1566            s_aPaths[i].pNtPath->UniStr.Buffer = NULL;
     1567        }
     1568    }
     1569}
     1570#endif /* IN_RING3 */
     1571
     1572
    13091573/**
    13101574 * This initializes the certificates globals so we don't have to reparse them
     
    13261590    if (RT_SUCCESS(rc))
    13271591    {
     1592        SUP_DPRINTF(("System32:  %ls\n", g_System32NtPath.UniStr.Buffer));
     1593        SUP_DPRINTF(("WinSxS:    %ls\n", g_WinSxSNtPath.UniStr.Buffer));
     1594#ifdef IN_RING3
     1595        supHardenedWinInitImageVerifierWinPaths();
     1596#endif
     1597
    13281598        /*
    13291599         * Initialize it, leaving the cleanup to the termination call.
     
    13591629
    13601630        if (RT_SUCCESS(rc))
    1361             return VINF_SUCCESS;
     1631        {
     1632            /*
     1633             * Finally initialize known SIDs that we use.
     1634             */
     1635            SID_IDENTIFIER_AUTHORITY s_NtAuth = SECURITY_NT_AUTHORITY;
     1636            NTSTATUS rcNt = RtlInitializeSid(&g_TrustedInstallerSid, &s_NtAuth, SECURITY_SERVICE_ID_RID_COUNT);
     1637            if (NT_SUCCESS(rcNt))
     1638            {
     1639                *RtlSubAuthoritySid(&g_TrustedInstallerSid, 0) = SECURITY_SERVICE_ID_BASE_RID;
     1640                *RtlSubAuthoritySid(&g_TrustedInstallerSid, 1) = 956008885;
     1641                *RtlSubAuthoritySid(&g_TrustedInstallerSid, 2) = 3418522649;
     1642                *RtlSubAuthoritySid(&g_TrustedInstallerSid, 3) = 1831038044;
     1643                *RtlSubAuthoritySid(&g_TrustedInstallerSid, 4) = 1853292631;
     1644                *RtlSubAuthoritySid(&g_TrustedInstallerSid, 5) = 2271478464;
     1645                return VINF_SUCCESS;
     1646            }
     1647            rc = RTErrConvertFromNtStatus(rcNt);
     1648        }
    13621649        supHardenedWinTermImageVerifier();
    13631650    }
     
    17712058            rc = RTErrInfoSetF(pErrInfo, VERR_LDRVI_UNSUPPORTED_ARCH,
    17722059                               "WinVerifyTrust failed with hrc=%Rhrc on '%ls'", hrc, pwszName);
     2060        SUP_DPRINTF(("supR3HardNtViCallWinVerifyTrust: WinVerifyTrust failed with %#x (%s) on '%ls'\n",
     2061                     hrc, pszErrConst, pwszName));
    17732062    }
    17742063
     
    18262115
    18272116        NTSTATUS rcNt = NtCreateFile(&hFile,
    1828                                      FILE_READ_DATA | SYNCHRONIZE,
     2117                                     FILE_READ_DATA | READ_CONTROL | SYNCHRONIZE,
    18292118                                     &ObjAttr,
    18302119                                     &Ios,
     
    19612250
    19622251                                HRESULT hrc = pfnWinVerifyTrust(NULL /*hwnd*/, &s_aPolicies[iPolicy], &TrustData);
    1963                                 SUP_DPRINTF(("supR3HardNtViCallWinVerifyTrustCatFile: WinVerifyTrust => %#x; cat=%ls\n", hrc, CatInfo.wszCatalogFile));
     2252                                SUP_DPRINTF(("supR3HardNtViCallWinVerifyTrustCatFile: WinVerifyTrust => %#x; cat='%ls'; file='%ls'\n",
     2253                                             hrc, CatInfo.wszCatalogFile, pwszName));
    19642254
    19652255                                if (SUCCEEDED(hrc))
  • trunk/src/VBox/HostDrivers/Support/win/SUPHardenedVerifyProcess-win.cpp

    r52213 r52365  
    15351535         * for this image.
    15361536         */
    1537         uint32_t fFlags = pImage->fDll ? 0 : SUPHNTVI_F_REQUIRE_BUILD_CERT;
     1537        uint32_t fFlags = pImage->fDll
     1538                        ? SUPHNTVI_F_TRUSTED_INSTALLER_OWNER | SUPHNTVI_F_ALLOW_CAT_FILE_VERIFICATION
     1539                        : SUPHNTVI_F_REQUIRE_BUILD_CERT;
    15381540        if (pImage->f32bitResourceDll)
    15391541            fFlags |= SUPHNTVI_F_RESOURCE_IMAGE;
  • trunk/src/VBox/HostDrivers/Support/win/SUPR3HardenedMain-win.cpp

    r52356 r52365  
    658658            {
    659659                supR3HardenedError(VINF_SUCCESS, false,
    660                                    "supR3HardenedMonitor_NtCreateSection: NtQueryObject -> %#x (fImage=%d fExecMap=%d fExecProt=%d)\n",
     660                                   "NtCreateSection: NtQueryObject -> %#x (fImage=%d fExecMap=%d fExecProt=%d)\n",
    661661                                   fImage, fExecMap, fExecProt);
    662662                return rcNt;
     
    675675            if (pCacheHit)
    676676            {
    677                 SUP_DPRINTF(("supR3HardenedMonitor_NtCreateSection: cache hit (%Rrc) on %ls\n", pCacheHit->rc, pCacheHit->wszPath));
     677                SUP_DPRINTF(("NtCreateSection: cache hit (%Rrc) on %ls\n", pCacheHit->rc, pCacheHit->wszPath));
    678678                if (RT_SUCCESS(pCacheHit->rc))
    679679                    return g_pfnNtCreateSectionReal(phSection, fAccess, pObjAttribs, pcbSection, fProtect, fAttribs, hFile);
    680680                supR3HardenedError(VINF_SUCCESS, false,
    681                                    "supR3HardenedMonitor_NtCreateSection: cached rc=%Rrc fImage=%d fExecMap=%d fExecProt=%d %ls\n",
     681                                   "NtCreateSection: cached rc=%Rrc fImage=%d fExecMap=%d fExecProt=%d %ls\n",
    682682                                   pCacheHit->rc, fImage, fExecMap, fExecProt, uBuf.UniStr.Buffer);
    683683                return STATUS_TRUST_FAILURE;
     
    686686            /*
    687687             * On XP the loader might hand us handles with just FILE_EXECUTE and
    688              * SYNCRHONIZE, the means reading will fail later on.  So, we might
    689              * have to reopen the file here in order to validate it - annoying.
     688             * SYNCHRONIZE, the means reading will fail later on.  Also, we need
     689             * READ_CONTROL access to check the file ownership later on, and non
     690             * of the OS versions seems be giving us that.  So, in effect we
     691             * more or less always reopen the file here.
    690692             */
    691693            HANDLE hMyFile = NULL;
    692694            rcNt = NtDuplicateObject(NtCurrentProcess(), hFile, NtCurrentProcess(),
    693695                                     &hMyFile,
    694                                      FILE_READ_DATA | SYNCHRONIZE,
     696                                     FILE_READ_DATA | READ_CONTROL | SYNCHRONIZE,
    695697                                     0 /* Handle attributes*/, 0 /* Options */);
    696698            if (!NT_SUCCESS(rcNt))
     
    698700                if (rcNt == STATUS_ACCESS_DENIED)
    699701                {
    700                     HANDLE              hFile = RTNT_INVALID_HANDLE_VALUE;
    701702                    IO_STATUS_BLOCK     Ios   = RTNT_IO_STATUS_BLOCK_INITIALIZER;
    702 
    703703                    OBJECT_ATTRIBUTES   ObjAttr;
    704704                    InitializeObjectAttributes(&ObjAttr, &uBuf.UniStr, OBJ_CASE_INSENSITIVE, NULL /*hRootDir*/, NULL /*pSecDesc*/);
    705705
    706706                    rcNt = NtCreateFile(&hMyFile,
    707                                         FILE_READ_DATA | SYNCHRONIZE,
     707                                        FILE_READ_DATA | READ_CONTROL | SYNCHRONIZE,
    708708                                        &ObjAttr,
    709709                                        &Ios,
     
    712712                                        FILE_SHARE_READ,
    713713                                        FILE_OPEN,
    714                                         FILE_NON_DIRECTORY_FILE,
     714                                        FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT,
    715715                                        NULL /*EaBuffer*/,
    716716                                        0 /*EaLength*/);
     
    720720                    {
    721721                        supR3HardenedError(VINF_SUCCESS, false,
    722                                            "supR3HardenedMonitor_NtCreateSection: Failed to duplicate and open the file: rcNt=%#x hFile=%p %ls\n",
     722                                           "NtCreateSection: Failed to duplicate and open the file: rcNt=%#x hFile=%p %ls\n",
    723723                                           rcNt, hFile, uBuf.UniStr.Buffer);
    724724                        return rcNt;
     725                    }
     726
     727                    /* Check that we've got the same file. */
     728                    LARGE_INTEGER idMyFile, idInFile;
     729                    bool fMyValid = supR3HardenedWinVerifyCacheGetIndexNumber(hMyFile, &idMyFile);
     730                    bool fInValid = supR3HardenedWinVerifyCacheGetIndexNumber(hFile, &idInFile);
     731                    if (   fMyValid
     732                        && (   fMyValid != fInValid
     733                            || idMyFile.QuadPart != idInFile.QuadPart))
     734                    {
     735                        supR3HardenedError(VINF_SUCCESS, false,
     736                                           "NtCreateSection: Re-opened has different ID that input: %#llx vx %#llx (%ls)\n",
     737                                           rcNt, idMyFile.QuadPart, idInFile.QuadPart, uBuf.UniStr.Buffer);
     738                        NtClose(hMyFile);
     739                        return STATUS_TRUST_FAILURE;
    725740                    }
    726741                }
    727742                else
    728743                {
     744                    SUP_DPRINTF(("supR3HardenedMonitor_NtCreateSection: NtDuplicateObject -> %#x\n", rcNt));
    729745#ifdef DEBUG
    730746
     
    757773                        fProtect = (fProtect & ~PAGE_EXECUTE) | PAGE_READONLY;
    758774                    fProtect = (fProtect & ~UINT32_C(0xf0)) | ((fProtect & UINT32_C(0xe0)) >> 4);
     775                    if (hMyFile != hFile)
     776                        NtClose(hMyFile);
    759777                    return g_pfnNtCreateSectionReal(phSection, fAccess, pObjAttribs, pcbSection, fProtect, fAttribs, hFile);
    760778                }
     
    763781            /*
    764782             * Check the path.  We don't allow DLLs to be loaded from just anywhere:
    765              *      1. System32   - normal code or cat signing.
    766              *      2. WinSxS     - normal code or cat signing.
    767              *      3. VirtualBox - kernel code signing and integrity checks.
     783             *      1. System32      - normal code or cat signing, owner TrustedInstaller.
     784             *      2. WinSxS        - normal code or cat signing, owner TrustedInstaller.
     785             *      3. VirtualBox    - kernel code signing and integrity checks.
     786             *      4. AppPatchDir   - normal code or cat signing, owner TrustedInstaller.
     787             *      5. Program Files - normal code or cat signing, owner TrustedInstaller.
     788             *      6. Common Files  - normal code or cat signing, owner TrustedInstaller.
     789             *      7. x86 variations of 4 & 5 - ditto.
    768790             */
    769791            bool fSystem32 = false;
     
    773795            {
    774796                fSystem32 = true;
    775                 fFlags |= SUPHNTVI_F_ALLOW_CAT_FILE_VERIFICATION;
     797                fFlags |= SUPHNTVI_F_ALLOW_CAT_FILE_VERIFICATION | SUPHNTVI_F_TRUSTED_INSTALLER_OWNER;
    776798            }
    777799            else if (supHardViUniStrPathStartsWithUniStr(&uBuf.UniStr, &g_WinSxSNtPath.UniStr, true /*fCheckSlash*/))
    778                 fFlags |= SUPHNTVI_F_ALLOW_CAT_FILE_VERIFICATION;
     800                fFlags |= SUPHNTVI_F_ALLOW_CAT_FILE_VERIFICATION | SUPHNTVI_F_TRUSTED_INSTALLER_OWNER;
    779801            else if (supHardViUtf16PathStartsWithEx(uBuf.UniStr.Buffer, uBuf.UniStr.Length / sizeof(WCHAR),
    780802                                                    g_SupLibHardenedExeNtPath.UniStr.Buffer,
     
    783805#ifdef VBOX_PERMIT_MORE
    784806            else if (supHardViIsAppPatchDir(uBuf.UniStr.Buffer, uBuf.UniStr.Length / sizeof(WCHAR)))
    785                 fFlags |= SUPHNTVI_F_ALLOW_CAT_FILE_VERIFICATION;
     807                fFlags |= SUPHNTVI_F_ALLOW_CAT_FILE_VERIFICATION | SUPHNTVI_F_TRUSTED_INSTALLER_OWNER;
     808            else if (supHardViUniStrPathStartsWithUniStr(&uBuf.UniStr, &g_ProgramFilesNtPath.UniStr, true /*fCheckSlash*/))
     809                fFlags |= SUPHNTVI_F_ALLOW_CAT_FILE_VERIFICATION | SUPHNTVI_F_TRUSTED_INSTALLER_OWNER;
     810            else if (supHardViUniStrPathStartsWithUniStr(&uBuf.UniStr, &g_CommonFilesNtPath.UniStr, true /*fCheckSlash*/))
     811                fFlags |= SUPHNTVI_F_ALLOW_CAT_FILE_VERIFICATION | SUPHNTVI_F_TRUSTED_INSTALLER_OWNER;
     812# ifdef RT_ARCH_AMD64
     813            else if (supHardViUniStrPathStartsWithUniStr(&uBuf.UniStr, &g_ProgramFilesX86NtPath.UniStr, true /*fCheckSlash*/))
     814                fFlags |= SUPHNTVI_F_ALLOW_CAT_FILE_VERIFICATION | SUPHNTVI_F_TRUSTED_INSTALLER_OWNER;
     815            else if (supHardViUniStrPathStartsWithUniStr(&uBuf.UniStr, &g_CommonFilesX86NtPath.UniStr, true /*fCheckSlash*/))
     816                fFlags |= SUPHNTVI_F_ALLOW_CAT_FILE_VERIFICATION | SUPHNTVI_F_TRUSTED_INSTALLER_OWNER;
     817# endif
    786818#endif
    787819#ifdef VBOX_PERMIT_VISUAL_STUDIO_PROFILING
  • trunk/src/VBox/HostDrivers/Support/win/import-template-kernel32.h

    r52356 r52365  
     1SUPHARNT_IMPORT_STDCALL(CloseHandle, 4)
     2SUPHARNT_IMPORT_STDCALL(CreateFileW, 28)
     3SUPHARNT_IMPORT_STDCALL(CreateProcessW, 40)
     4SUPHARNT_IMPORT_STDCALL(ExitProcess, 4)
     5SUPHARNT_IMPORT_STDCALL(GetCurrentThreadId, 0)
     6SUPHARNT_IMPORT_STDCALL(GetFullPathNameA, 16)
    17SUPHARNT_IMPORT_STDCALL(GetLastError, 0)
    2 SUPHARNT_IMPORT_STDCALL(GetFullPathNameA, 16)
    3 SUPHARNT_IMPORT_STDCALL(CloseHandle, 4)
     8SUPHARNT_IMPORT_STDCALL(GetModuleFileNameW, 12)
     9SUPHARNT_IMPORT_STDCALL(GetModuleHandleA, 4)
     10SUPHARNT_IMPORT_STDCALL(GetModuleHandleW, 4)
     11SUPHARNT_IMPORT_STDCALL(GetProcAddress, 8)
     12SUPHARNT_IMPORT_STDCALL(GetProcessHeap, 0)
     13SUPHARNT_IMPORT_STDCALL(GetTickCount, 0)
     14SUPHARNT_IMPORT_STDCALL(HeapAlloc, 12)
     15SUPHARNT_IMPORT_STDCALL(HeapFree, 8)
     16SUPHARNT_IMPORT_STDCALL(HeapReAlloc, 16)
     17SUPHARNT_IMPORT_STDCALL(LoadLibraryExW, 12)
     18SUPHARNT_IMPORT_STDCALL(OutputDebugStringA, 4)
     19SUPHARNT_IMPORT_STDCALL(Sleep, 4)
     20SUPHARNT_IMPORT_STDCALL(VirtualProtectEx, 20)
    421SUPHARNT_IMPORT_STDCALL(WriteFile, 20)
    5 SUPHARNT_IMPORT_STDCALL(OutputDebugStringA, 4)
    6 SUPHARNT_IMPORT_STDCALL(ExitProcess, 4)
    7 SUPHARNT_IMPORT_STDCALL(GetModuleHandleA, 4)
    8 SUPHARNT_IMPORT_STDCALL(GetModuleFileNameW, 12)
    9 SUPHARNT_IMPORT_STDCALL(CreateFileW, 28)
    10 SUPHARNT_IMPORT_STDCALL(Sleep, 4)
    11 SUPHARNT_IMPORT_STDCALL(CreateProcessW, 40)
    12 SUPHARNT_IMPORT_STDCALL(GetTickCount, 0)
    13 SUPHARNT_IMPORT_STDCALL(GetModuleHandleW, 4)
    14 SUPHARNT_IMPORT_STDCALL(HeapAlloc, 12)
    15 SUPHARNT_IMPORT_STDCALL(GetProcessHeap, 0)
    16 SUPHARNT_IMPORT_STDCALL(HeapReAlloc, 16)
    17 SUPHARNT_IMPORT_STDCALL(HeapFree, 8)
    18 SUPHARNT_IMPORT_STDCALL(LoadLibraryExW, 12)
    19 SUPHARNT_IMPORT_STDCALL(VirtualProtectEx, 20)
    20 SUPHARNT_IMPORT_STDCALL(GetProcAddress, 8)
    21 
  • trunk/src/VBox/HostDrivers/Support/win/import-template-ntdll.h

    r52356 r52365  
    99SUPHARNT_IMPORT_SYSCALL(NtMapViewOfSection, 40)
    1010SUPHARNT_IMPORT_SYSCALL(NtOpenDirectoryObject, 12)
     11SUPHARNT_IMPORT_SYSCALL(NtOpenKey, 12)
    1112SUPHARNT_IMPORT_SYSCALL(NtOpenProcess, 16)
    1213SUPHARNT_IMPORT_SYSCALL(NtOpenProcessToken, 12)
     
    2122SUPHARNT_IMPORT_SYSCALL(NtQueryInformationToken, 20)
    2223SUPHARNT_IMPORT_SYSCALL(NtQueryObject, 20)
     24SUPHARNT_IMPORT_SYSCALL(NtQuerySecurityObject, 20)
    2325SUPHARNT_IMPORT_SYSCALL(NtQueryTimerResolution, 12)
     26SUPHARNT_IMPORT_SYSCALL(NtQueryValueKey, 24)
    2427SUPHARNT_IMPORT_SYSCALL(NtQueryVirtualMemory, 24)
    2528SUPHARNT_IMPORT_SYSCALL(NtReadFile, 36)
     
    5760SUPHARNT_IMPORT_STDCALL(RtlCreateUserThread, 40)
    5861SUPHARNT_IMPORT_STDCALL(RtlDestroyProcessParameters, 4)
     62SUPHARNT_IMPORT_STDCALL(RtlEqualSid, 8)
     63SUPHARNT_IMPORT_STDCALL(RtlExpandEnvironmentStrings_U, 16)
    5964SUPHARNT_IMPORT_STDCALL(RtlGetVersion, 4)
    6065SUPHARNT_IMPORT_STDCALL(RtlInitializeSid, 12)
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