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

Xu Yang posted 1 patch 3 weeks, 4 days ago
There is a newer version of this series
drivers/usb/chipidea/otg.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
[PATCH] usb: chipidea: otg: fix invalid role name print when no current role
Posted by Xu Yang 3 weeks, 4 days ago
From: Xu Yang <xu.yang_2@nxp.com>

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 is NULL, causing a NULL pointer dereference 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 CI_ROLE_END.

Fixes: cbec6bd55a45 ("usb: chipidea: move otg related things to otg file")
Cc: stable@vger.kernel.org
Signed-off-by: Xu Yang <xu.yang_2@nxp.com>
---
 drivers/usb/chipidea/otg.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/usb/chipidea/otg.c b/drivers/usb/chipidea/otg.c
index fecc7d7e2f0d..457417e81b78 100644
--- a/drivers/usb/chipidea/otg.c
+++ b/drivers/usb/chipidea/otg.c
@@ -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)
 			/*
-- 
2.34.1
Re: [PATCH] usb: chipidea: otg: fix invalid role name print when no current role
Posted by Frank Li 3 weeks, 3 days ago
On Tue, Sep 01, 2026 at 02:01:50PM +0800, Xu Yang wrote:
> From: Xu Yang <xu.yang_2@nxp.com>
>
> 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 is NULL, causing a NULL pointer dereference in the debug print.

This should be out of boundery access,

struct ci_role_driver           *roles[CI_ROLE_END];

ci->roles[CI_ROLE_END] to access next field,
        enum ci_role                    role;
        bool                            is_otg;

NULL should be "luck" value.

>
> 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 CI_ROLE_END.
>
> Fixes: cbec6bd55a45 ("usb: chipidea: move otg related things to otg file")
> Cc: stable@vger.kernel.org
> Signed-off-by: Xu Yang <xu.yang_2@nxp.com>
> ---
>  drivers/usb/chipidea/otg.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/usb/chipidea/otg.c b/drivers/usb/chipidea/otg.c
> index fecc7d7e2f0d..457417e81b78 100644
> --- a/drivers/usb/chipidea/otg.c
> +++ b/drivers/usb/chipidea/otg.c
> @@ -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);

This is out of boundery check

	ci->role >= CI_ROLE_END  or ci-role < CI_ROLE_END, and revert :

Frank

>
>  		if (ci->vbus_active && ci->role == CI_ROLE_GADGET)
>  			/*
> --
> 2.34.1
>
>
Re: [PATCH] usb: chipidea: otg: fix invalid role name print when no current role
Posted by Xu Yang 2 weeks, 5 days ago
On Tue, Sep 01, 2026 at 02:30:55PM -0500, Frank Li wrote:
> On Tue, Sep 01, 2026 at 02:01:50PM +0800, Xu Yang wrote:
> > From: Xu Yang <xu.yang_2@nxp.com>
> >
> > 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 is NULL, causing a NULL pointer dereference in the debug print.
> 
> This should be out of boundery access,
> 
> struct ci_role_driver           *roles[CI_ROLE_END];
> 
> ci->roles[CI_ROLE_END] to access next field,
>         enum ci_role                    role;
>         bool                            is_otg;
> 
> NULL should be "luck" value.

Yes, it's true.

> 
> >
> > 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 CI_ROLE_END.
> >
> > Fixes: cbec6bd55a45 ("usb: chipidea: move otg related things to otg file")
> > Cc: stable@vger.kernel.org
> > Signed-off-by: Xu Yang <xu.yang_2@nxp.com>
> > ---
> >  drivers/usb/chipidea/otg.c | 3 ++-
> >  1 file changed, 2 insertions(+), 1 deletion(-)
> >
> > diff --git a/drivers/usb/chipidea/otg.c b/drivers/usb/chipidea/otg.c
> > index fecc7d7e2f0d..457417e81b78 100644
> > --- a/drivers/usb/chipidea/otg.c
> > +++ b/drivers/usb/chipidea/otg.c
> > @@ -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);
> 
> This is out of boundery check
> 
> 	ci->role >= CI_ROLE_END  or ci-role < CI_ROLE_END, and revert :

OK.

Thanks,
Xu Yang