Skip to content

Commit

Permalink
fix beyond-buffer access in PhGetRemoteMappedImageGuardFlagsEx (#2357)
Browse files Browse the repository at this point in the history
Some images (GOG GalaxyClient.exe) have LoadConfig directory smaller than size
provided inside `IMAGE_LOAD_CONFIG_DIRECTORY` structure.

We need to make sure to to access only valid part of buffer returned by
`PhGetRemoteMappedImageDirectoryEntry`.

Found using ApplicationVerifier.
  • Loading branch information
ge0rdi authored Jan 3, 2025
1 parent 417c944 commit 9cd7155
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions phlib/mapimg.c
Original file line number Diff line number Diff line change
Expand Up @@ -1360,18 +1360,19 @@ NTSTATUS PhGetRemoteMappedImageGuardFlagsEx(
if (RemoteMappedImage->Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)
{
PIMAGE_LOAD_CONFIG_DIRECTORY32 config32 = NULL;
ULONG config32Length = 0;

status = PhGetRemoteMappedImageDirectoryEntry(
RemoteMappedImage,
ReadVirtualMemoryCallback,
IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG,
&config32,
NULL
&config32Length
);

if (NT_SUCCESS(status))
{
if (RTL_CONTAINS_FIELD(config32, config32->Size, GuardFlags))
if (RTL_CONTAINS_FIELD(config32, min(config32->Size, config32Length), GuardFlags))
{
guardFlags = config32->GuardFlags;
}
Expand All @@ -1386,18 +1387,19 @@ NTSTATUS PhGetRemoteMappedImageGuardFlagsEx(
else
{
PIMAGE_LOAD_CONFIG_DIRECTORY64 config64 = NULL;
ULONG config64Length = 0;

status = PhGetRemoteMappedImageDirectoryEntry(
RemoteMappedImage,
ReadVirtualMemoryCallback,
IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG,
&config64,
NULL
&config64Length
);

if (NT_SUCCESS(status))
{
if (RTL_CONTAINS_FIELD(config64, config64->Size, GuardFlags))
if (RTL_CONTAINS_FIELD(config64, min(config64->Size, config64Length), GuardFlags))
{
guardFlags = config64->GuardFlags;
}
Expand Down

0 comments on commit 9cd7155

Please sign in to comment.