nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

Blame SOURCES/0221-video-readers-png-Abort-sooner-if-a-read-operation-f.patch

b35c50
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
b35c50
From: Daniel Axtens <dja@axtens.net>
b35c50
Date: Tue, 6 Jul 2021 14:02:55 +1000
b35c50
Subject: [PATCH] video/readers/png: Abort sooner if a read operation fails
b35c50
b35c50
Fuzzing revealed some inputs that were taking a long time, potentially
b35c50
forever, because they did not bail quickly upon encountering an I/O error.
b35c50
b35c50
Try to catch I/O errors sooner and bail out.
b35c50
b35c50
Signed-off-by: Daniel Axtens <dja@axtens.net>
b35c50
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
b35c50
(cherry picked from commit 882be97d1df6449b9fd4d593f0cb70005fde3494)
b35c50
---
b35c50
 grub-core/video/readers/png.c | 55 ++++++++++++++++++++++++++++++++++++-------
b35c50
 1 file changed, 47 insertions(+), 8 deletions(-)
b35c50
b35c50
diff --git a/grub-core/video/readers/png.c b/grub-core/video/readers/png.c
b35c50
index 0157ff7420..e2a6b1cf3c 100644
b35c50
--- a/grub-core/video/readers/png.c
b35c50
+++ b/grub-core/video/readers/png.c
b35c50
@@ -142,6 +142,7 @@ static grub_uint8_t
b35c50
 grub_png_get_byte (struct grub_png_data *data)
b35c50
 {
b35c50
   grub_uint8_t r;
b35c50
+  grub_ssize_t bytes_read = 0;
b35c50
 
b35c50
   if ((data->inside_idat) && (data->idat_remain == 0))
b35c50
     {
b35c50
@@ -175,7 +176,14 @@ grub_png_get_byte (struct grub_png_data *data)
b35c50
     }
b35c50
 
b35c50
   r = 0;
b35c50
-  grub_file_read (data->file, &r, 1);
b35c50
+  bytes_read = grub_file_read (data->file, &r, 1);
b35c50
+
b35c50
+  if (bytes_read != 1)
b35c50
+    {
b35c50
+      grub_error (GRUB_ERR_BAD_FILE_TYPE,
b35c50
+		  "png: unexpected end of data");
b35c50
+      return 0;
b35c50
+    }
b35c50
 
b35c50
   if (data->inside_idat)
b35c50
     data->idat_remain--;
b35c50
@@ -231,15 +239,16 @@ grub_png_decode_image_palette (struct grub_png_data *data,
b35c50
   if (len == 0)
b35c50
     return GRUB_ERR_NONE;
b35c50
 
b35c50
-  for (i = 0; 3 * i < len && i < 256; i++)
b35c50
+  grub_errno = GRUB_ERR_NONE;
b35c50
+  for (i = 0; 3 * i < len && i < 256 && grub_errno == GRUB_ERR_NONE; i++)
b35c50
     for (j = 0; j < 3; j++)
b35c50
       data->palette[i][j] = grub_png_get_byte (data);
b35c50
-  for (i *= 3; i < len; i++)
b35c50
+  for (i *= 3; i < len && grub_errno == GRUB_ERR_NONE; i++)
b35c50
     grub_png_get_byte (data);
b35c50
 
b35c50
   grub_png_get_dword (data);
b35c50
 
b35c50
-  return GRUB_ERR_NONE;
b35c50
+  return grub_errno;
b35c50
 }
b35c50
 
b35c50
 static grub_err_t
b35c50
@@ -256,9 +265,13 @@ grub_png_decode_image_header (struct grub_png_data *data)
b35c50
     return grub_error (GRUB_ERR_BAD_FILE_TYPE, "png: invalid image size");
b35c50
 
b35c50
   color_bits = grub_png_get_byte (data);
b35c50
+  if (grub_errno != GRUB_ERR_NONE)
b35c50
+    return grub_errno;
b35c50
   data->is_16bit = (color_bits == 16);
b35c50
 
b35c50
   color_type = grub_png_get_byte (data);
b35c50
+  if (grub_errno != GRUB_ERR_NONE)
b35c50
+    return grub_errno;
b35c50
 
b35c50
   /* According to PNG spec, no other types are valid.  */
b35c50
   if ((color_type & ~(PNG_COLOR_MASK_ALPHA | PNG_COLOR_MASK_COLOR))
b35c50
@@ -340,14 +353,20 @@ grub_png_decode_image_header (struct grub_png_data *data)
b35c50
   if (grub_png_get_byte (data) != PNG_COMPRESSION_BASE)
b35c50
     return grub_error (GRUB_ERR_BAD_FILE_TYPE,
b35c50
 		       "png: compression method not supported");
b35c50
+  if (grub_errno != GRUB_ERR_NONE)
b35c50
+    return grub_errno;
b35c50
 
b35c50
   if (grub_png_get_byte (data) != PNG_FILTER_TYPE_BASE)
b35c50
     return grub_error (GRUB_ERR_BAD_FILE_TYPE,
b35c50
 		       "png: filter method not supported");
b35c50
+  if (grub_errno != GRUB_ERR_NONE)
b35c50
+    return grub_errno;
b35c50
 
b35c50
   if (grub_png_get_byte (data) != PNG_INTERLACE_NONE)
b35c50
     return grub_error (GRUB_ERR_BAD_FILE_TYPE,
b35c50
 		       "png: interlace method not supported");
b35c50
+  if (grub_errno != GRUB_ERR_NONE)
b35c50
+    return grub_errno;
b35c50
 
b35c50
   /* Skip crc checksum.  */
b35c50
   grub_png_get_dword (data);
b35c50
@@ -449,7 +468,7 @@ grub_png_get_huff_code (struct grub_png_data *data, struct huff_table *ht)
b35c50
   int code, i;
b35c50
 
b35c50
   code = 0;
b35c50
-  for (i = 0; i < ht->max_length; i++)
b35c50
+  for (i = 0; i < ht->max_length && grub_errno == GRUB_ERR_NONE; i++)
b35c50
     {
b35c50
       code = (code << 1) + grub_png_get_bits (data, 1);
b35c50
       if (code < ht->maxval[i])
b35c50
@@ -504,8 +523,14 @@ grub_png_init_dynamic_block (struct grub_png_data *data)
b35c50
   grub_uint8_t lens[DEFLATE_HCLEN_MAX];
b35c50
 
b35c50
   nl = DEFLATE_HLIT_BASE + grub_png_get_bits (data, 5);
b35c50
+  if (grub_errno != GRUB_ERR_NONE)
b35c50
+    return grub_errno;
b35c50
   nd = DEFLATE_HDIST_BASE + grub_png_get_bits (data, 5);
b35c50
+  if (grub_errno != GRUB_ERR_NONE)
b35c50
+    return grub_errno;
b35c50
   nb = DEFLATE_HCLEN_BASE + grub_png_get_bits (data, 4);
b35c50
+  if (grub_errno != GRUB_ERR_NONE)
b35c50
+    return grub_errno;
b35c50
 
b35c50
   if ((nl > DEFLATE_HLIT_MAX) || (nd > DEFLATE_HDIST_MAX) ||
b35c50
       (nb > DEFLATE_HCLEN_MAX))
b35c50
@@ -533,7 +558,7 @@ grub_png_init_dynamic_block (struct grub_png_data *data)
b35c50
 			    data->dist_offset);
b35c50
 
b35c50
   prev = 0;
b35c50
-  for (i = 0; i < nl + nd; i++)
b35c50
+  for (i = 0; i < nl + nd && grub_errno == GRUB_ERR_NONE; i++)
b35c50
     {
b35c50
       int n, code;
b35c50
       struct huff_table *ht;
b35c50
@@ -721,17 +746,21 @@ grub_png_read_dynamic_block (struct grub_png_data *data)
b35c50
 	  len = cplens[n];
b35c50
 	  if (cplext[n])
b35c50
 	    len += grub_png_get_bits (data, cplext[n]);
b35c50
+	  if (grub_errno != GRUB_ERR_NONE)
b35c50
+	    return grub_errno;
b35c50
 
b35c50
 	  n = grub_png_get_huff_code (data, &data->dist_table);
b35c50
 	  dist = cpdist[n];
b35c50
 	  if (cpdext[n])
b35c50
 	    dist += grub_png_get_bits (data, cpdext[n]);
b35c50
+	  if (grub_errno != GRUB_ERR_NONE)
b35c50
+	    return grub_errno;
b35c50
 
b35c50
 	  pos = data->wp - dist;
b35c50
 	  if (pos < 0)
b35c50
 	    pos += WSIZE;
b35c50
 
b35c50
-	  while (len > 0)
b35c50
+	  while (len > 0 && grub_errno == GRUB_ERR_NONE)
b35c50
 	    {
b35c50
 	      data->slide[data->wp] = data->slide[pos];
b35c50
 	      grub_png_output_byte (data, data->slide[data->wp]);
b35c50
@@ -759,7 +788,11 @@ grub_png_decode_image_data (struct grub_png_data *data)
b35c50
   int final;
b35c50
 
b35c50
   cmf = grub_png_get_byte (data);
b35c50
+  if (grub_errno != GRUB_ERR_NONE)
b35c50
+    return grub_errno;
b35c50
   flg = grub_png_get_byte (data);
b35c50
+  if (grub_errno != GRUB_ERR_NONE)
b35c50
+    return grub_errno;
b35c50
 
b35c50
   if ((cmf & 0xF) != Z_DEFLATED)
b35c50
     return grub_error (GRUB_ERR_BAD_FILE_TYPE,
b35c50
@@ -774,7 +807,11 @@ grub_png_decode_image_data (struct grub_png_data *data)
b35c50
       int block_type;
b35c50
 
b35c50
       final = grub_png_get_bits (data, 1);
b35c50
+      if (grub_errno != GRUB_ERR_NONE)
b35c50
+	return grub_errno;
b35c50
       block_type = grub_png_get_bits (data, 2);
b35c50
+      if (grub_errno != GRUB_ERR_NONE)
b35c50
+	return grub_errno;
b35c50
 
b35c50
       switch (block_type)
b35c50
 	{
b35c50
@@ -790,7 +827,7 @@ grub_png_decode_image_data (struct grub_png_data *data)
b35c50
 	    grub_png_get_byte (data);
b35c50
 	    grub_png_get_byte (data);
b35c50
 
b35c50
-	    for (i = 0; i < len; i++)
b35c50
+	    for (i = 0; i < len && grub_errno == GRUB_ERR_NONE; i++)
b35c50
 	      grub_png_output_byte (data, grub_png_get_byte (data));
b35c50
 
b35c50
 	    break;
b35c50
@@ -1045,6 +1082,8 @@ grub_png_decode_png (struct grub_png_data *data)
b35c50
 
b35c50
       len = grub_png_get_dword (data);
b35c50
       type = grub_png_get_dword (data);
b35c50
+      if (grub_errno != GRUB_ERR_NONE)
b35c50
+	break;
b35c50
       data->next_offset = data->file->offset + len + 4;
b35c50
 
b35c50
       switch (type)