|
Line 0
Link Here
|
|
|
1 |
revision 1.41 |
| 2 |
date: 2015-12-27 16:25:11 +0000; author: erouault; state: Exp; lines: +45 -12; commitid: gXczlJDfVlBdzBOy; |
| 3 |
* libtiff/tif_luv.c: fix potential out-of-bound writes in decode |
| 4 |
functions in non debug builds by replacing assert()s by regular if |
| 5 |
checks (bugzilla #2522). |
| 6 |
Fix potential out-of-bound reads in case of short input data. |
| 7 |
|
| 8 |
Index: libtiff/tif_luv.c |
| 9 |
=================================================================== |
| 10 |
RCS file: /cvs/maptools/cvsroot/libtiff/libtiff/tif_luv.c,v |
| 11 |
retrieving revision 1.40 |
| 12 |
retrieving revision 1.41 |
| 13 |
diff -u -r1.40 -r1.41 |
| 14 |
--- libtiff/tif_luv.c 21 Jun 2015 01:09:09 -0000 1.40 |
| 15 |
+++ libtiff/tif_luv.c 27 Dec 2015 16:25:11 -0000 1.41 |
| 16 |
@@ -1,4 +1,4 @@ |
| 17 |
-/* $Id: tif_luv.c,v 1.40 2015-06-21 01:09:09 bfriesen Exp $ */ |
| 18 |
+/* $Id: tif_luv.c,v 1.41 2015-12-27 16:25:11 erouault Exp $ */ |
| 19 |
|
| 20 |
/* |
| 21 |
* Copyright (c) 1997 Greg Ward Larson |
| 22 |
@@ -202,7 +202,11 @@ |
| 23 |
if (sp->user_datafmt == SGILOGDATAFMT_16BIT) |
| 24 |
tp = (int16*) op; |
| 25 |
else { |
| 26 |
- assert(sp->tbuflen >= npixels); |
| 27 |
+ if(sp->tbuflen < npixels) { |
| 28 |
+ TIFFErrorExt(tif->tif_clientdata, module, |
| 29 |
+ "Translation buffer too short"); |
| 30 |
+ return (0); |
| 31 |
+ } |
| 32 |
tp = (int16*) sp->tbuf; |
| 33 |
} |
| 34 |
_TIFFmemset((void*) tp, 0, npixels*sizeof (tp[0])); |
| 35 |
@@ -211,9 +215,11 @@ |
| 36 |
cc = tif->tif_rawcc; |
| 37 |
/* get each byte string */ |
| 38 |
for (shft = 2*8; (shft -= 8) >= 0; ) { |
| 39 |
- for (i = 0; i < npixels && cc > 0; ) |
| 40 |
+ for (i = 0; i < npixels && cc > 0; ) { |
| 41 |
if (*bp >= 128) { /* run */ |
| 42 |
- rc = *bp++ + (2-128); /* TODO: potential input buffer overrun when decoding corrupt or truncated data */ |
| 43 |
+ if( cc < 2 ) |
| 44 |
+ break; |
| 45 |
+ rc = *bp++ + (2-128); |
| 46 |
b = (int16)(*bp++ << shft); |
| 47 |
cc -= 2; |
| 48 |
while (rc-- && i < npixels) |
| 49 |
@@ -223,6 +229,7 @@ |
| 50 |
while (--cc && rc-- && i < npixels) |
| 51 |
tp[i++] |= (int16)*bp++ << shft; |
| 52 |
} |
| 53 |
+ } |
| 54 |
if (i != npixels) { |
| 55 |
#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__)) |
| 56 |
TIFFErrorExt(tif->tif_clientdata, module, |
| 57 |
@@ -268,13 +275,17 @@ |
| 58 |
if (sp->user_datafmt == SGILOGDATAFMT_RAW) |
| 59 |
tp = (uint32 *)op; |
| 60 |
else { |
| 61 |
- assert(sp->tbuflen >= npixels); |
| 62 |
+ if(sp->tbuflen < npixels) { |
| 63 |
+ TIFFErrorExt(tif->tif_clientdata, module, |
| 64 |
+ "Translation buffer too short"); |
| 65 |
+ return (0); |
| 66 |
+ } |
| 67 |
tp = (uint32 *) sp->tbuf; |
| 68 |
} |
| 69 |
/* copy to array of uint32 */ |
| 70 |
bp = (unsigned char*) tif->tif_rawcp; |
| 71 |
cc = tif->tif_rawcc; |
| 72 |
- for (i = 0; i < npixels && cc > 0; i++) { |
| 73 |
+ for (i = 0; i < npixels && cc >= 3; i++) { |
| 74 |
tp[i] = bp[0] << 16 | bp[1] << 8 | bp[2]; |
| 75 |
bp += 3; |
| 76 |
cc -= 3; |
| 77 |
@@ -325,7 +336,11 @@ |
| 78 |
if (sp->user_datafmt == SGILOGDATAFMT_RAW) |
| 79 |
tp = (uint32*) op; |
| 80 |
else { |
| 81 |
- assert(sp->tbuflen >= npixels); |
| 82 |
+ if(sp->tbuflen < npixels) { |
| 83 |
+ TIFFErrorExt(tif->tif_clientdata, module, |
| 84 |
+ "Translation buffer too short"); |
| 85 |
+ return (0); |
| 86 |
+ } |
| 87 |
tp = (uint32*) sp->tbuf; |
| 88 |
} |
| 89 |
_TIFFmemset((void*) tp, 0, npixels*sizeof (tp[0])); |
| 90 |
@@ -334,11 +349,13 @@ |
| 91 |
cc = tif->tif_rawcc; |
| 92 |
/* get each byte string */ |
| 93 |
for (shft = 4*8; (shft -= 8) >= 0; ) { |
| 94 |
- for (i = 0; i < npixels && cc > 0; ) |
| 95 |
+ for (i = 0; i < npixels && cc > 0; ) { |
| 96 |
if (*bp >= 128) { /* run */ |
| 97 |
+ if( cc < 2 ) |
| 98 |
+ break; |
| 99 |
rc = *bp++ + (2-128); |
| 100 |
b = (uint32)*bp++ << shft; |
| 101 |
- cc -= 2; /* TODO: potential input buffer overrun when decoding corrupt or truncated data */ |
| 102 |
+ cc -= 2; |
| 103 |
while (rc-- && i < npixels) |
| 104 |
tp[i++] |= b; |
| 105 |
} else { /* non-run */ |
| 106 |
@@ -346,6 +363,7 @@ |
| 107 |
while (--cc && rc-- && i < npixels) |
| 108 |
tp[i++] |= (uint32)*bp++ << shft; |
| 109 |
} |
| 110 |
+ } |
| 111 |
if (i != npixels) { |
| 112 |
#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__)) |
| 113 |
TIFFErrorExt(tif->tif_clientdata, module, |
| 114 |
@@ -413,6 +431,7 @@ |
| 115 |
static int |
| 116 |
LogL16Encode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s) |
| 117 |
{ |
| 118 |
+ static const char module[] = "LogL16Encode"; |
| 119 |
LogLuvState* sp = EncoderState(tif); |
| 120 |
int shft; |
| 121 |
tmsize_t i; |
| 122 |
@@ -433,7 +452,11 @@ |
| 123 |
tp = (int16*) bp; |
| 124 |
else { |
| 125 |
tp = (int16*) sp->tbuf; |
| 126 |
- assert(sp->tbuflen >= npixels); |
| 127 |
+ if(sp->tbuflen < npixels) { |
| 128 |
+ TIFFErrorExt(tif->tif_clientdata, module, |
| 129 |
+ "Translation buffer too short"); |
| 130 |
+ return (0); |
| 131 |
+ } |
| 132 |
(*sp->tfunc)(sp, bp, npixels); |
| 133 |
} |
| 134 |
/* compress each byte string */ |
| 135 |
@@ -506,6 +529,7 @@ |
| 136 |
static int |
| 137 |
LogLuvEncode24(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s) |
| 138 |
{ |
| 139 |
+ static const char module[] = "LogLuvEncode24"; |
| 140 |
LogLuvState* sp = EncoderState(tif); |
| 141 |
tmsize_t i; |
| 142 |
tmsize_t npixels; |
| 143 |
@@ -521,7 +545,11 @@ |
| 144 |
tp = (uint32*) bp; |
| 145 |
else { |
| 146 |
tp = (uint32*) sp->tbuf; |
| 147 |
- assert(sp->tbuflen >= npixels); |
| 148 |
+ if(sp->tbuflen < npixels) { |
| 149 |
+ TIFFErrorExt(tif->tif_clientdata, module, |
| 150 |
+ "Translation buffer too short"); |
| 151 |
+ return (0); |
| 152 |
+ } |
| 153 |
(*sp->tfunc)(sp, bp, npixels); |
| 154 |
} |
| 155 |
/* write out encoded pixels */ |
| 156 |
@@ -553,6 +581,7 @@ |
| 157 |
static int |
| 158 |
LogLuvEncode32(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s) |
| 159 |
{ |
| 160 |
+ static const char module[] = "LogLuvEncode32"; |
| 161 |
LogLuvState* sp = EncoderState(tif); |
| 162 |
int shft; |
| 163 |
tmsize_t i; |
| 164 |
@@ -574,7 +603,11 @@ |
| 165 |
tp = (uint32*) bp; |
| 166 |
else { |
| 167 |
tp = (uint32*) sp->tbuf; |
| 168 |
- assert(sp->tbuflen >= npixels); |
| 169 |
+ if(sp->tbuflen < npixels) { |
| 170 |
+ TIFFErrorExt(tif->tif_clientdata, module, |
| 171 |
+ "Translation buffer too short"); |
| 172 |
+ return (0); |
| 173 |
+ } |
| 174 |
(*sp->tfunc)(sp, bp, npixels); |
| 175 |
} |
| 176 |
/* compress each byte string */ |