USB: hub: make sure stale buffers are not enumerated
PATCH 1/1 USB: hub: make sure stale buffers are not enumerated
Quoting Alan Stern on why we cannot just check errors:
The operation carried out here is deliberately unsafe (for full-speed devices). It is made before we know the actual maxpacket size for ep0, and as a result it might return an error code even when it works okay. This shouldn't happen, but a lot of USB hardware is unreliable.
Therefore we must not ignore the result merely because r < 0. If we do that, the kernel might stop working with some devices.
He is absolutely right. However, we must make sure that in case we read nothing or a short answer, the buffer contains nothing that can be misinterpreted as a valid answer.
So we have to zero it before we use it for IO.
--- drivers/usb/core/hub.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/usb/core/hub.c +2 -1
@@ -4873,7 +4873,8 @@ hub_port_init(struct usb_hub *hub, struct usb_device *udev, int port1, }#define GET_DESCRIPTOR_BUFSIZE 64 - buf =kmalloc (GET_DESCRIPTOR_BUFSIZE ,GFP_NOIO ); +/* zeroed so we don't operate on a stale buffer on errors */ + buf =kzalloc (GET_DESCRIPTOR_BUFSIZE ,GFP_NOIO );if (!buf) { retval = -ENOMEM ;continue ;
Signature · 1 line
2.41.0
Quoted 7 lines · Oliver Neukum
Quoting Alan Stern on why we cannot just check errors:
The operation carried out here is deliberately unsafe (for full-speed devices). It is made before we know the actual maxpacket size for ep0, and as a result it might return an error code even when it works okay. This shouldn't happen, but a lot of USB hardware is unreliable.
Quoted 7 lines
Therefore we must not ignore the result merely because r < 0. If we do that, the kernel might stop working with some devices.
He is absolutely right. However, we must make sure that in case we read nothing or a short answer, the buffer contains nothing that can be misinterpreted as a valid answer.
So we have to zero it before we use it for IO.
This patch is neither correct or needed. The current implementation sets
buf->bMaxPacketSize0 = 0
before reading the descriptor and makes sure that that field is non-zero before accessing buf->bDescriptorType which lies before bMaxPacketSize0.
It may be subtle, but it looks correct.
Johan
Quoted 9 lines · Johan Hovold
This patch is neither correct or needed. The current implementation sets
buf->bMaxPacketSize0 = 0before reading the descriptor and makes sure that that field is non-zero before accessing
buf->bDescriptorTypewhich lies before bMaxPacketSize0.It may be subtle, but it looks correct.
True, but I am afraid not sufficient. It neglects the case of getting a partial read. That is
buf->bMaxPacketSize0
can be genuine, but the later test
if (buf->bDescriptorType ==
USB_DT_DEVICE) {still spuriously succeed
Regards
Oliver
Quoted 15 lines · Oliver Neukum
Quoted 9 lines · Johan Hovold
This patch is neither correct or needed. The current implementation sets
buf->bMaxPacketSize0 = 0before reading the descriptor and makes sure that that field is non-zero before accessing
buf->bDescriptorTypewhich lies before bMaxPacketSize0.It may be subtle, but it looks correct.
True, but I am afraid not sufficient. It neglects the case of getting a partial read. That is
Quoted 7 lines
buf->bMaxPacketSize0can be genuine, but the later test
if (buf->bDescriptorType==USB_DT_DEVICE) {still spuriously succeed
How can it? bDescriptorType is at the start of the device descriptor, whereas bMaxPacketSize0 is more towards the end. If the later part get transferred from the device, the earlier part must have been transferred as well. Even if the transfer was short.
Alan Stern
Quoted 4 lines · Alan Stern
How can it? bDescriptorType is at the start of the device descriptor, whereas bMaxPacketSize0 is more towards the end. If the later part get transferred from the device, the earlier part must have been transferred as well. Even if the transfer was short.
Do we really guarantee that in an error case the buffer is filled from front to back?
Regards
Oliver
Quoted 8 lines · Oliver Neukum
Quoted 4 lines · Alan Stern
How can it? bDescriptorType is at the start of the device descriptor, whereas bMaxPacketSize0 is more towards the end. If the later part get transferred from the device, the earlier part must have been transferred as well. Even if the transfer was short.
Do we really guarantee that in an error case the buffer is filled from front to back?
We don't guarantee anything in an error case. But for short transfers with no other errors, yes, this is guaranteed.
However if you still want to address this concern, just set buf->bDescriptorType to 0 at the same time as buf->bMaxPacketSize0.
Alan Stern
Quoted 26 lines · Oliver Neukum
Quoting Alan Stern on why we cannot just check errors:
The operation carried out here is deliberately unsafe (for full-speed devices). It is made before we know the actual maxpacket size for ep0, and as a result it might return an error code even when it works okay. This shouldn't happen, but a lot of USB hardware is unreliable.
Therefore we must not ignore the result merely because r < 0. If we do that, the kernel might stop working with some devices.
He is absolutely right. However, we must make sure that in case we read nothing or a short answer, the buffer contains nothing that can be misinterpreted as a valid answer.
So we have to zero it before we use it for IO.Reported-by liulongfang <[email protected]>Signed-off-by Oliver Neukum <[email protected]>--- drivers/usb/core/hub.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)drivers/usb/core/hub.c +0
-0
drivers/usb/core/hub.c hub_port_init(struct usb_hub *hub, struct usb_device *udev, int port1, · line 4880
Quoted 9 lines
drivers/usb/core/hub.c +2
-1@@ -4873,7 +4873,8 @@ hub_port_init(struct usb_hub *hub, struct usb_device *udev, int port1, }#define GET_DESCRIPTOR_BUFSIZE 64 - buf =kmalloc (GET_DESCRIPTOR_BUFSIZE ,GFP_NOIO ); +/* zeroed so we don't operate on a stale buffer on errors */ + buf =kzalloc (GET_DESCRIPTOR_BUFSIZE ,GFP_NOIO );if (!buf) { retval = -ENOMEM ;continue ;
There is no need for kzalloc.
The only accesses we do to buf (besides feeding it to usb_control_msg) are to read buf->bMaxPacketSize0 and buf->bDescriptorType. The only field that actually needs to be initialized is bMaxPacketSize0, and the code already sets it to 0 before calling usb_control_msg. Furthermore, we don't try to read bDescriptorType if bMaxPacketSize0 is invalid after the I/O operation.
I guess if you really want to be safe, you could do:
- udev->descriptor.bMaxPacketSize0 =
- buf->bMaxPacketSize0;
+ if (r == 0)
+ udev->descriptor.bMaxPacketSize0 =
+ buf->bMaxPacketSize0;
at line 4918. But even that's not necessary, since the following call to hub_port_reset doesn't use udev->descriptor.bMaxPacketSize0, so it doesn't matter if that field contains garbage.
Alan Stern