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