Skip to content

Commit

Permalink
Fix + Tests: Add more tests covering the modules API, fix error handl…
Browse files Browse the repository at this point in the history
…ing bug that were found. (#177)

* Fix: Handle more error cases in module API.

- `DetoursFindFunction` wasn't gracefully handling NULL function name.

- `DetourEnumerateModules` wasn't resetting GLE on success.

- `DetourEnumerateExports` wasn't gracefully handling NULL export callback.

- `DetourEnumerateImports` wasn't gracefully handling NULL arguments.

* Tests: Add more tests covering the modules API.

Test basic functionality and error handling of the Detours Module API.

* DetourLoadImageHlp
* DetourFindFunction
* DetourEnumerateModules
* DetourEnumerateExports
* DetourEnumerateImports
* DetourGetSizeOfPayloads
* DetourFindPayload
* DetourFindPayloadEx
* DetourRestoreAfterWithEx
  • Loading branch information
bgianfo authored and number201724 committed Mar 5, 2021
1 parent ebbf721 commit f109e89
Show file tree
Hide file tree
Showing 2 changed files with 549 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/modules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ PDETOUR_SYM_INFO DetourLoadImageHlp(VOID)
PVOID WINAPI DetourFindFunction(_In_ LPCSTR pszModule,
_In_ LPCSTR pszFunction)
{
if (pszFunction == NULL) {
SetLastError(ERROR_INVALID_PARAMETER);
return NULL;
}

/////////////////////////////////////////////// First, try GetProcAddress.
//
#pragma prefast(suppress:28752, "We don't do the unicode conversion for LoadLibraryExA.")
Expand Down Expand Up @@ -277,6 +282,7 @@ HMODULE WINAPI DetourEnumerateModules(_In_opt_ HMODULE hModuleLast)
continue;
}

SetLastError(NO_ERROR);
return (HMODULE)pDosHeader;
}
#pragma prefast(suppress:28940, "A bad pointer means this probably isn't a PE header.")
Expand Down Expand Up @@ -456,6 +462,11 @@ BOOL WINAPI DetourEnumerateExports(_In_ HMODULE hModule,
_In_opt_ PVOID pContext,
_In_ PF_DETOUR_ENUMERATE_EXPORT_CALLBACK pfExport)
{
if (pfExport == NULL) {
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}

PIMAGE_DOS_HEADER pDosHeader = (PIMAGE_DOS_HEADER)hModule;
if (hModule == NULL) {
pDosHeader = (PIMAGE_DOS_HEADER)GetModuleHandleW(NULL);
Expand Down Expand Up @@ -658,6 +669,11 @@ BOOL WINAPI DetourEnumerateImports(_In_opt_ HMODULE hModule,
_In_opt_ PF_DETOUR_IMPORT_FILE_CALLBACK pfImportFile,
_In_opt_ PF_DETOUR_IMPORT_FUNC_CALLBACK pfImportFunc)
{
if (pfImportFile == NULL || pfImportFunc == NULL) {
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}

_DETOUR_ENUMERATE_IMPORTS_THUNK_CONTEXT const context = { pContext, pfImportFunc };

return DetourEnumerateImportsEx(hModule,
Expand Down
Loading

0 comments on commit f109e89

Please sign in to comment.