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

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