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