Skip to contentSkip to conversation

command/extcmd: perform explicit NULL check in both the unregister helpers

PATCH In review Srish Srinivasan 3 participants 5 messages Sep 22, 2025 – Oct 1, 2025

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 command/extcmd: perform explicit NULL check in both the unregister helpers

During command registration, grub_register_command_prio returns a 0 when there is a failure in memory allocation. In such a situation, calls to grub_unregister_{command(), extcmd()} during command unregistration will result in dereferencing a NULL pointer.

Perform explicit NULL check in both the unregister helpers to prevent undefined behaviour due to a NULL pointer dereference.

---
 grub-core/commands/extcmd.c | 3 +++
 grub-core/kern/command.c    | 3 +++
 2 files changed, 6 insertions(+)
grub-core/commands/extcmd.c +3 -0
index c236be13a..7f8cb3f67 100644
@@ -139,6 +139,9 @@ grub_register_extcmd_lockdown (const char *name, grub_extcmd_func_t func,
 void
 grub_unregister_extcmd (grub_extcmd_t ext)
 {
+  if (ext == NULL)
+    return;
+
   grub_unregister_command (ext->cmd);
   grub_free (ext);
 }
grub-core/kern/command.c +3 -0
index 5812e131c..a1b4c81a9 100644
@@ -104,6 +104,9 @@ grub_register_command_lockdown (const char *name,
 void
 grub_unregister_command (grub_command_t cmd)
 {
+  if (cmd == NULL)
+    return;
+
   if ((cmd->prio & GRUB_COMMAND_FLAG_ACTIVE) && (cmd->next))
     cmd->next->prio |= GRUB_COMMAND_FLAG_ACTIVE;
   grub_list_remove (GRUB_AS_LIST (cmd));
Signature · 6 lines
2.51.0

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

Looks good to me, thank you.

Quoted 49 lines · Srish Srinivasan

During command registration, grub_register_command_prio returns a 0 when there is a failure in memory allocation. In such a situation, calls to grub_unregister_{command(), extcmd()} during command unregistration will result in dereferencing a NULL pointer.

Perform explicit NULL check in both the unregister helpers to prevent undefined behaviour due to a NULL pointer dereference.

---
 grub-core/commands/extcmd.c | 3 +++
 grub-core/kern/command.c    | 3 +++
 2 files changed, 6 insertions(+)
grub-core/commands/extcmd.c +3 -0
index c236be13a..7f8cb3f67 100644
@@ -139,6 +139,9 @@ grub_register_extcmd_lockdown (const char *name, grub_extcmd_func_t func,
 void
 grub_unregister_extcmd (grub_extcmd_t ext)
 {
+  if (ext == NULL)
+    return;
+
   grub_unregister_command (ext->cmd);
   grub_free (ext);
 }
grub-core/kern/command.c +3 -1
index 5812e131c..a1b4c81a9 100644
@@ -104,6 +104,9 @@ grub_register_command_lockdown (const char *name,
 void
 grub_unregister_command (grub_command_t cmd)
 {
+  if (cmd == NULL)
+    return;
+
   if ((cmd->prio & GRUB_COMMAND_FLAG_ACTIVE) && (cmd->next))
     cmd->next->prio |= GRUB_COMMAND_FLAG_ACTIVE;
   grub_list_remove (GRUB_AS_LIST (cmd));
--

2.51.0

Quoted 4 lines

_______________________________________________
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

#2 lore raw
In reply to Srish Srinivasan
Quoted 6 lines · Srish Srinivasan

During command registration, grub_register_command_prio returns a 0 when there is a failure in memory allocation. In such a situation, calls to grub_unregister_{command(), extcmd()} during command unregistration will result in dereferencing a NULL pointer.

Quoted 6 lines

Perform explicit NULL check in both the unregister helpers to prevent undefined behaviour due to a NULL pointer dereference.

Patch LGTM to me but it does not seem to be posted earlier to the grub-devel. So, I cannot accept Sudhakar's and Stefan's RBs until they are confirmed here.

Though Reviewed-by: Daniel Kiper <[email protected]>...

Daniel

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

#3 lore raw
Quoted 13 lines · Daniel Kiper
Quoted 6 lines · Srish Srinivasan

During command registration, grub_register_command_prio returns a 0 when there is a failure in memory allocation. In such a situation, calls to grub_unregister_{command(), extcmd()} during command unregistration will result in dereferencing a NULL pointer.

Quoted 6 lines

Perform explicit NULL check in both the unregister helpers to prevent undefined behaviour due to a NULL pointer dereference.

Quoted 7 lines

Patch LGTM to me but it does not seem to be posted earlier to the grub-devel. So, I cannot accept Sudhakar's and Stefan's RBs until they are confirmed here.

Though Reviewed-by: Daniel Kiper <[email protected]>...

Daniel

Hi Daniel,
thank you for the reviewed-by.

And, here are the links to v2 for the same patch where I got reviews from Sudhakar and Stefan.

 Sudhakar's reviewed-by: https://lists.gnu.org/archive/html/grub-devel/2025-09/msg00087.html
 Stefan's reviewed-by: https://lists.gnu.org/archive/html/grub-devel/2025-09/msg00113.html

And, here is the link to v1 for the same patch just for your reference.

 https://lists.gnu.org/archive/html/grub-devel/2025-09/msg00072.html

Hope this helps.
Thank You.

Quoted 5 lines

_______________________________________________
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

#4 lore raw
Quoted 32 lines · Srish Srinivasan
Quoted 13 lines · Daniel Kiper
Quoted 6 lines · Srish Srinivasan

During command registration, grub_register_command_prio returns a 0 when there is a failure in memory allocation. In such a situation, calls to grub_unregister_{command(), extcmd()} during command unregistration will result in dereferencing a NULL pointer.

Quoted 6 lines

Perform explicit NULL check in both the unregister helpers to prevent undefined behaviour due to a NULL pointer dereference.

Quoted 7 lines

Patch LGTM to me but it does not seem to be posted earlier to the grub-devel. So, I cannot accept Sudhakar's and Stefan's RBs until they are confirmed here.

Though Reviewed-by: Daniel Kiper <[email protected]>...

Daniel

Hi Daniel,
thank you for the reviewed-by.

And, here are the links to v2 for the same patch where I got reviews from Sudhakar and Stefan.

 Sudhakar's reviewed-by:
https://lists.gnu.org/archive/html/grub-devel/2025-09/msg00087.html
 Stefan's reviewed-by:
https://lists.gnu.org/archive/html/grub-devel/2025-09/msg00113.html

Quoted 5 lines

And, here is the link to v1 for the same patch just for your reference.

 https://lists.gnu.org/archive/html/grub-devel/2025-09/msg00072.html

Hope this helps.

Sorry, I missed both emails due to subject change. I will get Sudhakar's and Stefan's RBs then.

Daniel

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