Skip to contentSkip to conversation

usb: chipidea: otg: fix invalid role name print when no current role

PATCH New Xu Yang 2 participants 2 messages Sep 7

Subsystems (3)
lore
Share & tools
lore

Xu Yang

author
#0 lore raw

PATCH v2 1/1 usb: chipidea: otg: fix invalid role name print when no current role

From: Xu Yang <[email protected]>

ci_handle_id_switch() prints the role transition via ci_role(ci)->name for the current role, but when ci->role is CI_ROLE_END (no role assigned yet), ci_role(ci) call BUG_ON() and return ci->roles[CI_ROLE_END] which will cause invalid memory access in the debug print.

This can happen if ci_role_start() fails during a role switch, since ci_role_stop() has already set ci->role to CI_ROLE_END.

Fix it by printing "none" instead of dereferencing ci_role(ci)->name when ci->role is not a valid role.

Fixes cbec6bd55a45 ("usb: chipidea: move otg related things to otg file")

---
Changes in v2:
- improve commit message
- use " >= " sign

---
 drivers/usb/chipidea/otg.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/usb/chipidea/otg.c +2 -1
index fecc7d7e2f0d..5eb3b32c2e87 100644
@@ -179,7 +179,8 @@ void ci_handle_id_switch(struct ci_hdrc *ci)
 	role = ci_otg_role(ci);
 	if (role != ci->role) {
 		dev_dbg(ci->dev, "switching from %s to %s\n",
-			ci_role(ci)->name, ci->roles[role]->name);
+			ci->role >= CI_ROLE_END ? "none" : ci_role(ci)->name,
+			ci->roles[role]->name);
 
 		if (ci->vbus_active && ci->role == CI_ROLE_GADGET)
 			/*
Signature · 1 line
2.34.1
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] A hardware failure during role switch to gadget mode triggers a BUG_ON() panic instead of a graceful error in hw_device_reset(). --

commit c6988a7e6f8c802fa9431c1e969bc414e366bec6
Author: Xu Yang <[email protected]>

usb: chipidea: otg: fix invalid role name print when no current role

This commit fixes an invalid memory access and BUG_ON() panic in ci_handle_id_switch() when logging role transitions. It ensures "none" is printed instead of dereferencing ci_role(ci)->name if the current role is CI_ROLE_END.

drivers/usb/chipidea/otg.c void ci_handle_id_switch(struct ci_hdrc *ci) · line 185

Quoted 13 lines
drivers/usb/chipidea/otg.c +2 -1
index fecc7d7e2f0d7..5eb3b32c2e871 100644
@@ -179,7 +179,8 @@ void ci_handle_id_switch(struct ci_hdrc *ci)
 	role = ci_otg_role(ci);
 	if (role != ci->role) {
 		dev_dbg(ci->dev, "switching from %s to %s\n",
-			ci_role(ci)->name, ci->roles[role]->name);
+			ci->role >= CI_ROLE_END ? "none" : ci_role(ci)->name,
+			ci->roles[role]->name);
 
 		if (ci->vbus_active && ci->role == CI_ROLE_GADGET)

[Severity: High]
This is a pre-existing issue, but the same underlying BUG_ON() panic can still happen during a role switch to gadget mode if a hardware failure occurs in hw_device_reset():

drivers/usb/chipidea/core.c:hw_device_reset() {
	...
	if (hw_read(ci, OP_USBMODE, USBMODE_CM) != USBMODE_CM_DC) {
		dev_err(ci->dev, "cannot enter in %s device mode\n",
			ci_role(ci)->name);
	...
}

During a role switch, ci->role is set to CI_ROLE_END by ci_role_stop() and isn't updated to CI_ROLE_GADGET until ci->roles[role]->start() successfully completes.

If hw_device_reset() fails to read back USBMODE_CM_DC during this time, won't the error path dereferencing ci_role(ci)->name hit the same BUG_ON(ci->role >= CI_ROLE_END) panic instead of gracefully logging the hardware reset error?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1