From patchwork Wed Aug 19 22:47:39 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chris Webb X-Patchwork-Id: 31673 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by bilbo.ozlabs.org (Postfix) with ESMTPS id 255A1B707B for ; Thu, 20 Aug 2009 08:48:43 +1000 (EST) Received: from localhost ([127.0.0.1]:58382 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MdtxP-0001mY-Q6 for incoming@patchwork.ozlabs.org; Wed, 19 Aug 2009 18:48:39 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1Mdtws-0001mO-G0 for qemu-devel@nongnu.org; Wed, 19 Aug 2009 18:48:06 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1Mdtwq-0001mC-5R for qemu-devel@nongnu.org; Wed, 19 Aug 2009 18:48:04 -0400 Received: from [199.232.76.173] (port=60527 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Mdtwq-0001m8-08 for qemu-devel@nongnu.org; Wed, 19 Aug 2009 18:48:04 -0400 Received: from alpha.arachsys.com ([91.203.57.7]:44871) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1Mdtwp-0002Cb-9c for qemu-devel@nongnu.org; Wed, 19 Aug 2009 18:48:03 -0400 Received: from arachsys.demon.co.uk ([83.104.159.199] helo=miranda.arachsys.com) by alpha.arachsys.com with esmtpa (Exim 4.52) id 1MdtwS-0003oK-R5; Wed, 19 Aug 2009 23:47:41 +0100 Received: from chris by miranda.arachsys.com with local (Exim 4.52) id 1MdtwR-00081X-KF; Wed, 19 Aug 2009 23:47:39 +0100 Date: Wed, 19 Aug 2009 23:47:39 +0100 From: Chris Webb To: Avi Kivity , Gerd Hoffmann Message-ID: <20090819224739.GB17276@arachsys.com> References: <20090812150159.GW5348@arachsys.com> <4A82E200.3040107@redhat.com> <20090812162401.GB8115@arachsys.com> <20090813122333.GA2863@arachsys.com> <4A840A3E.1040400@redhat.com> <20090813124350.GA21678@arachsys.com> <20090813124546.GB21678@arachsys.com> <4A840DE0.2060202@redhat.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <4A840DE0.2060202@redhat.com> User-Agent: Mutt/1.5.19 (2009-01-05) X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 3) Cc: qemu-devel@nongnu.org, kvm@vger.kernel.org Subject: [Qemu-devel] Re: qemu-kvm segfaults in qemu_del_timer (0.10.5 and 0.10.6) X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Avi Kivity writes: > master branch has a patch that fixes a use-after-free when > disconnecting. Unfortunately it doesn't port cleanly to stable-0.10. I've collected quite a few more core dumps from segfaults of client virtual machines now, all of which are VNC related and could quite plausibly be use of a VncState after it has been freed. I looked at Gerd's patch [198a00: vnc: rework VncState release workflow] and have taken a stab at the equivalent patch for stable qemu & qemu-kvm 0.10. With the following applied, VNC connections and disconnections still work correctly, so it doesn't horribly break anything, but I can't immediately confirm whether it will cure the rare segfaults as I haven't yet found a rapid way of reproducing the crashes other than by waiting for one. diff --git a/vnc.c b/vnc.c --- a/vnc.c +++ b/vnc.c @@ -200,6 +200,8 @@ static void vnc_write_u8(VncState *vs, uint8_t value); static void vnc_flush(VncState *vs); static void vnc_update_client(void *opaque); +static void vnc_disconnect_start(VncState *vs); +static void vnc_disconnect_finish(VncState *vs); static void vnc_client_read(void *opaque); static void vnc_colordepth(VncState *vs); @@ -633,8 +635,6 @@ static void vnc_copy(VncState *vs, int src_x, int src_y, int dst_x, int dst_y, int w, int h) { - vnc_update_client(vs); - vnc_write_u8(vs, 0); /* msg id */ vnc_write_u8(vs, 0); vnc_write_u16(vs, 1); /* number of rects */ @@ -647,13 +647,21 @@ static void vnc_dpy_copy(DisplayState *ds, int src_x, int src_y, int dst_x, int dst_y, int w, int h) { VncDisplay *vd = ds->opaque; - VncState *vs = vd->clients; - while (vs != NULL) { + VncState *vs, *vn; + + for (vs = vd->clients; vs != NULL; vs = vn) { + vn = vs->next; + if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) { + vnc_update_client(vs); + /* vs might be free()ed here */ + } + } + + for (vs = vd->clients; vs != NULL; vs = vs->next) { if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) vnc_copy(vs, src_x, src_y, dst_x, dst_y, w, h); else /* TODO */ vnc_update(vs, dst_x, dst_y, w, h); - vs = vs->next; } } @@ -763,6 +771,8 @@ if (vs->csock != -1) { qemu_mod_timer(vs->timer, qemu_get_clock(rt_clock) + VNC_REFRESH_INTERVAL); + } else { + vnc_disconnect_finish(vs); } } @@ -832,6 +842,47 @@ } } +static void vnc_disconnect_start(VncState *vs) +{ + if (vs->csock == -1) + return; + qemu_set_fd_handler2(vs->csock, NULL, NULL, NULL, NULL); + closesocket(vs->csock); + vs->csock = -1; +} + +static void vnc_disconnect_finish(VncState *vs) +{ + qemu_del_timer(vs->timer); + qemu_free_timer(vs->timer); + if (vs->input.buffer) qemu_free(vs->input.buffer); + if (vs->output.buffer) qemu_free(vs->output.buffer); +#ifdef CONFIG_VNC_TLS + if (vs->tls_session) { + gnutls_deinit(vs->tls_session); + vs->tls_session = NULL; + } +#endif /* CONFIG_VNC_TLS */ + audio_del(vs); + + VncState *p, *parent = NULL; + for (p = vs->vd->clients; p != NULL; p = p->next) { + if (p == vs) { + if (parent) + parent->next = p->next; + else + vs->vd->clients = p->next; + break; + } + parent = p; + } + if (!vs->vd->clients) + dcl->idle = 1; + + qemu_free(vs->old_data); + qemu_free(vs); +} + static int vnc_client_io_error(VncState *vs, int ret, int last_errno) { if (ret == 0 || ret == -1) { @@ -849,36 +900,7 @@ } VNC_DEBUG("Closing down client sock %d %d\n", ret, ret < 0 ? last_errno : 0); - qemu_set_fd_handler2(vs->csock, NULL, NULL, NULL, NULL); - closesocket(vs->csock); - qemu_del_timer(vs->timer); - qemu_free_timer(vs->timer); - if (vs->input.buffer) qemu_free(vs->input.buffer); - if (vs->output.buffer) qemu_free(vs->output.buffer); -#ifdef CONFIG_VNC_TLS - if (vs->tls_session) { - gnutls_deinit(vs->tls_session); - vs->tls_session = NULL; - } -#endif /* CONFIG_VNC_TLS */ - audio_del(vs); - - VncState *p, *parent = NULL; - for (p = vs->vd->clients; p != NULL; p = p->next) { - if (p == vs) { - if (parent) - parent->next = p->next; - else - vs->vd->clients = p->next; - break; - } - parent = p; - } - if (!vs->vd->clients) - dcl->idle = 1; - - qemu_free(vs->old_data); - qemu_free(vs); + vnc_disconnect_start(vs); return 0; } @@ -887,7 +909,8 @@ static void vnc_client_error(VncState *vs) { - vnc_client_io_error(vs, -1, EINVAL); + VNC_DEBUG("Closing down client sock: protocol error\n"); + vnc_disconnect_start(vs); } static void vnc_client_write(void *opaque) @@ -947,8 +970,11 @@ #endif /* CONFIG_VNC_TLS */ ret = recv(vs->csock, buffer_end(&vs->input), 4096, 0); ret = vnc_client_io_error(vs, ret, socket_error()); - if (!ret) + if (!ret) { + if (vs->csock == -1) + vnc_disconnect_finish(vs); return; + } vs->input.offset += ret; @@ -957,8 +983,10 @@ int ret; ret = vs->read_handler(vs, vs->input.buffer, len); - if (vs->csock == -1) + if (vs->csock == -1) { + vnc_disconnect_finish(vs); return; + } if (!ret) { memmove(vs->input.buffer, vs->input.buffer + len, (vs->input.offset - len)); @@ -973,7 +1001,7 @@ { buffer_reserve(&vs->output, len); - if (buffer_empty(&vs->output)) { + if (vs->csock != -1 && buffer_empty(&vs->output)) { qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, vnc_client_write, vs); } @@ -1014,7 +1042,7 @@ static void vnc_flush(VncState *vs) { - if (vs->output.offset) + if (vs->csock != -1 && vs->output.offset) vnc_client_write(vs); } @@ -2282,11 +2310,13 @@ vnc_read_when(vs, protocol_version, 12); memset(vs->old_data, 0, ds_get_linesize(vs->ds) * ds_get_height(vs->ds)); memset(vs->dirty_row, 0xFF, sizeof(vs->dirty_row)); - vnc_update_client(vs); reset_keys(vs); vs->next = vd->clients; vd->clients = vs; + + vnc_update_client(vs); + /* vs might be free()ed here */ } static void vnc_listen_read(void *opaque)