Skip to contentSkip to conversation

nbd: Initialize reply on failure

PATCH In review Eric Blake 3 participants 3 messages Jul 19, 2019 – Jul 22, 2019 Block layer

lore
Share & tools
lore

Eric Blake

author
#0 lore raw

PATCH v2 1/1 nbd: Initialize reply on failure

We've had two separate reports of different callers running into use of uninitialized data if s->quit is set (one detected by gcc -O3, another by valgrind), due to checking 'nbd_reply_is_simple(reply) || s->quit' in the wrong order. Rather than chasing down which callers need to pre-initialize reply, and whether there are any other uninitialized uses, it's easier to guarantee that reply will always be set by nbd_co_receive_one_chunk() even on failure.

The uninitialized use happens to be harmless (the only time the variable is uninitialized is if s->quit is set, so the conditional results in the same action regardless of what was read from reply), and was introduced in commit 65e01d47.

In fixing the problem, it can also be seen that all (one) callers pass in a non-NULL reply, so there is a dead condtional to also be cleaned up.

---
 block/nbd.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
block/nbd.c +2 -3
index 81edabbf35ed..57c1a205811a 100644
@@ -640,12 +640,11 @@ static coroutine_fn int nbd_co_receive_one_chunk(
                                           request_ret, qiov, payload, errp);
 
     if (ret < 0) {
+        memset(reply, 0, sizeof(*reply));
         s->quit = true;
     } else {
         /* For assert at loop start in nbd_connection_entry */
-        if (reply) {
-            *reply = s->reply;
-        }
+        *reply = s->reply;
         s->reply.handle = 0;
     }
 
Signature · 1 line
2.20.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 8 lines · Eric Blake

We've had two separate reports of different callers running into use of uninitialized data if s->quit is set (one detected by gcc -O3, another by valgrind), due to checking 'nbd_reply_is_simple(reply) || s->quit' in the wrong order. Rather than chasing down which callers need to pre-initialize reply, and whether there are any other uninitialized uses, it's easier to guarantee that reply will always be set by nbd_co_receive_one_chunk() even on failure.

Quoted 7 lines

The uninitialized use happens to be harmless (the only time the variable is uninitialized is if s->quit is set, so the conditional results in the same action regardless of what was read from reply), and was introduced in commit 65e01d47.

In fixing the problem, it can also be seen that all (one) callers pass in a non-NULL reply, so there is a dead condtional to also be cleaned

"conditional"

Quoted 18 lines

up.

---
 block/nbd.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
block/nbd.c +1 -0
index 81edabbf35ed..57c1a205811a 100644
@@ -640,12 +640,11 @@ static coroutine_fn int nbd_co_receive_one_chunk(
                                           request_ret, qiov, payload, errp);
 
     if (ret < 0) {
+        memset(reply, 0, sizeof(*reply));

block/nbd.c static coroutine_fn int nbd_co_receive_one_chunk( · line 650

Quoted 10 lines
block/nbd.c +1 -3
         s->quit = true;
     } else {
         /* For assert at loop start in nbd_connection_entry */
-        if (reply) {
-            *reply = s->reply;
-        }
+        *reply = s->reply;
         s->reply.handle = 0;
     }
 
Reviewed-by on v2 1/1
#2 lore raw
In reply to Eric Blake
Quoted 33 lines · Eric Blake

We've had two separate reports of different callers running into use of uninitialized data if s->quit is set (one detected by gcc -O3, another by valgrind), due to checking 'nbd_reply_is_simple(reply) || s->quit' in the wrong order. Rather than chasing down which callers need to pre-initialize reply, and whether there are any other uninitialized uses, it's easier to guarantee that reply will always be set by nbd_co_receive_one_chunk() even on failure.

The uninitialized use happens to be harmless (the only time the variable is uninitialized is if s->quit is set, so the conditional results in the same action regardless of what was read from reply), and was introduced in commit 65e01d47.

In fixing the problem, it can also be seen that all (one) callers pass in a non-NULL reply, so there is a dead condtional to also be cleaned up.

---
  block/nbd.c | 5 ++---
  1 file changed, 2 insertions(+), 3 deletions(-)
block/nbd.c +1 -0
index 81edabbf35ed..57c1a205811a 100644
@@ -640,12 +640,11 @@ static coroutine_fn int nbd_co_receive_one_chunk(
                                            request_ret, qiov, payload, errp);
 
      if (ret < 0) {
+        memset(reply, 0, sizeof(*reply));

block/nbd.c static coroutine_fn int nbd_co_receive_one_chunk( · line 650

Quoted 10 lines
block/nbd.c +1 -3
          s->quit = true;
      } else {
          /* For assert at loop start in nbd_connection_entry */
-        if (reply) {
-            *reply = s->reply;
-        }
+        *reply = s->reply;
          s->reply.handle = 0;
      }
 
Signature · 2 lines
With the best regards,
Andrey Shinkevich