Blame SOURCES/exiv2-CVE-2021-3482.patch

991155
From 22ea582c6b74ada30bec3a6b15de3c3e52f2b4da Mon Sep 17 00:00:00 2001
991155
From: Robin Mills <robin@clanmills.com>
991155
Date: Mon, 5 Apr 2021 20:33:25 +0100
991155
Subject: [PATCH] fix_1522_jp2image_exif_asan
991155
991155
---
991155
 src/jp2image.cpp | 9 ++++++---
991155
 1 file changed, 6 insertions(+), 3 deletions(-)
991155
991155
diff --git a/src/jp2image.cpp b/src/jp2image.cpp
991155
index eb31cea4a..88ab9b2d6 100644
991155
--- a/src/jp2image.cpp
991155
+++ b/src/jp2image.cpp
991155
@@ -28,6 +28,7 @@
991155
 #include "image.hpp"
991155
 #include "image_int.hpp"
991155
 #include "basicio.hpp"
991155
+#include "enforce.hpp"
991155
 #include "error.hpp"
991155
 #include "futils.hpp"
991155
 #include "types.hpp"
991155
@@ -353,7 +354,7 @@ static void boxes_check(size_t b,size_t m)
991155
                             if (io_->error()) throw Error(kerFailedToReadImageData);
991155
                             if (bufRead != rawData.size_) throw Error(kerInputDataReadFailed);
991155
 
991155
-                            if (rawData.size_ > 0)
991155
+                            if (rawData.size_ > 8) // "II*\0long"
991155
                             {
991155
                                 // Find the position of Exif header in bytes array.
991155
                                 long pos = (     (rawData.pData_[0]      == rawData.pData_[1])
991155
@@ -497,6 +498,7 @@ static void boxes_check(size_t b,size_t m)
991155
                 position   = io_->tell();
991155
                 box.length = getLong((byte*)&box.length, bigEndian);
991155
                 box.type = getLong((byte*)&box.type, bigEndian);
991155
+                enforce(box.length <= io_->size()-io_->tell() , Exiv2::kerCorruptedMetadata);
991155
 
991155
                 if (bPrint) {
991155
                     out << Internal::stringFormat("%8ld | %8ld | ", (size_t)(position - sizeof(box)),
991155
@@ -581,12 +583,13 @@ static void boxes_check(size_t b,size_t m)
991155
                                 throw Error(kerInputDataReadFailed);
991155
 
991155
                             if (bPrint) {
991155
-                                out << Internal::binaryToString(makeSlice(rawData, 0, 40));
991155
+                                out << Internal::binaryToString(
991155
+                                        makeSlice(rawData, 0, rawData.size_>40?40:rawData.size_));
991155
                                 out.flush();
991155
                             }
991155
                             lf(out, bLF);
991155
 
991155
-                            if (bIsExif && bRecursive && rawData.size_ > 0) {
991155
+                            if (bIsExif && bRecursive && rawData.size_ > 8) { // "II*\0long"
991155
                                 if ((rawData.pData_[0] == rawData.pData_[1]) &&
991155
                                     (rawData.pData_[0] == 'I' || rawData.pData_[0] == 'M')) {
991155
                                     BasicIo::AutoPtr p = BasicIo::AutoPtr(new MemIo(rawData.pData_, rawData.size_));
991155