dafbaf8
From patchwork Wed Oct 12 16:51:24 2016
dafbaf8
Content-Type: text/plain; charset="utf-8"
dafbaf8
MIME-Version: 1.0
dafbaf8
Content-Transfer-Encoding: 7bit
dafbaf8
Subject: [v3] vfio/pci: Fix integer overflows, bitmask check
dafbaf8
From: Vlad Tsyrklevich <vlad@tsyrklevich.net>
dafbaf8
X-Patchwork-Id: 9373631
dafbaf8
Message-Id: <1476291084-50737-1-git-send-email-vlad@tsyrklevich.net>
dafbaf8
To: kvm@vger.kernel.org
dafbaf8
Cc: alex.williamson@redhat.com, Vlad Tsyrklevich <vlad@tsyrklevich.net>
dafbaf8
Date: Wed, 12 Oct 2016 18:51:24 +0200
dafbaf8
dafbaf8
The VFIO_DEVICE_SET_IRQS ioctl did not sufficiently sanitize
dafbaf8
user-supplied integers, potentially allowing memory corruption. This
dafbaf8
patch adds appropriate integer overflow checks, checks the range bounds
dafbaf8
for VFIO_IRQ_SET_DATA_NONE, and also verifies that only single element
dafbaf8
in the VFIO_IRQ_SET_DATA_TYPE_MASK bitmask is set.
dafbaf8
VFIO_IRQ_SET_ACTION_TYPE_MASK is already correctly checked later in
dafbaf8
vfio_pci_set_irqs_ioctl().
dafbaf8
dafbaf8
Furthermore, a kzalloc is changed to a kcalloc because the use of a
dafbaf8
kzalloc with an integer multiplication allowed an integer overflow
dafbaf8
condition to be reached without this patch. kcalloc checks for overflow
dafbaf8
and should prevent a similar occurrence.
dafbaf8
dafbaf8
Signed-off-by: Vlad Tsyrklevich <vlad@tsyrklevich.net>
dafbaf8
---
dafbaf8
 drivers/vfio/pci/vfio_pci.c       | 33 +++++++++++++++++++++------------
dafbaf8
 drivers/vfio/pci/vfio_pci_intrs.c |  2 +-
dafbaf8
 2 files changed, 22 insertions(+), 13 deletions(-)
dafbaf8
dafbaf8
diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c
dafbaf8
index d624a52..031bc08 100644
dafbaf8
--- a/drivers/vfio/pci/vfio_pci.c
dafbaf8
+++ b/drivers/vfio/pci/vfio_pci.c
dafbaf8
@@ -829,8 +829,9 @@ static long vfio_pci_ioctl(void *device_data,
dafbaf8
 
dafbaf8
 	} else if (cmd == VFIO_DEVICE_SET_IRQS) {
dafbaf8
 		struct vfio_irq_set hdr;
dafbaf8
+		size_t size;
dafbaf8
 		u8 *data = NULL;
dafbaf8
-		int ret = 0;
dafbaf8
+		int max, ret = 0;
dafbaf8
 
dafbaf8
 		minsz = offsetofend(struct vfio_irq_set, count);
dafbaf8
 
dafbaf8
@@ -838,23 +839,31 @@ static long vfio_pci_ioctl(void *device_data,
dafbaf8
 			return -EFAULT;
dafbaf8
 
dafbaf8
 		if (hdr.argsz < minsz || hdr.index >= VFIO_PCI_NUM_IRQS ||
dafbaf8
+		    hdr.count >= (U32_MAX - hdr.start) ||
dafbaf8
 		    hdr.flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK |
dafbaf8
 				  VFIO_IRQ_SET_ACTION_TYPE_MASK))
dafbaf8
 			return -EINVAL;
dafbaf8
 
dafbaf8
-		if (!(hdr.flags & VFIO_IRQ_SET_DATA_NONE)) {
dafbaf8
-			size_t size;
dafbaf8
-			int max = vfio_pci_get_irq_count(vdev, hdr.index);
dafbaf8
+		max = vfio_pci_get_irq_count(vdev, hdr.index);
dafbaf8
+		if (hdr.start >= max || hdr.start + hdr.count > max)
dafbaf8
+			return -EINVAL;
dafbaf8
 
dafbaf8
-			if (hdr.flags & VFIO_IRQ_SET_DATA_BOOL)
dafbaf8
-				size = sizeof(uint8_t);
dafbaf8
-			else if (hdr.flags & VFIO_IRQ_SET_DATA_EVENTFD)
dafbaf8
-				size = sizeof(int32_t);
dafbaf8
-			else
dafbaf8
-				return -EINVAL;
dafbaf8
+		switch (hdr.flags & VFIO_IRQ_SET_DATA_TYPE_MASK) {
dafbaf8
+		case VFIO_IRQ_SET_DATA_NONE:
dafbaf8
+			size = 0;
dafbaf8
+			break;
dafbaf8
+		case VFIO_IRQ_SET_DATA_BOOL:
dafbaf8
+			size = sizeof(uint8_t);
dafbaf8
+			break;
dafbaf8
+		case VFIO_IRQ_SET_DATA_EVENTFD:
dafbaf8
+			size = sizeof(int32_t);
dafbaf8
+			break;
dafbaf8
+		default:
dafbaf8
+			return -EINVAL;
dafbaf8
+		}
dafbaf8
 
dafbaf8
-			if (hdr.argsz - minsz < hdr.count * size ||
dafbaf8
-			    hdr.start >= max || hdr.start + hdr.count > max)
dafbaf8
+		if (size) {
dafbaf8
+			if (hdr.argsz - minsz < hdr.count * size)
dafbaf8
 				return -EINVAL;
dafbaf8
 
dafbaf8
 			data = memdup_user((void __user *)(arg + minsz),
dafbaf8
diff --git a/drivers/vfio/pci/vfio_pci_intrs.c b/drivers/vfio/pci/vfio_pci_intrs.c
dafbaf8
index c2e6089..1c46045 100644
dafbaf8
--- a/drivers/vfio/pci/vfio_pci_intrs.c
dafbaf8
+++ b/drivers/vfio/pci/vfio_pci_intrs.c
dafbaf8
@@ -256,7 +256,7 @@ static int vfio_msi_enable(struct vfio_pci_device *vdev, int nvec, bool msix)
dafbaf8
 	if (!is_irq_none(vdev))
dafbaf8
 		return -EINVAL;
dafbaf8
 
dafbaf8
-	vdev->ctx = kzalloc(nvec * sizeof(struct vfio_pci_irq_ctx), GFP_KERNEL);
dafbaf8
+	vdev->ctx = kcalloc(nvec, sizeof(struct vfio_pci_irq_ctx), GFP_KERNEL);
dafbaf8
 	if (!vdev->ctx)
dafbaf8
 		return -ENOMEM;
dafbaf8