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

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