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

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