Blame SOURCES/0032-CVE-2022-2867-CVE-2022-2868-tiffcrop.c-Fix-issue-352.patch

df59fa
From 5d214a07db3bb8dcea8354d8f1e52f9c46264acb Mon Sep 17 00:00:00 2001
df59fa
From: Su Laus <sulau@freenet.de>
df59fa
Date: Wed, 9 Feb 2022 21:31:29 +0000
df59fa
Subject: [PATCH] (CVE-2022-2867 CVE-2022-2868) tiffcrop.c: Fix issue #352
df59fa
 heap-buffer-overflow by correcting uint32_t underflow.
df59fa
df59fa
(cherry picked from commit 07d79fcac2ead271b60e32aeb80f7b4f3be9ac8c)
df59fa
---
df59fa
 tools/tiffcrop.c | 81 +++++++++++++++++++++++++++++++-----------------
df59fa
 1 file changed, 53 insertions(+), 28 deletions(-)
df59fa
df59fa
diff --git a/tools/tiffcrop.c b/tools/tiffcrop.c
df59fa
index ea0b98be..5801b8f6 100644
df59fa
--- a/tools/tiffcrop.c
df59fa
+++ b/tools/tiffcrop.c
df59fa
@@ -5152,29 +5152,45 @@ computeInputPixelOffsets(struct crop_mask *crop, struct image_data *image,
df59fa
 	y1 = _TIFFClampDoubleToUInt32(crop->corners[i].Y1);
df59fa
 	y2 = _TIFFClampDoubleToUInt32(crop->corners[i].Y2);
df59fa
 	}
df59fa
-      if (x1 < 1)
df59fa
-        crop->regionlist[i].x1 = 0;
df59fa
-      else
df59fa
-        crop->regionlist[i].x1 = (uint32) (x1 - 1);
df59fa
+      /* a) Region needs to be within image sizes 0.. width-1; 0..length-1 
df59fa
+       * b) Corners are expected to be submitted as top-left to bottom-right.
df59fa
+       *    Therefore, check that and reorder input.
df59fa
+       * (be aware x,y are already casted to (uint32_t) and avoid (0 - 1) )
df59fa
+       */
df59fa
+      uint32 aux;
df59fa
+      if (x1 > x2) {
df59fa
+        aux = x1;
df59fa
+        x1 = x2;
df59fa
+        x2 = aux;
df59fa
+      }
df59fa
+      if (y1 > y2) {
df59fa
+        aux = y1;
df59fa
+        y1 = y2;
df59fa
+        y2 = aux;
df59fa
+      }
df59fa
+      if (x1 > image->width - 1)
df59fa
+        crop->regionlist[i].x1 = image->width - 1;
df59fa
+      else if (x1 > 0)
df59fa
+        crop->regionlist[i].x1 = (uint32)(x1 - 1);
df59fa
 
df59fa
       if (x2 > image->width - 1)
df59fa
         crop->regionlist[i].x2 = image->width - 1;
df59fa
-      else
df59fa
-        crop->regionlist[i].x2 = (uint32) (x2 - 1);
df59fa
-      zwidth  = crop->regionlist[i].x2 - crop->regionlist[i].x1 + 1; 
df59fa
+      else if (x2 > 0)
df59fa
+        crop->regionlist[i].x2 = (uint32)(x2 - 1);
df59fa
 
df59fa
-      if (y1 < 1)
df59fa
-        crop->regionlist[i].y1 = 0;
df59fa
-      else
df59fa
-        crop->regionlist[i].y1 = (uint32) (y1 - 1);
df59fa
+      zwidth = crop->regionlist[i].x2 - crop->regionlist[i].x1 + 1;
df59fa
+
df59fa
+      if (y1 > image->length - 1)
df59fa
+        crop->regionlist[i].y1 = image->length - 1;
df59fa
+      else if (y1 > 0)
df59fa
+        crop->regionlist[i].y1 = (uint32)(y1 - 1);
df59fa
 
df59fa
       if (y2 > image->length - 1)
df59fa
         crop->regionlist[i].y2 = image->length - 1;
df59fa
-      else
df59fa
-        crop->regionlist[i].y2 = (uint32) (y2 - 1);
df59fa
-
df59fa
-      zlength = crop->regionlist[i].y2 - crop->regionlist[i].y1 + 1; 
df59fa
+      else if (y2 > 0)
df59fa
+        crop->regionlist[i].y2 = (uint32)(y2 - 1);
df59fa
 
df59fa
+      zlength = crop->regionlist[i].y2 - crop->regionlist[i].y1 + 1;
df59fa
       if (zwidth > max_width)
df59fa
         max_width = zwidth;
df59fa
       if (zlength > max_length)
df59fa
@@ -5204,7 +5220,7 @@ computeInputPixelOffsets(struct crop_mask *crop, struct image_data *image,
df59fa
 	}
df59fa
       }
df59fa
     return (0);
df59fa
-    }
df59fa
+    }  /* crop_mode == CROP_REGIONS */
df59fa
   
df59fa
   /* Convert crop margins into offsets into image
df59fa
    * Margins are expressed as pixel rows and columns, not bytes
df59fa
@@ -5240,7 +5256,7 @@ computeInputPixelOffsets(struct crop_mask *crop, struct image_data *image,
df59fa
       bmargin = (uint32) 0;
df59fa
       return (-1);
df59fa
       }
df59fa
-    }
df59fa
+    }  /* crop_mode == CROP_MARGINS */
df59fa
   else
df59fa
     { /* no margins requested */
df59fa
     tmargin = (uint32) 0;
df59fa
@@ -5331,24 +5347,23 @@ computeInputPixelOffsets(struct crop_mask *crop, struct image_data *image,
df59fa
   off->endx   = endx;
df59fa
   off->endy   = endy;
df59fa
 
df59fa
-  crop_width  = endx - startx + 1;
df59fa
-  crop_length = endy - starty + 1;
df59fa
-
df59fa
-  if (crop_width <= 0)
df59fa
+  if (endx + 1 <= startx)
df59fa
     {
df59fa
     TIFFError("computeInputPixelOffsets", 
df59fa
                "Invalid left/right margins and /or image crop width requested");
df59fa
     return (-1);
df59fa
     }
df59fa
+  crop_width  = endx - startx + 1;
df59fa
   if (crop_width > image->width)
df59fa
     crop_width = image->width;
df59fa
 
df59fa
-  if (crop_length <= 0)
df59fa
+  if (endy + 1 <= starty)
df59fa
     {
df59fa
     TIFFError("computeInputPixelOffsets", 
df59fa
               "Invalid top/bottom margins and /or image crop length requested");
df59fa
     return (-1);
df59fa
     }
df59fa
+  crop_length = endy - starty + 1;
df59fa
   if (crop_length > image->length)
df59fa
     crop_length = image->length;
df59fa
 
df59fa
@@ -5448,10 +5463,17 @@ getCropOffsets(struct image_data *image, struct crop_mask *crop, struct dump_opt
df59fa
   else
df59fa
     crop->selections = crop->zones;
df59fa
 
df59fa
-  for (i = 0; i < crop->zones; i++)
df59fa
+  /* Initialize regions iterator i */
df59fa
+  i = 0;
df59fa
+  for (int j = 0; j < crop->zones; j++)
df59fa
     {
df59fa
-    seg = crop->zonelist[i].position;
df59fa
-    total = crop->zonelist[i].total;
df59fa
+    seg = crop->zonelist[j].position;
df59fa
+    total = crop->zonelist[j].total;
df59fa
+
df59fa
+    /* check for not allowed zone cases like 0:0; 4:3; etc. and skip that input */
df59fa
+    if (seg == 0 || total == 0 || seg > total) {
df59fa
+        continue;
df59fa
+    }
df59fa
 
df59fa
     switch (crop->edge_ref) 
df59fa
       {
df59fa
@@ -5578,10 +5600,13 @@ getCropOffsets(struct image_data *image, struct crop_mask *crop, struct dump_opt
df59fa
   if (dump->outfile != NULL)
df59fa
     dump_info (dump->outfile, dump->format, "",  "Zone %d, width: %4d, length: %4d, x1: %4d  x2: %4d  y1: %4d  y2: %4d",
df59fa
                     i + 1, (uint32)zwidth, (uint32)zlength,
df59fa
-		    crop->regionlist[i].x1, crop->regionlist[i].x2, 
df59fa
-                    crop->regionlist[i].y1, crop->regionlist[i].y2);
df59fa
+               crop->regionlist[i].x1, crop->regionlist[i].x2,
df59fa
+               crop->regionlist[i].y1, crop->regionlist[i].y2);
df59fa
+  /* increment regions iterator */
df59fa
+  i++;
df59fa
     }
df59fa
-
df59fa
+    /* set number of generated regions out of given zones */
df59fa
+    crop->selections = i;
df59fa
   return (0);
df59fa
   } /* end getCropOffsets */
df59fa