Skip to contentSkip to conversation

Fix some Coverity resource leak bugs

PATCH In review Alec Brown 2 participants 4 messages Nov 10, 2021 – Nov 17, 2021

lore
Share & tools
lore

Alec Brown

author
#0 lore raw

cover Fix some Coverity resource leak bugs

There were a couple of commits which made fixes to a few Coverity bugs addressing resource leaks, but missed some cases where a variable was not being freed before a return statement was called.

The Coverity Bugs being addressed are:
CID 292443
CID 73804

Alec Brown (2):
commands/probe: Fix resource leaks
disk/ldm: Fix resource leak

 grub-core/commands/probe.c | 57 +++++++++++++++++++++++++++++++++++++++++++--------------
 grub-core/disk/ldm.c       |  1 +
 2 files changed, 44 insertions(+), 14 deletions(-)
author
#1 lore raw

PATCH 1/2 commands/probe: Fix resource leaks

Commit 1fc860bb76bb (commands/probe: Fix a resource leak when probing disks), missed other cases where grub_device_close() should be called before a return statement is called. Also found that grub_disk_close() wasn't being called when an error is being returned. To avoid conflict with grub_errno, grub_error_push() should be called before either grub_device_close() or grub_disk_close() is called and grub_error_pop() should be called before grub_errno is returned.

Fixes 1fc860bb76bb (commands/probe: Fix a resource leak when probing disks)
Fixes CID 292443
---
 grub-core/commands/probe.c | 57 ++++++++++++++++++++++++++++----------
 1 file changed, 43 insertions(+), 14 deletions(-)
grub-core/commands/probe.c +43 -14
index e53b61178..9a80ea54f 100644
@@ -122,7 +122,13 @@ grub_cmd_probe (grub_extcmd_context_t ctxt, int argc, char **args)
 	      grub_gpt_part_guid_t *guid;
 
 	      if (grub_disk_read(disk, p->offset, p->index, sizeof(entry), &entry))
-		return grub_errno;
+		{
+		  grub_error_push ();
+		  grub_disk_close (disk);
+		  grub_device_close (dev);
+		  grub_error_pop ();
+		  return grub_errno;
+		}
 	      guid = &entry.guid;
 	      grub_snprintf (val, sizeof(val),
 			     "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
@@ -153,7 +159,12 @@ grub_cmd_probe (grub_extcmd_context_t ctxt, int argc, char **args)
     }
   fs = grub_fs_probe (dev);
   if (! fs)
-    return grub_errno;
+    {
+      grub_error_push ();
+      grub_device_close (dev);
+      grub_error_pop ();
+      return grub_errno;
+    }
   if (state[3].set)
     {
       if (state[0].set)
@@ -167,14 +178,23 @@ grub_cmd_probe (grub_extcmd_context_t ctxt, int argc, char **args)
     {
       char *uuid;
       if (! fs->fs_uuid)
-	return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
-			   N_("%s does not support UUIDs"), fs->name);
+	{
+	  grub_device_close (dev);
+	  return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
+			     N_("%s does not support UUIDs"), fs->name);
+	}
       err = fs->fs_uuid (dev, &uuid);
       if (err)
-	return err;
+	{
+	  grub_device_close (dev);
+	  return err;
+	}
       if (! uuid)
-	return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
-			   N_("%s does not support UUIDs"), fs->name);
+	{
+	  grub_device_close (dev);
+	  return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
+			     N_("%s does not support UUIDs"), fs->name);
+	}
 
       if (state[0].set)
 	grub_env_set (state[0].arg, uuid);
@@ -188,16 +208,25 @@ grub_cmd_probe (grub_extcmd_context_t ctxt, int argc, char **args)
     {
       char *label;
       if (! fs->fs_label)
-	return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
-			   N_("filesystem `%s' does not support labels"),
-			   fs->name);
+	{
+	  grub_device_close (dev);
+	  return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
+			     N_("filesystem `%s' does not support labels"),
+			     fs->name);
+	}
       err = fs->fs_label (dev, &label);
       if (err)
-	return err;
+	{
+	  grub_device_close (dev);
+	  return err;
+	}
       if (! label)
-	return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
-			   N_("filesystem `%s' does not support labels"),
-			   fs->name);
+	{
+	  grub_device_close (dev);
+	  return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
+			     N_("filesystem `%s' does not support labels"),
+			     fs->name);
+	}
 
       if (state[0].set)
 	grub_env_set (state[0].arg, label);
Signature · 1 line
2.27.0
author
#2 lore raw

PATCH 2/2 disk/ldm: Fix resource leak

In reply to Alec Brown

Commit 23e39f50ca7a (disk/ldm: Make sure comp data is freed before exiting from make_vg()) fixed several spots in make_vg() where comp data was leaking memory when an error was being handled but missed one. To avoid leaking memory, comp should be freed when an error is being handled after comp has been successfully allocated memory in the for loop.

Fixes 23e39f50ca7a (disk/ldm: Make sure comp data is freed before exiting from make_vg())
Fixes CID 73804
---
 grub-core/disk/ldm.c | 1 +
 1 file changed, 1 insertion(+)
grub-core/disk/ldm.c +1 -0
index 4577a51dc..337abf704 100644
@@ -487,6 +487,7 @@ make_vg (grub_disk_t disk,
 	  ptr = vblk[i].dynamic;
 	  if (ptr + *ptr + 1 >= vblk[i].dynamic + sizeof (vblk[i].dynamic))
 	    {
+	      grub_free (comp);
 	      goto fail2;
 	    }
 	  comp->internal_id = grub_malloc ((grub_size_t) ptr[0] + 2);
Signature · 1 line
2.27.0
Reviewed-by on the v1 series
#3 lore raw
In reply to Alec Brown
Quoted 11 lines · Alec Brown

There were a couple of commits which made fixes to a few Coverity bugs addressing resource leaks, but missed some cases where a variable was not being freed before a return statement was called.

The Coverity Bugs being addressed are:
CID 292443
CID 73804

Alec Brown (2):
commands/probe: Fix resource leaks
disk/ldm: Fix resource leak

Thank you for fixing these issues.

Daniel