nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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