Skip to contentSkip to conversation

SCSI improved LBA-out-of-range checks

PATCH New Ronnie Sahlberg 2 participants 5 messages Jul 12, 2012

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 SCSI improved LBA-out-of-range checks

Paolo, List

Please find a small patch to the scsi emulation.
This patch improves the checkign that the requested lbas are all available. We check both that lba+len is not going past the end of the device
but also if lba+len < lba

This second condition could occur for deviously crafted scsi packets where lba is set to 0xffffffffffffffff and len is set to 2
in which case lba+len would wrap to 1

#1 lore raw

PATCH 1/1 SCSI: improve the lba-out-of-range tests for read/write/verify

Improve the tests for the LBA to cover more cases, the new test looks like this

        if (r->req.cmd.lba > r->req.cmd.lba + len
        ||  r->req.cmd.lba + len > s->qdev.max_lba) {

For the 16 byte opcodes, the lba is a uint64, so the first check is to make sure that we do not wrap. For example if an opcode would specify the LBA:0xffffffffffffffff and LEN:2
then lba+len would wrap to 1.

The second part of the test is to verify that ALL requested blocks are available, not just the first one.

---
 hw/scsi-disk.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
hw/scsi-disk.c +4 -2
index b2f3c0c..40c05de 100644
@@ -1558,7 +1558,8 @@ static int32_t scsi_send_command(SCSIRequest *req, uint8_t *buf)
         if (r->req.cmd.buf[1] & 0xe0) {
             goto fail;
         }
-        if (r->req.cmd.lba > s->qdev.max_lba) {
+        if (r->req.cmd.lba > r->req.cmd.lba + len
+        ||  r->req.cmd.lba + len > s->qdev.max_lba) {
             goto illegal_lba;
         }
         r->sector = r->req.cmd.lba * (s->qdev.blocksize / 512);
@@ -1581,7 +1582,8 @@ static int32_t scsi_send_command(SCSIRequest *req, uint8_t *buf)
         if (r->req.cmd.buf[1] & 0xe0) {
             goto fail;
         }
-        if (r->req.cmd.lba > s->qdev.max_lba) {
+        if (r->req.cmd.lba > r->req.cmd.lba + len
+        ||  r->req.cmd.lba + len > s->qdev.max_lba) {
             goto illegal_lba;
         }
         r->sector = r->req.cmd.lba * (s->qdev.blocksize / 512);
Signature · 1 line
1.7.3.1
#2 lore raw

Re: [Qemu-devel] [PATCH] SCSI: improve the lba-out-of-range tests for read/write/verify

Il 12/07/2012 08:52, Ronnie Sahlberg ha scritto:

Quoted 9 lines

Improve the tests for the LBA to cover more cases, the new test looks like this

        if (r->req.cmd.lba > r->req.cmd.lba + len
        ||  r->req.cmd.lba + len > s->qdev.max_lba) {

For the 16 byte opcodes, the lba is a uint64, so the first check is to make sure that we do not wrap. For example if an opcode would specify the LBA:0xffffffffffffffff and LEN:2
then lba+len would wrap to 1.

The second part of the test is to verify that ALL requested blocks are available, not just the first one.

Fixed code style for || and applied to scsi-next.

Thanks!

Paolo

Quoted 30 lines
---
 hw/scsi-disk.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
hw/scsi-disk.c +4 -2
index b2f3c0c..40c05de 100644
@@ -1558,7 +1558,8 @@ static int32_t scsi_send_command(SCSIRequest *req, uint8_t *buf)
         if (r->req.cmd.buf[1] & 0xe0) {
             goto fail;
         }
-        if (r->req.cmd.lba > s->qdev.max_lba) {
+        if (r->req.cmd.lba > r->req.cmd.lba + len
+        ||  r->req.cmd.lba + len > s->qdev.max_lba) {
             goto illegal_lba;
         }
         r->sector = r->req.cmd.lba * (s->qdev.blocksize / 512);
@@ -1581,7 +1582,8 @@ static int32_t scsi_send_command(SCSIRequest *req, uint8_t *buf)
         if (r->req.cmd.buf[1] & 0xe0) {
             goto fail;
         }
-        if (r->req.cmd.lba > s->qdev.max_lba) {
+        if (r->req.cmd.lba > r->req.cmd.lba + len
+        ||  r->req.cmd.lba + len > s->qdev.max_lba) {
             goto illegal_lba;
         }
         r->sector = r->req.cmd.lba * (s->qdev.blocksize / 512);
#3 lore raw

Re: [Qemu-devel] [PATCH] SCSI: improve the lba-out-of-range tests for read/write/verify

Sorry, there is a bug in it.

Can you change it to :

      ||  r->req.cmd.lba + len > s->qdev.max_lba + 1) {

or else the last lba of the device will be flagged as out of range.

Quoted 49 lines · Paolo Bonzini

Il 12/07/2012 08:52, Ronnie Sahlberg ha scritto:

Quoted 9 lines

Improve the tests for the LBA to cover more cases, the new test looks like this

        if (r->req.cmd.lba > r->req.cmd.lba + len
        ||  r->req.cmd.lba + len > s->qdev.max_lba) {

For the 16 byte opcodes, the lba is a uint64, so the first check is to make sure that we do not wrap. For example if an opcode would specify the LBA:0xffffffffffffffff and LEN:2
then lba+len would wrap to 1.

The second part of the test is to verify that ALL requested blocks are available, not just the first one.

Fixed code style for || and applied to scsi-next.

Thanks!

Paolo

Quoted 30 lines
---
 hw/scsi-disk.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
hw/scsi-disk.c +4 -2
index b2f3c0c..40c05de 100644
@@ -1558,7 +1558,8 @@ static int32_t scsi_send_command(SCSIRequest *req, uint8_t *buf)
         if (r->req.cmd.buf[1] & 0xe0) {
             goto fail;
         }
-        if (r->req.cmd.lba > s->qdev.max_lba) {
+        if (r->req.cmd.lba > r->req.cmd.lba + len
+        ||  r->req.cmd.lba + len > s->qdev.max_lba) {
             goto illegal_lba;
         }
         r->sector = r->req.cmd.lba * (s->qdev.blocksize / 512);
@@ -1581,7 +1582,8 @@ static int32_t scsi_send_command(SCSIRequest *req, uint8_t *buf)
         if (r->req.cmd.buf[1] & 0xe0) {
             goto fail;
         }
-        if (r->req.cmd.lba > s->qdev.max_lba) {
+        if (r->req.cmd.lba > r->req.cmd.lba + len
+        ||  r->req.cmd.lba + len > s->qdev.max_lba) {
             goto illegal_lba;
         }
         r->sector = r->req.cmd.lba * (s->qdev.blocksize / 512);
#4 lore raw

Re: [Qemu-devel] [PATCH] SCSI: improve the lba-out-of-range tests for read/write/verify

Il 12/07/2012 09:06, ronnie sahlberg ha scritto:

Quoted 6 lines

Sorry, there is a bug in it.

Can you change it to :

      ||  r->req.cmd.lba + len > s->qdev.max_lba + 1) {

or else the last lba of the device will be flagged as out of range.

Yes, done that already. Can you add a test for the last LBA to your testsuite?

Paolo