Blame SOURCES/libtiff-CVE-2018-7456.patch

0e6869
From f1c2759436a0ee5181cae7176114f57e45f0eb16 Mon Sep 17 00:00:00 2001
0e6869
From: Hugo Lefeuvre <hle@debian.org>
0e6869
Date: Sun, 8 Apr 2018 14:07:08 -0400
0e6869
Subject: [PATCH] Fix NULL pointer dereference in TIFFPrintDirectory
0e6869
0e6869
The TIFFPrintDirectory function relies on the following assumptions,
0e6869
supposed to be guaranteed by the specification:
0e6869
0e6869
(a) A Transfer Function field is only present if the TIFF file has
0e6869
    photometric type < 3.
0e6869
0e6869
(b) If SamplesPerPixel > Color Channels, then the ExtraSamples field
0e6869
    has count SamplesPerPixel - (Color Channels) and contains
0e6869
    information about supplementary channels.
0e6869
0e6869
While respect of (a) and (b) are essential for the well functioning of
0e6869
TIFFPrintDirectory, no checks are realized neither by the callee nor
0e6869
by TIFFPrintDirectory itself. Hence, following scenarios might happen
0e6869
and trigger the NULL pointer dereference:
0e6869
0e6869
(1) TIFF File of photometric type 4 or more has illegal Transfer
0e6869
    Function field.
0e6869
0e6869
(2) TIFF File has photometric type 3 or less and defines a
0e6869
    SamplesPerPixel field such that SamplesPerPixel > Color Channels
0e6869
    without defining all extra samples in the ExtraSamples fields.
0e6869
0e6869
In this patch, we address both issues with respect of the following
0e6869
principles:
0e6869
0e6869
(A) In the case of (1), the defined transfer table should be printed
0e6869
    safely even if it isn't 'legal'. This allows us to avoid expensive
0e6869
    checks in TIFFPrintDirectory. Also, it is quite possible that
0e6869
    an alternative photometric type would be developed (not part of the
0e6869
    standard) and would allow definition of Transfer Table. We want
0e6869
    libtiff to be able to handle this scenario out of the box.
0e6869
0e6869
(B) In the case of (2), the transfer table should be printed at its
0e6869
    right size, that is if TIFF file has photometric type Palette
0e6869
    then the transfer table should have one row and not three, even
0e6869
    if two extra samples are declared.
0e6869
0e6869
In order to fulfill (A) we simply add a new 'i < 3' end condition to
0e6869
the broken TIFFPrintDirectory loop. This makes sure that in any case
0e6869
where (b) would be respected but not (a), everything stays fine.
0e6869
0e6869
(B) is fulfilled by the loop condition
0e6869
'i < td->td_samplesperpixel - td->td_extrasamples'. This is enough as
0e6869
long as (b) is respected.
0e6869
0e6869
Naturally, we also make sure (b) is respected. This is done in the
0e6869
TIFFReadDirectory function by making sure any non-color channel is
0e6869
counted in ExtraSamples.
0e6869
0e6869
This commit addresses CVE-2018-7456.
0e6869
---
0e6869
 libtiff/tif_dirread.c | 61 +++++++++++++++++++++++++++++++++++++++++++
0e6869
 libtiff/tif_print.c   |  2 +-
0e6869
 libtiff/tif_unix.c    |  9 +++++++
0e6869
 libtiff/tiffio.h      |  1 +
0e6869
 4 files changed, 72 insertions(+), 1 deletion(-)
0e6869
0e6869
diff --git a/libtiff/tif_dirread.c b/libtiff/tif_dirread.c
0e6869
index 0a9484d..8dc40b5 100644
0e6869
--- a/libtiff/tif_dirread.c
0e6869
+++ b/libtiff/tif_dirread.c
0e6869
@@ -65,6 +65,35 @@ static	int TIFFFetchDoubleArray(TIFF*, TIFFDirEntry*, double*);
0e6869
 static	int TIFFFetchAnyArray(TIFF*, TIFFDirEntry*, double*);
0e6869
 static	int TIFFFetchShortPair(TIFF*, TIFFDirEntry*);
0e6869
 static	void ChopUpSingleUncompressedStrip(TIFF*);
0e6869
+static	int _TIFFGetMaxColorChannels(uint16 photometric);
0e6869
+
0e6869
+/*
0e6869
+ * Return the maximum number of color channels specified for a given photometric
0e6869
+ * type. 0 is returned if photometric type isn't supported or no default value
0e6869
+ * is defined by the specification.
0e6869
+ */
0e6869
+static int _TIFFGetMaxColorChannels( uint16 photometric )
0e6869
+{
0e6869
+    switch (photometric) {
0e6869
+	case PHOTOMETRIC_PALETTE:
0e6869
+	case PHOTOMETRIC_MINISWHITE:
0e6869
+	case PHOTOMETRIC_MINISBLACK:
0e6869
+            return 1;
0e6869
+	case PHOTOMETRIC_YCBCR:
0e6869
+	case PHOTOMETRIC_RGB:
0e6869
+	case PHOTOMETRIC_CIELAB:
0e6869
+            return 3;
0e6869
+	case PHOTOMETRIC_SEPARATED:
0e6869
+	case PHOTOMETRIC_MASK:
0e6869
+            return 4;
0e6869
+	case PHOTOMETRIC_LOGL:
0e6869
+	case PHOTOMETRIC_LOGLUV:
0e6869
+	case PHOTOMETRIC_ITULAB:
0e6869
+	case PHOTOMETRIC_ICCLAB:
0e6869
+	default:
0e6869
+            return 0;
0e6869
+    }
0e6869
+}
0e6869
 
0e6869
 /*
0e6869
  * Read the next TIFF directory from a file and convert it to the internal
0e6869
@@ -86,6 +115,7 @@ TIFFReadDirectory(TIFF* tif)
0e6869
 	uint16 previous_tag = 0;
0e6869
 	int diroutoforderwarning = 0, compressionknown = 0;
0e6869
 	int haveunknowntags = 0;
0e6869
+	int color_channels;
0e6869
 
0e6869
 	tif->tif_diroff = tif->tif_nextdiroff;
0e6869
 	/*
0e6869
@@ -617,6 +647,37 @@ TIFFReadDirectory(TIFF* tif)
0e6869
 			}
0e6869
 		}
0e6869
 	}
0e6869
+
0e6869
+	/*
0e6869
+	 * Make sure all non-color channels are extrasamples.
0e6869
+	 * If it's not the case, define them as such.
0e6869
+	 */
0e6869
+        color_channels = _TIFFGetMaxColorChannels(tif->tif_dir.td_photometric);
0e6869
+        if (color_channels && tif->tif_dir.td_samplesperpixel - tif->tif_dir.td_extrasamples > color_channels) {
0e6869
+                uint16 old_extrasamples;
0e6869
+                uint16 *new_sampleinfo;
0e6869
+
0e6869
+                TIFFWarningExt(tif->tif_clientdata,module, "Sum of Photometric type-related "
0e6869
+                    "color channels and ExtraSamples doesn't match SamplesPerPixel. "
0e6869
+                    "Defining non-color channels as ExtraSamples.");
0e6869
+
0e6869
+                old_extrasamples = tif->tif_dir.td_extrasamples;
0e6869
+                tif->tif_dir.td_extrasamples = (tif->tif_dir.td_samplesperpixel - color_channels);
0e6869
+
0e6869
+                // sampleinfo should contain information relative to these new extra samples
0e6869
+                new_sampleinfo = (uint16*) _TIFFcalloc(tif->tif_dir.td_extrasamples, sizeof(uint16));
0e6869
+                if (!new_sampleinfo) {
0e6869
+                    TIFFErrorExt(tif->tif_clientdata, module, "Failed to allocate memory for "
0e6869
+                                "temporary new sampleinfo array (%d 16 bit elements)",
0e6869
+                                tif->tif_dir.td_extrasamples);
0e6869
+                    goto bad;
0e6869
+                }
0e6869
+
0e6869
+                memcpy(new_sampleinfo, tif->tif_dir.td_sampleinfo, old_extrasamples * sizeof(uint16));
0e6869
+                _TIFFsetShortArray(&tif->tif_dir.td_sampleinfo, new_sampleinfo, tif->tif_dir.td_extrasamples);
0e6869
+                _TIFFfree(new_sampleinfo);
0e6869
+        }
0e6869
+
0e6869
 	/*
0e6869
 	 * Verify Palette image has a Colormap.
0e6869
 	 */
0e6869
diff --git a/libtiff/tif_print.c b/libtiff/tif_print.c
0e6869
index 0f6ea01..1ec4f26 100644
0e6869
--- a/libtiff/tif_print.c
0e6869
+++ b/libtiff/tif_print.c
0e6869
@@ -503,7 +503,7 @@ TIFFPrintDirectory(TIFF* tif, FILE* fd, long flags)
0e6869
 			for (l = 0; l < n; l++) {
0e6869
 				fprintf(fd, "    %2lu: %5u",
0e6869
 				    l, td->td_transferfunction[0][l]);
0e6869
-				for (i = 1; i < td->td_samplesperpixel; i++)
0e6869
+				for (i = 1; i < td->td_samplesperpixel - td->td_extrasamples && i < 3; i++)
0e6869
 					fprintf(fd, " %5u",
0e6869
 					    td->td_transferfunction[i][l]);
0e6869
 				fputc('\n', fd);
0e6869
diff --git a/libtiff/tif_unix.c b/libtiff/tif_unix.c
0e6869
index b73e80d..5d29040 100644
0e6869
--- a/libtiff/tif_unix.c
0e6869
+++ b/libtiff/tif_unix.c
0e6869
@@ -241,6 +241,15 @@ _TIFFmalloc(tsize_t s)
0e6869
 	return (malloc((size_t) s));
0e6869
 }
0e6869
 
0e6869
+void*
0e6869
+_TIFFcalloc(tsize_t nmemb, tsize_t siz)
0e6869
+{
0e6869
+    if( nmemb == 0 || siz == 0 )
0e6869
+        return ((void *) NULL);
0e6869
+
0e6869
+    return calloc((size_t) nmemb, (size_t)siz);
0e6869
+}
0e6869
+
0e6869
 void
0e6869
 _TIFFfree(tdata_t p)
0e6869
 {
0e6869
diff --git a/libtiff/tiffio.h b/libtiff/tiffio.h
0e6869
index 06ec25c..3cf8e75 100644
0e6869
--- a/libtiff/tiffio.h
0e6869
+++ b/libtiff/tiffio.h
0e6869
@@ -281,6 +281,7 @@ extern	TIFFCodec* TIFFGetConfiguredCODECs(void);
0e6869
  */
0e6869
 
0e6869
 extern	tdata_t _TIFFmalloc(tsize_t);
0e6869
+extern	tdata_t _TIFFcalloc(tsize_t, tsize_t);
0e6869
 extern	tdata_t _TIFFrealloc(tdata_t, tsize_t);
0e6869
 extern	void _TIFFmemset(tdata_t, int, tsize_t);
0e6869
 extern	void _TIFFmemcpy(tdata_t, const tdata_t, tsize_t);
0e6869
-- 
0e6869
2.17.0
0e6869