Skip to contentSkip to conversation

Fix misc. Coverity issues

PATCH In review Alec Brown via Grub-devel 2 participants 6 messages Nov 17, 2025

lore
Share & tools
lore
#0 lore raw

cover Fix misc. Coverity issues

Coverity marked multiple issues in grub-core/fs/zfs/zfs.c and grub-core/mmap/mmap.c. These patches help resolve these issues.

This patch set fixes the following CIDs:
CID 96655
CID 896330
CID 896331
CID 896334
CID 896336
CID 897337
CID 896340
CID 314020

Alec Brown

Alec Brown (2):
mmap/mmap: Fix resource leak
fs/zfs: Avoid pointer downcasting in dnode_get()

 grub-core/fs/zfs/zfs.c | 6 +++---
 grub-core/mmap/mmap.c  | 8 ++++++--
 2 files changed, 9 insertions(+), 5 deletions(-)

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#1 lore raw

PATCH 1/2 mmap/mmap: Fix resource leak

In the function grub_mmap_iterate(), memory is allocated to "ctx.scanline_events" and "present" but isn't freed when error handling grub_malloc(). Prior to returning grub_errno, these variables should be freed to prevent a resource leak.

Fixes CID 96655
---
 grub-core/mmap/mmap.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
grub-core/mmap/mmap.c +6 -2
index 8f03b7765..7c7d3911c 100644
@@ -242,8 +242,12 @@ grub_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
 	  else
 	    {
 	      struct mm_list *n = grub_malloc (sizeof (*n));
-              if (n == NULL)
-                return grub_errno;
+	      if (n == NULL)
+		{
+		  grub_free (ctx.scanline_events);
+		  grub_free (present);
+		  return grub_errno;
+		}
 
 	      n->val = ctx.scanline_events[i].memtype;
 	      n->present = 1;
Signature · 7 lines
2.27.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] mmap/mmap: Fix resource leak

Quoted 12 lines · Alec Brown via Grub-devel

In the function grub_mmap_iterate(), memory is allocated to "ctx.scanline_events" and "present" but isn't freed when error handling grub_malloc(). Prior to returning grub_errno, these variables should be freed to prevent a resource leak.

Fixes CID 96655

Thanks,
Sudhakar

Quoted 31 lines

---
grub-core/mmap/mmap.c | 8 ++++++--

1 file changed, 6 insertions(+), 2 deletions(-)
grub-core/mmap/mmap.c +6 -2
index 8f03b7765..7c7d3911c 100644
@@ -242,8 +242,12 @@ grub_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
 else
   {
     struct mm_list *n = grub_malloc (sizeof (*n));
-              if (n == NULL)
-                return grub_errno;
+      if (n == NULL)
+ {
+  grub_free (ctx.scanline_events);
+  grub_free (present);
+  return grub_errno;
+ }
 
     n->val = ctx.scanline_events[i].memtype;
     n->present = 1;
Signature · 7 lines
2.27.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#3 lore raw

PATCH 2/2 fs/zfs: Avoid pointer downcasting in dnode_get()

In reply to Alec Brown via Grub-devel

Coverity marks multiple issues in grub-core/fs/zfs/zfs.c as either "Untrusted value as argument", "Untrusted pointer read", or "Untrusted loop bound". Each of these issues share a common cause where Coverity finds that data->dnode_buf gets tainted by dnbuf since it is downcasting from (void *) to (dnode_phys_t *) and could imply that the data the pointer points to is tainted. However, the function zio_read(), which reads this data from disk, sanitizes this data by verifying its checksum. To resolve the issues for Coverity, setting dnbuf to (dnode_phys_t *) at the start of the function dnode_get() seems to do the trick.

Fixes CID 896330
Fixes CID 896331
Fixes CID 896334
Fixes CID 896336
Fixes CID 897337
Fixes CID 896340
Fixes CID 314020
---
 grub-core/fs/zfs/zfs.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
grub-core/fs/zfs/zfs.c +3 -3
index afe821f9b..83dfa6d52 100644
@@ -2743,7 +2743,7 @@ dnode_get (dnode_end_t * mdn, grub_uint64_t objnum, grub_uint8_t type,
   grub_uint64_t blkid, blksz;	/* the block id this object dnode is in */
   int epbs;			/* shift of number of dnodes in a block */
   int idx;			/* index within a block */
-  void *dnbuf;
+  dnode_phys_t *dnbuf;
   grub_err_t err;
   grub_zfs_endian_t endian;
 
@@ -2773,7 +2773,7 @@ dnode_get (dnode_end_t * mdn, grub_uint64_t objnum, grub_uint8_t type,
 
   grub_dprintf ("zfs", "endian = %d, blkid=%llx\n", mdn->endian,
 		(unsigned long long) blkid);
-  err = dmu_read (mdn, blkid, &dnbuf, &endian, data);
+  err = dmu_read (mdn, blkid, (void **) &dnbuf, &endian, data);
   if (err)
     return err;
   grub_dprintf ("zfs", "alive\n");
@@ -2795,7 +2795,7 @@ dnode_get (dnode_end_t * mdn, grub_uint64_t objnum, grub_uint8_t type,
       data->dnode_endian = endian;
     }
 
-  grub_memmove (&(buf->dn), (dnode_phys_t *) dnbuf + idx, DNODE_SIZE);
+  grub_memmove (&(buf->dn), dnbuf + idx, DNODE_SIZE);
   if (data->dnode_buf == 0)
 	  /* dnbuf not used anymore if data->dnode_mdn malloc failed */
 	  grub_free (dnbuf);
Signature · 7 lines
2.27.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Reviewed-by on v1 2/2
#4 lore raw

Re: [PATCH 2/2] fs/zfs: Avoid pointer downcasting in dnode_get()

Quoted 22 lines · Alec Brown via Grub-devel

Coverity marks multiple issues in grub-core/fs/zfs/zfs.c as either "Untrusted value as argument", "Untrusted pointer read", or "Untrusted loop bound". Each of these issues share a common cause where Coverity finds that data->dnode_buf gets tainted by dnbuf since it is downcasting from (void *) to (dnode_phys_t *) and could imply that the data the pointer points to is tainted. However, the function zio_read(), which reads this data from disk, sanitizes this data by verifying its checksum. To resolve the issues for Coverity, setting dnbuf to (dnode_phys_t *) at the start of the function dnode_get() seems to do the trick.

Fixes CID 896330
Fixes CID 896331
Fixes CID 896334
Fixes CID 896336
Fixes CID 897337
Fixes CID 896340
Fixes CID 314020
Quoted 1 line

Thanks,
Sudhakar

Quoted 43 lines

---
grub-core/fs/zfs/zfs.c | 6 +++---

1 file changed, 3 insertions(+), 3 deletions(-)
grub-core/fs/zfs/zfs.c +3 -3
index afe821f9b..83dfa6d52 100644
@@ -2743,7 +2743,7 @@ dnode_get (dnode_end_t * mdn, grub_uint64_t objnum, grub_uint8_t type,
  grub_uint64_t blkid, blksz; /* the block id this object dnode is in */
  int epbs; /* shift of number of dnodes in a block */
  int idx; /* index within a block */
-  void *dnbuf;
+  dnode_phys_t *dnbuf;
  grub_err_t err;
  grub_zfs_endian_t endian;
 
@@ -2773,7 +2773,7 @@ dnode_get (dnode_end_t * mdn, grub_uint64_t objnum, grub_uint8_t type,
 
  grub_dprintf ("zfs", "endian = %d, blkid=%llx\n", mdn->endian,
 (unsigned long long) blkid);
-  err = dmu_read (mdn, blkid, &dnbuf, &endian, data);
+  err = dmu_read (mdn, blkid, (void **) &dnbuf, &endian, data);
  if (err)
    return err;
  grub_dprintf ("zfs", "alive\n");
@@ -2795,7 +2795,7 @@ dnode_get (dnode_end_t * mdn, grub_uint64_t objnum, grub_uint8_t type,
      data->dnode_endian = endian;
    }
 
-  grub_memmove (&(buf->dn), (dnode_phys_t *) dnbuf + idx, DNODE_SIZE);
+  grub_memmove (&(buf->dn), dnbuf + idx, DNODE_SIZE);
  if (data->dnode_buf == 0)
 /* dnbuf not used anymore if data->dnode_mdn malloc failed */
 grub_free (dnbuf);
Signature · 7 lines
2.27.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#5 lore raw
In reply to Alec Brown via Grub-devel
Quoted 13 lines · Alec Brown

Coverity marked multiple issues in grub-core/fs/zfs/zfs.c and grub-core/mmap/mmap.c. These patches help resolve these issues.

This patch set fixes the following CIDs:
CID 96655
CID 896330
CID 896331
CID 896334
CID 896336
CID 897337
CID 896340
CID 314020

Quoted 5 lines

Alec Brown

Alec Brown (2):
mmap/mmap: Fix resource leak
fs/zfs: Avoid pointer downcasting in dnode_get()

For both patches Reviewed-by: Daniel Kiper <[email protected]>...

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel