nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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