usb: typec: ucsi: add bounds check in gaokun_ucsi_handle_no_usb_event()
PATCH 1/1 usb: typec: ucsi: add bounds check in gaokun_ucsi_handle_no_usb_event()
gaokun_ucsi_handle_no_usb_event() uses the index derived from the CCI connector number to access the ports array without bounds checking. The index comes from UCSI_CCI_CONNECTOR(cci) - 1, which can range from -1 to 126, but the ports array is sized to num_ports (typically 2-4).
Other functions in the same driver validate the index before use (e.g. gaokun_ucsi_connector_status, gaokun_ucsi_handle_altmode), but this function was missed.
Add a bounds check to prevent out-of-bounds array access.
--- drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c +3 -0
@@ -349,6 +349,9 @@ static void gaokun_ucsi_handle_no_usb_event(struct gaokun_ucsi *uec, int idx) {struct gaokun_ucsi_port *port; +if (idx <0 || idx >= uec->num_ports) +return ; + port = &uec->ports[idx];if (!wait_for_completion_timeout (&port->usb_ack,2 *HZ )) {dev_warn (uec->dev,"No USB EVENT, triggered by UCSI EVENT" );
Signature · 1 line
2.43.0.windows.1
Quoted 6 lines · Nathan Rebello
gaokun_ucsi_handle_no_usb_event()uses the index derived from the CCI connector number to access the ports array without bounds checking. The index comes fromUCSI_CCI_CONNECTOR(cci)- 1, which can range from -1 to 126, but the ports array is sized tonum_ports(typically 2-4).
Generally, if there is a connector change, UCSI_CCI_CONNECTOR(cci) will be greater than 0, UCSI_CCI_CONNECTOR(cci) - 1 will be the port index. and it will always be >= 0. boundary checking is not needed.
NAK.
Best wishes,
Pengyu
Quoted 29 lines
Other functions in the same driver validate the index before use (e.g.
gaokun_ucsi_connector_status,gaokun_ucsi_handle_altmode), but this function was missed.Add a bounds check to prevent out-of-bounds array access.
Fixes 00327d7f2c8c ("usb: typec: ucsi: add Huawei Matebook E Go ucsi driver")Signed-off-by Nathan Rebello <[email protected]>--- drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c | 3 +++ 1 file changed, 3 insertions(+)drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c +3
-1@@ -349,6 +349,9 @@ static void gaokun_ucsi_handle_no_usb_event(struct gaokun_ucsi *uec, int idx) {struct gaokun_ucsi_port *port; +if (idx <0 || idx >= uec->num_ports) +return ; + port = &uec->ports[idx];if (!wait_for_completion_timeout (&port->usb_ack,2 *HZ )) {dev_warn (uec->dev,"No USB EVENT, triggered by UCSI EVENT" ); --2.43.0.windows.1
Agreed that UCSI_CCI_CONNECTOR(cci) will be > 0 when a connector change occurs, so idx >= 0. However, the check also guards against idx >= num_ports. UCSI_CCI_CONNECTOR() returns a 7-bit value (up to 127) while num_ports is typically 2-4. Other functions in the same driver already validate this -- gaokun_ucsi_connector_status() checks con->num > uec->num_ports at line 188, and gaokun_ucsi_handle_altmode() checks idx >= num_connectors at line 295. This function was the only one missing the upper bound check.
Nathan
Pengyu Luo
maintainerQuoted 10 lines · Nathan Rebello
Agreed that
UCSI_CCI_CONNECTOR(cci)will be > 0 when a connector change occurs, so idx >= 0. However, the check also guards against idx >=num_ports.UCSI_CCI_CONNECTOR()returns a 7-bit value (up to 127) whilenum_portsis typically 2-4. Other functions in the same driver already validate this --gaokun_ucsi_connector_status()checkscon->num>uec->num_portsat line 188, andgaokun_ucsi_handle_altmode()checks idx >=num_connectorsat line 295. This function was the only one missing the upper bound check.
Upper boundary checking LGTM, thanks.
Note: I followed the check like other qualcomm platforms do. It may be unnecessary, I think, if we trust the PPM would send the correct information. ucsi_connector_change() never check it.
Anyways, you are welcome to add the upper boundary check.
Best wishes,
Pengyu