Commit 665d7662 authored by Guus Sliepen's avatar Guus Sliepen Committed by Greg Kroah-Hartman

USB: usbtmc: sanity checks for DEV_DEP_MSG_IN urbs

According to the specifications, an instrument should not return more data in a
DEV_DEP_MSG_IN urb than requested.  However, some instruments can send more
than requested. This could cause the kernel to write the extra data past the
end of the buffer provided by read().

Fix this by checking that the value of the TranserSize field is not larger than
the urb itself and not larger than the size of the userspace buffer. Also
correctly decrement the remaining size of the buffer when userspace read()s
more than USBTMC_SIZE_IOBUFFER.
Signed-off-by: default avatarGuus Sliepen <guus@sliepen.org>
Cc: stable <stable@kernel.org>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@suse.de>
parent a9d43091
...@@ -369,13 +369,13 @@ static ssize_t usbtmc_read(struct file *filp, char __user *buf, ...@@ -369,13 +369,13 @@ static ssize_t usbtmc_read(struct file *filp, char __user *buf,
{ {
struct usbtmc_device_data *data; struct usbtmc_device_data *data;
struct device *dev; struct device *dev;
unsigned long int n_characters; u32 n_characters;
u8 *buffer; u8 *buffer;
int actual; int actual;
int done; size_t done;
int remaining; size_t remaining;
int retval; int retval;
int this_part; size_t this_part;
/* Get pointer to private data structure */ /* Get pointer to private data structure */
data = filp->private_data; data = filp->private_data;
...@@ -461,6 +461,18 @@ static ssize_t usbtmc_read(struct file *filp, char __user *buf, ...@@ -461,6 +461,18 @@ static ssize_t usbtmc_read(struct file *filp, char __user *buf,
(buffer[6] << 16) + (buffer[6] << 16) +
(buffer[7] << 24); (buffer[7] << 24);
/* Ensure the instrument doesn't lie about it */
if(n_characters > actual - 12) {
dev_err(dev, "Device lies about message size: %zu > %zu\n", n_characters, actual - 12);
n_characters = actual - 12;
}
/* Ensure the instrument doesn't send more back than requested */
if(n_characters > this_part) {
dev_err(dev, "Device returns more than requested: %zu > %zu\n", done + n_characters, done + this_part);
n_characters = this_part;
}
/* Copy buffer to user space */ /* Copy buffer to user space */
if (copy_to_user(buf + done, &buffer[12], n_characters)) { if (copy_to_user(buf + done, &buffer[12], n_characters)) {
/* There must have been an addressing problem */ /* There must have been an addressing problem */
...@@ -471,6 +483,8 @@ static ssize_t usbtmc_read(struct file *filp, char __user *buf, ...@@ -471,6 +483,8 @@ static ssize_t usbtmc_read(struct file *filp, char __user *buf,
done += n_characters; done += n_characters;
if (n_characters < USBTMC_SIZE_IOBUFFER) if (n_characters < USBTMC_SIZE_IOBUFFER)
remaining = 0; remaining = 0;
else
remaining -= n_characters;
} }
/* Update file position value */ /* Update file position value */
......
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