Commit a7b77835 authored by Dan Carpenter's avatar Dan Carpenter Committed by Felipe Balbi

usb: raw-gadget: Fix copy_to/from_user() checks

The copy_to/from_user() functions return the number of bytes remaining
but we want to return negative error codes.  I changed a couple checks
in raw_ioctl_ep_read() and raw_ioctl_ep0_read() to show that we still
we returning zero on error.

Fixes: f2c2e717 ("usb: gadget: add raw-gadget interface")
Signed-off-by: default avatarDan Carpenter <dan.carpenter@oracle.com>
Reviewed-by: default avatarAndrey Konovalov <andreyknvl@google.com>
Tested-by: default avatarAndrey Konovalov <andreyknvl@google.com>
Signed-off-by: default avatarFelipe Balbi <balbi@kernel.org>
parent 0666aa53
......@@ -400,9 +400,8 @@ static int raw_ioctl_init(struct raw_dev *dev, unsigned long value)
char *udc_device_name;
unsigned long flags;
ret = copy_from_user(&arg, (void __user *)value, sizeof(arg));
if (ret)
return ret;
if (copy_from_user(&arg, (void __user *)value, sizeof(arg)))
return -EFAULT;
switch (arg.speed) {
case USB_SPEED_UNKNOWN:
......@@ -509,15 +508,13 @@ static int raw_ioctl_run(struct raw_dev *dev, unsigned long value)
static int raw_ioctl_event_fetch(struct raw_dev *dev, unsigned long value)
{
int ret = 0;
struct usb_raw_event arg;
unsigned long flags;
struct usb_raw_event *event;
uint32_t length;
ret = copy_from_user(&arg, (void __user *)value, sizeof(arg));
if (ret)
return ret;
if (copy_from_user(&arg, (void __user *)value, sizeof(arg)))
return -EFAULT;
spin_lock_irqsave(&dev->lock, flags);
if (dev->state != STATE_DEV_RUNNING) {
......@@ -545,20 +542,19 @@ static int raw_ioctl_event_fetch(struct raw_dev *dev, unsigned long value)
return -ENODEV;
}
length = min(arg.length, event->length);
ret = copy_to_user((void __user *)value, event,
sizeof(*event) + length);
return ret;
if (copy_to_user((void __user *)value, event, sizeof(*event) + length))
return -EFAULT;
return 0;
}
static void *raw_alloc_io_data(struct usb_raw_ep_io *io, void __user *ptr,
bool get_from_user)
{
int ret;
void *data;
ret = copy_from_user(io, ptr, sizeof(*io));
if (ret)
return ERR_PTR(ret);
if (copy_from_user(io, ptr, sizeof(*io)))
return ERR_PTR(-EFAULT);
if (io->ep >= USB_RAW_MAX_ENDPOINTS)
return ERR_PTR(-EINVAL);
if (!usb_raw_io_flags_valid(io->flags))
......@@ -673,12 +669,13 @@ static int raw_ioctl_ep0_read(struct raw_dev *dev, unsigned long value)
if (IS_ERR(data))
return PTR_ERR(data);
ret = raw_process_ep0_io(dev, &io, data, false);
if (ret < 0) {
kfree(data);
return ret;
}
if (ret)
goto free;
length = min(io.length, (unsigned int)ret);
ret = copy_to_user((void __user *)(value + sizeof(io)), data, length);
if (copy_to_user((void __user *)(value + sizeof(io)), data, length))
ret = -EFAULT;
free:
kfree(data);
return ret;
}
......@@ -967,12 +964,13 @@ static int raw_ioctl_ep_read(struct raw_dev *dev, unsigned long value)
if (IS_ERR(data))
return PTR_ERR(data);
ret = raw_process_ep_io(dev, &io, data, false);
if (ret < 0) {
kfree(data);
return ret;
}
if (ret)
goto free;
length = min(io.length, (unsigned int)ret);
ret = copy_to_user((void __user *)(value + sizeof(io)), data, length);
if (copy_to_user((void __user *)(value + sizeof(io)), data, length))
ret = -EFAULT;
free:
kfree(data);
return ret;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment