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

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