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

1f379d
diff --git a/pngrutil.c b/pngrutil.c
1f379d
index 4ef05fe..f6f0864 100644
1f379d
--- a/pngrutil.c
1f379d
+++ b/pngrutil.c
1f379d
@@ -596,7 +596,7 @@ void /* PRIVATE */
1f379d
 png_handle_PLTE(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
1f379d
 {
1f379d
    png_color palette[PNG_MAX_PALETTE_LENGTH];
1f379d
-   int num, i;
1f379d
+   int max_palette_length, num, i;
1f379d
 #ifdef PNG_POINTER_INDEXING_SUPPORTED
1f379d
    png_colorp pal_ptr;
1f379d
 #endif
1f379d
@@ -648,9 +648,21 @@ png_handle_PLTE(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
1f379d
          png_error(png_ptr, "Invalid palette chunk");
1f379d
       }
1f379d
    }
1f379d
-
1f379d
+   /* The cast is safe because 'length' is less than 3*PNG_MAX_PALETTE_LENGTH */
1f379d
    num = (int)length / 3;
1f379d
 
1f379d
+   /* If the palette has 256 or fewer entries but is too large for the bit
1f379d
+    * depth, we don't issue an error, to preserve the behavior of previous
1f379d
+    * libpng versions. We silently truncate the unused extra palette entries
1f379d
+    * here.
1f379d
+    */
1f379d
+   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1f379d
+      max_palette_length = (1 << png_ptr->bit_depth);
1f379d
+   else
1f379d
+      max_palette_length = PNG_MAX_PALETTE_LENGTH;
1f379d
+   if (num > max_palette_length)
1f379d
+      num = max_palette_length;
1f379d
+
1f379d
 #ifdef PNG_POINTER_INDEXING_SUPPORTED
1f379d
    for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++)
1f379d
    {
1f379d
@@ -683,7 +695,7 @@ png_handle_PLTE(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
1f379d
    if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1f379d
 #endif
1f379d
    {
1f379d
-      png_crc_finish(png_ptr, 0);
1f379d
+      png_crc_finish(png_ptr, (int) length - num * 3);
1f379d
    }
1f379d
 
1f379d
 #ifndef PNG_READ_OPT_PLTE_SUPPORTED
1f379d
diff --git a/pngset.c b/pngset.c
1f379d
index 8e3f39a..c252088 100644
1f379d
--- a/pngset.c
1f379d
+++ b/pngset.c
1f379d
@@ -517,13 +517,17 @@ png_set_PLTE(png_structp png_ptr, png_infop info_ptr,
1f379d
     png_const_colorp palette, int num_palette)
1f379d
 {
1f379d
 
1f379d
+   png_uint_32 max_palette_length;
1f379d
+
1f379d
    png_debug1(1, "in %s storage function", "PLTE");
1f379d
 
1f379d
    if (png_ptr == NULL || info_ptr == NULL)
1f379d
       return;
1f379d
 
1f379d
-   if (num_palette < 0 || num_palette > PNG_MAX_PALETTE_LENGTH)
1f379d
-   {
1f379d
+   max_palette_length = (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ?
1f379d
+      (1 << info_ptr->bit_depth) : PNG_MAX_PALETTE_LENGTH;
1f379d
+
1f379d
+   if (num_palette < 0 || num_palette > (int) max_palette_length)   {
1f379d
       if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1f379d
          png_error(png_ptr, "Invalid palette length");
1f379d
 
1f379d
@@ -551,8 +555,8 @@ png_set_PLTE(png_structp png_ptr, png_infop info_ptr,
1f379d
    png_free_data(png_ptr, info_ptr, PNG_FREE_PLTE, 0);
1f379d
 
1f379d
    /* Changed in libpng-1.2.1 to allocate PNG_MAX_PALETTE_LENGTH instead
1f379d
-    * of num_palette entries, in case of an invalid PNG file that has
1f379d
-    * too-large sample values.
1f379d
+    * of num_palette entries, in case of an invalid PNG file or incorrect
1f379d
+    * call to png_set_PLTE() with too-large sample values.
1f379d
     */
1f379d
    png_ptr->palette = (png_colorp)png_calloc(png_ptr,
1f379d
        PNG_MAX_PALETTE_LENGTH * png_sizeof(png_color));
1f379d
diff --git a/pngwutil.c b/pngwutil.c
1f379d
index 19b75af..7ecaaca 100644
1f379d
--- a/pngwutil.c
1f379d
+++ b/pngwutil.c
1f379d
@@ -896,17 +896,20 @@ void /* PRIVATE */
1f379d
 png_write_PLTE(png_structp png_ptr, png_const_colorp palette,
1f379d
     png_uint_32 num_pal)
1f379d
 {
1f379d
-   png_uint_32 i;
1f379d
+   png_uint_32 max_palette_length, i;
1f379d
    png_const_colorp pal_ptr;
1f379d
    png_byte buf[3];
1f379d
 
1f379d
+   max_palette_length = (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ?
1f379d
+      (1 << png_ptr->bit_depth) : PNG_MAX_PALETTE_LENGTH;
1f379d
+
1f379d
    png_debug(1, "in png_write_PLTE");
1f379d
 
1f379d
    if ((
1f379d
 #ifdef PNG_MNG_FEATURES_SUPPORTED
1f379d
        !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
1f379d
 #endif
1f379d
-       num_pal == 0) || num_pal > 256)
1f379d
+       num_pal == 0) || num_pal > max_palette_length)
1f379d
    {
1f379d
       if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1f379d
       {