Blame SOURCES/0397-video-readers-jpeg-Catch-OOB-reads-writes-in-grub_jp.patch

b1bcb2
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
b1bcb2
From: Daniel Axtens <dja@axtens.net>
b1bcb2
Date: Fri, 15 Jan 2021 13:29:53 +1100
b1bcb2
Subject: [PATCH] video/readers/jpeg: Catch OOB reads/writes in
b1bcb2
 grub_jpeg_decode_du()
b1bcb2
b1bcb2
The key line is:
b1bcb2
b1bcb2
  du[jpeg_zigzag_order[pos]] = val * (int) data->quan_table[qt][pos];
b1bcb2
b1bcb2
jpeg_zigzag_order is grub_uint8_t[64].
b1bcb2
b1bcb2
I don't understand JPEG decoders quite well enough to explain what's
b1bcb2
going on here. However, I observe sometimes pos=64, which leads to an
b1bcb2
OOB read of the jpeg_zigzag_order global then an OOB write to du.
b1bcb2
That leads to various unpleasant memory corruption conditions.
b1bcb2
b1bcb2
Catch where pos >= ARRAY_SIZE(jpeg_zigzag_order) and bail.
b1bcb2
b1bcb2
Signed-off-by: Daniel Axtens <dja@axtens.net>
b1bcb2
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
b1bcb2
---
b1bcb2
 grub-core/video/readers/jpeg.c | 8 ++++++++
b1bcb2
 1 file changed, 8 insertions(+)
b1bcb2
b1bcb2
diff --git a/grub-core/video/readers/jpeg.c b/grub-core/video/readers/jpeg.c
b1bcb2
index 5d3fa4de37d..2cbbb467ebb 100644
b1bcb2
--- a/grub-core/video/readers/jpeg.c
b1bcb2
+++ b/grub-core/video/readers/jpeg.c
b1bcb2
@@ -523,6 +523,14 @@ grub_jpeg_decode_du (struct grub_jpeg_data *data, int id, jpeg_data_unit_t du)
b1bcb2
       val = grub_jpeg_get_number (data, num & 0xF);
b1bcb2
       num >>= 4;
b1bcb2
       pos += num;
b1bcb2
+
b1bcb2
+      if (pos >= ARRAY_SIZE (jpeg_zigzag_order))
b1bcb2
+	{
b1bcb2
+	  grub_error (GRUB_ERR_BAD_FILE_TYPE,
b1bcb2
+		      "jpeg: invalid position in zigzag order!?");
b1bcb2
+	  return;
b1bcb2
+	}
b1bcb2
+
b1bcb2
       du[jpeg_zigzag_order[pos]] = val * (int) data->quan_table[qt][pos];
b1bcb2
       pos++;
b1bcb2
     }