VirtualBox

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


Ignore:
Timestamp:
Jul 1, 2015 2:01:43 PM (10 years ago)
Author:
vboxsync
Message:

SUPHardNt: Allow hardened exectuable binaries in the 'testcase' subdirectory.

Location:
trunk/src/VBox/HostDrivers/Support
Files:
7 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/HostDrivers/Support/SUPLibInternal.h

    r56293 r56733  
    7979# define supR3HardenedPathAppPrivateNoArch supR3HardenedStaticPathAppPrivateNoArch
    8080# define supR3HardenedPathAppPrivateArch   supR3HardenedStaticPathAppPrivateArch
    81 # define supR3HardenedPathSharedLibs       supR3HardenedStaticPathSharedLibs
     81# define supR3HardenedPathAppSharedLibs    supR3HardenedStaticPathAppSharedLibs
    8282# define supR3HardenedPathAppDocs          supR3HardenedStaticPathAppDocs
    83 # define supR3HardenedPathExecDir          supR3HardenedStaticPathExecDir
     83# define supR3HardenedPathAppBin           supR3HardenedStaticPathAppBin
    8484# define supR3HardenedPathFilename         supR3HardenedStaticPathFilename
    8585# define supR3HardenedFatalV               supR3HardenedStaticFatalV
     
    176176{
    177177    kSupID_Invalid = 0,
    178     kSupID_Bin,
    179178    kSupID_AppBin,
    180     kSupID_SharedLib,
     179    kSupID_AppSharedLib,
    181180    kSupID_AppPrivArch,
    182181    kSupID_AppPrivArchComp,
     
    382381DECLHIDDEN(int)    supR3HardenedPathAppPrivateArch(char *pszPath, size_t cchPath);
    383382/** @copydoc RTPathSharedLibs */
    384 DECLHIDDEN(int)    supR3HardenedPathSharedLibs(char *pszPath, size_t cchPath);
     383DECLHIDDEN(int)    supR3HardenedPathAppSharedLibs(char *pszPath, size_t cchPath);
    385384/** @copydoc RTPathAppDocs */
    386385DECLHIDDEN(int)    supR3HardenedPathAppDocs(char *pszPath, size_t cchPath);
    387386/** @copydoc RTPathExecDir */
    388 DECLHIDDEN(int)    supR3HardenedPathExecDir(char *pszPath, size_t cchPath);
     387DECLHIDDEN(int)    supR3HardenedPathAppBin(char *pszPath, size_t cchPath);
    389388/** @copydoc RTPathFilename */
    390389DECLHIDDEN(char *) supR3HardenedPathFilename(const char *pszPath);
     
    441440
    442441
    443 DECLHIDDEN(int)     supR3HardenedVerifyAll(bool fFatal, const char *pszProgName);
     442DECLHIDDEN(int)     supR3HardenedVerifyAll(bool fFatal, const char *pszProgName, const char *pszExePath);
    444443DECLHIDDEN(int)     supR3HardenedVerifyFixedDir(SUPINSTDIR enmDir, bool fFatal);
    445444DECLHIDDEN(int)     supR3HardenedVerifyFixedFile(const char *pszFilename, bool fFatal);
     
    452451#ifdef RT_OS_WINDOWS
    453452DECLHIDDEN(void)    supR3HardenedWinInit(uint32_t fFlags, bool fAvastKludge);
     453DECLHIDDEN(void)    supR3HardenedWinInitAppBin(uint32_t fFlags);
    454454DECLHIDDEN(void)    supR3HardenedWinInitVersion(void);
    455455DECLHIDDEN(void)    supR3HardenedWinInitImports(void);
     456DECLHIDDEN(void)    supR3HardenedWinModifyDllSearchPath(uint32_t fFlags, const char *pszAppBinPath);
    456457# ifdef ___iprt_nt_nt_h___
    457458DECLHIDDEN(void)    supR3HardenedWinGetVeryEarlyImports(uintptr_t uNtDllAddr,
  • trunk/src/VBox/HostDrivers/Support/SUPR3HardenedIPRT.cpp

    r56293 r56733  
    7171 * @copydoc RTPathSharedLibs
    7272 */
    73 DECLHIDDEN(int) supR3HardenedPathSharedLibs(char *pszPath, size_t cchPath)
     73DECLHIDDEN(int) supR3HardenedPathAppSharedLibs(char *pszPath, size_t cchPath)
    7474{
    7575    return RTPathSharedLibs(pszPath, cchPath);
     
    8989 * @copydoc RTPathExecDir
    9090 */
    91 DECLHIDDEN(int) supR3HardenedPathExecDir(char *pszPath, size_t cchPath)
     91DECLHIDDEN(int) supR3HardenedPathAppBin(char *pszPath, size_t cchPath)
    9292{
    9393    return RTPathExecDir(pszPath, cchPath);
  • trunk/src/VBox/HostDrivers/Support/SUPR3HardenedMain.cpp

    r56293 r56733  
    134134#endif
    135135char                    g_szSupLibHardenedExePath[RTPATH_MAX];
    136 /** The program directory path. */
    137 static char             g_szSupLibHardenedDirPath[RTPATH_MAX];
     136/** The application bin directory path. */
     137static char             g_szSupLibHardenedAppBinPath[RTPATH_MAX];
    138138
    139139/** The program name. */
     
    781781
    782782#else
    783     return supR3HardenedPathExecDir(pszPath, cchPath);
     783    return supR3HardenedPathAppBin(pszPath, cchPath);
    784784#endif
    785785}
     
    800800
    801801#else
    802     return supR3HardenedPathExecDir(pszPath, cchPath);
     802    return supR3HardenedPathAppBin(pszPath, cchPath);
    803803#endif
    804804}
     
    808808 * @copydoc RTPathSharedLibs
    809809 */
    810 DECLHIDDEN(int) supR3HardenedPathSharedLibs(char *pszPath, size_t cchPath)
     810DECLHIDDEN(int) supR3HardenedPathAppSharedLibs(char *pszPath, size_t cchPath)
    811811{
    812812#if !defined(RT_OS_WINDOWS) && defined(RTPATH_SHARED_LIBS)
     
    814814    size_t cchPathSharedLibs = suplibHardenedStrLen(pszSrcPath);
    815815    if (cchPathSharedLibs >= cchPath)
    816         supR3HardenedFatal("supR3HardenedPathSharedLibs: Buffer overflow, %zu >= %zu\n", cchPathSharedLibs, cchPath);
     816        supR3HardenedFatal("supR3HardenedPathAppSharedLibs: Buffer overflow, %zu >= %zu\n", cchPathSharedLibs, cchPath);
    817817    suplibHardenedMemCopy(pszPath, pszSrcPath, cchPathSharedLibs + 1);
    818818    return VINF_SUCCESS;
    819819
    820820#else
    821     return supR3HardenedPathExecDir(pszPath, cchPath);
     821    return supR3HardenedPathAppBin(pszPath, cchPath);
    822822#endif
    823823}
     
    838838
    839839#else
    840     return supR3HardenedPathExecDir(pszPath, cchPath);
     840    return supR3HardenedPathAppBin(pszPath, cchPath);
    841841#endif
    842842}
     
    911911
    912912    /*
    913      * Strip off the filename part (RTPathStripFilename()).
    914      */
    915     suplibHardenedStrCopy(g_szSupLibHardenedDirPath, g_szSupLibHardenedExePath);
    916     suplibHardenedPathStripFilename(g_szSupLibHardenedDirPath);
     913     * Determine the application binary directory location.
     914     */
     915    suplibHardenedStrCopy(g_szSupLibHardenedAppBinPath, g_szSupLibHardenedExePath);
     916    suplibHardenedPathStripFilename(g_szSupLibHardenedAppBinPath);
     917
     918    if (g_enmSupR3HardenedMainState < SUPR3HARDENEDMAINSTATE_HARDENED_MAIN_CALLED)
     919        supR3HardenedFatal("supR3HardenedExecDir: Called before SUPR3HardenedMain! (%d)\n", g_enmSupR3HardenedMainState);
     920    switch (g_fSupHardenedMain & SUPSECMAIN_FLAGS_LOC_MASK)
     921    {
     922        case SUPSECMAIN_FLAGS_LOC_APP_BIN:
     923            break;
     924        case SUPSECMAIN_FLAGS_LOC_TESTCASE:
     925            suplibHardenedPathStripFilename(g_szSupLibHardenedAppBinPath);
     926            break;
     927        default:
     928            supR3HardenedFatal("supR3HardenedExecDir: Unknown program binary location: %#x\n", g_fSupHardenedMain);
     929    }
    917930}
    918931
     
    939952/**
    940953 * @copydoc RTPathExecDir
    941  */
    942 DECLHIDDEN(int) supR3HardenedPathExecDir(char *pszPath, size_t cchPath)
     954 * @remarks not quite like RTPathExecDir actually...
     955 */
     956DECLHIDDEN(int) supR3HardenedPathAppBin(char *pszPath, size_t cchPath)
    943957{
    944958    /*
    945959     * Lazy init (probably not required).
    946960     */
    947     if (!g_szSupLibHardenedDirPath[0])
     961    if (!g_szSupLibHardenedAppBinPath[0])
    948962        supR3HardenedGetFullExePath();
    949963
     
    951965     * Calc the length and check if there is space before copying.
    952966     */
    953     size_t cch = suplibHardenedStrLen(g_szSupLibHardenedDirPath) + 1;
     967    size_t cch = suplibHardenedStrLen(g_szSupLibHardenedAppBinPath) + 1;
    954968    if (cch <= cchPath)
    955969    {
    956         suplibHardenedMemCopy(pszPath, g_szSupLibHardenedDirPath, cch + 1);
     970        suplibHardenedMemCopy(pszPath, g_szSupLibHardenedAppBinPath, cch + 1);
    957971        return VINF_SUCCESS;
    958972    }
    959973
    960     supR3HardenedFatal("supR3HardenedPathExecDir: Buffer too small (%u < %u)\n", cchPath, cch);
     974    supR3HardenedFatal("supR3HardenedPathAppBin: Buffer too small (%u < %u)\n", cchPath, cch);
    961975    return VERR_BUFFER_OVERFLOW;
    962976}
     
    16281642     */
    16291643    char szPath[RTPATH_MAX];
    1630     supR3HardenedPathSharedLibs(szPath, sizeof(szPath) - sizeof("/VBoxRT" SUPLIB_DLL_SUFF));
     1644    supR3HardenedPathAppSharedLibs(szPath, sizeof(szPath) - sizeof("/VBoxRT" SUPLIB_DLL_SUFF));
    16311645    suplibHardenedStrCat(szPath, "/VBoxRT" SUPLIB_DLL_SUFF);
    16321646
     
    17741788    char szPath[RTPATH_MAX];
    17751789    supR3HardenedPathAppPrivateArch(szPath, sizeof(szPath) - 10);
     1790    const char *pszSubDirSlash;
     1791    switch (g_fSupHardenedMain & SUPSECMAIN_FLAGS_LOC_MASK)
     1792    {
     1793        case SUPSECMAIN_FLAGS_LOC_APP_BIN:
     1794            pszSubDirSlash = "/";
     1795            break;
     1796        case SUPSECMAIN_FLAGS_LOC_TESTCASE:
     1797            pszSubDirSlash = "/testcase/";
     1798            break;
     1799        default:
     1800            pszSubDirSlash = "/";
     1801            supR3HardenedFatal("supR3HardenedMainGetTrustedMain: Unknown program binary location: %#x\n", g_fSupHardenedMain);
     1802    }
    17761803    size_t cch = suplibHardenedStrLen(szPath);
    1777     suplibHardenedStrCopyEx(&szPath[cch], sizeof(szPath) - cch, "/", pszProgName, SUPLIB_DLL_SUFF, NULL);
     1804    suplibHardenedStrCopyEx(&szPath[cch], sizeof(szPath) - cch, pszSubDirSlash, pszProgName, SUPLIB_DLL_SUFF, NULL);
    17781805
    17791806    /*
     
    18441871        g_SupPreInitData.Data.hDevice = SUP_HDEVICE_NIL;
    18451872
     1873    /*
     1874     * Determine the full exe path as we'll be needing it for the verify all
     1875     * call(s) below.  (We have to do this early on Linux because we * *might*
     1876     * not be able to access /proc/self/exe after the seteuid call.)
     1877     */
     1878    supR3HardenedGetFullExePath();
     1879#ifdef RT_OS_WINDOWS
     1880    supR3HardenedWinInitAppBin(fFlags);
     1881#endif
     1882
    18461883#ifdef SUP_HARDENED_SUID
    1847 # ifdef RT_OS_LINUX
    1848     /*
    1849      * On linux we have to make sure the path is initialized because we
    1850      * *might* not be able to access /proc/self/exe after the seteuid call.
    1851      */
    1852     supR3HardenedGetFullExePath();
    1853 # endif
    1854 
    18551884    /*
    18561885     * Grab any options from the environment.
     
    18811910        SUP_DPRINTF(("SUPR3HardenedMain: Respawn #1\n"));
    18821911        supR3HardenedWinInit(SUPSECMAIN_FLAGS_DONT_OPEN_DEV, false /*fAvastKludge*/);
    1883         supR3HardenedVerifyAll(true /* fFatal */, pszProgName);
     1912        supR3HardenedVerifyAll(true /* fFatal */, pszProgName, g_szSupLibHardenedExePath);
    18841913        return supR3HardenedWinReSpawn(1 /*iWhich*/);
    18851914    }
     
    18981927     * Validate the installation.
    18991928     */
    1900     supR3HardenedVerifyAll(true /* fFatal */, pszProgName);
     1929    supR3HardenedVerifyAll(true /* fFatal */, pszProgName, g_szSupLibHardenedExePath);
    19011930
    19021931    /*
     
    19631992    g_enmSupR3HardenedMainState = SUPR3HARDENEDMAINSTATE_INIT_RUNTIME;
    19641993    supR3HardenedMainInitRuntime(fFlags);
     1994#ifdef RT_OS_WINDOWS
     1995    supR3HardenedWinModifyDllSearchPath(fFlags, g_szSupLibHardenedAppBinPath);
     1996#endif
    19651997
    19661998    /*
  • trunk/src/VBox/HostDrivers/Support/SUPR3HardenedMainTemplateTestcase.cpp

    r56721 r56733  
    11/* $Id$ */
    22/** @file
    3  * Hardened main() template.
     3 * Hardened main() template for testcases (in testcase subdir).
    44 */
    55
     
    3030int main(int argc, char **argv, char **envp)
    3131{
    32     return SUPR3HardenedMain(PROGRAM_NAME_STR, 0, argc, argv, envp);
     32    return SUPR3HardenedMain(PROGRAM_NAME_STR, SUPSECMAIN_FLAGS_LOC_TESTCASE, argc, argv, envp);
    3333}
    3434
  • trunk/src/VBox/HostDrivers/Support/SUPR3HardenedVerify.cpp

    r56293 r56733  
    105105 *
    106106 * @todo This needs reviewing against the linux packages.
    107  * @todo The excessive use of kSupID_SharedLib needs to be reviewed at some point. For
     107 * @todo The excessive use of kSupID_AppSharedLib needs to be reviewed at some point. For
    108108 *       the time being we're building the linux packages with SharedLib pointing to
    109109 *       AppPrivArch (lazy bird).
     
    123123#endif
    124124
    125     {   kSupIFT_Dll,  kSupID_SharedLib,         false, "VBoxRT" SUPLIB_DLL_SUFF },
    126     {   kSupIFT_Dll,  kSupID_SharedLib,         false, "VBoxVMM" SUPLIB_DLL_SUFF },
    127     {   kSupIFT_Dll,  kSupID_SharedLib,         false, "VBoxREM" SUPLIB_DLL_SUFF },
     125    {   kSupIFT_Dll,  kSupID_AppSharedLib,      false, "VBoxRT" SUPLIB_DLL_SUFF },
     126    {   kSupIFT_Dll,  kSupID_AppSharedLib,      false, "VBoxVMM" SUPLIB_DLL_SUFF },
     127    {   kSupIFT_Dll,  kSupID_AppSharedLib,      false, "VBoxREM" SUPLIB_DLL_SUFF },
    128128#if HC_ARCH_BITS == 32
    129     {   kSupIFT_Dll,  kSupID_SharedLib,          true, "VBoxREM32" SUPLIB_DLL_SUFF },
    130     {   kSupIFT_Dll,  kSupID_SharedLib,          true, "VBoxREM64" SUPLIB_DLL_SUFF },
    131 #endif
    132     {   kSupIFT_Dll,  kSupID_SharedLib,         false, "VBoxDD" SUPLIB_DLL_SUFF },
    133     {   kSupIFT_Dll,  kSupID_SharedLib,         false, "VBoxDD2" SUPLIB_DLL_SUFF },
    134     {   kSupIFT_Dll,  kSupID_SharedLib,         false, "VBoxDDU" SUPLIB_DLL_SUFF },
     129    {   kSupIFT_Dll,  kSupID_AppSharedLib,       true, "VBoxREM32" SUPLIB_DLL_SUFF },
     130    {   kSupIFT_Dll,  kSupID_AppSharedLib,       true, "VBoxREM64" SUPLIB_DLL_SUFF },
     131#endif
     132    {   kSupIFT_Dll,  kSupID_AppSharedLib,      false, "VBoxDD" SUPLIB_DLL_SUFF },
     133    {   kSupIFT_Dll,  kSupID_AppSharedLib,      false, "VBoxDD2" SUPLIB_DLL_SUFF },
     134    {   kSupIFT_Dll,  kSupID_AppSharedLib,      false, "VBoxDDU" SUPLIB_DLL_SUFF },
    135135
    136136//#ifdef VBOX_WITH_DEBUGGER_GUI
    137     {   kSupIFT_Dll,  kSupID_SharedLib,          true, "VBoxDbg" SUPLIB_DLL_SUFF },
    138     {   kSupIFT_Dll,  kSupID_SharedLib,          true, "VBoxDbg3" SUPLIB_DLL_SUFF },
     137    {   kSupIFT_Dll,  kSupID_AppSharedLib,       true, "VBoxDbg" SUPLIB_DLL_SUFF },
     138    {   kSupIFT_Dll,  kSupID_AppSharedLib,       true, "VBoxDbg3" SUPLIB_DLL_SUFF },
    139139//#endif
    140140
     
    165165    {   kSupIFT_Exe,  kSupID_AppBin,            false, "VBoxSVC" SUPLIB_EXE_SUFF },
    166166 #ifdef RT_OS_WINDOWS
    167     {   kSupIFT_Dll,  kSupID_SharedLib,         false, "VBoxC" SUPLIB_DLL_SUFF },
     167    {   kSupIFT_Dll,  kSupID_AppSharedLib,      false, "VBoxC" SUPLIB_DLL_SUFF },
    168168 #else
    169169    {   kSupIFT_Exe,  kSupID_AppPrivArch,       false, "VBoxXPCOMIPCD" SUPLIB_EXE_SUFF },
    170     {   kSupIFT_Dll,  kSupID_SharedLib,         false, "VBoxXPCOM" SUPLIB_DLL_SUFF },
     170    {   kSupIFT_Dll,  kSupID_AppSharedLib,      false, "VBoxXPCOM" SUPLIB_DLL_SUFF },
    171171    {   kSupIFT_Dll,  kSupID_AppPrivArchComp,   false, "VBoxXPCOMIPCC" SUPLIB_DLL_SUFF },
    172172    {   kSupIFT_Dll,  kSupID_AppPrivArchComp,   false, "VBoxC" SUPLIB_DLL_SUFF },
     
    176176#endif
    177177
    178     {   kSupIFT_Dll,  kSupID_SharedLib,          true, "VRDPAuth" SUPLIB_DLL_SUFF },
    179     {   kSupIFT_Dll,  kSupID_SharedLib,          true, "VBoxAuth" SUPLIB_DLL_SUFF },
    180     {   kSupIFT_Dll,  kSupID_SharedLib,          true, "VBoxVRDP" SUPLIB_DLL_SUFF },
     178    {   kSupIFT_Dll,  kSupID_AppSharedLib,       true, "VRDPAuth" SUPLIB_DLL_SUFF },
     179    {   kSupIFT_Dll,  kSupID_AppSharedLib,       true, "VBoxAuth" SUPLIB_DLL_SUFF },
     180    {   kSupIFT_Dll,  kSupID_AppSharedLib,       true, "VBoxVRDP" SUPLIB_DLL_SUFF },
    181181
    182182//#ifdef VBOX_WITH_HEADLESS
     
    190190    {   kSupIFT_Dll,  kSupID_AppPrivArch,        true, "VirtualBox" SUPLIB_DLL_SUFF },
    191191# if !defined(RT_OS_DARWIN) && !defined(RT_OS_WINDOWS) && !defined(RT_OS_OS2)
    192     {   kSupIFT_Dll,  kSupID_SharedLib,          true, "VBoxKeyboard" SUPLIB_DLL_SUFF },
     192    {   kSupIFT_Dll,  kSupID_AppSharedLib,       true, "VBoxKeyboard" SUPLIB_DLL_SUFF },
    193193# endif
    194194//#endif
     
    263263    switch (enmDir)
    264264    {
    265         case kSupID_AppBin: /** @todo fix this AppBin crap (uncertain wtf some binaries actually are installed). */
    266         case kSupID_Bin:
    267             rc = supR3HardenedPathExecDir(pszDst, cchDst);
     265        case kSupID_AppBin:
     266            rc = supR3HardenedPathAppBin(pszDst, cchDst);
    268267            break;
    269         case kSupID_SharedLib:
    270             rc = supR3HardenedPathSharedLibs(pszDst, cchDst);
     268        case kSupID_AppSharedLib:
     269            rc = supR3HardenedPathAppSharedLibs(pszDst, cchDst);
    271270            break;
    272271        case kSupID_AppPrivArch:
     
    288287            break;
    289288        case kSupID_Testcase:
    290             rc = supR3HardenedPathExecDir(pszDst, cchDst);
     289            rc = supR3HardenedPathAppBin(pszDst, cchDst);
    291290            if (RT_SUCCESS(rc))
    292291            {
     
    850849 * @returns See supR3HardenedVerifyAll.
    851850 * @param   pszProgName         See supR3HardenedVerifyAll.
     851 * @param   pszExePath          The path to the executable.
    852852 * @param   fFatal              See supR3HardenedVerifyAll.
    853853 * @param   fLeaveOpen          The leave open setting used by
    854854 *                              supR3HardenedVerifyAll.
    855855 */
    856 static int supR3HardenedVerifyProgram(const char *pszProgName, bool fFatal, bool fLeaveOpen)
     856static int supR3HardenedVerifyProgram(const char *pszProgName, const char *pszExePath, bool fFatal, bool fLeaveOpen)
    857857{
    858858    /*
     
    891891                fExe = true;
    892892
    893                 char szFilename[RTPATH_MAX];
    894                 int rc2 = supR3HardenedPathExecDir(szFilename, sizeof(szFilename) - cchProgName - sizeof(SUPLIB_EXE_SUFF));
    895                 if (RT_SUCCESS(rc2))
    896                 {
    897                     suplibHardenedStrCat(szFilename, "/");
    898                     suplibHardenedStrCat(szFilename, g_aSupInstallFiles[iFile].pszFile);
    899                     supR3HardenedVerifySameFile(iFile, szFilename, fFatal);
    900                 }
    901                 else
    902                     rc = supR3HardenedError(rc2, fFatal,
    903                                             "supR3HardenedVerifyProgram: failed to query program path: rc=%d\n", rc2);
     893                supR3HardenedVerifySameFile(iFile, pszExePath, fFatal);
    904894            }
    905895        }
     
    936926 *                              both the executable and corresponding
    937927 *                              DLL/DYLIB/SO are valid.
    938  */
    939 DECLHIDDEN(int) supR3HardenedVerifyAll(bool fFatal, const char *pszProgName)
     928 * @param   pszExePath          The path to the executable.
     929 */
     930DECLHIDDEN(int) supR3HardenedVerifyAll(bool fFatal, const char *pszProgName, const char *pszExePath)
    940931{
    941932    /*
     
    964955     * sign things.
    965956     */
    966     int rc2 = supR3HardenedVerifyProgram(pszProgName, fFatal, fLeaveOpen);
     957    int rc2 = supR3HardenedVerifyProgram(pszProgName, pszExePath, fFatal, fLeaveOpen);
    967958    if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
    968959        rc2 = rc;
  • trunk/src/VBox/HostDrivers/Support/win/SUPHardenedVerify-win.h

    r56293 r56733  
    205205#endif /* IN_RING3 && !VBOX_PERMIT_EVEN_MORE */
    206206extern SUPSYSROOTDIRBUF g_SupLibHardenedExeNtPath;
    207 extern uint32_t         g_offSupLibHardenedExeNtName;
     207extern SUPSYSROOTDIRBUF g_SupLibHardenedAppBinNtPath;
    208208
    209209#   ifdef IN_RING0
  • trunk/src/VBox/HostDrivers/Support/win/SUPHardenedVerifyImage-win.cpp

    r56293 r56733  
    20392039     */
    20402040    char szPath[RTPATH_MAX];
    2041     supR3HardenedPathSharedLibs(szPath, sizeof(szPath) - sizeof("/VBoxSupLib.DLL"));
     2041    supR3HardenedPathAppSharedLibs(szPath, sizeof(szPath) - sizeof("/VBoxSupLib.DLL"));
    20422042    suplibHardenedStrCat(szPath, "/VBoxSupLib.DLL");
    20432043    HMODULE hSupLibMod = (HMODULE)supR3HardenedWinLoadLibrary(szPath, true /*fSystem32Only*/);
  • trunk/src/VBox/HostDrivers/Support/win/SUPR3HardenedMain-win.cpp

    r56293 r56733  
    3535#endif
    3636#ifndef LOAD_LIBRARY_SEARCH_APPLICATION_DIR
    37 # define LOAD_LIBRARY_SEARCH_APPLICATION_DIR    0x200
    38 # define LOAD_LIBRARY_SEARCH_SYSTEM32           0x800
     37# define LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR       UINT32_C(0x100)
     38# define LOAD_LIBRARY_SEARCH_APPLICATION_DIR    UINT32_C(0x200)
     39# define LOAD_LIBRARY_SEARCH_USER_DIRS          UINT32_C(0x400)
     40# define LOAD_LIBRARY_SEARCH_SYSTEM32           UINT32_C(0x800)
    3941#endif
    4042
     
    274276/** The NT path of the executable. */
    275277SUPSYSROOTDIRBUF            g_SupLibHardenedExeNtPath;
     278/** The NT path of the application binary directory. */
     279SUPSYSROOTDIRBUF            g_SupLibHardenedAppBinNtPath;
    276280/** The offset into g_SupLibHardenedExeNtPath of the executable name (WCHAR,
    277281 * not byte). This also gives the length of the exectuable directory path,
    278282 * including a trailing slash. */
    279 uint32_t                    g_offSupLibHardenedExeNtName;
     283static uint32_t             g_offSupLibHardenedExeNtName;
     284/** Set if we need to use the LOAD_LIBRARY_SEARCH_USER_DIRS option. */
     285bool                        g_fSupLibHardenedDllSearchUserDirs = false;
    280286/** @} */
    281287
     
    464470        if (g_uNtVerCombined >= SUP_MAKE_NT_VER_SIMPLE(6, 0))
    465471        {
    466            fFlags |= LOAD_LIBRARY_SEARCH_SYSTEM32;
    467            if (!fSystem32Only)
    468                fFlags |= LOAD_LIBRARY_SEARCH_APPLICATION_DIR;
     472            fFlags |= LOAD_LIBRARY_SEARCH_SYSTEM32;
     473            if (!fSystem32Only)
     474            {
     475                fFlags |= LOAD_LIBRARY_SEARCH_APPLICATION_DIR;
     476                if (g_fSupLibHardenedDllSearchUserDirs)
     477                    fFlags |= LOAD_LIBRARY_SEARCH_USER_DIRS;
     478            }
    469479        }
    470480
     
    806816                        continue;
    807817                    }
    808                     if (supR3HardenedWinVerifyCacheLookupImport(g_SupLibHardenedExeNtPath.UniStr.Buffer,
    809                                                                 g_offSupLibHardenedExeNtName,
     818                    if (supR3HardenedWinVerifyCacheLookupImport(g_SupLibHardenedAppBinNtPath.UniStr.Buffer,
     819                                                                g_SupLibHardenedAppBinNtPath.UniStr.Length / sizeof(CHAR),
    810820                                                                uBuf.szName) != NULL)
    811821                    {
     
    889899                                                            g_System32NtPath.UniStr.Length / sizeof(WCHAR),
    890900                                                            pCur->szName)
    891                 && !supR3HardenedWinVerifyCacheLookupImport(g_SupLibHardenedExeNtPath.UniStr.Buffer,
    892                                                             g_offSupLibHardenedExeNtName,
     901                && !supR3HardenedWinVerifyCacheLookupImport(g_SupLibHardenedAppBinNtPath.UniStr.Buffer,
     902                                                            g_SupLibHardenedAppBinNtPath.UniStr.Length / sizeof(WCHAR),
    893903                                                            pCur->szName)
    894904                && (   pCur->cwcAltSearchDir == 0
     
    983993                    {
    984994                        { g_System32NtPath.UniStr.Buffer,           g_System32NtPath.UniStr.Length / sizeof(WCHAR) },
    985                         { g_SupLibHardenedExeNtPath.UniStr.Buffer,  g_offSupLibHardenedExeNtName - 1 },
     995                        { g_SupLibHardenedExeNtPath.UniStr.Buffer,  g_SupLibHardenedAppBinNtPath.UniStr.Length / sizeof(WCHAR) },
    986996                        { pCur->pwszAltSearchDir,                   pCur->cwcAltSearchDir },
    987997                    };
     
    13521362     *      7. x86 variations of 4 & 5 - ditto.
    13531363     */
    1354     Assert(g_SupLibHardenedExeNtPath.UniStr.Buffer[g_offSupLibHardenedExeNtName - 1] == '\\');
    13551364    uint32_t fFlags = 0;
    13561365    if (supHardViUniStrPathStartsWithUniStr(&uBuf.UniStr, &g_System32NtPath.UniStr, true /*fCheckSlash*/))
     
    13581367    else if (supHardViUniStrPathStartsWithUniStr(&uBuf.UniStr, &g_WinSxSNtPath.UniStr, true /*fCheckSlash*/))
    13591368        fFlags |= SUPHNTVI_F_ALLOW_CAT_FILE_VERIFICATION | SUPHNTVI_F_TRUSTED_INSTALLER_OWNER;
    1360     else if (supHardViUtf16PathStartsWithEx(uBuf.UniStr.Buffer, uBuf.UniStr.Length / sizeof(WCHAR),
    1361                                             g_SupLibHardenedExeNtPath.UniStr.Buffer,
    1362                                             g_offSupLibHardenedExeNtName, false /*fCheckSlash*/))
     1369    else if (supHardViUniStrPathStartsWithUniStr(&uBuf.UniStr, &g_SupLibHardenedAppBinNtPath.UniStr, true /*fCheckSlash*/))
    13631370        fFlags |= SUPHNTVI_F_REQUIRE_KERNEL_CODE_SIGNING | SUPHNTVI_F_REQUIRE_SIGNATURE_ENFORCEMENT;
    13641371# ifdef VBOX_PERMIT_MORE
     
    14041411     * integrity checks.
    14051412     */
    1406     Assert(g_SupLibHardenedExeNtPath.UniStr.Buffer[g_offSupLibHardenedExeNtName - 1] == '\\');
    14071413    uint32_t fFlags = 0;
    1408     if (supHardViUtf16PathStartsWithEx(uBuf.UniStr.Buffer, uBuf.UniStr.Length / sizeof(WCHAR),
    1409                                        g_SupLibHardenedExeNtPath.UniStr.Buffer,
    1410                                        g_offSupLibHardenedExeNtName, false /*fCheckSlash*/))
     1414    if (supHardViUniStrPathStartsWithUniStr(&uBuf.UniStr, &g_SupLibHardenedAppBinNtPath.UniStr, true /*fCheckSlash*/))
    14111415        fFlags |= SUPHNTVI_F_REQUIRE_KERNEL_CODE_SIGNING | SUPHNTVI_F_REQUIRE_SIGNATURE_ENFORCEMENT;
    14121416    else
     
    46494653                              "Window Vista without any service pack installed is not supported. Please install the latest service pack.");
    46504654#endif
     4655}
     4656
     4657
     4658/**
     4659 * Modifies the DLL search path for testcases.
     4660 *
     4661 * This makes sure the application binary path is in the search path.  When
     4662 * starting a testcase executable in the testcase/ subdirectory this isn't the
     4663 * case by default.  So, unless we do something about it we won't be able to
     4664 * import VBox DLLs.
     4665 *
     4666 * @param   fFlags          The main flags (giving the location).
     4667 * @param   pszAppBinPath   The path to the application binary directory
     4668 *                          (windows style).
     4669 */
     4670DECLHIDDEN(void) supR3HardenedWinModifyDllSearchPath(uint32_t fFlags, const char *pszAppBinPath)
     4671{
     4672    /*
     4673     * For the testcases to work, we must add the app bin directory to the
     4674     * DLL search list before the testcase dll is loaded or it won't be
     4675     * able to find the VBox DLLs.  This is done _after_ VBoxRT.dll is
     4676     * initialized and sets its defaults.
     4677     */
     4678    switch (fFlags & SUPSECMAIN_FLAGS_LOC_MASK)
     4679    {
     4680        case SUPSECMAIN_FLAGS_LOC_TESTCASE:
     4681            break;
     4682        default:
     4683            return;
     4684    }
     4685
     4686    /*
     4687     * Dynamically resolve the two APIs we need (the latter uses forwarders on w7).
     4688     */
     4689    HMODULE hModKernel32 = GetModuleHandleW(L"kernel32.dll");
     4690
     4691    typedef BOOL (WINAPI *PFNSETDLLDIRECTORY)(LPCWSTR);
     4692    PFNSETDLLDIRECTORY pfnSetDllDir;
     4693    pfnSetDllDir     = (PFNSETDLLDIRECTORY)GetProcAddress(hModKernel32, "SetDllDirectoryW");
     4694
     4695    typedef BOOL (WINAPI *PFNSETDEFAULTDLLDIRECTORIES)(DWORD);
     4696    PFNSETDEFAULTDLLDIRECTORIES pfnSetDefDllDirs;
     4697    pfnSetDefDllDirs = (PFNSETDEFAULTDLLDIRECTORIES)GetProcAddress(hModKernel32, "SetDefaultDllDirectories");
     4698
     4699    if (pfnSetDllDir != NULL)
     4700    {
     4701        /*
     4702         * Convert the path to UTF-16 and try set it.
     4703         */
     4704        PRTUTF16 pwszAppBinPath = NULL;
     4705        int rc = RTStrToUtf16(pszAppBinPath, &pwszAppBinPath);
     4706        if (RT_SUCCESS(rc))
     4707        {
     4708            if (pfnSetDllDir(pwszAppBinPath))
     4709            {
     4710                SUP_DPRINTF(("supR3HardenedWinModifyDllSearchPath: Set dll dir to '%ls'\n", pwszAppBinPath));
     4711                g_fSupLibHardenedDllSearchUserDirs = true;
     4712
     4713                /*
     4714                 * We set it alright, on W7 and later we also must modify the
     4715                 * default DLL search order.  See @bugref{6861} for details on
     4716                 * why we don't do this on Vista (also see init-win.cpp in IPRT).
     4717                 */
     4718                if (   pfnSetDefDllDirs
     4719                    && g_uNtVerCombined >= SUP_NT_VER_W70)
     4720                {
     4721                    if (pfnSetDefDllDirs(  LOAD_LIBRARY_SEARCH_APPLICATION_DIR
     4722                                         | LOAD_LIBRARY_SEARCH_SYSTEM32
     4723                                         | LOAD_LIBRARY_SEARCH_USER_DIRS))
     4724                        SUP_DPRINTF(("supR3HardenedWinModifyDllSearchPath: Successfully modified search dirs.\n"));
     4725                    else
     4726                        supR3HardenedFatal("supR3HardenedWinModifyDllSearchPath: SetDllDirectoryW(%ls) failed: %d\n",
     4727                                           pwszAppBinPath, RtlGetLastWin32Error());
     4728                }
     4729            }
     4730            else
     4731                supR3HardenedFatal("supR3HardenedWinModifyDllSearchPath: SetDllDirectoryW(%ls) failed: %d\n",
     4732                                   pwszAppBinPath, RtlGetLastWin32Error());
     4733            RTUtf16Free(pwszAppBinPath);
     4734        }
     4735        else
     4736            supR3HardenedFatal("supR3HardenedWinModifyDllSearchPath: RTStrToUtf16(%s) failed: %d\n", pszAppBinPath, rc);
     4737    }
     4738}
     4739
     4740
     4741/**
     4742 * Initializes the application binary directory path.
     4743 *
     4744 * This is called once or twice.
     4745 *
     4746 * @param   fFlags          The main flags (giving the location).
     4747 */
     4748DECLHIDDEN(void) supR3HardenedWinInitAppBin(uint32_t fFlags)
     4749{
     4750    USHORT cwc = (USHORT)g_offSupLibHardenedExeNtName - 1;
     4751    g_SupLibHardenedAppBinNtPath.UniStr.Buffer = g_SupLibHardenedAppBinNtPath.awcBuffer;
     4752    memcpy(g_SupLibHardenedAppBinNtPath.UniStr.Buffer, g_SupLibHardenedExeNtPath.UniStr.Buffer, cwc * sizeof(WCHAR));
     4753
     4754    switch (fFlags & SUPSECMAIN_FLAGS_LOC_MASK)
     4755    {
     4756        case SUPSECMAIN_FLAGS_LOC_APP_BIN:
     4757            break;
     4758        case SUPSECMAIN_FLAGS_LOC_TESTCASE:
     4759        {
     4760            /* Drop one directory level. */
     4761            USHORT off = cwc;
     4762            WCHAR  wc;
     4763            while (   off > 1
     4764                   && (wc = g_SupLibHardenedAppBinNtPath.UniStr.Buffer[off - 1]) != '\0')
     4765                if (wc != '\\' && wc != '/')
     4766                    off--;
     4767                else
     4768                {
     4769                    if (g_SupLibHardenedAppBinNtPath.UniStr.Buffer[off - 2] == ':')
     4770                        cwc = off;
     4771                    else
     4772                        cwc = off - 1;
     4773                    break;
     4774                }
     4775            break;
     4776        }
     4777        default:
     4778            supR3HardenedFatal("supR3HardenedWinInitAppBin: Unknown program binary location: %#x\n", fFlags);
     4779    }
     4780
     4781    g_SupLibHardenedAppBinNtPath.UniStr.Buffer[cwc]   = '\0';
     4782    g_SupLibHardenedAppBinNtPath.UniStr.Length        = cwc * sizeof(WCHAR);
     4783    g_SupLibHardenedAppBinNtPath.UniStr.MaximumLength = sizeof(g_SupLibHardenedAppBinNtPath.awcBuffer);
     4784    SUP_DPRINTF(("supR3HardenedWinInitAppBin(%#x): '%ls'\n", fFlags, g_SupLibHardenedAppBinNtPath.UniStr.Buffer));
    46514785}
    46524786
     
    53725506
    53735507    /*
     5508     * Preliminary app binary path init.  May change when SUPR3HardenedMain is
     5509     * called (via main below).
     5510     */
     5511    supR3HardenedWinInitAppBin(SUPSECMAIN_FLAGS_LOC_APP_BIN);
     5512
     5513    /*
    53745514     * If we've done early init already, register the DLL load notification
    53755515     * callback and reinstall the NtDll patches.
     
    55665706
    55675707    /*
     5708     * Preliminary app binary path init.  May change when SUPR3HardenedMain is called.
     5709     */
     5710    supR3HardenedWinInitAppBin(SUPSECMAIN_FLAGS_LOC_APP_BIN);
     5711
     5712    /*
    55685713     * Initialize the image verification stuff (hooks LdrLoadDll and NtCreateSection).
    55695714     */
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