From 94aa267edfffed7fe6912798ec99db65c09e522d Mon Sep 17 00:00:00 2001 From: LADSoft Date: Fri, 4 Aug 2023 09:15:24 -0400 Subject: [PATCH 1/8] fix putenv to not have a buffer overflow --- src/clibs/procont/putenv.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/clibs/procont/putenv.c b/src/clibs/procont/putenv.c index 4151a5485..b6dc4f72c 100644 --- a/src/clibs/procont/putenv.c +++ b/src/clibs/procont/putenv.c @@ -34,12 +34,11 @@ extern char _RTL_DATA** _environ; int _RTL_FUNC putenv(const char* name) { - wchar_t buf[260], *x = buf; + wchar_t *buf = calloc(sizeof(wchar_t), strlen(name)+1), *x = buf; const char *y = name; while (*y) *x++ = *y++; - *x= *y; - int rv = _wputenv(wcsdup(buf)); + int rv = _wputenv(buf); if (rv) return rv; char **q = _environ, **p; From 187451070b9c49d608546c35358199e22b121990 Mon Sep 17 00:00:00 2001 From: LADSoft Date: Fri, 4 Aug 2023 09:26:02 -0400 Subject: [PATCH 2/8] remove old file --- src/olink/pe.spc | 44 -------------------------------------------- 1 file changed, 44 deletions(-) delete mode 100644 src/olink/pe.spc diff --git a/src/olink/pe.spc b/src/olink/pe.spc deleted file mode 100644 index 4b3755776..000000000 --- a/src/olink/pe.spc +++ /dev/null @@ -1,44 +0,0 @@ -partition { - overlay { - _CODESTART = $; - CODEBASE = $; - region {} code [ align = 2]; - region {} text [ align = 2]; - region {} vsc* [ align = 2]; - __TLSINITSTART = $; - region {} tls [align = 8]; - __TLSINITEND = $; - IMPORTTHUNKS = $; - region {} importThunks [align = 4, size = IMPORTCOUNT * 6 ]; - _CODEEND = $; - CODESIZE = $ - CODEBASE; - } .text; -} pt1 [addr=IMAGEBASE + OBJECTALIGN, fill = 0x90]; - -partition { - overlay { - RAMBASE = $; - region {} const [ align = 8, roundsize = 8]; - region {} data [ align = 8, roundsize = 4]; - region {} vsd* [ align = 8, roundsize = 4]; - _INITSTART = $; - region {} cstartup [ align = 2]; - _INITEND = $; - _EXITSTART = $; - region {} crundown [ align = 2]; - _EXITEND = $; - _TLSINITSTART = $; - region {} tstartup [ align = 2]; - _TLSINITEND = $; - _TLSEXITSTART = $; - region {} trundown [ align = 2]; - _TLSEXITEND = $; - region {} string [ align = 2]; - INITSIZE = $ - RAMBASE; - _BSSSTART = $; - region {} bss [ align = 8, roundsize = 4]; - region {} vsb* [ align = 8]; - _BSSEND = $; - RAMSIZE = $ - RAMBASE; - } .data ; -} pt2 [addr=((CODEBASE + CODESIZE + OBJECTALIGN - 1) / OBJECTALIGN) * OBJECTALIGN]; From c69c1c2ef69cf2cbbe95d41810d17ee990d9a09e Mon Sep 17 00:00:00 2001 From: LADSoft Date: Fri, 4 Aug 2023 09:30:11 -0400 Subject: [PATCH 3/8] #344 make friendly to linux builds --- src/dlpe/PEImportObject.cpp | 2 ++ src/dlpe/PEObject.h | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/src/dlpe/PEImportObject.cpp b/src/dlpe/PEImportObject.cpp index c733e45d1..6ff90203d 100644 --- a/src/dlpe/PEImportObject.cpp +++ b/src/dlpe/PEImportObject.cpp @@ -317,6 +317,7 @@ void PEImportObject::LoadHandles(DllImports& delay) } void PEImportObject::LoadBindingInfo(DllImports& delay, std::map modules) { +#ifdef TARGET_OS_WINDOWS if (bindTable) { for (auto&& m : modules) @@ -346,6 +347,7 @@ void PEImportObject::LoadBindingInfo(DllImports& delay, std: } } } +#endif } size_t PEImportObject::ThunkSize(std::string name) { diff --git a/src/dlpe/PEObject.h b/src/dlpe/PEObject.h index 4a076c520..8ec873e33 100644 --- a/src/dlpe/PEObject.h +++ b/src/dlpe/PEObject.h @@ -25,7 +25,15 @@ #ifndef PEObject_H #define PEObject_H +#ifdef TARGET_OS_WINDOWS #include +#else +#include +typedef uint32_t DWORD; +typedef void * FARPROC; +#endif + + #include "ObjTypes.h" #define PEHEADER_ONLY #include "PEHeader.h" From 7c732e24b0e93522d5ad30c312a6dbd83317e928 Mon Sep 17 00:00:00 2001 From: LADSoft Date: Fri, 4 Aug 2023 21:10:52 -0400 Subject: [PATCH 4/8] wputenv should use the correct environment --- src/clibs/procont/wputenv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/clibs/procont/wputenv.c b/src/clibs/procont/wputenv.c index f2751790c..f07099a48 100644 --- a/src/clibs/procont/wputenv.c +++ b/src/clibs/procont/wputenv.c @@ -54,7 +54,7 @@ int _RTL_FUNC _wputenv(const wchar_t* name) } q++; } - p = (wchar_t**)realloc(_environ, (count + 2) * sizeof(wchar_t**)); + p = (wchar_t**)realloc(__wenviron, (count + 2) * sizeof(wchar_t**)); if (!p) { __ll_exit_critical(); From 55419b68e6053aecfcb45cca19ea40ad6ed11222 Mon Sep 17 00:00:00 2001 From: LADSoft Date: Fri, 4 Aug 2023 21:11:36 -0400 Subject: [PATCH 5/8] improve _spawnxxx to not have buffer overruns --- src/clibs/platform/win32/rtl/llspawn.c | 6 +++++- src/clibs/procont/spawn.c | 14 ++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/clibs/platform/win32/rtl/llspawn.c b/src/clibs/platform/win32/rtl/llspawn.c index a20fec40f..8d351e7b5 100644 --- a/src/clibs/platform/win32/rtl/llspawn.c +++ b/src/clibs/platform/win32/rtl/llspawn.c @@ -91,13 +91,14 @@ int __ll_spawn(char* file, char* parms, char** env, int mode) PROCESS_INFORMATION pi; STARTUPINFO si; DWORD rv = -1; - char buf[1000], *block = createenviron(env); + char *buf, *block = createenviron(env); memset(&si, 0, sizeof(si)); si.cb = sizeof(STARTUPINFO); si.dwFlags = STARTF_USESTDHANDLES; si.hStdInput = (HANDLE)__uiohandle(fileno(stdin)); si.hStdOutput = (HANDLE)__uiohandle(fileno(stdout)); si.hStdError = (HANDLE)__uiohandle(fileno(stderr)); + buf = calloc(sizeof(char), 4 + strlen(file) + strlen(parms)); sprintf(buf, "\"%s\" %s", file, parms); if (CreateProcess(file, buf, 0, 0, TRUE, NORMAL_PRIORITY_CLASS | (DETACHED_PROCESS * (mode == P_DETACH)), (LPVOID)block, 0, &si, &pi)) @@ -114,11 +115,14 @@ int __ll_spawn(char* file, char* parms, char** env, int mode) CloseHandle(pi.hProcess); CloseHandle(pi.hThread); } + free(buf); free(block); return rv; } else { + errno= GetLastError(); + free(buf); free(block); return -1; } diff --git a/src/clibs/procont/spawn.c b/src/clibs/procont/spawn.c index bd2d8c397..c0e82886d 100644 --- a/src/clibs/procont/spawn.c +++ b/src/clibs/procont/spawn.c @@ -41,11 +41,17 @@ static int spawnbase(const char* path, const char* args[], const char* env[], in FILE* fil; int rv; char name[260], *vv; - char parms[1024]; - parms[0] = ' '; - parms[1] = 0; + char *parms= NULL; if (*args) { + int len = 2; + const char **p = args; + while (*p) + { + len += strlen(p) + 1; + p++; + } + parms = calloc(sizeof(char), len); while (*++args) { strcat(parms, " "); @@ -82,7 +88,7 @@ static int spawnbase(const char* path, const char* args[], const char* env[], in vv = name; } fflush(0); - rv = __ll_spawn(vv, parms, env, toexit); + rv = __ll_spawn(vv, parms ? parms : " ", env, toexit); if (toexit == P_OVERLAY) exit(rv); return rv; From c1c94c245609ce1cf47c1f92a0bfd73698591a31 Mon Sep 17 00:00:00 2001 From: LADSoft Date: Sat, 5 Aug 2023 00:20:45 -0400 Subject: [PATCH 6/8] fix linux build --- src/dlpe/PEObject.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/dlpe/PEObject.h b/src/dlpe/PEObject.h index 8ec873e33..9757faa55 100644 --- a/src/dlpe/PEObject.h +++ b/src/dlpe/PEObject.h @@ -30,7 +30,8 @@ #else #include typedef uint32_t DWORD; -typedef void * FARPROC; +typedef DWORD FARPROC; +typedef unsigned char * BYTE; #endif From e95a01c6dc23c37ccf04105c0e703fba11f0aaef Mon Sep 17 00:00:00 2001 From: LADSoft Date: Sat, 5 Aug 2023 00:31:02 -0400 Subject: [PATCH 7/8] fix linux build --- src/dlpe/PEObject.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dlpe/PEObject.h b/src/dlpe/PEObject.h index 9757faa55..e654778c4 100644 --- a/src/dlpe/PEObject.h +++ b/src/dlpe/PEObject.h @@ -31,7 +31,7 @@ #include typedef uint32_t DWORD; typedef DWORD FARPROC; -typedef unsigned char * BYTE; +typedef unsigned char BYTE; #endif From 18ede53ed7b696ec4a1675bc1bfd6509d3d8c9c9 Mon Sep 17 00:00:00 2001 From: LADSoft Date: Sat, 5 Aug 2023 00:47:53 -0400 Subject: [PATCH 8/8] fix linux build --- src/dlpe/PEImportObject.cpp | 14 +++++++++++++- src/dlpe/PEObject.h | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/dlpe/PEImportObject.cpp b/src/dlpe/PEImportObject.cpp index 6ff90203d..cda7c0d4a 100644 --- a/src/dlpe/PEImportObject.cpp +++ b/src/dlpe/PEImportObject.cpp @@ -227,9 +227,19 @@ void DllImports::WriteDirectory(DWORD virtual_addr, DWORD im m->moduleHandleRVA, RVA(iatPos), RVA(namePos), +#ifdef TARGET_OS_WINDOWS bindPos ? RVA(bindPos) : 0, +#else + 0, +#endif unloadPos ? RVA(unloadPos) : 0, - m->time}; +#ifdef TARGET_OS_WINDOWS + m->time +#else + 0 +#endif +}; + std::copy((char*)&dir, (char*)(&dir + 1), data + directoryPos); std::copy(m->name.begin(), m->name.end(), data + stringPos); directoryPos += sizeof(DelayLoadDirectory); @@ -280,10 +290,12 @@ void DllImports::WriteTables(std::vector& thunkFixups *unloadPointer = thunkTableRVA + imageBase; thunkFixups.push_back(RVA((BYTE*)unloadPointer - data) + imageBase); } +#ifdef TARGET_OS_WINDOWS if (bindPos) { *bindPointer = (DWORD)std::get<3>(e); } +#endif thunkTableRVA += PEImportObject::DelayLoadThunkSize; std::copy(name.begin(), name.end(), data + stringPos + sizeof(HintType)); sym->SetOffset(new ObjExpression(RVA((iatPointer - iatBase) * sizeof(DWORD) + iatPos) + imageBase)); diff --git a/src/dlpe/PEObject.h b/src/dlpe/PEObject.h index e654778c4..38cd35895 100644 --- a/src/dlpe/PEObject.h +++ b/src/dlpe/PEObject.h @@ -30,7 +30,7 @@ #else #include typedef uint32_t DWORD; -typedef DWORD FARPROC; +typedef void* FARPROC; typedef unsigned char BYTE; #endif