Blame SOURCES/0025-CVE-2022-0891-tiffcrop-fix-issue-380-and-382-heap-bu.patch

edc570
From 465c2d93e2a2d20ac4844ad0d98b35f00e8063fb Mon Sep 17 00:00:00 2001
edc570
From: Su Laus <sulau@freenet.de>
edc570
Date: Tue, 8 Mar 2022 17:02:44 +0000
edc570
Subject: [PATCH] (CVE-2022-0891) tiffcrop: fix issue #380 and #382 heap buffer
edc570
 overflow in extractImageSection
edc570
edc570
(cherry picked from commit 232282fd8f9c21eefe8d2d2b96cdbbb172fe7b7c)
edc570
---
edc570
 tools/tiffcrop.c | 84 ++++++++++++++++++------------------------------
edc570
 1 file changed, 32 insertions(+), 52 deletions(-)
edc570
edc570
diff --git a/tools/tiffcrop.c b/tools/tiffcrop.c
edc570
index a6129148..83cf80ad 100644
edc570
--- a/tools/tiffcrop.c
edc570
+++ b/tools/tiffcrop.c
edc570
@@ -6668,10 +6668,10 @@ extractImageSection(struct image_data *image, struct pageseg *section,
edc570
 #ifdef DEVELMODE
edc570
   uint32    img_length;
edc570
 #endif
edc570
-  uint32    j, shift1, shift2, trailing_bits;
edc570
+  uint32    j, shift1, trailing_bits;
edc570
   uint32    row, first_row, last_row, first_col, last_col;
edc570
   uint32    src_offset, dst_offset, row_offset, col_offset;
edc570
-  uint32    offset1, offset2, full_bytes;
edc570
+  uint32    offset1, full_bytes;
edc570
   uint32    sect_width;
edc570
 #ifdef DEVELMODE
edc570
   uint32    sect_length;
edc570
@@ -6681,7 +6681,6 @@ extractImageSection(struct image_data *image, struct pageseg *section,
edc570
 #ifdef DEVELMODE
edc570
   int      k;
edc570
   unsigned char bitset;
edc570
-  static char *bitarray = NULL;
edc570
 #endif
edc570
 
edc570
   img_width = image->width;
edc570
@@ -6699,17 +6698,12 @@ extractImageSection(struct image_data *image, struct pageseg *section,
edc570
   dst_offset = 0;
edc570
 
edc570
 #ifdef DEVELMODE
edc570
-  if (bitarray == NULL)
edc570
-    {
edc570
-    if ((bitarray = (char *)malloc(img_width)) == NULL)
edc570
-      {
edc570
-      TIFFError ("", "DEBUG: Unable to allocate debugging bitarray");
edc570
-      return (-1);
edc570
-      }
edc570
-    }
edc570
+  char bitarray[39];
edc570
 #endif
edc570
 
edc570
-  /* rows, columns, width, length are expressed in pixels */
edc570
+  /* rows, columns, width, length are expressed in pixels
edc570
+   * first_row, last_row, .. are index into image array starting at 0 to width-1,
edc570
+   * last_col shall be also extracted.  */
edc570
   first_row = section->y1;
edc570
   last_row  = section->y2;
edc570
   first_col = section->x1;
edc570
@@ -6719,9 +6713,14 @@ extractImageSection(struct image_data *image, struct pageseg *section,
edc570
 #ifdef DEVELMODE
edc570
   sect_length = last_row - first_row + 1;
edc570
 #endif
edc570
-  img_rowsize = ((img_width * bps + 7) / 8) * spp;
edc570
+    /* The read function loadImage() used copy separate plane data into a buffer as interleaved
edc570
+     * samples rather than separate planes so the same logic works to extract regions
edc570
+     * regardless of the way the data are organized in the input file.
edc570
+     * Furthermore, bytes and bits are arranged in buffer according to COMPRESSION=1 and FILLORDER=1 
edc570
+     */
edc570
+    img_rowsize = (((img_width * spp * bps) + 7) / 8);    /* row size in full bytes of source image */
edc570
   full_bytes = (sect_width * spp * bps) / 8;   /* number of COMPLETE bytes per row in section */
edc570
-  trailing_bits = (sect_width * bps) % 8;
edc570
+    trailing_bits = (sect_width * spp * bps) % 8;         /* trailing bits within the last byte of destination buffer */
edc570
 
edc570
 #ifdef DEVELMODE
edc570
     TIFFError ("", "First row: %d, last row: %d, First col: %d, last col: %d\n",
edc570
@@ -6734,10 +6733,9 @@ extractImageSection(struct image_data *image, struct pageseg *section,
edc570
 
edc570
   if ((bps % 8) == 0)
edc570
     {
edc570
-    col_offset = first_col * spp * bps / 8;
edc570
+    col_offset = (first_col * spp * bps) / 8;
edc570
     for (row = first_row; row <= last_row; row++)
edc570
       {
edc570
-      /* row_offset = row * img_width * spp * bps / 8; */
edc570
       row_offset = row * img_rowsize;
edc570
       src_offset = row_offset + col_offset;
edc570
 
edc570
@@ -6750,14 +6748,12 @@ extractImageSection(struct image_data *image, struct pageseg *section,
edc570
     }
edc570
   else
edc570
     { /* bps != 8 */
edc570
-    shift1  = spp * ((first_col * bps) % 8);
edc570
-    shift2  = spp * ((last_col * bps) % 8);
edc570
+    shift1 = ((first_col * spp * bps) % 8);           /* shift1 = bits to skip in the first byte of source buffer*/
edc570
     for (row = first_row; row <= last_row; row++)
edc570
       {
edc570
       /* pull out the first byte */
edc570
       row_offset = row * img_rowsize;
edc570
-      offset1 = row_offset + (first_col * bps / 8);
edc570
-      offset2 = row_offset + (last_col * bps / 8);
edc570
+      offset1 = row_offset + ((first_col * spp * bps) / 8);   /* offset1 = offset into source of byte with first bits to be extracted */
edc570
 
edc570
 #ifdef DEVELMODE
edc570
       for (j = 0, k = 7; j < 8; j++, k--)
edc570
@@ -6769,12 +6765,12 @@ extractImageSection(struct image_data *image, struct pageseg *section,
edc570
       sprintf(&bitarray[9], " ");
edc570
       for (j = 10, k = 7; j < 18; j++, k--)
edc570
         {
edc570
-        bitset = *(src_buff + offset2) & (((unsigned char)1 << k)) ? 1 : 0;
edc570
+        bitset = *(src_buff + offset1 + full_bytes) & (((unsigned char)1 << k)) ? 1 : 0;
edc570
         sprintf(&bitarray[j], (bitset) ? "1" : "0");
edc570
         }
edc570
       bitarray[18] = '\0';
edc570
-      TIFFError ("", "Row: %3d Offset1: %d,  Shift1: %d,    Offset2: %d,  Shift2:  %d\n", 
edc570
-                 row, offset1, shift1, offset2, shift2); 
edc570
+      TIFFError ("", "Row: %3d Offset1: %d,  Shift1: %d,    Offset2: %d,  Trailing_bits:  %d\n",
edc570
+                 row, offset1, shift1, offset1+full_bytes, trailing_bits);
edc570
 #endif
edc570
 
edc570
       bytebuff1 = bytebuff2 = 0;
edc570
@@ -6798,11 +6794,12 @@ extractImageSection(struct image_data *image, struct pageseg *section,
edc570
 
edc570
         if (trailing_bits != 0)
edc570
           {
edc570
-	  bytebuff2 = src_buff[offset2] & ((unsigned char)255 << (7 - shift2));
edc570
+      /* Only copy higher bits of samples and mask lower bits of not wanted column samples to zero */
edc570
+	  bytebuff2 = src_buff[offset1 + full_bytes] & ((unsigned char)255 << (8 - trailing_bits));
edc570
           sect_buff[dst_offset] = bytebuff2;
edc570
 #ifdef DEVELMODE
edc570
 	  TIFFError ("", "        Trailing bits src offset:  %8d, Dst offset: %8d\n", 
edc570
-                              offset2, dst_offset); 
edc570
+          offset1 + full_bytes, dst_offset);
edc570
           for (j = 30, k = 7; j < 38; j++, k--)
edc570
             {
edc570
             bitset = *(sect_buff + dst_offset) & (((unsigned char)1 << k)) ? 1 : 0;
edc570
@@ -6821,8 +6818,10 @@ extractImageSection(struct image_data *image, struct pageseg *section,
edc570
 #endif
edc570
         for (j = 0; j <= full_bytes; j++) 
edc570
           {
edc570
+          /* Skip the first shift1 bits and shift the source up by shift1 bits before save to destination.*/
edc570
+          /* Attention: src_buff size needs to be some bytes larger than image size, because could read behind image here. */
edc570
 	  bytebuff1 = src_buff[offset1 + j] & ((unsigned char)255 >> shift1);
edc570
-	  bytebuff2 = src_buff[offset1 + j + 1] & ((unsigned char)255 << (7 - shift1));
edc570
+          bytebuff2 = src_buff[offset1 + j + 1] & ((unsigned char)255 << (8 - shift1));
edc570
           sect_buff[dst_offset + j] = (bytebuff1 << shift1) | (bytebuff2 >> (8 - shift1));
edc570
           }
edc570
 #ifdef DEVELMODE
edc570
@@ -6838,35 +6837,16 @@ extractImageSection(struct image_data *image, struct pageseg *section,
edc570
 #endif
edc570
         dst_offset += full_bytes;
edc570
 
edc570
+        /* Copy the trailing_bits for the last byte in the destination buffer. 
edc570
+           Could come from one ore two bytes of the source buffer. */
edc570
         if (trailing_bits != 0)
edc570
           {
edc570
 #ifdef DEVELMODE
edc570
-	    TIFFError ("", "        Trailing bits   src offset: %8d, Dst offset: %8d\n", offset1 + full_bytes, dst_offset); 
edc570
-#endif
edc570
-	  if (shift2 > shift1)
edc570
-            {
edc570
-	    bytebuff1 = src_buff[offset1 + full_bytes] & ((unsigned char)255 << (7 - shift2));
edc570
-            bytebuff2 = bytebuff1 & ((unsigned char)255 << shift1);
edc570
-            sect_buff[dst_offset] = bytebuff2;
edc570
-#ifdef DEVELMODE
edc570
-	    TIFFError ("", "        Shift2 > Shift1\n"); 
edc570
+          TIFFError("", "        Trailing bits %4d   src offset: %8d, Dst offset: %8d\n", trailing_bits, offset1 + full_bytes, dst_offset);
edc570
 #endif
edc570
-            }
edc570
-          else
edc570
-            {
edc570
-	    if (shift2 < shift1)
edc570
-              {
edc570
-              bytebuff2 = ((unsigned char)255 << (shift1 - shift2 - 1));
edc570
-	      sect_buff[dst_offset] &= bytebuff2;
edc570
-#ifdef DEVELMODE
edc570
-	      TIFFError ("", "        Shift2 < Shift1\n"); 
edc570
-#endif
edc570
-              }
edc570
-#ifdef DEVELMODE
edc570
-            else
edc570
-	      TIFFError ("", "        Shift2 == Shift1\n"); 
edc570
-#endif
edc570
-            }
edc570
+          /* More than necessary bits are already copied into last destination buffer, 
edc570
+           * only masking of last byte in destination buffer is necessary.*/ 
edc570
+          sect_buff[dst_offset] &= ((uint8_t)0xFF << (8 - trailing_bits));
edc570
 	  }
edc570
 #ifdef DEVELMODE
edc570
 	  sprintf(&bitarray[28], " ");
edc570
@@ -7020,7 +7000,7 @@ writeImageSections(TIFF *in, TIFF *out, struct image_data *image,
edc570
     width  = sections[i].x2 - sections[i].x1 + 1;
edc570
     length = sections[i].y2 - sections[i].y1 + 1;
edc570
     sectsize = (uint32)
edc570
-	    ceil((width * image->bps + 7) / (double)8) * image->spp * length;
edc570
+	    ceil((width * image->bps * image->spp + 7) / (double)8) * length;
edc570
     /* allocate a buffer if we don't have one already */
edc570
     if (createImageSection(sectsize, sect_buff_ptr))
edc570
       {
edc570
-- 
edc570
2.34.1
edc570