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

3528ec
From ba8ce45d1eda9c6291af38f41f09a5c9d238f707 Mon Sep 17 00:00:00 2001
3528ec
From: Even Rouault <even.rouault@spatialys.com>
3528ec
Date: Sat, 12 May 2018 15:32:31 +0200
3528ec
Subject: [PATCH 03/10] LZWDecodeCompat(): fix potential index-out-of-bounds
3528ec
 write. Fixes http://bugzilla.maptools.org/show_bug.cgi?id=2780 /
3528ec
 CVE-2018-8905
3528ec
3528ec
The fix consists in using the similar code LZWDecode() to validate we
3528ec
don't write outside of the output buffer.
3528ec
---
3528ec
 libtiff/tif_lzw.c | 17 ++++++++++++-----
3528ec
 1 file changed, 12 insertions(+), 5 deletions(-)
3528ec
3528ec
diff --git a/libtiff/tif_lzw.c b/libtiff/tif_lzw.c
3528ec
index d7fbe74..af35c2a 100644
3528ec
--- a/libtiff/tif_lzw.c
3528ec
+++ b/libtiff/tif_lzw.c
3528ec
@@ -589,6 +589,7 @@ LZWDecodeCompat(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s)
3528ec
 	char *tp;
3528ec
 	unsigned char *bp;
3528ec
 	int code, nbits;
3528ec
+	int len;
3528ec
 	long nextbits, nextdata, nbitsmask;
3528ec
 	code_t *codep, *free_entp, *maxcodep, *oldcodep;
3528ec
 
3528ec
@@ -733,12 +734,18 @@ LZWDecodeCompat(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s)
3528ec
 				}  while (--occ);
3528ec
 				break;
3528ec
 			}
3528ec
-			assert(occ >= codep->length);
3528ec
-			op += codep->length, occ -= codep->length;
3528ec
-			tp = op;
3528ec
+			len = codep->length;
3528ec
+			tp = op + len;
3528ec
 			do {
3528ec
-				*--tp = codep->value;
3528ec
-			} while( (codep = codep->next) != NULL );
3528ec
+				int t;
3528ec
+				--tp;
3528ec
+				t = codep->value;
3528ec
+				codep = codep->next;
3528ec
+				*tp = (char)t;
3528ec
+			} while (codep && tp > op);
3528ec
+			assert(occ >= len);
3528ec
+			op += len;
3528ec
+			occ -= len;
3528ec
 		} else
3528ec
 			*op++ = code, occ--;
3528ec
 	}
3528ec
-- 
3528ec
2.17.2
3528ec