nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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