nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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