* [Qemu-devel] [PATCH v3] block/ssh: Avoid segfault if inet_connect doesn't set errno. @ 2015-07-22 13:27 Richard W.M. Jones 2015-07-22 13:27 ` Richard W.M. Jones 2015-07-28 4:24 ` [Qemu-devel] " Jeff Cody 0 siblings, 2 replies; 6+ messages in thread From: Richard W.M. Jones @ 2015-07-22 13:27 UTC (permalink / raw) To: qemu-devel; +Cc: kwolf, jcody, qemu-block v3: Same as v2 but set the return value to -EIO. ^ permalink raw reply [flat|nested] 6+ messages in thread
* [Qemu-devel] [PATCH v3] block/ssh: Avoid segfault if inet_connect doesn't set errno. 2015-07-22 13:27 [Qemu-devel] [PATCH v3] block/ssh: Avoid segfault if inet_connect doesn't set errno Richard W.M. Jones @ 2015-07-22 13:27 ` Richard W.M. Jones 2015-07-22 13:56 ` Jeff Cody 2015-07-28 4:24 ` [Qemu-devel] " Jeff Cody 1 sibling, 1 reply; 6+ messages in thread From: Richard W.M. Jones @ 2015-07-22 13:27 UTC (permalink / raw) To: qemu-devel; +Cc: kwolf, jcody, qemu-block 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 -EIO (same as block/nbd.c and block/sheepdog.c). 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..8d06739 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 = -EIO; goto err; } -- 2.4.3 ^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [Qemu-devel] [PATCH v3] block/ssh: Avoid segfault if inet_connect doesn't set errno. 2015-07-22 13:27 ` Richard W.M. Jones @ 2015-07-22 13:56 ` Jeff Cody 2015-07-24 15:08 ` [Qemu-devel] [Qemu-block] " Stefan Hajnoczi 0 siblings, 1 reply; 6+ messages in thread From: Jeff Cody @ 2015-07-22 13:56 UTC (permalink / raw) To: Richard W.M. Jones; +Cc: kwolf, qemu-devel, qemu-block On Wed, Jul 22, 2015 at 02:27:47PM +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 -EIO (same as block/nbd.c and > block/sheepdog.c). 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..8d06739 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 = -EIO; > goto err; > } > > -- > 2.4.3 > Reviewed-by: Jeff Cody <jcody@redhat.com> ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Qemu-devel] [Qemu-block] [PATCH v3] block/ssh: Avoid segfault if inet_connect doesn't set errno. 2015-07-22 13:56 ` Jeff Cody @ 2015-07-24 15:08 ` Stefan Hajnoczi 2015-07-24 15:14 ` Jeff Cody 0 siblings, 1 reply; 6+ messages in thread From: Stefan Hajnoczi @ 2015-07-24 15:08 UTC (permalink / raw) To: Jeff Cody; +Cc: kwolf, Richard W.M. Jones, qemu-block, qemu-devel [-- Attachment #1: Type: text/plain, Size: 495 bytes --] On Wed, Jul 22, 2015 at 09:56:41AM -0400, Jeff Cody wrote: > On Wed, Jul 22, 2015 at 02:27:47PM +0100, Richard W.M. Jones wrote: > Reviewed-by: Jeff Cody <jcody@redhat.com> Jeff: Are you taking this through your tree like gluster, rbd, sheepdog, etc? $ scripts/get_maintainer.pl -f block/ssh.c "Richard W.M. Jones" <rjones@redhat.com> (supporter:SSH) Jeff Cody <jcody@redhat.com> (supporter:SSH) Kevin Wolf <kwolf@redhat.com> (supporter:Block layer core) qemu-block@nongnu.org (open list:SSH) [-- Attachment #2: Type: application/pgp-signature, Size: 473 bytes --] ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Qemu-devel] [Qemu-block] [PATCH v3] block/ssh: Avoid segfault if inet_connect doesn't set errno. 2015-07-24 15:08 ` [Qemu-devel] [Qemu-block] " Stefan Hajnoczi @ 2015-07-24 15:14 ` Jeff Cody 0 siblings, 0 replies; 6+ messages in thread From: Jeff Cody @ 2015-07-24 15:14 UTC (permalink / raw) To: Stefan Hajnoczi; +Cc: kwolf, Richard W.M. Jones, qemu-block, qemu-devel On Fri, Jul 24, 2015 at 04:08:57PM +0100, Stefan Hajnoczi wrote: > On Wed, Jul 22, 2015 at 09:56:41AM -0400, Jeff Cody wrote: > > On Wed, Jul 22, 2015 at 02:27:47PM +0100, Richard W.M. Jones wrote: > > Reviewed-by: Jeff Cody <jcody@redhat.com> > > Jeff: Are you taking this through your tree like gluster, rbd, sheepdog, > etc? > > $ scripts/get_maintainer.pl -f block/ssh.c > "Richard W.M. Jones" <rjones@redhat.com> (supporter:SSH) > Jeff Cody <jcody@redhat.com> (supporter:SSH) > Kevin Wolf <kwolf@redhat.com> (supporter:Block layer core) > qemu-block@nongnu.org (open list:SSH) Yes, I am. Thanks, Jeff ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Qemu-devel] [PATCH v3] block/ssh: Avoid segfault if inet_connect doesn't set errno. 2015-07-22 13:27 [Qemu-devel] [PATCH v3] block/ssh: Avoid segfault if inet_connect doesn't set errno Richard W.M. Jones 2015-07-22 13:27 ` Richard W.M. Jones @ 2015-07-28 4:24 ` Jeff Cody 1 sibling, 0 replies; 6+ messages in thread From: Jeff Cody @ 2015-07-28 4:24 UTC (permalink / raw) To: Richard W.M. Jones; +Cc: kwolf, qemu-devel, qemu-block On Wed, Jul 22, 2015 at 02:27:46PM +0100, Richard W.M. Jones wrote: > v3: > Same as v2 but set the return value to -EIO. > Thanks, applied to my block tree: https://github.com/codyprime/qemu-kvm-jtc/tree/block Thanks, Jeff ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2015-07-28 4:24 UTC | newest] Thread overview: 6+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2015-07-22 13:27 [Qemu-devel] [PATCH v3] block/ssh: Avoid segfault if inet_connect doesn't set errno Richard W.M. Jones 2015-07-22 13:27 ` Richard W.M. Jones 2015-07-22 13:56 ` Jeff Cody 2015-07-24 15:08 ` [Qemu-devel] [Qemu-block] " Stefan Hajnoczi 2015-07-24 15:14 ` Jeff Cody 2015-07-28 4:24 ` [Qemu-devel] " Jeff Cody
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).