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

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