qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Kevin Wolf <kwolf@redhat.com>
To: "Daniel P. Berrange" <berrange@redhat.com>
Cc: jcody@redhat.com, "Richard W.M. Jones" <rjones@redhat.com>,
	qemu-block@nongnu.org, qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [PATCH v2] block/ssh: Avoid segfault if inet_connect doesn't set errno.
Date: Wed, 22 Jul 2015 15:24:16 +0200	[thread overview]
Message-ID: <20150722132416.GA3349@noname.redhat.com> (raw)
In-Reply-To: <20150722131050.GK12010@redhat.com>

Am 22.07.2015 um 15:10 hat Daniel P. Berrange geschrieben:
> On Wed, Jul 22, 2015 at 02:07:22PM +0100, Richard W.M. Jones wrote:
> > On some (but not all) systems:
> > 
> >   $ qemu-img create -f qcow2 overlay -b ssh://xen/
> >   Segmentation fault
> > 
> > It turns out this happens when inet_connect returns -1 in the
> > following code, but errno == 0.
> > 
> >   s->sock = inet_connect(s->hostport, errp);
> >   if (s->sock < 0) {
> >       ret = -errno;
> >       goto err;
> >   }
> > 
> > In the test case above, no host called "xen" exists, so getaddrinfo fails.
> > 
> > On Fedora 22, getaddrinfo happens to set errno = ENOENT (although it
> > is *not* documented to do that), so it doesn't segfault.
> > 
> > On RHEL 7, errno is not set by the failing getaddrinfo, so ret =
> > -errno = 0, so the caller doesn't know there was an error and
> > continues with a half-initialized BDRVSSHState struct, and everything
> > goes south from there, eventually resulting in a segfault.
> > 
> > Fix this by setting ret to -EINVAL.  The real error is saved in the
> > Error** errp struct, so it is printed correctly:
> > 
> >   $ ./qemu-img create -f qcow2 overlay -b ssh://xen/
> >   qemu-img: overlay: address resolution failed for xen:22: No address associated with hostname
> > 
> > Signed-off-by: Richard W.M. Jones <rjones@redhat.com>
> > Reported-by: Jun Li
> > BZ: https://bugzilla.redhat.com/show_bug.cgi?id=1147343
> > ---
> >  block/ssh.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git a/block/ssh.c b/block/ssh.c
> > index aebb18c..8d4dc2a 100644
> > --- a/block/ssh.c
> > +++ b/block/ssh.c
> > @@ -563,7 +563,7 @@ static int connect_to_ssh(BDRVSSHState *s, QDict *options,
> >      /* Open the socket and connect. */
> >      s->sock = inet_connect(s->hostport, errp);
> >      if (s->sock < 0) {
> > -        ret = -errno;
> > +        ret = -EINVAL;
> >          goto err;
> 
> There are a reasonable number of other uses of inet_connect() in QEMU,
> so can't we fix inet_connect() itself to set EINVAL in the error case
> instead of just fixing one caller.

None of the other callers try to use errno, which isn't even documented
as part of the inet_connect() interface. So I think setting errno inside
inet_connect() would be wrong.

If we were to change anything, we might consider returning -EINVAL
instead of -1, though I'm not sure how useful that change would be. We
would still have to fix this call in the ssh block driver.

Kevin

  parent reply	other threads:[~2015-07-22 13:24 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-22 13:07 [Qemu-devel] [PATCH v2] block/ssh: Avoid segfault if inet_connect doesn't set errno Richard W.M. Jones
2015-07-22 13:07 ` Richard W.M. Jones
2015-07-22 13:10   ` Daniel P. Berrange
2015-07-22 13:17     ` Richard W.M. Jones
2015-07-22 13:24     ` Kevin Wolf [this message]
2015-07-22 13:17   ` Jeff Cody
2015-07-22 13:22     ` Richard W.M. Jones
2015-07-22 13:26       ` Jeff Cody

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150722132416.GA3349@noname.redhat.com \
    --to=kwolf@redhat.com \
    --cc=berrange@redhat.com \
    --cc=jcody@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=rjones@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).