|
|
7c98bb |
Defend against integer overflow in buffer size calculations within tiff2pdf.
|
|
|
7c98bb |
CVE-2012-2113
|
|
|
7c98bb |
|
|
|
7c98bb |
|
|
|
7c98bb |
diff -Naur tiff-3.9.4.orig/tools/tiff2pdf.c tiff-3.9.4/tools/tiff2pdf.c
|
|
|
7c98bb |
--- tiff-3.9.4.orig/tools/tiff2pdf.c 2010-06-13 16:51:44.000000000 -0400
|
|
|
7c98bb |
+++ tiff-3.9.4/tools/tiff2pdf.c 2012-06-27 13:13:44.581229393 -0400
|
|
|
7c98bb |
@@ -44,6 +44,7 @@
|
|
|
7c98bb |
# include <io.h>
|
|
|
7c98bb |
#endif
|
|
|
7c98bb |
|
|
|
7c98bb |
+#include "tiffiop.h"
|
|
|
7c98bb |
#include "tiffio.h"
|
|
|
7c98bb |
|
|
|
7c98bb |
#ifndef HAVE_GETOPT
|
|
|
7c98bb |
@@ -414,6 +415,34 @@
|
|
|
7c98bb |
(void) handle, (void) data, (void) offset;
|
|
|
7c98bb |
}
|
|
|
7c98bb |
|
|
|
7c98bb |
+static uint64
|
|
|
7c98bb |
+checkAdd64(uint64 summand1, uint64 summand2, T2P* t2p)
|
|
|
7c98bb |
+{
|
|
|
7c98bb |
+ uint64 bytes = summand1 + summand2;
|
|
|
7c98bb |
+
|
|
|
7c98bb |
+ if (bytes - summand1 != summand2) {
|
|
|
7c98bb |
+ TIFFError(TIFF2PDF_MODULE, "Integer overflow");
|
|
|
7c98bb |
+ t2p->t2p_error = T2P_ERR_ERROR;
|
|
|
7c98bb |
+ bytes = 0;
|
|
|
7c98bb |
+ }
|
|
|
7c98bb |
+
|
|
|
7c98bb |
+ return bytes;
|
|
|
7c98bb |
+}
|
|
|
7c98bb |
+
|
|
|
7c98bb |
+static uint64
|
|
|
7c98bb |
+checkMultiply64(uint64 first, uint64 second, T2P* t2p)
|
|
|
7c98bb |
+{
|
|
|
7c98bb |
+ uint64 bytes = first * second;
|
|
|
7c98bb |
+
|
|
|
7c98bb |
+ if (second && bytes / second != first) {
|
|
|
7c98bb |
+ TIFFError(TIFF2PDF_MODULE, "Integer overflow");
|
|
|
7c98bb |
+ t2p->t2p_error = T2P_ERR_ERROR;
|
|
|
7c98bb |
+ bytes = 0;
|
|
|
7c98bb |
+ }
|
|
|
7c98bb |
+
|
|
|
7c98bb |
+ return bytes;
|
|
|
7c98bb |
+}
|
|
|
7c98bb |
+
|
|
|
7c98bb |
/*
|
|
|
7c98bb |
|
|
|
7c98bb |
This is the main function.
|
|
|
7c98bb |
@@ -1828,9 +1857,7 @@
|
|
|
7c98bb |
tstrip_t i=0;
|
|
|
7c98bb |
tstrip_t stripcount=0;
|
|
|
7c98bb |
#endif
|
|
|
7c98bb |
-#ifdef OJPEG_SUPPORT
|
|
|
7c98bb |
- tsize_t k = 0;
|
|
|
7c98bb |
-#endif
|
|
|
7c98bb |
+ uint64 k = 0;
|
|
|
7c98bb |
|
|
|
7c98bb |
if(t2p->pdf_transcode == T2P_TRANSCODE_RAW){
|
|
|
7c98bb |
#ifdef CCITT_SUPPORT
|
|
|
7c98bb |
@@ -1858,19 +1885,25 @@
|
|
|
7c98bb |
}
|
|
|
7c98bb |
stripcount=TIFFNumberOfStrips(input);
|
|
|
7c98bb |
for(i=0;i
|
|
|
7c98bb |
- k += sbc[i];
|
|
|
7c98bb |
+ k = checkAdd64(k, sbc[i], t2p);
|
|
|
7c98bb |
}
|
|
|
7c98bb |
if(TIFFGetField(input, TIFFTAG_JPEGIFOFFSET, &(t2p->tiff_dataoffset))){
|
|
|
7c98bb |
if(t2p->tiff_dataoffset != 0){
|
|
|
7c98bb |
if(TIFFGetField(input, TIFFTAG_JPEGIFBYTECOUNT, &(t2p->tiff_datasize))!=0){
|
|
|
7c98bb |
if(t2p->tiff_datasize < k) {
|
|
|
7c98bb |
- t2p->pdf_ojpegiflength=t2p->tiff_datasize;
|
|
|
7c98bb |
- t2p->tiff_datasize+=k;
|
|
|
7c98bb |
- t2p->tiff_datasize+=6;
|
|
|
7c98bb |
- t2p->tiff_datasize+=2*stripcount;
|
|
|
7c98bb |
TIFFWarning(TIFF2PDF_MODULE,
|
|
|
7c98bb |
"Input file %s has short JPEG interchange file byte count",
|
|
|
7c98bb |
TIFFFileName(input));
|
|
|
7c98bb |
+ t2p->pdf_ojpegiflength=t2p->tiff_datasize;
|
|
|
7c98bb |
+ k = checkAdd64(k, t2p->tiff_datasize, t2p);
|
|
|
7c98bb |
+ k = checkAdd64(k, 6, t2p);
|
|
|
7c98bb |
+ k = checkAdd64(k, stripcount, t2p);
|
|
|
7c98bb |
+ k = checkAdd64(k, stripcount, t2p);
|
|
|
7c98bb |
+ t2p->tiff_datasize = (tsize_t) k;
|
|
|
7c98bb |
+ if ((uint64) t2p->tiff_datasize != k) {
|
|
|
7c98bb |
+ TIFFError(TIFF2PDF_MODULE, "Integer overflow");
|
|
|
7c98bb |
+ t2p->t2p_error = T2P_ERR_ERROR;
|
|
|
7c98bb |
+ }
|
|
|
7c98bb |
return;
|
|
|
7c98bb |
}
|
|
|
7c98bb |
return;
|
|
|
7c98bb |
@@ -1883,9 +1916,14 @@
|
|
|
7c98bb |
}
|
|
|
7c98bb |
}
|
|
|
7c98bb |
}
|
|
|
7c98bb |
- t2p->tiff_datasize+=k;
|
|
|
7c98bb |
- t2p->tiff_datasize+=2*stripcount;
|
|
|
7c98bb |
- t2p->tiff_datasize+=2048;
|
|
|
7c98bb |
+ k = checkAdd64(k, stripcount, t2p);
|
|
|
7c98bb |
+ k = checkAdd64(k, stripcount, t2p);
|
|
|
7c98bb |
+ k = checkAdd64(k, 2048, t2p);
|
|
|
7c98bb |
+ t2p->tiff_datasize = (tsize_t) k;
|
|
|
7c98bb |
+ if ((uint64) t2p->tiff_datasize != k) {
|
|
|
7c98bb |
+ TIFFError(TIFF2PDF_MODULE, "Integer overflow");
|
|
|
7c98bb |
+ t2p->t2p_error = T2P_ERR_ERROR;
|
|
|
7c98bb |
+ }
|
|
|
7c98bb |
return;
|
|
|
7c98bb |
}
|
|
|
7c98bb |
#endif
|
|
|
7c98bb |
@@ -1894,11 +1932,11 @@
|
|
|
7c98bb |
uint32 count = 0;
|
|
|
7c98bb |
if(TIFFGetField(input, TIFFTAG_JPEGTABLES, &count, &jpt) != 0 ){
|
|
|
7c98bb |
if(count > 4){
|
|
|
7c98bb |
- t2p->tiff_datasize += count;
|
|
|
7c98bb |
- t2p->tiff_datasize -= 2; /* don't use EOI of header */
|
|
|
7c98bb |
+ k += count;
|
|
|
7c98bb |
+ k -= 2; /* don't use EOI of header */
|
|
|
7c98bb |
}
|
|
|
7c98bb |
} else {
|
|
|
7c98bb |
- t2p->tiff_datasize = 2; /* SOI for first strip */
|
|
|
7c98bb |
+ k = 2; /* SOI for first strip */
|
|
|
7c98bb |
}
|
|
|
7c98bb |
stripcount=TIFFNumberOfStrips(input);
|
|
|
7c98bb |
if(!TIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc)){
|
|
|
7c98bb |
@@ -1909,18 +1947,33 @@
|
|
|
7c98bb |
return;
|
|
|
7c98bb |
}
|
|
|
7c98bb |
for(i=0;i
|
|
|
7c98bb |
- t2p->tiff_datasize += sbc[i];
|
|
|
7c98bb |
- t2p->tiff_datasize -=4; /* don't use SOI or EOI of strip */
|
|
|
7c98bb |
+ k = checkAdd64(k, sbc[i], t2p);
|
|
|
7c98bb |
+ k -=4; /* don't use SOI or EOI of strip */
|
|
|
7c98bb |
+ }
|
|
|
7c98bb |
+ k = checkAdd64(k, 2, t2p); /* use EOI of last strip */
|
|
|
7c98bb |
+ t2p->tiff_datasize = (tsize_t) k;
|
|
|
7c98bb |
+ if ((uint64) t2p->tiff_datasize != k) {
|
|
|
7c98bb |
+ TIFFError(TIFF2PDF_MODULE, "Integer overflow");
|
|
|
7c98bb |
+ t2p->t2p_error = T2P_ERR_ERROR;
|
|
|
7c98bb |
}
|
|
|
7c98bb |
- t2p->tiff_datasize +=2; /* use EOI of last strip */
|
|
|
7c98bb |
return;
|
|
|
7c98bb |
}
|
|
|
7c98bb |
#endif
|
|
|
7c98bb |
(void) 0;
|
|
|
7c98bb |
}
|
|
|
7c98bb |
- t2p->tiff_datasize=TIFFScanlineSize(input) * t2p->tiff_length;
|
|
|
7c98bb |
+ k = checkMultiply64(TIFFScanlineSize(input), t2p->tiff_length, t2p);
|
|
|
7c98bb |
if(t2p->tiff_planar==PLANARCONFIG_SEPARATE){
|
|
|
7c98bb |
- t2p->tiff_datasize*= t2p->tiff_samplesperpixel;
|
|
|
7c98bb |
+ k = checkMultiply64(k, t2p->tiff_samplesperpixel, t2p);
|
|
|
7c98bb |
+ }
|
|
|
7c98bb |
+ if (k == 0) {
|
|
|
7c98bb |
+ /* Assume we had overflow inside TIFFScanlineSize */
|
|
|
7c98bb |
+ t2p->t2p_error = T2P_ERR_ERROR;
|
|
|
7c98bb |
+ }
|
|
|
7c98bb |
+
|
|
|
7c98bb |
+ t2p->tiff_datasize = (tsize_t) k;
|
|
|
7c98bb |
+ if ((uint64) t2p->tiff_datasize != k) {
|
|
|
7c98bb |
+ TIFFError(TIFF2PDF_MODULE, "Integer overflow");
|
|
|
7c98bb |
+ t2p->t2p_error = T2P_ERR_ERROR;
|
|
|
7c98bb |
}
|
|
|
7c98bb |
|
|
|
7c98bb |
return;
|
|
|
7c98bb |
@@ -1938,6 +1991,7 @@
|
|
|
7c98bb |
#ifdef JPEG_SUPPORT
|
|
|
7c98bb |
unsigned char* jpt;
|
|
|
7c98bb |
#endif
|
|
|
7c98bb |
+ uint64 k;
|
|
|
7c98bb |
|
|
|
7c98bb |
edge |= t2p_tile_is_right_edge(t2p->tiff_tiles[t2p->pdf_page], tile);
|
|
|
7c98bb |
edge |= t2p_tile_is_bottom_edge(t2p->tiff_tiles[t2p->pdf_page], tile);
|
|
|
7c98bb |
@@ -1949,14 +2003,17 @@
|
|
|
7c98bb |
#endif
|
|
|
7c98bb |
){
|
|
|
7c98bb |
t2p->tiff_datasize=TIFFTileSize(input);
|
|
|
7c98bb |
+ if (t2p->tiff_datasize == 0) {
|
|
|
7c98bb |
+ /* Assume we had overflow inside TIFFTileSize */
|
|
|
7c98bb |
+ t2p->t2p_error = T2P_ERR_ERROR;
|
|
|
7c98bb |
+ }
|
|
|
7c98bb |
return;
|
|
|
7c98bb |
} else {
|
|
|
7c98bb |
TIFFGetField(input, TIFFTAG_TILEBYTECOUNTS, &tbc;;
|
|
|
7c98bb |
- t2p->tiff_datasize=tbc[tile];
|
|
|
7c98bb |
+ k=tbc[tile];
|
|
|
7c98bb |
#ifdef OJPEG_SUPPORT
|
|
|
7c98bb |
if(t2p->tiff_compression==COMPRESSION_OJPEG){
|
|
|
7c98bb |
- t2p->tiff_datasize+=2048;
|
|
|
7c98bb |
- return;
|
|
|
7c98bb |
+ k = checkAdd64(k, 2048, t2p);
|
|
|
7c98bb |
}
|
|
|
7c98bb |
#endif
|
|
|
7c98bb |
#ifdef JPEG_SUPPORT
|
|
|
7c98bb |
@@ -1964,18 +2021,33 @@
|
|
|
7c98bb |
uint32 count = 0;
|
|
|
7c98bb |
if(TIFFGetField(input, TIFFTAG_JPEGTABLES, &count, &jpt)!=0){
|
|
|
7c98bb |
if(count > 4){
|
|
|
7c98bb |
- t2p->tiff_datasize += count;
|
|
|
7c98bb |
- t2p->tiff_datasize -= 4; /* don't use EOI of header or SOI of tile */
|
|
|
7c98bb |
+ k = checkAdd64(k, count, t2p);
|
|
|
7c98bb |
+ k -= 4; /* don't use EOI of header or SOI of tile */
|
|
|
7c98bb |
}
|
|
|
7c98bb |
}
|
|
|
7c98bb |
}
|
|
|
7c98bb |
#endif
|
|
|
7c98bb |
+ t2p->tiff_datasize = (tsize_t) k;
|
|
|
7c98bb |
+ if ((uint64) t2p->tiff_datasize != k) {
|
|
|
7c98bb |
+ TIFFError(TIFF2PDF_MODULE, "Integer overflow");
|
|
|
7c98bb |
+ t2p->t2p_error = T2P_ERR_ERROR;
|
|
|
7c98bb |
+ }
|
|
|
7c98bb |
return;
|
|
|
7c98bb |
}
|
|
|
7c98bb |
}
|
|
|
7c98bb |
- t2p->tiff_datasize=TIFFTileSize(input);
|
|
|
7c98bb |
+ k = TIFFTileSize(input);
|
|
|
7c98bb |
if(t2p->tiff_planar==PLANARCONFIG_SEPARATE){
|
|
|
7c98bb |
- t2p->tiff_datasize*= t2p->tiff_samplesperpixel;
|
|
|
7c98bb |
+ k = checkMultiply64(k, t2p->tiff_samplesperpixel, t2p);
|
|
|
7c98bb |
+ }
|
|
|
7c98bb |
+ if (k == 0) {
|
|
|
7c98bb |
+ /* Assume we had overflow inside TIFFTileSize */
|
|
|
7c98bb |
+ t2p->t2p_error = T2P_ERR_ERROR;
|
|
|
7c98bb |
+ }
|
|
|
7c98bb |
+
|
|
|
7c98bb |
+ t2p->tiff_datasize = (tsize_t) k;
|
|
|
7c98bb |
+ if ((uint64) t2p->tiff_datasize != k) {
|
|
|
7c98bb |
+ TIFFError(TIFF2PDF_MODULE, "Integer overflow");
|
|
|
7c98bb |
+ t2p->t2p_error = T2P_ERR_ERROR;
|
|
|
7c98bb |
}
|
|
|
7c98bb |
|
|
|
7c98bb |
return;
|
|
|
7c98bb |
@@ -2068,6 +2140,10 @@
|
|
|
7c98bb |
uint32 max_striplength=0;
|
|
|
7c98bb |
#endif
|
|
|
7c98bb |
|
|
|
7c98bb |
+ /* Fail if prior error (in particular, can't trust tiff_datasize) */
|
|
|
7c98bb |
+ if (t2p->t2p_error != T2P_ERR_OK)
|
|
|
7c98bb |
+ return(0);
|
|
|
7c98bb |
+
|
|
|
7c98bb |
if(t2p->pdf_transcode == T2P_TRANSCODE_RAW){
|
|
|
7c98bb |
#ifdef CCITT_SUPPORT
|
|
|
7c98bb |
if(t2p->pdf_compression == T2P_COMPRESS_G4){
|
|
|
7c98bb |
@@ -2641,6 +2717,10 @@
|
|
|
7c98bb |
uint32 xuint32=0;
|
|
|
7c98bb |
#endif
|
|
|
7c98bb |
|
|
|
7c98bb |
+ /* Fail if prior error (in particular, can't trust tiff_datasize) */
|
|
|
7c98bb |
+ if (t2p->t2p_error != T2P_ERR_OK)
|
|
|
7c98bb |
+ return(0);
|
|
|
7c98bb |
+
|
|
|
7c98bb |
edge |= t2p_tile_is_right_edge(t2p->tiff_tiles[t2p->pdf_page], tile);
|
|
|
7c98bb |
edge |= t2p_tile_is_bottom_edge(t2p->tiff_tiles[t2p->pdf_page], tile);
|
|
|
7c98bb |
|