From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55511) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bMs2r-0008WR-47 for qemu-devel@nongnu.org; Tue, 12 Jul 2016 03:23:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bMs2l-0001Rf-Ah for qemu-devel@nongnu.org; Tue, 12 Jul 2016 03:23:52 -0400 Received: from mx1.redhat.com ([209.132.183.28]:48510) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bMs2l-0001RK-4r for qemu-devel@nongnu.org; Tue, 12 Jul 2016 03:23:47 -0400 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 5F1A060A4 for ; Tue, 12 Jul 2016 07:23:46 +0000 (UTC) From: Gerd Hoffmann Date: Tue, 12 Jul 2016 09:23:43 +0200 Message-Id: <1468308223-2496-4-git-send-email-kraxel@redhat.com> In-Reply-To: <1468308223-2496-1-git-send-email-kraxel@redhat.com> References: <1468308223-2496-1-git-send-email-kraxel@redhat.com> Subject: [Qemu-devel] [PULL 3/3] ui: avoid crash if vnc client disconnects with writes pending List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: "Daniel P. Berrange" , Gerd Hoffmann From: "Daniel P. Berrange" The vnc_client_read() function is called from the vnc_client_io() event handler callback when there is incoming data to process. If it detects that the client has disconnected, then it will trigger cleanup and free'ing of the VncState client struct at a safe time. Unfortunately, the vnc_client_io() event handler will also call vnc_client_write() to handle any outgoing data writes. So if vnc_client_io() was invoked with both G_IO_IN and G_IO_OUT events set, and the client disconnects, we may try to write to a client which has just been freed. https://bugs.launchpad.net/qemu/+bug/1594861 Signed-off-by: Daniel P. Berrange Message-id: 1467042529-3372-1-git-send-email-berrange@redhat.com Signed-off-by: Gerd Hoffmann --- ui/vnc.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/ui/vnc.c b/ui/vnc.c index bd602b6..e3f857c 100644 --- a/ui/vnc.c +++ b/ui/vnc.c @@ -1436,8 +1436,9 @@ static void vnc_jobs_bh(void *opaque) * First function called whenever there is more data to be read from * the client socket. Will delegate actual work according to whether * SASL SSF layers are enabled (thus requiring decryption calls) + * Returns 0 on success, -1 if client disconnected */ -static void vnc_client_read(VncState *vs) +static int vnc_client_read(VncState *vs) { ssize_t ret; @@ -1450,8 +1451,9 @@ static void vnc_client_read(VncState *vs) if (!ret) { if (vs->disconnecting) { vnc_disconnect_finish(vs); + return -1; } - return; + return 0; } while (vs->read_handler && vs->input.offset >= vs->read_handler_expect) { @@ -1461,7 +1463,7 @@ static void vnc_client_read(VncState *vs) ret = vs->read_handler(vs, vs->input.buffer, len); if (vs->disconnecting) { vnc_disconnect_finish(vs); - return; + return -1; } if (!ret) { @@ -1470,6 +1472,7 @@ static void vnc_client_read(VncState *vs) vs->read_handler_expect = ret; } } + return 0; } gboolean vnc_client_io(QIOChannel *ioc G_GNUC_UNUSED, @@ -1477,7 +1480,9 @@ gboolean vnc_client_io(QIOChannel *ioc G_GNUC_UNUSED, { VncState *vs = opaque; if (condition & G_IO_IN) { - vnc_client_read(vs); + if (vnc_client_read(vs) < 0) { + return TRUE; + } } if (condition & G_IO_OUT) { vnc_client_write(vs); -- 1.8.3.1