Blame SOURCES/libtiff-coverity.patch

3528ec
From f4ee7a53cc422490986225c49f92935b3ba52866 Mon Sep 17 00:00:00 2001
3528ec
From: =?UTF-8?q?Nikola=20Forr=C3=B3?= <nforro@redhat.com>
3528ec
Date: Thu, 13 Dec 2018 17:06:44 +0100
3528ec
Subject: [PATCH] Fix Covscan defects
3528ec
3528ec
---
3528ec
 contrib/addtiffo/addtiffo.c |  3 ++-
3528ec
 libtiff/tif_dir.c           |  2 +-
3528ec
 libtiff/tif_ojpeg.c         |  7 ++++++-
3528ec
 tools/gif2tiff.c            | 21 +++++++++++++++------
3528ec
 tools/ras2tiff.c            | 22 +++++++++++++++++++++-
3528ec
 tools/rasterfile.h          | 16 +++++++++-------
3528ec
 tools/tiffcrop.c            |  4 ++++
3528ec
 7 files changed, 58 insertions(+), 17 deletions(-)
3528ec
3528ec
diff --git a/contrib/addtiffo/addtiffo.c b/contrib/addtiffo/addtiffo.c
3528ec
index d3920e2..47f5fa8 100644
3528ec
--- a/contrib/addtiffo/addtiffo.c
3528ec
+++ b/contrib/addtiffo/addtiffo.c
3528ec
@@ -120,7 +120,8 @@ int main( int argc, char ** argv )
3528ec
     while( nOverviewCount < argc - 2 && nOverviewCount < 100 )
3528ec
     {
3528ec
         anOverviews[nOverviewCount] = atoi(argv[nOverviewCount+2]);
3528ec
-        if( anOverviews[nOverviewCount] <= 0)
3528ec
+        if( (anOverviews[nOverviewCount] <= 0) ||
3528ec
+            ((anOverviews[nOverviewCount] > 1024)))
3528ec
         {
3528ec
             fprintf( stderr, "Incorrect parameters\n" );
3528ec
             return(1);
3528ec
diff --git a/libtiff/tif_dir.c b/libtiff/tif_dir.c
3528ec
index f812fa2..9c613da 100644
3528ec
--- a/libtiff/tif_dir.c
3528ec
+++ b/libtiff/tif_dir.c
3528ec
@@ -706,7 +706,7 @@ badvaluedouble:
3528ec
         TIFFErrorExt(tif->tif_clientdata, module,
3528ec
              "%s: Bad value %f for \"%s\" tag",
3528ec
              tif->tif_name, dblval,
3528ec
-		     fip->field_name);
3528ec
+		     fip ? fip->field_name : "Unknown");
3528ec
         va_end(ap);
3528ec
         }
3528ec
     return (0);
3528ec
diff --git a/libtiff/tif_ojpeg.c b/libtiff/tif_ojpeg.c
3528ec
index 6ea3c38..1d9c77c 100644
3528ec
--- a/libtiff/tif_ojpeg.c
3528ec
+++ b/libtiff/tif_ojpeg.c
3528ec
@@ -528,6 +528,8 @@ OJPEGVSetField(TIFF* tif, uint32 tag, va_list ap)
3528ec
 	uint32 ma;
3528ec
 	uint64* mb;
3528ec
 	uint32 n;
3528ec
+	const TIFFField* fip;
3528ec
+
3528ec
 	switch(tag)
3528ec
 	{
3528ec
 		case TIFFTAG_JPEGIFOFFSET:
3528ec
@@ -597,7 +599,10 @@ OJPEGVSetField(TIFF* tif, uint32 tag, va_list ap)
3528ec
 		default:
3528ec
 			return (*sp->vsetparent)(tif,tag,ap);
3528ec
 	}
3528ec
-	TIFFSetFieldBit(tif,TIFFFieldWithTag(tif,tag)->field_bit);
3528ec
+	fip = TIFFFieldWithTag(tif,tag);
3528ec
+	if( fip == NULL ) /* shouldn't happen */
3528ec
+	    return(0);
3528ec
+	TIFFSetFieldBit(tif,fip->field_bit);
3528ec
 	tif->tif_flags|=TIFF_DIRTYDIRECT;
3528ec
 	return(1);
3528ec
 }
3528ec
diff --git a/tools/gif2tiff.c b/tools/gif2tiff.c
3528ec
index e89ac5b..012345d 100644
3528ec
--- a/tools/gif2tiff.c
3528ec
+++ b/tools/gif2tiff.c
3528ec
@@ -38,6 +38,7 @@
3528ec
 #include <stdio.h>
3528ec
 #include <stdlib.h>
3528ec
 #include <string.h>
3528ec
+#include <errno.h>
3528ec
 #include <math.h>
3528ec
 
3528ec
 #ifdef HAVE_UNISTD_H
3528ec
@@ -266,13 +267,15 @@ readgifimage(char* mode)
3528ec
     unsigned char localmap[256][3];
3528ec
     int localbits;
3528ec
     int status;
3528ec
+    size_t raster_size;
3528ec
 
3528ec
-    if (fread(buf, 1, 9, infile) == 0) {
3528ec
-        perror(filename);
3528ec
+    if (fread(buf, 1, 9, infile) != 9) {
3528ec
+        fprintf(stderr, "short read from file %s (%s)\n",
3528ec
+                filename, strerror(errno));
3528ec
 	return (0);
3528ec
     }
3528ec
-    width = buf[4] + (buf[5] << 8);
3528ec
-    height = buf[6] + (buf[7] << 8);
3528ec
+    width = (buf[4] + (buf[5] << 8)) & 0xffff; /* 16 bit */
3528ec
+    height = (buf[6] + (buf[7] << 8)) & 0xffff;  /* 16 bit */
3528ec
     local = buf[8] & 0x80;
3528ec
     interleaved = buf[8] & 0x40;
3528ec
 
3528ec
@@ -280,11 +283,17 @@ readgifimage(char* mode)
3528ec
         fprintf(stderr, "no colormap present for image\n");
3528ec
         return (0);
3528ec
     }
3528ec
-    if (width == 0 || height == 0) {
3528ec
+    if (width == 0UL || height == 0UL || (width > 2000000000UL / height)) {
3528ec
         fprintf(stderr, "Invalid value of width or height\n");
3528ec
         return(0);
3528ec
     }
3528ec
-    if ((raster = (unsigned char*) _TIFFmalloc(width*height+EXTRAFUDGE)) == NULL) {
3528ec
+    raster_size=width*height;
3528ec
+    if ((raster_size/width) == height) {
3528ec
+        raster_size += EXTRAFUDGE;  /* Add elbow room */
3528ec
+    } else {
3528ec
+        raster_size=0;
3528ec
+    }
3528ec
+    if ((raster = (unsigned char*) _TIFFmalloc(raster_size)) == NULL) {
3528ec
         fprintf(stderr, "not enough memory for image\n");
3528ec
         return (0);
3528ec
     }
3528ec
diff --git a/tools/ras2tiff.c b/tools/ras2tiff.c
3528ec
index ec8a071..007dd8c 100644
3528ec
--- a/tools/ras2tiff.c
3528ec
+++ b/tools/ras2tiff.c
3528ec
@@ -30,6 +30,7 @@
3528ec
 #include <stdlib.h>
3528ec
 #include <string.h>
3528ec
 #include <ctype.h>
3528ec
+#include <limits.h>
3528ec
 
3528ec
 #ifdef HAVE_UNISTD_H
3528ec
 # include <unistd.h>
3528ec
@@ -122,6 +123,25 @@ main(int argc, char* argv[])
3528ec
 		fclose(in);
3528ec
 		return (-3);
3528ec
 	}
3528ec
+        if ((h.ras_width <= 0) || (h.ras_width >= INT_MAX) ||
3528ec
+            (h.ras_height <= 0) || (h.ras_height >= INT_MAX) ||
3528ec
+            (h.ras_depth <= 0) || (h.ras_depth >= INT_MAX) ||
3528ec
+            (h.ras_length <= 0) || (h.ras_length >= INT_MAX) ||
3528ec
+            (h.ras_type < 0) ||
3528ec
+            (h.ras_maptype < 0) ||
3528ec
+            (h.ras_maplength < 0) || (h.ras_maplength >= INT_MAX)) {
3528ec
+                fprintf(stderr, "%s: Improper image header.\n", argv[optind]);
3528ec
+                fclose(in);
3528ec
+		return (-2);
3528ec
+        }
3528ec
+        if ((h.ras_depth != 1) &&
3528ec
+            (h.ras_depth != 8) &&
3528ec
+            (h.ras_depth != 24)) {
3528ec
+                fprintf(stderr, "%s: Improper image depth (%d).\n",
3528ec
+                        argv[optind], h.ras_depth);
3528ec
+                fclose(in);
3528ec
+		return (-2);
3528ec
+        }
3528ec
 	out = TIFFOpen(argv[optind+1], "w");
3528ec
 	if (out == NULL)
3528ec
 	{
3528ec
@@ -153,7 +173,7 @@ main(int argc, char* argv[])
3528ec
 		mapsize = 1<
3528ec
 		if (h.ras_maplength > mapsize*3) {
3528ec
 			fprintf(stderr,
3528ec
-			    "%s: Huh, %ld colormap entries, should be %d?\n",
3528ec
+			    "%s: Huh, %d colormap entries, should be %d?\n",
3528ec
 			    argv[optind], h.ras_maplength, mapsize*3);
3528ec
 			return (-7);
3528ec
 		}
3528ec
diff --git a/tools/rasterfile.h b/tools/rasterfile.h
3528ec
index 833e095..33da707 100644
3528ec
--- a/tools/rasterfile.h
3528ec
+++ b/tools/rasterfile.h
3528ec
@@ -1,17 +1,19 @@
3528ec
 /* $Header: /cvs/libtiff/tools/rasterfile.h,v 1.3 2003/11/12 19:14:33 dron Exp $ */
3528ec
 
3528ec
+#include "tiff.h"
3528ec
+
3528ec
 /*
3528ec
  * Description of header for files containing raster images
3528ec
  */
3528ec
 struct rasterfile {
3528ec
 	char	ras_magic[4];		/* magic number */
3528ec
-	long	ras_width;		/* width (pixels) of image */
3528ec
-	long	ras_height;		/* height (pixels) of image */
3528ec
-	long	ras_depth;		/* depth (1, 8, or 24 bits) of pixel */
3528ec
-	long	ras_length;		/* length (bytes) of image */
3528ec
-	long	ras_type;		/* type of file; see RT_* below */
3528ec
-	long	ras_maptype;		/* type of colormap; see RMT_* below */
3528ec
-	long	ras_maplength;		/* length (bytes) of following map */
3528ec
+       int32   ras_width;              /* width (pixels) of image */
3528ec
+       int32   ras_height;             /* height (pixels) of image */
3528ec
+       int32   ras_depth;              /* depth (1, 8, or 24 bits) of pixel */
3528ec
+       int32   ras_length;             /* length (bytes) of image */
3528ec
+       int32   ras_type;               /* type of file; see RT_* below */
3528ec
+       int32   ras_maptype;            /* type of colormap; see RMT_* below */
3528ec
+       int32   ras_maplength;          /* length (bytes) of following map */
3528ec
 	/* color map follows for ras_maplength bytes, followed by image */
3528ec
 };
3528ec
 #define	RAS_MAGIC	"\x59\xa6\x6a\x95"
3528ec
diff --git a/tools/tiffcrop.c b/tools/tiffcrop.c
3528ec
index 0192f3f..ae6ec1a 100644
3528ec
--- a/tools/tiffcrop.c
3528ec
+++ b/tools/tiffcrop.c
3528ec
@@ -2029,6 +2029,10 @@ void  process_command_opts (int argc, char *argv[], char *mp, char *mode, uint32
3528ec
                     {
3528ec
 		    crop_data->zones++;
3528ec
 		    opt_offset = strchr(opt_ptr, ':');
3528ec
+		    if (!opt_offset) {
3528ec
+			TIFFError("Wrong parameter syntax for -Z", "tiffcrop -h");
3528ec
+			exit(-1);
3528ec
+		    }
3528ec
                     *opt_offset = '\0';
3528ec
                     crop_data->zonelist[i].position = atoi(opt_ptr);
3528ec
                     crop_data->zonelist[i].total    = atoi(opt_offset + 1);
3528ec
-- 
3528ec
2.21.0
3528ec