Skip to contentSkip to conversation

usb: typec: Prevent setting invalid opmode value

PATCH In review Sriharsha Allenki 2 participants 3 messages Oct 22, 2020 – Oct 27, 2020

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

PATCH 1/1 usb: typec: Prevent setting invalid opmode value

Setting opmode to invalid values would lead to a paging fault failure when there is an access to the power_operation_mode.

Prevent this by checking the validity of the value that the opmode is being set.

Fixes fab9288428ec ("usb: USB Type-C connector class")
---
 drivers/usb/typec/class.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/usb/typec/class.c +2 -1
index 35eec70..63efe16 100644
@@ -1427,7 +1427,8 @@ void typec_set_pwr_opmode(struct typec_port *port,
 {
 	struct device *partner_dev;
 
-	if (port->pwr_opmode == opmode)
+	if ((port->pwr_opmode == opmode) || (opmode < TYPEC_PWR_MODE_USB) ||
+						(opmode > TYPEC_PWR_MODE_PD))
 		return;
 
 	port->pwr_opmode = opmode;
Signature · 1 line
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project
maintainer
#1 lore raw
Quoted 19 lines · Sriharsha Allenki

Setting opmode to invalid values would lead to a paging fault failure when there is an access to the power_operation_mode.

Prevent this by checking the validity of the value that the opmode is being set.

Fixes fab9288428ec ("usb: USB Type-C connector class")
---
 drivers/usb/typec/class.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/usb/typec/class.c +0 -0
index 35eec70..63efe16 100644

drivers/usb/typec/class.c void typec_set_pwr_opmode(struct typec_port *port, · line 1430

Quoted 5 lines
drivers/usb/typec/class.c +1 -1
@@ -1427,7 +1427,8 @@ void typec_set_pwr_opmode(struct typec_port *port,
 {
 	struct device *partner_dev;
 
-	if (port->pwr_opmode == opmode)
+	if ((port->pwr_opmode == opmode) || (opmode < TYPEC_PWR_MODE_USB) ||

You don't need to check if opmode < anything. opmode is enum which apparently means that GCC handles it as unsigned. Since TYPEC_PWR_MODE_USB is 0 it means opmode < TYPEC_PWR_MODE_USB is never true.

drivers/usb/typec/class.c void typec_set_pwr_opmode(struct typec_port *port, · line 1432

Quoted 2 lines
drivers/usb/typec/class.c +1 -0
+						(opmode > TYPEC_PWR_MODE_PD))
 		return;

You really need to print an error at the very least. Otherwise we will just silently hide possible driver bugs.

To be honest, I'm not a big fan of this kind of checks. They have created more problems than they have fixed in more than one occasion to me. For example, there really is no guarantee that the maximum will always be TYPEC_PWR_MODE_PD, which means we probable should have something like TYPEC_PWR_MODE_MAX defined somewhere that you compare the opmode value to instead of TYPEC_PWR_MODE_PD to play it safe, but let's not bother with that for now (it will create other problems).

Basically, with functions like this, especially since it doesn't return anything, the responsibility of checking the validity of the parameters that the caller supplies to it belongs to the caller IMO, not the function itself. I would be happy to explain that in the kernel doc style comment of the function.

If you still feel that this change is really necessary, meaning you have some actual case where the caller can _not_ check the range before calling this function, then explain the case you have carefully in the commit message and add the check as a separate condition:

drivers/usb/typec/class.c +5 -0
index 35eec707cb512..7de6913d90f9c 100644
@@ -1430,6 +1430,11 @@ void typec_set_pwr_opmode(struct typec_port *port,
        if (port->pwr_opmode == opmode)
                return;
 
+       if (opmode > TYPEC_PWR_OPMODE_PD) {
+               dev_err(&port->dev, "blah-blah-blah\n");
+               return;
+       }
+
        port->pwr_opmode = opmode;
        sysfs_notify(&port->dev.kobj, NULL, "power_operation_mode");
        kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);

Otherwise you can just propose a patch that improves the documentation of this function, explaining that it does not take any responsibility over the parameters passed to it for now.

thanks,

Signature · 1 line
heikki
#2 lore raw

Hi Heikki,

Thanks a lot for the review.

Quoted 49 lines · Heikki Krogerus
Quoted 19 lines · Sriharsha Allenki

Setting opmode to invalid values would lead to a paging fault failure when there is an access to the power_operation_mode.

Prevent this by checking the validity of the value that the opmode is being set.

Fixes fab9288428ec ("usb: USB Type-C connector class")
---
 drivers/usb/typec/class.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/usb/typec/class.c +0 -0
index 35eec70..63efe16 100644

drivers/usb/typec/class.c void typec_set_pwr_opmode(struct typec_port *port, · line 1430

Quoted 5 lines
drivers/usb/typec/class.c +1 -1
@@ -1427,7 +1427,8 @@ void typec_set_pwr_opmode(struct typec_port *port,
 {
 	struct device *partner_dev;
 
-	if (port->pwr_opmode == opmode)
+	if ((port->pwr_opmode == opmode) || (opmode < TYPEC_PWR_MODE_USB) ||

You don't need to check if opmode < anything. opmode is enum which apparently means that GCC handles it as unsigned. Since TYPEC_PWR_MODE_USB is 0 it means opmode < TYPEC_PWR_MODE_USB is never true.

drivers/usb/typec/class.c void typec_set_pwr_opmode(struct typec_port *port, · line 1432

Quoted 2 lines
drivers/usb/typec/class.c +1 -0
+						(opmode > TYPEC_PWR_MODE_PD))
 		return;

You really need to print an error at the very least. Otherwise we will just silently hide possible driver bugs.

To be honest, I'm not a big fan of this kind of checks. They have created more problems than they have fixed in more than one occasion to me. For example, there really is no guarantee that the maximum will always be TYPEC_PWR_MODE_PD, which means we probable should have something like TYPEC_PWR_MODE_MAX defined somewhere that you compare the opmode value to instead of TYPEC_PWR_MODE_PD to play it safe, but let's not bother with that for now (it will create other problems).

Basically, with functions like this, especially since it doesn't return anything, the responsibility of checking the validity of the parameters that the caller supplies to it belongs to the caller IMO, not the function itself. I would be happy to explain that in the kernel doc style comment of the function.

Quoted 4 lines

If you still feel that this change is really necessary, meaning you have some actual case where the caller can _not_ check the range before calling this function, then explain the case you have carefully in the commit message and add the check as a separate condition:

We had a bug that was setting this out of index opmode leading to the mentioned paging fault, and as you have suggested we could have added the range check there and prevented this. But there are many calls to this function, and I thought it would be a good idea to abstract that range check into this function to prevent adding multiple range checks over the driver. And further motivation was also to prevent any potential unknown bugs.

I will resend the patch with the suggested changes; separate condition and anerror statement if the above justification is acceptable, else will propose a patch to improve the documentation.

Thanks,
Sriharsha

Quoted 25 lines
drivers/usb/typec/class.c +5 -0
index 35eec707cb512..7de6913d90f9c 100644
@@ -1430,6 +1430,11 @@ void typec_set_pwr_opmode(struct typec_port *port,
        if (port->pwr_opmode == opmode)
                return;
 
+       if (opmode > TYPEC_PWR_OPMODE_PD) {
+               dev_err(&port->dev, "blah-blah-blah\n");
+               return;
+       }
+
        port->pwr_opmode = opmode;
        sysfs_notify(&port->dev.kobj, NULL, "power_operation_mode");
        kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);

Otherwise you can just propose a patch that improves the documentation of this function, explaining that it does not take any responsibility over the parameters passed to it for now.

thanks,

Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
a Linux Foundation Collaborative Project