nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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