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

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