Skip to contentSkip to conversation

nbd: Don't mishandle unaligned client requests

PATCH In review Eric Blake 4 participants 7 messages Apr 21, 2016 – Apr 22, 2016

lore
Share & tools
lore

Eric Blake

author
#0 lore raw

PATCH 1/1 nbd: Don't mishandle unaligned client requests

The NBD protocol does not (yet) force any alignment constraints on clients. Even though qemu NBD clients always send requests that are aligned to 512 bytes, we must be prepared for non-qemu clients that don't care about alignment (even if it means they are less efficient). Our use of blk_read() and blk_write() was silently operating on the wrong file offsets when the client made an unaligned request, corrupting the client's data (but as the client already has control over the file we are serving, I don't think it is a security hole, per se, just a data corruption bug).

Note that in the case of NBD_CMD_READ, an unaligned length could cause us to return up to 511 bytes of uninitialized trailing garbage from blk_try_blockalign() - hopefully nothing sensitive from the heap's prior usage is ever leaked in that manner.

---

It's late for 2.6, but as a data corruption bug fix, I think it's worth having if there is still time.

 nbd/server.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
nbd/server.c +4 -6
index a13a691..2184c64 100644
@@ -1091,9 +1091,8 @@ static void nbd_trip(void *opaque)
             }
         }
 
-        ret = blk_read(exp->blk,
-                       (request.from + exp->dev_offset) / BDRV_SECTOR_SIZE,
-                       req->data, request.len / BDRV_SECTOR_SIZE);
+        ret = blk_pread(exp->blk, request.from + exp->dev_offset,
+                        req->data, request.len);
         if (ret < 0) {
             LOG("reading from file failed");
             reply.error = -ret;
@@ -1115,9 +1114,8 @@ static void nbd_trip(void *opaque)
 
         TRACE("Writing to device");
 
-        ret = blk_write(exp->blk,
-                        (request.from + exp->dev_offset) / BDRV_SECTOR_SIZE,
-                        req->data, request.len / BDRV_SECTOR_SIZE);
+        ret = blk_pwrite(exp->blk, request.from + exp->dev_offset,
+                        req->data, request.len);
         if (ret < 0) {
             LOG("writing to file failed");
             reply.error = -ret;
Signature · 1 line
2.5.5
#1 lore raw
Quoted 16 lines · Eric Blake

The NBD protocol does not (yet) force any alignment constraints on clients. Even though qemu NBD clients always send requests that are aligned to 512 bytes, we must be prepared for non-qemu clients that don't care about alignment (even if it means they are less efficient). Our use of blk_read() and blk_write() was silently operating on the wrong file offsets when the client made an unaligned request, corrupting the client's data (but as the client already has control over the file we are serving, I don't think it is a security hole, per se, just a data corruption bug).

Note that in the case of NBD_CMD_READ, an unaligned length could cause us to return up to 511 bytes of uninitialized trailing garbage from blk_try_blockalign() - hopefully nothing sensitive from the heap's prior usage is ever leaked in that manner.

Quoted 5 lines

---

It's late for 2.6, but as a data corruption bug fix, I think it's worth having if there is still time.

I want to tag rc3 today, but since it looks like there's going to be an rc4 for the virtio handler bug this can probably go into rc4 if it gets review.

thanks
-- PMM

Reviewed-by
#2 lore raw

Am 21.04.2016 um 18:28 hat Peter Maydell geschrieben:

Quoted 20 lines · Eric Blake
Quoted 19 lines

The NBD protocol does not (yet) force any alignment constraints on clients. Even though qemu NBD clients always send requests that are aligned to 512 bytes, we must be prepared for non-qemu clients that don't care about alignment (even if it means they are less efficient). Our use of blk_read() and blk_write() was silently operating on the wrong file offsets when the client made an unaligned request, corrupting the client's data (but as the client already has control over the file we are serving, I don't think it is a security hole, per se, just a data corruption bug).

Note that in the case of NBD_CMD_READ, an unaligned length could cause us to return up to 511 bytes of uninitialized trailing garbage from blk_try_blockalign() - hopefully nothing sensitive from the heap's prior usage is ever leaked in that manner.

---

Quoted 6 lines
Quoted 2 lines

It's late for 2.6, but as a data corruption bug fix, I think it's worth having if there is still time.

I want to tag rc3 today, but since it looks like there's going to be an rc4 for the virtio handler bug this can probably go into rc4 if it gets review.

Peter, do you want a pull request (which I would have to do because Paolo is away) or are you going to apply the patch directly?

Also adding Cc: qemu-stable, because this is an old bug that has existed ever since qemu-nbd was added.

Kevin

#3 lore raw
Quoted 24 lines · Kevin Wolf

Am 21.04.2016 um 18:28 hat Peter Maydell geschrieben:

Quoted 23 lines · Eric Blake
Quoted 21 lines

The NBD protocol does not (yet) force any alignment constraints on clients. Even though qemu NBD clients always send requests that are aligned to 512 bytes, we must be prepared for non-qemu clients that don't care about alignment (even if it means they are less efficient). Our use of blk_read() and blk_write() was silently operating on the wrong file offsets when the client made an unaligned request, corrupting the client's data (but as the client already has control over the file we are serving, I don't think it is a security hole, per se, just a data corruption bug).

Note that in the case of NBD_CMD_READ, an unaligned length could cause us to return up to 511 bytes of uninitialized trailing garbage from blk_try_blockalign() - hopefully nothing sensitive from the heap's prior usage is ever leaked in that manner.

---

It's late for 2.6, but as a data corruption bug fix, I think it's worth having if there is still time.

Quoted 8 lines
Quoted 3 lines

I want to tag rc3 today, but since it looks like there's going to be an rc4 for the virtio handler bug this can probably go into rc4 if it gets review.

Peter, do you want a pull request (which I would have to do because Paolo is away) or are you going to apply the patch directly?

If you're happy on the review and testing front I can apply it to master directly (I won't be able to do any testing beyond running "make check".)

thanks
-- PMM

Tested-by
#4 lore raw

Am 22.04.2016 um 11:29 hat Peter Maydell geschrieben:

Quoted 29 lines · Kevin Wolf
Quoted 28 lines

Am 21.04.2016 um 18:28 hat Peter Maydell geschrieben:

Quoted 26 lines · Eric Blake
Quoted 16 lines

The NBD protocol does not (yet) force any alignment constraints on clients. Even though qemu NBD clients always send requests that are aligned to 512 bytes, we must be prepared for non-qemu clients that don't care about alignment (even if it means they are less efficient). Our use of blk_read() and blk_write() was silently operating on the wrong file offsets when the client made an unaligned request, corrupting the client's data (but as the client already has control over the file we are serving, I don't think it is a security hole, per se, just a data corruption bug).

Note that in the case of NBD_CMD_READ, an unaligned length could cause us to return up to 511 bytes of uninitialized trailing garbage from blk_try_blockalign() - hopefully nothing sensitive from the heap's prior usage is ever leaked in that manner.

Quoted 5 lines

---

It's late for 2.6, but as a data corruption bug fix, I think it's worth having if there is still time.

I want to tag rc3 today, but since it looks like there's going to be an rc4 for the virtio handler bug this can probably go into rc4 if it gets review.

Quoted 8 lines
Quoted 4 lines

Peter, do you want a pull request (which I would have to do because Paolo is away) or are you going to apply the patch directly?

If you're happy on the review and testing front I can apply it to master directly (I won't be able to do any testing beyond running "make check".)

I am. It's a trivial patch anyway, but I've also tested it with qemu-iotests and by installing a guest on an NBD device. So if you like, you can also add:

#5 lore raw
Quoted 9 lines · Kevin Wolf

Am 22.04.2016 um 11:29 hat Peter Maydell geschrieben:

Quoted 7 lines · Kevin Wolf
Quoted 2 lines

Peter, do you want a pull request (which I would have to do because Paolo is away) or are you going to apply the patch directly?

If you're happy on the review and testing front I can apply it to master directly (I won't be able to do any testing beyond running "make check".)

Quoted 5 lines

I am. It's a trivial patch anyway, but I've also tested it with qemu-iotests and by installing a guest on an NBD device. So if you like, you can also add:

Thanks; applied to master.

-- PMM

Reviewed-by on v1 1/1
#6 lore raw
In reply to Eric Blake
Quoted 42 lines · Eric Blake

The NBD protocol does not (yet) force any alignment constraints on clients. Even though qemu NBD clients always send requests that are aligned to 512 bytes, we must be prepared for non-qemu clients that don't care about alignment (even if it means they are less efficient). Our use of blk_read() and blk_write() was silently operating on the wrong file offsets when the client made an unaligned request, corrupting the client's data (but as the client already has control over the file we are serving, I don't think it is a security hole, per se, just a data corruption bug).

Note that in the case of NBD_CMD_READ, an unaligned length could cause us to return up to 511 bytes of uninitialized trailing garbage from blk_try_blockalign() - hopefully nothing sensitive from the heap's prior usage is ever leaked in that manner.

---

It's late for 2.6, but as a data corruption bug fix, I think it's worth having if there is still time.

 nbd/server.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
nbd/server.c +2 -3
index a13a691..2184c64 100644
@@ -1091,9 +1091,8 @@ static void nbd_trip(void *opaque)
             }
         }
 
-        ret = blk_read(exp->blk,
-                       (request.from + exp->dev_offset) / BDRV_SECTOR_SIZE,
-                       req->data, request.len / BDRV_SECTOR_SIZE);
+        ret = blk_pread(exp->blk, request.from + exp->dev_offset,
+                        req->data, request.len);
         if (ret < 0) {
             LOG("reading from file failed");
             reply.error = -ret;

nbd/server.c static void nbd_trip(void *opaque) · line 1118

Quoted 8 lines
nbd/server.c +2 -3
@@ -1115,9 +1114,8 @@ static void nbd_trip(void *opaque)
 
         TRACE("Writing to device");
 
-        ret = blk_write(exp->blk,
-                        (request.from + exp->dev_offset) / BDRV_SECTOR_SIZE,
-                        req->data, request.len / BDRV_SECTOR_SIZE);
+        ret = blk_pwrite(exp->blk, request.from + exp->dev_offset,
+                        req->data, request.len);

Indentation is one column off, but can be ignored or fixed when applying.

Quoted 7 lines
nbd/server.c +0 -0
         if (ret < 0) {
             LOG("writing to file failed");
             reply.error = -ret;
Signature · 1 line
2.5.5