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

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