Skip to contentSkip to conversation

Fix resource leaks in gnulib

PATCH In review Alec Brown via Grub-devel 2 participants 5 messages Jun 10, 2025 – Jun 14, 2025

lore
Share & tools
lore
#0 lore raw

cover Fix resource leaks in gnulib

Coverity found a couple resource leaks in gnulib code that the GRUB is using. These fixes have been made in the latest version of gnulib and I've backported these changes to maintain consistency.

This patch set fixes the following CIDs:
CID: 473869
CID: 473887
CID: 473888

Alec Brown (2):
gnulib/regcomp: Fix resource leak
gnulib/regexec: Fix resource leak

 bootstrap.conf                                               |   7 ++-
 conf/Makefile.extra-dist                                     |   2 +
 grub-core/lib/gnulib-patches/fix-regcomp-resource-leak.patch | 110 ++++++++++++++++++++++++++++++++++++++++++++++++
 grub-core/lib/gnulib-patches/fix-regexec-resource-leak.patch |  11 +++++
 4 files changed, 129 insertions(+), 1 deletion(-)

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

#1 lore raw

PATCH 1/2 gnulib/regcomp: Fix resource leak

In the functions create_initial_state() and calc_eclosure_iter(), memory is allocated for the elems member of a re_node_set structure but that memory isn't freed on error. Before returning an error, a call to re_node_set_free() should be made to prevent the resource leak.

This issue has been fixed in the latest version of gnulib and I've backported this change to maintain consistency.

This issue was found by a Coverity Scan of GRUB2 under the following CIDs:
CID: 473869
CID: 473888

---
 bootstrap.conf                                |   5 +-
 conf/Makefile.extra-dist                      |   1 +
 .../fix-regcomp-resource-leak.patch           | 110 ++++++++++++++++++
 3 files changed, 115 insertions(+), 1 deletion(-)
 create mode 100644 grub-core/lib/gnulib-patches/fix-regcomp-resource-leak.patch
bootstrap.conf +4 -1
index 7a7813d28..7a464a289 100644
@@ -84,7 +84,10 @@ bootstrap_post_import_hook () {
   # Instead of patching our gnulib and therefore maintaining a fork, submit
   # changes to gnulib and update the hash above when they've merged.  Do not
   # add new patches here.
-  patch -d grub-core/lib/gnulib -p2 < grub-core/lib/gnulib-patches/fix-width.patch
+  for patchname in fix-width fix-regcomp-resource-leak; do
+    patch -d grub-core/lib/gnulib -p2 \
+       < "grub-core/lib/gnulib-patches/$patchname.patch"
+  done
 
   for patchname in \
       0001-Support-POTFILES-shell \
conf/Makefile.extra-dist +1 -0
index d9e2b8cc7..5bf3da429 100644
@@ -30,6 +30,7 @@ EXTRA_DIST += grub-core/genemuinit.sh
 EXTRA_DIST += grub-core/genemuinitheader.sh
 
 EXTRA_DIST += grub-core/lib/gnulib-patches/fix-width.patch
+EXTRA_DIST += grub-core/lib/gnulib-patches/fix-regcomp-resource-leak.patch
 
 EXTRA_DIST += grub-core/lib/libgcrypt
 EXTRA_DIST += grub-core/lib/libgcrypt-grub/mpi/generic
grub-core/lib/gnulib-patches/fix-regcomp-resource-leak.patch new file +110 -0

110 lines not on this page · raw message

Signature · 7 lines
2.27.0


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

PATCH 2/2 gnulib/regexec: Fix resource leak

In reply to Alec Brown via Grub-devel

In the function merge_state_with_log(), memory is allocated for the variable next_nodes when creating a union of the variables table_nodes and log_nodes. However, if next_state->entrance_nodes is NULL, then table_nodes becomes NULL and we still allocate memory to copy the content of log_nodes. This can cause a resource leak since we only free the memory for next_nodes if table_nodes isn't NULL. To prevent this, we need to check that next_state->entrance_nodes isn't NULL before allocating memory for the union.

This issue has been fixed in the latest version of gnulib and I've backported this change to maintain consistency.

This issue was found by a Coverity Scan of GRUB2 under the following CID:
CID: 473887

---
 bootstrap.conf                                        |  4 +++-
 conf/Makefile.extra-dist                              |  1 +
 .../gnulib-patches/fix-regexec-resource-leak.patch    | 11 +++++++++++
 3 files changed, 15 insertions(+), 1 deletion(-)
 create mode 100644 grub-core/lib/gnulib-patches/fix-regexec-resource-leak.patch
bootstrap.conf +3 -1
index 7a464a289..7cd375ba9 100644
@@ -84,7 +84,9 @@ bootstrap_post_import_hook () {
   # Instead of patching our gnulib and therefore maintaining a fork, submit
   # changes to gnulib and update the hash above when they've merged.  Do not
   # add new patches here.
-  for patchname in fix-width fix-regcomp-resource-leak; do
+  for patchname in fix-width \
+      fix-regcomp-resource-leak \
+      fix-regexec-resource-leak; do
     patch -d grub-core/lib/gnulib -p2 \
        < "grub-core/lib/gnulib-patches/$patchname.patch"
   done
conf/Makefile.extra-dist +1 -0
index 5bf3da429..07eee1956 100644
@@ -31,6 +31,7 @@ EXTRA_DIST += grub-core/genemuinitheader.sh
 
 EXTRA_DIST += grub-core/lib/gnulib-patches/fix-width.patch
 EXTRA_DIST += grub-core/lib/gnulib-patches/fix-regcomp-resource-leak.patch
+EXTRA_DIST += grub-core/lib/gnulib-patches/fix-regexec-resource-leak.patch
 
 EXTRA_DIST += grub-core/lib/libgcrypt
 EXTRA_DIST += grub-core/lib/libgcrypt-grub/mpi/generic
grub-core/lib/gnulib-patches/fix-regexec-resource-leak.patch new file +11 -0
new file mode 100644
index 000000000..f490e05fb
@@ -0,0 +1,11 @@
+--- a/lib/regexec.c
++++ b/lib/regexec.c
+@@ -2270,7 +2270,7 @@ merge_state_with_log (reg_errcode_t *err, re_match_context_t *mctx,
+ 	 these destinations and the results of the transition table.  */
+       pstate = mctx->state_log[cur_idx];
+       log_nodes = pstate->entrance_nodes;
+-      if (next_state != NULL)
++      if (next_state != NULL && next_state->entrance_nodes != NULL)
+ 	{
+ 	  table_nodes = next_state->entrance_nodes;
+ 	  *err = re_node_set_init_union (&next_nodes, table_nodes,
Signature · 7 lines
2.27.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#3 lore raw
In reply to Alec Brown via Grub-devel
Quoted 4 lines · Alec Brown

Coverity found a couple resource leaks in gnulib code that the GRUB is using. These fixes have been made in the latest version of gnulib and I've backported these changes to maintain consistency.

Quoted 8 lines

This patch set fixes the following CIDs:
CID: 473869
CID: 473887
CID: 473888

Alec Brown (2):
gnulib/regcomp: Fix resource leak
gnulib/regexec: Fix resource leak

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

Daniel

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

#4 lore raw
In reply to Alec Brown via Grub-devel

Hi Alec,

Quoted 3 lines · Alec Brown via Grub-devel

Coverity found a couple resource leaks in gnulib code that the GRUB is using. These fixes have been made in the latest version of gnulib and I've backported these changes to maintain consistency.

Thanks for fixing these in Gnulib.

There was a left shift overflow copied from Binutils that was fixed there years ago. I sent a patch a while ago that got lost in the noise [1]. Figured you may want to review it since it is simple.

Collin

[1] https://lists.nongnu.org/archive/html/grub-devel/2025-05/msg00027.html

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