Blame SOURCES/0286-iso9660-Don-t-leak-memory-on-realloc-failures.patch

9723a8
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
c294fc
From: Peter Jones <pjones@redhat.com>
c294fc
Date: Sat, 4 Jul 2020 12:25:09 -0400
9723a8
Subject: [PATCH] iso9660: Don't leak memory on realloc() failures
c294fc
c294fc
Signed-off-by: Peter Jones <pjones@redhat.com>
c294fc
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
c294fc
Upstream-commit-id: f2bd30b2fe7
c294fc
---
c294fc
 grub-core/fs/iso9660.c | 24 ++++++++++++++++++++----
c294fc
 1 file changed, 20 insertions(+), 4 deletions(-)
c294fc
c294fc
diff --git a/grub-core/fs/iso9660.c b/grub-core/fs/iso9660.c
b71686
index f45841e2b..6fc9302bc 100644
c294fc
--- a/grub-core/fs/iso9660.c
c294fc
+++ b/grub-core/fs/iso9660.c
c294fc
@@ -533,14 +533,20 @@ add_part (struct iterate_dir_ctx *ctx,
c294fc
 {
c294fc
   int size = ctx->symlink ? grub_strlen (ctx->symlink) : 0;
c294fc
   grub_size_t sz;
c294fc
+  char *new;
c294fc
 
c294fc
   if (grub_add (size, len2, &sz) ||
c294fc
       grub_add (sz, 1, &sz))
c294fc
     return;
c294fc
 
c294fc
-  ctx->symlink = grub_realloc (ctx->symlink, sz);
c294fc
-  if (! ctx->symlink)
c294fc
-    return;
c294fc
+  new = grub_realloc (ctx->symlink, sz);
c294fc
+  if (!new)
c294fc
+    {
c294fc
+      grub_free (ctx->symlink);
c294fc
+      ctx->symlink = NULL;
c294fc
+      return;
c294fc
+    }
c294fc
+  ctx->symlink = new;
c294fc
 
c294fc
   grub_memcpy (ctx->symlink + size, part, len2);
c294fc
   ctx->symlink[size + len2] = 0;  
c294fc
@@ -634,7 +640,12 @@ susp_iterate_dir (struct grub_iso9660_susp_entry *entry,
c294fc
 		   is the length.  Both are part of the `Component
c294fc
 		   Record'.  */
c294fc
 		if (ctx->symlink && !ctx->was_continue)
c294fc
-		  add_part (ctx, "/", 1);
c294fc
+		  {
c294fc
+		    add_part (ctx, "/", 1);
c294fc
+		    if (grub_errno)
c294fc
+		      return grub_errno;
c294fc
+		  }
c294fc
+
c294fc
 		add_part (ctx, (char *) &entry->data[pos + 2],
c294fc
 			  entry->data[pos + 1]);
c294fc
 		ctx->was_continue = (entry->data[pos] & 1);
c294fc
@@ -653,6 +664,11 @@ susp_iterate_dir (struct grub_iso9660_susp_entry *entry,
c294fc
 	      add_part (ctx, "/", 1);
c294fc
 	      break;
c294fc
 	    }
c294fc
+
c294fc
+	  /* Check if grub_realloc() failed in add_part(). */
c294fc
+	  if (grub_errno)
c294fc
+	    return grub_errno;
c294fc
+
c294fc
 	  /* In pos + 1 the length of the `Component Record' is
c294fc
 	     stored.  */
c294fc
 	  pos += entry->data[pos + 1] + 2;