Blame SOURCES/0516-video-readers-png-Drop-greyscale-support-to-fix-heap.patch

bf0270
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
bf0270
From: Daniel Axtens <dja@axtens.net>
bf0270
Date: Tue, 6 Jul 2021 18:51:35 +1000
bf0270
Subject: [PATCH] video/readers/png: Drop greyscale support to fix heap
bf0270
 out-of-bounds write
bf0270
bf0270
A 16-bit greyscale PNG without alpha is processed in the following loop:
bf0270
bf0270
      for (i = 0; i < (data->image_width * data->image_height);
bf0270
	   i++, d1 += 4, d2 += 2)
bf0270
	{
bf0270
	  d1[R3] = d2[1];
bf0270
	  d1[G3] = d2[1];
bf0270
	  d1[B3] = d2[1];
bf0270
	}
bf0270
bf0270
The increment of d1 is wrong. d1 is incremented by 4 bytes per iteration,
bf0270
but there are only 3 bytes allocated for storage. This means that image
bf0270
data will overwrite somewhat-attacker-controlled parts of memory - 3 bytes
bf0270
out of every 4 following the end of the image.
bf0270
bf0270
This has existed since greyscale support was added in 2013 in commit
bf0270
3ccf16dff98f (grub-core/video/readers/png.c: Support grayscale).
bf0270
bf0270
Saving starfield.png as a 16-bit greyscale image without alpha in the gimp
bf0270
and attempting to load it causes grub-emu to crash - I don't think this code
bf0270
has ever worked.
bf0270
bf0270
Delete all PNG greyscale support.
bf0270
bf0270
Fixes: CVE-2021-3695
bf0270
bf0270
Signed-off-by: Daniel Axtens <dja@axtens.net>
bf0270
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
bf0270
(cherry picked from commit 0e1d163382669bd734439d8864ee969616d971d9)
bf0270
[rharwood: context conflict]
bf0270
Signed-off-by: Robbie Harwood <rharwood@redhat.com>
bf0270
(cherry picked from commit 4c631c8119206b3178912df2905434d967661c3d)
bf0270
(cherry picked from commit 6d5d5f51266b8113c6ba560835500e3c135f3722)
bf0270
---
bf0270
 grub-core/video/readers/png.c | 85 +++----------------------------------------
bf0270
 1 file changed, 6 insertions(+), 79 deletions(-)
bf0270
bf0270
diff --git a/grub-core/video/readers/png.c b/grub-core/video/readers/png.c
bf0270
index 8955b8ecfd..a3161e25b6 100644
bf0270
--- a/grub-core/video/readers/png.c
bf0270
+++ b/grub-core/video/readers/png.c
bf0270
@@ -100,7 +100,7 @@ struct grub_png_data
bf0270
 
bf0270
   unsigned image_width, image_height;
bf0270
   int bpp, is_16bit;
bf0270
-  int raw_bytes, is_gray, is_alpha, is_palette;
bf0270
+  int raw_bytes, is_alpha, is_palette;
bf0270
   int row_bytes, color_bits;
bf0270
   grub_uint8_t *image_data;
bf0270
 
bf0270
@@ -296,13 +296,13 @@ grub_png_decode_image_header (struct grub_png_data *data)
bf0270
     data->bpp = 3;
bf0270
   else
bf0270
     {
bf0270
-      data->is_gray = 1;
bf0270
-      data->bpp = 1;
bf0270
+      return grub_error (GRUB_ERR_BAD_FILE_TYPE,
bf0270
+			 "png: color type not supported");
bf0270
     }
bf0270
 
bf0270
   if ((color_bits != 8) && (color_bits != 16)
bf0270
       && (color_bits != 4
bf0270
-	  || !(data->is_gray || data->is_palette)))
bf0270
+	  || !data->is_palette))
bf0270
     return grub_error (GRUB_ERR_BAD_FILE_TYPE,
bf0270
                        "png: bit depth must be 8 or 16");
bf0270
 
bf0270
@@ -331,7 +331,7 @@ grub_png_decode_image_header (struct grub_png_data *data)
bf0270
     }
bf0270
 
bf0270
 #ifndef GRUB_CPU_WORDS_BIGENDIAN
bf0270
-  if (data->is_16bit || data->is_gray || data->is_palette)
bf0270
+  if (data->is_16bit || data->is_palette)
bf0270
 #endif
bf0270
     {
bf0270
       data->image_data = grub_calloc (data->image_height, data->row_bytes);
bf0270
@@ -899,27 +899,8 @@ grub_png_convert_image (struct grub_png_data *data)
bf0270
       int shift;
bf0270
       int mask = (1 << data->color_bits) - 1;
bf0270
       unsigned j;
bf0270
-      if (data->is_gray)
bf0270
-	{
bf0270
-	  /* Generic formula is
bf0270
-	     (0xff * i) / ((1U << data->color_bits) - 1)
bf0270
-	     but for allowed bit depth of 1, 2 and for it's
bf0270
-	     equivalent to
bf0270
-	     (0xff / ((1U << data->color_bits) - 1)) * i
bf0270
-	     Precompute the multipliers to avoid division.
bf0270
-	  */
bf0270
 
bf0270
-	  const grub_uint8_t multipliers[5] = { 0xff, 0xff, 0x55, 0x24, 0x11 };
bf0270
-	  for (i = 0; i < (1U << data->color_bits); i++)
bf0270
-	    {
bf0270
-	      grub_uint8_t col = multipliers[data->color_bits] * i;
bf0270
-	      palette[i][0] = col;
bf0270
-	      palette[i][1] = col;
bf0270
-	      palette[i][2] = col;
bf0270
-	    }
bf0270
-	}
bf0270
-      else
bf0270
-	grub_memcpy (palette, data->palette, 3 << data->color_bits);
bf0270
+      grub_memcpy (palette, data->palette, 3 << data->color_bits);
bf0270
       d1c = d1;
bf0270
       d2c = d2;
bf0270
       for (j = 0; j < data->image_height; j++, d1c += data->image_width * 3,
bf0270
@@ -956,60 +937,6 @@ grub_png_convert_image (struct grub_png_data *data)
bf0270
 	}
bf0270
       return;
bf0270
     }
bf0270
-  
bf0270
-  if (data->is_gray)
bf0270
-    {
bf0270
-      switch (data->bpp)
bf0270
-	{
bf0270
-	case 4:
bf0270
-	  /* 16-bit gray with alpha.  */
bf0270
-	  for (i = 0; i < (data->image_width * data->image_height);
bf0270
-	       i++, d1 += 4, d2 += 4)
bf0270
-	    {
bf0270
-	      d1[R4] = d2[3];
bf0270
-	      d1[G4] = d2[3];
bf0270
-	      d1[B4] = d2[3];
bf0270
-	      d1[A4] = d2[1];
bf0270
-	    }
bf0270
-	  break;
bf0270
-	case 2:
bf0270
-	  if (data->is_16bit)
bf0270
-	    /* 16-bit gray without alpha.  */
bf0270
-	    {
bf0270
-	      for (i = 0; i < (data->image_width * data->image_height);
bf0270
-		   i++, d1 += 4, d2 += 2)
bf0270
-		{
bf0270
-		  d1[R3] = d2[1];
bf0270
-		  d1[G3] = d2[1];
bf0270
-		  d1[B3] = d2[1];
bf0270
-		}
bf0270
-	    }
bf0270
-	  else
bf0270
-	    /* 8-bit gray with alpha.  */
bf0270
-	    {
bf0270
-	      for (i = 0; i < (data->image_width * data->image_height);
bf0270
-		   i++, d1 += 4, d2 += 2)
bf0270
-		{
bf0270
-		  d1[R4] = d2[1];
bf0270
-		  d1[G4] = d2[1];
bf0270
-		  d1[B4] = d2[1];
bf0270
-		  d1[A4] = d2[0];
bf0270
-		}
bf0270
-	    }
bf0270
-	  break;
bf0270
-	  /* 8-bit gray without alpha.  */
bf0270
-	case 1:
bf0270
-	  for (i = 0; i < (data->image_width * data->image_height);
bf0270
-	       i++, d1 += 3, d2++)
bf0270
-	    {
bf0270
-	      d1[R3] = d2[0];
bf0270
-	      d1[G3] = d2[0];
bf0270
-	      d1[B3] = d2[0];
bf0270
-	    }
bf0270
-	  break;
bf0270
-	}
bf0270
-      return;
bf0270
-    }
bf0270
 
bf0270
     {
bf0270
   /* Only copy the upper 8 bit.  */