nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

Blame SOURCES/0526-net-dns-Fix-double-free-addresses-on-corrupt-DNS-res.patch

0ccc47
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
0ccc47
From: Daniel Axtens <dja@axtens.net>
0ccc47
Date: Thu, 16 Sep 2021 01:29:54 +1000
0ccc47
Subject: [PATCH] net/dns: Fix double-free addresses on corrupt DNS response
0ccc47
0ccc47
grub_net_dns_lookup() takes as inputs a pointer to an array of addresses
0ccc47
("addresses") for the given name, and pointer to a number of addresses
0ccc47
("naddresses"). grub_net_dns_lookup() is responsible for allocating
0ccc47
"addresses", and the caller is responsible for freeing it if
0ccc47
"naddresses" > 0.
0ccc47
0ccc47
The DNS recv_hook will sometimes set and free the addresses array,
0ccc47
for example if the packet is too short:
0ccc47
0ccc47
      if (ptr + 10 >= nb->tail)
0ccc47
	{
0ccc47
	  if (!*data->naddresses)
0ccc47
	    grub_free (*data->addresses);
0ccc47
	  grub_netbuff_free (nb);
0ccc47
	  return GRUB_ERR_NONE;
0ccc47
	}
0ccc47
0ccc47
Later on the nslookup command code unconditionally frees the "addresses"
0ccc47
array. Normally this is fine: the array is either populated with valid
0ccc47
data or is NULL. But in these sorts of error cases it is neither NULL
0ccc47
nor valid and we get a double-free.
0ccc47
0ccc47
Only free "addresses" if "naddresses" > 0.
0ccc47
0ccc47
It looks like the other use of grub_net_dns_lookup() is not affected.
0ccc47
0ccc47
Signed-off-by: Daniel Axtens <dja@axtens.net>
0ccc47
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
0ccc47
(cherry picked from commit eb2e69fcf51307757e43f55ee8c9354d1ee42dd1)
0ccc47
(cherry picked from commit d801a27e7acec6c1a83067fab0bb975877eaf704)
0ccc47
(cherry picked from commit 4d8b6e36ddfda4084e370b3b08c432e8a462e9be)
0ccc47
---
0ccc47
 grub-core/net/dns.c | 6 ++++--
0ccc47
 1 file changed, 4 insertions(+), 2 deletions(-)
0ccc47
0ccc47
diff --git a/grub-core/net/dns.c b/grub-core/net/dns.c
0ccc47
index 906ec7d678..135faac035 100644
0ccc47
--- a/grub-core/net/dns.c
0ccc47
+++ b/grub-core/net/dns.c
0ccc47
@@ -667,9 +667,11 @@ grub_cmd_nslookup (struct grub_command *cmd __attribute__ ((unused)),
0ccc47
       grub_net_addr_to_str (&addresses[i], buf);
0ccc47
       grub_printf ("%s\n", buf);
0ccc47
     }
0ccc47
-  grub_free (addresses);
0ccc47
   if (naddresses)
0ccc47
-    return GRUB_ERR_NONE;
0ccc47
+    {
0ccc47
+      grub_free (addresses);
0ccc47
+      return GRUB_ERR_NONE;
0ccc47
+    }
0ccc47
   return grub_error (GRUB_ERR_NET_NO_DOMAIN, N_("no DNS record found"));
0ccc47
 }
0ccc47