Skip to contentSkip to conversation

usb: usbtmc: Add ioctl for generic requests on control pipe

PATCH Superseded Greg Kroah-Hartman 1 participant 1 message May 22, 2018 USB

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Greg Kroah-Hartman

author maintainer
#0 lore raw

PATCH 5/12 usb: usbtmc: Add ioctl for generic requests on control pipe

Quoted 58 lines · On Mon, May 21, 2018 at 10:06:57PM +0000, [email protected] wrote:

Zitat von Greg KH <[email protected]>:

Quoted 55 lines · On Fri, May 18, 2018 at 01:32:51PM +0000, [email protected] wrote:
Quoted 53 lines

Zitat von Greg KH <[email protected]>:

Quoted 29 lines · Guido Kiener
Quoted 28 lines

Add USBTMC_IOCTL_CTRL_REQUEST to send arbitrary requests on the control pipe. Used by specific applications of IVI Foundation, Inc. to implement VISA API functions: viUsbControlIn/Out.

---
 drivers/usb/class/usbtmc.c   | 61 ++++++++++++++++++++++++++++++++++++
 include/uapi/linux/usb/tmc.h | 15 +++++++++
 2 files changed, 76 insertions(+)
drivers/usb/class/usbtmc.c +1 -0
index 152e2daa9644..00c2e51a23a7 100644
@@ -5,6 +5,7 @@
  * Copyright (C) 2007 Stefan Kopp, Gechingen, Germany
  * Copyright (C) 2008 Novell, Inc.
  * Copyright (C) 2008 Greg Kroah-Hartman <[email protected]>
+ * Copyright (C) 2018, IVI Foundation, Inc.
  */
 
 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
@@ -1263,6 +1264,62 @@ static int
 usbtmc_ioctl_indicator_pulse(struct usbtmc_device_data *data)
 	return rv;
 }
 
Quoted 10 lines

drivers/usb/class/usbtmc.c static int · line 1277

Quoted 10 lines
drivers/usb/class/usbtmc.c +10 -0
+static int usbtmc_ioctl_request(struct usbtmc_device_data *data,
+				void __user *arg)
+{
+	struct device *dev = &data->intf->dev;
+	struct usbtmc_ctrlrequest request;
+	u8 *buffer = NULL;
+	int rv;
+	unsigned long res;
+
+	res = copy_from_user(&request, arg, sizeof(struct

usbtmc_ctrlrequest));

Quoted 6 lines

drivers/usb/class/usbtmc.c static int · line 1281

Quoted 4 lines
drivers/usb/class/usbtmc.c +4 -0
+	if (res)
+		return -EFAULT;
+
+	buffer = kmalloc(min_t(u16, 256, request.req.wLength), GFP_KERNEL);

No validation that wLength is a sane number?

Ooops. It should be buffer = kmalloc(max_t(u16, 256, request.req.wLength), GFP_KERNEL);
Then all values of request.req.wLength (0..65535) are ok.

Quoted 5 lines
Quoted 2 lines

Yes, that would make more sense. But you should still reject known-invalid values, and not just "silently fix them up".

When I start here to refuse (current) unknown settings or flags, then I fear that people always want us to change or allow new flags.

That doesn't make sense, you always need to reject unknown values, or people will put crap in them and then get mad in the future when you want to use those fields/values for something else.

Quoted 1 line

A device should always be able to deal with all possible values.

But the kernel has to be sane and properly validate userspace values.

Quoted 4 lines

We
cannot prevent a
user from sending crazy messages to the device and I do not want to develop something like a firewall here.

Sure, I'm not saying to do that, you are not validating the other fields, just the length. The kernel will validate the other fields when you submit the urb.

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
Quoted 10 lines

drivers/usb/class/usbtmc.c static int · line 1288

Quoted 7 lines
drivers/usb/class/usbtmc.c +7 -0
+	rv = usb_control_msg(data->usb_dev,
+			usb_rcvctrlpipe(data->usb_dev, 0),
+			request.req.bRequest,
+			request.req.bRequestType,
+			request.req.wValue,
+			request.req.wIndex,
+			buffer, request.req.wLength, USB_CTRL_GET_TIMEOUT);

request.req.wLength might not be the actual size of buffer here due to your above min_t() check.

Quoted 4 lines
Quoted 1 line

Still wrong given the check above.

I'm missing something. I do not see the error. The size of buffer is just at least 256 bytes. It doesn't matter when request.req.wLength < 256.

If wLength is set to 10000 but buffer really isn't that big, bad things happen...

Quoted 3 lines

I thought this helps the memory management, since we need not to deal with different and odd memory sizes. Maybe we should just alloc always a size of request.req.wLength.

Again, you have to validate that number, if you want to bound it to 256, great, then bound it. Don't half-way do it please.

thanks,

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