Skip to contentSkip to conversation

vfio-mdev: return -EFAULT if copy_to_user() fails

PATCH In review Dan Carpenter 3 participants 3 messages Jan 7, 2017 – Jan 11, 2017

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

PATCH 1/3 vfio-mdev: return -EFAULT if copy_to_user() fails

The copy_to_user() function returns the number of bytes which it wasn't able to copy but we want to return a negative error code.

Fixes 9d1a546c53b4 ("docs: Sample driver to demonstrate how to use Mediated device framework.")
samples/vfio-mdev/mtty.c +12 -3
index 919c10d5b12e..d2656ff569c1 100644
@@ -1180,7 +1180,10 @@ static long mtty_ioctl(struct mdev_device *mdev, unsigned int cmd,
 
 		memcpy(&mdev_state->dev_info, &info, sizeof(info));
 
-		return copy_to_user((void __user *)arg, &info, minsz);
+		if (copy_to_user((void __user *)arg, &info, minsz))
+			return -EFAULT;
+
+		return 0;
 	}
 	case VFIO_DEVICE_GET_REGION_INFO:
 	{
@@ -1201,7 +1204,10 @@ static long mtty_ioctl(struct mdev_device *mdev, unsigned int cmd,
 		if (ret)
 			return ret;
 
-		return copy_to_user((void __user *)arg, &info, minsz);
+		if (copy_to_user((void __user *)arg, &info, minsz))
+			return -EFAULT;
+
+		return 0;
 	}
 
 	case VFIO_DEVICE_GET_IRQ_INFO:
@@ -1224,7 +1230,10 @@ static long mtty_ioctl(struct mdev_device *mdev, unsigned int cmd,
 		if (info.count = -1)
 			return -EINVAL;
 
-		return copy_to_user((void __user *)arg, &info, minsz);
+		if (copy_to_user((void __user *)arg, &info, minsz))
+			return -EFAULT;
+
+		return 0;
 	}
 	case VFIO_DEVICE_SET_IRQS:
 	{
maintainer
#1 lore raw

CC+= Alex to pull this change.

Reviewed by: Kirti Wankhede <[email protected]>

Thanks,
Kirti

Quoted 47 lines · Dan Carpenter

The copy_to_user() function returns the number of bytes which it wasn't able to copy but we want to return a negative error code.

Fixes 9d1a546c53b4 ("docs: Sample driver to demonstrate how to use Mediated device framework.")
samples/vfio-mdev/mtty.c +12 -3
index 919c10d5b12e..d2656ff569c1 100644
@@ -1180,7 +1180,10 @@ static long mtty_ioctl(struct mdev_device *mdev, unsigned int cmd,
 
 		memcpy(&mdev_state->dev_info, &info, sizeof(info));
 
-		return copy_to_user((void __user *)arg, &info, minsz);
+		if (copy_to_user((void __user *)arg, &info, minsz))
+			return -EFAULT;
+
+		return 0;
 	}
 	case VFIO_DEVICE_GET_REGION_INFO:
 	{
@@ -1201,7 +1204,10 @@ static long mtty_ioctl(struct mdev_device *mdev, unsigned int cmd,
 		if (ret)
 			return ret;
 
-		return copy_to_user((void __user *)arg, &info, minsz);
+		if (copy_to_user((void __user *)arg, &info, minsz))
+			return -EFAULT;
+
+		return 0;
 	}
 
 	case VFIO_DEVICE_GET_IRQ_INFO:
@@ -1224,7 +1230,10 @@ static long mtty_ioctl(struct mdev_device *mdev, unsigned int cmd,
 		if (info.count == -1)
 			return -EINVAL;
 
-		return copy_to_user((void __user *)arg, &info, minsz);
+		if (copy_to_user((void __user *)arg, &info, minsz))
+			return -EFAULT;
+
+		return 0;
 	}
 	case VFIO_DEVICE_SET_IRQS:
 	{
#2 lore raw
Quoted 3 lines · On Tue, 10 Jan 2017 00:07:23 +0530 Kirti Wankhede <[email protected]> wrote:

CC+= Alex to pull this change.

Reviewed by: Kirti Wankhede <[email protected]>

Thanks Dan. Applied all three to for-linus for v4.10 with Kirti's R-b. Thanks,

Alex

Quoted 52 lines · Dan Carpenter
Quoted 37 lines

The copy_to_user() function returns the number of bytes which it wasn't able to copy but we want to return a negative error code.

Fixes 9d1a546c53b4 ("docs: Sample driver to demonstrate how to use Mediated device framework.")
samples/vfio-mdev/mtty.c +8 -2
index 919c10d5b12e..d2656ff569c1 100644
@@ -1180,7 +1180,10 @@ static long mtty_ioctl(struct mdev_device *mdev, unsigned int cmd,
 
 		memcpy(&mdev_state->dev_info, &info, sizeof(info));
 
-		return copy_to_user((void __user *)arg, &info, minsz);
+		if (copy_to_user((void __user *)arg, &info, minsz))
+			return -EFAULT;
+
+		return 0;
 	}
 	case VFIO_DEVICE_GET_REGION_INFO:
 	{
@@ -1201,7 +1204,10 @@ static long mtty_ioctl(struct mdev_device *mdev, unsigned int cmd,
 		if (ret)
 			return ret;
 
-		return copy_to_user((void __user *)arg, &info, minsz);
+		if (copy_to_user((void __user *)arg, &info, minsz))
+			return -EFAULT;
+
+		return 0;
 	}
 
 	case VFIO_DEVICE_GET_IRQ_INFO:
@@ -1224,7 +1230,10 @@ static long mtty_ioctl(struct mdev_device *mdev, unsigned int cmd,
 		if (info.count = -1)

samples/vfio-mdev/mtty.c static long mtty_ioctl(struct mdev_device *mdev, unsigned int cmd, · line 1239

Quoted 10 lines
samples/vfio-mdev/mtty.c +4 -1
 			return -EINVAL;
 
-		return copy_to_user((void __user *)arg, &info, minsz);
+		if (copy_to_user((void __user *)arg, &info, minsz))
+			return -EFAULT;
+
+		return 0;
 	}
 	case VFIO_DEVICE_SET_IRQS:
 	{

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html