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

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