1*4882a593SmuzhiyunFrom 9b2645d830b4ad004824cf28d81f3b974faf0037 Mon Sep 17 00:00:00 2001
2*4882a593SmuzhiyunFrom: Su Laus <sulau@freenet.de>
3*4882a593SmuzhiyunDate: Tue, 8 Mar 2022 17:02:44 +0000
4*4882a593SmuzhiyunSubject: [PATCH] tiffcrop: fix issue #380 and #382 heap buffer overflow in
5*4882a593Smuzhiyun
6*4882a593SmuzhiyunCVE: CVE-2022-0891
7*4882a593SmuzhiyunCVE: CVE-2022-1056
8*4882a593SmuzhiyunUpstream-Status: Backport
9*4882a593SmuzhiyunSigned-off-by: Ross Burton <ross.burton@arm.com>
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun extractImageSection
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun---
14*4882a593Smuzhiyun tools/tiffcrop.c | 92 +++++++++++++++++++-----------------------------
15*4882a593Smuzhiyun 1 file changed, 36 insertions(+), 56 deletions(-)
16*4882a593Smuzhiyun
17*4882a593Smuzhiyundiff --git a/tools/tiffcrop.c b/tools/tiffcrop.c
18*4882a593Smuzhiyunindex b85c2ce..302a7e9 100644
19*4882a593Smuzhiyun--- a/tools/tiffcrop.c
20*4882a593Smuzhiyun+++ b/tools/tiffcrop.c
21*4882a593Smuzhiyun@@ -105,8 +105,8 @@
22*4882a593Smuzhiyun  *                of messages to monitor progress without enabling dump logs.
23*4882a593Smuzhiyun  */
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun-static   char tiffcrop_version_id[] = "2.4";
26*4882a593Smuzhiyun-static   char tiffcrop_rev_date[] = "12-13-2010";
27*4882a593Smuzhiyun+static   char tiffcrop_version_id[] = "2.4.1";
28*4882a593Smuzhiyun+static   char tiffcrop_rev_date[] = "03-03-2010";
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun #include "tif_config.h"
31*4882a593Smuzhiyun #include "libport.h"
32*4882a593Smuzhiyun@@ -6710,10 +6710,10 @@ extractImageSection(struct image_data *image, struct pageseg *section,
33*4882a593Smuzhiyun #ifdef DEVELMODE
34*4882a593Smuzhiyun   uint32_t    img_length;
35*4882a593Smuzhiyun #endif
36*4882a593Smuzhiyun-  uint32_t    j, shift1, shift2, trailing_bits;
37*4882a593Smuzhiyun+  uint32_t    j, shift1, trailing_bits;
38*4882a593Smuzhiyun   uint32_t    row, first_row, last_row, first_col, last_col;
39*4882a593Smuzhiyun   uint32_t    src_offset, dst_offset, row_offset, col_offset;
40*4882a593Smuzhiyun-  uint32_t    offset1, offset2, full_bytes;
41*4882a593Smuzhiyun+  uint32_t    offset1, full_bytes;
42*4882a593Smuzhiyun   uint32_t    sect_width;
43*4882a593Smuzhiyun #ifdef DEVELMODE
44*4882a593Smuzhiyun   uint32_t    sect_length;
45*4882a593Smuzhiyun@@ -6723,7 +6723,6 @@ extractImageSection(struct image_data *image, struct pageseg *section,
46*4882a593Smuzhiyun #ifdef DEVELMODE
47*4882a593Smuzhiyun   int      k;
48*4882a593Smuzhiyun   unsigned char bitset;
49*4882a593Smuzhiyun-  static char *bitarray = NULL;
50*4882a593Smuzhiyun #endif
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun   img_width = image->width;
53*4882a593Smuzhiyun@@ -6741,17 +6740,12 @@ extractImageSection(struct image_data *image, struct pageseg *section,
54*4882a593Smuzhiyun   dst_offset = 0;
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun #ifdef DEVELMODE
57*4882a593Smuzhiyun-  if (bitarray == NULL)
58*4882a593Smuzhiyun-    {
59*4882a593Smuzhiyun-    if ((bitarray = (char *)malloc(img_width)) == NULL)
60*4882a593Smuzhiyun-      {
61*4882a593Smuzhiyun-      TIFFError ("", "DEBUG: Unable to allocate debugging bitarray");
62*4882a593Smuzhiyun-      return (-1);
63*4882a593Smuzhiyun-      }
64*4882a593Smuzhiyun-    }
65*4882a593Smuzhiyun+  char bitarray[39];
66*4882a593Smuzhiyun #endif
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun-  /* rows, columns, width, length are expressed in pixels */
69*4882a593Smuzhiyun+  /* rows, columns, width, length are expressed in pixels
70*4882a593Smuzhiyun+   * first_row, last_row, .. are index into image array starting at 0 to width-1,
71*4882a593Smuzhiyun+   * last_col shall be also extracted.  */
72*4882a593Smuzhiyun   first_row = section->y1;
73*4882a593Smuzhiyun   last_row  = section->y2;
74*4882a593Smuzhiyun   first_col = section->x1;
75*4882a593Smuzhiyun@@ -6761,9 +6755,14 @@ extractImageSection(struct image_data *image, struct pageseg *section,
76*4882a593Smuzhiyun #ifdef DEVELMODE
77*4882a593Smuzhiyun   sect_length = last_row - first_row + 1;
78*4882a593Smuzhiyun #endif
79*4882a593Smuzhiyun-  img_rowsize = ((img_width * bps + 7) / 8) * spp;
80*4882a593Smuzhiyun-  full_bytes = (sect_width * spp * bps) / 8;   /* number of COMPLETE bytes per row in section */
81*4882a593Smuzhiyun-  trailing_bits = (sect_width * bps) % 8;
82*4882a593Smuzhiyun+    /* The read function loadImage() used copy separate plane data into a buffer as interleaved
83*4882a593Smuzhiyun+     * samples rather than separate planes so the same logic works to extract regions
84*4882a593Smuzhiyun+     * regardless of the way the data are organized in the input file.
85*4882a593Smuzhiyun+     * Furthermore, bytes and bits are arranged in buffer according to COMPRESSION=1 and FILLORDER=1
86*4882a593Smuzhiyun+     */
87*4882a593Smuzhiyun+    img_rowsize = (((img_width * spp * bps) + 7) / 8);    /* row size in full bytes of source image */
88*4882a593Smuzhiyun+    full_bytes = (sect_width * spp * bps) / 8;            /* number of COMPLETE bytes per row in section */
89*4882a593Smuzhiyun+    trailing_bits = (sect_width * spp * bps) % 8;         /* trailing bits within the last byte of destination buffer */
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun #ifdef DEVELMODE
92*4882a593Smuzhiyun     TIFFError ("", "First row: %"PRIu32", last row: %"PRIu32", First col: %"PRIu32", last col: %"PRIu32"\n",
93*4882a593Smuzhiyun@@ -6776,10 +6775,9 @@ extractImageSection(struct image_data *image, struct pageseg *section,
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun   if ((bps % 8) == 0)
96*4882a593Smuzhiyun     {
97*4882a593Smuzhiyun-    col_offset = first_col * spp * bps / 8;
98*4882a593Smuzhiyun+    col_offset = (first_col * spp * bps) / 8;
99*4882a593Smuzhiyun     for (row = first_row; row <= last_row; row++)
100*4882a593Smuzhiyun       {
101*4882a593Smuzhiyun-      /* row_offset = row * img_width * spp * bps / 8; */
102*4882a593Smuzhiyun       row_offset = row * img_rowsize;
103*4882a593Smuzhiyun       src_offset = row_offset + col_offset;
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun@@ -6792,14 +6790,12 @@ extractImageSection(struct image_data *image, struct pageseg *section,
106*4882a593Smuzhiyun     }
107*4882a593Smuzhiyun   else
108*4882a593Smuzhiyun     { /* bps != 8 */
109*4882a593Smuzhiyun-    shift1  = spp * ((first_col * bps) % 8);
110*4882a593Smuzhiyun-    shift2  = spp * ((last_col * bps) % 8);
111*4882a593Smuzhiyun+    shift1 = ((first_col * spp * bps) % 8);           /* shift1 = bits to skip in the first byte of source buffer*/
112*4882a593Smuzhiyun     for (row = first_row; row <= last_row; row++)
113*4882a593Smuzhiyun       {
114*4882a593Smuzhiyun       /* pull out the first byte */
115*4882a593Smuzhiyun       row_offset = row * img_rowsize;
116*4882a593Smuzhiyun-      offset1 = row_offset + (first_col * bps / 8);
117*4882a593Smuzhiyun-      offset2 = row_offset + (last_col * bps / 8);
118*4882a593Smuzhiyun+      offset1 = row_offset + ((first_col * spp * bps) / 8);   /* offset1 = offset into source of byte with first bits to be extracted */
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun #ifdef DEVELMODE
121*4882a593Smuzhiyun       for (j = 0, k = 7; j < 8; j++, k--)
122*4882a593Smuzhiyun@@ -6811,12 +6807,12 @@ extractImageSection(struct image_data *image, struct pageseg *section,
123*4882a593Smuzhiyun       sprintf(&bitarray[9], " ");
124*4882a593Smuzhiyun       for (j = 10, k = 7; j < 18; j++, k--)
125*4882a593Smuzhiyun         {
126*4882a593Smuzhiyun-        bitset = *(src_buff + offset2) & (((unsigned char)1 << k)) ? 1 : 0;
127*4882a593Smuzhiyun+        bitset = *(src_buff + offset1 + full_bytes) & (((unsigned char)1 << k)) ? 1 : 0;
128*4882a593Smuzhiyun         sprintf(&bitarray[j], (bitset) ? "1" : "0");
129*4882a593Smuzhiyun         }
130*4882a593Smuzhiyun       bitarray[18] = '\0';
131*4882a593Smuzhiyun-      TIFFError ("", "Row: %3d Offset1: %"PRIu32",  Shift1: %"PRIu32",    Offset2: %"PRIu32",  Shift2:  %"PRIu32"\n",
132*4882a593Smuzhiyun-                 row, offset1, shift1, offset2, shift2);
133*4882a593Smuzhiyun+      TIFFError ("", "Row: %3d Offset1: %"PRIu32",  Shift1: %"PRIu32",    Offset2: %"PRIu32",  Trailing_bits:  %"PRIu32"\n",
134*4882a593Smuzhiyun+                 row, offset1, shift1, offset1+full_bytes, trailing_bits);
135*4882a593Smuzhiyun #endif
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun       bytebuff1 = bytebuff2 = 0;
138*4882a593Smuzhiyun@@ -6840,11 +6836,12 @@ extractImageSection(struct image_data *image, struct pageseg *section,
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun         if (trailing_bits != 0)
141*4882a593Smuzhiyun           {
142*4882a593Smuzhiyun-	  bytebuff2 = src_buff[offset2] & ((unsigned char)255 << (7 - shift2));
143*4882a593Smuzhiyun+      /* Only copy higher bits of samples and mask lower bits of not wanted column samples to zero */
144*4882a593Smuzhiyun+	  bytebuff2 = src_buff[offset1 + full_bytes] & ((unsigned char)255 << (8 - trailing_bits));
145*4882a593Smuzhiyun           sect_buff[dst_offset] = bytebuff2;
146*4882a593Smuzhiyun #ifdef DEVELMODE
147*4882a593Smuzhiyun 	  TIFFError ("", "        Trailing bits src offset:  %8"PRIu32", Dst offset: %8"PRIu32"\n",
148*4882a593Smuzhiyun-                              offset2, dst_offset);
149*4882a593Smuzhiyun+          offset1 + full_bytes, dst_offset);
150*4882a593Smuzhiyun           for (j = 30, k = 7; j < 38; j++, k--)
151*4882a593Smuzhiyun             {
152*4882a593Smuzhiyun             bitset = *(sect_buff + dst_offset) & (((unsigned char)1 << k)) ? 1 : 0;
153*4882a593Smuzhiyun@@ -6863,8 +6860,10 @@ extractImageSection(struct image_data *image, struct pageseg *section,
154*4882a593Smuzhiyun #endif
155*4882a593Smuzhiyun         for (j = 0; j <= full_bytes; j++)
156*4882a593Smuzhiyun           {
157*4882a593Smuzhiyun-	  bytebuff1 = src_buff[offset1 + j] & ((unsigned char)255 >> shift1);
158*4882a593Smuzhiyun-	  bytebuff2 = src_buff[offset1 + j + 1] & ((unsigned char)255 << (7 - shift1));
159*4882a593Smuzhiyun+          /* Skip the first shift1 bits and shift the source up by shift1 bits before save to destination.*/
160*4882a593Smuzhiyun+          /* Attention: src_buff size needs to be some bytes larger than image size, because could read behind image here. */
161*4882a593Smuzhiyun+          bytebuff1 = src_buff[offset1 + j] & ((unsigned char)255 >> shift1);
162*4882a593Smuzhiyun+          bytebuff2 = src_buff[offset1 + j + 1] & ((unsigned char)255 << (8 - shift1));
163*4882a593Smuzhiyun           sect_buff[dst_offset + j] = (bytebuff1 << shift1) | (bytebuff2 >> (8 - shift1));
164*4882a593Smuzhiyun           }
165*4882a593Smuzhiyun #ifdef DEVELMODE
166*4882a593Smuzhiyun@@ -6880,36 +6879,17 @@ extractImageSection(struct image_data *image, struct pageseg *section,
167*4882a593Smuzhiyun #endif
168*4882a593Smuzhiyun         dst_offset += full_bytes;
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun+        /* Copy the trailing_bits for the last byte in the destination buffer.
171*4882a593Smuzhiyun+           Could come from one ore two bytes of the source buffer. */
172*4882a593Smuzhiyun         if (trailing_bits != 0)
173*4882a593Smuzhiyun           {
174*4882a593Smuzhiyun #ifdef DEVELMODE
175*4882a593Smuzhiyun-	    TIFFError ("", "        Trailing bits   src offset: %8"PRIu32", Dst offset: %8"PRIu32"\n", offset1 + full_bytes, dst_offset);
176*4882a593Smuzhiyun-#endif
177*4882a593Smuzhiyun-	  if (shift2 > shift1)
178*4882a593Smuzhiyun-            {
179*4882a593Smuzhiyun-	    bytebuff1 = src_buff[offset1 + full_bytes] & ((unsigned char)255 << (7 - shift2));
180*4882a593Smuzhiyun-            bytebuff2 = bytebuff1 & ((unsigned char)255 << shift1);
181*4882a593Smuzhiyun-            sect_buff[dst_offset] = bytebuff2;
182*4882a593Smuzhiyun-#ifdef DEVELMODE
183*4882a593Smuzhiyun-	    TIFFError ("", "        Shift2 > Shift1\n");
184*4882a593Smuzhiyun+          TIFFError("", "        Trailing bits %4"PRIu32"   src offset: %8"PRIu32", Dst offset: %8"PRIu32"\n", trailing_bits, offset1 + full_bytes, dst_offset);
185*4882a593Smuzhiyun #endif
186*4882a593Smuzhiyun+          /* More than necessary bits are already copied into last destination buffer,
187*4882a593Smuzhiyun+           * only masking of last byte in destination buffer is necessary.*/
188*4882a593Smuzhiyun+          sect_buff[dst_offset] &= ((uint8_t)0xFF << (8 - trailing_bits));
189*4882a593Smuzhiyun             }
190*4882a593Smuzhiyun-          else
191*4882a593Smuzhiyun-            {
192*4882a593Smuzhiyun-	    if (shift2 < shift1)
193*4882a593Smuzhiyun-              {
194*4882a593Smuzhiyun-              bytebuff2 = ((unsigned char)255 << (shift1 - shift2 - 1));
195*4882a593Smuzhiyun-	      sect_buff[dst_offset] &= bytebuff2;
196*4882a593Smuzhiyun-#ifdef DEVELMODE
197*4882a593Smuzhiyun-	      TIFFError ("", "        Shift2 < Shift1\n");
198*4882a593Smuzhiyun-#endif
199*4882a593Smuzhiyun-              }
200*4882a593Smuzhiyun-#ifdef DEVELMODE
201*4882a593Smuzhiyun-            else
202*4882a593Smuzhiyun-	      TIFFError ("", "        Shift2 == Shift1\n");
203*4882a593Smuzhiyun-#endif
204*4882a593Smuzhiyun-            }
205*4882a593Smuzhiyun-	  }
206*4882a593Smuzhiyun #ifdef DEVELMODE
207*4882a593Smuzhiyun 	  sprintf(&bitarray[28], " ");
208*4882a593Smuzhiyun 	  sprintf(&bitarray[29], " ");
209*4882a593Smuzhiyun@@ -7062,7 +7042,7 @@ writeImageSections(TIFF *in, TIFF *out, struct image_data *image,
210*4882a593Smuzhiyun     width  = sections[i].x2 - sections[i].x1 + 1;
211*4882a593Smuzhiyun     length = sections[i].y2 - sections[i].y1 + 1;
212*4882a593Smuzhiyun     sectsize = (uint32_t)
213*4882a593Smuzhiyun-	    ceil((width * image->bps + 7) / (double)8) * image->spp * length;
214*4882a593Smuzhiyun+	    ceil((width * image->bps * image->spp + 7) / (double)8) * length;
215*4882a593Smuzhiyun     /* allocate a buffer if we don't have one already */
216*4882a593Smuzhiyun     if (createImageSection(sectsize, sect_buff_ptr))
217*4882a593Smuzhiyun       {
218