diff --git a/src/crwimage_int.cpp b/src/crwimage_int.cpp index c2fd5f3a..4080c078 100644 --- a/src/crwimage_int.cpp +++ b/src/crwimage_int.cpp @@ -281,7 +281,7 @@ namespace Exiv2 { if (size < 4) throw Error(kerCorruptedMetadata); uint32_t o = getULong(pData + size - 4, byteOrder); - if ( o+2 > size ) + if ( o > size-2 ) throw Error(kerCorruptedMetadata); uint16_t count = getUShort(pData + o, byteOrder); #ifdef DEBUG @@ -289,7 +289,7 @@ namespace Exiv2 { <<", " << count << " entries \n"; #endif o += 2; - if ( (o + (count * 10)) > size ) + if ( static_cast(count) * 10 > size-o ) throw Error(kerCorruptedMetadata); for (uint16_t i = 0; i < count; ++i) { diff --git a/test/data/issue_843_poc.crw b/test/data/issue_843_poc.crw new file mode 100644 index 00000000..56628e2d Binary files /dev/null and b/test/data/issue_843_poc.crw differ diff --git a/tests/bugfixes/github/test_issue_843.py b/tests/bugfixes/github/test_issue_843.py new file mode 100644 index 00000000..2df9c1cf --- /dev/null +++ b/tests/bugfixes/github/test_issue_843.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- + +from system_tests import CaseMeta, path + + +class IntegerOverflowInCiffDirectoryReadDirectory(metaclass=CaseMeta): + """ + Regression test for the bug described in: + https://github.com/Exiv2/exiv2/issues/843 + + An integer overflow causes an out-of-bounds read. + """ + url = "https://github.com/Exiv2/exiv2/issues/843" + + filename = path("$data_path/issue_843_poc.crw") + commands = ["$exiv2 $filename"] + stdout = [""] + stderr = [ + """$exiv2_exception_message $filename: +$kerCorruptedMetadata +"""] + retval = [1]