nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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