nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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