vnc: fix resource leak when websocket channel error
PATCH 1/1 vnc: fix resource leak when websocket channel error
When we connect to vnc by websocket channel, and disconnect (maybe by some network exception) before handshake, qemu will left CLOSE_WAIT socket and never close it
After 04d2529da2 ("ui: convert VNC server to use QIOChannelSocket") and dd154c4d9f ("io: fix handling of EOF / error conditions in websock GSource"), the vnc call qio_channel_add_watch only care about G_IO_IN,
but mising G_IO_HUP and G_IO_ERR.
When the websocket channel get EOF or error, it cannot callback, because the caller ignore the event, that leads to resource leak
We need handle G_IO_HUP and G_IO_ERR event, then cleanup the channel
--- ui/vnc-auth-sasl.c | 3 ++- ui/vnc-auth-vencrypt.c | 3 ++- ui/vnc-jobs.c | 3 ++- ui/vnc-ws.c | 20 ++++++++++++++++---- ui/vnc.c | 24 ++++++++++++++++++------ 5 files changed, 40 insertions(+), 13 deletions(-)
ui/vnc-auth-sasl.c +2 -1
@@ -111,7 +111,8 @@ size_t vnc_client_write_sasl(VncState *vs)g_source_remove (vs->ioc_tag); } vs->ioc_tag =qio_channel_add_watch ( - vs->ioc,G_IO_IN , vnc_client_io, vs,NULL ); + vs->ioc,G_IO_IN |G_IO_HUP |G_IO_ERR , + vnc_client_io, vs,NULL ); }return ret;
ui/vnc-auth-vencrypt.c +2 -1
@@ -79,7 +79,8 @@ static void vnc_tls_handshake_done(QIOTask *task,g_source_remove (vs->ioc_tag); } vs->ioc_tag =qio_channel_add_watch ( - vs->ioc,G_IO_IN |G_IO_OUT , vnc_client_io, vs,NULL ); + vs->ioc,G_IO_IN |G_IO_HUP |G_IO_ERR |G_IO_OUT , + vnc_client_io, vs,NULL );start_auth_vencrypt_subauth (vs); } }
ui/vnc-jobs.c +2 -1
@@ -151,7 +151,8 @@ void vnc_jobs_consume_buffer(VncState *vs) }if (vs->disconnecting == FALSE) { vs->ioc_tag =qio_channel_add_watch ( - vs->ioc,G_IO_IN |G_IO_OUT , vnc_client_io, vs,NULL ); + vs->ioc,G_IO_IN |G_IO_HUP |G_IO_ERR |G_IO_OUT , + vnc_client_io, vs,NULL ); } }buffer_move (&vs->output, &vs->jobs_buffer);
ui/vnc-ws.c +16 -4
@@ -41,13 +41,14 @@ static void vncws_tls_handshake_done(QIOTask *task,g_source_remove (vs->ioc_tag); } vs->ioc_tag =qio_channel_add_watch ( -QIO_CHANNEL (vs->ioc),G_IO_IN , vncws_handshake_io, vs,NULL ); +QIO_CHANNEL (vs->ioc),G_IO_IN |G_IO_HUP |G_IO_ERR , + vncws_handshake_io, vs,NULL ); } } gboolean vncws_tls_handshake_io(QIOChannel *ioc G_GNUC_UNUSED, - GIOCondition condition G_GNUC_UNUSED, +GIOCondition condition,void *opaque) {VncState *vs = opaque;
@@ -59,6 +60,11 @@ gboolean vncws_tls_handshake_io(QIOChannel *ioc G_GNUC_UNUSED, vs->ioc_tag =0 ; } +if (condition & (G_IO_HUP |G_IO_ERR )) { +vnc_client_error (vs); +return TRUE; + } + tls = qio_channel_tls_new_server( vs->ioc, vs->vd->tlscreds,
@@ -105,13 +111,14 @@ static void vncws_handshake_done(QIOTask *task,g_source_remove (vs->ioc_tag); } vs->ioc_tag =qio_channel_add_watch ( - vs->ioc,G_IO_IN , vnc_client_io, vs,NULL ); + vs->ioc,G_IO_IN |G_IO_HUP |G_IO_ERR , + vnc_client_io, vs,NULL ); } } gboolean vncws_handshake_io(QIOChannel *ioc G_GNUC_UNUSED, - GIOCondition condition G_GNUC_UNUSED, +GIOCondition condition,void *opaque) {VncState *vs = opaque;
@@ -122,6 +129,11 @@ gboolean vncws_handshake_io(QIOChannel *ioc G_GNUC_UNUSED, vs->ioc_tag =0 ; } +if (condition & (G_IO_HUP |G_IO_ERR )) { +vnc_client_error (vs); +return TRUE; + } + wioc =qio_channel_websock_new_server (vs->ioc);qio_channel_set_name (QIO_CHANNEL (wioc),"vnc-ws-server-websock" );
ui/vnc.c +18 -6
@@ -1398,7 +1398,8 @@ static size_t vnc_client_write_plain(VncState *vs)g_source_remove (vs->ioc_tag); } vs->ioc_tag =qio_channel_add_watch ( - vs->ioc,G_IO_IN , vnc_client_io, vs,NULL ); + vs->ioc,G_IO_IN |G_IO_HUP |G_IO_ERR , + vnc_client_io, vs,NULL ); }return ret;
@@ -1435,7 +1436,8 @@ static void vnc_client_write(VncState *vs)g_source_remove (vs->ioc_tag); } vs->ioc_tag =qio_channel_add_watch ( - vs->ioc,G_IO_IN , vnc_client_io, vs,NULL ); + vs->ioc,G_IO_IN |G_IO_HUP |G_IO_ERR , + vnc_client_io, vs,NULL ); }vnc_unlock_output (vs); }
@@ -1551,6 +1553,12 @@ gboolean vnc_client_io(QIOChannel *ioc G_GNUC_UNUSED,VncState *vs = opaque;assert (vs->magic ==VNC_MAGIC ); + +if (condition & (G_IO_HUP |G_IO_ERR )) { +vnc_disconnect_start (vs); +return TRUE; + } +if (condition &G_IO_IN ) {if (vnc_client_read (vs) <0 ) {/* vs is free()ed here */
@@ -1612,7 +1620,8 @@ void vnc_write(VncState *vs, const void *data, size_t len)g_source_remove (vs->ioc_tag); } vs->ioc_tag =qio_channel_add_watch ( - vs->ioc,G_IO_IN |G_IO_OUT , vnc_client_io, vs,NULL ); + vs->ioc,G_IO_IN |G_IO_HUP |G_IO_ERR |G_IO_OUT , + vnc_client_io, vs,NULL ); }buffer_append (&vs->output, data, len);
@@ -3077,14 +3086,17 @@ static void vnc_connect(VncDisplay *vd, QIOChannelSocket *sioc, vs->websocket =1 ;if (vd->tlscreds) { vs->ioc_tag =qio_channel_add_watch ( - vs->ioc,G_IO_IN , vncws_tls_handshake_io, vs,NULL ); + vs->ioc,G_IO_IN |G_IO_HUP |G_IO_ERR , + vncws_tls_handshake_io, vs,NULL ); }else { vs->ioc_tag =qio_channel_add_watch ( - vs->ioc,G_IO_IN , vncws_handshake_io, vs,NULL ); + vs->ioc,G_IO_IN |G_IO_HUP |G_IO_ERR , + vncws_handshake_io, vs,NULL ); } }else { vs->ioc_tag =qio_channel_add_watch ( - vs->ioc,G_IO_IN , vnc_client_io, vs,NULL ); + vs->ioc,G_IO_IN |G_IO_HUP |G_IO_ERR , + vnc_client_io, vs,NULL ); }vnc_client_cache_addr (vs);
Signature · 1 line
2.17.1
Quoted 3 lines · Ding Hui
When we connect to vnc by websocket channel, and disconnect (maybe by some network exception) before handshake, qemu will left
CLOSE_WAITsocket and never close it
Added to fixes queue.
thanks,
Gerd