Skip to contentSkip to conversation

UEFI: Fix several memory leaks of UEFI handles

PATCH RESEND Superseded Khalid Ali 2 participants 3 messages May 29, 2025 – Jun 6, 2025

lore
Share & tools
lore

Khalid Ali

author
#0 lore raw

PATCH RESEND 1/1 UEFI: Fix several memory leaks of UEFI handles

Fix possible and absolute memory leaks of "handles" returned by grub_efi_locate_handle() using grub_malloc().

---
 grub-core/commands/efi/lsefi.c | 1 +
 grub-core/commands/efi/tpm.c   | 2 ++
 grub-core/term/efi/serial.c    | 4 ++--
 grub-core/video/efi_gop.c      | 2 +-
 4 files changed, 6 insertions(+), 3 deletions(-)
grub-core/commands/efi/lsefi.c +1 -0
index 7b8316d41..f4c10392d 100644
@@ -129,6 +129,7 @@ grub_cmd_lsefi (grub_command_t cmd __attribute__ ((unused)),
 
     }
 
+  grub_free (handles);
   return 0;
 }
 
grub-core/commands/efi/tpm.c +2 -0
index cbac69866..cb39b7bba 100644
@@ -112,6 +112,7 @@ grub_tpm_handle_find (grub_efi_handle_t *tpm_handle,
       grub_tpm_version = 1;
       *protocol_version = 1;
       grub_dprintf ("tpm", "TPM handle Found, version: 1\n");
+      grub_free (handles);
       return 1;
     }
 
@@ -124,6 +125,7 @@ grub_tpm_handle_find (grub_efi_handle_t *tpm_handle,
       grub_tpm_version = 2;
       *protocol_version = 2;
       grub_dprintf ("tpm", "TPM handle Found, version: 2\n");
+      grub_free (handles);
       return 1;
     }
 
grub-core/term/efi/serial.c +2 -2
index 5dfd2d86c..e409b8d5e 100644
@@ -169,13 +169,13 @@ grub_efiserial_init (void)
 
       port = grub_zalloc (sizeof (*port));
       if (!port)
-	return;
+	break;
 
       port->name = grub_malloc (sizeof ("efiXXXXXXXXXXXXXXXXXXXX"));
       if (!port->name)
 	{
 	  grub_free (port);
-	  return;
+	  break;
 	}
       grub_snprintf (port->name, sizeof ("efiXXXXXXXXXXXXXXXXXXXX"),
 		     "efi%d", num_serial++);
grub-core/video/efi_gop.c +1 -1
index 9452f5e58..1ad2e709d 100644
@@ -94,7 +94,7 @@ check_protocol (void)
   gop_handle = 0;
 
   grub_dprintf ("video", "GOP: no usable mode\n");
-
+  grub_free (handles);
   return 0;
 }
 
Signature · 7 lines
2.49.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#1 lore raw
Quoted 51 lines · Khalid Ali

Fix possible and absolute memory leaks of "handles" returned by grub_efi_locate_handle() using grub_malloc().

---
 grub-core/commands/efi/lsefi.c | 1 +
 grub-core/commands/efi/tpm.c   | 2 ++
 grub-core/term/efi/serial.c    | 4 ++--
 grub-core/video/efi_gop.c      | 2 +-
 4 files changed, 6 insertions(+), 3 deletions(-)
grub-core/commands/efi/lsefi.c +1 -0
index 7b8316d41..f4c10392d 100644
@@ -129,6 +129,7 @@ grub_cmd_lsefi (grub_command_t cmd __attribute__ 
 ((unused)),
 
     }
 
+  grub_free (handles);
   return 0;
 }
grub-core/commands/efi/tpm.c +2 -0
index cbac69866..cb39b7bba 100644
@@ -112,6 +112,7 @@ grub_tpm_handle_find (grub_efi_handle_t 
 *tpm_handle,
       grub_tpm_version = 1;
       *protocol_version = 1;
       grub_dprintf ("tpm", "TPM handle Found, version: 1\n");
+      grub_free (handles);
       return 1;
     }
@@ -124,6 +125,7 @@ grub_tpm_handle_find (grub_efi_handle_t 
 *tpm_handle,
       grub_tpm_version = 2;
       *protocol_version = 2;
       grub_dprintf ("tpm", "TPM handle Found, version: 2\n");
+      grub_free (handles);
       return 1;
     }
grub-core/term/efi/serial.c +0 -0
index 5dfd2d86c..e409b8d5e 100644

grub-core/term/efi/serial.c grub_efiserial_init (void) · line 171

Quoted 3 lines
grub-core/term/efi/serial.c +0 -0
@@ -169,13 +169,13 @@ grub_efiserial_init (void)
 
       port = grub_zalloc (sizeof (*port));
       if (!port)

Hi Khalid Ali,
For pointers, you have to use like port == NULL instead !port

grub-core/term/efi/serial.c grub_efiserial_init (void) · line 175

Quoted 5 lines
grub-core/term/efi/serial.c +1 -1
-	return;
+	break;
 
       port->name = grub_malloc (sizeof ("efiXXXXXXXXXXXXXXXXXXXX"));
       if (!port->name)

same above

port->name == NULL

Quoted 12 lines
grub-core/term/efi/serial.c +1 -1
 	{
 	  grub_free (port);
-	  return;
+	  break;
 	}
       grub_snprintf (port->name, sizeof ("efiXXXXXXXXXXXXXXXXXXXX"),
 		     "efi%d", num_serial++);
grub-core/video/efi_gop.c +0 -0
index 9452f5e58..1ad2e709d 100644

grub-core/video/efi_gop.c check_protocol (void) · line 99

Quoted 7 lines
grub-core/video/efi_gop.c +1 -1
@@ -94,7 +94,7 @@ check_protocol (void)
   gop_handle = 0;
 
   grub_dprintf ("video", "GOP: no usable mode\n");
-
+  grub_free (handles);
   return 0;
 }

Thanks,
Sudhakar Kuppusamy

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

author
#2 lore raw
Quoted 2 lines

Hi Khalid Ali,
For pointers, you have to use like port == NULL instead !port

Hi Sudhakar Kuppusamy,

Well i checked on entire source tree to look for some consistency pattern, and basically both are valid and used. I think it is matter of code author preference. Beside i didn't write the code, i was just fixing memory leaks. The code was already existed.

Regards, Khalid

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