Skip to content

Commit

Permalink
Avoid using RtlImageNtHeader from the context of kdcom. This should f…
Browse files Browse the repository at this point in the history
…ix ReactOS support
  • Loading branch information
4d61726b committed Jun 23, 2024
1 parent a17c0ad commit 64d7596
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
47 changes: 43 additions & 4 deletions VirtualKD-Redux/Lib/kdvmguestlib/kdvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,47 @@ ULONG KdVMGetActiveCallCount()
return KdVmActiveCallCount;
}

extern "C" NTSYSAPI PIMAGE_NT_HEADERS NTAPI RtlImageNtHeader(IN PVOID ModuleAddress);
#include <ntimage.h>
static PIMAGE_NT_HEADERS ImageNtHeader(PVOID pModuleAddress)
{
PIMAGE_NT_HEADERS pNtHeaders = NULL;
PIMAGE_DOS_HEADER pDosHeader = (PIMAGE_DOS_HEADER)pModuleAddress;

__try
{
if (pDosHeader->e_magic != IMAGE_DOS_SIGNATURE)
{
__leave;
}

PIMAGE_NT_HEADERS pNtHeadersTmp = (PIMAGE_NT_HEADERS)((PUCHAR)pModuleAddress + pDosHeader->e_lfanew);
if (pNtHeadersTmp->Signature != IMAGE_NT_SIGNATURE ||
pNtHeadersTmp->OptionalHeader.Magic !=
#ifdef _WIN64
IMAGE_NT_OPTIONAL_HDR64_MAGIC
#else
IMAGE_NT_OPTIONAL_HDR32_MAGIC
#endif
||
pNtHeadersTmp->FileHeader.Machine !=
#ifdef _WIN64
IMAGE_FILE_MACHINE_AMD64
#else
IMAGE_FILE_MACHINE_I386
#endif
)
{
__leave;
}

pNtHeaders = pNtHeadersTmp;
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
}

return pNtHeaders;
}

static PVOID GetModuleBaseAddress(PVOID pAddr)
{
CHAR* pBase = (char*)(((ULONG_PTR)(void*)pAddr / PAGE_SIZE) * PAGE_SIZE);
Expand All @@ -72,7 +111,7 @@ static PVOID GetModuleBaseAddress(PVOID pAddr)
continue;
}

PIMAGE_NT_HEADERS pHeaders = RtlImageNtHeader(pCurAddr);
PIMAGE_NT_HEADERS pHeaders = ImageNtHeader(pCurAddr);
if (pHeaders)
{
return pCurAddr;
Expand Down Expand Up @@ -470,7 +509,7 @@ NTSTATUS __stdcall KdDebuggerInitialize0(PVOID lpLoaderParameterBlock)
NTSTATUS st;

PVOID pAddr = GetModuleBaseAddress(KdDebuggerInitialize0);
PIMAGE_NT_HEADERS pHeaders = RtlImageNtHeader(pAddr);
PIMAGE_NT_HEADERS pHeaders = ImageNtHeader(pAddr);
if (pHeaders)
{
//Prevent the current module from being relocated to a different address and breaking the physical/virtual address mapping
Expand Down
8 changes: 4 additions & 4 deletions VirtualKD-Redux/createbundles.bat
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ REM copy Build\Win32\Release\kdpatch.reg Bundles\ReleaseBundle\target32
REM copy Build\x64\Release\kdpatch.reg Bundles\ReleaseBundle\target64
copy Build\Win32\Release\vminstall.exe Bundles\ReleaseBundle\target32
copy Build\x64\Release\vminstall.exe Bundles\ReleaseBundle\target64
copy Build\Win32\Release\kdbazis.dll Bundles\ReleaseBundle\target32
copy Build\x64\Release\kdbazis.dll Bundles\ReleaseBundle\target64
copy Build\Win32\Release\kdbazis.dll Bundles\ReleaseBundle\target32\kdcom.dll
copy Build\x64\Release\kdbazis.dll Bundles\ReleaseBundle\target64\kdcom.dll
REM copy Build\Win32\Release\kdpatch.sys Bundles\ReleaseBundle\target32
REM copy Build\x64\Release\kdpatch.sys Bundles\ReleaseBundle\target64
copy Certs\VKD-Redux_CA.cer Bundles\ReleaseBundle\
Expand All @@ -34,8 +34,8 @@ REM copy Build\Win32\Debug\kdpatch.reg Bundles\DebugBundle\target32
REM copy Build\x64\Debug\kdpatch.reg Bundles\DebugBundle\target64
copy Build\Win32\Debug\vminstall.exe Bundles\DebugBundle\target32
copy Build\x64\Debug\vminstall.exe Bundles\DebugBundle\target64
copy Build\Win32\Debug\kdbazis.dll Bundles\DebugBundle\target32
copy Build\x64\Debug\kdbazis.dll Bundles\DebugBundle\target64
copy Build\Win32\Debug\kdbazis.dll Bundles\DebugBundle\target32\kdcom.dll
copy Build\x64\Debug\kdbazis.dll Bundles\DebugBundle\target64\kdcom.dll
REM copy Build\Win32\Debug\kdpatch.sys Bundles\DebugBundle\target32
REM copy Build\x64\Debug\kdpatch.sys Bundles\DebugBundle\target64
copy Certs\VKD-Redux_CA.cer Bundles\DebugBundle\

0 comments on commit 64d7596

Please sign in to comment.