USB: hub: fix SS hub-descriptor handling
Subsystems (2)
PATCH 3/6 USB: hub: fix SS hub-descriptor handling
In reply to a message not in the archiveA SuperSpeed hub descriptor does not have any variable-length fields so bail out when reading a short descriptor.
This avoids parsing and leaking two bytes of uninitialised slab data through sysfs removable-attributes.
--- drivers/usb/core/hub.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/usb/core/hub.c +6 -2
@@ -380,8 +380,12 @@ static int get_hub_descriptor(struct usb_device *hdev, void *data)USB_REQ_GET_DESCRIPTOR ,USB_DIR_IN |USB_RT_HUB , dtype <<8 ,0 , data, size, USB_CTRL_GET_TIMEOUT); -if (ret >= (USB_DT_HUB_NONVAR_SIZE +2 )) +if (hub_is_superspeed (hdev)) { +if (ret == size) +return ret; + }else if (ret >= (USB_DT_HUB_NONVAR_SIZE +2 )) {return ret; + } }return -EINVAL ; }
@@ -1321,7 +1325,7 @@ static int hub_configure(struct usb_hub *hub,/* Request the entire hub descriptor. * hub->descriptor can handle USB_MAXCHILDREN ports, -* but the hub can/will return fewer bytes here. +* but a (non-SS) hub can/will return fewer bytes here. */ ret =get_hub_descriptor (hdev, hub->descriptor);if (ret <0 ) {
Signature · 1 line
2.13.0
PATCH 4/6 USB: hub: fix non-SS hub-descriptor handling
In reply to a message not in the archiveAdd missing sanity check on the non-SuperSpeed hub-descriptor length in order to avoid parsing and leaking two bytes of uninitialised slab data through sysfs removable-attributes (or a compound-device debug statement).
Note that we only make sure that the DeviceRemovable field is always present (and specifically ignore the unused PortPwrCtrlMask field) in order to continue support any hubs with non-compliant descriptors. As a further safeguard, the descriptor buffer is also cleared.
--- drivers/usb/core/hub.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/usb/core/hub.c +9 -4
@@ -362,7 +362,8 @@ static void usb_set_lpm_parameters(struct usb_device *udev) }/* USB 2.0 spec Section 11.24.4.5 */ -static int get_hub_descriptor (struct usb_device *hdev,void *data) +static int get_hub_descriptor (struct usb_device *hdev, +struct usb_hub_descriptor *desc) {int i, ret, size;unsigned dtype;
@@ -378,12 +379,16 @@ static int get_hub_descriptor(struct usb_device *hdev, void *data)for (i =0 ; i <3 ; i++) { ret =usb_control_msg (hdev,usb_rcvctrlpipe (hdev,0 ),USB_REQ_GET_DESCRIPTOR ,USB_DIR_IN |USB_RT_HUB , - dtype <<8 ,0 , data, size, + dtype <<8 ,0 , desc, size,USB_CTRL_GET_TIMEOUT );if (hub_is_superspeed (hdev)) {if (ret == size)return ret; - }else if (ret >= (USB_DT_HUB_NONVAR_SIZE +2 )) { + }else if (ret >=USB_DT_HUB_NONVAR_SIZE +2 ) { +/* Make sure we have the DeviceRemovable field. */ + size =USB_DT_HUB_NONVAR_SIZE + desc->bNbrPorts /8 +1 ; +if (ret < size) +return -EMSGSIZE ;return ret; } }
@@ -1317,7 +1322,7 @@ static int hub_configure(struct usb_hub *hub, }mutex_init (&hub->status_mutex); - hub->descriptor =kmalloc (sizeof (*hub->descriptor),GFP_KERNEL ); + hub->descriptor =kzalloc (sizeof (*hub->descriptor),GFP_KERNEL );if (!hub->descriptor) { ret = -ENOMEM ; goto fail;
Signature · 1 line
2.13.0
Quoted 2 lines · Johan Hovold
A SuperSpeed hub descriptor does not have any variable-length fields so bail out when reading a short descriptor.
You mean: bail out when reading a descriptor that is not exactly the right length. The existing code already bails out when it reads a short descriptor.
Alan Stern
Quoted 39 lines
This avoids parsing and leaking two bytes of uninitialised slab data through sysfs removable-attributes.
Fixes dbe79bbe9dcb ("USB 3.0 Hub Changes")Cc stable <[email protected]> # 2.6.39Cc John Youn <[email protected]>Signed-off-by Johan Hovold <[email protected]>--- drivers/usb/core/hub.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)drivers/usb/core/hub.c +6
-2@@ -380,8 +380,12 @@ static int get_hub_descriptor(struct usb_device *hdev, void *data)USB_REQ_GET_DESCRIPTOR ,USB_DIR_IN |USB_RT_HUB , dtype <<8 ,0 , data, size, USB_CTRL_GET_TIMEOUT); -if (ret >= (USB_DT_HUB_NONVAR_SIZE +2 )) +if (hub_is_superspeed (hdev)) { +if (ret == size) +return ret; + }else if (ret >= (USB_DT_HUB_NONVAR_SIZE +2 )) {return ret; + } }return -EINVAL ; }@@ -1321,7 +1325,7 @@ static int hub_configure(struct usb_hub *hub,/* Request the entire hub descriptor. * hub->descriptor can handle USB_MAXCHILDREN ports, -* but the hub can/will return fewer bytes here. +* but a (non-SS) hub can/will return fewer bytes here. */ ret =get_hub_descriptor (hdev, hub->descriptor);if (ret <0 ) {
Re: [PATCH 4/6] USB: hub: fix non-SS hub-descriptor handling
In reply to Johan HovoldQuoted 37 lines · Johan Hovold
Add missing sanity check on the non-SuperSpeed hub-descriptor length in order to avoid parsing and leaking two bytes of uninitialised slab data through sysfs removable-attributes (or a compound-device debug statement).
Note that we only make sure that the DeviceRemovable field is always present (and specifically ignore the unused PortPwrCtrlMask field) in order to continue support any hubs with non-compliant descriptors. As a further safeguard, the descriptor buffer is also cleared.
Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Cc stable <[email protected]> # 2.6.12Signed-off-by Johan Hovold <[email protected]>--- drivers/usb/core/hub.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)drivers/usb/core/hub.c +3
-2@@ -362,7 +362,8 @@ static void usb_set_lpm_parameters(struct usb_device *udev) }/* USB 2.0 spec Section 11.24.4.5 */ -static int get_hub_descriptor (struct usb_device *hdev,void *data) +static int get_hub_descriptor (struct usb_device *hdev, +struct usb_hub_descriptor *desc) {int i, ret, size;unsigned dtype;@@ -378,12 +379,16 @@ static int get_hub_descriptor(struct usb_device *hdev, void *data)for (i =0 ; i <3 ; i++) { ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB, - dtype << 8, 0, data, size, + dtype << 8, 0, desc, size,
drivers/usb/core/hub.c static int get_hub_descriptor(struct usb_device *hdev, void *data) · line 391
Quoted 10 lines
drivers/usb/core/hub.c +5
-1USB_CTRL_GET_TIMEOUT);if (hub_is_superspeed (hdev)) {if (ret == size)return ret; - }else if (ret >= (USB_DT_HUB_NONVAR_SIZE +2 )) { + }else if (ret >=USB_DT_HUB_NONVAR_SIZE +2 ) { +/* Make sure we have the DeviceRemovable field. */ + size =USB_DT_HUB_NONVAR_SIZE + desc->bNbrPorts /8 +1 ; +if (ret < size) +return -EMSGSIZE ;
The logic could be simplified a little. Since we don't really care about the return code when an error occurs, you could just do:
} else if (ret >= USB_DT_HUB_NONVAR_SIZE +
desc->bNbrPorts / 8 + 1) {
/* We have the entire DeviceRemovable field. */ return ret;
}
Alan Stern
Quoted 8 lines · Alan Stern
Quoted 2 lines · Johan Hovold
A SuperSpeed hub descriptor does not have any variable-length fields so bail out when reading a short descriptor.
You mean: bail out when reading a descriptor that is not exactly the right length. The existing code already bails out when it reads a short descriptor.
No, the current code happily accepts a 9-byte descriptor, while an SS descriptor is always 12 bytes. And since we request 12 bytes for SS hubs, the patch description is correct.
Quoted 21 lines
Quoted 21 lines
This avoids parsing and leaking two bytes of uninitialised slab data through sysfs removable-attributes.
Fixes dbe79bbe9dcb ("USB 3.0 Hub Changes")Cc stable <[email protected]> # 2.6.39Cc John Youn <[email protected]>Signed-off-by Johan Hovold <[email protected]>--- drivers/usb/core/hub.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)drivers/usb/core/hub.c +1
-1@@ -380,8 +380,12 @@ static int get_hub_descriptor(struct usb_device *hdev, void *data)USB_REQ_GET_DESCRIPTOR ,USB_DIR_IN |USB_RT_HUB , dtype <<8 ,0 , data, size, USB_CTRL_GET_TIMEOUT); -if (ret >= (USB_DT_HUB_NONVAR_SIZE +2 )) +if (hub_is_superspeed (hdev)) {
Quoted 8 lines
drivers/usb/core/hub.c static int get_hub_descriptor(struct usb_device *hdev, void *data) · line 391
Quoted 8 lines
drivers/usb/core/hub.c +4
-0+if (ret == size) +return ret; + }else if (ret >= (USB_DT_HUB_NONVAR_SIZE +2 )) {return ret; + } }return -EINVAL ; }
Thanks,
Johan
Alan Stern
Acked-byQuoted 6 lines · Johan Hovold
Quoted 5 lines · Alan Stern
Quoted 2 lines · Johan Hovold
A SuperSpeed hub descriptor does not have any variable-length fields so bail out when reading a short descriptor.
Quoted 7 lines
Quoted 3 lines
You mean: bail out when reading a descriptor that is not exactly the right length. The existing code already bails out when it reads a short descriptor.
No, the current code happily accepts a 9-byte descriptor, while an SS descriptor is always 12 bytes. And since we request 12 bytes for SS hubs, the patch description is correct.
My mistake; you are right.
Quoted 35 lines
Quoted 22 lines
Quoted 22 lines
This avoids parsing and leaking two bytes of uninitialised slab data through sysfs removable-attributes.
Fixes dbe79bbe9dcb ("USB 3.0 Hub Changes")Cc stable <[email protected]> # 2.6.39Cc John Youn <[email protected]>Signed-off-by Johan Hovold <[email protected]>--- drivers/usb/core/hub.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)drivers/usb/core/hub.c +1
-1@@ -380,8 +380,12 @@ static int get_hub_descriptor(struct usb_device *hdev, void *data)USB_REQ_GET_DESCRIPTOR ,USB_DIR_IN |USB_RT_HUB , dtype <<8 ,0 , data, size, USB_CTRL_GET_TIMEOUT); -if (ret >= (USB_DT_HUB_NONVAR_SIZE +2 )) +if (hub_is_superspeed (hdev)) {Quoted 8 lines
drivers/usb/core/hub.c static int get_hub_descriptor(struct usb_device *hdev, void *data) · line 391
Quoted 8 lines
drivers/usb/core/hub.c +4
-0+if (ret == size) +return ret; + }else if (ret >= (USB_DT_HUB_NONVAR_SIZE +2 )) {return ret; + } }return -EINVAL ; }Thanks,
Johan
Re: [PATCH 4/6] USB: hub: fix non-SS hub-descriptor handling
In reply to Alan SternQuoted 56 lines · Alan Stern
Quoted 37 lines · Johan Hovold
Add missing sanity check on the non-SuperSpeed hub-descriptor length in order to avoid parsing and leaking two bytes of uninitialised slab data through sysfs removable-attributes (or a compound-device debug statement).
Note that we only make sure that the DeviceRemovable field is always present (and specifically ignore the unused PortPwrCtrlMask field) in order to continue support any hubs with non-compliant descriptors. As a further safeguard, the descriptor buffer is also cleared.
Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Cc stable <[email protected]> # 2.6.12Signed-off-by Johan Hovold <[email protected]>--- drivers/usb/core/hub.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)drivers/usb/core/hub.c +3
-2@@ -362,7 +362,8 @@ static void usb_set_lpm_parameters(struct usb_device *udev) }/* USB 2.0 spec Section 11.24.4.5 */ -static int get_hub_descriptor (struct usb_device *hdev,void *data) +static int get_hub_descriptor (struct usb_device *hdev, +struct usb_hub_descriptor *desc) {int i, ret, size;unsigned dtype;@@ -378,12 +379,16 @@ static int get_hub_descriptor(struct usb_device *hdev, void *data)for (i =0 ; i <3 ; i++) { ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB, - dtype << 8, 0, data, size, + dtype << 8, 0, desc, size,drivers/usb/core/hub.c static int get_hub_descriptor(struct usb_device *hdev, void *data) · line 391
Quoted 10 lines
drivers/usb/core/hub.c +5
-1USB_CTRL_GET_TIMEOUT);if (hub_is_superspeed (hdev)) {if (ret == size)return ret; - }else if (ret >= (USB_DT_HUB_NONVAR_SIZE +2 )) { + }else if (ret >=USB_DT_HUB_NONVAR_SIZE +2 ) { +/* Make sure we have the DeviceRemovable field. */ + size =USB_DT_HUB_NONVAR_SIZE + desc->bNbrPorts /8 +1 ; +if (ret < size) +return -EMSGSIZE ;The logic could be simplified a little. Since we don't really care about the return code when an error occurs, you could just do:
} else if (ret >= USB_DT_HUB_NONVAR_SIZE + desc->bNbrPorts / 8 + 1) { /* We have the entire DeviceRemovable field. */
Review
Quoted 2 lines
(quoted diff) +0
-0return ret; }
Sure, that would work, but I it doesn't feel right to access bNbrPorts without first verifying we got the non-variable fields.
I considered dropping the +2 bit, but decided to keep it in the unlikely even that there are quirky devices out there that rely on it (e.g. first read always return 7 bytes). Spelling it out makes it sound overly conservative though. How about I drop that instead?
Thanks,
Johan
Re: [PATCH 4/6] USB: hub: fix non-SS hub-descriptor handling
Quoted 36 lines · Johan Hovold
Quoted 32 lines · Alan Stern
Quoted 13 lines · Johan Hovold
/* USB 2.0 spec Section 11.24.4.5 */
drivers/usb/core/hub.c +3
-2-static int get_hub_descriptor (struct usb_device *hdev,void *data) +static int get_hub_descriptor (struct usb_device *hdev, +struct usb_hub_descriptor *desc) {int i, ret, size;unsigned dtype;@@ -378,12 +379,16 @@ static int get_hub_descriptor(struct usb_device *hdev, void *data)for (i =0 ; i <3 ; i++) { ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB, - dtype << 8, 0, data, size, + dtype << 8, 0, desc, size,drivers/usb/core/hub.c static int get_hub_descriptor(struct usb_device *hdev, void *data) · line 391
Quoted 10 lines
drivers/usb/core/hub.c +5
-1USB_CTRL_GET_TIMEOUT);if (hub_is_superspeed (hdev)) {if (ret == size)return ret; - }else if (ret >= (USB_DT_HUB_NONVAR_SIZE +2 )) { + }else if (ret >=USB_DT_HUB_NONVAR_SIZE +2 ) { +/* Make sure we have the DeviceRemovable field. */ + size =USB_DT_HUB_NONVAR_SIZE + desc->bNbrPorts /8 +1 ; +if (ret < size) +return -EMSGSIZE ;The logic could be simplified a little. Since we don't really care about the return code when an error occurs, you could just do:
} else if (ret >= USB_DT_HUB_NONVAR_SIZE + desc->bNbrPorts / 8 + 1) { /* We have the entire DeviceRemovable field. */Review
Quoted 2 lines
(quoted diff) +0
-0return ret; }
Quoted 7 lines
Sure, that would work, but I it doesn't feel right to access bNbrPorts without first verifying we got the non-variable fields.
I considered dropping the +2 bit, but decided to keep it in the unlikely even that there are quirky devices out there that rely on it (e.g. first read always return 7 bytes). Spelling it out makes it sound overly conservative though. How about I drop that instead?
Then again, a non-SS hub descriptor is always at least (USB_DT_HUB_NONVAR_SIZE + 2) bytes long so keeping it kind of makes sense anyway.
Johan
Re: [PATCH 4/6] USB: hub: fix non-SS hub-descriptor handling
Quoted 31 lines · Johan Hovold
Quoted 30 lines · Johan Hovold
Quoted 29 lines · Alan Stern
Quoted 13 lines · Johan Hovold
/* USB 2.0 spec Section 11.24.4.5 */
drivers/usb/core/hub.c +3
-2-static int get_hub_descriptor (struct usb_device *hdev,void *data) +static int get_hub_descriptor (struct usb_device *hdev, +struct usb_hub_descriptor *desc) {int i, ret, size;unsigned dtype;@@ -378,12 +379,16 @@ static int get_hub_descriptor(struct usb_device *hdev, void *data)for (i =0 ; i <3 ; i++) { ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB, - dtype << 8, 0, data, size, + dtype << 8, 0, desc, size,drivers/usb/core/hub.c static int get_hub_descriptor(struct usb_device *hdev, void *data) · line 391
Quoted 10 lines
drivers/usb/core/hub.c +5
-1USB_CTRL_GET_TIMEOUT);if (hub_is_superspeed (hdev)) {if (ret == size)return ret; - }else if (ret >= (USB_DT_HUB_NONVAR_SIZE +2 )) { + }else if (ret >=USB_DT_HUB_NONVAR_SIZE +2 ) { +/* Make sure we have the DeviceRemovable field. */ + size =USB_DT_HUB_NONVAR_SIZE + desc->bNbrPorts /8 +1 ; +if (ret < size) +return -EMSGSIZE ;The logic could be simplified a little. Since we don't really care about the return code when an error occurs, you could just do:
Quoted 8 lines
Quoted 8 lines
Review
Quoted 5 lines
} else if (ret >= USB_DT_HUB_NONVAR_SIZE + desc->bNbrPorts / 8 + 1) { /* We have the entire DeviceRemovable field. */(quoted diff) +0
-0return ret; }Sure, that would work, but I it doesn't feel right to access bNbrPorts without first verifying we got the non-variable fields.
Accessing desc->bNbrPorts won't do any harm, even if it wasn't sent. After all, you now initialize the hub descriptor to all 0's. And even if the field contained garbage, it would just make this test less likely to succeed.
Quoted 8 lines
Quoted 4 lines
I considered dropping the +2 bit, but decided to keep it in the unlikely even that there are quirky devices out there that rely on it (e.g. first read always return 7 bytes). Spelling it out makes it sound overly conservative though. How about I drop that instead?
Then again, a non-SS hub descriptor is always at least (
USB_DT_HUB_NONVAR_SIZE+ 2) bytes long so keeping it kind of makes sense anyway.
Personally, I wouldn't worry about it. It's your decision.
Alan Stern
Re: [PATCH 4/6] USB: hub: fix non-SS hub-descriptor handling
Quoted 39 lines · Alan Stern
Quoted 37 lines · Johan Hovold
Quoted 36 lines · Johan Hovold
Quoted 34 lines · Alan Stern
Quoted 13 lines · Johan Hovold
/* USB 2.0 spec Section 11.24.4.5 */
drivers/usb/core/hub.c +3
-2-static int get_hub_descriptor (struct usb_device *hdev,void *data) +static int get_hub_descriptor (struct usb_device *hdev, +struct usb_hub_descriptor *desc) {int i, ret, size;unsigned dtype;@@ -378,12 +379,16 @@ static int get_hub_descriptor(struct usb_device *hdev, void *data)for (i =0 ; i <3 ; i++) { ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB, - dtype << 8, 0, data, size, + dtype << 8, 0, desc, size,drivers/usb/core/hub.c static int get_hub_descriptor(struct usb_device *hdev, void *data) · line 391
Quoted 10 lines
drivers/usb/core/hub.c +5
-1USB_CTRL_GET_TIMEOUT);if (hub_is_superspeed (hdev)) {if (ret == size)return ret; - }else if (ret >= (USB_DT_HUB_NONVAR_SIZE +2 )) { + }else if (ret >=USB_DT_HUB_NONVAR_SIZE +2 ) { +/* Make sure we have the DeviceRemovable field. */ + size =USB_DT_HUB_NONVAR_SIZE + desc->bNbrPorts /8 +1 ; +if (ret < size) +return -EMSGSIZE ;The logic could be simplified a little. Since we don't really care about the return code when an error occurs, you could just do:
} else if (ret >= USB_DT_HUB_NONVAR_SIZE + desc->bNbrPorts / 8 + 1) { /* We have the entire DeviceRemovable field. */(quoted diff) +0
-0return ret; }
Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
Sure, that would work, but I it doesn't feel right to access bNbrPorts without first verifying we got the non-variable fields.
Accessing
desc->bNbrPortswon't do any harm, even if it wasn't sent. After all, you now initialize the hub descriptor to all 0's. And even if the field contained garbage, it would just make this test less likely to succeed.
I know, but since it's not immediately obvious (and may set a bad example), I think we should avoid it.
Quoted 10 lines
Quoted 8 lines
Quoted 4 lines
I considered dropping the +2 bit, but decided to keep it in the unlikely even that there are quirky devices out there that rely on it (e.g. first read always return 7 bytes). Spelling it out makes it sound overly conservative though. How about I drop that instead?
Then again, a non-SS hub descriptor is always at least (
USB_DT_HUB_NONVAR_SIZE+ 2) bytes long so keeping it kind of makes sense anyway.Personally, I wouldn't worry about it. It's your decision.
Now I remember that dropping the +2 would also mean that we start accepting descriptors without a PortPwrCtrlMask field. I'll just leave it in for now.
Thanks,
Johan