Skip to contentSkip to conversation

nbd: Don't fail handshake on NBD_OPT_LIST descriptions

PATCH In review Eric Blake 3 participants 8 messages Apr 8, 2016 – Apr 14, 2016

lore
Share & tools
lore

Eric Blake

author
#0 lore raw

PATCH 1/1 nbd: Don't fail handshake on NBD_OPT_LIST descriptions

The NBD Protocol states that NBD_REP_SERVER may set 'length > sizeof(namelen) + namelen'; in which case the rest of the packet is a UTF-8 description of the export. While we don't know of any NBD servers that send this description yet, we had better consume the data so we don't choke when we start to talk to such a server.

Also, a (buggy/malicious) server that replies with length < sizeof(namelen) would cause us to block waiting for bytes that the server is not sending, and one that replies with super-huge lengths could cause us to temporarily allocate up to 4G memory. Sanity check things before blindly reading incorrectly.

---

Yet another case of code introduced in 2.6 that doesn't play nicely with spec-compliant servers...

Hopefully I've squashed them all now?

 nbd/client.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
nbd/client.c +21 -2
index 6777e58..48f2a21 100644
@@ -192,13 +192,18 @@ static int nbd_receive_list(QIOChannel *ioc, char **name, Error **errp)
             return -1;
         }
     } else if (type == NBD_REP_SERVER) {
+        if (len < sizeof(namelen) || len > NBD_MAX_BUFFER_SIZE) {
+            error_setg(errp, "incorrect option length");
+            return -1;
+        }
         if (read_sync(ioc, &namelen, sizeof(namelen)) != sizeof(namelen)) {
             error_setg(errp, "failed to read option name length");
             return -1;
         }
         namelen = be32_to_cpu(namelen);
-        if (len != (namelen + sizeof(namelen))) {
-            error_setg(errp, "incorrect option mame length");
+        len -= sizeof(namelen);
+        if (len < namelen) {
+            error_setg(errp, "incorrect option name length");
             return -1;
         }
         if (namelen > 255) {
@@ -214,6 +219,20 @@ static int nbd_receive_list(QIOChannel *ioc, char **name, Error **errp)
             return -1;
         }
         (*name)[namelen] = '\0';
+        len -= namelen;
+        if (len) {
+            char *buf = g_malloc(len + 1);
+            if (read_sync(ioc, buf, len) != len) {
+                error_setg(errp, "failed to read export description");
+                g_free(*name);
+                g_free(buf);
+                *name = NULL;
+                return -1;
+            }
+            buf[len] = '\0';
+            TRACE("Ignoring export description: %s", buf);
+            g_free(buf);
+        }
     } else {
         error_setg(errp, "Unexpected reply type %x expected %x",
                    type, NBD_REP_SERVER);
Signature · 1 line
2.5.5
Reviewed-by on v1 1/1
#1 lore raw
Quoted 39 lines · Eric Blake

The NBD Protocol states that NBD_REP_SERVER may set 'length > sizeof(namelen) + namelen'; in which case the rest of the packet is a UTF-8 description of the export. While we don't know of any NBD servers that send this description yet, we had better consume the data so we don't choke when we start to talk to such a server.

Also, a (buggy/malicious) server that replies with length < sizeof(namelen) would cause us to block waiting for bytes that the server is not sending, and one that replies with super-huge lengths could cause us to temporarily allocate up to 4G memory. Sanity check things before blindly reading incorrectly.

---

Yet another case of code introduced in 2.6 that doesn't play nicely with spec-compliant servers...

Hopefully I've squashed them all now?

nbd/client.c | 23 +++++++++++++++++++++--

1 file changed, 21 insertions(+), 2 deletions(-)
nbd/client.c +4 -0
index 6777e58..48f2a21 100644
@@ -192,13 +192,18 @@ static int nbd_receive_list(QIOChannel *ioc, char **name, Error **errp)
            return -1;
        }
    } else if (type == NBD_REP_SERVER) {
+        if (len < sizeof(namelen) || len > NBD_MAX_BUFFER_SIZE) {
+            error_setg(errp, "incorrect option length");
+            return -1;
+        }
        if (read_sync(ioc, &namelen, sizeof(namelen)) != sizeof(namelen)) {
            error_setg(errp, "failed to read option name length");
            return -1;

nbd/client.c static int nbd_receive_list(QIOChannel *ioc, char **name, Error **errp) · line 209

Quoted 10 lines
nbd/client.c +3 -2
        }
        namelen = be32_to_cpu(namelen);
-        if (len != (namelen + sizeof(namelen))) {
-            error_setg(errp, "incorrect option mame length");
+        len -= sizeof(namelen);
+        if (len < namelen) {
+            error_setg(errp, "incorrect option name length");
            return -1;
        }
        if (namelen > 255) {

Shouldn't that be 4096? You are after all reading up to NBD_MAX_BUFFER_SIZE (32K) of data just earlier.

Not technically the bug you are trying to fix, so

Quoted 25 lines
nbd/client.c +14 -0
@@ -214,6 +219,20 @@ static int nbd_receive_list(QIOChannel *ioc, char **name, Error **errp)
            return -1;
        }
        (*name)[namelen] = '\0';
+        len -= namelen;
+        if (len) {
+            char *buf = g_malloc(len + 1);
+            if (read_sync(ioc, buf, len) != len) {
+                error_setg(errp, "failed to read export description");
+                g_free(*name);
+                g_free(buf);
+                *name = NULL;
+                return -1;
+            }
+            buf[len] = '\0';
+            TRACE("Ignoring export description: %s", buf);
+            g_free(buf);
+        }
    } else {
        error_setg(errp, "Unexpected reply type %x expected %x",
                   type, NBD_REP_SERVER);
Signature · 1 line
2.5.5
Signature · 1 line
Alex Bligh
author
#2 lore raw
Quoted 19 lines · Alex Bligh
Quoted 15 lines · Eric Blake

The NBD Protocol states that NBD_REP_SERVER may set 'length > sizeof(namelen) + namelen'; in which case the rest of the packet is a UTF-8 description of the export. While we don't know of any NBD servers that send this description yet, we had better consume the data so we don't choke when we start to talk to such a server.

Also, a (buggy/malicious) server that replies with length < sizeof(namelen) would cause us to block waiting for bytes that the server is not sending, and one that replies with super-huge lengths could cause us to temporarily allocate up to 4G memory. Sanity check things before blindly reading incorrectly.

---

Quoted 8 lines

nbd/client.c static int nbd_receive_list(QIOChannel *ioc, char **name, Error **errp) · line 209

Quoted 5 lines
nbd/client.c +2 -0
+        if (len < namelen) {
+            error_setg(errp, "incorrect option name length");
            return -1;
        }
        if (namelen > 255) {

Shouldn't that be 4096? You are after all reading up to NBD_MAX_BUFFER_SIZE (32K) of data just earlier.

NBD_MAX_BUFFER_SIZE is actually 32M, not 32k.

Quoted 1 line

Not technically the bug you are trying to fix, so

And yes, I need to do a much bigger scrub of qemu code, both client and server, to allow export names longer than 255, up to the just-barely-documented 4096 maximum in the NBD protocol. But you are right that such an audit is separate from this immediate fix.

Quoted 2 lines

Thanks.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
author
#3 lore raw
In reply to Eric Blake

[adding qemu-block in cc, since Paolo can't send pull request]

Quoted 71 lines · Eric Blake

The NBD Protocol states that NBD_REP_SERVER may set 'length > sizeof(namelen) + namelen'; in which case the rest of the packet is a UTF-8 description of the export. While we don't know of any NBD servers that send this description yet, we had better consume the data so we don't choke when we start to talk to such a server.

Also, a (buggy/malicious) server that replies with length < sizeof(namelen) would cause us to block waiting for bytes that the server is not sending, and one that replies with super-huge lengths could cause us to temporarily allocate up to 4G memory. Sanity check things before blindly reading incorrectly.

---

Yet another case of code introduced in 2.6 that doesn't play nicely with spec-compliant servers...

Hopefully I've squashed them all now?

 nbd/client.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
nbd/client.c +21 -2
index 6777e58..48f2a21 100644
@@ -192,13 +192,18 @@ static int nbd_receive_list(QIOChannel *ioc, char **name, Error **errp)
             return -1;
         }
     } else if (type == NBD_REP_SERVER) {
+        if (len < sizeof(namelen) || len > NBD_MAX_BUFFER_SIZE) {
+            error_setg(errp, "incorrect option length");
+            return -1;
+        }
         if (read_sync(ioc, &namelen, sizeof(namelen)) != sizeof(namelen)) {
             error_setg(errp, "failed to read option name length");
             return -1;
         }
         namelen = be32_to_cpu(namelen);
-        if (len != (namelen + sizeof(namelen))) {
-            error_setg(errp, "incorrect option mame length");
+        len -= sizeof(namelen);
+        if (len < namelen) {
+            error_setg(errp, "incorrect option name length");
             return -1;
         }
         if (namelen > 255) {
@@ -214,6 +219,20 @@ static int nbd_receive_list(QIOChannel *ioc, char **name, Error **errp)
             return -1;
         }
         (*name)[namelen] = '\0';
+        len -= namelen;
+        if (len) {
+            char *buf = g_malloc(len + 1);
+            if (read_sync(ioc, buf, len) != len) {
+                error_setg(errp, "failed to read export description");
+                g_free(*name);
+                g_free(buf);
+                *name = NULL;
+                return -1;
+            }
+            buf[len] = '\0';
+            TRACE("Ignoring export description: %s", buf);
+            g_free(buf);
+        }
     } else {
         error_setg(errp, "Unexpected reply type %x expected %x",
                    type, NBD_REP_SERVER);
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
Reviewed-by
#4 lore raw
Quoted 17 lines · Eric Blake

[adding qemu-block in cc, since Paolo can't send pull request]

Quoted 14 lines · Eric Blake

The NBD Protocol states that NBD_REP_SERVER may set 'length > sizeof(namelen) + namelen'; in which case the rest of the packet is a UTF-8 description of the export. While we don't know of any NBD servers that send this description yet, we had better consume the data so we don't choke when we start to talk to such a server.

Also, a (buggy/malicious) server that replies with length < sizeof(namelen) would cause us to block waiting for bytes that the server is not sending, and one that replies with super-huge lengths could cause us to temporarily allocate up to 4G memory. Sanity check things before blindly reading incorrectly.

Quoted 53 lines
Quoted 53 lines

---

Yet another case of code introduced in 2.6 that doesn't play nicely with spec-compliant servers...

Hopefully I've squashed them all now?

nbd/client.c | 23 +++++++++++++++++++++--

1 file changed, 21 insertions(+), 2 deletions(-)
nbd/client.c +21 -2
index 6777e58..48f2a21 100644
@@ -192,13 +192,18 @@ static int nbd_receive_list(QIOChannel *ioc, char **name, Error **errp)
            return -1;
        }
    } else if (type == NBD_REP_SERVER) {
+        if (len < sizeof(namelen) || len > NBD_MAX_BUFFER_SIZE) {
+            error_setg(errp, "incorrect option length");
+            return -1;
+        }
        if (read_sync(ioc, &namelen, sizeof(namelen)) != sizeof(namelen)) {
            error_setg(errp, "failed to read option name length");
            return -1;
        }
        namelen = be32_to_cpu(namelen);
-        if (len != (namelen + sizeof(namelen))) {
-            error_setg(errp, "incorrect option mame length");
+        len -= sizeof(namelen);
+        if (len < namelen) {
+            error_setg(errp, "incorrect option name length");
            return -1;
        }
        if (namelen > 255) {
@@ -214,6 +219,20 @@ static int nbd_receive_list(QIOChannel *ioc, char **name, Error **errp)
            return -1;
        }
        (*name)[namelen] = '\0';
+        len -= namelen;
+        if (len) {
+            char *buf = g_malloc(len + 1);
+            if (read_sync(ioc, buf, len) != len) {
+                error_setg(errp, "failed to read export description");
+                g_free(*name);
+                g_free(buf);
+                *name = NULL;
+                return -1;
+            }
+            buf[len] = '\0';
+            TRACE("Ignoring export description: %s", buf);
+            g_free(buf);
+        }
Quoted 9 lines

nbd/client.c static int nbd_receive_list(QIOChannel *ioc, char **name, Error **errp) · line 238

Quoted 4 lines
nbd/client.c +0 -0
    } else {
        error_setg(errp, "Unexpected reply type %x expected %x",
                   type, NBD_REP_SERVER);

--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org

--
Alex Bligh

#5 lore raw
In reply to Eric Blake
Quoted 10 lines · Eric Blake

The NBD Protocol states that NBD_REP_SERVER may set 'length > sizeof(namelen) + namelen'; in which case the rest of the packet is a UTF-8 description of the export. While we don't know of any NBD servers that send this description yet, we had better consume the data so we don't choke when we start to talk to such a server.

Also, a (buggy/malicious) server that replies with length < sizeof(namelen) would cause us to block waiting for bytes that the server is not sending,

Well, you can still set length to anything and just send less... Both is equally non-compliant. But I agree that the check makes sense.

Quoted 46 lines

and one that replies with super-huge
lengths could cause us to temporarily allocate up to 4G memory. Sanity check things before blindly reading incorrectly.

---

Yet another case of code introduced in 2.6 that doesn't play nicely with spec-compliant servers...

Hopefully I've squashed them all now?

 nbd/client.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
nbd/client.c +9 -2
index 6777e58..48f2a21 100644
@@ -192,13 +192,18 @@ static int nbd_receive_list(QIOChannel *ioc, char **name, Error **errp)
             return -1;
         }
     } else if (type == NBD_REP_SERVER) {
+        if (len < sizeof(namelen) || len > NBD_MAX_BUFFER_SIZE) {
+            error_setg(errp, "incorrect option length");
+            return -1;
+        }
         if (read_sync(ioc, &namelen, sizeof(namelen)) != sizeof(namelen)) {
             error_setg(errp, "failed to read option name length");
             return -1;
         }
         namelen = be32_to_cpu(namelen);
-        if (len != (namelen + sizeof(namelen))) {
-            error_setg(errp, "incorrect option mame length");
+        len -= sizeof(namelen);
+        if (len < namelen) {
+            error_setg(errp, "incorrect option name length");
             return -1;
         }
         if (namelen > 255) {
@@ -214,6 +219,20 @@ static int nbd_receive_list(QIOChannel *ioc, char **name, Error **errp)
             return -1;
         }
         (*name)[namelen] = '\0';
+        len -= namelen;
+        if (len) {

nbd/client.c static int nbd_receive_list(QIOChannel *ioc, char **name, Error **errp) · line 233

Quoted 10 lines
nbd/client.c +10 -0
+            char *buf = g_malloc(len + 1);
+            if (read_sync(ioc, buf, len) != len) {
+                error_setg(errp, "failed to read export description");
+                g_free(*name);
+                g_free(buf);
+                *name = NULL;
+                return -1;
+            }
+            buf[len] = '\0';
+            TRACE("Ignoring export description: %s", buf);

I find this funny, somehow.

Perhaps it's because this may explicitly print something while explaining that it's being ignored.

nbd/client.c static int nbd_receive_list(QIOChannel *ioc, char **name, Error **errp) · line 238

Quoted 6 lines
nbd/client.c +2 -0
+            g_free(buf);
+        }
     } else {
         error_setg(errp, "Unexpected reply type %x expected %x",
                    type, NBD_REP_SERVER);

Thanks Eric, I applied this patch to my block branch (for 2.6). If this was not your intention, please speak up. :-)

https://github.com/XanClic/qemu/commits/block

Max

author
#6 lore raw
Quoted 28 lines · Max Reitz
Quoted 14 lines · Eric Blake

The NBD Protocol states that NBD_REP_SERVER may set 'length > sizeof(namelen) + namelen'; in which case the rest of the packet is a UTF-8 description of the export. While we don't know of any NBD servers that send this description yet, we had better consume the data so we don't choke when we start to talk to such a server.

nbd/client.c +2 -0
@@ -214,6 +219,20 @@ static int nbd_receive_list(QIOChannel *ioc, char **name, Error **errp)
             return -1;
         }
         (*name)[namelen] = '\0';
+        len -= namelen;
+        if (len) {

nbd/client.c static int nbd_receive_list(QIOChannel *ioc, char **name, Error **errp) · line 233

Quoted 10 lines
nbd/client.c +10 -0
+            char *buf = g_malloc(len + 1);
+            if (read_sync(ioc, buf, len) != len) {
+                error_setg(errp, "failed to read export description");
+                g_free(*name);
+                g_free(buf);
+                *name = NULL;
+                return -1;
+            }
+            buf[len] = '\0';
+            TRACE("Ignoring export description: %s", buf);

I find this funny, somehow.

Quoted 2 lines

Perhaps it's because this may explicitly print something while explaining that it's being ignored.

The server.c code had a nice function for skipping unwanted bytes; and in a 2.7 series, I borrowed that idea:

https://lists.gnu.org/archive/html/qemu-devel/2016-04/msg01597.html

but for the purpose of minimal churn in 2.6, I don't mind the (temporary) oddity.

Quoted 8 lines

nbd/client.c static int nbd_receive_list(QIOChannel *ioc, char **name, Error **errp) · line 238

Quoted 7 lines
nbd/client.c +2 -0
+            g_free(buf);
+        }
     } else {
         error_setg(errp, "Unexpected reply type %x expected %x",
                    type, NBD_REP_SERVER);
Quoted 4 lines

Thanks Eric, I applied this patch to my block branch (for 2.6). If this was not your intention, please speak up. :-)

https://github.com/XanClic/qemu/commits/block

You did the right thing.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#7 lore raw
Quoted 29 lines · Eric Blake
Quoted 28 lines · Max Reitz
Quoted 14 lines · Eric Blake

The NBD Protocol states that NBD_REP_SERVER may set 'length > sizeof(namelen) + namelen'; in which case the rest of the packet is a UTF-8 description of the export. While we don't know of any NBD servers that send this description yet, we had better consume the data so we don't choke when we start to talk to such a server.

nbd/client.c +2 -0
@@ -214,6 +219,20 @@ static int nbd_receive_list(QIOChannel *ioc, char **name, Error **errp)
             return -1;
         }
         (*name)[namelen] = '\0';
+        len -= namelen;
+        if (len) {

nbd/client.c static int nbd_receive_list(QIOChannel *ioc, char **name, Error **errp) · line 233

Quoted 10 lines
nbd/client.c +10 -0
+            char *buf = g_malloc(len + 1);
+            if (read_sync(ioc, buf, len) != len) {
+                error_setg(errp, "failed to read export description");
+                g_free(*name);
+                g_free(buf);
+                *name = NULL;
+                return -1;
+            }
+            buf[len] = '\0';
+            TRACE("Ignoring export description: %s", buf);

I find this funny, somehow.

Quoted 4 lines
Quoted 2 lines

Perhaps it's because this may explicitly print something while explaining that it's being ignored.

The server.c code had a nice function for skipping unwanted bytes;

I know; I added it. ;-)

Quoted 7 lines

and
in a 2.7 series, I borrowed that idea:

https://lists.gnu.org/archive/html/qemu-devel/2016-04/msg01597.html

but for the purpose of minimal churn in 2.6, I don't mind the (temporary) oddity.

Yeah, I didn't mean funny in the "why is that here" sense, but in the "I chuckled a little" sense. :-)

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
nbd/client.c +2 -0
+            g_free(buf);
+        }
     } else {
         error_setg(errp, "Unexpected reply type %x expected %x",
                    type, NBD_REP_SERVER);
Quoted 6 lines
Quoted 4 lines

Thanks Eric, I applied this patch to my block branch (for 2.6). If this was not your intention, please speak up. :-)

https://github.com/XanClic/qemu/commits/block

You did the right thing.

That phrasing makes me feel a little uncomfortable. It's a phrase I'd expect to hear after I have committed a deed of questionable morality, which was right only in the greater scheme of things and for the well-being of the many. So now I'm a bit worried. :-)

(Just kidding, of course.)

Max