scsi-disk: fix buffer overflow
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.
Signed-off-by Gerd Hoffmann <[email protected]>
--- hw/scsi-disk.c | 4 +++- 1 files changed, 3 insertions(+), 1 deletions(-)
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 )));/* Identify device as SCSI-3 rev 1. Some later commands are also implemented. */ outbuf[2 ] =5 ;
Signature · 1 line
1.6.6.1
Quoted 14 lines · Gerd Hoffmann
In case
s->versionis 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.Signed-off-by Gerd Hoffmann <[email protected]>--- hw/scsi-disk.c | 4 +++- 1 files changed, 3 insertions(+), 1 deletions(-)hw/scsi-disk.c +0
-0
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
Anthony Liguori
AppliedQuoted 6 lines · Gerd Hoffmann
In case
s->versionis 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.Signed-off-by Gerd Hoffmann<[email protected]>
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@@ -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 ;