usb: typec: ucsi: Clear PPM capability data in ucsi_init() error path
Subsystems (3)
PATCH 1/1 usb: typec: ucsi: Clear PPM capability data in ucsi_init() error path
From: Mayank Rana <[email protected]>
If ucsi_init() fails for some reason (e.g. ucsi_register_port() fails or general communication failure to the PPM), particularly at any point after the GET_CAPABILITY command had been issued, this results in unwinding the initialization and returning an error. However the ucsi structure's ucsi_capability member retains its current value, including likely a non-zero num_connectors. And because ucsi_init() itself is done in a workqueue a UCSI interface driver will be unaware that it failed and may think the ucsi_register() call was completely successful. Later, if ucsi_unregister() is called, due to this stale ucsi->cap value it would try to access the items in the ucsi->connector array which might not be in a proper state or not even allocated at all and results in NULL or invalid pointer dereference.
Fix this by clearing the ucsi->cap value to 0 during the error path of ucsi_init() in order to prevent a later ucsi_unregister() from entering the connector cleanup loop.
--- drivers/usb/typec/ucsi/ucsi.c | 1 + 1 file changed, 1 insertion(+)
drivers/usb/typec/ucsi/ucsi.c +1 -0
@@ -1253,6 +1253,7 @@ static int ucsi_init(struct ucsi *ucsi) } err_reset: +memset (&ucsi->cap,0 ,sizeof (ucsi->cap));ucsi_reset_ppm (ucsi); err:return ret;
Signature · 1 line
2.24.0
Quoted 16 lines · Jack Pham
From: Mayank Rana <[email protected]>
If
ucsi_init()fails for some reason (e.g.ucsi_register_port()fails or general communication failure to the PPM), particularly at any point after theGET_CAPABILITYcommand had been issued, this results in unwinding the initialization and returning an error. However the ucsi structure'sucsi_capabilitymember retains its current value, including likely a non-zeronum_connectors. And becauseucsi_init()itself is done in a workqueue a UCSI interface driver will be unaware that it failed and may think theucsi_register()call was completely successful. Later, ifucsi_unregister()is called, due to this staleucsi->capvalue it would try to access the items in theucsi->connectorarray which might not be in a proper state or not even allocated at all and results in NULL or invalid pointer dereference.
Quoted 8 lines
Fix this by clearing the
ucsi->capvalue to 0 during the error path ofucsi_init()in order to prevent a laterucsi_unregister()from entering the connector cleanup loop.Fixes c1b0bc2dabfa ("usb: typec: Add support for UCSI interface")Signed-off-by Mayank Rana <[email protected]>Signed-off-by Jack Pham <[email protected]>
Quoted 18 lines
--- drivers/usb/typec/ucsi/ucsi.c | 1 + 1 file changed, 1 insertion(+)drivers/usb/typec/ucsi/ucsi.c +1
-0@@ -1253,6 +1253,7 @@ static int ucsi_init(struct ucsi *ucsi) } err_reset: +memset (&ucsi->cap,0 ,sizeof (ucsi->cap));ucsi_reset_ppm (ucsi); err:return ret;Signature · 1 line
2.24.0
Signature · 1 line
heikki