Skip to content

Commit 9764ceb

Browse files
mooooonlightg00356712BissetJ
authored
Fix context setting errors in DetourEnumerateImports API (#266)
* fix context setting errors in DetourEnumerateImports API * Fix SAL annotation on DetouerEnumerateImportThunk SAL annotation for VoidContext should just be In rather than In_opt (this is consistent with how the existing DetouerEnumerateImportThunk is structured). This was causing an error in the static analysis checks in the CI build. * Update test_module_api.cpp Comparison of BOOL vs. bool, this should fix it. --------- Co-authored-by: g00356712 <[email protected]> Co-authored-by: Jeff Bisset <[email protected]>
1 parent ae57e62 commit 9764ceb

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

src/modules.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,7 @@ BOOL WINAPI DetourEnumerateImportsEx(_In_opt_ HMODULE hModule,
645645
struct _DETOUR_ENUMERATE_IMPORTS_THUNK_CONTEXT
646646
{
647647
PVOID pContext;
648+
PF_DETOUR_IMPORT_FILE_CALLBACK pfImportFile;
648649
PF_DETOUR_IMPORT_FUNC_CALLBACK pfImportFunc;
649650
};
650651

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

668+
static
669+
BOOL
670+
CALLBACK
671+
DetourEnumerateImportsFile(_In_ PVOID VoidContext,
672+
_In_opt_ HMODULE hModule,
673+
_In_opt_ LPCSTR pszFile)
674+
{
675+
_DETOUR_ENUMERATE_IMPORTS_THUNK_CONTEXT const * const
676+
pContext = (_DETOUR_ENUMERATE_IMPORTS_THUNK_CONTEXT*)VoidContext;
677+
return pContext->pfImportFile(pContext->pContext, hModule, pszFile);
678+
}
679+
680+
667681
BOOL WINAPI DetourEnumerateImports(_In_opt_ HMODULE hModule,
668682
_In_opt_ PVOID pContext,
669683
_In_opt_ PF_DETOUR_IMPORT_FILE_CALLBACK pfImportFile,
@@ -674,11 +688,10 @@ BOOL WINAPI DetourEnumerateImports(_In_opt_ HMODULE hModule,
674688
return FALSE;
675689
}
676690

677-
_DETOUR_ENUMERATE_IMPORTS_THUNK_CONTEXT const context = { pContext, pfImportFunc };
678-
691+
_DETOUR_ENUMERATE_IMPORTS_THUNK_CONTEXT const context = { pContext, pfImportFile, pfImportFunc };
679692
return DetourEnumerateImportsEx(hModule,
680693
(PVOID)&context,
681-
pfImportFile,
694+
&DetourEnumerateImportsFile,
682695
&DetourEnumerateImportsThunk);
683696
}
684697

tests/test_module_api.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,9 @@ BOOL WINAPI ImportFileCallback(PVOID pContext, HMODULE, PCSTR pszFile)
506506
reinterpret_cast<EnumerateImportsTestContext*>(pContext);
507507

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

511513
return TRUE;
512514
}
@@ -568,6 +570,20 @@ TEST_CASE("DetourEnumerateImports", "[module]")
568570
REQUIRE( context.ImportFuncCount == 0 );
569571
REQUIRE_FALSE( context.ImportFuncFound );
570572
}
573+
574+
SECTION("The context transferred during the input parameter is the same as the context parsed in the callback.")
575+
{
576+
SetLastError(ERROR_INVALID_HANDLE);
577+
578+
EnumerateImportsTestContext context {};
579+
auto success = DetourEnumerateImports(NULL, &context, ImportFileCallback, ImportFuncCallback);
580+
581+
REQUIRE( GetLastError() == 0 );
582+
REQUIRE( success );
583+
584+
REQUIRE( context.ImportFuncCount != 0 );
585+
REQUIRE( context.ImportCount != 0 );
586+
}
571587
}
572588

573589
TEST_CASE("DetourGetSizeOfPayloads", "[module]")

0 commit comments

Comments
 (0)