Skip to content

Commit

Permalink
DBG: fix potential crashes in GetModuleInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexodia committed Jan 20, 2019
1 parent 661360b commit 7d53b1a
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/dbg/module.cpp
Expand Up @@ -291,7 +291,7 @@ static void ReadTlsCallbacks(MODINFO & Info, ULONG_PTR FileMapVA)
// TODO: proper bounds checking
auto tlsArray = PULONG_PTR(tlsArrayOffset + FileMapVA);
while(*tlsArray)
Info.tlsCallbacks.push_back(*tlsArray++ - imageBase + Info.base);
Info.tlsCallbacks.push_back(duint(*tlsArray++ - imageBase + Info.base));
}

#ifndef IMAGE_REL_BASED_RESERVED
Expand Down Expand Up @@ -379,7 +379,7 @@ static void ReadBaseRelocationTable(MODINFO & Info, ULONG_PTR FileMapVA)
}

//Useful information: http://www.debuginfo.com/articles/debuginfomatch.html
void ReadDebugDirectory(MODINFO & Info, ULONG_PTR FileMapVA)
static void ReadDebugDirectory(MODINFO & Info, ULONG_PTR FileMapVA)
{
// Get the debug directory and its size
ULONG debugDirSize;
Expand Down Expand Up @@ -590,6 +590,18 @@ void ReadDebugDirectory(MODINFO & Info, ULONG_PTR FileMapVA)
}
}

static void GetUnsafeModuleInfoImpl(MODINFO & Info, ULONG_PTR FileMapVA, void(*func)(MODINFO &, ULONG_PTR), const char* name)
{
__try
{
func(Info, FileMapVA);
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
dprintf(QT_TRANSLATE_NOOP("DBG", "Exception while getting module info (%s), please report...\n"), name);
}
}

void GetModuleInfo(MODINFO & Info, ULONG_PTR FileMapVA)
{
// Get the PE headers
Expand All @@ -606,7 +618,7 @@ void GetModuleInfo(MODINFO & Info, ULONG_PTR FileMapVA)
// OEP can't start at the PE header/offset 0 -- except if module is an EXE.
Info.entry = moduleOEP + Info.base;

Info.headerImageBase = HEADER_FIELD(Info.headers, ImageBase);
Info.headerImageBase = (duint)HEADER_FIELD(Info.headers, ImageBase);

if(!moduleOEP)
{
Expand Down Expand Up @@ -647,11 +659,13 @@ void GetModuleInfo(MODINFO & Info, ULONG_PTR FileMapVA)
ntSection++;
}

ReadExportDirectory(Info, FileMapVA);
ReadImportDirectory(Info, FileMapVA);
ReadTlsCallbacks(Info, FileMapVA);
ReadBaseRelocationTable(Info, FileMapVA);
ReadDebugDirectory(Info, FileMapVA);
#define GetUnsafeModuleInfo(func) GetUnsafeModuleInfoImpl(Info, FileMapVA, func, #func)
GetUnsafeModuleInfo(ReadExportDirectory);
GetUnsafeModuleInfo(ReadImportDirectory);
GetUnsafeModuleInfo(ReadTlsCallbacks);
GetUnsafeModuleInfo(ReadBaseRelocationTable);
GetUnsafeModuleInfo(ReadDebugDirectory);
#undef GetUnsafeModuleInfo
}

bool ModLoad(duint Base, duint Size, const char* FullPath)
Expand Down

0 comments on commit 7d53b1a

Please sign in to comment.