Blame SOURCES/0414-commands-hashsum-Fix-a-memory-leak.patch

9723a8
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
9723a8
From: Chris Coulson <chris.coulson@canonical.com>
9723a8
Date: Tue, 1 Dec 2020 23:41:24 +0000
9723a8
Subject: [PATCH] commands/hashsum: Fix a memory leak
9723a8
9723a8
check_list() uses grub_file_getline(), which allocates a buffer.
9723a8
If the hash list file contains invalid lines, the function leaks
9723a8
this buffer when it returns an error.
9723a8
9723a8
Fixes: CID 176635
9723a8
9723a8
Signed-off-by: Chris Coulson <chris.coulson@canonical.com>
9723a8
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
9723a8
---
9723a8
 grub-core/commands/hashsum.c | 15 ++++++++++++---
9723a8
 1 file changed, 12 insertions(+), 3 deletions(-)
9723a8
9723a8
diff --git a/grub-core/commands/hashsum.c b/grub-core/commands/hashsum.c
3efed6
index 456ba908b6f..b8a22b0c8bb 100644
9723a8
--- a/grub-core/commands/hashsum.c
9723a8
+++ b/grub-core/commands/hashsum.c
9723a8
@@ -128,11 +128,17 @@ check_list (const gcry_md_spec_t *hash, const char *hashfilename,
9723a8
 	  high = hextoval (*p++);
9723a8
 	  low = hextoval (*p++);
9723a8
 	  if (high < 0 || low < 0)
9723a8
-	    return grub_error (GRUB_ERR_BAD_FILE_TYPE, "invalid hash list");
9723a8
+	    {
9723a8
+	      grub_free (buf);
9723a8
+	      return grub_error (GRUB_ERR_BAD_FILE_TYPE, "invalid hash list");
9723a8
+	    }
9723a8
 	  expected[i] = (high << 4) | low;
9723a8
 	}
9723a8
       if ((p[0] != ' ' && p[0] != '\t') || (p[1] != ' ' && p[1] != '\t'))
9723a8
-	return grub_error (GRUB_ERR_BAD_FILE_TYPE, "invalid hash list");
9723a8
+	{
9723a8
+	  grub_free (buf);
9723a8
+	  return grub_error (GRUB_ERR_BAD_FILE_TYPE, "invalid hash list");
9723a8
+	}
9723a8
       p += 2;
9723a8
       if (prefix)
9723a8
 	{
9723a8
@@ -140,7 +146,10 @@ check_list (const gcry_md_spec_t *hash, const char *hashfilename,
9723a8
 	  
9723a8
 	  filename = grub_xasprintf ("%s/%s", prefix, p);
9723a8
 	  if (!filename)
9723a8
-	    return grub_errno;
3efed6
+	    {
3efed6
+	      grub_free (buf);
3efed6
+	      return grub_errno;
3efed6
+	    }
3efed6
 	  file = grub_file_open (filename, GRUB_FILE_TYPE_TO_HASH
3efed6
 				 | (!uncompress ? GRUB_FILE_TYPE_NO_DECOMPRESS
3efed6
 				    : GRUB_FILE_TYPE_NONE));