Patchwork scsi-disk: fix buffer overflow

login
register
mail settings
Submitter Gerd Hoffmann
Date March 10, 2010, 4:47 p.m.
Message ID <1268239637-24524-1-git-send-email-kraxel@redhat.com>
Download mbox | patch
Permalink /patch/47257/
State New
Headers show

Comments

Gerd Hoffmann - March 10, 2010, 4:47 p.m.
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 <kraxel@redhat.com>
---
 hw/scsi-disk.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
Aurelien Jarno - March 13, 2010, 11:19 a.m.
On Wed, Mar 10, 2010 at 05:47:17PM +0100, Gerd Hoffmann wrote:
> 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 <kraxel@redhat.com>
> ---
>  hw/scsi-disk.c |    4 +++-
>  1 files changed, 3 insertions(+), 1 deletions(-)
> 
> diff --git a/hw/scsi-disk.c b/hw/scsi-disk.c
> index b2f61fe..5792545 100644
> --- a/hw/scsi-disk.c
> +++ b/hw/scsi-disk.c
> @@ -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()?

>      /* Identify device as SCSI-3 rev 1.
>         Some later commands are also implemented. */
>      outbuf[2] = 5;
> -- 
> 1.6.6.1
> 
> 
> 
>
Anthony Liguori - March 17, 2010, 4:59 p.m.
On 03/10/2010 10:47 AM, Gerd Hoffmann wrote:
> 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<kraxel@redhat.com>
>    

Applied.  Thanks.

Regards,

Anthony Liguori
> ---
>   hw/scsi-disk.c |    4 +++-
>   1 files changed, 3 insertions(+), 1 deletions(-)
>
> diff --git a/hw/scsi-disk.c b/hw/scsi-disk.c
> index b2f61fe..5792545 100644
> --- a/hw/scsi-disk.c
> +++ b/hw/scsi-disk.c
> @@ -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;
>

Patch

diff --git a/hw/scsi-disk.c b/hw/scsi-disk.c
index b2f61fe..5792545 100644
--- a/hw/scsi-disk.c
+++ b/hw/scsi-disk.c
@@ -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;