Blame SOURCES/0413-io-gzio-Catch-missing-values-in-huft_build-and-bail.patch

b1bcb2
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
b1bcb2
From: Daniel Axtens <dja@axtens.net>
b1bcb2
Date: Thu, 21 Jan 2021 12:20:49 +1100
b1bcb2
Subject: [PATCH] io/gzio: Catch missing values in huft_build() and bail
b1bcb2
b1bcb2
In huft_build(), "v" is a table of values in order of bit length.
b1bcb2
The code later (when setting up table entries in "r") assumes that all
b1bcb2
elements of this array corresponding to a code are initialized and less
b1bcb2
than N_MAX. However, it doesn't enforce this.
b1bcb2
b1bcb2
With sufficiently manipulated inputs (e.g. from fuzzing), there can be
b1bcb2
elements of "v" that are not filled. Therefore a lookup into "e" or "d"
b1bcb2
will use an uninitialized value. This can lead to an invalid/OOB read on
b1bcb2
those values, often leading to a crash.
b1bcb2
b1bcb2
Signed-off-by: Daniel Axtens <dja@axtens.net>
b1bcb2
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
b1bcb2
---
b1bcb2
 grub-core/io/gzio.c | 10 +++++++++-
b1bcb2
 1 file changed, 9 insertions(+), 1 deletion(-)
b1bcb2
b1bcb2
diff --git a/grub-core/io/gzio.c b/grub-core/io/gzio.c
b1bcb2
index d3b12a61e61..3e14347827b 100644
b1bcb2
--- a/grub-core/io/gzio.c
b1bcb2
+++ b/grub-core/io/gzio.c
b1bcb2
@@ -495,6 +495,7 @@ huft_build (unsigned *b,	/* code lengths in bits (all assumed <= BMAX) */
b1bcb2
     }
b1bcb2
 
b1bcb2
   /* Make a table of values in order of bit lengths */
b1bcb2
+  grub_memset (v, N_MAX, ARRAY_SIZE (v));
b1bcb2
   p = b;
b1bcb2
   i = 0;
b1bcb2
   do
b1bcb2
@@ -576,11 +577,18 @@ huft_build (unsigned *b,	/* code lengths in bits (all assumed <= BMAX) */
b1bcb2
 	      r.v.n = (ush) (*p);	/* simple code is just the value */
b1bcb2
 	      p++;		/* one compiler does not like *p++ */
b1bcb2
 	    }
b1bcb2
-	  else
b1bcb2
+	  else if (*p < N_MAX)
b1bcb2
 	    {
b1bcb2
 	      r.e = (uch) e[*p - s];	/* non-simple--look up in lists */
b1bcb2
 	      r.v.n = d[*p++ - s];
b1bcb2
 	    }
b1bcb2
+	  else
b1bcb2
+	    {
b1bcb2
+	      /* Detected an uninitialised value, abort. */
b1bcb2
+	      if (h)
b1bcb2
+		huft_free (u[0]);
b1bcb2
+	      return 2;
b1bcb2
+	    }
b1bcb2
 
b1bcb2
 	  /* fill code-like entries with r */
b1bcb2
 	  f = 1 << (k - w);