From: Paolo Bonzini <pbonzini@redhat.com>
To: Markus Armbruster <armbru@redhat.com>, qemu-devel@nongnu.org
Cc: berrange@redhat.com, kraxel@redhat.com
Subject: Re: [Qemu-devel] [PATCH for-2.9] sockets: Fix socket_address_to_string() hostname truncation
Date: Thu, 23 Mar 2017 12:40:24 +0100 [thread overview]
Message-ID: <24dcf0a9-971f-7576-f863-f7db01aa62aa@redhat.com> (raw)
In-Reply-To: <1490268208-23368-1-git-send-email-armbru@redhat.com>
On 23/03/2017 12:23, Markus Armbruster wrote:
> We first snprintf() to a fixed buffer, then g_strdup() the result
> *boggle*.
>
> Worse, the size of the fixed buffer INET6_ADDRSTRLEN + 5 + 4 is bogus:
> the 4 correctly accounts for '[', ']', ':' and '\0', but
> INET6_ADDRSTRLEN is not a suitable limit for inet->host, and 5 is not
> one for inet->port! They are for host and port in *numeric* form
> (exploiting that INET6_ADDRSTRLEN > INET_ADDRSTRLEN), but inet->host
> can also be a hostname, and inet->port can be a service name, to be
> resolved with getaddrinfo().
>
> Fortunately, the only user so far is the "socket" network backend's
> net_socket_connected(), which uses it to initialize a NetSocketState's
> info_str[]. info_str[] has considerable more space: 256 instead of
> 55. So the bug's impact appears to be limited to truncated "info
> networks" with the "socket" network backend.
>
> The fix is obvious: use g_strdup_printf().
>
> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> ---
> util/qemu-sockets.c | 9 ++-------
> 1 file changed, 2 insertions(+), 7 deletions(-)
>
> diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c
> index 7c120c4..40164bf 100644
> --- a/util/qemu-sockets.c
> +++ b/util/qemu-sockets.c
> @@ -1307,19 +1307,14 @@ char *socket_address_to_string(struct SocketAddress *addr, Error **errp)
> {
> char *buf;
> InetSocketAddress *inet;
> - char host_port[INET6_ADDRSTRLEN + 5 + 4];
/me learnt about INET6_ADDRSTRLEN today.
> switch (addr->type) {
> case SOCKET_ADDRESS_KIND_INET:
> inet = addr->u.inet.data;
> if (strchr(inet->host, ':') == NULL) {
> - snprintf(host_port, sizeof(host_port), "%s:%s", inet->host,
> - inet->port);
> - buf = g_strdup(host_port);
> + buf = g_strdup_printf("%s:%s", inet->host, inet->port);
> } else {
> - snprintf(host_port, sizeof(host_port), "[%s]:%s", inet->host,
> - inet->port);
> - buf = g_strdup(host_port);
> + buf = g_strdup_printf("[%s]:%s", inet->host, inet->port);
> }
> break;
>
>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
next prev parent reply other threads:[~2017-03-23 11:40 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-03-23 11:23 [Qemu-devel] [PATCH for-2.9] sockets: Fix socket_address_to_string() hostname truncation Markus Armbruster
2017-03-23 11:40 ` Paolo Bonzini [this message]
2017-04-03 11:26 ` Daniel P. Berrange
2017-04-03 12:31 ` Philippe Mathieu-Daudé
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=24dcf0a9-971f-7576-f863-f7db01aa62aa@redhat.com \
--to=pbonzini@redhat.com \
--cc=armbru@redhat.com \
--cc=berrange@redhat.com \
--cc=kraxel@redhat.com \
--cc=qemu-devel@nongnu.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).