nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

Blame SOURCES/0246-video-readers-jpeg-Abort-sooner-if-a-read-operation-.patch

1c6ba0
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
1c6ba0
From: Daniel Axtens <dja@axtens.net>
1c6ba0
Date: Mon, 28 Jun 2021 14:16:14 +1000
1c6ba0
Subject: [PATCH] video/readers/jpeg: 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 ab2e5d2e4bff488bbb557ed435a61ae102ef9f0c)
1c6ba0
(cherry picked from commit 1ff8df0d2dea8ec7c8575241d5e7d6622c204ec3)
1c6ba0
---
1c6ba0
 grub-core/video/readers/jpeg.c | 86 ++++++++++++++++++++++++++++++++++--------
1c6ba0
 1 file changed, 70 insertions(+), 16 deletions(-)
1c6ba0
1c6ba0
diff --git a/grub-core/video/readers/jpeg.c b/grub-core/video/readers/jpeg.c
1c6ba0
index e31602f766..10225abd53 100644
1c6ba0
--- a/grub-core/video/readers/jpeg.c
1c6ba0
+++ b/grub-core/video/readers/jpeg.c
1c6ba0
@@ -109,9 +109,17 @@ static grub_uint8_t
1c6ba0
 grub_jpeg_get_byte (struct grub_jpeg_data *data)
1c6ba0
 {
1c6ba0
   grub_uint8_t r;
1c6ba0
+  grub_ssize_t bytes_read;
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
+		  "jpeg: unexpected end of data");
1c6ba0
+      return 0;
1c6ba0
+    }
1c6ba0
 
1c6ba0
   return r;
1c6ba0
 }
1c6ba0
@@ -120,9 +128,17 @@ static grub_uint16_t
1c6ba0
 grub_jpeg_get_word (struct grub_jpeg_data *data)
1c6ba0
 {
1c6ba0
   grub_uint16_t r;
1c6ba0
+  grub_ssize_t bytes_read;
1c6ba0
 
1c6ba0
   r = 0;
1c6ba0
-  grub_file_read (data->file, &r, sizeof (grub_uint16_t));
1c6ba0
+  bytes_read = grub_file_read (data->file, &r, sizeof (grub_uint16_t));
1c6ba0
+
1c6ba0
+  if (bytes_read != sizeof (grub_uint16_t))
1c6ba0
+    {
1c6ba0
+      grub_error (GRUB_ERR_BAD_FILE_TYPE,
1c6ba0
+		  "jpeg: unexpected end of data");
1c6ba0
+      return 0;
1c6ba0
+    }
1c6ba0
 
1c6ba0
   return grub_be_to_cpu16 (r);
1c6ba0
 }
1c6ba0
@@ -135,6 +151,11 @@ grub_jpeg_get_bit (struct grub_jpeg_data *data)
1c6ba0
   if (data->bit_mask == 0)
1c6ba0
     {
1c6ba0
       data->bit_save = grub_jpeg_get_byte (data);
1c6ba0
+      if (grub_errno != GRUB_ERR_NONE) {
1c6ba0
+	grub_error (GRUB_ERR_BAD_FILE_TYPE,
1c6ba0
+		    "jpeg: file read error");
1c6ba0
+	return 0;
1c6ba0
+      }
1c6ba0
       if (data->bit_save == JPEG_ESC_CHAR)
1c6ba0
 	{
1c6ba0
 	  if (grub_jpeg_get_byte (data) != 0)
1c6ba0
@@ -143,6 +164,11 @@ grub_jpeg_get_bit (struct grub_jpeg_data *data)
1c6ba0
 			  "jpeg: invalid 0xFF in data stream");
1c6ba0
 	      return 0;
1c6ba0
 	    }
1c6ba0
+	  if (grub_errno != GRUB_ERR_NONE)
1c6ba0
+	    {
1c6ba0
+	      grub_error (GRUB_ERR_BAD_FILE_TYPE, "jpeg: file read error");
1c6ba0
+	      return 0;
1c6ba0
+	    }
1c6ba0
 	}
1c6ba0
       data->bit_mask = 0x80;
1c6ba0
     }
1c6ba0
@@ -161,7 +187,7 @@ grub_jpeg_get_number (struct grub_jpeg_data *data, int num)
1c6ba0
     return 0;
1c6ba0
 
1c6ba0
   msb = value = grub_jpeg_get_bit (data);
1c6ba0
-  for (i = 1; i < num; i++)
1c6ba0
+  for (i = 1; i < num && grub_errno == GRUB_ERR_NONE; i++)
1c6ba0
     value = (value << 1) + (grub_jpeg_get_bit (data) != 0);
1c6ba0
   if (!msb)
1c6ba0
     value += 1 - (1 << num);
1c6ba0
@@ -202,6 +228,8 @@ grub_jpeg_decode_huff_table (struct grub_jpeg_data *data)
1c6ba0
   while (data->file->offset + sizeof (count) + 1 <= next_marker)
1c6ba0
     {
1c6ba0
       id = grub_jpeg_get_byte (data);
1c6ba0
+      if (grub_errno != GRUB_ERR_NONE)
1c6ba0
+	return grub_errno;
1c6ba0
       ac = (id >> 4) & 1;
1c6ba0
       id &= 0xF;
1c6ba0
       if (id > 1)
1c6ba0
@@ -252,6 +280,8 @@ grub_jpeg_decode_quan_table (struct grub_jpeg_data *data)
1c6ba0
 
1c6ba0
   next_marker = data->file->offset;
1c6ba0
   next_marker += grub_jpeg_get_word (data);
1c6ba0
+  if (grub_errno != GRUB_ERR_NONE)
1c6ba0
+    return grub_errno;
1c6ba0
 
1c6ba0
   if (next_marker > data->file->size)
1c6ba0
     {
1c6ba0
@@ -263,6 +293,8 @@ grub_jpeg_decode_quan_table (struct grub_jpeg_data *data)
1c6ba0
 	 <= next_marker)
1c6ba0
     {
1c6ba0
       id = grub_jpeg_get_byte (data);
1c6ba0
+      if (grub_errno != GRUB_ERR_NONE)
1c6ba0
+        return grub_errno;
1c6ba0
       if (id >= 0x10)		/* Upper 4-bit is precision.  */
1c6ba0
 	return grub_error (GRUB_ERR_BAD_FILE_TYPE,
1c6ba0
 			   "jpeg: only 8-bit precision is supported");
1c6ba0
@@ -294,6 +326,9 @@ grub_jpeg_decode_sof (struct grub_jpeg_data *data)
1c6ba0
   next_marker = data->file->offset;
1c6ba0
   next_marker += grub_jpeg_get_word (data);
1c6ba0
 
1c6ba0
+  if (grub_errno != GRUB_ERR_NONE)
1c6ba0
+    return grub_errno;
1c6ba0
+
1c6ba0
   if (grub_jpeg_get_byte (data) != 8)
1c6ba0
     return grub_error (GRUB_ERR_BAD_FILE_TYPE,
1c6ba0
 		       "jpeg: only 8-bit precision is supported");
1c6ba0
@@ -319,6 +354,8 @@ grub_jpeg_decode_sof (struct grub_jpeg_data *data)
1c6ba0
 	return grub_error (GRUB_ERR_BAD_FILE_TYPE, "jpeg: invalid index");
1c6ba0
 
1c6ba0
       ss = grub_jpeg_get_byte (data);	/* Sampling factor.  */
1c6ba0
+      if (grub_errno != GRUB_ERR_NONE)
1c6ba0
+	return grub_errno;
1c6ba0
       if (!id)
1c6ba0
 	{
1c6ba0
 	  grub_uint8_t vs, hs;
1c6ba0
@@ -498,7 +535,7 @@ grub_jpeg_idct_transform (jpeg_data_unit_t du)
1c6ba0
     }
1c6ba0
 }
1c6ba0
 
1c6ba0
-static void
1c6ba0
+static grub_err_t
1c6ba0
 grub_jpeg_decode_du (struct grub_jpeg_data *data, int id, jpeg_data_unit_t du)
1c6ba0
 {
1c6ba0
   int h1, h2, qt;
1c6ba0
@@ -513,6 +550,9 @@ grub_jpeg_decode_du (struct grub_jpeg_data *data, int id, jpeg_data_unit_t du)
1c6ba0
   data->dc_value[id] +=
1c6ba0
     grub_jpeg_get_number (data, grub_jpeg_get_huff_code (data, h1));
1c6ba0
 
1c6ba0
+  if (grub_errno != GRUB_ERR_NONE)
1c6ba0
+    return grub_errno;
1c6ba0
+
1c6ba0
   du[0] = data->dc_value[id] * (int) data->quan_table[qt][0];
1c6ba0
   pos = 1;
1c6ba0
   while (pos < ARRAY_SIZE (data->quan_table[qt]))
1c6ba0
@@ -527,11 +567,13 @@ grub_jpeg_decode_du (struct grub_jpeg_data *data, int id, jpeg_data_unit_t du)
1c6ba0
       num >>= 4;
1c6ba0
       pos += num;
1c6ba0
 
1c6ba0
+      if (grub_errno != GRUB_ERR_NONE)
1c6ba0
+        return grub_errno;
1c6ba0
+
1c6ba0
       if (pos >= ARRAY_SIZE (jpeg_zigzag_order))
1c6ba0
 	{
1c6ba0
-	  grub_error (GRUB_ERR_BAD_FILE_TYPE,
1c6ba0
-		      "jpeg: invalid position in zigzag order!?");
1c6ba0
-	  return;
1c6ba0
+	  return grub_error (GRUB_ERR_BAD_FILE_TYPE,
1c6ba0
+			     "jpeg: invalid position in zigzag order!?");
1c6ba0
 	}
1c6ba0
 
1c6ba0
       du[jpeg_zigzag_order[pos]] = val * (int) data->quan_table[qt][pos];
1c6ba0
@@ -539,6 +581,7 @@ grub_jpeg_decode_du (struct grub_jpeg_data *data, int id, jpeg_data_unit_t du)
1c6ba0
     }
1c6ba0
 
1c6ba0
   grub_jpeg_idct_transform (du);
1c6ba0
+  return GRUB_ERR_NONE;
1c6ba0
 }
1c6ba0
 
1c6ba0
 static void
1c6ba0
@@ -597,7 +640,8 @@ grub_jpeg_decode_sos (struct grub_jpeg_data *data)
1c6ba0
   data_offset += grub_jpeg_get_word (data);
1c6ba0
 
1c6ba0
   cc = grub_jpeg_get_byte (data);
1c6ba0
-
1c6ba0
+  if (grub_errno != GRUB_ERR_NONE)
1c6ba0
+    return grub_errno;
1c6ba0
   if (cc != 3 && cc != 1)
1c6ba0
     return grub_error (GRUB_ERR_BAD_FILE_TYPE,
1c6ba0
 		       "jpeg: component count must be 1 or 3");
1c6ba0
@@ -610,7 +654,8 @@ grub_jpeg_decode_sos (struct grub_jpeg_data *data)
1c6ba0
       id = grub_jpeg_get_byte (data) - 1;
1c6ba0
       if ((id < 0) || (id >= 3))
1c6ba0
 	return grub_error (GRUB_ERR_BAD_FILE_TYPE, "jpeg: invalid index");
1c6ba0
-
1c6ba0
+      if (grub_errno != GRUB_ERR_NONE)
1c6ba0
+	return grub_errno;
1c6ba0
       ht = grub_jpeg_get_byte (data);
1c6ba0
       data->comp_index[id][1] = (ht >> 4);
1c6ba0
       data->comp_index[id][2] = (ht & 0xF) + 2;
1c6ba0
@@ -618,11 +663,14 @@ grub_jpeg_decode_sos (struct grub_jpeg_data *data)
1c6ba0
       if ((data->comp_index[id][1] < 0) || (data->comp_index[id][1] > 3) ||
1c6ba0
 	  (data->comp_index[id][2] < 0) || (data->comp_index[id][2] > 3))
1c6ba0
 	return grub_error (GRUB_ERR_BAD_FILE_TYPE, "jpeg: invalid hufftable index");
1c6ba0
+      if (grub_errno != GRUB_ERR_NONE)
1c6ba0
+	return grub_errno;
1c6ba0
     }
1c6ba0
 
1c6ba0
   grub_jpeg_get_byte (data);	/* Skip 3 unused bytes.  */
1c6ba0
   grub_jpeg_get_word (data);
1c6ba0
-
1c6ba0
+  if (grub_errno != GRUB_ERR_NONE)
1c6ba0
+    return grub_errno;
1c6ba0
   if (data->file->offset != data_offset)
1c6ba0
     return grub_error (GRUB_ERR_BAD_FILE_TYPE, "jpeg: extra byte in sos");
1c6ba0
 
1c6ba0
@@ -640,6 +688,7 @@ grub_jpeg_decode_data (struct grub_jpeg_data *data)
1c6ba0
 {
1c6ba0
   unsigned c1, vb, hb, nr1, nc1;
1c6ba0
   int rst = data->dri;
1c6ba0
+  grub_err_t err = GRUB_ERR_NONE;
1c6ba0
 
1c6ba0
   vb = 8 << data->log_vs;
1c6ba0
   hb = 8 << data->log_hs;
1c6ba0
@@ -660,17 +709,22 @@ grub_jpeg_decode_data (struct grub_jpeg_data *data)
1c6ba0
 
1c6ba0
 	for (r2 = 0; r2 < (1U << data->log_vs); r2++)
1c6ba0
 	  for (c2 = 0; c2 < (1U << data->log_hs); c2++)
1c6ba0
-	    grub_jpeg_decode_du (data, 0, data->ydu[r2 * 2 + c2]);
1c6ba0
+            {
1c6ba0
+              err = grub_jpeg_decode_du (data, 0, data->ydu[r2 * 2 + c2]);
1c6ba0
+              if (err != GRUB_ERR_NONE)
1c6ba0
+                return err;
1c6ba0
+            }
1c6ba0
 
1c6ba0
 	if (data->color_components >= 3)
1c6ba0
 	  {
1c6ba0
-	    grub_jpeg_decode_du (data, 1, data->cbdu);
1c6ba0
-	    grub_jpeg_decode_du (data, 2, data->crdu);
1c6ba0
+	    err = grub_jpeg_decode_du (data, 1, data->cbdu);
1c6ba0
+	    if (err != GRUB_ERR_NONE)
1c6ba0
+	      return err;
1c6ba0
+	    err = grub_jpeg_decode_du (data, 2, data->crdu);
1c6ba0
+	    if (err != GRUB_ERR_NONE)
1c6ba0
+	      return err;
1c6ba0
 	  }
1c6ba0
 
1c6ba0
-	if (grub_errno)
1c6ba0
-	  return grub_errno;
1c6ba0
-
1c6ba0
 	nr2 = (data->r1 == nr1 - 1) ? (data->image_height - data->r1 * vb) : vb;
1c6ba0
 	nc2 = (c1 == nc1 - 1) ? (data->image_width - c1 * hb) : hb;
1c6ba0