Skip to contentSkip to conversation

Fix Coverity untrusted loop bound bugs in jpeg.c

PATCH Superseded Alec Brown 3 participants 6 messages Oct 20, 2022 – Oct 26, 2022

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Alec Brown

author
#0 lore raw

cover Fix Coverity untrusted loop bound bugs in jpeg.c

In grub-core/video/readers/jpeg.c, Coverity identified an untrusted loop bound bug. After resolving this bug, a private Coverity scan identified another untrusted loop bound bug in a different function. Since this bug only shows up after resolving the first bug, there isn't a CID for the second bug.

The Coverity bugs being addressed are:
CID 292450

Alec Brown (2):
video/readers: Add artificial limit to image dimensions video/readers/jpeg: Check next_marker is within file size

 docs/grub.texi                 |  3 ++-
 grub-core/video/readers/jpeg.c | 12 +++++++++++-
 grub-core/video/readers/png.c  |  6 +++++-
 grub-core/video/readers/tga.c  |  7 +++++++
 include/grub/bitmap.h          |  2 ++
 5 files changed, 27 insertions(+), 3 deletions(-)
author
#1 lore raw

PATCH 1/2 video/readers: Add artificial limit to image dimensions

In grub-core/video/readers/jpeg.c, the height and width of a JPEG image don't have an upper limit for how big the JPEG image can be. In coverity, this is getting flagged as an untrusted loop bound. This issue can also seen in PNG and TGA format images as well but coverity isn't flagging it. To prevent this, the constant IMAGE_HW_MAX_PX is being added to bitmap.h, which has a value of 16384, to act as an artifical limit and restrict the height and width of images. This value was picked as it is double the current max resolution size, which is 8K.

Fixes CID 292450
---
 docs/grub.texi                 | 3 ++-
 grub-core/video/readers/jpeg.c | 6 +++++-
 grub-core/video/readers/png.c  | 6 +++++-
 grub-core/video/readers/tga.c  | 7 +++++++
 include/grub/bitmap.h          | 2 ++
 5 files changed, 21 insertions(+), 3 deletions(-)
docs/grub.texi +2 -1
index 9835c878a..d448ca969 100644
@@ -1507,7 +1507,8 @@ resolution.  @xref{gfxmode}.
 Set a background image for use with the @samp{gfxterm} graphical terminal.
 The value of this option must be a file readable by GRUB at boot time, and
 it must end with @file{.png}, @file{.tga}, @file{.jpg}, or @file{.jpeg}.
-The image will be scaled if necessary to fit the screen.
+The image will be scaled if necessary to fit the screen. Image height and
+width will be restricted by an artificial limit of 16384.
 
 @item GRUB_THEME
 Set a theme for use with the @samp{gfxterm} graphical terminal.
grub-core/video/readers/jpeg.c +5 -1
index e31602f76..0eeea0e63 100644
@@ -301,7 +301,11 @@ grub_jpeg_decode_sof (struct grub_jpeg_data *data)
   data->image_height = grub_jpeg_get_word (data);
   data->image_width = grub_jpeg_get_word (data);
 
-  if ((!data->image_height) || (!data->image_width))
+  grub_dprintf ("jpeg", "image height: %d\n", data->image_height);
+  grub_dprintf ("jpeg", "image width: %d\n", data->image_width);
+
+  if ((!data->image_height) || (!data->image_width) ||
+      (data->image_height > IMAGE_HW_MAX_PX) || (data->image_width > IMAGE_HW_MAX_PX))
     return grub_error (GRUB_ERR_BAD_FILE_TYPE, "jpeg: invalid image size");
 
   cc = grub_jpeg_get_byte (data);
grub-core/video/readers/png.c +5 -1
index 54dfedf43..d658cbb0e 100644
@@ -252,7 +252,11 @@ grub_png_decode_image_header (struct grub_png_data *data)
   data->image_width = grub_png_get_dword (data);
   data->image_height = grub_png_get_dword (data);
 
-  if ((!data->image_height) || (!data->image_width))
+  grub_dprintf ("png", "image height: %d\n", data->image_height);
+  grub_dprintf ("png", "image width: %d\n", data->image_width);
+
+  if ((!data->image_height) || (!data->image_width) ||
+      (data->image_height > IMAGE_HW_MAX_PX) || (data->image_width > IMAGE_HW_MAX_PX))
     return grub_error (GRUB_ERR_BAD_FILE_TYPE, "png: invalid image size");
 
   color_bits = grub_png_get_byte (data);
grub-core/video/readers/tga.c +7 -0
index a9ec3a1b6..f2f563d06 100644
@@ -340,6 +340,13 @@ grub_video_reader_tga (struct grub_video_bitmap **bitmap,
   data.image_width = grub_le_to_cpu16 (data.hdr.image_width);
   data.image_height = grub_le_to_cpu16 (data.hdr.image_height);
 
+  grub_dprintf ("tga", "image height: %d\n", data.image_height);
+  grub_dprintf ("tga", "image width: %d\n", data.image_width);
+
+  /* Check image height and width are within restrictions */
+  if ((data.image_height > IMAGE_HW_MAX_PX) || (data.image_width > IMAGE_HW_MAX_PX))
+    return grub_error (GRUB_ERR_BAD_FILE_TYPE, "tga: invalid image size");
+
   /* Check that bitmap encoding is supported.  */
   switch (data.hdr.image_type)
     {
include/grub/bitmap.h +2 -0
index 5728f8ca3..149d37bfe 100644
@@ -24,6 +24,8 @@
 #include <grub/types.h>
 #include <grub/video.h>
 
+#define IMAGE_HW_MAX_PX		16384
+
 struct grub_video_bitmap
 {
   /* Bitmap format description.  */
Signature · 1 line
2.27.0
author
#2 lore raw

PATCH 2/2 video/readers/jpeg: Check next_marker is within file size

In reply to Alec Brown

In grub-core/video/readers/jpeg.c, the function grub_jpeg_decode_huff_table() has the variable next_marker which reads data from grub_jpeg_get_word() and then uses it as an upper limit in a while loop. However, the function isn't checking that next_marker is within the file size, so this check is being added to the function.

---
 grub-core/video/readers/jpeg.c | 6 ++++++
 1 file changed, 6 insertions(+)
grub-core/video/readers/jpeg.c +6 -0
index 0eeea0e63..c0f95fbf9 100644
@@ -199,6 +199,12 @@ grub_jpeg_decode_huff_table (struct grub_jpeg_data *data)
   next_marker = data->file->offset;
   next_marker += grub_jpeg_get_word (data);
 
+  if (next_marker > data->file->size)
+    {
+      /* Should never be set beyond the size of the file. */
+      return grub_error (GRUB_ERR_BAD_FILE_TYPE, "jpeg: invalid next reference");
+    }
+
   while (data->file->offset + sizeof (count) + 1 <= next_marker)
     {
       id = grub_jpeg_get_byte (data);
Signature · 1 line
2.27.0
#3 lore raw

Re: [PATCH 2/2] video/readers/jpeg: Check next_marker is within file size

Quoted 16 lines · Alec Brown

In grub-core/video/readers/jpeg.c, the function grub_jpeg_decode_huff_table() has the variable next_marker which reads data from grub_jpeg_get_word() and then uses it as an upper limit in a while loop. However, the function isn't checking that next_marker is within the file size, so this check is being added to the function.

---
 grub-core/video/readers/jpeg.c | 6 ++++++
 1 file changed, 6 insertions(+)
grub-core/video/readers/jpeg.c +0 -0
index 0eeea0e63..c0f95fbf9 100644

grub-core/video/readers/jpeg.c grub_jpeg_decode_huff_table (struct grub_jpeg_data *data) · line 207

Quoted 9 lines
grub-core/video/readers/jpeg.c +6 -0
@@ -199,6 +199,12 @@ grub_jpeg_decode_huff_table (struct grub_jpeg_data *data)
   next_marker = data->file->offset;
   next_marker += grub_jpeg_get_word (data);
 
+  if (next_marker > data->file->size)
+    {
+      /* Should never be set beyond the size of the file. */
+      return grub_error (GRUB_ERR_BAD_FILE_TYPE, "jpeg: invalid next reference");
+    }
+

This is a good idea. I briefly wondered why this didn't lead to issues detected in fuzzing. It'll be because the code reads byte by byte until we get an EOF and then the loop will bail. Your change is still good, I just wanted to double check that you hadn't accidentally found and fixed a more serious bug.

The fuller context:

while (data->file->offset + sizeof (count) + 1 <= next_marker)

    {
      id = grub_jpeg_get_byte (data);
      if (grub_errno != GRUB_ERR_NONE)
	return grub_errno;
Quoted 11 lines
grub-core/video/readers/jpeg.c +0 -0
   while (data->file->offset + sizeof (count) + 1 <= next_marker)
     {
       id = grub_jpeg_get_byte (data);
Signature · 7 lines
2.27.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
author
#4 lore raw

Re: [External] : Re: [PATCH 2/2] video/readers/jpeg: Check next_marker is within file size

Quoted 21 lines · Daniel Axtens
Quoted 19 lines · Alec Brown

In grub-core/video/readers/jpeg.c, the function grub_jpeg_decode_huff_table() has the variable next_marker which reads data from grub_jpeg_get_word() and then uses it as an upper limit in a while loop. However, the function isn't checking that next_marker is within the file size, so this check is being added to the function.

---
 grub-core/video/readers/jpeg.c | 6 ++++++
 1 file changed, 6 insertions(+)
grub-core/video/readers/jpeg.c +0 -0
index 0eeea0e63..c0f95fbf9 100644
@@ -199,6 +199,12 @@ grub_jpeg_decode_huff_table (struct grub_jpeg_data *data)
   next_marker = data->file->offset;
   next_marker += grub_jpeg_get_word (data);
 
Quoted 13 lines

grub-core/video/readers/jpeg.c grub_jpeg_decode_huff_table (struct grub_jpeg_data *data) · line 207

Quoted 6 lines
grub-core/video/readers/jpeg.c +6 -0
+  if (next_marker > data->file->size)
+    {
+      /* Should never be set beyond the size of the file. */
+      return grub_error (GRUB_ERR_BAD_FILE_TYPE, "jpeg: invalid next reference");
+    }
+

This is a good idea. I briefly wondered why this didn't lead to issues detected in fuzzing. It'll be because the code reads byte by byte until we get an EOF and then the loop will bail. Your change is still good, I just wanted to double check that you hadn't accidentally found and fixed a more serious bug.

I took a look at the code again and realized I made a mistake. There is already a fix for this issue. Turns out the branch I was using to develop my code was outdated and this patch is redundant. My bad.

Alec Brown

Quoted 20 lines

The fuller context:

while (data->file->offset + sizeof (count) + 1 <= next_marker)

    {
      id = grub_jpeg_get_byte (data);
      if (grub_errno != GRUB_ERR_NONE)
	return grub_errno;
Quoted 11 lines
grub-core/video/readers/jpeg.c +0 -0
   while (data->file->offset + sizeof (count) + 1 <= next_marker)
     {
       id = grub_jpeg_get_byte (data);
Signature · 7 lines
2.27.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://urldefense.com/v3/__https://lists.gnu.org/mailman/listinfo/grub-devel__;!!ACWV5N9M2RV99hQ!IlmVbvmGcbJZqlfowZKaM9HBO2MzpJDPrCjfmlLWmsN8tlaAmt5iNq5JZTiU3rQor7YXKdCwTYpAAQ$  
Reviewed-by on the v1 series
#5 lore raw
In reply to Alec Brown
Quoted 11 lines · Alec Brown

In grub-core/video/readers/jpeg.c, Coverity identified an untrusted loop bound bug. After resolving this bug, a private Coverity scan identified another untrusted loop bound bug in a different function. Since this bug only shows up after resolving the first bug, there isn't a CID for the second bug.

The Coverity bugs being addressed are:
CID 292450

Alec Brown (2):
video/readers: Add artificial limit to image dimensions video/readers/jpeg: Check next_marker is within file size

Thank you for fixing these issues!

Daniel