Skip to contentSkip to conversation

kern: perform NULL check in unregister paths (command/extcmd)

PATCH In review Srish Srinivasan 3 participants 3 messages Sep 9, 2025

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 kern: perform NULL check in unregister paths (command/extcmd)

Many modules call grub_unregister_{command(), extcmd()} from GRUB_MOD_FINI() without checking for a failure in registration. This could lead to a NULL pointer dereference in unregistration.

Perform explicit NULL check in both the unregister helpers.

---
 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 · 7 lines
2.51.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Reviewed-by on v2 1/1
#1 lore raw
Quoted 11 lines · Srish Srinivasan

Many modules call grub_unregister_{command(), extcmd()} from GRUB_MOD_FINI() without checking for a failure in registration. This could lead to a NULL pointer dereference in unregistration.

Perform explicit NULL check in both the unregister helpers.

Quoted 36 lines

---
grub-core/commands/extcmd.c | 3 +++
grub-core/kern/command.c | 3 +++

2 files changed, 6 insertions(+)
grub-core/commands/extcmd.c +0 -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)
{

grub-core/commands/extcmd.c +3 -0
+  if (ext == NULL)
+    return;
+
  grub_unregister_command (ext->cmd);
  grub_free (ext);

}

grub-core/kern/command.c +0 -0
index 5812e131c..a1b4c81a9 100644
@@ -104,6 +104,9 @@ grub_register_command_lockdown (const char *name,

void
grub_unregister_command (grub_command_t cmd)
{

grub-core/kern/command.c +3 -0
+  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 · 1 line
2.51.0

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

Reviewed-by
#2 lore raw
Quoted 8 lines · Sudhakar Kuppusamy
Quoted 8 lines · Srish Srinivasan

Many modules call grub_unregister_{command(), extcmd()} from GRUB_MOD_FINI() without checking for a failure in registration. This could lead to a NULL pointer dereference in unregistration.

Quoted 5 lines
Quoted 3 lines

Perform explicit NULL check in both the unregister helpers.

Quoted 29 lines
Quoted 29 lines

---
grub-core/commands/extcmd.c | 3 +++
grub-core/kern/command.c | 3 +++

2 files changed, 6 insertions(+)
grub-core/commands/extcmd.c +0 -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)
{

grub-core/commands/extcmd.c +3 -0
+  if (ext == NULL)
+    return;
+
   grub_unregister_command (ext->cmd);
   grub_free (ext);

}

grub-core/kern/command.c +0 -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)

Quoted 9 lines
Quoted 8 lines
grub-core/kern/command.c +2 -0
+    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 · 1 line
2.51.0

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