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

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