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