[Qemu-devel] [PATCH] char: don't skip client cleanup if 'connected' flag is unset

Daniel P. Berrange posted 1 patch 6 years, 6 months ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20171005152856.6166-1-berrange@redhat.com
Test s390x passed
There is a newer version of this series
chardev/char-socket.c | 31 +++++++++++++++++--------------
1 file changed, 17 insertions(+), 14 deletions(-)
[Qemu-devel] [PATCH] char: don't skip client cleanup if 'connected' flag is unset
Posted by Daniel P. Berrange 6 years, 6 months ago
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 <berrange@redhat.com>
---
 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 = SOCKET_CHARDEV(chr);
     int i;
 
-    if (!s->connected) {
-        return;
-    }
-
     if (s->read_msgfds_num) {
         for (i = 0; i < s->read_msgfds_num; i++) {
             close(s->read_msgfds[i]);
@@ -347,10 +343,14 @@ static void tcp_chr_free_connection(Chardev *chr)
 
     tcp_set_msgfds(chr, NULL, 0);
     remove_fd_in_watch(chr);
-    object_unref(OBJECT(s->sioc));
-    s->sioc = NULL;
-    object_unref(OBJECT(s->ioc));
-    s->ioc = NULL;
+    if (s->sioc) {
+        object_unref(OBJECT(s->sioc));
+        s->sioc = NULL;
+    }
+    if (s->ioc) {
+        object_unref(OBJECT(s->ioc));
+        s->ioc = NULL;
+    }
     g_free(chr->filename);
     chr->filename = NULL;
     s->connected = 0;
@@ -394,22 +394,25 @@ static void update_disconnected_filename(SocketChardev *s)
                                          s->is_listen, s->is_telnet);
 }
 
+/* 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 == true
+ */
 static void tcp_chr_disconnect(Chardev *chr)
 {
     SocketChardev *s = SOCKET_CHARDEV(chr);
-
-    if (!s->connected) {
-        return;
-    }
+    bool emitClose = s->connected;
 
     tcp_chr_free_connection(chr);
 
-    if (s->listen_ioc) {
+    if (s->listen_ioc && s->listen_tag == 0) {
         s->listen_tag = 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);
     }
-- 
2.13.5


Re: [Qemu-devel] [PATCH] char: don't skip client cleanup if 'connected' flag is unset
Posted by Eric Blake 6 years, 6 months ago
On 10/05/2017 10:28 AM, Daniel P. Berrange wrote:
> 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 <berrange@redhat.com>
> ---
>  chardev/char-socket.c | 31 +++++++++++++++++--------------
>  1 file changed, 17 insertions(+), 14 deletions(-)
> 

Reviewed-by: Eric Blake <eblake@redhat.com>

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org

Re: [Qemu-devel] [PATCH] char: don't skip client cleanup if 'connected' flag is unset
Posted by Marc-André Lureau 6 years, 6 months ago
Hi

On Thu, Oct 5, 2017 at 5:28 PM, Daniel P. Berrange <berrange@redhat.com> wrote:
> 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 <berrange@redhat.com>
> ---
>  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 = SOCKET_CHARDEV(chr);
>      int i;
>
> -    if (!s->connected) {
> -        return;
> -    }
> -
>      if (s->read_msgfds_num) {
>          for (i = 0; i < s->read_msgfds_num; i++) {
>              close(s->read_msgfds[i]);
> @@ -347,10 +343,14 @@ static void tcp_chr_free_connection(Chardev *chr)
>
>      tcp_set_msgfds(chr, NULL, 0);
>      remove_fd_in_watch(chr);
> -    object_unref(OBJECT(s->sioc));
> -    s->sioc = NULL;
> -    object_unref(OBJECT(s->ioc));
> -    s->ioc = NULL;
> +    if (s->sioc) {
> +        object_unref(OBJECT(s->sioc));
> +        s->sioc = NULL;
> +    }
> +    if (s->ioc) {
> +        object_unref(OBJECT(s->ioc));
> +        s->ioc = NULL;
> +    }

object_unref() and OBJECT() are null-safe.

>      g_free(chr->filename);
>      chr->filename = NULL;
>      s->connected = 0;
> @@ -394,22 +394,25 @@ static void update_disconnected_filename(SocketChardev *s)
>                                           s->is_listen, s->is_telnet);
>  }
>
> +/* 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 == true
> + */
>  static void tcp_chr_disconnect(Chardev *chr)
>  {
>      SocketChardev *s = SOCKET_CHARDEV(chr);
> -
> -    if (!s->connected) {
> -        return;
> -    }
> +    bool emitClose = s->connected;

ewh, which code style do you pick today? :)

>
>      tcp_chr_free_connection(chr);
>
> -    if (s->listen_ioc) {
> +    if (s->listen_ioc && s->listen_tag == 0) {
>          s->listen_tag = 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);
>      }
> --
> 2.13.5
>
>

other than that, looks good to me

-- 
Marc-André Lureau

Re: [Qemu-devel] [PATCH] char: don't skip client cleanup if 'connected' flag is unset
Posted by Daniel P. Berrange 6 years, 6 months ago
On Thu, Oct 05, 2017 at 05:35:39PM +0200, Marc-André Lureau wrote:
> Hi
> 
> On Thu, Oct 5, 2017 at 5:28 PM, Daniel P. Berrange <berrange@redhat.com> wrote:
> > 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 <berrange@redhat.com>
> > ---
> >  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 = SOCKET_CHARDEV(chr);
> >      int i;
> >
> > -    if (!s->connected) {
> > -        return;
> > -    }
> > -
> >      if (s->read_msgfds_num) {
> >          for (i = 0; i < s->read_msgfds_num; i++) {
> >              close(s->read_msgfds[i]);
> > @@ -347,10 +343,14 @@ static void tcp_chr_free_connection(Chardev *chr)
> >
> >      tcp_set_msgfds(chr, NULL, 0);
> >      remove_fd_in_watch(chr);
> > -    object_unref(OBJECT(s->sioc));
> > -    s->sioc = NULL;
> > -    object_unref(OBJECT(s->ioc));
> > -    s->ioc = NULL;
> > +    if (s->sioc) {
> > +        object_unref(OBJECT(s->sioc));
> > +        s->sioc = NULL;
> > +    }
> > +    if (s->ioc) {
> > +        object_unref(OBJECT(s->ioc));
> > +        s->ioc = NULL;
> > +    }
> 
> object_unref() and OBJECT() are null-safe.
> 
> >      g_free(chr->filename);
> >      chr->filename = NULL;
> >      s->connected = 0;
> > @@ -394,22 +394,25 @@ static void update_disconnected_filename(SocketChardev *s)
> >                                           s->is_listen, s->is_telnet);
> >  }
> >
> > +/* 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 == true
> > + */
> >  static void tcp_chr_disconnect(Chardev *chr)
> >  {
> >      SocketChardev *s = SOCKET_CHARDEV(chr);
> > -
> > -    if (!s->connected) {
> > -        return;
> > -    }
> > +    bool emitClose = s->connected;
> 
> ewh, which code style do you pick today? :)

Which bit don't you like ?  The 'bool' or the camelCase, or both ?


Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|

Re: [Qemu-devel] [PATCH] char: don't skip client cleanup if 'connected' flag is unset
Posted by Marc-André Lureau 6 years, 6 months ago
Hi

On Thu, Oct 5, 2017 at 5:40 PM, Daniel P. Berrange <berrange@redhat.com> wrote:
> On Thu, Oct 05, 2017 at 05:35:39PM +0200, Marc-André Lureau wrote:
>> Hi
>>
>> On Thu, Oct 5, 2017 at 5:28 PM, Daniel P. Berrange <berrange@redhat.com> wrote:
>> > 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 <berrange@redhat.com>
>> > ---
>> >  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 = SOCKET_CHARDEV(chr);
>> >      int i;
>> >
>> > -    if (!s->connected) {
>> > -        return;
>> > -    }
>> > -
>> >      if (s->read_msgfds_num) {
>> >          for (i = 0; i < s->read_msgfds_num; i++) {
>> >              close(s->read_msgfds[i]);
>> > @@ -347,10 +343,14 @@ static void tcp_chr_free_connection(Chardev *chr)
>> >
>> >      tcp_set_msgfds(chr, NULL, 0);
>> >      remove_fd_in_watch(chr);
>> > -    object_unref(OBJECT(s->sioc));
>> > -    s->sioc = NULL;
>> > -    object_unref(OBJECT(s->ioc));
>> > -    s->ioc = NULL;
>> > +    if (s->sioc) {
>> > +        object_unref(OBJECT(s->sioc));
>> > +        s->sioc = NULL;
>> > +    }
>> > +    if (s->ioc) {
>> > +        object_unref(OBJECT(s->ioc));
>> > +        s->ioc = NULL;
>> > +    }
>>
>> object_unref() and OBJECT() are null-safe.
>>
>> >      g_free(chr->filename);
>> >      chr->filename = NULL;
>> >      s->connected = 0;
>> > @@ -394,22 +394,25 @@ static void update_disconnected_filename(SocketChardev *s)
>> >                                           s->is_listen, s->is_telnet);
>> >  }
>> >
>> > +/* 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 == true
>> > + */
>> >  static void tcp_chr_disconnect(Chardev *chr)
>> >  {
>> >      SocketChardev *s = SOCKET_CHARDEV(chr);
>> > -
>> > -    if (!s->connected) {
>> > -        return;
>> > -    }
>> > +    bool emitClose = s->connected;
>>
>> ewh, which code style do you pick today? :)
>
> Which bit don't you like ?  The 'bool' or the camelCase, or both ?

the mixedCase, let's stick to lower_case for variables.

>
>
> Regards,
> Daniel
> --
> |: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
> |: https://libvirt.org         -o-            https://fstop138.berrange.com :|
> |: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|



-- 
Marc-André Lureau