* [Qemu-devel] [PATCH] block/ssh: Avoid segfault if inet_connect doesn't set errno.
@ 2015-07-22 12:07 Richard W.M. Jones
2015-07-22 13:03 ` Richard W.M. Jones
0 siblings, 1 reply; 2+ messages in thread
From: Richard W.M. Jones @ 2015-07-22 12:07 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 NULL in the
following code, but errno is not set (0).
s->sock = inet_connect(s->hostport, errp);
if (s->sock < 0) {
ret = -errno;
goto err;
}
In the case above, 'xen' doesn't exist 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 == 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.
Fix this by setting errno 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
BZ: https://bugzilla.redhat.com/show_bug.cgi?id=1147343
Signed-off-by: Richard W.M. Jones <rjones@redhat.com>
---
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;
}
--
2.4.3
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [Qemu-devel] [PATCH] block/ssh: Avoid segfault if inet_connect doesn't set errno.
2015-07-22 12:07 [Qemu-devel] [PATCH] block/ssh: Avoid segfault if inet_connect doesn't set errno Richard W.M. Jones
@ 2015-07-22 13:03 ` Richard W.M. Jones
0 siblings, 0 replies; 2+ messages in thread
From: Richard W.M. Jones @ 2015-07-22 13:03 UTC (permalink / raw)
To: qemu-devel; +Cc: kwolf, jcody, qemu-block
On Wed, Jul 22, 2015 at 01:07:53PM +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 NULL in the
> following code, but errno is not set (0).
Bleah .. not NULL, -1. I'm going to send an updated v2
patch which will just fix the message.
Rich.
> s->sock = inet_connect(s->hostport, errp);
> if (s->sock < 0) {
> ret = -errno;
> goto err;
> }
>
> In the case above, 'xen' doesn't exist 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 == 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.
>
> Fix this by setting errno 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
>
> BZ: https://bugzilla.redhat.com/show_bug.cgi?id=1147343
> Signed-off-by: Richard W.M. Jones <rjones@redhat.com>
> ---
> 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;
> }
>
> --
> 2.4.3
>
--
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-df lists disk usage of guests without needing to install any
software inside the virtual machine. Supports Linux and Windows.
http://people.redhat.com/~rjones/virt-df/
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2015-07-22 13:03 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-07-22 12:07 [Qemu-devel] [PATCH] block/ssh: Avoid segfault if inet_connect doesn't set errno Richard W.M. Jones
2015-07-22 13:03 ` Richard W.M. Jones
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).