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 issue #872 #873

Merged
merged 1 commit into from
Oct 21, 2020
Merged
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
6 changes: 6 additions & 0 deletions include/retdec/pelib/ImageLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,11 @@ class ImageLoader
return (sectionIndex < sections.size()) ? &sections[sectionIndex] : nullptr;
s3rvac marked this conversation as resolved.
Show resolved Hide resolved
}

std::uint64_t getSizeOfFile() const
{
return fileSize;
}

std::uint64_t getOrdinalMask() const
{
return (uint64_t)1 << (getImageBitability() - 1);
Expand Down Expand Up @@ -446,6 +451,7 @@ class ImageLoader
PELIB_IMAGE_OPTIONAL_HEADER optionalHeader; // 32/64-bit optional header
ByteBuffer rawFileData; // Loaded content of the image in case it couldn't have been mapped
LoaderError ldrError;
std::uint64_t fileSize; // Size of the raw file
std::uint32_t windowsBuildNumber;
std::uint32_t ntSignature;
std::uint32_t maxSectionCount;
Expand Down
3 changes: 3 additions & 0 deletions src/pelib/ImageLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,9 @@ int PeLib::ImageLoader::Load(
{
int fileError;

// Remember the size of the file for later use
fileSize = fileData.size();

// Check and capture DOS header
fileError = captureDosHeader(fileData);
if(fileError != ERROR_NONE)
Expand Down
11 changes: 10 additions & 1 deletion src/pelib/RelocationsDirectory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace PeLib
std::uint32_t rva = imageLoader.getDataDirRva(PELIB_IMAGE_DIRECTORY_ENTRY_BASERELOC);
std::uint32_t size = imageLoader.getDataDirSize(PELIB_IMAGE_DIRECTORY_ENTRY_BASERELOC);
std::uint32_t sizeOfImage = imageLoader.getSizeOfImage();
std::uint64_t sizeOfFile = imageLoader.getSizeOfFile();

// Check for relocations out of image
if(rva >= sizeOfImage || (rva + size) < rva || (rva + size) > sizeOfImage)
Expand All @@ -34,9 +35,17 @@ namespace PeLib
return ERROR_INVALID_FILE;
}

// Check for relocations out of file
if(size > sizeOfFile)
{
RelocationsDirectory::setLoaderError(LDR_ERROR_RELOCATIONS_OUT_OF_IMAGE);
return ERROR_INVALID_FILE;
}

// Read the entire relocation directory from the image
std::vector<std::uint8_t> vRelocDirectory(size);
imageLoader.readImage(vRelocDirectory.data(), rva, size);
if(imageLoader.readImage(vRelocDirectory.data(), rva, size) != size)
return ERROR_INVALID_FILE;

// Parse the relocations directory
read(vRelocDirectory.data(), size, sizeOfImage);
Expand Down