* [PATCH] usb: typec: fix potential NULL dereference
@ 2023-04-17 19:50 Maxim Korotkov
2023-04-18 6:16 ` Greg Kroah-Hartman
0 siblings, 1 reply; 4+ messages in thread
From: Maxim Korotkov @ 2023-04-17 19:50 UTC (permalink / raw)
To: Heikki Krogerus
Cc: Maxim Korotkov, Greg Kroah-Hartman, linux-usb, linux-kernel,
lvc-project
The pointer 'adev' was being dereferenced before being checked for NULL
in the 'type_alt mode_enter()' and 'type_alt mode_exit()' functions.
Although this is a hypothetical issue, it's better to move the pointer
assignment after the NULL check to avoid any potential problems.
Found by Linux Verification Center with Svace static analyzer.
Fixes: 8a37d87d72f0 ("usb: typec: Bus type for alternate modes")
Signed-off-by: Maxim Korotkov <korotkov.maxim.s@gmail.com>
---
drivers/usb/typec/bus.c | 13 +++++++++----
1 file changed, 9 insertions(+), 4 deletions(-)
diff --git a/drivers/usb/typec/bus.c b/drivers/usb/typec/bus.c
index 098f0efaa58d..ae0aca8f33db 100644
--- a/drivers/usb/typec/bus.c
+++ b/drivers/usb/typec/bus.c
@@ -125,13 +125,16 @@ EXPORT_SYMBOL_GPL(typec_altmode_notify);
*/
int typec_altmode_enter(struct typec_altmode *adev, u32 *vdo)
{
- struct altmode *partner = to_altmode(adev)->partner;
- struct typec_altmode *pdev = &partner->adev;
+ struct altmode *partner;
+ struct typec_altmode *pdev;
int ret;
if (!adev || adev->active)
return 0;
+ partner = to_altmode(adev)->partner;
+ pdev = &partner->adev;
+
if (!pdev->ops || !pdev->ops->enter)
return -EOPNOTSUPP;
@@ -156,13 +159,15 @@ EXPORT_SYMBOL_GPL(typec_altmode_enter);
*/
int typec_altmode_exit(struct typec_altmode *adev)
{
- struct altmode *partner = to_altmode(adev)->partner;
- struct typec_altmode *pdev = &partner->adev;
+ struct altmode *partner;
+ struct typec_altmode *pdev;
int ret;
if (!adev || !adev->active)
return 0;
+ partner = to_altmode(adev)->partner;
+ pdev = &partner->adev;
if (!pdev->ops || !pdev->ops->exit)
return -EOPNOTSUPP;
--
2.37.2
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] usb: typec: fix potential NULL dereference
2023-04-17 19:50 [PATCH] usb: typec: fix potential NULL dereference Maxim Korotkov
@ 2023-04-18 6:16 ` Greg Kroah-Hartman
2023-04-18 6:56 ` Maxim Korotkov
0 siblings, 1 reply; 4+ messages in thread
From: Greg Kroah-Hartman @ 2023-04-18 6:16 UTC (permalink / raw)
To: Maxim Korotkov; +Cc: Heikki Krogerus, linux-usb, linux-kernel, lvc-project
On Mon, Apr 17, 2023 at 10:50:03PM +0300, Maxim Korotkov wrote:
> The pointer 'adev' was being dereferenced before being checked for NULL
> in the 'type_alt mode_enter()' and 'type_alt mode_exit()' functions.
> Although this is a hypothetical issue, it's better to move the pointer
> assignment after the NULL check to avoid any potential problems.
>
> Found by Linux Verification Center with Svace static analyzer.
>
> Fixes: 8a37d87d72f0 ("usb: typec: Bus type for alternate modes")
> Signed-off-by: Maxim Korotkov <korotkov.maxim.s@gmail.com>
> ---
> drivers/usb/typec/bus.c | 13 +++++++++----
> 1 file changed, 9 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/usb/typec/bus.c b/drivers/usb/typec/bus.c
> index 098f0efaa58d..ae0aca8f33db 100644
> --- a/drivers/usb/typec/bus.c
> +++ b/drivers/usb/typec/bus.c
> @@ -125,13 +125,16 @@ EXPORT_SYMBOL_GPL(typec_altmode_notify);
> */
> int typec_altmode_enter(struct typec_altmode *adev, u32 *vdo)
> {
> - struct altmode *partner = to_altmode(adev)->partner;
> - struct typec_altmode *pdev = &partner->adev;
> + struct altmode *partner;
> + struct typec_altmode *pdev;
> int ret;
>
> if (!adev || adev->active)
> return 0;
>
> + partner = to_altmode(adev)->partner;
> + pdev = &partner->adev;
As you point out, the original code is still fine here, we check before
we actually use these values.
Also, can adev every actually be NULL? In looking at the code paths, I
can't see how that could happen.
thanks,
greg k-h
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] usb: typec: fix potential NULL dereference
2023-04-18 6:16 ` Greg Kroah-Hartman
@ 2023-04-18 6:56 ` Maxim Korotkov
2023-04-20 8:39 ` Heikki Krogerus
0 siblings, 1 reply; 4+ messages in thread
From: Maxim Korotkov @ 2023-04-18 6:56 UTC (permalink / raw)
To: Greg Kroah-Hartman; +Cc: Heikki Krogerus, linux-usb, linux-kernel, lvc-project
On 18.04.2023 09:16, Greg Kroah-Hartman wrote:
> On Mon, Apr 17, 2023 at 10:50:03PM +0300, Maxim Korotkov wrote:
>> The pointer 'adev' was being dereferenced before being checked for NULL
>> in the 'type_alt mode_enter()' and 'type_alt mode_exit()' functions.
>> Although this is a hypothetical issue, it's better to move the pointer
>> assignment after the NULL check to avoid any potential problems.
>>
>> Found by Linux Verification Center with Svace static analyzer.
>>
>> Fixes: 8a37d87d72f0 ("usb: typec: Bus type for alternate modes")
>> Signed-off-by: Maxim Korotkov <korotkov.maxim.s@gmail.com>
>> ---
>> drivers/usb/typec/bus.c | 13 +++++++++----
>> 1 file changed, 9 insertions(+), 4 deletions(-)
>>
>> diff --git a/drivers/usb/typec/bus.c b/drivers/usb/typec/bus.c
>> index 098f0efaa58d..ae0aca8f33db 100644
>> --- a/drivers/usb/typec/bus.c
>> +++ b/drivers/usb/typec/bus.c
>> @@ -125,13 +125,16 @@ EXPORT_SYMBOL_GPL(typec_altmode_notify);
>> */
>> int typec_altmode_enter(struct typec_altmode *adev, u32 *vdo)
>> {
>> - struct altmode *partner = to_altmode(adev)->partner;
>> - struct typec_altmode *pdev = &partner->adev;
>> + struct altmode *partner;
>> + struct typec_altmode *pdev;
>> int ret;
>>
>> if (!adev || adev->active)
>> return 0;
>>
>> + partner = to_altmode(adev)->partner;
>> + pdev = &partner->adev;
>
> As you point out, the original code is still fine here, we check before
> we actually use these values.
>
> Also, can adev every actually be NULL? In looking at the code paths, I
> can't see how that could happen.
>
> thanks,
>
> greg k-h
I agree that the adev will most likely never be NULL, but usually this
pointer is checked before usage (for example in typec_altmode_notify()
or typec_altmode_vdm()). It is a little odd that in these functions it
utilized before check. Is it just extra check that can be removed?
best regards, Max
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] usb: typec: fix potential NULL dereference
2023-04-18 6:56 ` Maxim Korotkov
@ 2023-04-20 8:39 ` Heikki Krogerus
0 siblings, 0 replies; 4+ messages in thread
From: Heikki Krogerus @ 2023-04-20 8:39 UTC (permalink / raw)
To: Maxim Korotkov; +Cc: Greg Kroah-Hartman, linux-usb, linux-kernel, lvc-project
On Tue, Apr 18, 2023 at 09:56:51AM +0300, Maxim Korotkov wrote:
> On 18.04.2023 09:16, Greg Kroah-Hartman wrote:
> > On Mon, Apr 17, 2023 at 10:50:03PM +0300, Maxim Korotkov wrote:
> > > The pointer 'adev' was being dereferenced before being checked for NULL
> > > in the 'type_alt mode_enter()' and 'type_alt mode_exit()' functions.
> > > Although this is a hypothetical issue, it's better to move the pointer
> > > assignment after the NULL check to avoid any potential problems.
> > >
> > > Found by Linux Verification Center with Svace static analyzer.
> > >
> > > Fixes: 8a37d87d72f0 ("usb: typec: Bus type for alternate modes")
> > > Signed-off-by: Maxim Korotkov <korotkov.maxim.s@gmail.com>
> > > ---
> > > drivers/usb/typec/bus.c | 13 +++++++++----
> > > 1 file changed, 9 insertions(+), 4 deletions(-)
> > >
> > > diff --git a/drivers/usb/typec/bus.c b/drivers/usb/typec/bus.c
> > > index 098f0efaa58d..ae0aca8f33db 100644
> > > --- a/drivers/usb/typec/bus.c
> > > +++ b/drivers/usb/typec/bus.c
> > > @@ -125,13 +125,16 @@ EXPORT_SYMBOL_GPL(typec_altmode_notify);
> > > */
> > > int typec_altmode_enter(struct typec_altmode *adev, u32 *vdo)
> > > {
> > > - struct altmode *partner = to_altmode(adev)->partner;
> > > - struct typec_altmode *pdev = &partner->adev;
> > > + struct altmode *partner;
> > > + struct typec_altmode *pdev;
> > > int ret;
> > > if (!adev || adev->active)
> > > return 0;
> > > + partner = to_altmode(adev)->partner;
> > > + pdev = &partner->adev;
> >
> > As you point out, the original code is still fine here, we check before
> > we actually use these values.
> >
> > Also, can adev every actually be NULL? In looking at the code paths, I
> > can't see how that could happen.
> >
> > thanks,
> >
> > greg k-h
>
> I agree that the adev will most likely never be NULL, but usually this
> pointer is checked before usage (for example in typec_altmode_notify() or
> typec_altmode_vdm()). It is a little odd that in these functions it utilized
> before check. Is it just extra check that can be removed?
Please go ahead and remove it.
thanks,
--
heikki
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2023-04-20 8:39 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-04-17 19:50 [PATCH] usb: typec: fix potential NULL dereference Maxim Korotkov
2023-04-18 6:16 ` Greg Kroah-Hartman
2023-04-18 6:56 ` Maxim Korotkov
2023-04-20 8:39 ` Heikki Krogerus
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).