Skip to content

Commit

Permalink
Merge pull request #822 from avast/LZ_HighMemoryUsage_2
Browse files Browse the repository at this point in the history
* Check for invalid IAT directory
  • Loading branch information
s3rvac authored Jul 25, 2020
2 parents 235228a + 2132ddc commit 956b1a3
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 24 deletions.
2 changes: 1 addition & 1 deletion include/retdec/pelib/ImportDirectory.h
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ namespace PeLib
m_occupiedAddresses.back().second += 1;

// Push the import descriptor into the vector
vOldIidCurr.push_back(iidCurr);
vOldIidCurr.push_back(std::move(iidCurr));
}

// Space occupied by import descriptors
Expand Down
15 changes: 0 additions & 15 deletions include/retdec/pelib/PeLibAux.h
Original file line number Diff line number Diff line change
Expand Up @@ -623,21 +623,6 @@ namespace PeLib
static inline std::size_t size() {return 8;}
};

template<int>
struct FieldSizes;

template<>
struct FieldSizes<32>
{
typedef std::uint32_t VAR4_8;
};

template<>
struct FieldSizes<64>
{
typedef std::uint64_t VAR4_8;
};

struct PELIB_IMAGE_OPTIONAL_HEADER32
{
std::uint16_t Magic;
Expand Down
9 changes: 6 additions & 3 deletions src/pelib/BoundImportDirectory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,13 @@ namespace PeLib
std::uint32_t importSize = imageLoader.getDataDirSize(PELIB_IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT);
std::uint32_t sizeOfImage = imageLoader.getSizeOfImage();

if(importRva >= sizeOfImage || (importRva + importSize) >= sizeOfImage)
{
// Refuse to load blatantly invalid bound import directory
if(importSize & 0xFF000000)
return ERROR_INVALID_FILE;

// Refuse to load too large import directories
if((importRva + importSize) < importRva || importRva >= sizeOfImage || (importRva + importSize) >= sizeOfImage)
return ERROR_INVALID_FILE;
}

std::vector<unsigned char> vBimpDir(importSize);
imageLoader.readImage(reinterpret_cast<char*>(vBimpDir.data()), importRva, importSize);
Expand Down
10 changes: 6 additions & 4 deletions src/pelib/IatDirectory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ namespace PeLib
std::uint32_t sizeOfImage = imageLoader.getSizeOfImage();
int fileError = ERROR_NONE;

// Check whether the IAT is outside the image
if(iatRva >= sizeOfImage)
{
// Refuse to load blatantly invalid IAT
if(iatSize & 0xFF000000)
return ERROR_INVALID_FILE;

// Refuse to load too large IAT directories
if((iatRva + iatSize) < iatRva || iatRva >= sizeOfImage || (iatRva + iatSize) >= sizeOfImage)
return ERROR_INVALID_FILE;
}

// Trim the array size to the size of image
if((iatRva + iatSize) > sizeOfImage)
Expand Down
2 changes: 1 addition & 1 deletion src/pelib/RelocationsDirectory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ namespace PeLib
}

// Push the data to the relocations vector
m_vRelocations.push_back(ibrCurr);
m_vRelocations.push_back(std::move(ibrCurr));
}
}
}
Expand Down

0 comments on commit 956b1a3

Please sign in to comment.