nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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