nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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