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

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