3d407d2
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
3d407d2
From: Daniel Axtens <dja@axtens.net>
3d407d2
Date: Mon, 20 Sep 2021 01:12:24 +1000
3d407d2
Subject: [PATCH] net/tftp: Prevent a UAF and double-free from a failed seek
3d407d2
3d407d2
A malicious tftp server can cause UAFs and a double free.
3d407d2
3d407d2
An attempt to read from a network file is handled by grub_net_fs_read(). If
3d407d2
the read is at an offset other than the current offset, grub_net_seek_real()
3d407d2
is invoked.
3d407d2
3d407d2
In grub_net_seek_real(), if a backwards seek cannot be satisfied from the
3d407d2
currently received packets, and the underlying transport does not provide
3d407d2
a seek method, then grub_net_seek_real() will close and reopen the network
3d407d2
protocol layer.
3d407d2
3d407d2
For tftp, the ->close() call goes to tftp_close() and frees the tftp_data_t
3d407d2
file->data. The file->data pointer is not nulled out after the free.
3d407d2
3d407d2
If the ->open() call fails, the file->data will not be reallocated and will
3d407d2
continue point to a freed memory block. This could happen from a server
3d407d2
refusing to send the requisite ack to the new tftp request, for example.
3d407d2
3d407d2
The seek and the read will then fail, but the grub_file continues to exist:
3d407d2
the failed seek does not necessarily cause the entire file to be thrown
3d407d2
away (e.g. where the file is checked to see if it is gzipped/lzio/xz/etc.,
3d407d2
a read failure is interpreted as a decompressor passing on the file, not as
3d407d2
an invalidation of the entire grub_file_t structure).
3d407d2
3d407d2
This means subsequent attempts to read or seek the file will use the old
3d407d2
file->data after free. Eventually, the file will be close()d again and
3d407d2
file->data will be freed again.
3d407d2
3d407d2
Mark a net_fs file that doesn't reopen as broken. Do not permit read() or
3d407d2
close() on a broken file (seek is not exposed directly to the file API -
3d407d2
it is only called as part of read, so this blocks seeks as well).
3d407d2
3d407d2
As an additional defence, null out the ->data pointer if tftp_open() fails.
3d407d2
That would have lead to a simple null pointer dereference rather than
3d407d2
a mess of UAFs.
3d407d2
3d407d2
This may affect other protocols, I haven't checked.
3d407d2
3d407d2
Signed-off-by: Daniel Axtens <dja@axtens.net>
3d407d2
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
3d407d2
(cherry picked from commit dada1dda695439bb55b2848dddc2d89843552f81)
3d407d2
---
3d407d2
 grub-core/net/net.c  | 11 +++++++++--
3d407d2
 grub-core/net/tftp.c |  1 +
3d407d2
 include/grub/net.h   |  1 +
3d407d2
 3 files changed, 11 insertions(+), 2 deletions(-)
3d407d2
3d407d2
diff --git a/grub-core/net/net.c b/grub-core/net/net.c
3d407d2
index 55aed92722..1001c611d1 100644
3d407d2
--- a/grub-core/net/net.c
3d407d2
+++ b/grub-core/net/net.c
3d407d2
@@ -1625,7 +1625,8 @@ grub_net_fs_close (grub_file_t file)
3d407d2
       grub_netbuff_free (file->device->net->packs.first->nb);
3d407d2
       grub_net_remove_packet (file->device->net->packs.first);
3d407d2
     }
3d407d2
-  file->device->net->protocol->close (file);
3d407d2
+  if (!file->device->net->broken)
3d407d2
+    file->device->net->protocol->close (file);
3d407d2
   grub_free (file->device->net->name);
3d407d2
   return GRUB_ERR_NONE;
3d407d2
 }
3d407d2
@@ -1847,7 +1848,10 @@ grub_net_seek_real (struct grub_file *file, grub_off_t offset)
3d407d2
     file->device->net->stall = 0;
3d407d2
     err = file->device->net->protocol->open (file, file->device->net->name);
3d407d2
     if (err)
3d407d2
-      return err;
3d407d2
+      {
3d407d2
+	file->device->net->broken = 1;
3d407d2
+	return err;
3d407d2
+      }
3d407d2
     grub_net_fs_read_real (file, NULL, offset);
3d407d2
     return grub_errno;
3d407d2
   }
3d407d2
@@ -1856,6 +1860,9 @@ grub_net_seek_real (struct grub_file *file, grub_off_t offset)
3d407d2
 static grub_ssize_t
3d407d2
 grub_net_fs_read (grub_file_t file, char *buf, grub_size_t len)
3d407d2
 {
3d407d2
+  if (file->device->net->broken)
3d407d2
+    return -1;
3d407d2
+
3d407d2
   if (file->offset != file->device->net->offset)
3d407d2
     {
3d407d2
       grub_err_t err;
3d407d2
diff --git a/grub-core/net/tftp.c b/grub-core/net/tftp.c
3d407d2
index d54b13f09f..788ad1dc44 100644
3d407d2
--- a/grub-core/net/tftp.c
3d407d2
+++ b/grub-core/net/tftp.c
3d407d2
@@ -408,6 +408,7 @@ tftp_open (struct grub_file *file, const char *filename)
3d407d2
     {
3d407d2
       grub_net_udp_close (data->sock);
3d407d2
       grub_free (data);
3d407d2
+      file->data = NULL;
3d407d2
       return grub_errno;
3d407d2
     }
3d407d2
 
3d407d2
diff --git a/include/grub/net.h b/include/grub/net.h
3d407d2
index 42af7de250..9e4898cc6b 100644
3d407d2
--- a/include/grub/net.h
3d407d2
+++ b/include/grub/net.h
3d407d2
@@ -280,6 +280,7 @@ typedef struct grub_net
3d407d2
   grub_fs_t fs;
3d407d2
   int eof;
3d407d2
   int stall;
3d407d2
+  int broken;
3d407d2
 } *grub_net_t;
3d407d2
 
3d407d2
 extern grub_net_t (*EXPORT_VAR (grub_net_open)) (const char *name);