Received: from list by lists.gnu.org with archive (Exim 4.71) id 1aTcYI-0001Ar-UB for mharc-grub-devel@gnu.org; Wed, 10 Feb 2016 16:43:58 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:52604) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aTcYG-0001AA-D5 for grub-devel@gnu.org; Wed, 10 Feb 2016 16:43:57 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aTcYC-0003uC-Br for grub-devel@gnu.org; Wed, 10 Feb 2016 16:43:56 -0500 Received: from 66-220-144-178.intmgw.facebook.com ([66.220.144.178]:27138 helo=mx-out.facebook.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aTcYC-0003tu-5U for grub-devel@gnu.org; Wed, 10 Feb 2016 16:43:52 -0500 Received: from facebook.com (2401:db00:2040:5012:face:0:9:0) by mx-out.facebook.com (10.102.107.97) with ESMTP id 0d6adc44d03c11e5a74c0002c99331b0-5be9270 for ; Wed, 10 Feb 2016 13:20:04 -0800 Received: by devbig041.ash4.facebook.com (Postfix, from userid 8730) id D19934540BEF; Wed, 10 Feb 2016 13:21:10 -0800 (PST) From: Josef Bacik To: grub-devel@gnu.org, kernel-team@fb.com Subject: [PATCH 11/14] dns: reset data->naddresses for every packet we receive Date: Wed, 10 Feb 2016 13:21:05 -0800 Message-Id: <1455139268-3241273-12-git-send-email-jbacik@fb.com> X-Mailer: git-send-email 1.8.1 In-Reply-To: <1455139268-3241273-1-git-send-email-jbacik@fb.com> References: <1455139268-3241273-1-git-send-email-jbacik@fb.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [generic] [fuzzy] X-Received-From: 66.220.144.178 Cc: Josef Bacik X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list Reply-To: The development of GNU GRUB List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 10 Feb 2016 21:43:57 -0000 I noticed when debugging a problem that we'd corrupt memory if our dns server didn't respond fast enough and we ended up asking for both an AAAA and A record for a server. The problem is we alloc data->addresses based on the number of addresses in the packet, but we populate it based on data->naddresses. So we get the AAAA record with one address, and we add that, then we get the A record with one address and now data->naddresses == 1 but the ancount is 1, so we allocate data->addresses to hold one address but write the new address outside the array. We also leak the old addresses memory. So fix this by noticing if we already have an address and free the old memory and reset naddresses so we don't overflow our new array. Signed-off-by: Josef Bacik --- grub-core/net/dns.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/grub-core/net/dns.c b/grub-core/net/dns.c index 86e609b..7a6c4b4 100644 --- a/grub-core/net/dns.c +++ b/grub-core/net/dns.c @@ -276,6 +276,9 @@ recv_hook (grub_net_udp_socket_t sock __attribute__ ((unused)), ptr++; ptr += 4; } + if (*data->naddresses) + grub_free (*data->addresses); + *data->naddresses = 0; *data->addresses = grub_malloc (sizeof ((*data->addresses)[0]) * grub_be_to_cpu16 (head->ancount)); if (!*data->addresses) -- 1.8.1