Blame SOURCES/exiv2-wrong-brackets.patch

240d3a
From 1e07c98dfcbd8ac10ee02088f08235f5e1700148 Mon Sep 17 00:00:00 2001
240d3a
From: =?UTF-8?q?Dan=20=C4=8Cerm=C3=A1k?= <dan.cermak@cgc-instruments.com>
240d3a
Date: Wed, 27 Sep 2017 23:38:49 +0200
240d3a
Subject: Fixed wrong brackets: size*count + pad can overflow before the cast
240d3a
240d3a
=> Should fix #76 (most of the work has been done by Robin Mills in
240d3a
   6e3855aed7ba8bb4731fc4087ca7f9078b2f3d97)
240d3a
240d3a
The problem with #76 is the contents of the 26th IFD, with the
240d3a
following contents:
240d3a
tag: 0x8649
240d3a
type: 0x1
240d3a
count: 0xffff ffff
240d3a
offset: 0x4974
240d3a
240d3a
The issue is the size of count (uint32_t), as adding anything to it
240d3a
causes an overflow. Especially the expression:
240d3a
(size*count + pad+20)
240d3a
results in an overflow and gives 20 as a result instead of
240d3a
0x100000014, thus the condition in the if in the next line is false
240d3a
and the program continues to run (until it crashes at io.read).
240d3a
240d3a
To properly account for the overflow, the brackets have to be removed,
240d3a
as then the result is saved in the correctly sized type and not cast
240d3a
after being calculated in the smaller type.
240d3a
240d3a
diff --git a/src/image.cpp b/src/image.cpp
240d3a
index ec5b873e..199671b9 100644
240d3a
--- a/src/image.cpp
240d3a
+++ b/src/image.cpp
240d3a
@@ -401,7 +401,7 @@ namespace Exiv2 {
240d3a
                 // if ( offset > io.size() ) offset = 0; // Denial of service?
240d3a
 
240d3a
                 // #55 memory allocation crash test/data/POC8
240d3a
-                long long allocate = (long long) (size*count + pad+20);
240d3a
+                long long allocate = (long long) size*count + pad+20;
240d3a
                 if ( allocate > (long long) io.size() ) {
240d3a
                     throw Error(57);
240d3a
                 }