Received: from eggs.gnu.org ([2001:4830:134:3::10]:50294) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XTZK9-0003tf-0B for qemu-devel@nongnu.org; Mon, 15 Sep 2014 12:40:28 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XTZK1-0006uw-V6 for qemu-devel@nongnu.org; Mon, 15 Sep 2014 12:40:20 -0400 From: =?UTF-8?q?Andreas=20F=C3=A4rber?= Date: Mon, 15 Sep 2014 18:40:07 +0200 Message-Id: <1410799208-3250-4-git-send-email-afaerber@suse.de> In-Reply-To: <1410799208-3250-1-git-send-email-afaerber@suse.de> References: <1410799208-3250-1-git-send-email-afaerber@suse.de> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PATCH v3 3/4] ivshmem: Fix potential OOB r/w access List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Sebastian Krahmer , "Michael S. Tsirkin" , qemu-stable@nongnu.org, David Marchand , Bruce Rogers , Sebastian Krahmer , Stefan Hajnoczi , Cam Macdonell , =?UTF-8?q?Andreas=20F=C3=A4rber?= From: Sebastian Krahmer Fix OOB access via malformed incoming_posn parameters and check that requested memory is actually alloc'ed. Signed-off-by: Sebastian Krahmer [AF: Rebased, cleanups, avoid fd leak] Cc: qemu-stable@nongnu.org Signed-off-by: Andreas F=C3=A4rber --- hw/misc/ivshmem.c | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/hw/misc/ivshmem.c b/hw/misc/ivshmem.c index 24f74f6..ecef82a 100644 --- a/hw/misc/ivshmem.c +++ b/hw/misc/ivshmem.c @@ -29,6 +29,7 @@ =20 #include #include +#include =20 #define PCI_VENDOR_ID_IVSHMEM PCI_VENDOR_ID_REDHAT_QUMRANET #define PCI_DEVICE_ID_IVSHMEM 0x1110 @@ -410,14 +411,24 @@ static void close_guest_eventfds(IVShmemState *s, i= nt posn) =20 /* this function increase the dynamic storage need to store data about o= ther * guests */ -static void increase_dynamic_storage(IVShmemState *s, int new_min_size) = { +static int increase_dynamic_storage(IVShmemState *s, int new_min_size) +{ =20 int j, old_nb_alloc; =20 + /* check for integer overflow */ + if (new_min_size >=3D INT_MAX / sizeof(Peer) - 1 || new_min_size <=3D= 0) { + return -1; + } + old_nb_alloc =3D s->nb_peers; =20 - while (new_min_size >=3D s->nb_peers) - s->nb_peers =3D s->nb_peers * 2; + if (new_min_size >=3D s->nb_peers) { + /* +1 because #new_min_size is used as last array index */ + s->nb_peers =3D new_min_size + 1; + } else { + return 0; + } =20 IVSHMEM_DPRINTF("bumping storage to %d guests\n", s->nb_peers); s->peers =3D g_realloc(s->peers, s->nb_peers * sizeof(Peer)); @@ -427,6 +438,8 @@ static void increase_dynamic_storage(IVShmemState *s,= int new_min_size) { s->peers[j].eventfds =3D NULL; s->peers[j].nb_eventfds =3D 0; } + + return 0; } =20 static void ivshmem_read(void *opaque, const uint8_t *buf, int size) @@ -469,7 +482,13 @@ static void ivshmem_read(void *opaque, const uint8_t= *buf, int size) =20 /* make sure we have enough space for this guest */ if (incoming_posn >=3D s->nb_peers) { - increase_dynamic_storage(s, incoming_posn); + if (increase_dynamic_storage(s, incoming_posn) < 0) { + error_report("increase_dynamic_storage() failed"); + if (tmp_fd !=3D -1) { + close(tmp_fd); + } + return; + } } =20 if (tmp_fd =3D=3D -1) { --=20 1.8.4.5