Skip to contentSkip to conversation

nbd: Initialize reply on failure

PATCH Superseded Eric Blake 3 participants 7 messages Jul 19, 2019 Block layer

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Eric Blake

author
#0 lore raw

PATCH 1/1 nbd: Initialize reply on failure

We've had two separate reports of a caller 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, it's easier to guarantee that reply will always be set by nbd_co_receive_one_chunk() even on failure.

---

Replaces: https://lists.gnu.org/archive/html/qemu-devel/2019-07/msg04477.html
Replaces: https://lists.gnu.org/archive/html/qemu-devel/2019-07/msg03712.html

 block/nbd.c | 1 +
 1 file changed, 1 insertion(+)
block/nbd.c +1 -0
index 8d565cc624ec..f751a8e633e5 100644
@@ -640,6 +640,7 @@ 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 */
Signature · 1 line
2.20.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 24 lines · Eric Blake

We've had two separate reports of a caller 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, it's easier to guarantee that reply will always be set by nbd_co_receive_one_chunk() even on failure.

---

Replaces: https://lists.gnu.org/archive/html/qemu-devel/2019-07/msg04477.html
Replaces: https://lists.gnu.org/archive/html/qemu-devel/2019-07/msg03712.html

  block/nbd.c | 1 +
  1 file changed, 1 insertion(+)
block/nbd.c +0 -0
index 8d565cc624ec..f751a8e633e5 100644

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

Quoted 7 lines
block/nbd.c +1 -0
@@ -640,6 +640,7 @@ 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 */
Signature · 2 lines
With the best regards,
Andrey Shinkevich
Reviewed-by on v1 1/1
#2 lore raw
In reply to Eric Blake
Quoted 23 lines · Eric Blake

We've had two separate reports of a caller 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, it's easier to guarantee that reply will always be set by nbd_co_receive_one_chunk() even on failure.

---

Replaces: https://lists.gnu.org/archive/html/qemu-devel/2019-07/msg04477.html
Replaces: https://lists.gnu.org/archive/html/qemu-devel/2019-07/msg03712.html

 block/nbd.c | 1 +
 1 file changed, 1 insertion(+)
block/nbd.c +0 -0
index 8d565cc624ec..f751a8e633e5 100644

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

Quoted 4 lines
block/nbd.c +1 -0
@@ -640,6 +640,7 @@ static coroutine_fn int nbd_co_receive_one_chunk(
                                           request_ret, qiov, payload, errp);
 
     if (ret < 0) {
+        memset(reply, 0, sizeof *reply);

I never had problem with sizeof without parenthesis, but here I find it not easy to review.

Anyhow, this definitively looks like 4.1 material.

Preferently with sizeof(), but I don't mind, so:

Quoted 4 lines
block/nbd.c +0 -0
         s->quit = true;
     } else {
         /* For assert at loop start in nbd_connection_entry */
author
#3 lore raw
Quoted 5 lines · Philippe Mathieu-Daudé
Quoted 2 lines
     if (ret < 0) {

+ memset(reply, 0, sizeof *reply);

I never had problem with sizeof without parenthesis, but here I find it not easy to review.

Holdover from my work on GNU coding style projects: the rationale is that you can tell 'sizeof(type)' apart from 'sizeof expr' if you always omit the () in the latter case, which further makes it possible to tell at a glance when you are using the expr form (preferred, because the type of the expression can change and the sizeof is still correct) or a type name (harder to audit, since changing a variable's type means you also have to change any associated sizeof in later code using that variable). But I will readily admit qemu is not GNU style:

$ git grep 'sizeof ' | wc
    394    2500   29756
$ git grep 'sizeof(' | wc
   8899   46172  671537

so I've fixed it to use ().

Quoted 5 lines

Anyhow, this definitively looks like 4.1 material.

Preferently with sizeof(), but I don't mind, so:

Thanks. Queued on my NBD tree for -rc2.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
author
#4 lore raw
In reply to Eric Blake
Quoted 7 lines · Eric Blake

We've had two separate reports of a caller 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, it's easier to guarantee that reply will always be set by nbd_co_receive_one_chunk() even on failure.

I'm adding:

The bug is harmless (the only time uninitialized use is possible is if s->quit is set, so the conditional resolves to the same branch regardless of the contents of reply), but was introduced in commit 65e01d47.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
author
#5 lore raw
In reply to Eric Blake
Quoted 7 lines · Eric Blake

We've had two separate reports of a caller 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, it's easier to guarantee that reply will always be set by nbd_co_receive_one_chunk() even on failure.

Quoted 5 lines

---

Blech. Needs a v2. Expanding context:

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

Quoted 9 lines

+++ b/block/nbd.c

block/nbd.c +1 -0
@@ -640,6 +640,7 @@ 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;
        }

either callers can pass in reply==NULL (in which case the memset() dereferences NULL, oops), or always pass in non-NULL reply (in which case the null check is dead code).

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
#6 lore raw
Quoted 30 lines · Eric Blake
Quoted 7 lines · Eric Blake

We've had two separate reports of a caller 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, it's easier to guarantee that reply will always be set by nbd_co_receive_one_chunk() even on failure.

Quoted 5 lines

---

Blech. Needs a v2. Expanding context:

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

Quoted 9 lines

+++ b/block/nbd.c

block/nbd.c +1 -0
@@ -640,6 +640,7 @@ 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;
        }
Quoted 2 lines

either callers can pass in reply==NULL (in which case the memset() dereferences NULL, oops), or always pass in non-NULL reply (in which

Oh good catch...

Quoted 2 lines

case the null check is dead code).