nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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