Blame SOURCES/0008-CVE-2022-3570-CVE-2022-3598-tiffcrop-subroutines-req.patch

c96876
From aacb1f89c5aa85d513dea8bdb1fd6818519bae2d Mon Sep 17 00:00:00 2001
c96876
From: Su Laus <sulau@freenet.de>
c96876
Date: Thu, 13 Oct 2022 14:33:27 +0000
c96876
Subject: [PATCH] (CVE-2022-3570 CVE-2022-3598) tiffcrop subroutines require a
c96876
 larger buffer (fixes #271, #381, #386, #388, #389, #435)
c96876
c96876
(cherry picked from commit afd7086090dafd3949afd172822cbcec4ed17d56)
c96876
---
c96876
 tools/tiffcrop.c | 205 +++++++++++++++++++++++++++--------------------
c96876
 1 file changed, 116 insertions(+), 89 deletions(-)
c96876
c96876
diff --git a/tools/tiffcrop.c b/tools/tiffcrop.c
c96876
index 613ce7f8..70d56e55 100644
c96876
--- a/tools/tiffcrop.c
c96876
+++ b/tools/tiffcrop.c
c96876
@@ -212,6 +212,10 @@ static   char tiffcrop_rev_date[] = "02-09-2022";
c96876
 
c96876
 #define TIFF_DIR_MAX  65534
c96876
 
c96876
+/* Some conversion subroutines require image buffers, which are at least 3 bytes
c96876
+ * larger than the necessary size for the image itself. */
c96876
+#define NUM_BUFF_OVERSIZE_BYTES   3
c96876
+
c96876
 /* Offsets into buffer for margins and fixed width and length segments */
c96876
 struct offset {
c96876
   uint32_t  tmargin;
c96876
@@ -233,7 +237,7 @@ struct offset {
c96876
  */
c96876
 
c96876
 struct  buffinfo {
c96876
-  uint32_t size;           /* size of this buffer */
c96876
+  size_t size;           /* size of this buffer */
c96876
   unsigned char *buffer; /* address of the allocated buffer */
c96876
 };
c96876
 
c96876
@@ -810,8 +814,8 @@ static int readContigTilesIntoBuffer (TIFF* in, uint8_t* buf,
c96876
   uint32_t dst_rowsize, shift_width;
c96876
   uint32_t bytes_per_sample, bytes_per_pixel;
c96876
   uint32_t trailing_bits, prev_trailing_bits;
c96876
-  uint32_t tile_rowsize  = TIFFTileRowSize(in);
c96876
-  uint32_t src_offset, dst_offset;
c96876
+  tmsize_t tile_rowsize  = TIFFTileRowSize(in);
c96876
+  tmsize_t src_offset, dst_offset;
c96876
   uint32_t row_offset, col_offset;
c96876
   uint8_t *bufp = (uint8_t*) buf;
c96876
   unsigned char *src = NULL;
c96876
@@ -861,7 +865,7 @@ static int readContigTilesIntoBuffer (TIFF* in, uint8_t* buf,
c96876
       TIFFError("readContigTilesIntoBuffer", "Integer overflow when calculating buffer size.");
c96876
       exit(EXIT_FAILURE);
c96876
   }
c96876
-  tilebuf = limitMalloc(tile_buffsize + 3);
c96876
+  tilebuf = limitMalloc(tile_buffsize + NUM_BUFF_OVERSIZE_BYTES);
c96876
   if (tilebuf == 0)
c96876
     return 0;
c96876
   tilebuf[tile_buffsize] = 0;
c96876
@@ -1024,7 +1028,7 @@ static int  readSeparateTilesIntoBuffer (TIFF* in, uint8_t *obuf,
c96876
   for (sample = 0; (sample < spp) && (sample < MAX_SAMPLES); sample++)
c96876
     {
c96876
     srcbuffs[sample] = NULL;
c96876
-    tbuff = (unsigned char *)limitMalloc(tilesize + 8);
c96876
+    tbuff = (unsigned char *)limitMalloc(tilesize + NUM_BUFF_OVERSIZE_BYTES);
c96876
     if (!tbuff)
c96876
       {
c96876
       TIFFError ("readSeparateTilesIntoBuffer", 
c96876
@@ -1217,7 +1221,8 @@ writeBufferToSeparateStrips (TIFF* out, uint8_t* buf,
c96876
   }
c96876
   rowstripsize = rowsperstrip * bytes_per_sample * (width + 1); 
c96876
 
c96876
-  obuf = limitMalloc (rowstripsize);
c96876
+  /* Add 3 padding bytes for extractContigSamples32bits */
c96876
+  obuf = limitMalloc (rowstripsize + NUM_BUFF_OVERSIZE_BYTES);
c96876
   if (obuf == NULL)
c96876
     return 1;
c96876
   
c96876
@@ -1229,7 +1234,7 @@ writeBufferToSeparateStrips (TIFF* out, uint8_t* buf,
c96876
 
c96876
       stripsize = TIFFVStripSize(out, nrows);
c96876
       src = buf + (row * rowsize);
c96876
-      memset (obuf, '\0', rowstripsize);
c96876
+      memset (obuf, '\0',rowstripsize + NUM_BUFF_OVERSIZE_BYTES);
c96876
       if (extractContigSamplesToBuffer(obuf, src, nrows, width, s, spp, bps, dump))
c96876
         {
c96876
         _TIFFfree(obuf);
c96876
@@ -1237,10 +1242,15 @@ writeBufferToSeparateStrips (TIFF* out, uint8_t* buf,
c96876
 	}
c96876
       if ((dump->outfile != NULL) && (dump->level == 1))
c96876
         {
c96876
-        dump_info(dump->outfile, dump->format,"", 
c96876
+          if (scanlinesize > 0x0ffffffffULL) {
c96876
+              dump_info(dump->infile, dump->format, "loadImage",
c96876
+                  "Attention: scanlinesize %"PRIu64" is larger than UINT32_MAX.\nFollowing dump might be wrong.",
c96876
+                  scanlinesize);
c96876
+          }
c96876
+          dump_info(dump->outfile, dump->format,"",
c96876
                   "Sample %2d, Strip: %2d, bytes: %4d, Row %4d, bytes: %4d, Input offset: %6d", 
c96876
-                  s + 1, strip + 1, stripsize, row + 1, scanlinesize, src - buf);
c96876
-        dump_buffer(dump->outfile, dump->format, nrows, scanlinesize, row, obuf);
c96876
+                  s + 1, strip + 1, stripsize, row + 1, (uint32_t)scanlinesize, src - buf);
c96876
+        dump_buffer(dump->outfile, dump->format, nrows, (uint32_t)scanlinesize, row, obuf);
c96876
 	}
c96876
 
c96876
       if (TIFFWriteEncodedStrip(out, strip++, obuf, stripsize) < 0)
c96876
@@ -1267,7 +1277,7 @@ static int writeBufferToContigTiles (TIFF* out, uint8_t* buf, uint32_t imageleng
c96876
   uint32_t tl, tw;
c96876
   uint32_t row, col, nrow, ncol;
c96876
   uint32_t src_rowsize, col_offset;
c96876
-  uint32_t tile_rowsize  = TIFFTileRowSize(out);
c96876
+  tmsize_t tile_rowsize  = TIFFTileRowSize(out);
c96876
   uint8_t* bufp = (uint8_t*) buf;
c96876
   tsize_t tile_buffsize = 0;
c96876
   tsize_t tilesize = TIFFTileSize(out);
c96876
@@ -1310,9 +1320,11 @@ static int writeBufferToContigTiles (TIFF* out, uint8_t* buf, uint32_t imageleng
c96876
   }
c96876
   src_rowsize = ((imagewidth * spp * bps) + 7U) / 8;
c96876
 
c96876
-  tilebuf = limitMalloc(tile_buffsize);
c96876
+  /* Add 3 padding bytes for extractContigSamples32bits */
c96876
+  tilebuf = limitMalloc(tile_buffsize + NUM_BUFF_OVERSIZE_BYTES);
c96876
   if (tilebuf == 0)
c96876
     return 1;
c96876
+  memset(tilebuf, 0, tile_buffsize + NUM_BUFF_OVERSIZE_BYTES);
c96876
   for (row = 0; row < imagelength; row += tl)
c96876
     {
c96876
     nrow = (row + tl > imagelength) ? imagelength - row : tl;
c96876
@@ -1358,7 +1370,8 @@ static int writeBufferToSeparateTiles (TIFF* out, uint8_t* buf, uint32_t imagele
c96876
                                        uint32_t imagewidth, tsample_t spp,
c96876
                                        struct dump_opts * dump)
c96876
   {
c96876
-  tdata_t obuf = limitMalloc(TIFFTileSize(out));
c96876
+  /* Add 3 padding bytes for extractContigSamples32bits */
c96876
+  tdata_t obuf = limitMalloc(TIFFTileSize(out) + NUM_BUFF_OVERSIZE_BYTES);
c96876
   uint32_t tl, tw;
c96876
   uint32_t row, col, nrow, ncol;
c96876
   uint32_t src_rowsize, col_offset;
c96876
@@ -1368,6 +1381,7 @@ static int writeBufferToSeparateTiles (TIFF* out, uint8_t* buf, uint32_t imagele
c96876
 
c96876
   if (obuf == NULL)
c96876
     return 1;
c96876
+  memset(obuf, 0, TIFFTileSize(out) + NUM_BUFF_OVERSIZE_BYTES);
c96876
 
c96876
   if( !TIFFGetField(out, TIFFTAG_TILELENGTH, &tl) ||
c96876
       !TIFFGetField(out, TIFFTAG_TILEWIDTH, &tw) ||
c96876
@@ -1793,14 +1807,14 @@ void  process_command_opts (int argc, char *argv[], char *mp, char *mode, uint32
c96876
                       
c96876
                     *opt_offset = '\0';
c96876
                     /* convert option to lowercase */
c96876
-                    end = strlen (opt_ptr);
c96876
+                    end = (unsigned int)strlen (opt_ptr);
c96876
                     for (i = 0; i < end; i++)
c96876
                       *(opt_ptr + i) = tolower((int) *(opt_ptr + i));
c96876
                     /* Look for dump format specification */
c96876
                     if (strncmp(opt_ptr, "for", 3) == 0)
c96876
                       {
c96876
 		      /* convert value to lowercase */
c96876
-                      end = strlen (opt_offset + 1);
c96876
+                      end = (unsigned int)strlen (opt_offset + 1);
c96876
                       for (i = 1; i <= end; i++)
c96876
                         *(opt_offset + i) = tolower((int) *(opt_offset + i));
c96876
                       /* check dump format value */
c96876
@@ -2273,6 +2287,8 @@ main(int argc, char* argv[])
c96876
   size_t length;
c96876
   char   temp_filename[PATH_MAX + 16]; /* Extra space keeps the compiler from complaining */
c96876
 
c96876
+  assert(NUM_BUFF_OVERSIZE_BYTES >= 3);
c96876
+
c96876
   little_endian = *((unsigned char *)&little_endian) & '1';
c96876
 
c96876
   initImageData(&image);
c96876
@@ -3227,13 +3243,13 @@ extractContigSamples32bits (uint8_t *in, uint8_t *out, uint32_t cols,
c96876
       /* If we have a full buffer's worth, write it out */
c96876
       if (ready_bits >= 32)
c96876
         {
c96876
-        bytebuff1 = (buff2 >> 56);
c96876
+        bytebuff1 = (uint8_t)(buff2 >> 56);
c96876
         *dst++ = bytebuff1;
c96876
-        bytebuff2 = (buff2 >> 48);
c96876
+        bytebuff2 = (uint8_t)(buff2 >> 48);
c96876
         *dst++ = bytebuff2;
c96876
-        bytebuff3 = (buff2 >> 40);
c96876
+        bytebuff3 = (uint8_t)(buff2 >> 40);
c96876
         *dst++ = bytebuff3;
c96876
-        bytebuff4 = (buff2 >> 32);
c96876
+        bytebuff4 = (uint8_t)(buff2 >> 32);
c96876
         *dst++ = bytebuff4;
c96876
         ready_bits -= 32;
c96876
                     
c96876
@@ -3642,13 +3658,13 @@ extractContigSamplesShifted32bits (uint8_t *in, uint8_t *out, uint32_t cols,
c96876
         }
c96876
       else  /* If we have a full buffer's worth, write it out */
c96876
         {
c96876
-        bytebuff1 = (buff2 >> 56);
c96876
+        bytebuff1 = (uint8_t)(buff2 >> 56);
c96876
         *dst++ = bytebuff1;
c96876
-        bytebuff2 = (buff2 >> 48);
c96876
+        bytebuff2 = (uint8_t)(buff2 >> 48);
c96876
         *dst++ = bytebuff2;
c96876
-        bytebuff3 = (buff2 >> 40);
c96876
+        bytebuff3 = (uint8_t)(buff2 >> 40);
c96876
         *dst++ = bytebuff3;
c96876
-        bytebuff4 = (buff2 >> 32);
c96876
+        bytebuff4 = (uint8_t)(buff2 >> 32);
c96876
         *dst++ = bytebuff4;
c96876
         ready_bits -= 32;
c96876
                     
c96876
@@ -3825,10 +3841,10 @@ extractContigSamplesToTileBuffer(uint8_t *out, uint8_t *in, uint32_t rows, uint3
c96876
 static int readContigStripsIntoBuffer (TIFF* in, uint8_t* buf)
c96876
 {
c96876
         uint8_t* bufp = buf;
c96876
-        int32_t  bytes_read = 0;
c96876
+        tmsize_t  bytes_read = 0;
c96876
         uint32_t strip, nstrips   = TIFFNumberOfStrips(in);
c96876
-        uint32_t stripsize = TIFFStripSize(in);
c96876
-        uint32_t rows = 0;
c96876
+        tmsize_t stripsize = TIFFStripSize(in);
c96876
+        tmsize_t rows = 0;
c96876
         uint32_t rps = TIFFGetFieldDefaulted(in, TIFFTAG_ROWSPERSTRIP, &rps;;
c96876
         tsize_t scanline_size = TIFFScanlineSize(in);
c96876
 
c96876
@@ -3841,11 +3857,11 @@ static int readContigStripsIntoBuffer (TIFF* in, uint8_t* buf)
c96876
                 bytes_read = TIFFReadEncodedStrip (in, strip, bufp, -1);
c96876
                 rows = bytes_read / scanline_size;
c96876
                 if ((strip < (nstrips - 1)) && (bytes_read != (int32_t)stripsize))
c96876
-                        TIFFError("", "Strip %"PRIu32": read %"PRId32" bytes, strip size %"PRIu32,
c96876
+                        TIFFError("", "Strip %"PRIu32": read %"PRId64" bytes, strip size %"PRIu64,
c96876
                                   strip + 1, bytes_read, stripsize);
c96876
 
c96876
                 if (bytes_read < 0 && !ignore) {
c96876
-                        TIFFError("", "Error reading strip %"PRIu32" after %"PRIu32" rows",
c96876
+                        TIFFError("", "Error reading strip %"PRIu32" after %"PRIu64" rows",
c96876
                                   strip, rows);
c96876
                         return 0;
c96876
                 }
c96876
@@ -4310,13 +4326,13 @@ combineSeparateSamples32bits (uint8_t *in[], uint8_t *out, uint32_t cols,
c96876
 	/* If we have a full buffer's worth, write it out */
c96876
 	if (ready_bits >= 32)
c96876
 	  {
c96876
-	  bytebuff1 = (buff2 >> 56);
c96876
+	  bytebuff1 = (uint8_t)(buff2 >> 56);
c96876
 	  *dst++ = bytebuff1;
c96876
-	  bytebuff2 = (buff2 >> 48);
c96876
+	  bytebuff2 = (uint8_t)(buff2 >> 48);
c96876
 	  *dst++ = bytebuff2;
c96876
-	  bytebuff3 = (buff2 >> 40);
c96876
+	  bytebuff3 = (uint8_t)(buff2 >> 40);
c96876
 	  *dst++ = bytebuff3;
c96876
-	  bytebuff4 = (buff2 >> 32);
c96876
+	  bytebuff4 = (uint8_t)(buff2 >> 32);
c96876
 	  *dst++ = bytebuff4;
c96876
 	  ready_bits -= 32;
c96876
                     
c96876
@@ -4359,10 +4375,10 @@ combineSeparateSamples32bits (uint8_t *in[], uint8_t *out, uint32_t cols,
c96876
 	         "Row %3d, Col %3d, Src byte offset %3d  bit offset %2d  Dst offset %3d",
c96876
 		 row + 1, col + 1, src_byte, src_bit, dst - out);
c96876
 
c96876
-      dump_long (dumpfile, format, "Match bits ", matchbits);
c96876
+      dump_wide (dumpfile, format, "Match bits ", matchbits);
c96876
       dump_data (dumpfile, format, "Src   bits ", src, 4);
c96876
-      dump_long (dumpfile, format, "Buff1 bits ", buff1);
c96876
-      dump_long (dumpfile, format, "Buff2 bits ", buff2);
c96876
+      dump_wide (dumpfile, format, "Buff1 bits ", buff1);
c96876
+      dump_wide (dumpfile, format, "Buff2 bits ", buff2);
c96876
       dump_byte (dumpfile, format, "Write bits1", bytebuff1);
c96876
       dump_byte (dumpfile, format, "Write bits2", bytebuff2);
c96876
       dump_info (dumpfile, format, "", "Ready bits:  %2d", ready_bits); 
c96876
@@ -4835,13 +4851,13 @@ combineSeparateTileSamples32bits (uint8_t *in[], uint8_t *out, uint32_t cols,
c96876
 	/* If we have a full buffer's worth, write it out */
c96876
 	if (ready_bits >= 32)
c96876
 	  {
c96876
-	  bytebuff1 = (buff2 >> 56);
c96876
+	  bytebuff1 = (uint8_t)(buff2 >> 56);
c96876
 	  *dst++ = bytebuff1;
c96876
-	  bytebuff2 = (buff2 >> 48);
c96876
+	  bytebuff2 = (uint8_t)(buff2 >> 48);
c96876
 	  *dst++ = bytebuff2;
c96876
-	  bytebuff3 = (buff2 >> 40);
c96876
+	  bytebuff3 = (uint8_t)(buff2 >> 40);
c96876
 	  *dst++ = bytebuff3;
c96876
-	  bytebuff4 = (buff2 >> 32);
c96876
+	  bytebuff4 = (uint8_t)(buff2 >> 32);
c96876
 	  *dst++ = bytebuff4;
c96876
 	  ready_bits -= 32;
c96876
                     
c96876
@@ -4884,10 +4900,10 @@ combineSeparateTileSamples32bits (uint8_t *in[], uint8_t *out, uint32_t cols,
c96876
 	         "Row %3d, Col %3d, Src byte offset %3d  bit offset %2d  Dst offset %3d",
c96876
 		 row + 1, col + 1, src_byte, src_bit, dst - out);
c96876
 
c96876
-      dump_long (dumpfile, format, "Match bits ", matchbits);
c96876
+      dump_wide (dumpfile, format, "Match bits ", matchbits);
c96876
       dump_data (dumpfile, format, "Src   bits ", src, 4);
c96876
-      dump_long (dumpfile, format, "Buff1 bits ", buff1);
c96876
-      dump_long (dumpfile, format, "Buff2 bits ", buff2);
c96876
+      dump_wide (dumpfile, format, "Buff1 bits ", buff1);
c96876
+      dump_wide (dumpfile, format, "Buff2 bits ", buff2);
c96876
       dump_byte (dumpfile, format, "Write bits1", bytebuff1);
c96876
       dump_byte (dumpfile, format, "Write bits2", bytebuff2);
c96876
       dump_info (dumpfile, format, "", "Ready bits:  %2d", ready_bits); 
c96876
@@ -4910,7 +4926,7 @@ static int readSeparateStripsIntoBuffer (TIFF *in, uint8_t *obuf, uint32_t lengt
c96876
   {
c96876
   int i, bytes_per_sample, bytes_per_pixel, shift_width, result = 1;
c96876
   uint32_t j;
c96876
-  int32_t  bytes_read = 0;
c96876
+  tmsize_t  bytes_read = 0;
c96876
   uint16_t bps = 0, planar;
c96876
   uint32_t nstrips;
c96876
   uint32_t strips_per_sample;
c96876
@@ -4976,7 +4992,7 @@ static int readSeparateStripsIntoBuffer (TIFF *in, uint8_t *obuf, uint32_t lengt
c96876
   for (s = 0; (s < spp) && (s < MAX_SAMPLES); s++)
c96876
     {
c96876
     srcbuffs[s] = NULL;
c96876
-    buff = limitMalloc(stripsize + 3);
c96876
+    buff = limitMalloc(stripsize + NUM_BUFF_OVERSIZE_BYTES);
c96876
     if (!buff)
c96876
       {
c96876
       TIFFError ("readSeparateStripsIntoBuffer", 
c96876
@@ -4999,7 +5015,7 @@ static int readSeparateStripsIntoBuffer (TIFF *in, uint8_t *obuf, uint32_t lengt
c96876
       buff = srcbuffs[s];
c96876
       strip = (s * strips_per_sample) + j; 
c96876
       bytes_read = TIFFReadEncodedStrip (in, strip, buff, stripsize);
c96876
-      rows_this_strip = bytes_read / src_rowsize;
c96876
+      rows_this_strip = (uint32_t)(bytes_read / src_rowsize);
c96876
       if (bytes_read < 0 && !ignore)
c96876
         {
c96876
         TIFFError(TIFFFileName(in),
c96876
@@ -6062,13 +6078,14 @@ loadImage(TIFF* in, struct image_data *image, struct dump_opts *dump, unsigned c
c96876
   uint16_t   input_compression = 0, input_photometric = 0;
c96876
   uint16_t   subsampling_horiz, subsampling_vert;
c96876
   uint32_t   width = 0, length = 0;
c96876
-  uint32_t   stsize = 0, tlsize = 0, buffsize = 0, scanlinesize = 0;
c96876
+  tmsize_t   stsize = 0, tlsize = 0, buffsize = 0;
c96876
+  tmsize_t   scanlinesize = 0;
c96876
   uint32_t   tw = 0, tl = 0;       /* Tile width and length */
c96876
-  uint32_t   tile_rowsize = 0;
c96876
+  tmsize_t   tile_rowsize = 0;
c96876
   unsigned char *read_buff = NULL;
c96876
   unsigned char *new_buff  = NULL;
c96876
   int      readunit = 0;
c96876
-  static   uint32_t  prev_readsize = 0;
c96876
+  static   tmsize_t  prev_readsize = 0;
c96876
 
c96876
   TIFFGetFieldDefaulted(in, TIFFTAG_BITSPERSAMPLE, &bps;;
c96876
   TIFFGetFieldDefaulted(in, TIFFTAG_SAMPLESPERPIXEL, &spp;;
c96876
@@ -6325,6 +6342,8 @@ loadImage(TIFF* in, struct image_data *image, struct dump_opts *dump, unsigned c
c96876
     /* The buffsize_check and the possible adaptation of buffsize 
c96876
      * has to account also for padding of each line to a byte boundary. 
c96876
      * This is assumed by mirrorImage() and rotateImage().
c96876
+     * Furthermore, functions like extractContigSamplesShifted32bits()
c96876
+     * need a buffer, which is at least 3 bytes larger than the actual image.
c96876
      * Otherwise buffer-overflow might occur there.
c96876
      */
c96876
     buffsize_check = length * (uint32_t)(((width * spp * bps) + 7) / 8);
c96876
@@ -6376,7 +6395,7 @@ loadImage(TIFF* in, struct image_data *image, struct dump_opts *dump, unsigned c
c96876
         TIFFError("loadImage", "Unable to allocate/reallocate read buffer");
c96876
         return (-1);
c96876
     }
c96876
-    read_buff = (unsigned char *)limitMalloc(buffsize+3);
c96876
+    read_buff = (unsigned char *)limitMalloc(buffsize + NUM_BUFF_OVERSIZE_BYTES);
c96876
   }
c96876
   else
c96876
     {
c96876
@@ -6387,11 +6406,11 @@ loadImage(TIFF* in, struct image_data *image, struct dump_opts *dump, unsigned c
c96876
           TIFFError("loadImage", "Unable to allocate/reallocate read buffer");
c96876
           return (-1);
c96876
       }
c96876
-      new_buff = _TIFFrealloc(read_buff, buffsize+3);
c96876
+      new_buff = _TIFFrealloc(read_buff, buffsize + NUM_BUFF_OVERSIZE_BYTES);
c96876
       if (!new_buff)
c96876
         {
c96876
 	free (read_buff);
c96876
-        read_buff = (unsigned char *)limitMalloc(buffsize+3);
c96876
+        read_buff = (unsigned char *)limitMalloc(buffsize + NUM_BUFF_OVERSIZE_BYTES);
c96876
         }
c96876
       else
c96876
         read_buff = new_buff;
c96876
@@ -6464,8 +6483,13 @@ loadImage(TIFF* in, struct image_data *image, struct dump_opts *dump, unsigned c
c96876
     dump_info  (dump->infile, dump->format, "", 
c96876
                 "Bits per sample %"PRIu16", Samples per pixel %"PRIu16, bps, spp);
c96876
 
c96876
+    if (scanlinesize > 0x0ffffffffULL) {
c96876
+        dump_info(dump->infile, dump->format, "loadImage",
c96876
+            "Attention: scanlinesize %"PRIu64" is larger than UINT32_MAX.\nFollowing dump might be wrong.",
c96876
+            scanlinesize);
c96876
+    }
c96876
     for (i = 0; i < length; i++)
c96876
-      dump_buffer(dump->infile, dump->format, 1, scanlinesize, 
c96876
+      dump_buffer(dump->infile, dump->format, 1, (uint32_t)scanlinesize, 
c96876
                   i, read_buff + (i * scanlinesize));
c96876
     }
c96876
   return (0);
c96876
@@ -7485,13 +7509,13 @@ writeSingleSection(TIFF *in, TIFF *out, struct image_data *image,
c96876
      if (TIFFGetField(in, TIFFTAG_NUMBEROFINKS, &ninks)) {
c96876
        TIFFSetField(out, TIFFTAG_NUMBEROFINKS, ninks);
c96876
        if (TIFFGetField(in, TIFFTAG_INKNAMES, &inknames)) {
c96876
-	 int inknameslen = strlen(inknames) + 1;
c96876
+	 int inknameslen = (int)strlen(inknames) + 1;
c96876
 	 const char* cp = inknames;
c96876
 	 while (ninks > 1) {
c96876
 	   cp = strchr(cp, '\0');
c96876
 	   if (cp) {
c96876
 	     cp++;
c96876
-	     inknameslen += (strlen(cp) + 1);
c96876
+	     inknameslen += ((int)strlen(cp) + 1);
c96876
 	   }
c96876
 	   ninks--;
c96876
          }
c96876
@@ -7554,23 +7578,23 @@ createImageSection(uint32_t sectsize, unsigned char **sect_buff_ptr)
c96876
 
c96876
   if (!sect_buff)
c96876
     {
c96876
-    sect_buff = (unsigned char *)limitMalloc(sectsize);
c96876
+    sect_buff = (unsigned char *)limitMalloc(sectsize + NUM_BUFF_OVERSIZE_BYTES);
c96876
     if (!sect_buff)
c96876
     {
c96876
         TIFFError("createImageSection", "Unable to allocate/reallocate section buffer");
c96876
         return (-1);
c96876
     }
c96876
-    _TIFFmemset(sect_buff, 0, sectsize);
c96876
+    _TIFFmemset(sect_buff, 0, sectsize + NUM_BUFF_OVERSIZE_BYTES);
c96876
     }
c96876
   else
c96876
     {
c96876
     if (prev_sectsize < sectsize)
c96876
       {
c96876
-      new_buff = _TIFFrealloc(sect_buff, sectsize);
c96876
+      new_buff = _TIFFrealloc(sect_buff, sectsize + NUM_BUFF_OVERSIZE_BYTES);
c96876
       if (!new_buff)
c96876
         {
c96876
           _TIFFfree (sect_buff);
c96876
-        sect_buff = (unsigned char *)limitMalloc(sectsize);
c96876
+        sect_buff = (unsigned char *)limitMalloc(sectsize + NUM_BUFF_OVERSIZE_BYTES);
c96876
         }
c96876
       else
c96876
         sect_buff = new_buff;
c96876
@@ -7580,7 +7604,7 @@ createImageSection(uint32_t sectsize, unsigned char **sect_buff_ptr)
c96876
           TIFFError("createImageSection", "Unable to allocate/reallocate section buffer");
c96876
           return (-1);
c96876
       }
c96876
-      _TIFFmemset(sect_buff, 0, sectsize);
c96876
+      _TIFFmemset(sect_buff, 0, sectsize + NUM_BUFF_OVERSIZE_BYTES);
c96876
       }
c96876
     }
c96876
 
c96876
@@ -7611,17 +7635,17 @@ processCropSelections(struct image_data *image, struct crop_mask *crop,
c96876
     cropsize = crop->bufftotal;
c96876
     crop_buff = seg_buffs[0].buffer; 
c96876
     if (!crop_buff)
c96876
-      crop_buff = (unsigned char *)limitMalloc(cropsize);
c96876
+      crop_buff = (unsigned char *)limitMalloc(cropsize + NUM_BUFF_OVERSIZE_BYTES);
c96876
     else
c96876
       {
c96876
       prev_cropsize = seg_buffs[0].size;
c96876
       if (prev_cropsize < cropsize)
c96876
         {
c96876
-        next_buff = _TIFFrealloc(crop_buff, cropsize);
c96876
+        next_buff = _TIFFrealloc(crop_buff, cropsize + NUM_BUFF_OVERSIZE_BYTES);
c96876
         if (! next_buff)
c96876
           {
c96876
           _TIFFfree (crop_buff);
c96876
-          crop_buff = (unsigned char *)limitMalloc(cropsize);
c96876
+          crop_buff = (unsigned char *)limitMalloc(cropsize + NUM_BUFF_OVERSIZE_BYTES);
c96876
           }
c96876
         else
c96876
           crop_buff = next_buff;
c96876
@@ -7634,7 +7658,7 @@ processCropSelections(struct image_data *image, struct crop_mask *crop,
c96876
       return (-1);
c96876
       }
c96876
  
c96876
-    _TIFFmemset(crop_buff, 0, cropsize);
c96876
+    _TIFFmemset(crop_buff, 0, cropsize + NUM_BUFF_OVERSIZE_BYTES);
c96876
     seg_buffs[0].buffer = crop_buff;
c96876
     seg_buffs[0].size = cropsize;
c96876
 
c96876
@@ -7714,17 +7738,17 @@ processCropSelections(struct image_data *image, struct crop_mask *crop,
c96876
         cropsize = crop->bufftotal;
c96876
       crop_buff = seg_buffs[i].buffer; 
c96876
       if (!crop_buff)
c96876
-        crop_buff = (unsigned char *)limitMalloc(cropsize);
c96876
+        crop_buff = (unsigned char *)limitMalloc(cropsize + NUM_BUFF_OVERSIZE_BYTES);
c96876
       else
c96876
         {
c96876
         prev_cropsize = seg_buffs[0].size;
c96876
         if (prev_cropsize < cropsize)
c96876
           {
c96876
-          next_buff = _TIFFrealloc(crop_buff, cropsize);
c96876
+          next_buff = _TIFFrealloc(crop_buff, cropsize + NUM_BUFF_OVERSIZE_BYTES);
c96876
           if (! next_buff)
c96876
             {
c96876
             _TIFFfree (crop_buff);
c96876
-            crop_buff = (unsigned char *)limitMalloc(cropsize);
c96876
+            crop_buff = (unsigned char *)limitMalloc(cropsize + NUM_BUFF_OVERSIZE_BYTES);
c96876
             }
c96876
           else
c96876
             crop_buff = next_buff;
c96876
@@ -7737,7 +7761,7 @@ processCropSelections(struct image_data *image, struct crop_mask *crop,
c96876
         return (-1);
c96876
         }
c96876
  
c96876
-      _TIFFmemset(crop_buff, 0, cropsize);
c96876
+      _TIFFmemset(crop_buff, 0, cropsize + NUM_BUFF_OVERSIZE_BYTES);
c96876
       seg_buffs[i].buffer = crop_buff;
c96876
       seg_buffs[i].size = cropsize;
c96876
 
c96876
@@ -7853,24 +7877,24 @@ createCroppedImage(struct image_data *image, struct crop_mask *crop,
c96876
   crop_buff = *crop_buff_ptr;
c96876
   if (!crop_buff)
c96876
     {
c96876
-    crop_buff = (unsigned char *)limitMalloc(cropsize);
c96876
+    crop_buff = (unsigned char *)limitMalloc(cropsize + NUM_BUFF_OVERSIZE_BYTES);
c96876
     if (!crop_buff)
c96876
     {
c96876
         TIFFError("createCroppedImage", "Unable to allocate/reallocate crop buffer");
c96876
         return (-1);
c96876
     }
c96876
-    _TIFFmemset(crop_buff, 0, cropsize);
c96876
+    _TIFFmemset(crop_buff, 0, cropsize + NUM_BUFF_OVERSIZE_BYTES);
c96876
     prev_cropsize = cropsize;
c96876
     }
c96876
   else
c96876
     {
c96876
     if (prev_cropsize < cropsize)
c96876
       {
c96876
-      new_buff = _TIFFrealloc(crop_buff, cropsize);
c96876
+      new_buff = _TIFFrealloc(crop_buff, cropsize + NUM_BUFF_OVERSIZE_BYTES);
c96876
       if (!new_buff)
c96876
         {
c96876
 	free (crop_buff);
c96876
-        crop_buff = (unsigned char *)limitMalloc(cropsize);
c96876
+        crop_buff = (unsigned char *)limitMalloc(cropsize + NUM_BUFF_OVERSIZE_BYTES);
c96876
         }
c96876
       else
c96876
         crop_buff = new_buff;
c96876
@@ -7879,7 +7903,7 @@ createCroppedImage(struct image_data *image, struct crop_mask *crop,
c96876
           TIFFError("createCroppedImage", "Unable to allocate/reallocate crop buffer");
c96876
           return (-1);
c96876
       }
c96876
-      _TIFFmemset(crop_buff, 0, cropsize);
c96876
+      _TIFFmemset(crop_buff, 0, cropsize + NUM_BUFF_OVERSIZE_BYTES);
c96876
       }
c96876
     }
c96876
 
c96876
@@ -8177,13 +8201,13 @@ writeCroppedImage(TIFF *in, TIFF *out, struct image_data *image,
c96876
      if (TIFFGetField(in, TIFFTAG_NUMBEROFINKS, &ninks)) {
c96876
        TIFFSetField(out, TIFFTAG_NUMBEROFINKS, ninks);
c96876
        if (TIFFGetField(in, TIFFTAG_INKNAMES, &inknames)) {
c96876
-	 int inknameslen = strlen(inknames) + 1;
c96876
+	 int inknameslen = (int)strlen(inknames) + 1;
c96876
 	 const char* cp = inknames;
c96876
 	 while (ninks > 1) {
c96876
 	   cp = strchr(cp, '\0');
c96876
 	   if (cp) {
c96876
 	     cp++;
c96876
-	     inknameslen += (strlen(cp) + 1);
c96876
+	     inknameslen += ((int)strlen(cp) + 1);
c96876
 	   }
c96876
 	   ninks--;
c96876
          }
c96876
@@ -8568,13 +8592,13 @@ rotateContigSamples32bits(uint16_t rotation, uint16_t spp, uint16_t bps, uint32_
c96876
         }
c96876
       else /* If we have a full buffer's worth, write it out */
c96876
         {
c96876
-        bytebuff1 = (buff2 >> 56);
c96876
+        bytebuff1 = (uint8_t)(buff2 >> 56);
c96876
         *dst++ = bytebuff1;
c96876
-        bytebuff2 = (buff2 >> 48);
c96876
+        bytebuff2 = (uint8_t)(buff2 >> 48);
c96876
         *dst++ = bytebuff2;
c96876
-        bytebuff3 = (buff2 >> 40);
c96876
+        bytebuff3 = (uint8_t)(buff2 >> 40);
c96876
         *dst++ = bytebuff3;
c96876
-        bytebuff4 = (buff2 >> 32);
c96876
+        bytebuff4 = (uint8_t)(buff2 >> 32);
c96876
         *dst++ = bytebuff4;
c96876
         ready_bits -= 32;
c96876
                     
c96876
@@ -8643,12 +8667,13 @@ rotateImage(uint16_t rotation, struct image_data *image, uint32_t *img_width,
c96876
               return (-1);
c96876
     }
c96876
 
c96876
-  if (!(rbuff = (unsigned char *)limitMalloc(buffsize)))
c96876
+  /* Add 3 padding bytes for extractContigSamplesShifted32bits */
c96876
+  if (!(rbuff = (unsigned char *)limitMalloc(buffsize + NUM_BUFF_OVERSIZE_BYTES)))
c96876
     {
c96876
-    TIFFError("rotateImage", "Unable to allocate rotation buffer of %1u bytes", buffsize);
c96876
+    TIFFError("rotateImage", "Unable to allocate rotation buffer of %1u bytes", buffsize + NUM_BUFF_OVERSIZE_BYTES);
c96876
     return (-1);
c96876
     }
c96876
-  _TIFFmemset(rbuff, '\0', buffsize);
c96876
+  _TIFFmemset(rbuff, '\0', buffsize + NUM_BUFF_OVERSIZE_BYTES);
c96876
 
c96876
   ibuff = *ibuff_ptr;
c96876
   switch (rotation)
c96876
@@ -9176,13 +9201,13 @@ reverseSamples32bits (uint16_t spp, uint16_t bps, uint32_t width,
c96876
         }
c96876
       else /* If we have a full buffer's worth, write it out */
c96876
         {
c96876
-        bytebuff1 = (buff2 >> 56);
c96876
+        bytebuff1 = (uint8_t)(buff2 >> 56);
c96876
         *dst++ = bytebuff1;
c96876
-        bytebuff2 = (buff2 >> 48);
c96876
+        bytebuff2 = (uint8_t)(buff2 >> 48);
c96876
         *dst++ = bytebuff2;
c96876
-        bytebuff3 = (buff2 >> 40);
c96876
+        bytebuff3 = (uint8_t)(buff2 >> 40);
c96876
         *dst++ = bytebuff3;
c96876
-        bytebuff4 = (buff2 >> 32);
c96876
+        bytebuff4 = (uint8_t)(buff2 >> 32);
c96876
         *dst++ = bytebuff4;
c96876
         ready_bits -= 32;
c96876
                     
c96876
@@ -9273,12 +9298,13 @@ mirrorImage(uint16_t spp, uint16_t bps, uint16_t mirror, uint32_t width, uint32_
c96876
     {
c96876
     case MIRROR_BOTH:
c96876
     case MIRROR_VERT: 
c96876
-             line_buff = (unsigned char *)limitMalloc(rowsize);
c96876
+             line_buff = (unsigned char *)limitMalloc(rowsize + NUM_BUFF_OVERSIZE_BYTES);
c96876
              if (line_buff == NULL)
c96876
                {
c96876
-	       TIFFError ("mirrorImage", "Unable to allocate mirror line buffer of %1u bytes", rowsize);
c96876
+	       TIFFError ("mirrorImage", "Unable to allocate mirror line buffer of %1u bytes", rowsize + NUM_BUFF_OVERSIZE_BYTES);
c96876
                return (-1);
c96876
                }
c96876
+             _TIFFmemset(line_buff, '\0', rowsize + NUM_BUFF_OVERSIZE_BYTES);
c96876
 
c96876
              dst = ibuff + (rowsize * (length - 1));
c96876
              for (row = 0; row < length / 2; row++)
c96876
@@ -9310,11 +9336,12 @@ mirrorImage(uint16_t spp, uint16_t bps, uint16_t mirror, uint32_t width, uint32_
c96876
 		}
c96876
 	      else
c96876
                 { /* non 8 bit per sample  data */
c96876
-                if (!(line_buff = (unsigned char *)limitMalloc(rowsize + 1)))
c96876
+                if (!(line_buff = (unsigned char *)limitMalloc(rowsize + NUM_BUFF_OVERSIZE_BYTES)))
c96876
                   {
c96876
                   TIFFError("mirrorImage", "Unable to allocate mirror line buffer");
c96876
                   return (-1);
c96876
                   }
c96876
+                _TIFFmemset(line_buff, '\0', rowsize + NUM_BUFF_OVERSIZE_BYTES);
c96876
                 bytes_per_sample = (bps + 7) / 8;
c96876
                 bytes_per_pixel  = ((bps * spp) + 7) / 8;
c96876
                 if (bytes_per_pixel < (bytes_per_sample + 1))
c96876
@@ -9326,7 +9353,7 @@ mirrorImage(uint16_t spp, uint16_t bps, uint16_t mirror, uint32_t width, uint32_
c96876
                   {
c96876
 		  row_offset = row * rowsize;
c96876
                   src = ibuff + row_offset;
c96876
-                  _TIFFmemset (line_buff, '\0', rowsize);
c96876
+                  _TIFFmemset (line_buff, '\0', rowsize + NUM_BUFF_OVERSIZE_BYTES);
c96876
                   switch (shift_width)
c96876
                     {
c96876
                     case 1: if (reverseSamples16bits(spp, bps, width, src, line_buff))