Skip to contentSkip to conversation

block/ssh: Avoid segfault if inet_connect doesn't set errno.

PATCH Applied Richard W.M. Jones 3 participants 6 messages Jul 22, 2015 – Jul 28, 2015 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 block/ssh: Avoid segfault if inet_connect doesn't set errno.

v3:
Same as v2 but set the return value to -EIO.

#1 lore raw

PATCH v3 1/1 block/ssh: Avoid segfault if inet_connect doesn't set errno.

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
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(-)
block/ssh.c +1 -1
index aebb18c..8d06739 100644
@@ -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;
     }
 
Signature · 1 line
2.4.3
Reviewed-by on v3 1/1
#2 lore raw
Quoted 54 lines · Richard W.M. Jones

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
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(-)
block/ssh.c +1 -1
index aebb18c..8d06739 100644
@@ -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;
     }
 
Signature · 1 line
2.4.3
#3 lore raw
Quoted 2 lines · Jeff Cody

On Wed, Jul 22, 2015 at 02:27:47PM +0100, Richard W.M. Jones wrote:

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" <[email protected]> (supporter:SSH)
Jeff Cody <[email protected]> (supporter:SSH)
Kevin Wolf <[email protected]> (supporter:Block layer core)
[email protected] (open list:SSH)
#4 lore raw
Quoted 4 lines · Stefan Hajnoczi
Quoted 2 lines · Jeff Cody

On Wed, Jul 22, 2015 at 02:27:47PM +0100, Richard W.M. Jones wrote:

Quoted 8 lines

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" <[email protected]> (supporter:SSH)
Jeff Cody <[email protected]> (supporter:SSH)
Kevin Wolf <[email protected]> (supporter:Block layer core)
[email protected] (open list:SSH)

Yes, I am.

Thanks,
Jeff