From nobody Mon Apr 29 07:21:02 2024 Delivered-To: importer@patchew.org Received-SPF: temperror (zoho.com: Error in retrieving data from DNS) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=temperror (zoho.com: Error in retrieving data from DNS) 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 1508185162953530.2631752624903; Mon, 16 Oct 2017 13:19:22 -0700 (PDT) Received: from localhost ([::1]:35034 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e4Br2-0005ne-KZ for importer@patchew.org; Mon, 16 Oct 2017 16:19:16 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44384) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e4Boz-0004bX-2J for qemu-devel@nongnu.org; Mon, 16 Oct 2017 16:17:10 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1e4Bov-0000CE-Om for qemu-devel@nongnu.org; Mon, 16 Oct 2017 16:17:09 -0400 Received: from mx1.redhat.com ([209.132.183.28]:59876) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1e4Bov-0000BL-Gk for qemu-devel@nongnu.org; Mon, 16 Oct 2017 16:17:05 -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 9B82B85540; Mon, 16 Oct 2017 20:17:04 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.33.36.3]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8F3725D6B5; Mon, 16 Oct 2017 20:17:03 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 9B82B85540 Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=berrange@redhat.com From: "Daniel P. Berrange" To: qemu-devel@nongnu.org Date: Mon, 16 Oct 2017 21:16:40 +0100 Message-Id: <20171016201650.18399-2-berrange@redhat.com> In-Reply-To: <20171016201650.18399-1-berrange@redhat.com> References: <20171016201650.18399-1-berrange@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.28]); Mon, 16 Oct 2017 20:17:04 +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] [PULL v1 01/11] sockets: factor out a new try_bind() function 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: Peter Maydell , Knut Omang Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_6 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Knut Omang A refactoring step to prepare for the problem exposed by the test-listen test in the previous commit. Simplify and reorganize the IPv6 specific extra measures and move it out of the for loop to increase code readability. No semantic changes. Signed-off-by: Knut Omang Reviewed-by: Daniel P. Berrange Signed-off-by: Daniel P. Berrange --- util/qemu-sockets.c | 69 ++++++++++++++++++++++++++++++-------------------= ---- 1 file changed, 39 insertions(+), 30 deletions(-) diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c index d149383bb9..211ebadcec 100644 --- a/util/qemu-sockets.c +++ b/util/qemu-sockets.c @@ -149,6 +149,44 @@ int inet_ai_family_from_address(InetSocketAddress *add= r, return PF_UNSPEC; } =20 +static int try_bind(int socket, InetSocketAddress *saddr, struct addrinfo = *e) +{ +#ifndef IPV6_V6ONLY + return bind(socket, e->ai_addr, e->ai_addrlen); +#else + /* + * Deals with first & last cases in matrix in comment + * for inet_ai_family_from_address(). + */ + int v6only =3D + ((!saddr->has_ipv4 && !saddr->has_ipv6) || + (saddr->has_ipv4 && saddr->ipv4 && + saddr->has_ipv6 && saddr->ipv6)) ? 0 : 1; + int stat; + + rebind: + if (e->ai_family =3D=3D PF_INET6) { + qemu_setsockopt(socket, IPPROTO_IPV6, IPV6_V6ONLY, &v6only, + sizeof(v6only)); + } + + stat =3D bind(socket, e->ai_addr, e->ai_addrlen); + if (!stat) { + return 0; + } + + /* If we got EADDRINUSE from an IPv6 bind & v6only is unset, + * it could be that the IPv4 port is already claimed, so retry + * with v6only set + */ + if (e->ai_family =3D=3D PF_INET6 && errno =3D=3D EADDRINUSE && !v6only= ) { + v6only =3D 1; + goto rebind; + } + return stat; +#endif +} + static int inet_listen_saddr(InetSocketAddress *saddr, int port_offset, bool update_addr, @@ -228,39 +266,10 @@ static int inet_listen_saddr(InetSocketAddress *saddr, port_min =3D inet_getport(e); port_max =3D saddr->has_to ? saddr->to + port_offset : port_min; for (p =3D port_min; p <=3D port_max; p++) { -#ifdef IPV6_V6ONLY - /* - * Deals with first & last cases in matrix in comment - * for inet_ai_family_from_address(). - */ - int v6only =3D - ((!saddr->has_ipv4 && !saddr->has_ipv6) || - (saddr->has_ipv4 && saddr->ipv4 && - saddr->has_ipv6 && saddr->ipv6)) ? 0 : 1; -#endif inet_setport(e, p); -#ifdef IPV6_V6ONLY - rebind: - if (e->ai_family =3D=3D PF_INET6) { - qemu_setsockopt(slisten, IPPROTO_IPV6, IPV6_V6ONLY, &v6onl= y, - sizeof(v6only)); - } -#endif - if (bind(slisten, e->ai_addr, e->ai_addrlen) =3D=3D 0) { + if (try_bind(slisten, saddr, e) >=3D 0) { goto listen; } - -#ifdef IPV6_V6ONLY - /* If we got EADDRINUSE from an IPv6 bind & V6ONLY is unset, - * it could be that the IPv4 port is already claimed, so retry - * with V6ONLY set - */ - if (e->ai_family =3D=3D PF_INET6 && errno =3D=3D EADDRINUSE &&= !v6only) { - v6only =3D 1; - goto rebind; - } -#endif - if (p =3D=3D port_max) { if (!e->ai_next) { error_setg_errno(errp, errno, "Failed to bind socket"); --=20 2.13.5 From nobody Mon Apr 29 07:21:02 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.zohomail.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 1508185304100220.52475973954358; Mon, 16 Oct 2017 13:21:44 -0700 (PDT) Received: from localhost ([::1]:35048 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e4BtL-0007yp-Ce for importer@patchew.org; Mon, 16 Oct 2017 16:21:39 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44387) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e4Boz-0004bc-3V for qemu-devel@nongnu.org; Mon, 16 Oct 2017 16:17:09 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1e4Box-0000DC-5L for qemu-devel@nongnu.org; Mon, 16 Oct 2017 16:17:09 -0400 Received: from mx1.redhat.com ([209.132.183.28]:56942) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1e4Bow-0000Ci-V6 for qemu-devel@nongnu.org; Mon, 16 Oct 2017 16:17:07 -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 16F4F883C5; Mon, 16 Oct 2017 20:17:06 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.33.36.3]) by smtp.corp.redhat.com (Postfix) with ESMTP id E82135D6B4; Mon, 16 Oct 2017 20:17:04 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 16F4F883C5 Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=berrange@redhat.com From: "Daniel P. Berrange" To: qemu-devel@nongnu.org Date: Mon, 16 Oct 2017 21:16:41 +0100 Message-Id: <20171016201650.18399-3-berrange@redhat.com> In-Reply-To: <20171016201650.18399-1-berrange@redhat.com> References: <20171016201650.18399-1-berrange@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.26]); Mon, 16 Oct 2017 20:17:06 +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] [PULL v1 02/11] sockets: factor out create_fast_reuse_socket 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: Peter Maydell , Knut Omang 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" From: Knut Omang Another refactoring step to prepare for fixing the problem exposed with the test-listen test in the previous commit Signed-off-by: Knut Omang Reviewed-by: Daniel P. Berrange Signed-off-by: Daniel P. Berrange --- util/qemu-sockets.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c index 211ebadcec..65f6dcd5ef 100644 --- a/util/qemu-sockets.c +++ b/util/qemu-sockets.c @@ -149,6 +149,16 @@ int inet_ai_family_from_address(InetSocketAddress *add= r, return PF_UNSPEC; } =20 +static int create_fast_reuse_socket(struct addrinfo *e) +{ + int slisten =3D qemu_socket(e->ai_family, e->ai_socktype, e->ai_protoc= ol); + if (slisten < 0) { + return -1; + } + socket_set_fast_reuse(slisten); + return slisten; +} + static int try_bind(int socket, InetSocketAddress *saddr, struct addrinfo = *e) { #ifndef IPV6_V6ONLY @@ -253,7 +263,8 @@ static int inet_listen_saddr(InetSocketAddress *saddr, getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen, uaddr,INET6_ADDRSTRLEN,uport,32, NI_NUMERICHOST | NI_NUMERICSERV); - slisten =3D qemu_socket(e->ai_family, e->ai_socktype, e->ai_protoc= ol); + + slisten =3D create_fast_reuse_socket(e); if (slisten < 0) { if (!e->ai_next) { error_setg_errno(errp, errno, "Failed to create socket"); @@ -261,8 +272,6 @@ static int inet_listen_saddr(InetSocketAddress *saddr, continue; } =20 - socket_set_fast_reuse(slisten); - port_min =3D inet_getport(e); port_max =3D saddr->has_to ? saddr->to + port_offset : port_min; for (p =3D port_min; p <=3D port_max; p++) { --=20 2.13.5 From nobody Mon Apr 29 07:21:02 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.zohomail.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 150818538293220.257292144670487; Mon, 16 Oct 2017 13:23:02 -0700 (PDT) Received: from localhost ([::1]:35053 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e4BuW-0000ro-JE for importer@patchew.org; Mon, 16 Oct 2017 16:22:52 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44410) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e4Boz-0004cF-Tt for qemu-devel@nongnu.org; Mon, 16 Oct 2017 16:17:11 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1e4Boy-0000E9-M6 for qemu-devel@nongnu.org; Mon, 16 Oct 2017 16:17:09 -0400 Received: from mx1.redhat.com ([209.132.183.28]:39944) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1e4Boy-0000Dh-Dx for qemu-devel@nongnu.org; Mon, 16 Oct 2017 16:17:08 -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 8A2934E4E6; Mon, 16 Oct 2017 20:17:07 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.33.36.3]) by smtp.corp.redhat.com (Postfix) with ESMTP id 63F085D6B5; Mon, 16 Oct 2017 20:17:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 8A2934E4E6 Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=berrange@redhat.com From: "Daniel P. Berrange" To: qemu-devel@nongnu.org Date: Mon, 16 Oct 2017 21:16:42 +0100 Message-Id: <20171016201650.18399-4-berrange@redhat.com> In-Reply-To: <20171016201650.18399-1-berrange@redhat.com> References: <20171016201650.18399-1-berrange@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.38]); Mon, 16 Oct 2017 20:17:07 +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] [PULL v1 03/11] sockets: Handle race condition between binds to the same port 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: Peter Maydell , Knut Omang 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" From: Knut Omang If an offset of ports is specified to the inet_listen_saddr function(), and two or more processes tries to bind from these ports at the same time, occasionally more than one process may be able to bind to the same port. The condition is detected by listen() but too late to avoid a failure. This function is called by socket_listen() and used by all socket listening code in QEMU, so all cases where any form of dynamic port selection is used should be subject to this issue. Add code to close and re-establish the socket when this condition is observed, hiding the race condition from the user. Also clean up some issues with error handling to allow more accurate reporting of the cause of an error. This has been developed and tested by means of the test-listen unit test in the previous commit. Enable the test for make check now that it passes. Reviewed-by: Bhavesh Davda Reviewed-by: Yuval Shaia Reviewed-by: Girish Moodalbail Signed-off-by: Knut Omang Reviewed-by: Daniel P. Berrange Signed-off-by: Daniel P. Berrange --- util/qemu-sockets.c | 58 +++++++++++++++++++++++++++++++++++++------------= ---- 1 file changed, 41 insertions(+), 17 deletions(-) diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c index 65f6dcd5ef..b47fb45885 100644 --- a/util/qemu-sockets.c +++ b/util/qemu-sockets.c @@ -206,7 +206,10 @@ static int inet_listen_saddr(InetSocketAddress *saddr, char port[33]; char uaddr[INET6_ADDRSTRLEN+1]; char uport[33]; - int slisten, rc, port_min, port_max, p; + int rc, port_min, port_max, p; + int slisten =3D 0; + int saved_errno =3D 0; + bool socket_created =3D false; Error *err =3D NULL; =20 memset(&ai,0, sizeof(ai)); @@ -258,7 +261,7 @@ static int inet_listen_saddr(InetSocketAddress *saddr, return -1; } =20 - /* create socket + bind */ + /* create socket + bind/listen */ for (e =3D res; e !=3D NULL; e =3D e->ai_next) { getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen, uaddr,INET6_ADDRSTRLEN,uport,32, @@ -266,37 +269,58 @@ static int inet_listen_saddr(InetSocketAddress *saddr, =20 slisten =3D create_fast_reuse_socket(e); if (slisten < 0) { - if (!e->ai_next) { - error_setg_errno(errp, errno, "Failed to create socket"); - } continue; } =20 + socket_created =3D true; port_min =3D inet_getport(e); port_max =3D saddr->has_to ? saddr->to + port_offset : port_min; for (p =3D port_min; p <=3D port_max; p++) { inet_setport(e, p); - if (try_bind(slisten, saddr, e) >=3D 0) { - goto listen; - } - if (p =3D=3D port_max) { - if (!e->ai_next) { + rc =3D try_bind(slisten, saddr, e); + if (rc) { + if (errno =3D=3D EADDRINUSE) { + continue; + } else { error_setg_errno(errp, errno, "Failed to bind socket"); + goto listen_failed; } } + if (!listen(slisten, 1)) { + goto listen_ok; + } + if (errno !=3D EADDRINUSE) { + error_setg_errno(errp, errno, "Failed to listen on socket"= ); + goto listen_failed; + } + /* Someone else managed to bind to the same port and beat us + * to listen on it! Socket semantics does not allow us to + * recover from this situation, so we need to recreate the + * socket to allow bind attempts for subsequent ports: + */ + closesocket(slisten); + slisten =3D create_fast_reuse_socket(e); + if (slisten < 0) { + error_setg_errno(errp, errno, + "Failed to recreate failed listening sock= et"); + goto listen_failed; + } } + } + error_setg_errno(errp, errno, + socket_created ? + "Failed to find an available port" : + "Failed to create a socket"); +listen_failed: + saved_errno =3D errno; + if (slisten >=3D 0) { closesocket(slisten); } freeaddrinfo(res); + errno =3D saved_errno; return -1; =20 -listen: - if (listen(slisten,1) !=3D 0) { - error_setg_errno(errp, errno, "Failed to listen on socket"); - closesocket(slisten); - freeaddrinfo(res); - return -1; - } +listen_ok: if (update_addr) { g_free(saddr->host); saddr->host =3D g_strdup(uaddr); --=20 2.13.5 From nobody Mon Apr 29 07:21:02 2024 Delivered-To: importer@patchew.org Received-SPF: temperror (zoho.com: Error in retrieving data from DNS) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=temperror (zoho.com: Error in retrieving data from DNS) 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 1508185174018607.3042957936361; Mon, 16 Oct 2017 13:19:34 -0700 (PDT) Received: from localhost ([::1]:35035 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e4Br4-0005op-9K for importer@patchew.org; Mon, 16 Oct 2017 16:19:18 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44423) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e4Bp1-0004cw-4i for qemu-devel@nongnu.org; Mon, 16 Oct 2017 16:17:12 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1e4Boz-0000F0-TN for qemu-devel@nongnu.org; Mon, 16 Oct 2017 16:17:11 -0400 Received: from mx1.redhat.com ([209.132.183.28]:40074) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1e4Boz-0000ES-NI for qemu-devel@nongnu.org; Mon, 16 Oct 2017 16:17:09 -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 D182D4E915; Mon, 16 Oct 2017 20:17:08 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.33.36.3]) by smtp.corp.redhat.com (Postfix) with ESMTP id D67655D6B4; Mon, 16 Oct 2017 20:17:07 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com D182D4E915 Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=berrange@redhat.com From: "Daniel P. Berrange" To: qemu-devel@nongnu.org Date: Mon, 16 Oct 2017 21:16:43 +0100 Message-Id: <20171016201650.18399-5-berrange@redhat.com> In-Reply-To: <20171016201650.18399-1-berrange@redhat.com> References: <20171016201650.18399-1-berrange@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.38]); Mon, 16 Oct 2017 20:17:08 +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] [PULL v1 04/11] io: monitor encoutput buffer size from websocket GSource 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: Peter Maydell Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_6 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The websocket GSource is monitoring the size of the rawoutput buffer to determine if the channel can accepts more writes. The rawoutput buffer, however, is merely a temporary staging buffer before data is copied into the encoutput buffer. Thus its size will always be zero when the GSource runs. This flaw causes the encoutput buffer to grow without bound if the other end of the underlying data channel doesn't read data being sent. This can be seen with VNC if a client is on a slow WAN link and the guest OS is sending many screen updates. A malicious VNC client can act like it is on a slow link by playing a video in the guest and then reading data very slowly, causing QEMU host memory to expand arbitrarily. This issue is assigned CVE-2017-15268, publically reported in https://bugs.launchpad.net/qemu/+bug/1718964 Reviewed-by: Eric Blake Signed-off-by: Daniel P. Berrange --- io/channel-websock.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/io/channel-websock.c b/io/channel-websock.c index d1d471f86e..04bcc059cd 100644 --- a/io/channel-websock.c +++ b/io/channel-websock.c @@ -28,7 +28,7 @@ #include =20 =20 -/* Max amount to allow in rawinput/rawoutput buffers */ +/* Max amount to allow in rawinput/encoutput buffers */ #define QIO_CHANNEL_WEBSOCK_MAX_BUFFER 8192 =20 #define QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN 24 @@ -1208,7 +1208,7 @@ qio_channel_websock_source_check(GSource *source) if (wsource->wioc->rawinput.offset || wsource->wioc->io_eof) { cond |=3D G_IO_IN; } - if (wsource->wioc->rawoutput.offset < QIO_CHANNEL_WEBSOCK_MAX_BUFFER) { + if (wsource->wioc->encoutput.offset < QIO_CHANNEL_WEBSOCK_MAX_BUFFER) { cond |=3D G_IO_OUT; } =20 --=20 2.13.5 From nobody Mon Apr 29 07:21:02 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.zohomail.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 (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1508185439616999.2034220423385; Mon, 16 Oct 2017 13:23:59 -0700 (PDT) Received: from localhost ([::1]:35056 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e4BvQ-0001b8-S8 for importer@patchew.org; Mon, 16 Oct 2017 16:23:48 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44450) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e4Bp2-0004dp-AS for qemu-devel@nongnu.org; Mon, 16 Oct 2017 16:17:13 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1e4Bp1-0000GH-B5 for qemu-devel@nongnu.org; Mon, 16 Oct 2017 16:17:12 -0400 Received: from mx1.redhat.com ([209.132.183.28]:40386) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1e4Bp1-0000FO-22 for qemu-devel@nongnu.org; Mon, 16 Oct 2017 16:17:11 -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 2879D7F755; Mon, 16 Oct 2017 20:17:10 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.33.36.3]) by smtp.corp.redhat.com (Postfix) with ESMTP id 29CA35D6B4; Mon, 16 Oct 2017 20:17:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 2879D7F755 Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=berrange@redhat.com From: "Daniel P. Berrange" To: qemu-devel@nongnu.org Date: Mon, 16 Oct 2017 21:16:44 +0100 Message-Id: <20171016201650.18399-6-berrange@redhat.com> In-Reply-To: <20171016201650.18399-1-berrange@redhat.com> References: <20171016201650.18399-1-berrange@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.27]); Mon, 16 Oct 2017 20:17:10 +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] [PULL v1 05/11] io: simplify websocket ping reply handling 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: Peter Maydell 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 must ensure we don't get flooded with ping replies if the outbound channel is slow. Currently we do this by keeping the ping reply in a separate temporary buffer and only writing it if the encoutput buffer is completely empty. This is overly pessimistic, as it is reasonable to add a ping reply to the encoutput buffer even if it has previous data in it, as long as that previous data doesn't include a ping reply. To track this better, put the ping reply directly into the encoutput buffer, and then record the size of encoutput at this time in pong_remain. As we write encoutput to the underlying channel, we can decrement the pong_remain counter. Once it hits zero, we can accept further ping replies for transmission. Reviewed-by: Eric Blake Signed-off-by: Daniel P. Berrange --- include/io/channel-websock.h | 2 +- io/channel-websock.c | 28 +++++++++++++++------------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/include/io/channel-websock.h b/include/io/channel-websock.h index ff32d8651b..3762707b9c 100644 --- a/include/io/channel-websock.h +++ b/include/io/channel-websock.h @@ -60,8 +60,8 @@ struct QIOChannelWebsock { Buffer encoutput; Buffer rawinput; Buffer rawoutput; - Buffer ping_reply; size_t payload_remain; + size_t pong_remain; QIOChannelWebsockMask mask; guint io_tag; Error *io_err; diff --git a/io/channel-websock.c b/io/channel-websock.c index 04bcc059cd..6083f74c9b 100644 --- a/io/channel-websock.c +++ b/io/channel-websock.c @@ -825,11 +825,14 @@ static int qio_channel_websock_decode_payload(QIOChan= nelWebsock *ioc, } return -1; } else if (ioc->opcode =3D=3D QIO_CHANNEL_WEBSOCK_OPCODE_PING) { - /* ping frames produce an immediate reply */ - buffer_reset(&ioc->ping_reply); - qio_channel_websock_encode_buffer( - ioc, &ioc->ping_reply, QIO_CHANNEL_WEBSOCK_OPCODE_PONG, - &ioc->encinput); + /* ping frames produce an immediate reply, as long as we've not st= ill + * got a previous pong queued, in which case we drop the new pong = */ + if (ioc->pong_remain =3D=3D 0) { + qio_channel_websock_encode_buffer( + ioc, &ioc->encoutput, QIO_CHANNEL_WEBSOCK_OPCODE_PONG, + &ioc->encinput); + ioc->pong_remain =3D ioc->encoutput.offset; + } } /* pong frames are ignored */ =20 if (payload_len) { @@ -888,7 +891,6 @@ static void qio_channel_websock_finalize(Object *obj) buffer_free(&ioc->encoutput); buffer_free(&ioc->rawinput); buffer_free(&ioc->rawoutput); - buffer_free(&ioc->ping_reply); object_unref(OBJECT(ioc->master)); if (ioc->io_tag) { g_source_remove(ioc->io_tag); @@ -946,12 +948,7 @@ static ssize_t qio_channel_websock_write_wire(QIOChann= elWebsock *ioc, ssize_t ret; ssize_t done =3D 0; =20 - /* ping replies take priority over binary data */ - if (!ioc->ping_reply.offset) { - qio_channel_websock_encode(ioc); - } else if (!ioc->encoutput.offset) { - buffer_move_empty(&ioc->encoutput, &ioc->ping_reply); - } + qio_channel_websock_encode(ioc); =20 while (ioc->encoutput.offset > 0) { ret =3D qio_channel_write(ioc->master, @@ -968,6 +965,11 @@ static ssize_t qio_channel_websock_write_wire(QIOChann= elWebsock *ioc, } buffer_advance(&ioc->encoutput, ret); done +=3D ret; + if (ioc->pong_remain < ret) { + ioc->pong_remain =3D 0; + } else { + ioc->pong_remain -=3D ret; + } } return done; } @@ -1026,7 +1028,7 @@ static void qio_channel_websock_set_watch(QIOChannelW= ebsock *ioc) return; } =20 - if (ioc->encoutput.offset || ioc->ping_reply.offset) { + if (ioc->encoutput.offset) { cond |=3D G_IO_OUT; } if (ioc->encinput.offset < QIO_CHANNEL_WEBSOCK_MAX_BUFFER && --=20 2.13.5 From nobody Mon Apr 29 07:21:02 2024 Delivered-To: importer@patchew.org Received-SPF: temperror (zoho.com: Error in retrieving data from DNS) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=temperror (zoho.com: Error in retrieving data from DNS) 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 1508185444568583.8263955061187; Mon, 16 Oct 2017 13:24:04 -0700 (PDT) Received: from localhost ([::1]:35057 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e4BvU-0001cw-1E for importer@patchew.org; Mon, 16 Oct 2017 16:23:52 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44461) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e4Bp3-0004ei-9Y for qemu-devel@nongnu.org; Mon, 16 Oct 2017 16:17:14 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1e4Bp2-0000Gx-Dn for qemu-devel@nongnu.org; Mon, 16 Oct 2017 16:17:13 -0400 Received: from mx1.redhat.com ([209.132.183.28]:49922) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1e4Bp2-0000GS-7U for qemu-devel@nongnu.org; Mon, 16 Oct 2017 16:17:12 -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 58A16C057FA7; Mon, 16 Oct 2017 20:17:11 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.33.36.3]) by smtp.corp.redhat.com (Postfix) with ESMTP id 766F85D6B2; Mon, 16 Oct 2017 20:17:10 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 58A16C057FA7 Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=berrange@redhat.com From: "Daniel P. Berrange" To: qemu-devel@nongnu.org Date: Mon, 16 Oct 2017 21:16:45 +0100 Message-Id: <20171016201650.18399-7-berrange@redhat.com> In-Reply-To: <20171016201650.18399-1-berrange@redhat.com> References: <20171016201650.18399-1-berrange@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.32]); Mon, 16 Oct 2017 20:17:11 +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] [PULL v1 06/11] io: get rid of qio_channel_websock_encode helper method 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: Peter Maydell Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_6 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The qio_channel_websock_encode method is only used in one place, everything else calls qio_channel_websock_encode_buffer directly. It can also be pushed up a level into the qio_channel_websock_writev method, since every other caller of qio_channel_websock_write_wire has already filled encoutput. Reviewed-by: Eric Blake Signed-off-by: Daniel P. Berrange --- io/channel-websock.c | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/io/channel-websock.c b/io/channel-websock.c index 6083f74c9b..700f5bea22 100644 --- a/io/channel-websock.c +++ b/io/channel-websock.c @@ -616,18 +616,6 @@ static void qio_channel_websock_encode_buffer(QIOChann= elWebsock *ioc, } =20 =20 -static void qio_channel_websock_encode(QIOChannelWebsock *ioc) -{ - if (!ioc->rawoutput.offset) { - return; - } - qio_channel_websock_encode_buffer( - ioc, &ioc->encoutput, QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME, - &ioc->rawoutput); - buffer_reset(&ioc->rawoutput); -} - - static ssize_t qio_channel_websock_write_wire(QIOChannelWebsock *, Error *= *); =20 =20 @@ -948,8 +936,6 @@ static ssize_t qio_channel_websock_write_wire(QIOChanne= lWebsock *ioc, ssize_t ret; ssize_t done =3D 0; =20 - qio_channel_websock_encode(ioc); - while (ioc->encoutput.offset > 0) { ret =3D qio_channel_write(ioc->master, (char *)ioc->encoutput.buffer, @@ -1134,6 +1120,12 @@ static ssize_t qio_channel_websock_writev(QIOChannel= *ioc, } =20 done: + if (ioc->rawoutput.offset) { + qio_channel_websock_encode_buffer( + ioc, &ioc->encoutput, QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME, + &ioc->rawoutput); + buffer_reset(&ioc->rawoutput); + } ret =3D qio_channel_websock_write_wire(wioc, errp); if (ret < 0 && ret !=3D QIO_CHANNEL_ERR_BLOCK) { --=20 2.13.5 From nobody Mon Apr 29 07:21:02 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.zohomail.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 (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1508185570983688.7645208109192; Mon, 16 Oct 2017 13:26:10 -0700 (PDT) Received: from localhost ([::1]:35071 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e4BxW-0003ST-4E for importer@patchew.org; Mon, 16 Oct 2017 16:25:58 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44484) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e4Bp5-0004gX-0j for qemu-devel@nongnu.org; Mon, 16 Oct 2017 16:17:19 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1e4Bp3-0000Hm-Lg for qemu-devel@nongnu.org; Mon, 16 Oct 2017 16:17:14 -0400 Received: from mx1.redhat.com ([209.132.183.28]:32427) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1e4Bp3-0000H7-DO for qemu-devel@nongnu.org; Mon, 16 Oct 2017 16:17:13 -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 888A581DE4; Mon, 16 Oct 2017 20:17:12 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.33.36.3]) by smtp.corp.redhat.com (Postfix) with ESMTP id A68765D6B2; Mon, 16 Oct 2017 20:17:11 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 888A581DE4 Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=berrange@redhat.com From: "Daniel P. Berrange" To: qemu-devel@nongnu.org Date: Mon, 16 Oct 2017 21:16:46 +0100 Message-Id: <20171016201650.18399-8-berrange@redhat.com> In-Reply-To: <20171016201650.18399-1-berrange@redhat.com> References: <20171016201650.18399-1-berrange@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.25]); Mon, 16 Oct 2017 20:17:12 +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] [PULL v1 07/11] io: pass a struct iovec into qio_channel_websock_encode 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: Peter Maydell 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" Instead of requiring use of another Buffer, pass a struct iovec into qio_channel_websock_encode, which gives callers more flexibility in how they process data. Reviewed-by: Eric Blake Signed-off-by: Daniel P. Berrange --- io/channel-websock.c | 71 ++++++++++++++++++++++++++++++++----------------= ---- 1 file changed, 44 insertions(+), 27 deletions(-) diff --git a/io/channel-websock.c b/io/channel-websock.c index 700f5bea22..93c06f5b94 100644 --- a/io/channel-websock.c +++ b/io/channel-websock.c @@ -582,37 +582,48 @@ static gboolean qio_channel_websock_handshake_io(QIOC= hannel *ioc, } =20 =20 -static void qio_channel_websock_encode_buffer(QIOChannelWebsock *ioc, - Buffer *output, - uint8_t opcode, Buffer *buff= er) +static void qio_channel_websock_encode(QIOChannelWebsock *ioc, + uint8_t opcode, + const struct iovec *iov, + size_t niov, + size_t size) { size_t header_size; + size_t i; union { char buf[QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT]; QIOChannelWebsockHeader ws; } header; =20 + assert(size <=3D iov_size(iov, niov)); + header.ws.b0 =3D QIO_CHANNEL_WEBSOCK_HEADER_FIELD_FIN | (opcode & QIO_CHANNEL_WEBSOCK_HEADER_FIELD_OPCODE); - if (buffer->offset < QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_7_BIT) { - header.ws.b1 =3D (uint8_t)buffer->offset; + if (size < QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_7_BIT) { + header.ws.b1 =3D (uint8_t)size; header_size =3D QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT; - } else if (buffer->offset < - QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_16_BIT) { + } else if (size < QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_16_BIT) { header.ws.b1 =3D QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT; - header.ws.u.s16.l16 =3D cpu_to_be16((uint16_t)buffer->offset); + header.ws.u.s16.l16 =3D cpu_to_be16((uint16_t)size); header_size =3D QIO_CHANNEL_WEBSOCK_HEADER_LEN_16_BIT; } else { header.ws.b1 =3D QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_64_BIT; - header.ws.u.s64.l64 =3D cpu_to_be64(buffer->offset); + header.ws.u.s64.l64 =3D cpu_to_be64(size); header_size =3D QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT; } header_size -=3D QIO_CHANNEL_WEBSOCK_HEADER_LEN_MASK; =20 - trace_qio_channel_websock_encode(ioc, opcode, header_size, buffer->off= set); - buffer_reserve(output, header_size + buffer->offset); - buffer_append(output, header.buf, header_size); - buffer_append(output, buffer->buffer, buffer->offset); + trace_qio_channel_websock_encode(ioc, opcode, header_size, size); + buffer_reserve(&ioc->encoutput, header_size + size); + buffer_append(&ioc->encoutput, header.buf, header_size); + for (i =3D 0; i < niov && size !=3D 0; i++) { + size_t want =3D iov[i].iov_len; + if (want > size) { + want =3D size; + } + buffer_append(&ioc->encoutput, iov[i].iov_base, want); + size -=3D want; + } } =20 =20 @@ -622,6 +633,7 @@ static ssize_t qio_channel_websock_write_wire(QIOChanne= lWebsock *, Error **); static void qio_channel_websock_write_close(QIOChannelWebsock *ioc, uint16_t code, const char *rea= son) { + struct iovec iov; buffer_reserve(&ioc->rawoutput, 2 + (reason ? strlen(reason) : 0)); *(uint16_t *)(ioc->rawoutput.buffer + ioc->rawoutput.offset) =3D cpu_to_be16(code); @@ -629,9 +641,10 @@ static void qio_channel_websock_write_close(QIOChannel= Websock *ioc, if (reason) { buffer_append(&ioc->rawoutput, reason, strlen(reason)); } - qio_channel_websock_encode_buffer( - ioc, &ioc->encoutput, QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE, - &ioc->rawoutput); + iov.iov_base =3D ioc->rawoutput.buffer; + iov.iov_len =3D ioc->rawoutput.offset; + qio_channel_websock_encode(ioc, QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE, + &iov, 1, iov.iov_len); buffer_reset(&ioc->rawoutput); qio_channel_websock_write_wire(ioc, NULL); qio_channel_shutdown(ioc->master, QIO_CHANNEL_SHUTDOWN_BOTH, NULL); @@ -801,9 +814,10 @@ static int qio_channel_websock_decode_payload(QIOChann= elWebsock *ioc, error_setg(errp, "websocket closed by peer"); if (payload_len) { /* echo client status */ - qio_channel_websock_encode_buffer( - ioc, &ioc->encoutput, QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE, - &ioc->encinput); + struct iovec iov =3D { .iov_base =3D ioc->encinput.buffer, + .iov_len =3D ioc->encinput.offset }; + qio_channel_websock_encode(ioc, QIO_CHANNEL_WEBSOCK_OPCODE_CLO= SE, + &iov, 1, iov.iov_len); qio_channel_websock_write_wire(ioc, NULL); qio_channel_shutdown(ioc->master, QIO_CHANNEL_SHUTDOWN_BOTH, N= ULL); } else { @@ -816,9 +830,10 @@ static int qio_channel_websock_decode_payload(QIOChann= elWebsock *ioc, /* ping frames produce an immediate reply, as long as we've not st= ill * got a previous pong queued, in which case we drop the new pong = */ if (ioc->pong_remain =3D=3D 0) { - qio_channel_websock_encode_buffer( - ioc, &ioc->encoutput, QIO_CHANNEL_WEBSOCK_OPCODE_PONG, - &ioc->encinput); + struct iovec iov =3D { .iov_base =3D ioc->encinput.buffer, + .iov_len =3D ioc->encinput.offset }; + qio_channel_websock_encode(ioc, QIO_CHANNEL_WEBSOCK_OPCODE_PON= G, + &iov, 1, iov.iov_len); ioc->pong_remain =3D ioc->encoutput.offset; } } /* pong frames are ignored */ @@ -1120,11 +1135,13 @@ static ssize_t qio_channel_websock_writev(QIOChanne= l *ioc, } =20 done: - if (ioc->rawoutput.offset) { - qio_channel_websock_encode_buffer( - ioc, &ioc->encoutput, QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME, - &ioc->rawoutput); - buffer_reset(&ioc->rawoutput); + if (wioc->rawoutput.offset) { + struct iovec iov =3D { .iov_base =3D wioc->rawoutput.buffer, + .iov_len =3D wioc->rawoutput.offset }; + qio_channel_websock_encode(wioc, + QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME, + &iov, 1, iov.iov_len); + buffer_reset(&wioc->rawoutput); } ret =3D qio_channel_websock_write_wire(wioc, errp); if (ret < 0 && --=20 2.13.5 From nobody Mon Apr 29 07:21:02 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.zohomail.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 1508185777669358.3139867761313; Mon, 16 Oct 2017 13:29:37 -0700 (PDT) Received: from localhost ([::1]:35085 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e4C0s-00064w-OT for importer@patchew.org; Mon, 16 Oct 2017 16:29:26 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44526) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e4Bp9-0004kr-6e for qemu-devel@nongnu.org; Mon, 16 Oct 2017 16:17:20 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1e4Bp4-0000IP-RU for qemu-devel@nongnu.org; Mon, 16 Oct 2017 16:17:19 -0400 Received: from mx1.redhat.com ([209.132.183.28]:47934) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1e4Bp4-0000Hy-Ih for qemu-devel@nongnu.org; Mon, 16 Oct 2017 16:17:14 -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 B586581DE4; Mon, 16 Oct 2017 20:17:13 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.33.36.3]) by smtp.corp.redhat.com (Postfix) with ESMTP id D514E5D6B2; Mon, 16 Oct 2017 20:17:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com B586581DE4 Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=berrange@redhat.com From: "Daniel P. Berrange" To: qemu-devel@nongnu.org Date: Mon, 16 Oct 2017 21:16:47 +0100 Message-Id: <20171016201650.18399-9-berrange@redhat.com> In-Reply-To: <20171016201650.18399-1-berrange@redhat.com> References: <20171016201650.18399-1-berrange@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.25]); Mon, 16 Oct 2017 20:17:13 +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] [PULL v1 08/11] io: get rid of bounce buffering in websock write path 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: Peter Maydell 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" Currently most outbound I/O on the websock channel gets copied into the rawoutput buffer, and then immediately copied again into the encoutput buffer, with a header prepended. Now that qio_channel_websock_encode accepts a struct iovec, we can trivially remove this bounce buffering and write directly to encoutput. In doing so, we also now correctly validate the encoutput size against the QIO_CHANNEL_WEBSOCK_MAX_BUFFER limit. Reviewed-by: Eric Blake Signed-off-by: Daniel P. Berrange --- include/io/channel-websock.h | 1 - io/channel-websock.c | 64 +++++++++++++++++++---------------------= ---- 2 files changed, 28 insertions(+), 37 deletions(-) diff --git a/include/io/channel-websock.h b/include/io/channel-websock.h index 3762707b9c..a7e5e92e61 100644 --- a/include/io/channel-websock.h +++ b/include/io/channel-websock.h @@ -59,7 +59,6 @@ struct QIOChannelWebsock { Buffer encinput; Buffer encoutput; Buffer rawinput; - Buffer rawoutput; size_t payload_remain; size_t pong_remain; QIOChannelWebsockMask mask; diff --git a/io/channel-websock.c b/io/channel-websock.c index 93c06f5b94..e82b1beab3 100644 --- a/io/channel-websock.c +++ b/io/channel-websock.c @@ -24,6 +24,7 @@ #include "io/channel-websock.h" #include "crypto/hash.h" #include "trace.h" +#include "qemu/iov.h" =20 #include =20 @@ -633,19 +634,22 @@ static ssize_t qio_channel_websock_write_wire(QIOChan= nelWebsock *, Error **); static void qio_channel_websock_write_close(QIOChannelWebsock *ioc, uint16_t code, const char *rea= son) { - struct iovec iov; - buffer_reserve(&ioc->rawoutput, 2 + (reason ? strlen(reason) : 0)); - *(uint16_t *)(ioc->rawoutput.buffer + ioc->rawoutput.offset) =3D - cpu_to_be16(code); - ioc->rawoutput.offset +=3D 2; + struct iovec iov[2] =3D { + { .iov_base =3D &code, .iov_len =3D sizeof(code) }, + }; + size_t niov =3D 1; + size_t size =3D iov[0].iov_len; + + cpu_to_be16s(&code); + if (reason) { - buffer_append(&ioc->rawoutput, reason, strlen(reason)); + iov[1].iov_base =3D (void *)reason; + iov[1].iov_len =3D strlen(reason); + size +=3D iov[1].iov_len; + niov++; } - iov.iov_base =3D ioc->rawoutput.buffer; - iov.iov_len =3D ioc->rawoutput.offset; qio_channel_websock_encode(ioc, QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE, - &iov, 1, iov.iov_len); - buffer_reset(&ioc->rawoutput); + iov, niov, size); qio_channel_websock_write_wire(ioc, NULL); qio_channel_shutdown(ioc->master, QIO_CHANNEL_SHUTDOWN_BOTH, NULL); } @@ -893,7 +897,6 @@ static void qio_channel_websock_finalize(Object *obj) buffer_free(&ioc->encinput); buffer_free(&ioc->encoutput); buffer_free(&ioc->rawinput); - buffer_free(&ioc->rawoutput); object_unref(OBJECT(ioc->master)); if (ioc->io_tag) { g_source_remove(ioc->io_tag); @@ -1103,8 +1106,8 @@ static ssize_t qio_channel_websock_writev(QIOChannel = *ioc, Error **errp) { QIOChannelWebsock *wioc =3D QIO_CHANNEL_WEBSOCK(ioc); - size_t i; - ssize_t done =3D 0; + ssize_t want =3D iov_size(iov, niov); + ssize_t avail; ssize_t ret; =20 if (wioc->io_err) { @@ -1117,32 +1120,21 @@ static ssize_t qio_channel_websock_writev(QIOChanne= l *ioc, return -1; } =20 - for (i =3D 0; i < niov; i++) { - size_t want =3D iov[i].iov_len; - if ((want + wioc->rawoutput.offset) > QIO_CHANNEL_WEBSOCK_MAX_BUFF= ER) { - want =3D (QIO_CHANNEL_WEBSOCK_MAX_BUFFER - wioc->rawoutput.off= set); - } - if (want =3D=3D 0) { - goto done; - } - - buffer_reserve(&wioc->rawoutput, want); - buffer_append(&wioc->rawoutput, iov[i].iov_base, want); - done +=3D want; - if (want < iov[i].iov_len) { - break; - } + avail =3D wioc->encoutput.offset >=3D QIO_CHANNEL_WEBSOCK_MAX_BUFFER ? + 0 : (QIO_CHANNEL_WEBSOCK_MAX_BUFFER - wioc->encoutput.offset); + if (want > avail) { + want =3D avail; } =20 - done: - if (wioc->rawoutput.offset) { - struct iovec iov =3D { .iov_base =3D wioc->rawoutput.buffer, - .iov_len =3D wioc->rawoutput.offset }; + if (want) { qio_channel_websock_encode(wioc, QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME, - &iov, 1, iov.iov_len); - buffer_reset(&wioc->rawoutput); + iov, niov, want); } + + /* Even if want =3D=3D 0, we'll try write_wire in case there's + * pending data we could usefully flush out + */ ret =3D qio_channel_websock_write_wire(wioc, errp); if (ret < 0 && ret !=3D QIO_CHANNEL_ERR_BLOCK) { @@ -1152,11 +1144,11 @@ static ssize_t qio_channel_websock_writev(QIOChanne= l *ioc, =20 qio_channel_websock_set_watch(wioc); =20 - if (done =3D=3D 0) { + if (want =3D=3D 0) { return QIO_CHANNEL_ERR_BLOCK; } =20 - return done; + return want; } =20 static int qio_channel_websock_set_blocking(QIOChannel *ioc, --=20 2.13.5 From nobody Mon Apr 29 07:21:02 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.zohomail.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 (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1508185678039689.1871976048658; Mon, 16 Oct 2017 13:27:58 -0700 (PDT) Received: from localhost ([::1]:35081 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e4BzJ-0004xS-Ag for importer@patchew.org; Mon, 16 Oct 2017 16:27:49 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44525) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e4Bp9-0004kq-6c for qemu-devel@nongnu.org; Mon, 16 Oct 2017 16:17:20 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1e4Bp5-0000KF-UO for qemu-devel@nongnu.org; Mon, 16 Oct 2017 16:17:19 -0400 Received: from mx1.redhat.com ([209.132.183.28]:38260) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1e4Bp5-0000Ih-PB for qemu-devel@nongnu.org; Mon, 16 Oct 2017 16:17:15 -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 E5D3C5AFC7; Mon, 16 Oct 2017 20:17:14 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.33.36.3]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0FED55D6B2; Mon, 16 Oct 2017 20:17:13 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com E5D3C5AFC7 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=fail smtp.mailfrom=berrange@redhat.com From: "Daniel P. Berrange" To: qemu-devel@nongnu.org Date: Mon, 16 Oct 2017 21:16:48 +0100 Message-Id: <20171016201650.18399-10-berrange@redhat.com> In-Reply-To: <20171016201650.18399-1-berrange@redhat.com> References: <20171016201650.18399-1-berrange@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]); Mon, 16 Oct 2017 20:17:15 +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] [PULL v1 09/11] io: cope with websock 'Connection' header having multiple values 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: Peter Maydell 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" The noVNC server sends a header "Connection: keep-alive, Upgrade" which fails our simple equality test. Split the header on ',', trim whitespace and then check for 'upgrade' token. Reviewed-by: Eric Blake Signed-off-by: Daniel P. Berrange --- io/channel-websock.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/io/channel-websock.c b/io/channel-websock.c index e82b1beab3..0354845e52 100644 --- a/io/channel-websock.c +++ b/io/channel-websock.c @@ -374,6 +374,9 @@ static void qio_channel_websock_handshake_process(QIOCh= annelWebsock *ioc, size_t nhdrs =3D G_N_ELEMENTS(hdrs); const char *protocols =3D NULL, *version =3D NULL, *key =3D NULL, *host =3D NULL, *connection =3D NULL, *upgrade =3D NULL; + char **connectionv; + bool upgraded =3D false; + size_t i; =20 nhdrs =3D qio_channel_websock_extract_headers(ioc, buffer, hdrs, nhdrs= , errp); if (!nhdrs) { @@ -440,7 +443,16 @@ static void qio_channel_websock_handshake_process(QIOC= hannelWebsock *ioc, goto bad_request; } =20 - if (strcasecmp(connection, QIO_CHANNEL_WEBSOCK_CONNECTION_UPGRADE) != =3D 0) { + connectionv =3D g_strsplit(connection, ",", 0); + for (i =3D 0; connectionv !=3D NULL && connectionv[i] !=3D NULL; i++) { + g_strstrip(connectionv[i]); + if (strcasecmp(connectionv[i], + QIO_CHANNEL_WEBSOCK_CONNECTION_UPGRADE) =3D=3D 0) { + upgraded =3D true; + } + } + g_strfreev(connectionv); + if (!upgraded) { error_setg(errp, "No connection upgrade requested '%s'", connectio= n); goto bad_request; } --=20 2.13.5 From nobody Mon Apr 29 07:21:02 2024 Delivered-To: importer@patchew.org Received-SPF: temperror (zoho.com: Error in retrieving data from DNS) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=temperror (zoho.com: Error in retrieving data from DNS) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1508185565707388.3453195778769; Mon, 16 Oct 2017 13:26:05 -0700 (PDT) Received: from localhost ([::1]:35070 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e4BxU-0003RC-Bj for importer@patchew.org; Mon, 16 Oct 2017 16:25:56 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44529) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e4Bp9-0004kw-7n for qemu-devel@nongnu.org; Mon, 16 Oct 2017 16:17:20 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1e4Bp7-0000LV-5w for qemu-devel@nongnu.org; Mon, 16 Oct 2017 16:17:19 -0400 Received: from mx1.redhat.com ([209.132.183.28]:60398) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1e4Bp6-0000Kv-Vi for qemu-devel@nongnu.org; Mon, 16 Oct 2017 16:17:17 -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 222E27EA80; Mon, 16 Oct 2017 20:17:16 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.33.36.3]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3DC6B5D6B3; Mon, 16 Oct 2017 20:17:15 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 222E27EA80 Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=berrange@redhat.com From: "Daniel P. Berrange" To: qemu-devel@nongnu.org Date: Mon, 16 Oct 2017 21:16:49 +0100 Message-Id: <20171016201650.18399-11-berrange@redhat.com> In-Reply-To: <20171016201650.18399-1-berrange@redhat.com> References: <20171016201650.18399-1-berrange@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.28]); Mon, 16 Oct 2017 20:17:16 +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] [PULL v1 10/11] io: add trace points for websocket HTTP protocol headers 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: Peter Maydell Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_6 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Reviewed-by: Eric Blake Signed-off-by: Daniel P. Berrange --- io/channel-websock.c | 4 ++++ io/trace-events | 2 ++ 2 files changed, 6 insertions(+) diff --git a/io/channel-websock.c b/io/channel-websock.c index 0354845e52..aa35ef3274 100644 --- a/io/channel-websock.c +++ b/io/channel-websock.c @@ -224,6 +224,7 @@ qio_channel_websock_extract_headers(QIOChannelWebsock *= ioc, goto bad_request; } *nl =3D '\0'; + trace_qio_channel_websock_http_greeting(ioc, buffer); =20 tmp =3D strchr(buffer, ' '); if (!tmp) { @@ -425,6 +426,9 @@ static void qio_channel_websock_handshake_process(QIOCh= annelWebsock *ioc, goto bad_request; } =20 + trace_qio_channel_websock_http_request(ioc, protocols, version, + host, connection, upgrade, key); + if (!g_strrstr(protocols, QIO_CHANNEL_WEBSOCK_PROTOCOL_BINARY)) { error_setg(errp, "No '%s' protocol is supported by client '%s'", QIO_CHANNEL_WEBSOCK_PROTOCOL_BINARY, protocols); diff --git a/io/trace-events b/io/trace-events index 801b5dcb61..f70bad7cbe 100644 --- a/io/trace-events +++ b/io/trace-events @@ -48,6 +48,8 @@ qio_channel_websock_handshake_pending(void *ioc, int stat= us) "Websock handshake qio_channel_websock_handshake_reply(void *ioc) "Websock handshake reply io= c=3D%p" qio_channel_websock_handshake_fail(void *ioc, const char *msg) "Websock ha= ndshake fail ioc=3D%p err=3D%s" qio_channel_websock_handshake_complete(void *ioc) "Websock handshake compl= ete ioc=3D%p" +qio_channel_websock_http_greeting(void *ioc, const char *greeting) "Websoc= ket HTTP request ioc=3D%p greeting=3D'%s'" +qio_channel_websock_http_request(void *ioc, const char *protocols, const c= har *version, const char *host, const char *connection, const char *upgrade= , const char *key) "Websocket HTTP request ioc=3D%p protocols=3D'%s' versio= n=3D'%s' host=3D'%s' connection=3D'%s' upgrade=3D'%s' key=3D'%s'" qio_channel_websock_header_partial_decode(void *ioc, size_t payloadlen, un= signed char fin, unsigned char opcode, unsigned char has_mask) "Websocket h= eader decoded ioc=3D%p payload-len=3D%zu fin=3D0x%x opcode=3D0x%x has_mask= =3D0x%x" qio_channel_websock_header_full_decode(void *ioc, size_t headerlen, size_t= payloadlen, uint32_t mask) "Websocket header decoded ioc=3D%p header-len= =3D%zu payload-len=3D%zu mask=3D0x%x" qio_channel_websock_payload_decode(void *ioc, uint8_t opcode, size_t paylo= ad_remain) "Websocket header decoded ioc=3D%p opcode=3D0x%x payload-remain= =3D%zu" --=20 2.13.5 From nobody Mon Apr 29 07:21:02 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.zohomail.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 (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1508185560660730.777508343376; Mon, 16 Oct 2017 13:26:00 -0700 (PDT) Received: from localhost ([::1]:35068 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e4BxN-0003KX-Op for importer@patchew.org; Mon, 16 Oct 2017 16:25:49 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44558) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e4BpA-0004mY-Vb for qemu-devel@nongnu.org; Mon, 16 Oct 2017 16:17:23 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1e4BpA-0000NL-48 for qemu-devel@nongnu.org; Mon, 16 Oct 2017 16:17:20 -0400 Received: from mx1.redhat.com ([209.132.183.28]:21459) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1e4Bp9-0000Mx-UO for qemu-devel@nongnu.org; Mon, 16 Oct 2017 16:17:20 -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 1B4F84E8AC; Mon, 16 Oct 2017 20:17:19 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.33.36.3]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6DAC25D6B2; Mon, 16 Oct 2017 20:17:16 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 1B4F84E8AC Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=berrange@redhat.com From: "Daniel P. Berrange" To: qemu-devel@nongnu.org Date: Mon, 16 Oct 2017 21:16:50 +0100 Message-Id: <20171016201650.18399-12-berrange@redhat.com> In-Reply-To: <20171016201650.18399-1-berrange@redhat.com> References: <20171016201650.18399-1-berrange@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.38]); Mon, 16 Oct 2017 20:17:19 +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] [PULL v1 11/11] io: fix mem leak in websock error path 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: Peter Maydell 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" Coverity pointed out the 'date' is not free()d in the error path Reviewed-by: Eric Blake Signed-off-by: Daniel P. Berrange --- io/channel-websock.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/io/channel-websock.c b/io/channel-websock.c index aa35ef3274..df2c3a9f99 100644 --- a/io/channel-websock.c +++ b/io/channel-websock.c @@ -341,7 +341,7 @@ static void qio_channel_websock_handshake_send_res_ok(Q= IOChannelWebsock *ioc, char combined_key[QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN + QIO_CHANNEL_WEBSOCK_GUID_LEN + 1]; char *accept =3D NULL; - char *date =3D qio_channel_websock_date_str(); + char *date =3D NULL; =20 g_strlcpy(combined_key, key, QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN + 1); g_strlcat(combined_key, QIO_CHANNEL_WEBSOCK_GUID, @@ -360,6 +360,7 @@ static void qio_channel_websock_handshake_send_res_ok(Q= IOChannelWebsock *ioc, return; } =20 + date =3D qio_channel_websock_date_str(); qio_channel_websock_handshake_send_res( ioc, QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_OK, date, accept); =20 --=20 2.13.5