Blame SOURCES/0413-io-gzio-Add-init_dynamic_block-clean-up-if-unpacking.patch

b1bcb2
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
b1bcb2
From: Daniel Axtens <dja@axtens.net>
b1bcb2
Date: Thu, 21 Jan 2021 00:05:58 +1100
b1bcb2
Subject: [PATCH] io/gzio: Add init_dynamic_block() clean up if unpacking codes
b1bcb2
 fails
b1bcb2
b1bcb2
init_dynamic_block() didn't clean up gzio->tl and td in some error
b1bcb2
paths. This left td pointing to part of tl. Then in grub_gzio_close(),
b1bcb2
when tl was freed the storage for td would also be freed. The code then
b1bcb2
attempts to free td explicitly, performing a UAF and then a double free.
b1bcb2
b1bcb2
Explicitly clean up tl and td in the error paths.
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 | 12 +++++++++---
b1bcb2
 1 file changed, 9 insertions(+), 3 deletions(-)
b1bcb2
b1bcb2
diff --git a/grub-core/io/gzio.c b/grub-core/io/gzio.c
b1bcb2
index 09e0e2842f2..d3b12a61e61 100644
b1bcb2
--- a/grub-core/io/gzio.c
b1bcb2
+++ b/grub-core/io/gzio.c
b1bcb2
@@ -941,7 +941,7 @@ init_dynamic_block (grub_gzio_t gzio)
b1bcb2
 	  if ((unsigned) i + j > n)
b1bcb2
 	    {
b1bcb2
 	      grub_error (GRUB_ERR_BAD_COMPRESSED_DATA, "too many codes found");
b1bcb2
-	      return;
b1bcb2
+	      goto fail;
b1bcb2
 	    }
b1bcb2
 	  while (j--)
b1bcb2
 	    ll[i++] = l;
b1bcb2
@@ -954,7 +954,7 @@ init_dynamic_block (grub_gzio_t gzio)
b1bcb2
 	  if ((unsigned) i + j > n)
b1bcb2
 	    {
b1bcb2
 	      grub_error (GRUB_ERR_BAD_COMPRESSED_DATA, "too many codes found");
b1bcb2
-	      return;
b1bcb2
+	      goto fail;
b1bcb2
 	    }
b1bcb2
 	  while (j--)
b1bcb2
 	    ll[i++] = 0;
b1bcb2
@@ -969,7 +969,7 @@ init_dynamic_block (grub_gzio_t gzio)
b1bcb2
 	  if ((unsigned) i + j > n)
b1bcb2
 	    {
b1bcb2
 	      grub_error (GRUB_ERR_BAD_COMPRESSED_DATA, "too many codes found");
b1bcb2
-	      return;
b1bcb2
+	      goto fail;
b1bcb2
 	    }
b1bcb2
 	  while (j--)
b1bcb2
 	    ll[i++] = 0;
b1bcb2
@@ -1007,6 +1007,12 @@ init_dynamic_block (grub_gzio_t gzio)
b1bcb2
   /* indicate we're now working on a block */
b1bcb2
   gzio->code_state = 0;
b1bcb2
   gzio->block_len++;
b1bcb2
+  return;
b1bcb2
+
b1bcb2
+ fail:
b1bcb2
+  huft_free (gzio->tl);
b1bcb2
+  gzio->td = NULL;
b1bcb2
+  gzio->tl = NULL;
b1bcb2
 }
b1bcb2
 
b1bcb2