Skip to contentSkip to conversation

nbd: Don't inf-loop on early EOF

PATCH In review Eric Blake 3 participants 5 messages Nov 7, 2016 – Nov 9, 2016

lore
Share & tools
lore

Eric Blake

author
#0 lore raw

PATCH 1/1 nbd: Don't inf-loop on early EOF

Commit 7d3123e converted a single read_sync() into a while loop that assumed that read_sync() would either make progress or give an error. But when the server hangs up early, the client sees EOF (a read_sync() of 0) and never makes progress, which in turn caused qemu-iotest './check -nbd 83' to go into an infinite loop.

Rework the loop to accomodate reads cut short by EOF.

---
 nbd/client.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
nbd/client.c +7 -6
index 5d94e34..29b6edf 100644
@@ -79,20 +79,21 @@ static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports);
  * the amount of bytes consumed. */
 static ssize_t drop_sync(QIOChannel *ioc, size_t size)
 {
-    ssize_t ret, dropped = size;
+    ssize_t ret = 0;
     char small[1024];
     char *buffer;
 
     buffer = sizeof(small) < size ? small : g_malloc(MIN(65536, size));
     while (size > 0) {
-        ret = read_sync(ioc, buffer, MIN(65536, size));
-        if (ret < 0) {
+        ssize_t count = read_sync(ioc, buffer, MIN(65536, size));
+
+        if (count <= 0) {
             goto cleanup;
         }
-        assert(ret <= size);
-        size -= ret;
+        assert(count <= size);
+        size -= count;
+        ret += count;
     }
-    ret = dropped;
 
  cleanup:
     if (buffer != small) {
Signature · 1 line
2.7.4
author
#1 lore raw

[updating subject line, to make sure this regression is fixed]

Quoted 7 lines · Eric Blake

Commit 7d3123e converted a single read_sync() into a while loop that assumed that read_sync() would either make progress or give an error. But when the server hangs up early, the client sees EOF (a read_sync() of 0) and never makes progress, which in turn caused qemu-iotest './check -nbd 83' to go into an infinite loop.

Rework the loop to accomodate reads cut short by EOF.

and s/accomodate/accommodate/ if the maintainer would be so nice (not every day I get to correct my own typos)

Quoted 40 lines
---
 nbd/client.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
nbd/client.c +7 -6
index 5d94e34..29b6edf 100644
@@ -79,20 +79,21 @@ static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports);
  * the amount of bytes consumed. */
 static ssize_t drop_sync(QIOChannel *ioc, size_t size)
 {
-    ssize_t ret, dropped = size;
+    ssize_t ret = 0;
     char small[1024];
     char *buffer;
 
     buffer = sizeof(small) < size ? small : g_malloc(MIN(65536, size));
     while (size > 0) {
-        ret = read_sync(ioc, buffer, MIN(65536, size));
-        if (ret < 0) {
+        ssize_t count = read_sync(ioc, buffer, MIN(65536, size));
+
+        if (count <= 0) {
             goto cleanup;
         }
-        assert(ret <= size);
-        size -= ret;
+        assert(count <= size);
+        size -= count;
+        ret += count;
     }
-    ret = dropped;
 
  cleanup:
     if (buffer != small) {
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
Reviewed-by on v1 1/1
#2 lore raw
In reply to Eric Blake
Quoted 6 lines · Eric Blake

Commit 7d3123e converted a single read_sync() into a while loop that assumed that read_sync() would either make progress or give an error. But when the server hangs up early, the client sees EOF (a read_sync() of 0) and never makes progress, which in turn caused qemu-iotest './check -nbd 83' to go into an infinite loop.

Quoted 7 lines

Rework the loop to accomodate reads cut short by EOF.

---
 nbd/client.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

But what about the server's nbd_negotiate_drop_sync()? It uses pretty much the same code, so it seems susceptible to the same issue (only that we don't have a test for that side).

Max

author
#3 lore raw
Quoted 15 lines · Max Reitz
Quoted 6 lines · Eric Blake

Commit 7d3123e converted a single read_sync() into a while loop that assumed that read_sync() would either make progress or give an error. But when the server hangs up early, the client sees EOF (a read_sync() of 0) and never makes progress, which in turn caused qemu-iotest './check -nbd 83' to go into an infinite loop.

Quoted 7 lines

Rework the loop to accomodate reads cut short by EOF.

---
 nbd/client.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
Quoted 5 lines

But what about the server's nbd_negotiate_drop_sync()? It uses pretty much the same code, so it seems susceptible to the same issue (only that we don't have a test for that side).

If so, that's an older bug (pre-existing back to at least 2.6?), so it should be a separate fix, if anything.

I guess it's time to figure out how to test the server against ill-behaved clients...

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#4 lore raw
Quoted 22 lines · Eric Blake
Quoted 15 lines · Max Reitz
Quoted 6 lines · Eric Blake

Commit 7d3123e converted a single read_sync() into a while loop that assumed that read_sync() would either make progress or give an error. But when the server hangs up early, the client sees EOF (a read_sync() of 0) and never makes progress, which in turn caused qemu-iotest './check -nbd 83' to go into an infinite loop.

Quoted 7 lines

Rework the loop to accomodate reads cut short by EOF.

---
 nbd/client.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
Quoted 5 lines

But what about the server's nbd_negotiate_drop_sync()? It uses pretty much the same code, so it seems susceptible to the same issue (only that we don't have a test for that side).

Quoted 5 lines

If so, that's an older bug (pre-existing back to at least 2.6?), so it should be a separate fix, if anything.

I guess it's time to figure out how to test the server against ill-behaved clients...

Using afl perhaps?

Paolo