Skip to contentSkip to conversation

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

PATCH Superseded Srish Srinivasan 2 participants 3 messages Sep 8, 2025 – Sep 9, 2025

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 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 | 2 ++
 grub-core/kern/command.c    | 2 ++
 2 files changed, 4 insertions(+)
grub-core/commands/extcmd.c +2 -0
index c236be13a..f142305ed 100644
@@ -139,6 +139,8 @@ grub_register_extcmd_lockdown (const char *name, grub_extcmd_func_t func,
 void
 grub_unregister_extcmd (grub_extcmd_t ext)
 {
+  if (!ext)
+    return;
   grub_unregister_command (ext->cmd);
   grub_free (ext);
 }
grub-core/kern/command.c +2 -0
index 5812e131c..3ab93ce20 100644
@@ -104,6 +104,8 @@ grub_register_command_lockdown (const char *name,
 void
 grub_unregister_command (grub_command_t cmd)
 {
+  if (!cmd)
+    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.48.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#1 lore raw
Quoted 21 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.

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

2 files changed, 4 insertions(+)
grub-core/commands/extcmd.c +0 -0
index c236be13a..f142305ed 100644
@@ -139,6 +139,8 @@ grub_register_extcmd_lockdown (const char *name, grub_extcmd_func_t func,
Quoted 4 lines

void
grub_unregister_extcmd (grub_extcmd_t ext)
{
+ if (!ext)

For pointer, use ext == NULL

Quoted 1 line

+ return;

It would be nice if add empty line here

Quoted 8 lines

grub_unregister_command (ext->cmd);
grub_free (ext);
}

grub-core/kern/command.c +0 -0
index 5812e131c..3ab93ce20 100644
@@ -104,6 +104,8 @@ grub_register_command_lockdown (const char *name,
Quoted 4 lines

void
grub_unregister_command (grub_command_t cmd)
{
+ if (!cmd)

For pointer, use cmd == NULL

Quoted 1 line

+ return;

It would be nice if add empty line here

Thanks,
Sudhakar

Quoted 6 lines

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.48.1

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

#2 lore raw
Quoted 40 lines · Sudhakar Kuppusamy
Quoted 20 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.

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

2 files changed, 4 insertions(+)
grub-core/commands/extcmd.c +0 -0
index c236be13a..f142305ed 100644
@@ -139,6 +139,8 @@ grub_register_extcmd_lockdown (const char *name, grub_extcmd_func_t func,
Quoted 4 lines

void
grub_unregister_extcmd (grub_extcmd_t ext)
{
+ if (!ext)

For pointer, use ext == NULL

Quoted 1 line

+ return;

It would be nice if add empty line here

Quoted 8 lines
   grub_unregister_command (ext->cmd);
   grub_free (ext);
}
grub-core/kern/command.c +0 -0
index 5812e131c..3ab93ce20 100644
@@ -104,6 +104,8 @@ grub_register_command_lockdown (const char *name,
Quoted 4 lines

void
grub_unregister_command (grub_command_t cmd)
{
+ if (!cmd)

For pointer, use cmd == NULL

Quoted 5 lines
Quoted 1 line

+ return;

It would be nice if add empty line here

Thanks,
Sudhakar

Hi Sudhakar,
Thanks for the review.

Will send out v2 with the fixes.

Quoted 11 lines
Quoted 6 lines
   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.48.1

_______________________________________________
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