Blame SOURCES/0235-net-tftp-Prevent-a-UAF-and-double-free-from-a-failed.patch

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