nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

Blame SOURCES/0245-video-readers-jpeg-Block-int-underflow-wild-pointer-.patch

1c6ba0
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
1c6ba0
From: Daniel Axtens <dja@axtens.net>
1c6ba0
Date: Wed, 7 Jul 2021 15:38:19 +1000
1c6ba0
Subject: [PATCH] video/readers/jpeg: Block int underflow -> wild pointer write
1c6ba0
1c6ba0
Certain 1 px wide images caused a wild pointer write in
1c6ba0
grub_jpeg_ycrcb_to_rgb(). This was caused because in grub_jpeg_decode_data(),
1c6ba0
we have the following loop:
1c6ba0
1c6ba0
for (; data->r1 < nr1 && (!data->dri || rst);
1c6ba0
     data->r1++, data->bitmap_ptr += (vb * data->image_width - hb * nc1) * 3)
1c6ba0
1c6ba0
We did not check if vb * width >= hb * nc1.
1c6ba0
1c6ba0
On a 64-bit platform, if that turns out to be negative, it will underflow,
1c6ba0
be interpreted as unsigned 64-bit, then be added to the 64-bit pointer, so
1c6ba0
we see data->bitmap_ptr jump, e.g.:
1c6ba0
1c6ba0
0x6180_0000_0480 to
1c6ba0
0x6181_0000_0498
1c6ba0
     ^
1c6ba0
     ~--- carry has occurred and this pointer is now far away from
1c6ba0
          any object.
1c6ba0
1c6ba0
On a 32-bit platform, it will decrement the pointer, creating a pointer
1c6ba0
that won't crash but will overwrite random data.
1c6ba0
1c6ba0
Catch the underflow and error out.
1c6ba0
1c6ba0
Fixes: CVE-2021-3697
1c6ba0
1c6ba0
Signed-off-by: Daniel Axtens <dja@axtens.net>
1c6ba0
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
1c6ba0
(cherry picked from commit 41aeb2004db9924fecd9f2dd64bc2a5a5594a4b5)
1c6ba0
(cherry picked from commit 5f9582490792108306d047379fed2371bee286f8)
1c6ba0
---
1c6ba0
 grub-core/video/readers/jpeg.c | 4 ++++
1c6ba0
 1 file changed, 4 insertions(+)
1c6ba0
1c6ba0
diff --git a/grub-core/video/readers/jpeg.c b/grub-core/video/readers/jpeg.c
1c6ba0
index 1df1171d78..2da04094b3 100644
1c6ba0
--- a/grub-core/video/readers/jpeg.c
1c6ba0
+++ b/grub-core/video/readers/jpeg.c
1c6ba0
@@ -705,6 +705,10 @@ grub_jpeg_decode_data (struct grub_jpeg_data *data)
1c6ba0
     return grub_error (GRUB_ERR_BAD_FILE_TYPE,
1c6ba0
 		       "jpeg: attempted to decode data before start of stream");
1c6ba0
 
1c6ba0
+  if (vb * data->image_width <= hb * nc1)
1c6ba0
+    return grub_error (GRUB_ERR_BAD_FILE_TYPE,
1c6ba0
+		       "jpeg: cannot decode image with these dimensions");
1c6ba0
+
1c6ba0
   for (; data->r1 < nr1 && (!data->dri || rst);
1c6ba0
        data->r1++, data->bitmap_ptr += (vb * data->image_width - hb * nc1) * 3)
1c6ba0
     for (c1 = 0;  c1 < nc1 && (!data->dri || rst);