nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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