Changeset 3043 in kBuild
- Timestamp:
- May 11, 2017 1:28:59 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kWorker/kWorker.c
r3042 r3043 1035 1035 *********************************************************************************************************************************/ 1036 1036 static FNKLDRMODGETIMPORT kwLdrModuleGetImportCallback; 1037 static int kwLdrModuleResolveAndLookup(const char *pszName, PKWMODULE pExe, PKWMODULE pImporter, PKWMODULE *ppMod); 1037 static int kwLdrModuleResolveAndLookup(const char *pszName, PKWMODULE pExe, PKWMODULE pImporter, 1038 const char *pszSearchPath, PKWMODULE *ppMod); 1038 1039 static KBOOL kwSandboxHandleTableEnter(PKWSANDBOX pSandbox, PKWHANDLE pHandle, HANDLE hHandle); 1039 1040 #ifdef WITH_CONSOLE_OUTPUT_BUFFERING … … 2202 2203 * @param pExeMod The executable module of the process (for 2203 2204 * 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 */ 2207 static PKWMODULE kwLdrModuleCreateNonNative(const char *pszPath, KU32 uHashPath, KBOOL fExe, 2208 PKWMODULE pExeMod, const char *pszSearchPath) 2206 2209 { 2207 2210 /* … … 2311 2314 if (rc == 0) 2312 2315 { 2313 rc = kwLdrModuleResolveAndLookup(szName, pExeMod, pMod, &pMod->u.Manual.apImpMods[iImp]); 2316 rc = kwLdrModuleResolveAndLookup(szName, pExeMod, pMod, pszSearchPath, 2317 &pMod->u.Manual.apImpMods[iImp]); 2314 2318 if (rc == 0) 2315 2319 continue; … … 2606 2610 * native loader or need to sandbox the DLL. 2607 2611 * @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 */ 2614 static PKWMODULE kwLdrModuleTryLoadDll(const char *pszPath, KWLOCATION enmLocation, PKWMODULE pExeMod, const char *pszSearchPath) 2610 2615 { 2611 2616 /* … … 2644 2649 kwLdrModuleShouldDoNativeReplacements(pszName, enmLocation)); 2645 2650 else 2646 pMod = kwLdrModuleCreateNonNative(szNormPath, uHashPath, K_FALSE /*fExe*/, pExeMod );2651 pMod = kwLdrModuleCreateNonNative(szNormPath, uHashPath, K_FALSE /*fExe*/, pExeMod, pszSearchPath); 2647 2652 if (pMod) 2648 2653 return pMod; … … 2666 2671 * @param pExe The executable (optional). 2667 2672 * @param pImporter The module doing the importing (optional). 2673 * @param pszSearchPath The PATH to search (optional). 2668 2674 * @param ppMod Where to return the module pointer w/ reference. 2669 2675 */ 2670 static int kwLdrModuleResolveAndLookup(const char *pszName, PKWMODULE pExe, PKWMODULE pImporter, PKWMODULE *ppMod) 2676 static int kwLdrModuleResolveAndLookup(const char *pszName, PKWMODULE pExe, PKWMODULE pImporter, 2677 const char *pszSearchPath, PKWMODULE *ppMod) 2671 2678 { 2672 2679 KSIZE const cchName = kHlpStrLen(pszName); … … 2687 2694 if (fNeedSuffix) 2688 2695 kHlpMemCopy(psz - 1, ".dll", sizeof(".dll")); 2689 pMod = kwLdrModuleTryLoadDll(szPath, KWLOCATION_IMPORTER_DIR, pExe );2696 pMod = kwLdrModuleTryLoadDll(szPath, KWLOCATION_IMPORTER_DIR, pExe, pszSearchPath); 2690 2697 } 2691 2698 … … 2698 2705 if (fNeedSuffix) 2699 2706 kHlpMemCopy(psz - 1, ".dll", sizeof(".dll")); 2700 pMod = kwLdrModuleTryLoadDll(szPath, KWLOCATION_EXE_DIR, pExe );2707 pMod = kwLdrModuleTryLoadDll(szPath, KWLOCATION_EXE_DIR, pExe, pszSearchPath); 2701 2708 } 2702 2709 … … 2712 2719 if (fNeedSuffix) 2713 2720 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 } 2715 2759 } 2716 2760 … … 3089 3133 * A reference is donated by the caller and must be 3090 3134 * released. 3091 */ 3092 static PKWTOOL kwToolEntryCreate(PKFSOBJ pToolFsObj) 3135 * @param pszSearchPath The PATH environment variable value, or NULL. 3136 */ 3137 static PKWTOOL kwToolEntryCreate(PKFSOBJ pToolFsObj, const char *pszSearchPath) 3093 3138 { 3094 3139 KSIZE cwcPath = pToolFsObj->cwcParent + pToolFsObj->cwcName + 1; … … 3108 3153 3109 3154 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); 3111 3157 if (pTool->u.Sandboxed.pExe) 3112 3158 { … … 3147 3193 * @returns Pointer to the tool entry. NULL on failure. 3148 3194 * @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 */ 3198 static PKWTOOL kwToolLookup(const char *pszExe, KU32 cEnvVars, const char **papszEnvVars) 3151 3199 { 3152 3200 /* … … 3168 3216 if (pToolFsObj->bObjType == KFSOBJ_TYPE_FILE) 3169 3217 { 3218 const char *pszSearchPath; 3170 3219 PKWTOOL pTool = (PKWTOOL)kFsCacheObjGetUserData(g_pFsCache, pToolFsObj, KW_DATA_KEY_TOOL); 3171 3220 if (pTool) … … 3178 3227 * Need to create a new tool. 3179 3228 */ 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); 3181 3237 } 3182 3238 kFsCacheObjRelease(g_pFsCache, pToolFsObj); 3183 3239 } 3184 else3185 pToolFsObj = kFsCacheLookupA(g_pFsCache, pszExe, &enmError);3186 3240 return NULL; 3187 3241 } … … 4639 4693 { 4640 4694 KSIZE cchFilename = kHlpStrLen(pszFilename); 4695 const char *pszSearchPath; 4641 4696 PKWDYNLOAD pDynLoad; 4642 4697 PKWMODULE pMod; … … 4713 4768 * We start by being very lazy and reusing the code for resolving imports. 4714 4769 */ 4770 pszSearchPath = kwSandboxDoGetEnvA(&g_Sandbox, "PATH", 4); 4715 4771 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); 4717 4773 else 4718 4774 { 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); 4720 4776 if (rc != 0) 4721 4777 pMod = NULL; … … 4758 4814 static HMODULE WINAPI kwSandbox_Kernel32_Native_LoadLibraryExA(LPCSTR pszFilename, HANDLE hFile, DWORD fFlags) 4759 4815 { 4760 char sz Tmp[512];4816 char szPath[1024]; 4761 4817 KWLDR_LOG(("kwSandbox_Kernel32_Native_LoadLibraryExA(%s, %p, %#x)\n", pszFilename, hFile, fFlags)); 4762 4818 … … 4768 4824 KSIZE cchFilename = kHlpStrLen(pszFilename); 4769 4825 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++; 4778 4874 } 4779 4875 } … … 9675 9771 const char *pszVar = papszEnvVars[i]; 9676 9772 KSIZE cchVar = kHlpStrLen(pszVar); 9773 const char *pszEqual; 9677 9774 if ( cchVar > 0 9678 && kHlpMemChr(pszVar, '=', cchVar) != NULL)9775 && (pszEqual = kHlpMemChr(pszVar, '=', cchVar)) != NULL) 9679 9776 { 9680 9777 char *pszCopy = kHlpDup(pszVar, cchVar + 1); … … 9686 9783 pSandbox->papwszEnvVars[iDst] = pwszCopy; 9687 9784 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 9688 9795 iDst++; 9689 9796 } … … 9705 9812 else 9706 9813 return kwErrPrintfRc(KERR_NO_MEMORY, "Error setting up environment variables: kwSandboxGrowEnv failed\n"); 9707 9708 9814 9709 9815 /* … … 10216 10322 * Lookup the tool. 10217 10323 */ 10218 pTool = kwToolLookup(pszExecutable );10324 pTool = kwToolLookup(pszExecutable, cEnvVars, papszEnvVars); 10219 10325 if (pTool) 10220 10326 {
Note:
See TracChangeset
for help on using the changeset viewer.