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

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