Skip to contentSkip to conversation

scsi-disk: fix buffer overflow

PATCH Applied Gerd Hoffmann 3 participants 3 messages Mar 10, 2010 – Mar 17, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scsi-disk: fix buffer overflow

In case s->version is shorter than 4 bytes we overflow the memcpy src buffer. Fix it by clearing the target buffer, then copy only the amount of bytes we actually have.

---
 hw/scsi-disk.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
hw/scsi-disk.c +3 -1
index b2f61fe..5792545 100644
@@ -459,7 +459,9 @@ static int scsi_disk_emulate_inquiry(SCSIRequest *req, uint8_t *outbuf)
         memcpy(&outbuf[16], "QEMU HARDDISK   ", 16);
     }
     memcpy(&outbuf[8], "QEMU    ", 8);
-    memcpy(&outbuf[32], s->version ? s->version : QEMU_VERSION, 4);
+    memset(&outbuf[32], 0, 4);
+    memcpy(&outbuf[32], s->version ? s->version : QEMU_VERSION,
+           MIN(4, strlen(s->version ? s->version : QEMU_VERSION)));
     /* Identify device as SCSI-3 rev 1.
        Some later commands are also implemented. */
     outbuf[2] = 5;
Signature · 1 line
1.6.6.1
#1 lore raw
Quoted 14 lines · Gerd Hoffmann

In case s->version is shorter than 4 bytes we overflow the memcpy src buffer. Fix it by clearing the target buffer, then copy only the amount of bytes we actually have.

---
 hw/scsi-disk.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
hw/scsi-disk.c +0 -0
index b2f61fe..5792545 100644

hw/scsi-disk.c static int scsi_disk_emulate_inquiry(SCSIRequest *req, uint8_t *outbuf) · line 464

Quoted 7 lines
hw/scsi-disk.c +3 -1
@@ -459,7 +459,9 @@ static int scsi_disk_emulate_inquiry(SCSIRequest *req, uint8_t *outbuf)
         memcpy(&outbuf[16], "QEMU HARDDISK   ", 16);
     }
     memcpy(&outbuf[8], "QEMU    ", 8);
-    memcpy(&outbuf[32], s->version ? s->version : QEMU_VERSION, 4);
+    memset(&outbuf[32], 0, 4);
+    memcpy(&outbuf[32], s->version ? s->version : QEMU_VERSION,
+           MIN(4, strlen(s->version ? s->version : QEMU_VERSION)));

Wouldn't be simpler to use strncpy() instead of a memcpy()?

Quoted 9 lines
hw/scsi-disk.c +0 -0
     /* Identify device as SCSI-3 rev 1.
        Some later commands are also implemented. */
     outbuf[2] = 5;
Signature · 1 line
1.6.6.1
Signature · 2 lines
Aurelien Jarno                          GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net
#2 lore raw
In reply to Gerd Hoffmann
Quoted 6 lines · Gerd Hoffmann

In case s->version is shorter than 4 bytes we overflow the memcpy src buffer. Fix it by clearing the target buffer, then copy only the amount of bytes we actually have.

Applied. Thanks.

Regards,

Anthony Liguori

Quoted 20 lines
---
  hw/scsi-disk.c |    4 +++-
  1 files changed, 3 insertions(+), 1 deletions(-)
hw/scsi-disk.c +3 -1
index b2f61fe..5792545 100644
@@ -459,7 +459,9 @@ static int scsi_disk_emulate_inquiry(SCSIRequest *req, uint8_t *outbuf)
          memcpy(&outbuf[16], "QEMU HARDDISK   ", 16);
      }
      memcpy(&outbuf[8], "QEMU    ", 8);
-    memcpy(&outbuf[32], s->version ? s->version : QEMU_VERSION, 4);
+    memset(&outbuf[32], 0, 4);
+    memcpy(&outbuf[32], s->version ? s->version : QEMU_VERSION,
+           MIN(4, strlen(s->version ? s->version : QEMU_VERSION)));
      /* Identify device as SCSI-3 rev 1.
         Some later commands are also implemented. */
      outbuf[2] = 5;