Skip to contentSkip to conversation

usb: typec: fix potential NULL dereference

PATCH In review Maxim Korotkov 3 participants 4 messages Apr 17, 2023 – Apr 20, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: typec: fix potential NULL dereference

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")
---
 drivers/usb/typec/bus.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/usb/typec/bus.c +9 -4
index 098f0efaa58d..ae0aca8f33db 100644
@@ -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;
 
Signature · 1 line
2.37.2
#1 lore raw
Quoted 22 lines · Maxim Korotkov

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")
---
 drivers/usb/typec/bus.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/usb/typec/bus.c +0 -1
index 098f0efaa58d..ae0aca8f33db 100644
@@ -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;

drivers/usb/typec/bus.c EXPORT_SYMBOL_GPL(typec_altmode_notify); · line 136

Quoted 10 lines
drivers/usb/typec/bus.c +4 -1
-	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

#2 lore raw
Quoted 37 lines · Greg Kroah-Hartman
Quoted 22 lines · Maxim Korotkov

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")
---
  drivers/usb/typec/bus.c | 13 +++++++++----
  1 file changed, 9 insertions(+), 4 deletions(-)
drivers/usb/typec/bus.c +0 -1
index 098f0efaa58d..ae0aca8f33db 100644
@@ -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;

drivers/usb/typec/bus.c EXPORT_SYMBOL_GPL(typec_altmode_notify); · line 136

Quoted 10 lines
drivers/usb/typec/bus.c +4 -1
-	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.

Quoted 6 lines

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

maintainer
#3 lore raw
Quoted 39 lines · Maxim Korotkov
Quoted 38 lines · Greg Kroah-Hartman
Quoted 20 lines · Maxim Korotkov

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")
---
  drivers/usb/typec/bus.c | 13 +++++++++----
  1 file changed, 9 insertions(+), 4 deletions(-)
drivers/usb/typec/bus.c +0 -0
index 098f0efaa58d..ae0aca8f33db 100644
@@ -125,13 +125,16 @@ EXPORT_SYMBOL_GPL(typec_altmode_notify);
   */
  int typec_altmode_enter(struct typec_altmode *adev, u32 *vdo)

drivers/usb/typec/bus.c EXPORT_SYMBOL_GPL(typec_altmode_notify); · line 134

Quoted 10 lines
drivers/usb/typec/bus.c +4 -2
  {
-	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.

Quoted 8 lines
Quoted 3 lines

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,

Signature · 1 line
heikki