Home | History | Annotate | Download | only in libtiff
      1 From b18012dae552f85dcc5c57d3bf4e997a15b1cc1c Mon Sep 17 00:00:00 2001
      2 From: erouault <erouault>
      3 Date: Sun, 27 Dec 2015 16:55:20 +0000
      4 Subject: [PATCH] * libtiff/tif_next.c: fix potential out-of-bound write in
      5  NeXTDecode() triggered by http://lcamtuf.coredump.cx/afl/vulns/libtiff5.tif
      6  (bugzilla #2508)
      7 
      8 ---
      9  ChangeLog          |  6 ++++++
     10  libtiff/tif_next.c | 10 ++++++++--
     11  2 files changed, 14 insertions(+), 2 deletions(-)
     12 
     13 diff --git a/libtiff/tif_next.c b/libtiff/tif_next.c
     14 index dd669cc..0a5b635 100644
     15 --- a/libtiff/tif_next.c
     16 +++ b/libtiff/tif_next.c
     17 @@ -37,7 +37,7 @@
     18  	case 0:	op[0]  = (unsigned char) ((v) << 6); break;	\
     19  	case 1:	op[0] |= (v) << 4; break;	\
     20  	case 2:	op[0] |= (v) << 2; break;	\
     21 -	case 3:	*op++ |= (v);	   break;	\
     22 +	case 3:	*op++ |= (v);	   op_offset++; break;	\
     23  	}					\
     24  }
     25  
     26 @@ -106,6 +106,7 @@ NeXTDecode(TIFF* tif, uint8* buf, tmsize_t occ, uint16 s)
     27  			uint32 imagewidth = tif->tif_dir.td_imagewidth;
     28              if( isTiled(tif) )
     29                  imagewidth = tif->tif_dir.td_tilewidth;
     30 +            tmsize_t op_offset = 0;
     31  
     32  			/*
     33  			 * The scanline is composed of a sequence of constant
     34 @@ -122,10 +123,15 @@ NeXTDecode(TIFF* tif, uint8* buf, tmsize_t occ, uint16 s)
     35  				 * bounds, potentially resulting in a security
     36  				 * issue.
     37  				 */
     38 -				while (n-- > 0 && npixels < imagewidth)
     39 +				while (n-- > 0 && npixels < imagewidth && op_offset < scanline)
     40  					SETPIXEL(op, grey);
     41  				if (npixels >= imagewidth)
     42  					break;
     43 +                if (op_offset >= scanline ) {
     44 +                    TIFFErrorExt(tif->tif_clientdata, module, "Invalid data for scanline %ld",
     45 +                        (long) tif->tif_row);
     46 +                    return (0);
     47 +                }
     48  				if (cc == 0)
     49  					goto bad;
     50  				n = *bp++, cc--;
     51