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

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