Skip to contentSkip to conversation

vfio-mdev: buffer overflow in ioctl()

PATCH In review Dan Carpenter 2 participants 2 messages Jan 7, 2017 – Jan 9, 2017

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 2/3 vfio-mdev: buffer overflow in ioctl()

This is a sample driver for documentation so the impact is probably pretty low. But we should check that bar_index is valid so we don't write beyond the end of the mdev_state->region_info[] array.

Fixes 9d1a546c53b4 ("docs: Sample driver to demonstrate how to use Mediated device framework.")
samples/vfio-mdev/mtty.c +5 -2
index d2656ff569c1..353666d68207 100644
@@ -1073,7 +1073,7 @@ int mtty_get_region_info(struct mdev_device *mdev,
 {
 	unsigned int size = 0;
 	struct mdev_state *mdev_state;
-	int bar_index;
+	u32 bar_index;
 
 	if (!mdev)
 		return -EINVAL;
@@ -1082,8 +1082,11 @@ int mtty_get_region_info(struct mdev_device *mdev,
 	if (!mdev_state)
 		return -EINVAL;
 
-	mutex_lock(&mdev_state->ops_lock);
 	bar_index = region_info->index;
+	if (bar_index >= VFIO_PCI_NUM_REGIONS)
+		return -EINVAL;
+
+	mutex_lock(&mdev_state->ops_lock);
 
 	switch (bar_index) {
 	case VFIO_PCI_CONFIG_REGION_INDEX:
maintainer
#1 lore raw

CC+= Alex

Thanks Dan for fixing.

Reviewed by: Kirti Wankhede <[email protected]>

Thanks,
Kirti

Quoted 34 lines · Dan Carpenter

This is a sample driver for documentation so the impact is probably pretty low. But we should check that bar_index is valid so we don't write beyond the end of the mdev_state->region_info[] array.

Fixes 9d1a546c53b4 ("docs: Sample driver to demonstrate how to use Mediated device framework.")
samples/vfio-mdev/mtty.c +5 -2
index d2656ff569c1..353666d68207 100644
@@ -1073,7 +1073,7 @@ int mtty_get_region_info(struct mdev_device *mdev,
 {
 	unsigned int size = 0;
 	struct mdev_state *mdev_state;
-	int bar_index;
+	u32 bar_index;
 
 	if (!mdev)
 		return -EINVAL;
@@ -1082,8 +1082,11 @@ int mtty_get_region_info(struct mdev_device *mdev,
 	if (!mdev_state)
 		return -EINVAL;
 
-	mutex_lock(&mdev_state->ops_lock);
 	bar_index = region_info->index;
+	if (bar_index >= VFIO_PCI_NUM_REGIONS)
+		return -EINVAL;
+
+	mutex_lock(&mdev_state->ops_lock);
 
 	switch (bar_index) {
 	case VFIO_PCI_CONFIG_REGION_INDEX: