# HG changeset patch # User Tassilo Philipp # Date 1617737019 -7200 # Node ID a29af998e45702e825d1e38f5f1434b5d1e854b1 # Parent 77ec5123e7abc211e12b43425cb1bae6e4fbc811 - dynload w/ PE files: * some better header check * check if export table exists (thanks Ashok!) * make some dynsyms function behaviour consistent with ELF and mach-o (thanks Ashok!) diff -r 77ec5123e7ab -r a29af998e457 dynload/dynload_syms_pe.c --- a/dynload/dynload_syms_pe.c Sat Jan 23 14:30:24 2021 +0100 +++ b/dynload/dynload_syms_pe.c Tue Apr 06 21:23:39 2021 +0200 @@ -59,7 +59,19 @@ base = (const char*)pLib; pDOSHeader = (IMAGE_DOS_HEADER*)base; pNTHeader = (IMAGE_NT_HEADERS*)(base + pDOSHeader->e_lfanew); + + /* optional header present and big enough? this header should exist as it's only optional for object files */ + if(pNTHeader->FileHeader.SizeOfOptionalHeader < (&pNTHeader->OptionalHeader.DataDirectory - &pNTHeader->OptionalHeader)) + return NULL; + + /* export table available? */ + if(pNTHeader->OptionalHeader.NumberOfRvaAndSizes <= IMAGE_DIRECTORY_ENTRY_EXPORT) + return NULL; + pExportsDataDir = &pNTHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT]; + if(!pExportsDataDir->VirtualAddress) + return NULL; + pExports = (IMAGE_EXPORT_DIRECTORY*)(base + pExportsDataDir->VirtualAddress); pSyms = (DLSyms*)dlAllocMem(sizeof(DLSyms)); @@ -85,12 +97,14 @@ int dlSymsCount(DLSyms* pSyms) { - return (int)pSyms->count; + return pSyms ? (int)pSyms->count : 0; } const char* dlSymsName(DLSyms* pSyms, int index) { + if(!pSyms || index < 0 || index >= pSyms->count) + return NULL; return pSyms->pBase + pSyms->pNames[index]; }