Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix context setting errors in DetourEnumerateImports API #266

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/modules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,7 @@ BOOL WINAPI DetourEnumerateImportsEx(_In_opt_ HMODULE hModule,
struct _DETOUR_ENUMERATE_IMPORTS_THUNK_CONTEXT
{
PVOID pContext;
PF_DETOUR_IMPORT_FILE_CALLBACK pfImportFile;
PF_DETOUR_IMPORT_FUNC_CALLBACK pfImportFunc;
};

Expand All @@ -664,6 +665,19 @@ DetourEnumerateImportsThunk(_In_ PVOID VoidContext,
return pContext->pfImportFunc(pContext->pContext, nOrdinal, pszFunc, ppvFunc ? *ppvFunc : NULL);
}

static
BOOL
CALLBACK
DetourEnumerateImportsFile(_In_opt_ PVOID VoidContext,
_In_opt_ HMODULE hModule,
_In_opt_ LPCSTR pszFile)
{
_DETOUR_ENUMERATE_IMPORTS_THUNK_CONTEXT const * const
pContext = (_DETOUR_ENUMERATE_IMPORTS_THUNK_CONTEXT*)VoidContext;
return pContext->pfImportFile(pContext->pContext, hModule, pszFile);
}


BOOL WINAPI DetourEnumerateImports(_In_opt_ HMODULE hModule,
_In_opt_ PVOID pContext,
_In_opt_ PF_DETOUR_IMPORT_FILE_CALLBACK pfImportFile,
Expand All @@ -674,11 +688,10 @@ BOOL WINAPI DetourEnumerateImports(_In_opt_ HMODULE hModule,
return FALSE;
}

_DETOUR_ENUMERATE_IMPORTS_THUNK_CONTEXT const context = { pContext, pfImportFunc };

_DETOUR_ENUMERATE_IMPORTS_THUNK_CONTEXT const context = { pContext, pfImportFile, pfImportFunc };
return DetourEnumerateImportsEx(hModule,
(PVOID)&context,
pfImportFile,
&DetourEnumerateImportsFile,
&DetourEnumerateImportsThunk);
}

Expand Down
18 changes: 17 additions & 1 deletion tests/test_module_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,9 @@ BOOL WINAPI ImportFileCallback(PVOID pContext, HMODULE, PCSTR pszFile)
reinterpret_cast<EnumerateImportsTestContext*>(pContext);

context->ImportCount++;
context->ImportModuleFound |= Catch::contains(pszFile, "ntdll");
if (pszFile != NULL) {
context->ImportModuleFound |= Catch::contains(pszFile, "ntdll");
}

return TRUE;
}
Expand Down Expand Up @@ -471,6 +473,20 @@ TEST_CASE("DetourEnumerateImports", "[module]")
REQUIRE( context.ImportFuncCount == 0 );
REQUIRE_FALSE( context.ImportFuncFound );
}

SECTION("The context transferred during the input parameter is the same as the context parsed in the callback.")
{
SetLastError(ERROR_INVALID_HANDLE);

EnumerateImportsTestContext context {};
auto success = DetourEnumerateImports(NULL, &context, ImportFileCallback, ImportFuncCallback);

REQUIRE( GetLastError() == 0 );
REQUIRE( success == true );

REQUIRE( context.ImportFuncCount != 0 );
REQUIRE( context.ImportCount != 0 );
}
}

TEST_CASE("DetourGetSizeOfPayloads", "[module]")
Expand Down