Skip to contentSkip to conversation

Fix some Coverity low-hanging bugs

PATCH In review Darren Kenny 2 participants 8 messages Oct 26, 2021 – Oct 28, 2021

lore
Share & tools
lore
#0 lore raw

cover Fix some Coverity low-hanging bugs

Coverity has flagged a number of small issues that should be fixed to help in cleaning up the code - these here are primarily memory leaks or uninitialized variables.

In theory leaked memory is significant, but for short-lived processes it is minor.

Similarly for unitinialized variables - some compilers will do the right thing and zero out the value allocated on the stack, but some won't. So it is better to be sure of the content that leave it open for possible misuse.

Darren Kenny (6):
grub-install-common: Fix memory leak in copy_all()
grub-mkrescue: Fix memory leak in write_part()
grub-fstest: Fix resource leaks in cmd_cmp()
grub-mkfont: Fix memory leak in write_font_pf2()
zfs: Fix possible insecure use of chunk size in zap_leaf_array_get()
gzio: Fix possible use of uninitialized variable in huft_build()

 grub-core/fs/zfs/zfs.c     | 3 ++-
 grub-core/io/gzio.c        | 2 +-
 util/grub-fstest.c         | 7 ++++++-
 util/grub-install-common.c | 4 +++-
 util/grub-mkfont.c         | 1 +
 util/grub-mkrescue.c       | 1 +
 6 files changed, 14 insertions(+), 4 deletions(-)
Signature · 1 line
2.27.0
#1 lore raw

PATCH 1/6 grub-install-common: Fix memory leak in copy_all()

The copy_all() function skips a section of code using continue, but fails to free the memory in srcf first, leaking it.

Fixes CID 314026
---
 util/grub-install-common.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
util/grub-install-common.c +3 -1
index 4e212e690c52..0995fa741834 100644
@@ -753,8 +753,10 @@ copy_all (const char *srcd,
 	continue;
       srcf = grub_util_path_concat (2, srcd, de->d_name);
       if (grub_util_is_special_file (srcf)
-	  || grub_util_is_directory (srcf))
+	  || grub_util_is_directory (srcf)) {
+	free(srcf);
 	continue;
+      }  
       dstf = grub_util_path_concat (2, dstd, de->d_name);
       grub_install_compress_file (srcf, dstf, 1);
       free (srcf);
Signature · 1 line
2.27.0
#2 lore raw

PATCH 2/6 grub-mkrescue: Fix memory leak in write_part()

In reply to Darren Kenny

In the function write_part(), the value of 'inname' is not used beyond the grub_util_fopen() call, so is should be freed to avoid leakage.

Fixes CID 314028
---
 util/grub-mkrescue.c | 1 +
 1 file changed, 1 insertion(+)
util/grub-mkrescue.c +1 -0
index fb4dcc6d58f7..d5e4c92fd74d 100644
@@ -229,6 +229,7 @@ write_part (FILE *f, const char *srcdir)
   char *inname = grub_util_path_concat (2, srcdir, "partmap.lst");
   char buf[260];
   in = grub_util_fopen (inname, "rb");
+  free(inname);
   if (!in)
     return;
   while (fgets (buf, 256, in))
Signature · 1 line
2.27.0
#3 lore raw

PATCH 3/6 grub-fstest: Fix resource leaks in cmd_cmp()

In reply to Darren Kenny

In the function cmd_cmp() within the while loop, srcnew and destnew are being allocated but are never freed either before leaving scope or in the recursive calls being made to cmd_cmp().

Fixes CID 314032
Fixes CID 314045
---
 util/grub-fstest.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
util/grub-fstest.c +6 -1
index 838656420098..da0751222f88 100644
@@ -299,10 +299,15 @@ cmd_cmp (char *src, char *dest)
 	  *ptr++ = '/';
 	  strcpy (ptr, entry->d_name);
 
-	  if (grub_util_is_special_file (destnew))
+	  if (grub_util_is_special_file (destnew)) {
+	    free(srcnew);
+	    free(destnew);
 	    continue;
+	  }
 
 	  cmd_cmp (srcnew, destnew);
+	  free(srcnew);
+	  free(destnew);
 	}
       grub_util_fd_closedir (dir);
       return;
Signature · 1 line
2.27.0
#4 lore raw

PATCH 4/6 grub-mkfont: Fix memory leak in write_font_pf2()

In reply to Darren Kenny

In the function write_font_pf2() memory is allocated for 'font_name' to construct a new name, but it is not released before returning from the function, leaking the allocated memory.

Fixes CID 314015
---
 util/grub-mkfont.c | 1 +
 1 file changed, 1 insertion(+)
util/grub-mkfont.c +1 -0
index 0fe45a6103dd..97e8d27e91d6 100644
@@ -928,6 +928,7 @@ write_font_pf2 (struct grub_font_info *font_info, char *output_file)
 			     file, output_file);
     }
 
+  free(font_name);
   fclose (file);
 }
 
Signature · 1 line
2.27.0
#5 lore raw

PATCH 5/6 zfs: Fix possible insecure use of chunk size in zap_leaf_array_get()

In reply to Darren Kenny

In zap_leaf_array_get() the chunk size passed in is considered tainted by Coverity, and is being used before it is tested for validity.

To fix this the assignment of 'la' is moved until after the test of the value of 'chunk'.

Fixes CID 314014
---
 grub-core/fs/zfs/zfs.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
grub-core/fs/zfs/zfs.c +2 -1
index 44e4e18147af..e9d7a7d0e4f6 100644
@@ -2229,7 +2229,7 @@ zap_leaf_array_get (zap_leaf_phys_t * l, grub_zfs_endian_t endian, int blksft,
 
   while (bseen < array_len)
     {
-      struct zap_leaf_array *la = &ZAP_LEAF_CHUNK (l, blksft, chunk)->l_array;
+      struct zap_leaf_array *la;
       grub_size_t toread = array_len - bseen;
 
       if (toread > ZAP_LEAF_ARRAY_BYTES)
@@ -2239,6 +2239,7 @@ zap_leaf_array_get (zap_leaf_phys_t * l, grub_zfs_endian_t endian, int blksft,
 	/* Don't use grub_error because this error is to be ignored.  */
 	return GRUB_ERR_BAD_FS;
 
+      la = &ZAP_LEAF_CHUNK (l, blksft, chunk)->l_array;
       grub_memcpy (buf + bseen,la->la_array,  toread);
       chunk = grub_zfs_to_cpu16 (la->la_next, endian);
       bseen += toread;
Signature · 1 line
2.27.0
#6 lore raw

PATCH 6/6 gzio: Fix possible use of uninitialized variable in huft_build()

In reply to Darren Kenny

In huft_build() it is possible to reach the for loop where 'r' is being assigned to 'q[j]' without 'r.v' ever being initialized.

Fixes CID 314024
---
 grub-core/io/gzio.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
grub-core/io/gzio.c +1 -1
index aea86a0a9a92..10156e569c85 100644
@@ -447,7 +447,7 @@ huft_build (unsigned *b,	/* code lengths in bits (all assumed <= BMAX) */
   int l;			/* bits per table (returned in m) */
   register unsigned *p;		/* pointer into c[], b[], or v[] */
   register struct huft *q;	/* points to current table */
-  struct huft r;		/* table entry for structure assignment */
+  struct huft r = {0};		/* table entry for structure assignment */
   struct huft *u[BMAX];		/* table stack */
   unsigned v[N_MAX];		/* values in order of bit length */
   register int w;		/* bits before this table == (l * h) */
Signature · 1 line
2.27.0
#7 lore raw
In reply to Darren Kenny
Quoted 11 lines · Darren Kenny

Coverity has flagged a number of small issues that should be fixed to help in cleaning up the code - these here are primarily memory leaks or uninitialized variables.

In theory leaked memory is significant, but for short-lived processes it is minor.

Similarly for unitinialized variables - some compilers will do the right thing and zero out the value allocated on the stack, but some won't. So it is better to be sure of the content that leave it open for possible misuse.

Quoted 7 lines

Darren Kenny (6):
grub-install-common: Fix memory leak in copy_all()
grub-mkrescue: Fix memory leak in write_part()
grub-fstest: Fix resource leaks in cmd_cmp()
grub-mkfont: Fix memory leak in write_font_pf2()
zfs: Fix possible insecure use of chunk size in zap_leaf_array_get()
gzio: Fix possible use of uninitialized variable in huft_build()

For all the patches: Reviewed-by: Daniel Kiper <[email protected]>

Thank you for fixing these issues.

Daniel