Re: [PATCH] USB: more u32 conversion after transfer_buffer_length and actual_length

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Alan Stern wrote:
> On Sun, 8 Mar 2009, Roel Kluin wrote:
> 
>> A test for a negative urb->transfer_buffer_length has become obsolete.
> 
>> usb_internal_control_msg() returns both error values and a length. To ensure
>> that it does not return an unsigned length that is converted into a negative,
>> the returned length is limited to INT_MAX.
> 
> Instead of doing it this way, it would be simpler and more robust to
> change the test for a negative urb->transfer_buffer_length in
> submit_urb() into a test for urb->transfer_buffer_length > INT_MAX.  

While I think this test may be useful for conversions elsewhere, In this
functions actual_length is converted, not transfer_buffer_length, or am I
missing something?

I have adapted it however, since it should occur in usb_start_wait_urb().
in that function the first conversion occurs.

> (Strictly speaking, this shouldn't be INT_MAX -- it should be the
> largest possible 32-bit signed integer.  But until 64-bit integers
> become the default we will be safe...)

The largest possible signed integer?

>  Then we would know that the
> length returned by usb_internal_control_msg() could never be too large.
> 
> Alan Stern

how's this?
------------------------------>8-------------8<---------------------------------
transfer_buffer_length and actual_length have become unsigned, therefore some
additional conversion of local variables, function arguments and print
specifications is desired. 
A test for a negative urb->transfer_buffer_length became obsolete; instead
we ensure that it does not exceed INT_MAX.

rh_string() does no longer return -EPIPE in the case of an unsupported ID.
Instead its only caller, rh_call_control() does the check.

usb_start_wait_urb() returns both error values and a length. To ensure that it
does not return an unsigned length that is converted into a negative, the
returned length is limited to INT_MAX.

Signed-off-by: Roel Kluin <roel.kluin@xxxxxxxxx>
---
 drivers/usb/core/devio.c   |    6 +++---
 drivers/usb/core/hcd.c     |   31 ++++++++++++-------------------
 drivers/usb/core/hub.c     |    2 +-
 drivers/usb/core/message.c |    8 ++++----
 drivers/usb/core/urb.c     |    2 +-
 5 files changed, 21 insertions(+), 28 deletions(-)

diff --git a/drivers/usb/core/devio.c b/drivers/usb/core/devio.c
index 7513bb0..64ed07c 100644
--- a/drivers/usb/core/devio.c
+++ b/drivers/usb/core/devio.c
@@ -302,7 +302,7 @@ static inline struct async *async_getpending(struct dev_state *ps,
 
 static void snoop_urb(struct urb *urb, void __user *userurb)
 {
-	int j;
+	u32 j;
 	unsigned char *data = urb->transfer_buffer;
 
 	if (!usbfs_snoop)
@@ -311,9 +311,9 @@ static void snoop_urb(struct urb *urb, void __user *userurb)
 	dev_info(&urb->dev->dev, "direction=%s\n",
 			usb_urb_dir_in(urb) ? "IN" : "OUT");
 	dev_info(&urb->dev->dev, "userurb=%p\n", userurb);
-	dev_info(&urb->dev->dev, "transfer_buffer_length=%d\n",
+	dev_info(&urb->dev->dev, "transfer_buffer_length=%u\n",
 		 urb->transfer_buffer_length);
-	dev_info(&urb->dev->dev, "actual_length=%d\n", urb->actual_length);
+	dev_info(&urb->dev->dev, "actual_length=%u\n", urb->actual_length);
 	dev_info(&urb->dev->dev, "data: ");
 	for (j = 0; j < urb->transfer_buffer_length; ++j)
 		printk("%02x ", data[j]);
diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index 3c711db..8cec7cc 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -279,9 +279,9 @@ static const u8 hs_rh_config_descriptor [] = {
  * helper routine for returning string descriptors in UTF-16LE
  * input can actually be ISO-8859-1; ASCII is its 7-bit subset
  */
-static int ascii2utf (char *s, u8 *utf, int utfmax)
+static u32 ascii2utf(char *s, u8 *utf, int utfmax)
 {
-	int retval;
+	u32 retval;
 
 	for (retval = 0; *s && utfmax > 1; utfmax -= 2, retval += 2) {
 		*utf++ = *s++;
@@ -304,19 +304,15 @@ static int ascii2utf (char *s, u8 *utf, int utfmax)
  * Produces either a manufacturer, product or serial number string for the
  * virtual root hub device.
  */
-static int rh_string (
-	int		id,
-	struct usb_hcd	*hcd,
-	u8		*data,
-	int		len
-) {
+static u32 rh_string(int id, struct usb_hcd *hcd, u8 *data, u32 len)
+{
 	char buf [100];
 
 	// language ids
 	if (id == 0) {
 		buf[0] = 4;    buf[1] = 3;	/* 4 bytes string data */
 		buf[2] = 0x09; buf[3] = 0x04;	/* MSFT-speak for "en-us" */
-		len = min (len, 4);
+		len = min_t(u32, len, 4);
 		memcpy (data, buf, len);
 		return len;
 
@@ -332,10 +328,7 @@ static int rh_string (
 	} else if (id == 3) {
 		snprintf (buf, sizeof buf, "%s %s %s", init_utsname()->sysname,
 			init_utsname()->release, hcd->driver->description);
-
-	// unsupported IDs --> "protocol stall"
-	} else
-		return -EPIPE;
+	}
 
 	switch (len) {		/* All cases fall through */
 	default:
@@ -360,9 +353,8 @@ static int rh_call_control (struct usb_hcd *hcd, struct urb *urb)
 	u8		tbuf [sizeof (struct usb_hub_descriptor)]
 		__attribute__((aligned(4)));
 	const u8	*bufp = tbuf;
-	int		len = 0;
+	u32		len = 0;
 	int		status;
-	int		n;
 	u8		patch_wakeup = 0;
 	u8		patch_protocol = 0;
 
@@ -456,10 +448,11 @@ static int rh_call_control (struct usb_hcd *hcd, struct urb *urb)
 				patch_wakeup = 1;
 			break;
 		case USB_DT_STRING << 8:
-			n = rh_string (wValue & 0xff, hcd, ubuf, wLength);
-			if (n < 0)
+			if ((wValue & 0xff) < 4)
+				urb->actual_length = rh_string(wValue & 0xff,
+						hcd, ubuf, wLength);
+			else /* unsupported IDs --> "protocol stall" */
 				goto error;
-			urb->actual_length = n;
 			break;
 		default:
 			goto error;
@@ -629,7 +622,7 @@ static int rh_queue_status (struct usb_hcd *hcd, struct urb *urb)
 {
 	int		retval;
 	unsigned long	flags;
-	int		len = 1 + (urb->dev->maxchild / 8);
+	u32		len = 1 + (urb->dev->maxchild / 8);
 
 	spin_lock_irqsave (&hcd_root_hub_lock, flags);
 	if (hcd->status_urb || urb->transfer_buffer_length < len) {
diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index cd50d86..9e09539 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -392,7 +392,7 @@ static void hub_irq(struct urb *urb)
 {
 	struct usb_hub *hub = urb->context;
 	int status = urb->status;
-	int i;
+	u32 i;
 	unsigned long bits;
 
 	switch (status) {
diff --git a/drivers/usb/core/message.c b/drivers/usb/core/message.c
index 49e7f56..cd77357 100644
--- a/drivers/usb/core/message.c
+++ b/drivers/usb/core/message.c
@@ -59,7 +59,7 @@ static int usb_start_wait_urb(struct urb *urb, int timeout, int *actual_length)
 		retval = (ctx.status == -ENOENT ? -ETIMEDOUT : ctx.status);
 
 		dev_dbg(&urb->dev->dev,
-			"%s timed out on ep%d%s len=%d/%d\n",
+			"%s timed out on ep%d%s len=%u/%u\n",
 			current->comm,
 			usb_endpoint_num(&urb->ep->desc),
 			usb_urb_dir_in(urb) ? "in" : "out",
@@ -69,7 +69,7 @@ static int usb_start_wait_urb(struct urb *urb, int timeout, int *actual_length)
 		retval = ctx.status;
 out:
 	if (actual_length)
-		*actual_length = urb->actual_length;
+		*actual_length = min_t(u32, urb->actual_length, INT_MAX);
 
 	usb_free_urb(urb);
 	return retval;
@@ -402,7 +402,7 @@ int usb_sg_init(struct usb_sg_request *io, struct usb_device *dev,
 		urb_flags |= URB_SHORT_NOT_OK;
 
 	for_each_sg(sg, sg, io->entries, i) {
-		unsigned len;
+		size_t len;
 
 		io->urbs[i] = usb_alloc_urb(0, mem_flags);
 		if (!io->urbs[i]) {
@@ -449,7 +449,7 @@ int usb_sg_init(struct usb_sg_request *io, struct usb_device *dev,
 		}
 
 		if (length) {
-			len = min_t(unsigned, len, length);
+			len = min_t(size_t, len, length);
 			length -= len;
 			if (length == 0)
 				io->entries = i + 1;
diff --git a/drivers/usb/core/urb.c b/drivers/usb/core/urb.c
index 58bc5e3..673e2fb 100644
--- a/drivers/usb/core/urb.c
+++ b/drivers/usb/core/urb.c
@@ -370,7 +370,7 @@ int usb_submit_urb(struct urb *urb, gfp_t mem_flags)
 	}
 
 	/* the I/O buffer must be mapped/unmapped, except when length=0 */
-	if (urb->transfer_buffer_length < 0)
+	if (urb->transfer_buffer_length > INT_MAX)
 		return -EMSGSIZE;
 
 #ifdef DEBUG
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Media]     [Linux Input]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Old Linux USB Devel Archive]

  Powered by Linux