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

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