hc
2024-07-16 5fbd6e2385615a225453562361c4bdab3b15fda1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
From 18490336d91da2b532277cba56473bfed1376fc4 Mon Sep 17 00:00:00 2001
From: Daniel Axtens <dja@axtens.net>
Date: Thu, 21 Jan 2021 00:05:58 +1100
Subject: [PATCH] io/gzio: Add init_dynamic_block() clean up if unpacking codes
 fails
 
init_dynamic_block() didn't clean up gzio->tl and td in some error
paths. This left td pointing to part of tl. Then in grub_gzio_close(),
when tl was freed the storage for td would also be freed. The code then
attempts to free td explicitly, performing a UAF and then a double free.
 
Explicitly clean up tl and td in the error paths.
 
Signed-off-by: Daniel Axtens <dja@axtens.net>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Signed-off-by: Stefan Sørensen <stefan.sorensen@spectralink.com>
---
 grub-core/io/gzio.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
 
diff --git a/grub-core/io/gzio.c b/grub-core/io/gzio.c
index 4a8eaea..4236f0f 100644
--- a/grub-core/io/gzio.c
+++ b/grub-core/io/gzio.c
@@ -953,7 +953,7 @@ init_dynamic_block (grub_gzio_t gzio)
       if ((unsigned) i + j > n)
         {
           grub_error (GRUB_ERR_BAD_COMPRESSED_DATA, "too many codes found");
-          return;
+          goto fail;
         }
       while (j--)
         ll[i++] = l;
@@ -966,7 +966,7 @@ init_dynamic_block (grub_gzio_t gzio)
       if ((unsigned) i + j > n)
         {
           grub_error (GRUB_ERR_BAD_COMPRESSED_DATA, "too many codes found");
-          return;
+          goto fail;
         }
       while (j--)
         ll[i++] = 0;
@@ -981,7 +981,7 @@ init_dynamic_block (grub_gzio_t gzio)
       if ((unsigned) i + j > n)
         {
           grub_error (GRUB_ERR_BAD_COMPRESSED_DATA, "too many codes found");
-          return;
+          goto fail;
         }
       while (j--)
         ll[i++] = 0;
@@ -1019,6 +1019,12 @@ init_dynamic_block (grub_gzio_t gzio)
   /* indicate we're now working on a block */
   gzio->code_state = 0;
   gzio->block_len++;
+  return;
+
+ fail:
+  huft_free (gzio->tl);
+  gzio->td = NULL;
+  gzio->tl = NULL;
 }
 
 
-- 
2.14.2