From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:42433) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZXWtv-00046E-FO for qemu-devel@nongnu.org; Thu, 03 Sep 2015 11:58:12 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZXWts-0007U8-6Y for qemu-devel@nongnu.org; Thu, 03 Sep 2015 11:58:11 -0400 Received: from mx1.redhat.com ([209.132.183.28]:51478) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZXWtr-0007Ts-Ua for qemu-devel@nongnu.org; Thu, 03 Sep 2015 11:58:08 -0400 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by mx1.redhat.com (Postfix) with ESMTPS id 79C73A19B6 for ; Thu, 3 Sep 2015 15:58:07 +0000 (UTC) From: "Daniel P. Berrange" Date: Thu, 3 Sep 2015 16:39:03 +0100 Message-Id: <1441294768-8712-22-git-send-email-berrange@redhat.com> In-Reply-To: <1441294768-8712-1-git-send-email-berrange@redhat.com> References: <1441294768-8712-1-git-send-email-berrange@redhat.com> Subject: [Qemu-devel] [PATCH FYI 21/46] char: don't assume telnet initialization will not block List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Juan Quintela , "Dr. David Alan Gilbert" , Gerd Hoffmann , Amit Shah , Paolo Bonzini The current code for doing telnet initialization is writing to a socket without checking the return status. While it is highly unlikely to be a problem when writing to a bare socket, as the buffers are large enough to prevent blocking, this cannot be assumed safe with TLS sockets. So write the telnet initialization code into a memory buffer and then use an I/O watch to fully send the data. Signed-off-by: Daniel P. Berrange --- qemu-char.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 67 insertions(+), 18 deletions(-) diff --git a/qemu-char.c b/qemu-char.c index f2724b9..fe75f09 100644 --- a/qemu-char.c +++ b/qemu-char.c @@ -2769,19 +2769,68 @@ static void tcp_chr_update_read_handler(CharDriverState *chr) } } -#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c; -static void tcp_chr_telnet_init(QIOChannel *ioc) +typedef struct { + CharDriverState *chr; + char buf[12]; + size_t buflen; +} TCPCharDriverTelnetInit; + +static gboolean tcp_chr_telnet_init_io(QIOChannel *ioc, + GIOCondition cond G_GNUC_UNUSED, + gpointer user_data) { - char buf[3]; - /* Send the telnet negotion to put telnet in binary, no echo, single char mode */ - IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */ - qio_channel_write(ioc, buf, 3, NULL); - IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */ - qio_channel_write(ioc, buf, 3, NULL); - IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */ - qio_channel_write(ioc, buf, 3, NULL); - IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */ - qio_channel_write(ioc, buf, 3, NULL); + TCPCharDriverTelnetInit *init = user_data; + ssize_t ret; + + ret = qio_channel_write(ioc, init->buf, init->buflen, NULL); + if (ret < 0) { + if (ret == QIO_CHANNEL_ERR_BLOCK) { + ret = 0; + } else { + tcp_chr_disconnect(init->chr); + return FALSE; + } + } + init->buflen -= ret; + + if (init->buflen == 0) { + tcp_chr_connect(init->chr); + return FALSE; + } + + return TRUE; +} + +static void tcp_chr_telnet_init(CharDriverState *chr) +{ + TCPCharDriver *s = chr->opaque; + TCPCharDriverTelnetInit *init = + g_new0(TCPCharDriverTelnetInit, 1); + size_t n = 0; + + init->chr = chr; + init->buflen = 12; + +#define IACSET(x, a, b, c) \ + do { \ + x[n++] = a; \ + x[n++] = b; \ + x[n++] = c; \ + } while (0) + + /* Prep the telnet negotion to put telnet in binary, + * no echo, single char mode */ + IACSET(init->buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */ + IACSET(init->buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */ + IACSET(init->buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */ + IACSET(init->buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */ + +#undef IACSET + + qio_channel_add_watch( + s->ioc, G_IO_OUT, + tcp_chr_telnet_init_io, + init, NULL); } static int tcp_chr_new_client(CharDriverState *chr, QIOChannelSocket *sioc) @@ -2802,7 +2851,12 @@ static int tcp_chr_new_client(CharDriverState *chr, QIOChannelSocket *sioc) g_source_remove(s->listen_tag); s->listen_tag = 0; } - tcp_chr_connect(chr); + + if (s->do_telnetopt) { + tcp_chr_telnet_init(chr); + } else { + tcp_chr_connect(chr); + } return 0; } @@ -2827,7 +2881,6 @@ static gboolean tcp_chr_accept(QIOChannel *channel, void *opaque) { CharDriverState *chr = opaque; - TCPCharDriver *s = chr->opaque; QIOChannelSocket *sioc; sioc = qio_channel_socket_accept(QIO_CHANNEL_SOCKET(channel), @@ -2836,10 +2889,6 @@ static gboolean tcp_chr_accept(QIOChannel *channel, return TRUE; } - if (s->do_telnetopt) { - tcp_chr_telnet_init(QIO_CHANNEL(sioc)); - } - tcp_chr_new_client(chr, sioc); object_unref(OBJECT(sioc)); -- 2.4.3