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

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