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