Skip to contentSkip to conversation

usb: gadget: core: Check for unset descriptor

PATCH Superseded [email protected] 2 participants 6 messages Jul 21, 2024 – Jul 22, 2024

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: gadget: core: Check for unset descriptor

From: Chris Wulff <[email protected]>

Make sure the descriptor has been set before looking at maxpacket. This fixes a null pointer panic in this case.

This may happen if the gadget doesn't properly set up the endpoint for the current speed, or the gadget descriptors are malformed and the descriptor for the speed/endpoint are not found.

Fixes 54f83b8c8ea9 ("USB: gadget: Reject endpoints with 0 maxpacket value")
---
 drivers/usb/gadget/udc/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/gadget/udc/core.c +1 -1
index 2dfae7a17b3f..36a5d5935889 100644
@@ -118,7 +118,7 @@ int usb_ep_enable(struct usb_ep *ep)
 		goto out;
 
 	/* UDC drivers can't handle endpoints with maxpacket size 0 */
-	if (usb_endpoint_maxp(ep->desc) == 0) {
+	if (!ep->desc || usb_endpoint_maxp(ep->desc) == 0) {
 		/*
 		 * We should log an error message here, but we can't call
 		 * dev_err() because there's no way to find the gadget
Signature · 1 line
2.43.0
#1 lore raw
Quoted 8 lines · On Sun, Jul 21, 2024 at 03:20:49PM -0400, [email protected] wrote:

From: Chris Wulff <[email protected]>

Make sure the descriptor has been set before looking at maxpacket. This fixes a null pointer panic in this case.

This may happen if the gadget doesn't properly set up the endpoint for the current speed, or the gadget descriptors are malformed and the descriptor for the speed/endpoint are not found.

If that happens, doesn't it mean there's a bug in the gadget driver? And if there's a bug, don't we want to be told about it by a big impossible-to-miss error message, so the bug can be fixed?

Quoted 12 lines
Fixes 54f83b8c8ea9 ("USB: gadget: Reject endpoints with 0 maxpacket value")
---
 drivers/usb/gadget/udc/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/gadget/udc/core.c +0 -0
index 2dfae7a17b3f..36a5d5935889 100644

drivers/usb/gadget/udc/core.c int usb_ep_enable(struct usb_ep *ep) · line 124

Quoted 8 lines
drivers/usb/gadget/udc/core.c +1 -1
@@ -118,7 +118,7 @@ int usb_ep_enable(struct usb_ep *ep)
 		goto out;
 
 	/* UDC drivers can't handle endpoints with maxpacket size 0 */
-	if (usb_endpoint_maxp(ep->desc) == 0) {
+	if (!ep->desc || usb_endpoint_maxp(ep->desc) == 0) {
 		/*
 		 * We should log an error message here, but we can't call
 		 * dev_err() because there's no way to find the gadget

This will just hide the error. That's not good.

Alan Stern

#2 lore raw
Quoted 7 lines · Alan Stern
Quoted 5 lines · On Sun, Jul 21, 2024 at 03:20:49PM -0400, [email protected] wrote:

From: Chris Wulff <[email protected]>

Make sure the descriptor has been set before looking at maxpacket. This fixes a null pointer panic in this case.

Quoted 7 lines
Quoted 3 lines

This may happen if the gadget doesn't properly set up the endpoint for the current speed, or the gadget descriptors are malformed and the descriptor for the speed/endpoint are not found.

If that happens, doesn't it mean there's a bug in the gadget driver? And if there's a bug, don't we want to be told about it by a big impossible-to-miss error message, so the bug can be fixed?

Yes, this is an indicator of a problem in a gadget driver as was the previous check for a zero max packet size. In this case, the panic is in an interrupt context and it doesn't make it out to the console. This just results in a system freeze without this fix.

Quoted 15 lines
Quoted 15 lines
Fixes 54f83b8c8ea9 ("USB: gadget: Reject endpoints with 0 maxpacket value")
---
 drivers/usb/gadget/udc/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/gadget/udc/core.c +0 -0
index 2dfae7a17b3f..36a5d5935889 100644
@@ -118,7 +118,7 @@ int usb_ep_enable(struct usb_ep *ep)
              goto out;
 
Quoted 8 lines

drivers/usb/gadget/udc/core.c int usb_ep_enable(struct usb_ep *ep) · line 124

Quoted 6 lines
drivers/usb/gadget/udc/core.c +1 -1
      /* UDC drivers can't handle endpoints with maxpacket size 0 */
-     if (usb_endpoint_maxp(ep->desc) == 0) {
+     if (!ep->desc || usb_endpoint_maxp(ep->desc) == 0) {
              /*
               * We should log an error message here, but we can't call
               * dev_err() because there's no way to find the gadget

This will just hide the error. That's not good.

The previous check was also hiding the error, and introduced a panic. I could add a printk to that error case, though it would be unassociated with the gadget that caused the problem. This function does also return an error code when it fails, so the calling function can check that and print an error.

Quoted 2 lines

Alan Stern

#3 lore raw
Quoted 45 lines · Chris Wulff
Quoted 7 lines · Alan Stern
Quoted 5 lines · On Sun, Jul 21, 2024 at 03:20:49PM -0400, [email protected] wrote:

From: Chris Wulff <[email protected]>

Make sure the descriptor has been set before looking at maxpacket. This fixes a null pointer panic in this case.

Quoted 7 lines
Quoted 3 lines

This may happen if the gadget doesn't properly set up the endpoint for the current speed, or the gadget descriptors are malformed and the descriptor for the speed/endpoint are not found.

If that happens, doesn't it mean there's a bug in the gadget driver? And if there's a bug, don't we want to be told about it by a big impossible-to-miss error message, so the bug can be fixed?

Yes, this is an indicator of a problem in a gadget driver as was the previous check for a zero max packet size. In this case, the panic is in an interrupt context and it doesn't make it out to the console. This just results in a system freeze without this fix.

Quoted 15 lines
Quoted 15 lines
Fixes 54f83b8c8ea9 ("USB: gadget: Reject endpoints with 0 maxpacket value")
---
 drivers/usb/gadget/udc/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/gadget/udc/core.c +0 -0
index 2dfae7a17b3f..36a5d5935889 100644
@@ -118,7 +118,7 @@ int usb_ep_enable(struct usb_ep *ep)
              goto out;
 
Quoted 8 lines

drivers/usb/gadget/udc/core.c int usb_ep_enable(struct usb_ep *ep) · line 124

Quoted 6 lines
drivers/usb/gadget/udc/core.c +1 -1
      /* UDC drivers can't handle endpoints with maxpacket size 0 */
-     if (usb_endpoint_maxp(ep->desc) == 0) {
+     if (!ep->desc || usb_endpoint_maxp(ep->desc) == 0) {
              /*
               * We should log an error message here, but we can't call
               * dev_err() because there's no way to find the gadget

This will just hide the error. That's not good.

Quoted 5 lines

The previous check was also hiding the error, and introduced a panic. I could add a printk to that error case, though it would be unassociated with the gadget that caused the problem. This function does also return an error code when it fails, so the calling function can check that and print an error.

Okay. It wouldn't hurt to print out an error message, even if there's no way to tell which gadget it refers to. A dump_stack() would help in that regard, but it won't be needed if the guilty party will always be pretty obvious.

By the way, how did you manage to trigger this error? None of the in-kernel gadget drivers are known to have this bug, and both the gadgetfs and raw_gadget drivers prevent userspace from doing it. Were you testing a gadget driver that was under development?

Alan Stern

#4 lore raw
Quoted 6 lines · Alan Stern
Quoted 4 lines · Chris Wulff
Quoted 3 lines · Alan Stern
Quoted 1 line · On Sun, Jul 21, 2024 at 03:20:49PM -0400, [email protected] wrote:

From: Chris Wulff <[email protected]>

...

Quoted 11 lines
Quoted 5 lines

The previous check was also hiding the error, and introduced a panic. I could add a printk to that error case, though it would be unassociated with the gadget that caused the problem. This function does also return an error code when it fails, so the calling function can check that and print an error.

Okay. It wouldn't hurt to print out an error message, even if there's no way to tell which gadget it refers to. A dump_stack() would help in that regard, but it won't be needed if the guilty party will always be pretty obvious.

Quoted 4 lines

By the way, how did you manage to trigger this error? None of the in-kernel gadget drivers are known to have this bug, and both the gadgetfs and raw_gadget drivers prevent userspace from doing it. Were you testing a gadget driver that was under development?

I am working on adding alternate settings to UAC1/2 gadgets, so this really was a case of trying to make the failure in development easier to deal with. I don't believe there are any problems with existing gadgets causing this.

I will add an error message and submit a new version. Perhaps WARN_ON_ONCE would be appropriate here to get that backtrace instead of a printk?

Quoted 2 lines

Alan Stern

#5 lore raw
Quoted 24 lines · Chris Wulff
Quoted 6 lines · Alan Stern
Quoted 4 lines · Chris Wulff
Quoted 3 lines · Alan Stern
Quoted 1 line · On Sun, Jul 21, 2024 at 03:20:49PM -0400, [email protected] wrote:

From: Chris Wulff <[email protected]>

...

Quoted 11 lines
Quoted 5 lines

The previous check was also hiding the error, and introduced a panic. I could add a printk to that error case, though it would be unassociated with the gadget that caused the problem. This function does also return an error code when it fails, so the calling function can check that and print an error.

Okay. It wouldn't hurt to print out an error message, even if there's no way to tell which gadget it refers to. A dump_stack() would help in that regard, but it won't be needed if the guilty party will always be pretty obvious.

Quoted 4 lines

By the way, how did you manage to trigger this error? None of the in-kernel gadget drivers are known to have this bug, and both the gadgetfs and raw_gadget drivers prevent userspace from doing it. Were you testing a gadget driver that was under development?

Quoted 7 lines

I am working on adding alternate settings to UAC1/2 gadgets, so this really was a case of trying to make the failure in development easier to deal with. I don't believe there are any problems with existing gadgets causing this.

I will add an error message and submit a new version. Perhaps WARN_ON_ONCE would be appropriate here to get that backtrace instead of a printk?

That sounds good. You should also mention in the patch description that the purpose is to prevent undebuggable panics during driver development, and no existing drivers will trigger the warning.

Alan Stern