From patchwork Fri Apr 26 08:34:02 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Wang X-Patchwork-Id: 239760 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 4AD182C0104 for ; Fri, 26 Apr 2013 18:44:14 +1000 (EST) Received: from localhost ([::1]:41307 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UVeGJ-0005Oi-1U for incoming@patchwork.ozlabs.org; Fri, 26 Apr 2013 04:44:11 -0400 Received: from eggs.gnu.org ([208.118.235.92]:60616) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UVeFw-0005N0-0h for qemu-devel@nongnu.org; Fri, 26 Apr 2013 04:43:50 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UVeFu-0007Oy-QV for qemu-devel@nongnu.org; Fri, 26 Apr 2013 04:43:47 -0400 Received: from mx1.redhat.com ([209.132.183.28]:35593) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UVeFu-0007Om-J7 for qemu-devel@nongnu.org; Fri, 26 Apr 2013 04:43:46 -0400 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r3Q8hjMU002716 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Fri, 26 Apr 2013 04:43:45 -0400 Received: from amd-6168-8-1.englab.nay.redhat.com (amd-6168-8-1.englab.nay.redhat.com [10.66.104.52]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id r3Q8hg17022139; Fri, 26 Apr 2013 04:43:42 -0400 From: Jason Wang To: aliguori@us.ibm.com, mst@redhat.com, qemu-devel@nongnu.org Date: Fri, 26 Apr 2013 16:34:02 +0800 Message-Id: <1366965244-20542-1-git-send-email-jasowang@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Jason Wang , pmatouse@redhat.com Subject: [Qemu-devel] [PATCH 1/3] virtio-pci: properly validate address before accessing config X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org There are several several issues in the current checking: - The check was based on the minus of unsigned values which can overflow - It was done after .{set|get}_config() which can lead crash when config_len is zero since vdev->config is NULL Fix this by: - Validate the address in virtio_pci_config_{read|write}() before .{set|get}_config - Use addition instead minus to do the validation Cc: Michael S. Tsirkin Cc: Petr Matousek Signed-off-by: Jason Wang --- hw/virtio/virtio-pci.c | 9 +++++++++ hw/virtio/virtio.c | 18 ------------------ 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/hw/virtio/virtio-pci.c b/hw/virtio/virtio-pci.c index a1f15a8..7f6c7d1 100644 --- a/hw/virtio/virtio-pci.c +++ b/hw/virtio/virtio-pci.c @@ -400,6 +400,10 @@ static uint64_t virtio_pci_config_read(void *opaque, hwaddr addr, } addr -= config; + if (addr + size > proxy->vdev->config_len) { + return (uint32_t)-1; + } + switch (size) { case 1: val = virtio_config_readb(proxy->vdev, addr); @@ -430,6 +434,11 @@ static void virtio_pci_config_write(void *opaque, hwaddr addr, return; } addr -= config; + + if (addr + size > proxy->vdev->config_len) { + return; + } + /* * Virtio-PCI is odd. Ioports are LE but config space is target native * endian. diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index 1c2282c..3397b5e 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -563,9 +563,6 @@ uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr) vdev->get_config(vdev, vdev->config); - if (addr > (vdev->config_len - sizeof(val))) - return (uint32_t)-1; - val = ldub_p(vdev->config + addr); return val; } @@ -576,9 +573,6 @@ uint32_t virtio_config_readw(VirtIODevice *vdev, uint32_t addr) vdev->get_config(vdev, vdev->config); - if (addr > (vdev->config_len - sizeof(val))) - return (uint32_t)-1; - val = lduw_p(vdev->config + addr); return val; } @@ -589,9 +583,6 @@ uint32_t virtio_config_readl(VirtIODevice *vdev, uint32_t addr) vdev->get_config(vdev, vdev->config); - if (addr > (vdev->config_len - sizeof(val))) - return (uint32_t)-1; - val = ldl_p(vdev->config + addr); return val; } @@ -600,9 +591,6 @@ void virtio_config_writeb(VirtIODevice *vdev, uint32_t addr, uint32_t data) { uint8_t val = data; - if (addr > (vdev->config_len - sizeof(val))) - return; - stb_p(vdev->config + addr, val); if (vdev->set_config) @@ -613,9 +601,6 @@ void virtio_config_writew(VirtIODevice *vdev, uint32_t addr, uint32_t data) { uint16_t val = data; - if (addr > (vdev->config_len - sizeof(val))) - return; - stw_p(vdev->config + addr, val); if (vdev->set_config) @@ -626,9 +611,6 @@ void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data) { uint32_t val = data; - if (addr > (vdev->config_len - sizeof(val))) - return; - stl_p(vdev->config + addr, val); if (vdev->set_config)