From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47763) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XkFz7-0007TX-4p for qemu-devel@nongnu.org; Fri, 31 Oct 2014 13:27:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XkFz2-00088m-F2 for qemu-devel@nongnu.org; Fri, 31 Oct 2014 13:27:37 -0400 From: Paolo Bonzini Date: Fri, 31 Oct 2014 18:26:08 +0100 Message-Id: <1414776373-9704-31-git-send-email-pbonzini@redhat.com> In-Reply-To: <1414776373-9704-1-git-send-email-pbonzini@redhat.com> References: <1414776373-9704-1-git-send-email-pbonzini@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PULL 30/35] ivshmem: Check ivshmem_read() size argument List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Cam Macdonell , qemu-stable@nongnu.org, Stefan Hajnoczi , =?UTF-8?q?Andreas=20F=C3=A4rber?= From: Stefan Hajnoczi 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. 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=C3=A4rber Reviewed-by: Michael S. Tsirkin Signed-off-by: Paolo Bonzini --- hw/misc/ivshmem.c | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) 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 mem= ory. @@ -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(incoming= _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_fif= o)); + 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), &n= um); + 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 accompany= 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, ivshmem_= 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.3.1