Changeset 2865
- Timestamp:
- Sep 3, 2016, 12:17:13 AM (9 years ago)
- Location:
- trunk/src/kWorker
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kWorker/kWorker.c
r2860 r2865 164 164 /** The state. */ 165 165 KWMODSTATE enmState; 166 #if defined(KBUILD_OS_WINDOWS) && defined(KBUILD_ARCH_AMD64) 167 /** The number of entries in the table. */ 168 KU32 cFunctions; 169 /** The function table address (in the copy). */ 170 PRUNTIME_FUNCTION paFunctions; 171 /** Set if we've already registered a function table already. */ 172 KBOOL fRegisteredFunctionTable; 173 #endif 174 /** Set if we share memory with other executables. */ 175 KBOOL fUseLdBuf; 166 176 /** Number of imported modules. */ 167 177 KSIZE cImpMods; … … 431 441 static KWSANDBOX g_Sandbox; 432 442 443 /** The module currently occupying g_abDefLdBuf. */ 444 static PKWMODULE g_pModInLdBuf = NULL; 445 433 446 /** Module hash table. */ 434 447 static PKWMODULE g_apModules[127]; … … 1219 1232 pMod->pLdrMod = pLdrMod; 1220 1233 pMod->u.Manual.cImpMods = (KU32)cImports; 1234 pMod->u.Manual.fUseLdBuf = K_FALSE; 1235 #if defined(KBUILD_OS_WINDOWS) && defined(KBUILD_ARCH_AMD64) 1236 pMod->u.Manual.fRegisteredFunctionTable = K_FALSE; 1237 #endif 1221 1238 pMod->pszPath = (char *)kHlpMemCopy(&pMod->u.Manual.apImpMods[cImports + 1], pszPath, cbPath); 1222 1239 pMod->pwszPath = (wchar_t *)(pMod->pszPath + cbPath + (cbPath & 1)); … … 1231 1248 pMod->u.Manual.cbImage = kLdrModSize(pLdrMod); 1232 1249 if ( !fFixed 1250 || pLdrMod->enmType != KLDRTYPE_EXECUTABLE_FIXED /* only allow fixed executables */ 1233 1251 || (KUPTR)pMod->u.Manual.pvLoad - (KUPTR)g_abDefLdBuf >= sizeof(g_abDefLdBuf) 1234 1252 || sizeof(g_abDefLdBuf) - (KUPTR)pMod->u.Manual.pvLoad - (KUPTR)g_abDefLdBuf < pMod->u.Manual.cbImage) 1235 1253 rc = kHlpPageAlloc(&pMod->u.Manual.pvLoad, pMod->u.Manual.cbImage, KPROT_EXECUTE_READWRITE, fFixed); 1254 else 1255 pMod->u.Manual.fUseLdBuf = K_TRUE; 1236 1256 if (rc == 0) 1237 1257 { … … 1271 1291 if (rc == 0) 1272 1292 { 1293 #if defined(KBUILD_OS_WINDOWS) && defined(KBUILD_ARCH_AMD64) 1294 /* 1295 * Find the function table. No validation here because the 1296 * loader did that already, right... 1297 */ 1298 KU8 *pbImg = (KU8 *)pMod->u.Manual.pvCopy; 1299 IMAGE_NT_HEADERS const *pNtHdrs; 1300 IMAGE_DATA_DIRECTORY const *pXcptDir; 1301 if (((PIMAGE_DOS_HEADER)pbImg)->e_magic == IMAGE_DOS_SIGNATURE) 1302 pNtHdrs = (PIMAGE_NT_HEADERS)&pbImg[((PIMAGE_DOS_HEADER)pbImg)->e_lfanew]; 1303 else 1304 pNtHdrs = (PIMAGE_NT_HEADERS)pbImg; 1305 pXcptDir = &pNtHdrs->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXCEPTION]; 1306 kHlpAssert(pNtHdrs->Signature == IMAGE_NT_SIGNATURE); 1307 if (pXcptDir->Size > 0) 1308 { 1309 pMod->u.Manual.cFunctions = pXcptDir->Size / sizeof(pMod->u.Manual.paFunctions[0]); 1310 kHlpAssert( pMod->u.Manual.cFunctions * sizeof(pMod->u.Manual.paFunctions[0]) 1311 == pXcptDir->Size); 1312 pMod->u.Manual.paFunctions = (PRUNTIME_FUNCTION)&pbImg[pXcptDir->VirtualAddress]; 1313 } 1314 else 1315 { 1316 pMod->u.Manual.cFunctions = 0; 1317 pMod->u.Manual.paFunctions = NULL; 1318 } 1319 #endif 1320 1321 /* 1322 * Final finish. 1323 */ 1273 1324 pMod->u.Manual.pvBits = pMod->u.Manual.pvCopy; 1274 1325 pMod->u.Manual.enmState = KWMODSTATE_NEEDS_BITS; … … 1577 1628 if (pMod->u.Manual.enmState == KWMODSTATE_NEEDS_BITS) 1578 1629 { 1630 if (pMod->u.Manual.fUseLdBuf) 1631 { 1632 #if defined(KBUILD_OS_WINDOWS) && defined(KBUILD_ARCH_AMD64) 1633 if (g_pModInLdBuf != NULL && g_pModInLdBuf != pMod && pMod->u.Manual.fRegisteredFunctionTable) 1634 { 1635 BOOLEAN fRc = RtlDeleteFunctionTable(pMod->u.Manual.paFunctions); 1636 kHlpAssert(fRc); K_NOREF(fRc); 1637 } 1638 #endif 1639 g_pModInLdBuf = pMod; 1640 } 1641 1579 1642 kHlpMemCopy(pMod->u.Manual.pvLoad, pMod->u.Manual.pvCopy, pMod->u.Manual.cbImage); 1580 1643 pMod->u.Manual.enmState = KWMODSTATE_NEEDS_INIT; 1581 1644 } 1645 1646 #if defined(KBUILD_OS_WINDOWS) && defined(KBUILD_ARCH_AMD64) 1647 /* Need to register function table? */ 1648 if ( !pMod->u.Manual.fRegisteredFunctionTable 1649 && pMod->u.Manual.cFunctions > 0) 1650 { 1651 pMod->u.Manual.fRegisteredFunctionTable = RtlAddFunctionTable(pMod->u.Manual.paFunctions, 1652 pMod->u.Manual.cFunctions, 1653 (KUPTR)pMod->u.Manual.pvLoad) != FALSE; 1654 kHlpAssert(pMod->u.Manual.fRegisteredFunctionTable); 1655 } 1656 #endif 1582 1657 1583 1658 if (pMod->u.Manual.enmState == KWMODSTATE_NEEDS_INIT) … … 4066 4141 4067 4142 4143 /* 4144 * 4145 * Misc function only intercepted while debugging. 4146 * Misc function only intercepted while debugging. 4147 * Misc function only intercepted while debugging. 4148 * 4149 */ 4150 4151 #ifndef NDEBUG 4152 4153 /** CRT - memcpy */ 4154 static void * __cdecl kwSandbox_msvcrt_memcpy(void *pvDst, void const *pvSrc, size_t cb) 4155 { 4156 KU8 const *pbSrc = (KU8 const *)pvSrc; 4157 KU8 *pbDst = (KU8 *)pvDst; 4158 KSIZE cbLeft = cb; 4159 while (cbLeft-- > 0) 4160 *pbDst++ = *pbSrc++; 4161 return pvDst; 4162 } 4163 4164 #endif /* NDEBUG */ 4165 4166 4167 4068 4168 /** 4069 4169 * Functions that needs replacing for sandboxed execution. … … 4172 4272 { TUPLE("__p__environ"), NULL, (KUPTR)kwSandbox_msvcrt___p__environ }, 4173 4273 { TUPLE("__p__wenviron"), NULL, (KUPTR)kwSandbox_msvcrt___p__wenviron }, 4274 4275 #ifndef NDEBUG 4276 { TUPLE("memcpy"), NULL, (KUPTR)kwSandbox_msvcrt_memcpy }, 4277 #endif 4174 4278 }; 4175 4279 /** Number of entries in g_aReplacements. */ … … 4455 4559 4456 4560 4457 static int kwSandboxExec(PKW TOOL pTool, KU32 cArgs, const char **papszArgs, KBOOL fWatcomBrainDamange,4561 static int kwSandboxExec(PKWSANDBOX pSandbox, PKWTOOL pTool, KU32 cArgs, const char **papszArgs, KBOOL fWatcomBrainDamange, 4458 4562 KU32 cEnvVars, const char **papszEnvVars) 4459 4563 { … … 4521 4625 return rcExit; 4522 4626 } 4523 4524 4525 #if 04526 static int kwExecCmdLine(const char *pszExe, const char *pszCmdLine)4527 {4528 int rc;4529 PKWTOOL pTool = kwToolLookup(pszExe);4530 if (pTool)4531 {4532 int rc;4533 int rcExitCode;4534 switch (pTool->enmType)4535 {4536 case KWTOOLTYPE_SANDBOXED:4537 KW_LOG(("Sandboxing tool %s\n", pTool->pszPath));4538 rc = kwSandboxExec(pTool, pszCmdLine, &rcExitCode);4539 break;4540 4541 case KWTOOLTYPE_WATCOM:4542 KW_LOG(("TODO: Watcom style tool %s\n", pTool->pszPath));4543 rc = rcExitCode = 2;4544 break;4545 4546 case KWTOOLTYPE_EXEC:4547 KW_LOG(("TODO: Direct exec tool %s\n", pTool->pszPath));4548 rc = rcExitCode = 2;4549 break;4550 4551 default:4552 kHlpAssertFailed();4553 rc = rcExitCode = 2;4554 break;4555 }4556 KW_LOG(("rcExitCode=%d (rc=%d)\n", rcExitCode, rc));4557 }4558 else4559 rc = 1;4560 return rc;4561 }4562 #endif4563 4627 4564 4628 … … 4620 4684 { 4621 4685 KW_LOG(("Sandboxing tool %s\n", pTool->pszPath)); 4622 rcExit = kwSandboxExec( pTool, cArgs, papszArgs, fWatcomBrainDamange, cEnvVars, papszEnvVars);4686 rcExit = kwSandboxExec(&g_Sandbox, pTool, cArgs, papszArgs, fWatcomBrainDamange, cEnvVars, papszEnvVars); 4623 4687 } 4624 4688 else … … 4950 5014 } 4951 5015 4952 5016 #if 1 4953 5017 4954 5018 int main(int argc, char **argv) 4955 5019 { 4956 #if 14957 5020 KSIZE cbMsgBuf = 0; 4958 5021 KU8 *pbMsgBuf = NULL; … … 5081 5144 return rc > 0 ? 0 : 1; 5082 5145 } 5146 } 5083 5147 5084 5148 #else 5149 5150 static int kwExecCmdLine(const char *pszExe, const char *pszCmdLine) 5151 { 5152 int rc; 5153 PKWTOOL pTool = kwToolLookup(pszExe); 5154 if (pTool) 5155 { 5156 int rcExitCode; 5157 switch (pTool->enmType) 5158 { 5159 case KWTOOLTYPE_SANDBOXED: 5160 KW_LOG(("Sandboxing tool %s\n", pTool->pszPath)); 5161 rc = kwSandboxExec(&g_Sandbox, pTool, pszCmdLine, &rcExitCode); 5162 break; 5163 default: 5164 kHlpAssertFailed(); 5165 KW_LOG(("TODO: Direct exec tool %s\n", pTool->pszPath)); 5166 rc = rcExitCode = 2; 5167 break; 5168 } 5169 KW_LOG(("rcExitCode=%d (rc=%d)\n", rcExitCode, rc)); 5170 } 5171 else 5172 rc = 1; 5173 return rc; 5174 } 5175 5176 int main(int argc, char **argv) 5177 { 5085 5178 int rc = 0; 5086 5179 int i; … … 5108 5201 # endif 5109 5202 return rc; 5203 } 5110 5204 5111 5205 #endif 5112 } 5113 5206
Note:
See TracChangeset
for help on using the changeset viewer.