From nobody Thu May 2 11:14:14 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 1507217443719301.03632250197313; Thu, 5 Oct 2017 08:30:43 -0700 (PDT) Received: from localhost ([::1]:40433 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e086a-0004kM-JK for importer@patchew.org; Thu, 05 Oct 2017 11:30:32 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56936) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e085H-000429-JP for qemu-devel@nongnu.org; Thu, 05 Oct 2017 11:29:12 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1e085E-0007Q8-AY for qemu-devel@nongnu.org; Thu, 05 Oct 2017 11:29:11 -0400 Received: from mx1.redhat.com ([209.132.183.28]:38900) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1e085E-0007OY-1c for qemu-devel@nongnu.org; Thu, 05 Oct 2017 11:29:08 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id F342F627EF for ; Thu, 5 Oct 2017 15:29:05 +0000 (UTC) Received: from t460.redhat.com (unknown [10.33.36.71]) by smtp.corp.redhat.com (Postfix) with ESMTP id AF4DF5C54B; Thu, 5 Oct 2017 15:28:59 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com F342F627EF 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: Thu, 5 Oct 2017 16:28:56 +0100 Message-Id: <20171005152856.6166-1-berrange@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Thu, 05 Oct 2017 15:29: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] [PATCH] char: don't skip client cleanup if 'connected' flag is unset 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: Paolo Bonzini , Gerd Hoffmann , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= 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 tcp_chr_free_connection & tcp_chr_disconnect methods both skip all of their cleanup work unless the 's->connected' flag is set. This flag is set when the incoming client connection is ready to use. Crucially this is *after* the TLS handshake has been completed. So if the TLS handshake fails and we try to cleanup the failed client, all the cleanup is skipped as 's->connected' is still false. The only important thing that should be skipped in this case is sending of the CHR_EVENT_CLOSED, because we never got as far as sending the corresponding CHR_EVENT_OPENED. Every other bit of cleanup can be robust against being called even when s->connected is false. Signed-off-by: Daniel P. Berrange Reviewed-by: Eric Blake --- chardev/char-socket.c | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/chardev/char-socket.c b/chardev/char-socket.c index e65148fe97..a34d4d72c2 100644 --- a/chardev/char-socket.c +++ b/chardev/char-socket.c @@ -332,10 +332,6 @@ static void tcp_chr_free_connection(Chardev *chr) SocketChardev *s =3D SOCKET_CHARDEV(chr); int i; =20 - if (!s->connected) { - return; - } - if (s->read_msgfds_num) { for (i =3D 0; i < s->read_msgfds_num; i++) { close(s->read_msgfds[i]); @@ -347,10 +343,14 @@ static void tcp_chr_free_connection(Chardev *chr) =20 tcp_set_msgfds(chr, NULL, 0); remove_fd_in_watch(chr); - object_unref(OBJECT(s->sioc)); - s->sioc =3D NULL; - object_unref(OBJECT(s->ioc)); - s->ioc =3D NULL; + if (s->sioc) { + object_unref(OBJECT(s->sioc)); + s->sioc =3D NULL; + } + if (s->ioc) { + object_unref(OBJECT(s->ioc)); + s->ioc =3D NULL; + } g_free(chr->filename); chr->filename =3D NULL; s->connected =3D 0; @@ -394,22 +394,25 @@ static void update_disconnected_filename(SocketCharde= v *s) s->is_listen, s->is_telnet); } =20 +/* NB may be called even if tcp_chr_connect has not been + * reached, due to TLS or telnet initialization failure, + * so can *not* assume s->connected =3D=3D true + */ static void tcp_chr_disconnect(Chardev *chr) { SocketChardev *s =3D SOCKET_CHARDEV(chr); - - if (!s->connected) { - return; - } + bool emitClose =3D s->connected; =20 tcp_chr_free_connection(chr); =20 - if (s->listen_ioc) { + if (s->listen_ioc && s->listen_tag =3D=3D 0) { s->listen_tag =3D qio_channel_add_watch( QIO_CHANNEL(s->listen_ioc), G_IO_IN, tcp_chr_accept, chr, NULL= ); } update_disconnected_filename(s); - qemu_chr_be_event(chr, CHR_EVENT_CLOSED); + if (emitClose) { + qemu_chr_be_event(chr, CHR_EVENT_CLOSED); + } if (s->reconnect_time) { qemu_chr_socket_restart_timer(chr); } --=20 2.13.5