From patchwork Thu Jul 12 07:05:12 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: SCSI Improved checking for LBA out of range Date: Wed, 11 Jul 2012 21:05:12 -0000 From: ronniesahlberg@gmail.com X-Patchwork-Id: 170585 Message-Id: <1342076712-27013-2-git-send-email-ronniesahlberg@gmail.com> To: pbonzini@redhat.com, qemu-devel@nongnu.org Cc: Ronnie Sahlberg 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 + 1) { 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. Signed-off-by: Ronnie Sahlberg --- hw/scsi-disk.c | 6 ++++-- 1 files changed, 4 insertions(+), 2 deletions(-) diff --git a/hw/scsi-disk.c b/hw/scsi-disk.c index b2f3c0c..2c2be33 100644 --- a/hw/scsi-disk.c +++ b/hw/scsi-disk.c @@ -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 + 1) { 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 + 1) { goto illegal_lba; } r->sector = r->req.cmd.lba * (s->qdev.blocksize / 512);