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

Fixed ImageLoader::Save() #1029

Merged
merged 4 commits into from
Oct 26, 2021
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fixed out-of-bounds read
  • Loading branch information
Ladislav Zezula committed Oct 9, 2021
commit a00a6631a4a83cc923bc31371e53596d2ea97e5a
38 changes: 20 additions & 18 deletions src/pelib/ImageLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1146,29 +1146,31 @@ std::uint32_t PeLib::ImageLoader::readWriteImage(
if(rva < rvaEnd)
{
std::uint8_t * bufferPtr = static_cast<std::uint8_t *>(buffer);
std::size_t pageIndex = rva / PELIB_PAGE_SIZE;

// The page index must be in range
if(pageIndex < pages.size())
while(rva < rvaEnd)
{
while(rva < rvaEnd)
{
PELIB_FILE_PAGE & page = pages[pageIndex++];
std::uint32_t offsetInPage = rva & (PELIB_PAGE_SIZE - 1);
std::uint32_t bytesInPage = PELIB_PAGE_SIZE - offsetInPage;

// Perhaps the last page loaded?
if(bytesInPage > (rvaEnd - rva))
bytesInPage = (rvaEnd - rva);
std::uint32_t offsetInPage = rva & (PELIB_PAGE_SIZE - 1);
std::uint32_t bytesInPage = PELIB_PAGE_SIZE - offsetInPage;
std::size_t pageIndex = rva / PELIB_PAGE_SIZE;

// Perform the read/write operation
ReadWrite(page, bufferPtr, offsetInPage, bytesInPage);
// Perhaps the last page loaded?
if(bytesInPage > (rvaEnd - rva))
bytesInPage = (rvaEnd - rva);

// Move pointers
bufferPtr += bytesInPage;
bytesRead += bytesInPage;
rva += bytesInPage;
// The page index must be in range
if(pageIndex < pages.size())
{
ReadWrite(pages[pageIndex], bufferPtr, offsetInPage, bytesInPage);
}
else
{
memset(bufferPtr, 0, bytesInPage);
}

// Move pointers
bufferPtr += bytesInPage;
bytesRead += bytesInPage;
rva += bytesInPage;
}
}

Expand Down