From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37946) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZHtnf-0006HF-Cx for qemu-devel@nongnu.org; Wed, 22 Jul 2015 09:11:10 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZHtnZ-00016G-Hk for qemu-devel@nongnu.org; Wed, 22 Jul 2015 09:11:07 -0400 Date: Wed, 22 Jul 2015 14:10:51 +0100 From: "Daniel P. Berrange" Message-ID: <20150722131050.GK12010@redhat.com> References: <1437570442-30203-1-git-send-email-rjones@redhat.com> <1437570442-30203-2-git-send-email-rjones@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <1437570442-30203-2-git-send-email-rjones@redhat.com> Subject: Re: [Qemu-devel] [PATCH v2] block/ssh: Avoid segfault if inet_connect doesn't set errno. Reply-To: "Daniel P. Berrange" List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: "Richard W.M. Jones" Cc: kwolf@redhat.com, jcody@redhat.com, qemu-devel@nongnu.org, qemu-block@nongnu.org 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 > 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. Regards, Daniel -- |: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|