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