From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55515) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b85uo-0003br-GE for qemu-devel@nongnu.org; Wed, 01 Jun 2016 09:10:31 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b85uj-0000l5-AR for qemu-devel@nongnu.org; Wed, 01 Jun 2016 09:10:29 -0400 Received: from mx1.redhat.com ([209.132.183.28]:43051) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b85uj-0000kl-4B for qemu-devel@nongnu.org; Wed, 01 Jun 2016 09:10:25 -0400 References: <1464717207-7549-1-git-send-email-ppandit@redhat.com> From: Paolo Bonzini Message-ID: <8c6531e9-6c5a-9e9a-e8d6-d9fd30d85b15@redhat.com> Date: Wed, 1 Jun 2016 15:10:16 +0200 MIME-Version: 1.0 In-Reply-To: <1464717207-7549-1-git-send-email-ppandit@redhat.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [PATCH] scsi: check buffer length before reading scsi command List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: P J P , Qemu Developers Cc: Li Qiang , Prasad J Pandit , =?UTF-8?Q?Herv=c3=a9_Poussineau?= On 31/05/2016 19:53, P J P wrote: > From: Prasad J Pandit >=20 > The 53C9X Fast SCSI Controller(FSC) comes with an internal 16-byte > FIFO buffer. It is used to handle command and data transfer. > Routine get_cmd() in non-DMA mode, uses 'ti_size' to read scsi > command into a buffer. Add check to validate command length against > buffer size to avoid any overrun. >=20 > Reported-by: Li Qiang > Signed-off-by: Prasad J Pandit > --- > hw/scsi/esp.c | 3 +++ > 1 file changed, 3 insertions(+) >=20 > diff --git a/hw/scsi/esp.c b/hw/scsi/esp.c > index 60c1b28..953027a 100644 > --- a/hw/scsi/esp.c > +++ b/hw/scsi/esp.c > @@ -98,6 +98,9 @@ static uint32_t get_cmd(ESPState *s, uint8_t *buf, ui= nt8_t buflen) > s->dma_memory_read(s->dma_opaque, buf, dmalen); > } else { > dmalen =3D s->ti_size; > + if (dmalen > TI_BUFSZ) { > + return 0; > + } > memcpy(buf, s->ti_buf, dmalen); > buf[0] =3D buf[2] >> 5; > } >=20 In theory this shouldn't happen, but I agree that it is better to be defensive. I'm queuing this patch. At least the following patch is needed to ensure that ti_size always matches ti_rptr/ti_wptr (Herv=C3=A9, what do you think about it? should I resubmit it formally?). Also, things are more complicated than necessary due to ti_size being used for both DMA and FIFO transfers. diff --git a/hw/scsi/esp.c b/hw/scsi/esp.c index c2f6f8f..6407844 100644 --- a/hw/scsi/esp.c +++ b/hw/scsi/esp.c @@ -222,7 +222,7 @@ static void write_response(ESPState *s) } else { s->ti_size =3D 2; s->ti_rptr =3D 0; - s->ti_wptr =3D 0; + s->ti_wptr =3D 2; s->rregs[ESP_RFLAGS] =3D 2; } esp_raise_irq(s); Paolo