From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43800) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aftoB-0006iI-2l for qemu-devel@nongnu.org; Tue, 15 Mar 2016 14:35:08 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1afto9-0001rv-AE for qemu-devel@nongnu.org; Tue, 15 Mar 2016 14:35:06 -0400 Received: from mx1.redhat.com ([209.132.183.28]:60571) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1afto8-0001rD-UG for qemu-devel@nongnu.org; Tue, 15 Mar 2016 14:35:05 -0400 From: Markus Armbruster Date: Tue, 15 Mar 2016 19:34:37 +0100 Message-Id: <1458066895-20632-23-git-send-email-armbru@redhat.com> In-Reply-To: <1458066895-20632-1-git-send-email-armbru@redhat.com> References: <1458066895-20632-1-git-send-email-armbru@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PATCH v3 22/40] ivshmem: Simplify rejection of invalid peer ID from server List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: claudio.fontana@huawei.com, cam@cs.ualberta.ca, mlureau@redhat.com, david.marchand@6wind.com, pbonzini@redhat.com ivshmem_read() processes server messages. These are 64 bit signed integers. -1 is shared memory setup, 16 bit unsigned is a peer ID, anything else is invalid. ivshmem_read() rejects invalid negative messages right away, silently. Invalid positive messages get rejected only in resize_peers(), and ivshmem_read() then prints the rather cryptic message "failed to resize peers array". Extend the first check to cover all invalid messages, make it report "server sent invalid message", and drop the second check. Now resize_peers() can't fail anymore; simplify. Signed-off-by: Markus Armbruster Reviewed-by: Marc-Andr=C3=A9 Lureau --- hw/misc/ivshmem.c | 61 ++++++++++++++++++++-----------------------------= ------ 1 file changed, 22 insertions(+), 39 deletions(-) diff --git a/hw/misc/ivshmem.c b/hw/misc/ivshmem.c index 61e21cd..703b3bf 100644 --- a/hw/misc/ivshmem.c +++ b/hw/misc/ivshmem.c @@ -39,7 +39,7 @@ #define PCI_VENDOR_ID_IVSHMEM PCI_VENDOR_ID_REDHAT_QUMRANET #define PCI_DEVICE_ID_IVSHMEM 0x1110 =20 -#define IVSHMEM_MAX_PEERS G_MAXUINT16 +#define IVSHMEM_MAX_PEERS UINT16_MAX #define IVSHMEM_IOEVENTFD 0 #define IVSHMEM_MSI 1 =20 @@ -93,7 +93,7 @@ typedef struct IVShmemState { uint32_t ivshmem_64bit; =20 Peer *peers; - int nb_peers; /* how many peers we have space for */ + int nb_peers; /* space in @peers[] */ =20 int vm_id; uint32_t vectors; @@ -451,34 +451,21 @@ static void close_peer_eventfds(IVShmemState *s, in= t posn) s->peers[posn].nb_eventfds =3D 0; } =20 -/* this function increase the dynamic storage need to store data about o= ther - * peers */ -static int resize_peers(IVShmemState *s, int new_min_size) +static void resize_peers(IVShmemState *s, int nb_peers) { + int old_nb_peers =3D s->nb_peers; + int i; =20 - int j, old_size; + assert(nb_peers > old_nb_peers); + IVSHMEM_DPRINTF("bumping storage to %d peers\n", nb_peers); =20 - /* limit number of max peers */ - if (new_min_size <=3D 0 || new_min_size > IVSHMEM_MAX_PEERS) { - return -1; - } - if (new_min_size <=3D s->nb_peers) { - return 0; - } - - old_size =3D s->nb_peers; - s->nb_peers =3D new_min_size; + s->peers =3D g_realloc(s->peers, nb_peers * sizeof(Peer)); + s->nb_peers =3D nb_peers; =20 - IVSHMEM_DPRINTF("bumping storage to %d peers\n", s->nb_peers); - - s->peers =3D g_realloc(s->peers, s->nb_peers * sizeof(Peer)); - - for (j =3D old_size; j < s->nb_peers; j++) { - s->peers[j].eventfds =3D g_new0(EventNotifier, s->vectors); - s->peers[j].nb_eventfds =3D 0; + for (i =3D old_nb_peers; i < nb_peers; i++) { + s->peers[i].eventfds =3D g_new0(EventNotifier, s->vectors); + s->peers[i].nb_eventfds =3D 0; } - - return 0; } =20 static bool fifo_update_and_get(IVShmemState *s, const uint8_t *buf, int= size, @@ -590,25 +577,21 @@ static void ivshmem_read(void *opaque, const uint8_= t *buf, int size) return; } =20 - if (incoming_posn < -1) { - IVSHMEM_DPRINTF("invalid incoming_posn %" PRId64 "\n", incoming_= posn); - return; - } - - /* pick off s->server_chr->msgfd and store it, posn should accompany= msg */ incoming_fd =3D qemu_chr_fe_get_msgfd(s->server_chr); IVSHMEM_DPRINTF("posn is %" PRId64 ", fd is %d\n", incoming_posn, incoming_fd); =20 - /* make sure we have enough space for this peer */ + if (incoming_posn < -1 || incoming_posn > IVSHMEM_MAX_PEERS) { + error_report("server sent invalid message %" PRId64, + incoming_posn); + if (incoming_fd !=3D -1) { + close(incoming_fd); + } + return; + } + if (incoming_posn >=3D s->nb_peers) { - if (resize_peers(s, incoming_posn + 1) < 0) { - error_report("failed to resize peers array"); - if (incoming_fd !=3D -1) { - close(incoming_fd); - } - return; - } + resize_peers(s, incoming_posn + 1); } =20 peer =3D &s->peers[incoming_posn]; --=20 2.4.3