VirtualBox

Changeset 3043 in kBuild


Ignore:
Timestamp:
May 11, 2017 1:28:59 PM (8 years ago)
Author:
bird
Message:

kWorker: DLL and EXE search PATH fixes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kWorker/kWorker.c

    r3042 r3043  
    10351035*********************************************************************************************************************************/
    10361036static FNKLDRMODGETIMPORT kwLdrModuleGetImportCallback;
    1037 static int kwLdrModuleResolveAndLookup(const char *pszName, PKWMODULE pExe, PKWMODULE pImporter, PKWMODULE *ppMod);
     1037static int kwLdrModuleResolveAndLookup(const char *pszName, PKWMODULE pExe, PKWMODULE pImporter,
     1038                                       const char *pszSearchPath, PKWMODULE *ppMod);
    10381039static KBOOL kwSandboxHandleTableEnter(PKWSANDBOX pSandbox, PKWHANDLE pHandle, HANDLE hHandle);
    10391040#ifdef WITH_CONSOLE_OUTPUT_BUFFERING
     
    22022203 * @param   pExeMod             The executable module of the process (for
    22032204 *                              resolving imports).  NULL if fExe is set.
    2204  */
    2205 static PKWMODULE kwLdrModuleCreateNonNative(const char *pszPath, KU32 uHashPath, KBOOL fExe, PKWMODULE pExeMod)
     2205 * @param   pszSearchPath       The PATH to search for imports.  Can be NULL.
     2206 */
     2207static PKWMODULE kwLdrModuleCreateNonNative(const char *pszPath, KU32 uHashPath, KBOOL fExe,
     2208                                            PKWMODULE pExeMod, const char *pszSearchPath)
    22062209{
    22072210    /*
     
    23112314                                if (rc == 0)
    23122315                                {
    2313                                     rc = kwLdrModuleResolveAndLookup(szName, pExeMod, pMod, &pMod->u.Manual.apImpMods[iImp]);
     2316                                    rc = kwLdrModuleResolveAndLookup(szName, pExeMod, pMod, pszSearchPath,
     2317                                                                     &pMod->u.Manual.apImpMods[iImp]);
    23142318                                    if (rc == 0)
    23152319                                        continue;
     
    26062610 *                              native loader or need to sandbox the DLL.
    26072611 * @param   pExe                The executable (optional).
    2608  */
    2609 static PKWMODULE kwLdrModuleTryLoadDll(const char *pszPath, KWLOCATION enmLocation, PKWMODULE pExeMod)
     2612 * @param   pszSearchPath       The PATH to search (optional).
     2613 */
     2614static PKWMODULE kwLdrModuleTryLoadDll(const char *pszPath, KWLOCATION enmLocation, PKWMODULE pExeMod, const char *pszSearchPath)
    26102615{
    26112616    /*
     
    26442649                                               kwLdrModuleShouldDoNativeReplacements(pszName, enmLocation));
    26452650            else
    2646                 pMod = kwLdrModuleCreateNonNative(szNormPath, uHashPath, K_FALSE /*fExe*/, pExeMod);
     2651                pMod = kwLdrModuleCreateNonNative(szNormPath, uHashPath, K_FALSE /*fExe*/, pExeMod, pszSearchPath);
    26472652            if (pMod)
    26482653                return pMod;
     
    26662671 * @param   pExe                The executable (optional).
    26672672 * @param   pImporter           The module doing the importing (optional).
     2673 * @param   pszSearchPath       The PATH to search (optional).
    26682674 * @param   ppMod               Where to return the module pointer w/ reference.
    26692675 */
    2670 static int kwLdrModuleResolveAndLookup(const char *pszName, PKWMODULE pExe, PKWMODULE pImporter, PKWMODULE *ppMod)
     2676static int kwLdrModuleResolveAndLookup(const char *pszName, PKWMODULE pExe, PKWMODULE pImporter,
     2677                                       const char *pszSearchPath, PKWMODULE *ppMod)
    26712678{
    26722679    KSIZE const cchName = kHlpStrLen(pszName);
     
    26872694        if (fNeedSuffix)
    26882695            kHlpMemCopy(psz - 1, ".dll", sizeof(".dll"));
    2689         pMod = kwLdrModuleTryLoadDll(szPath, KWLOCATION_IMPORTER_DIR, pExe);
     2696        pMod = kwLdrModuleTryLoadDll(szPath, KWLOCATION_IMPORTER_DIR, pExe, pszSearchPath);
    26902697    }
    26912698
     
    26982705        if (fNeedSuffix)
    26992706            kHlpMemCopy(psz - 1, ".dll", sizeof(".dll"));
    2700         pMod = kwLdrModuleTryLoadDll(szPath, KWLOCATION_EXE_DIR, pExe);
     2707        pMod = kwLdrModuleTryLoadDll(szPath, KWLOCATION_EXE_DIR, pExe, pszSearchPath);
    27012708    }
    27022709
     
    27122719        if (fNeedSuffix)
    27132720            kHlpMemCopy(psz - 1, ".dll", sizeof(".dll"));
    2714         pMod = kwLdrModuleTryLoadDll(szPath, KWLOCATION_SYSTEM32, pExe);
     2721        pMod = kwLdrModuleTryLoadDll(szPath, KWLOCATION_SYSTEM32, pExe, pszSearchPath);
     2722    }
     2723
     2724    /* The path. */
     2725    if (   pMod == NULL
     2726        && pszSearchPath)
     2727    {
     2728        const char *pszCur = pszSearchPath;
     2729        while (*pszCur != '\0')
     2730        {
     2731            /* Find the end of the component */
     2732            KSIZE cch = 0;
     2733            while (pszCur[cch] != ';' && pszCur[cch] != '\0')
     2734                cch++;
     2735
     2736            if (   cch > 0 /* wrong, but whatever */
     2737                && cch + 1 + cchName + cchSuffix < sizeof(szPath))
     2738            {
     2739                char *pszDst = kHlpMemPCopy(szPath, pszCur, cch);
     2740                if (   szPath[cch - 1] != ':'
     2741                    && szPath[cch - 1] != '/'
     2742                    && szPath[cch - 1] != '\\')
     2743                    *pszDst++ = '\\';
     2744                pszDst = kHlpMemPCopy(pszDst, pszName, cchName);
     2745                if (fNeedSuffix)
     2746                    pszDst = kHlpMemPCopy(pszDst, ".dll", 4);
     2747                *pszDst = '\0';
     2748
     2749                pMod = kwLdrModuleTryLoadDll(szPath, KWLOCATION_SYSTEM32, pExe, pszSearchPath);
     2750                if (pMod)
     2751                    break;
     2752            }
     2753
     2754            /* Advance */
     2755            pszCur += cch;
     2756            while (*pszCur == ';')
     2757                pszCur++;
     2758        }
    27152759    }
    27162760
     
    30893133 *                              A reference is donated by the caller and must be
    30903134 *                              released.
    3091  */
    3092 static PKWTOOL kwToolEntryCreate(PKFSOBJ pToolFsObj)
     3135 * @param   pszSearchPath       The PATH environment variable value, or NULL.
     3136 */
     3137static PKWTOOL kwToolEntryCreate(PKFSOBJ pToolFsObj, const char *pszSearchPath)
    30933138{
    30943139    KSIZE   cwcPath = pToolFsObj->cwcParent + pToolFsObj->cwcName + 1;
     
    31083153
    31093154        pTool->enmType = KWTOOLTYPE_SANDBOXED;
    3110         pTool->u.Sandboxed.pExe = kwLdrModuleCreateNonNative(pTool->pszPath, kwStrHash(pTool->pszPath), K_TRUE /*fExe*/, NULL);
     3155        pTool->u.Sandboxed.pExe = kwLdrModuleCreateNonNative(pTool->pszPath, kwStrHash(pTool->pszPath), K_TRUE /*fExe*/,
     3156                                                             NULL /*pEexeMod*/, pszSearchPath);
    31113157        if (pTool->u.Sandboxed.pExe)
    31123158        {
     
    31473193 * @returns Pointer to the tool entry.  NULL on failure.
    31483194 * @param   pszExe              The executable for the tool (not normalized).
    3149  */
    3150 static PKWTOOL kwToolLookup(const char *pszExe)
     3195 * @param   cEnvVars            Number of environment varibles.
     3196 * @param   papszEnvVars        Environment variables.  For getting the PATH.
     3197 */
     3198static PKWTOOL kwToolLookup(const char *pszExe, KU32 cEnvVars, const char **papszEnvVars)
    31513199{
    31523200    /*
     
    31683216        if (pToolFsObj->bObjType == KFSOBJ_TYPE_FILE)
    31693217        {
     3218            const char *pszSearchPath;
    31703219            PKWTOOL pTool = (PKWTOOL)kFsCacheObjGetUserData(g_pFsCache, pToolFsObj, KW_DATA_KEY_TOOL);
    31713220            if (pTool)
     
    31783227             * Need to create a new tool.
    31793228             */
    3180             return kwToolEntryCreate(pToolFsObj);
     3229            pszSearchPath = NULL;
     3230            while (cEnvVars-- > 0)
     3231                if (_strnicmp(papszEnvVars[cEnvVars], "PATH=", 5) == 0)
     3232                {
     3233                    pszSearchPath = &papszEnvVars[cEnvVars][5];
     3234                    break;
     3235                }
     3236            return kwToolEntryCreate(pToolFsObj, pszSearchPath);
    31813237        }
    31823238        kFsCacheObjRelease(g_pFsCache, pToolFsObj);
    31833239    }
    3184     else
    3185         pToolFsObj = kFsCacheLookupA(g_pFsCache, pszExe, &enmError);
    31863240    return NULL;
    31873241}
     
    46394693{
    46404694    KSIZE       cchFilename = kHlpStrLen(pszFilename);
     4695    const char *pszSearchPath;
    46414696    PKWDYNLOAD  pDynLoad;
    46424697    PKWMODULE   pMod;
     
    47134768     * We start by being very lazy and reusing the code for resolving imports.
    47144769     */
     4770    pszSearchPath = kwSandboxDoGetEnvA(&g_Sandbox, "PATH", 4);
    47154771    if (!kHlpIsFilenameOnly(pszFilename))
    4716         pMod = kwLdrModuleTryLoadDll(pszFilename, KWLOCATION_UNKNOWN, g_Sandbox.pTool->u.Sandboxed.pExe);
     4772        pMod = kwLdrModuleTryLoadDll(pszFilename, KWLOCATION_UNKNOWN, g_Sandbox.pTool->u.Sandboxed.pExe, pszSearchPath);
    47174773    else
    47184774    {
    4719         rc = kwLdrModuleResolveAndLookup(pszFilename, g_Sandbox.pTool->u.Sandboxed.pExe, NULL /*pImporter*/, &pMod);
     4775        rc = kwLdrModuleResolveAndLookup(pszFilename, g_Sandbox.pTool->u.Sandboxed.pExe, NULL /*pImporter*/, pszSearchPath, &pMod);
    47204776        if (rc != 0)
    47214777            pMod = NULL;
     
    47584814static HMODULE WINAPI kwSandbox_Kernel32_Native_LoadLibraryExA(LPCSTR pszFilename, HANDLE hFile, DWORD fFlags)
    47594815{
    4760     char szTmp[512];
     4816    char szPath[1024];
    47614817    KWLDR_LOG(("kwSandbox_Kernel32_Native_LoadLibraryExA(%s, %p, %#x)\n", pszFilename, hFile, fFlags));
    47624818
     
    47684824        KSIZE cchFilename = kHlpStrLen(pszFilename);
    47694825        KSIZE cchExePath  = g_Sandbox.pTool->u.Sandboxed.pExe->offFilename;
    4770         if (cchExePath + cchFilename + 1 <= sizeof(szTmp))
    4771         {
    4772             kHlpMemCopy(szTmp, g_Sandbox.pTool->u.Sandboxed.pExe->pszPath, cchExePath);
    4773             kHlpMemCopy(&szTmp[cchExePath], pszFilename, cchFilename + 1);
    4774             if (kwFsPathExists(szTmp))
    4775             {
    4776                 KWLDR_LOG(("kwSandbox_Kernel32_Native_LoadLibraryExA: %s -> %s\n", pszFilename, szTmp));
    4777                 pszFilename = szTmp;
     4826        if (cchExePath + cchFilename + 1 <= sizeof(szPath))
     4827        {
     4828            kHlpMemCopy(szPath, g_Sandbox.pTool->u.Sandboxed.pExe->pszPath, cchExePath);
     4829            kHlpMemCopy(&szPath[cchExePath], pszFilename, cchFilename + 1);
     4830            if (kwFsPathExists(szPath))
     4831            {
     4832                KWLDR_LOG(("kwSandbox_Kernel32_Native_LoadLibraryExA: %s -> %s\n", pszFilename, szPath));
     4833                pszFilename = szPath;
     4834            }
     4835        }
     4836
     4837        if (pszFilename != szPath)
     4838        {
     4839            KSIZE cchSuffix = 0;
     4840            KBOOL fNeedSuffix = K_FALSE;
     4841            const char *pszCur = kwSandboxDoGetEnvA(&g_Sandbox, "PATH", 4);
     4842            while (*pszCur != '\0')
     4843            {
     4844                /* Find the end of the component */
     4845                KSIZE cch = 0;
     4846                while (pszCur[cch] != ';' && pszCur[cch] != '\0')
     4847                    cch++;
     4848
     4849                if (   cch > 0 /* wrong, but whatever */
     4850                    && cch + 1 + cchFilename + cchSuffix < sizeof(szPath))
     4851                {
     4852                    char *pszDst = kHlpMemPCopy(szPath, pszCur, cch);
     4853                    if (   szPath[cch - 1] != ':'
     4854                        && szPath[cch - 1] != '/'
     4855                        && szPath[cch - 1] != '\\')
     4856                        *pszDst++ = '\\';
     4857                    pszDst = kHlpMemPCopy(pszDst, pszFilename, cchFilename);
     4858                    if (fNeedSuffix)
     4859                        pszDst = kHlpMemPCopy(pszDst, ".dll", 4);
     4860                    *pszDst = '\0';
     4861
     4862                    if (kwFsPathExists(szPath))
     4863                    {
     4864                        KWLDR_LOG(("kwSandbox_Kernel32_Native_LoadLibraryExA: %s -> %s\n", pszFilename, szPath));
     4865                        pszFilename = szPath;
     4866                        break;
     4867                    }
     4868                }
     4869
     4870                /* Advance */
     4871                pszCur += cch;
     4872                while (*pszCur == ';')
     4873                    pszCur++;
    47784874            }
    47794875        }
     
    96759771            const char *pszVar   = papszEnvVars[i];
    96769772            KSIZE       cchVar   = kHlpStrLen(pszVar);
     9773            const char *pszEqual;
    96779774            if (   cchVar > 0
    9678                 && kHlpMemChr(pszVar, '=', cchVar) != NULL)
     9775                && (pszEqual = kHlpMemChr(pszVar, '=', cchVar)) != NULL)
    96799776            {
    96809777                char       *pszCopy  = kHlpDup(pszVar, cchVar + 1);
     
    96869783                    pSandbox->papwszEnvVars[iDst] = pwszCopy;
    96879784                    pSandbox->wenviron[iDst]      = pwszCopy;
     9785
     9786                    /* When we see the path, we must tell the system or native exec and module loading won't work . */
     9787                    if (   (pszEqual - pszVar) == 4
     9788                        && (  pszCopy[0] == 'P' || pszCopy[0] == 'p')
     9789                        && (  pszCopy[1] == 'A' || pszCopy[1] == 'a')
     9790                        && (  pszCopy[2] == 'T' || pszCopy[2] == 't')
     9791                        && (  pszCopy[3] == 'H' || pszCopy[3] == 'h'))
     9792                        if (!SetEnvironmentVariableW(L"Path", &pwszCopy[5]))
     9793                            kwErrPrintf("kwSandboxInit: SetEnvironmentVariableW(Path,) failed: %u\n", GetLastError());
     9794
    96889795                    iDst++;
    96899796                }
     
    97059812    else
    97069813        return kwErrPrintfRc(KERR_NO_MEMORY, "Error setting up environment variables: kwSandboxGrowEnv failed\n");
    9707 
    97089814
    97099815    /*
     
    1021610322     * Lookup the tool.
    1021710323     */
    10218     pTool = kwToolLookup(pszExecutable);
     10324    pTool = kwToolLookup(pszExecutable, cEnvVars, papszEnvVars);
    1021910325    if (pTool)
    1022010326    {
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