From: Michael Chang <mchang@suse.com>
To: grub-devel@gnu.org
Subject: [PATCH] dns: fix heap corruption
Date: Thu, 7 Jul 2016 17:18:17 +0800 [thread overview]
Message-ID: <20160707091817.GA16571@linux-9gqx.suse.de> (raw)
Since commit f9d1b4422efb2c06e5472fb2c304712e2029796b I occasionally bumped
into heap corruption problem during dns lookup.
After tracing the issue, it looks the *data->addresses array is not correctly
allocated. It need to hold accumulated dns look up result but not only the new
result in new message. The heap corruption occured when appending new result to
it.
This patch fixed the issue for me by reallocating the array if it found too
small to hold all the result.
Thanks,
---
grub-core/net/dns.c | 23 +++++++++++++++++------
1 file changed, 17 insertions(+), 6 deletions(-)
diff --git a/grub-core/net/dns.c b/grub-core/net/dns.c
index 89741dd..b8d8873 100644
--- a/grub-core/net/dns.c
+++ b/grub-core/net/dns.c
@@ -276,14 +276,25 @@ recv_hook (grub_net_udp_socket_t sock __attribute__ ((unused)),
ptr++;
ptr += 4;
}
- *data->addresses = grub_malloc (sizeof ((*data->addresses)[0])
- * grub_be_to_cpu16 (head->ancount));
- if (!*data->addresses)
+
+ if (ALIGN_UP (grub_be_to_cpu16 (head->ancount) + *data->naddresses, 4) > ALIGN_UP (*data->naddresses, 4))
{
- grub_errno = GRUB_ERR_NONE;
- grub_netbuff_free (nb);
- return GRUB_ERR_NONE;
+ grub_net_network_level_address_t *old_addresses = *data->addresses;
+ *data->addresses = grub_malloc (sizeof ((*data->addresses)[0])
+ * ALIGN_UP (grub_be_to_cpu16 (head->ancount) + *data->naddresses, 4));
+ if (!*data->addresses)
+ {
+ grub_errno = GRUB_ERR_NONE;
+ grub_netbuff_free (nb);
+ return GRUB_ERR_NONE;
+ }
+ if (*data->naddresses)
+ {
+ grub_memcpy (*data->addresses, old_addresses, sizeof ((*data->addresses)[0]) * (*data->naddresses));
+ grub_free (old_addresses);
+ }
}
+
reparse_ptr = ptr;
reparse:
for (i = 0, ptr = reparse_ptr; i < grub_be_to_cpu16 (head->ancount); i++)
--
2.6.2
next reply other threads:[~2016-07-07 9:18 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-07-07 9:18 Michael Chang [this message]
2016-07-08 19:54 ` [PATCH] dns: fix heap corruption Andrei Borzenkov
2016-07-11 6:02 ` Michael Chang
2016-07-26 17:40 ` Andrei Borzenkov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20160707091817.GA16571@linux-9gqx.suse.de \
--to=mchang@suse.com \
--cc=grub-devel@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).