nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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