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

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