Skip to contentSkip to conversation

net/tftp: Fix NULL pointer dereference in grub_net_udp_close()

PATCH In review Lidong Chen via Grub-devel 3 participants 4 messages Oct 18, 2025 – Oct 24, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net/tftp: Fix NULL pointer dereference in grub_net_udp_close()

A NULL pointer dereference can occur in grub_net_udp_close(data->sock) when handling a malformed TFTP OACK packet.

This issue was discovered via fuzzing. When a malformed OACK packet contains an invalid file size ("tsize") value, tftp_receive() detects the error and saves it via grub_error_save (&data->save_err). Later, tftp_open() restores this error and calls grub_net_udp_close(data->sock), assuming the socket is still valid.

However, the socket may have already been closed and set to NULL after processing the final data block in tftp_receive(), leading to a NULL pointer dereferencing when attempting to close it again.

Fix by checking if the socket is non-NULL before closing.

---
 grub-core/net/tftp.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
grub-core/net/tftp.c +5 -1
index 336b78691..63953bc19 100644
@@ -412,7 +412,11 @@ tftp_open (struct grub_file *file, const char *filename)
     grub_error_load (&data->save_err);
   if (grub_errno)
     {
-      grub_net_udp_close (data->sock);
+      if (data->sock != NULL)
+	{
+	  grub_net_udp_close (data->sock);
+	  data->sock = NULL;
+	}
       grub_free (data);
       file->data = NULL;
       return grub_errno;
Signature · 7 lines
2.43.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
author Reviewed-by Daniel Kiper on v1 1/1
#1 lore raw
Quoted 9 lines · Lidong Chen

A NULL pointer dereference can occur in grub_net_udp_close(data->sock) when handling a malformed TFTP OACK packet.

This issue was discovered via fuzzing. When a malformed OACK packet contains an invalid file size ("tsize") value, tftp_receive() detects the error and saves it via grub_error_save (&data->save_err). Later, tftp_open() restores this error and calls grub_net_udp_close(data->sock), assuming the socket is still valid.

Quoted 7 lines

However, the socket may have already been closed and set to NULL after processing the final data block in tftp_receive(), leading to a NULL pointer dereferencing when attempting to close it again.

Fix by checking if the socket is non-NULL before closing.

Daniel

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

Reviewed-by on v1 1/1
#2 lore raw
In reply to Lidong Chen via Grub-devel
Quoted 13 lines · Lidong Chen via Grub-devel

A NULL pointer dereference can occur in grub_net_udp_close(data->sock) when handling a malformed TFTP OACK packet.

This issue was discovered via fuzzing. When a malformed OACK packet contains an invalid file size ("tsize") value, tftp_receive() detects the error and saves it via grub_error_save (&data->save_err). Later, tftp_open() restores this error and calls grub_net_udp_close(data->sock), assuming the socket is still valid.

Quoted 7 lines

However, the socket may have already been closed and set to NULL after processing the final data block in tftp_receive(), leading to a NULL pointer dereferencing when attempting to close it again.

Fix by checking if the socket is non-NULL before closing.

Nit: below one

Quoted 9 lines

---
grub-core/net/tftp.c | 6 +++++-

1 file changed, 5 insertions(+), 1 deletion(-)
grub-core/net/tftp.c +0 -0
index 336b78691..63953bc19 100644

grub-core/net/tftp.c tftp_open (struct grub_file *file, const char *filename) · line 419

Quoted 9 lines
grub-core/net/tftp.c +5 -1
@@ -412,7 +412,11 @@ tftp_open (struct grub_file *file, const char *filename)
    grub_error_load (&data->save_err);
  if (grub_errno)
    {
-      grub_net_udp_close (data->sock);
+      if (data->sock != NULL)
+       {
+         grub_net_udp_close (data->sock);
+         data->sock = NULL;
+       }

Correct the indentation like

if (data->sock != NULL)

  {
    grub_net_udp_close (data->sock);
    data->sock = NULL;

}

Thanks
Sudhakar

Quoted 11 lines
grub-core/net/tftp.c +0 -0
      grub_free (data);
      file->data = NULL;
      return grub_errno;
Signature · 7 lines
2.43.0


_______________________________________________
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

#3 lore raw
Quoted 29 lines · Sudhakar Kuppusamy
Quoted 13 lines · Lidong Chen via Grub-devel

A NULL pointer dereference can occur in grub_net_udp_close(data->sock) when handling a malformed TFTP OACK packet.

This issue was discovered via fuzzing. When a malformed OACK packet contains an invalid file size ("tsize") value, tftp_receive() detects the error and saves it via grub_error_save (&data->save_err). Later, tftp_open() restores this error and calls grub_net_udp_close(data->sock), assuming the socket is still valid.

Quoted 7 lines

However, the socket may have already been closed and set to NULL after processing the final data block in tftp_receive(), leading to a NULL pointer dereferencing when attempting to close it again.

Fix by checking if the socket is non-NULL before closing.

Nit: below one

Quoted 4 lines

---
grub-core/net/tftp.c | 6 +++++-

1 file changed, 5 insertions(+), 1 deletion(-)
Quoted 16 lines
Quoted 5 lines
grub-core/net/tftp.c +0 -0
index 336b78691..63953bc19 100644

grub-core/net/tftp.c tftp_open (struct grub_file *file, const char *filename) · line 419

Quoted 9 lines
grub-core/net/tftp.c +5 -1
@@ -412,7 +412,11 @@ tftp_open (struct grub_file *file, const char *filename)
    grub_error_load (&data->save_err);
  if (grub_errno)
    {
-      grub_net_udp_close (data->sock);
+      if (data->sock != NULL)
+       {
+         grub_net_udp_close (data->sock);
+         data->sock = NULL;
+       }

Correct the indentation like

The indentions, with tabs, are correct but can be rendered incorrectly in your email reader...

Anyway, thank you for review...

Daniel

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