[Qemu-devel] [PATCH 2/2] io: fix qio_channel_socket_accept err handling

Peter Xu posted 2 patches 8 years, 6 months ago
There is a newer version of this series
[Qemu-devel] [PATCH 2/2] io: fix qio_channel_socket_accept err handling
Posted by Peter Xu 8 years, 6 months ago
When accept failed, we should setup errp with the reason. More
importantly, the caller may assume errp be non-NULL when error happens,
and not setting the errp may crash QEMU.

At the same time, move the trace_qio_channel_socket_accept_fail() after
the if check on EINTR. Two reasons:

1. when EINTR happened, it's not really a fault (we should just try
   again), so we should not log with an "accept failure".

2. trace_*() functions may overwrite errno, then the old errno will be
   missing. We need to either check errno before trace_*() calls, or
   reserve the errno.

Signed-off-by: Peter Xu <peterx@redhat.com>
---
 io/channel-socket.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/io/channel-socket.c b/io/channel-socket.c
index 53386b7..442f230 100644
--- a/io/channel-socket.c
+++ b/io/channel-socket.c
@@ -340,10 +340,11 @@ qio_channel_socket_accept(QIOChannelSocket *ioc,
     cioc->fd = qemu_accept(ioc->fd, (struct sockaddr *)&cioc->remoteAddr,
                            &cioc->remoteAddrLen);
     if (cioc->fd < 0) {
-        trace_qio_channel_socket_accept_fail(ioc);
         if (errno == EINTR) {
             goto retry;
         }
+        trace_qio_channel_socket_accept_fail(ioc);
+        error_setg_errno(errp, errno, "Unable to accept connection");
         goto error;
     }
 
-- 
2.7.4


Re: [Qemu-devel] [PATCH 2/2] io: fix qio_channel_socket_accept err handling
Posted by Juan Quintela 8 years, 6 months ago
Peter Xu <peterx@redhat.com> wrote:
> When accept failed, we should setup errp with the reason. More
> importantly, the caller may assume errp be non-NULL when error happens,
> and not setting the errp may crash QEMU.
>
> At the same time, move the trace_qio_channel_socket_accept_fail() after
> the if check on EINTR. Two reasons:
>
> 1. when EINTR happened, it's not really a fault (we should just try
>    again), so we should not log with an "accept failure".
>
> 2. trace_*() functions may overwrite errno, then the old errno will be
>    missing. We need to either check errno before trace_*() calls, or
>    reserve the errno.
>
> Signed-off-by: Peter Xu <peterx@redhat.com>

Reviewed-by: Juan Quintela <quintela@redhat.com>

Re: [Qemu-devel] [PATCH 2/2] io: fix qio_channel_socket_accept err handling
Posted by Daniel P. Berrange 8 years, 6 months ago
On Wed, Aug 02, 2017 at 11:25:21AM +0800, Peter Xu wrote:
> When accept failed, we should setup errp with the reason. More
> importantly, the caller may assume errp be non-NULL when error happens,
> and not setting the errp may crash QEMU.
> 
> At the same time, move the trace_qio_channel_socket_accept_fail() after
> the if check on EINTR. Two reasons:
> 
> 1. when EINTR happened, it's not really a fault (we should just try
>    again), so we should not log with an "accept failure".
> 
> 2. trace_*() functions may overwrite errno, then the old errno will be
>    missing. We need to either check errno before trace_*() calls, or
>    reserve the errno.
> 
> Signed-off-by: Peter Xu <peterx@redhat.com>
> ---
>  io/channel-socket.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/io/channel-socket.c b/io/channel-socket.c
> index 53386b7..442f230 100644
> --- a/io/channel-socket.c
> +++ b/io/channel-socket.c
> @@ -340,10 +340,11 @@ qio_channel_socket_accept(QIOChannelSocket *ioc,
>      cioc->fd = qemu_accept(ioc->fd, (struct sockaddr *)&cioc->remoteAddr,
>                             &cioc->remoteAddrLen);
>      if (cioc->fd < 0) {
> -        trace_qio_channel_socket_accept_fail(ioc);
>          if (errno == EINTR) {
>              goto retry;
>          }
> +        trace_qio_channel_socket_accept_fail(ioc);
> +        error_setg_errno(errp, errno, "Unable to accept connection");

Err, you're still clobbering errno in trace_qio_channel_socket_accept_fail
before calling error_setg_errno


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 2/2] io: fix qio_channel_socket_accept err handling
Posted by Peter Xu 8 years, 6 months ago
On Wed, Aug 02, 2017 at 10:30:20AM +0100, Daniel P. Berrange wrote:
> On Wed, Aug 02, 2017 at 11:25:21AM +0800, Peter Xu wrote:
> > When accept failed, we should setup errp with the reason. More
> > importantly, the caller may assume errp be non-NULL when error happens,
> > and not setting the errp may crash QEMU.
> > 
> > At the same time, move the trace_qio_channel_socket_accept_fail() after
> > the if check on EINTR. Two reasons:
> > 
> > 1. when EINTR happened, it's not really a fault (we should just try
> >    again), so we should not log with an "accept failure".
> > 
> > 2. trace_*() functions may overwrite errno, then the old errno will be
> >    missing. We need to either check errno before trace_*() calls, or
> >    reserve the errno.
> > 
> > Signed-off-by: Peter Xu <peterx@redhat.com>
> > ---
> >  io/channel-socket.c | 3 ++-
> >  1 file changed, 2 insertions(+), 1 deletion(-)
> > 
> > diff --git a/io/channel-socket.c b/io/channel-socket.c
> > index 53386b7..442f230 100644
> > --- a/io/channel-socket.c
> > +++ b/io/channel-socket.c
> > @@ -340,10 +340,11 @@ qio_channel_socket_accept(QIOChannelSocket *ioc,
> >      cioc->fd = qemu_accept(ioc->fd, (struct sockaddr *)&cioc->remoteAddr,
> >                             &cioc->remoteAddrLen);
> >      if (cioc->fd < 0) {
> > -        trace_qio_channel_socket_accept_fail(ioc);
> >          if (errno == EINTR) {
> >              goto retry;
> >          }
> > +        trace_qio_channel_socket_accept_fail(ioc);
> > +        error_setg_errno(errp, errno, "Unable to accept connection");
> 
> Err, you're still clobbering errno in trace_qio_channel_socket_accept_fail
> before calling error_setg_errno

Oops! I'll do a quick respin.  Thanks for pointing out.

-- 
Peter Xu