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