From nobody Mon May 6 16:07:53 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1490268264583767.9965066610728; Thu, 23 Mar 2017 04:24:24 -0700 (PDT) Received: from localhost ([::1]:55718 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cr0qs-0007xV-UJ for importer@patchew.org; Thu, 23 Mar 2017 07:24:22 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59061) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cr0q9-0007eS-Ix for qemu-devel@nongnu.org; Thu, 23 Mar 2017 07:23:38 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cr0q4-00039p-Mj for qemu-devel@nongnu.org; Thu, 23 Mar 2017 07:23:37 -0400 Received: from mx1.redhat.com ([209.132.183.28]:41474) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cr0q4-000381-H3 for qemu-devel@nongnu.org; Thu, 23 Mar 2017 07:23:32 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 69F7861D33 for ; Thu, 23 Mar 2017 11:23:32 +0000 (UTC) Received: from blackfin.pond.sub.org (ovpn-116-26.ams2.redhat.com [10.36.116.26]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8E74C7E862; Thu, 23 Mar 2017 11:23:30 +0000 (UTC) Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id 0B0711138645; Thu, 23 Mar 2017 12:23:28 +0100 (CET) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 69F7861D33 Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=armbru@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 69F7861D33 From: Markus Armbruster To: qemu-devel@nongnu.org Date: Thu, 23 Mar 2017 12:23:28 +0100 Message-Id: <1490268208-23368-1-git-send-email-armbru@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Thu, 23 Mar 2017 11:23:32 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH for-2.9] sockets: Fix socket_address_to_string() hostname truncation X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: pbonzini@redhat.com, kraxel@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: Daniel P. Berrange Reviewed-by: Paolo Bonzini Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- 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]; =20 switch (addr->type) { case SOCKET_ADDRESS_KIND_INET: inet =3D addr->u.inet.data; if (strchr(inet->host, ':') =3D=3D NULL) { - snprintf(host_port, sizeof(host_port), "%s:%s", inet->host, - inet->port); - buf =3D g_strdup(host_port); + buf =3D g_strdup_printf("%s:%s", inet->host, inet->port); } else { - snprintf(host_port, sizeof(host_port), "[%s]:%s", inet->host, - inet->port); - buf =3D g_strdup(host_port); + buf =3D g_strdup_printf("[%s]:%s", inet->host, inet->port); } break; =20 --=20 2.7.4