Blame SOURCES/libpng12-CVE-2015-8126.patch

91b28b
diff --git a/pngrutil.c b/pngrutil.c
91b28b
index 1de7427..72a8b5e 100644
91b28b
--- a/pngrutil.c
91b28b
+++ b/pngrutil.c
91b28b
@@ -503,7 +503,7 @@ void /* PRIVATE */
91b28b
 png_handle_PLTE(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
91b28b
 {
91b28b
    png_color palette[PNG_MAX_PALETTE_LENGTH];
91b28b
-   int num, i;
91b28b
+   int max_palette_length, num, i;
91b28b
 #ifdef PNG_POINTER_INDEXING_SUPPORTED
91b28b
    png_colorp pal_ptr;
91b28b
 #endif
91b28b
@@ -555,8 +555,21 @@ png_handle_PLTE(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
91b28b
       }
91b28b
    }
91b28b
 
91b28b
+   /* The cast is safe because 'length' is less than 3*PNG_MAX_PALETTE_LENGTH */
91b28b
    num = (int)length / 3;
91b28b
 
91b28b
+   /* If the palette has 256 or fewer entries but is too large for the bit depth,
91b28b
+    * we don't issue an error, to preserve the behavior of previous libpng versions.
91b28b
+    * We silently truncate the unused extra palette entries here.
91b28b
+    */
91b28b
+   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
91b28b
+      max_palette_length = (1 << png_ptr->bit_depth);
91b28b
+   else
91b28b
+      max_palette_length = PNG_MAX_PALETTE_LENGTH;
91b28b
+
91b28b
+   if (num > max_palette_length)
91b28b
+      num = max_palette_length;
91b28b
+
91b28b
 #ifdef PNG_POINTER_INDEXING_SUPPORTED
91b28b
    for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++)
91b28b
    {
91b28b
@@ -589,7 +602,7 @@ png_handle_PLTE(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
91b28b
    if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
91b28b
 #endif
91b28b
    {
91b28b
-      png_crc_finish(png_ptr, 0);
91b28b
+      png_crc_finish(png_ptr, (int) length - num * 3);
91b28b
    }
91b28b
 #ifndef PNG_READ_OPT_PLTE_SUPPORTED
91b28b
    else if (png_crc_error(png_ptr))  /* Only if we have a CRC error */
91b28b
@@ -1097,7 +1110,7 @@ png_handle_iCCP(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
91b28b
    /* There should be at least one zero (the compression type byte)
91b28b
     * following the separator, and we should be on it
91b28b
     */
91b28b
-   if ( profile >= png_ptr->chunkdata + slength - 1)
91b28b
+   if ( slength < 1 || profile >= png_ptr->chunkdata + slength - 1)
91b28b
    {
91b28b
       png_free(png_ptr, png_ptr->chunkdata);
91b28b
       png_ptr->chunkdata = NULL;
91b28b
@@ -1225,7 +1238,7 @@ png_handle_sPLT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
91b28b
    ++entry_start;
91b28b
 
91b28b
    /* A sample depth should follow the separator, and we should be on it  */
91b28b
-   if (entry_start > (png_bytep)png_ptr->chunkdata + slength - 2)
91b28b
+   if (slength < 2 || entry_start > (png_bytep)png_ptr->chunkdata + slength - 2)
91b28b
    {
91b28b
       png_free(png_ptr, png_ptr->chunkdata);
91b28b
       png_ptr->chunkdata = NULL;
91b28b
@@ -1699,7 +1712,7 @@ png_handle_pCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
91b28b
 
91b28b
    /* We need to have at least 12 bytes after the purpose string
91b28b
       in order to get the parameter information. */
91b28b
-   if (endptr <= buf + 12)
91b28b
+   if (slength < 12 || endptr <= buf + 12)
91b28b
    {
91b28b
       png_warning(png_ptr, "Invalid pCAL data");
91b28b
       png_free(png_ptr, png_ptr->chunkdata);
91b28b
@@ -2155,7 +2168,7 @@ png_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
91b28b
       /* Empty loop */ ;
91b28b
 
91b28b
    /* zTXt must have some text after the chunkdataword */
91b28b
-   if (text >= png_ptr->chunkdata + slength - 2)
91b28b
+   if (slength < 2 || text >= png_ptr->chunkdata + slength - 2)
91b28b
    {
91b28b
       png_warning(png_ptr, "Truncated zTXt chunk");
91b28b
       png_free(png_ptr, png_ptr->chunkdata);
91b28b
@@ -2281,7 +2294,7 @@ png_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
91b28b
     * keyword
91b28b
     */
91b28b
 
91b28b
-   if (lang >= png_ptr->chunkdata + slength - 3)
91b28b
+   if (slength < 3 || lang >= png_ptr->chunkdata + slength - 3)
91b28b
    {
91b28b
       png_warning(png_ptr, "Truncated iTXt chunk");
91b28b
       png_free(png_ptr, png_ptr->chunkdata);
91b28b
diff --git a/pngset.c b/pngset.c
91b28b
index 7a47b1e..54ac931 100644
91b28b
--- a/pngset.c
91b28b
+++ b/pngset.c
91b28b
@@ -446,12 +446,17 @@ png_set_PLTE(png_structp png_ptr, png_infop info_ptr,
91b28b
    png_colorp palette, int num_palette)
91b28b
 {
91b28b
 
91b28b
+   png_uint_32 max_palette_length;
91b28b
+
91b28b
    png_debug1(1, "in %s storage function", "PLTE");
91b28b
 
91b28b
    if (png_ptr == NULL || info_ptr == NULL)
91b28b
       return;
91b28b
 
91b28b
-   if (num_palette < 0 || num_palette > PNG_MAX_PALETTE_LENGTH)
91b28b
+   max_palette_length = (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ?
91b28b
+      (1 << info_ptr->bit_depth) : PNG_MAX_PALETTE_LENGTH;
91b28b
+
91b28b
+   if (num_palette < 0 || num_palette > (int) max_palette_length)
91b28b
    {
91b28b
       if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
91b28b
          png_error(png_ptr, "Invalid palette length");
91b28b
diff --git a/pngwutil.c b/pngwutil.c
91b28b
index c75f53e..80128d6 100644
91b28b
--- a/pngwutil.c
91b28b
+++ b/pngwutil.c
91b28b
@@ -575,17 +575,20 @@ png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
91b28b
 #ifdef PNG_USE_LOCAL_ARRAYS
91b28b
    PNG_PLTE;
91b28b
 #endif
91b28b
-   png_uint_32 i;
91b28b
+   png_uint_32 max_palette_length, i;
91b28b
    png_colorp pal_ptr;
91b28b
    png_byte buf[3];
91b28b
 
91b28b
    png_debug(1, "in png_write_PLTE");
91b28b
 
91b28b
+   max_palette_length = (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ?
91b28b
+      (1 << png_ptr->bit_depth) : PNG_MAX_PALETTE_LENGTH;
91b28b
+
91b28b
    if ((
91b28b
 #ifdef PNG_MNG_FEATURES_SUPPORTED
91b28b
         !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
91b28b
 #endif
91b28b
-        num_pal == 0) || num_pal > 256)
91b28b
+        num_pal == 0) || num_pal > max_palette_length)
91b28b
    {
91b28b
      if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
91b28b
      {