c06457c
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
c06457c
From: Daniel Axtens <dja@axtens.net>
c06457c
Date: Tue, 6 Jul 2021 23:25:07 +1000
c06457c
Subject: [PATCH] video/readers/png: Avoid heap OOB R/W inserting huff table
c06457c
 items
c06457c
c06457c
In fuzzing we observed crashes where a code would attempt to be inserted
c06457c
into a huffman table before the start, leading to a set of heap OOB reads
c06457c
and writes as table entries with negative indices were shifted around and
c06457c
the new code written in.
c06457c
c06457c
Catch the case where we would underflow the array and bail.
c06457c
c06457c
Fixes: CVE-2021-3696
c06457c
c06457c
Signed-off-by: Daniel Axtens <dja@axtens.net>
c06457c
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
c06457c
(cherry picked from commit 1ae9a91d42cb40da8a6f11fac65541858e340afa)
c06457c
(cherry picked from commit 132ccc681cf642ad748580f26b54c9259a7f43fd)
c06457c
---
c06457c
 grub-core/video/readers/png.c | 7 +++++++
c06457c
 1 file changed, 7 insertions(+)
c06457c
c06457c
diff --git a/grub-core/video/readers/png.c b/grub-core/video/readers/png.c
c06457c
index a3161e25b6..d7ed5aa6cf 100644
c06457c
--- a/grub-core/video/readers/png.c
c06457c
+++ b/grub-core/video/readers/png.c
c06457c
@@ -438,6 +438,13 @@ grub_png_insert_huff_item (struct huff_table *ht, int code, int len)
c06457c
   for (i = len; i < ht->max_length; i++)
c06457c
     n += ht->maxval[i];
c06457c
 
c06457c
+  if (n > ht->num_values)
c06457c
+    {
c06457c
+      grub_error (GRUB_ERR_BAD_FILE_TYPE,
c06457c
+		  "png: out of range inserting huffman table item");
c06457c
+      return;
c06457c
+    }
c06457c
+
c06457c
   for (i = 0; i < n; i++)
c06457c
     ht->values[ht->num_values - i] = ht->values[ht->num_values - i - 1];
c06457c