From nobody Sun Apr 28 04:50:55 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1511463956824915.5568696745264; Thu, 23 Nov 2017 11:05:56 -0800 (PST) Received: from localhost ([::1]:45742 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eHwok-0005Xr-Rx for importer@patchew.org; Thu, 23 Nov 2017 14:05:46 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40907) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eHwkr-00030K-KY for qemu-devel@nongnu.org; Thu, 23 Nov 2017 14:01:51 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eHwkp-0006Pp-Sw for qemu-devel@nongnu.org; Thu, 23 Nov 2017 14:01:45 -0500 Received: from mx1.redhat.com ([209.132.183.28]:35796) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eHwkp-0006Oq-MW for qemu-devel@nongnu.org; Thu, 23 Nov 2017 14:01:43 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C4E29C9D57; Thu, 23 Nov 2017 19:01:42 +0000 (UTC) Received: from localhost.localdomain (ovpn-120-135.rdu2.redhat.com [10.10.120.135]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D5B4217AFE; Thu, 23 Nov 2017 19:01:40 +0000 (UTC) From: P J P To: Qemu Developers Date: Fri, 24 Nov 2017 00:31:30 +0530 Message-Id: <20171123190131.5138-2-ppandit@redhat.com> In-Reply-To: <20171123190131.5138-1-ppandit@redhat.com> References: <20171123190131.5138-1-ppandit@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Thu, 23 Nov 2017 19:01:42 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 1/2] virtio: check VirtQueue Vring object is set X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: zhangboxian , Stefan Hajnoczi , Prasad J Pandit Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Prasad J Pandit An user could attempt to use an uninitialised VirtQueue object or unset Vring.align leading to a arithmetic exception. Add check to avoid it. Reported-by: Zhangboxian Signed-off-by: Prasad J Pandit --- hw/virtio/virtio.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index 5884ce3480..d961780384 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -182,7 +182,7 @@ void virtio_queue_update_rings(VirtIODevice *vdev, int = n) { VRing *vring =3D &vdev->vq[n].vring; =20 - if (!vring->desc) { + if (!vring->desc || !vring->align) { /* not yet setup -> nothing to do */ return; } @@ -1414,6 +1414,9 @@ void virtio_config_modern_writel(VirtIODevice *vdev, =20 void virtio_queue_set_addr(VirtIODevice *vdev, int n, hwaddr addr) { + if (!vdev->vq[n].vring.num) { + return; + } vdev->vq[n].vring.desc =3D addr; virtio_queue_update_rings(vdev, n); } @@ -1494,8 +1497,10 @@ void virtio_queue_set_align(VirtIODevice *vdev, int = n, int align) */ assert(k->has_variable_vring_alignment); =20 - vdev->vq[n].vring.align =3D align; - virtio_queue_update_rings(vdev, n); + if (align) { + vdev->vq[n].vring.align =3D align; + virtio_queue_update_rings(vdev, n); + } } =20 static bool virtio_queue_notify_aio_vq(VirtQueue *vq) --=20 2.13.6 From nobody Sun Apr 28 04:50:55 2024 Delivered-To: importer@patchew.org Received-SPF: temperror (zoho.com: Error in retrieving data from DNS) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=temperror (zoho.com: Error in retrieving data from DNS) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1511463793608659.9438835147788; Thu, 23 Nov 2017 11:03:13 -0800 (PST) Received: from localhost ([::1]:45730 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eHwmC-00048X-OH for importer@patchew.org; Thu, 23 Nov 2017 14:03:08 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40950) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eHwkt-00033W-N1 for qemu-devel@nongnu.org; Thu, 23 Nov 2017 14:01:48 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eHwks-0006Sz-UO for qemu-devel@nongnu.org; Thu, 23 Nov 2017 14:01:47 -0500 Received: from mx1.redhat.com ([209.132.183.28]:55946) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eHwks-0006Rb-Iq for qemu-devel@nongnu.org; Thu, 23 Nov 2017 14:01:46 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A550A6A7CD; Thu, 23 Nov 2017 19:01:45 +0000 (UTC) Received: from localhost.localdomain (ovpn-120-135.rdu2.redhat.com [10.10.120.135]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 76A1A61784; Thu, 23 Nov 2017 19:01:43 +0000 (UTC) From: P J P To: Qemu Developers Date: Fri, 24 Nov 2017 00:31:31 +0530 Message-Id: <20171123190131.5138-3-ppandit@redhat.com> In-Reply-To: <20171123190131.5138-1-ppandit@redhat.com> References: <20171123190131.5138-1-ppandit@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Thu, 23 Nov 2017 19:01:45 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 2/2] tests: add test to check VirtQueue object X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: zhangboxian , Stefan Hajnoczi , Prasad J Pandit Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_6 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Prasad J Pandit An uninitialised VirtQueue object or one with Vring.align field set to zero(0) could lead to arithmetic exceptions. Add a unit test to validate it. Signed-off-by: Prasad J Pandit --- tests/virtio-blk-test.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/virtio-blk-test.c b/tests/virtio-blk-test.c index e6fb9bac87..d00ceb0501 100644 --- a/tests/virtio-blk-test.c +++ b/tests/virtio-blk-test.c @@ -674,6 +674,26 @@ static void pci_hotplug(void) qtest_shutdown(qs); } =20 +static void test_vring_align(void) +{ + QPCIBar bar0; + QOSState *qs; + QPCIDevice *dev; + + qs =3D pci_test_start(); + dev =3D qpci_device_find(qs->pcibus, QPCI_DEVFN(4, 0)); + g_assert(dev !=3D NULL); + + qpci_device_enable(dev); + bar0 =3D qpci_iomap(dev, 0, NULL); + + qpci_io_writeb(dev, bar0, VIRTIO_PCI_QUEUE_SEL, 2); + qpci_io_writel(dev, bar0, VIRTIO_PCI_QUEUE_PFN, 1); + + g_free(dev); + qtest_shutdown(qs); +} + static void mmio_basic(void) { QVirtioMMIODevice *dev; @@ -724,6 +744,7 @@ int main(int argc, char **argv) qtest_add_func("/virtio/blk/pci/basic", pci_basic); qtest_add_func("/virtio/blk/pci/indirect", pci_indirect); qtest_add_func("/virtio/blk/pci/config", pci_config); + qtest_add_func("/virtio/blk/pci/vring", test_vring_align); if (strcmp(arch, "i386") =3D=3D 0 || strcmp(arch, "x86_64") =3D=3D= 0) { qtest_add_func("/virtio/blk/pci/msix", pci_msix); qtest_add_func("/virtio/blk/pci/idx", pci_idx); --=20 2.13.6