Blame SOURCES/exiv2-CVE-2019-20421.patch

14512e
From 1b917c3f7dd86336a9f6fda4456422c419dfe88c Mon Sep 17 00:00:00 2001
14512e
From: clanmills <robin@clanmills.com>
14512e
Date: Tue, 1 Oct 2019 17:39:44 +0100
14512e
Subject: [PATCH] Fix #1011 fix_1011_jp2_readmetadata_loop
14512e
14512e
---
14512e
 src/jp2image.cpp                             |  25 +++++++++++++++----
14512e
 test/data/Jp2Image_readMetadata_loop.poc     | Bin 0 -> 738 bytes
14512e
 tests/bugfixes/github/test_CVE_2017_17725.py |   4 +--
14512e
 tests/bugfixes/github/test_issue_1011.py     |  13 ++++++++++
14512e
 4 files changed, 35 insertions(+), 7 deletions(-)
14512e
 create mode 100755 test/data/Jp2Image_readMetadata_loop.poc
14512e
 create mode 100644 tests/bugfixes/github/test_issue_1011.py
14512e
14512e
diff --git a/src/jp2image.cpp b/src/jp2image.cpp
14512e
index d5cd1340a..0de088d62 100644
14512e
--- a/src/jp2image.cpp
14512e
+++ b/src/jp2image.cpp
14512e
@@ -18,10 +18,6 @@
14512e
  * Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
14512e
  */
14512e
14512e
-/*
14512e
-  File:      jp2image.cpp
14512e
-*/
14512e
-
14512e
 // *****************************************************************************
14512e
14512e
 // included header files
14512e
@@ -197,6 +193,16 @@ namespace Exiv2
14512e
         return result;
14512e
     }
14512e
14512e
+static void boxes_check(size_t b,size_t m)
14512e
+{
14512e
+    if ( b > m ) {
14512e
+#ifdef EXIV2_DEBUG_MESSAGES
14512e
+        std::cout << "Exiv2::Jp2Image::readMetadata box maximum exceeded" << std::endl;
14512e
+#endif
14512e
+        throw Error(kerCorruptedMetadata);
14512e
+    }
14512e
+}
14512e
+
14512e
     void Jp2Image::readMetadata()
14512e
     {
14512e
 #ifdef EXIV2_DEBUG_MESSAGES
14512e
@@ -219,9 +225,12 @@ namespace Exiv2
14512e
         Jp2BoxHeader      subBox    = {0,0};
14512e
         Jp2ImageHeaderBox ihdr      = {0,0,0,0,0,0,0,0};
14512e
         Jp2UuidBox        uuid      = {{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
14512e
+        size_t            boxes     = 0 ;
14512e
+        size_t            boxem     = 1000 ; // boxes max
14512e
14512e
         while (io_->read((byte*)&box, sizeof(box)) == sizeof(box))
14512e
         {
14512e
+            boxes_check(boxes++,boxem );
14512e
             position   = io_->tell();
14512e
             box.length = getLong((byte*)&box.length, bigEndian);
14512e
             box.type   = getLong((byte*)&box.type, bigEndian);
14512e
@@ -251,8 +260,12 @@ namespace Exiv2
14512e
14512e
                     while (io_->read((byte*)&subBox, sizeof(subBox)) == sizeof(subBox) && subBox.length )
14512e
                     {
14512e
+                        boxes_check(boxes++, boxem) ;
14512e
                         subBox.length = getLong((byte*)&subBox.length, bigEndian);
14512e
                         subBox.type   = getLong((byte*)&subBox.type, bigEndian);
14512e
+                        if (subBox.length > io_->size() ) {
14512e
+                            throw Error(kerCorruptedMetadata);
14512e
+                        }
14512e
 #ifdef EXIV2_DEBUG_MESSAGES
14512e
                         std::cout << "Exiv2::Jp2Image::readMetadata: "
14512e
                         << "subBox = " << toAscii(subBox.type) << " length = " << subBox.length << std::endl;
14512e
@@ -308,7 +321,9 @@ namespace Exiv2
14512e
                         }
14512e
14512e
                         io_->seek(restore,BasicIo::beg);
14512e
-                        io_->seek(subBox.length, Exiv2::BasicIo::cur);
14512e
+                        if ( io_->seek(subBox.length, Exiv2::BasicIo::cur) != 0 ) {
14512e
+                            throw Error(kerCorruptedMetadata);
14512e
+                        }
14512e
                         restore = io_->tell();
14512e
                     }
14512e
                     break;
14512e
diff --git a/tests/bugfixes/github/test_CVE_2017_17725.py b/tests/bugfixes/github/test_CVE_2017_17725.py
14512e
index 1127b9806..670a75d8d 100644
14512e
--- a/tests/bugfixes/github/test_CVE_2017_17725.py
14512e
+++ b/tests/bugfixes/github/test_CVE_2017_17725.py
14512e
@@ -11,7 +11,7 @@ class TestCvePoC(metaclass=system_tests.CaseMeta):
14512e
     filename = "$data_path/poc_2017-12-12_issue188"
14512e
     commands = ["$exiv2 " + filename]
14512e
     stdout = [""]
14512e
-    stderr = ["""$exiv2_overflow_exception_message """ + filename + """:
14512e
-$addition_overflow_message
14512e
+    stderr = ["""$exiv2_exception_message """ + filename + """:
14512e
+$kerCorruptedMetadata
14512e
 """]
14512e
     retval = [1]