From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41207) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XW1dH-0007Qi-Uv for qemu-devel@nongnu.org; Mon, 22 Sep 2014 07:18:19 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XW1dD-0001Cv-V5 for qemu-devel@nongnu.org; Mon, 22 Sep 2014 07:18:15 -0400 Date: Mon, 22 Sep 2014 14:21:12 +0300 From: "Michael S. Tsirkin" Message-ID: <20140922112112.GD14882@redhat.com> References: <1410799208-3250-1-git-send-email-afaerber@suse.de> <1410799208-3250-2-git-send-email-afaerber@suse.de> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline In-Reply-To: <1410799208-3250-2-git-send-email-afaerber@suse.de> Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [PATCH v3 1/4] ivshmem: Check ivshmem_read() size argument List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Andreas =?iso-8859-1?Q?F=E4rber?= Cc: Bruce Rogers , Sebastian Krahmer , qemu-stable@nongnu.org, qemu-devel@nongnu.org, David Marchand , Stefan Hajnoczi , Cam Macdonell On Mon, Sep 15, 2014 at 06:40:05PM +0200, Andreas F=E4rber wrote: > From: Stefan Hajnoczi >=20 > The third argument to the fd_read() callback implemented by > ivshmem_read() is the number of bytes, not a flags field. Fix this and > check we received enough bytes before accessing the buffer pointer. >=20 > Cc: Cam Macdonell > Reported-by: Sebastian Krahmer > Signed-off-by: Stefan Hajnoczi > [AF: Handle partial reads via FIFO] > Reported-by: Peter Maydell > Cc: qemu-stable@nongnu.org > Signed-off-by: Andreas F=E4rber Reviewed-by: Michael S. Tsirkin > --- > hw/misc/ivshmem.c | 30 ++++++++++++++++++++++++++++-- > 1 file changed, 28 insertions(+), 2 deletions(-) >=20 > diff --git a/hw/misc/ivshmem.c b/hw/misc/ivshmem.c > index bd9d718..caeee1e 100644 > --- a/hw/misc/ivshmem.c > +++ b/hw/misc/ivshmem.c > @@ -24,6 +24,7 @@ > #include "migration/migration.h" > #include "qapi/qmp/qerror.h" > #include "qemu/event_notifier.h" > +#include "qemu/fifo8.h" > #include "sysemu/char.h" > =20 > #include > @@ -73,6 +74,7 @@ typedef struct IVShmemState { > =20 > CharDriverState **eventfd_chr; > CharDriverState *server_chr; > + Fifo8 incoming_fifo; > MemoryRegion ivshmem_mmio; > =20 > /* We might need to register the BAR before we actually have the m= emory. > @@ -424,14 +426,35 @@ static void increase_dynamic_storage(IVShmemState= *s, int new_min_size) { > } > } > =20 > -static void ivshmem_read(void *opaque, const uint8_t * buf, int flags) > +static void ivshmem_read(void *opaque, const uint8_t *buf, int size) > { > IVShmemState *s =3D opaque; > int incoming_fd, tmp_fd; > int guest_max_eventfd; > long incoming_posn; > =20 > - memcpy(&incoming_posn, buf, sizeof(long)); > + if (fifo8_is_empty(&s->incoming_fifo) && size =3D=3D sizeof(incomi= ng_posn)) { > + memcpy(&incoming_posn, buf, size); > + } else { > + const uint8_t *p; > + uint32_t num; > + > + IVSHMEM_DPRINTF("short read of %d bytes\n", size); > + num =3D MAX(size, sizeof(long) - fifo8_num_used(&s->incoming_f= ifo)); > + fifo8_push_all(&s->incoming_fifo, buf, num); > + if (fifo8_num_used(&s->incoming_fifo) < sizeof(incoming_posn))= { > + return; > + } > + size -=3D num; > + buf +=3D num; > + p =3D fifo8_pop_buf(&s->incoming_fifo, sizeof(incoming_posn), = &num); > + g_assert(num =3D=3D sizeof(incoming_posn)); > + memcpy(&incoming_posn, p, sizeof(incoming_posn)); > + if (size > 0) { > + fifo8_push_all(&s->incoming_fifo, buf, size); > + } > + } > + > /* pick off s->server_chr->msgfd and store it, posn should accompa= ny msg */ > tmp_fd =3D qemu_chr_fe_get_msgfd(s->server_chr); > IVSHMEM_DPRINTF("posn is %ld, fd is %d\n", incoming_posn, tmp_fd); > @@ -663,6 +686,8 @@ static int pci_ivshmem_init(PCIDevice *dev) > s->ivshmem_size =3D ivshmem_get_size(s); > } > =20 > + fifo8_create(&s->incoming_fifo, sizeof(long)); > + > register_savevm(DEVICE(dev), "ivshmem", 0, 0, ivshmem_save, ivshme= m_load, > = dev); > =20 > @@ -796,6 +821,7 @@ static void pci_ivshmem_uninit(PCIDevice *dev) > memory_region_del_subregion(&s->bar, &s->ivshmem); > vmstate_unregister_ram(&s->ivshmem, DEVICE(dev)); > unregister_savevm(DEVICE(dev), "ivshmem", s); > + fifo8_destroy(&s->incoming_fifo); > } > =20 > static Property ivshmem_properties[] =3D { > --=20 > 1.8.4.5