Skip to content

Commit

Permalink
Add check for DataBuf.size_ in Jp2Image::readMetadata()
Browse files Browse the repository at this point in the history
When parsing a subBox that is a ColorHeader, a length is extracted
from the input file and fed directly into DataBuf() (which calls
malloc). A crafted input file can provide arbitrarily (up to
max(uint32_t)-8) large values and result in excessive memory
allocation.

This commit adds a check for the new size of DataBuf so that it is not
larger than the remaining size of the file.

This fixes Exiv2#202 aka CVE-2018-4868
  • Loading branch information
D4N authored and a17r committed Apr 25, 2018
1 parent 876b131 commit ce4f575
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/jp2image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,12 @@ namespace Exiv2
#endif

const long pad = 3 ; // 3 padding bytes 2 0 0
DataBuf data(Safe::add(subBox.length, static_cast<uint32_t>(8)));
const size_t data_length = Safe::add(subBox.length, static_cast<uint32_t>(8));
// data_length makes no sense if it is larger than the rest of the file
if (data_length > io_->size() - io_->tell()) {
throw Error(58);
}
DataBuf data(data_length);
io_->read(data.pData_,data.size_);
const long iccLength = getULong(data.pData_+pad, bigEndian);
// subtracting pad from data.size_ is safe:
Expand Down

0 comments on commit ce4f575

Please sign in to comment.