usb: typec: tcpm: Check for port partner validity before consuming it
Subsystems (4)
Badhri Jagan Sridharan
maintainerPATCH 1/1 usb: typec: tcpm: Check for port partner validity before consuming it
typec_register_partner() does not guarantee partner registration to always succeed. In the event of failure, port->partner is set to the error value or NULL. Given that port->partner validity is not checked, this results in the following crash:
[17514.377076][ T275] Unable to handle kernel NULL pointer dereference at virtual address 00000000000003c0 [17514.378270][ T275] pc : run_state_machine+0x1bc8/0x1c08 [17514.378286][ T275] lr : run_state_machine+0x1b90/0x1c08 .. [17514.378377][ T275] Call trace: [17514.378381][ T275] run_state_machine+0x1bc8/0x1c08 [17514.378388][ T275] tcpm_state_machine_work+0x94/0xe4 [17514.378396][ T275] kthread_worker_fn+0x118/0x328 [17514.378406][ T275] kthread+0x1d0/0x23c [17514.378412][ T275] ret_from_fork+0x10/0x20
To prevent the crash, check for port->partner validity before derefencing it in all the call sites.
--- drivers/usb/typec/tcpm/tcpm.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-)
drivers/usb/typec/tcpm/tcpm.c +14 -5
@@ -4,7 +4,6 @@* * USB Power Delivery protocol stack. */ -#include <linux/completion.h> #include <linux/debugfs.h> #include <linux/device.h>
@@ -1580,7 +1579,8 @@ static void svdm_consume_identity(struct tcpm_port *port, const u32 *p, int cnt) port->partner_ident.cert_stat = p[VDO_INDEX_CSTAT ]; port->partner_ident.product = product; -typec_partner_set_identity (port->partner); +if (!IS_ERR_OR_NULL (port->partner)) +typec_partner_set_identity (port->partner); tcpm_log(port, "Identity: %04x:%04x.%04x", PD_IDH_VID(vdo),
@@ -1742,6 +1742,9 @@ static void tcpm_register_partner_altmodes(struct tcpm_port *port)struct typec_altmode *altmode;int i; +if (IS_ERR_OR_NULL (port->partner)) +return ; +for (i =0 ; i < modep->altmodes; i++) { altmode =typec_partner_register_altmode (port->partner, &modep->altmode_desc[i]);
@@ -4244,7 +4247,8 @@ static void tcpm_typec_connect(struct tcpm_port *port) port->partner =typec_register_partner (port->typec_port, &port->partner_desc); port->connected = true; -typec_partner_set_usb_power_delivery (port->partner, port->partner_pd); +if (!IS_ERR_OR_NULL (port->partner)) +typec_partner_set_usb_power_delivery (port->partner, port->partner_pd); } }
@@ -4323,8 +4327,10 @@ static void tcpm_typec_disconnect(struct tcpm_port *port) port->plug_prime =NULL ; port->cable =NULL ;if (port->connected) { -typec_partner_set_usb_power_delivery (port->partner,NULL ); -typec_unregister_partner (port->partner); +if (!IS_ERR_OR_NULL (port->partner)) { +typec_partner_set_usb_power_delivery (port->partner,NULL ); +typec_unregister_partner (port->partner); + } port->partner =NULL ; port->connected = false; }
@@ -4549,6 +4555,9 @@ static enum typec_cc_status tcpm_pwr_opmode_to_rp(enum typec_pwr_opmode opmode)static void tcpm_set_initial_svdm_version (struct tcpm_port *port) { +if (IS_ERR_OR_NULL (port->partner)) +return ; +switch (port->negotiated_rev) {case PD_REV30 :break ;
base-commit: 3f12222a4bebeb13ce06ddecc1610ad32fa835dd
Signature · 1 line
2.44.0.769.g3c40516874-goog
Quoted 5 lines · Badhri Jagan Sridharan
typec_register_partner()does not guarantee partner registration to always succeed. In the event of failure,port->partneris set to the error value or NULL. Given thatport->partnervalidity is not checked, this results in the following crash:
Quoted 10 lines
[17514.377076][ T275] Unable to handle kernel NULL pointer dereference at virtual address 00000000000003c0 [17514.378270][ T275] pc : run_state_machine+0x1bc8/0x1c08 [17514.378286][ T275] lr : run_state_machine+0x1b90/0x1c08 .. [17514.378377][ T275] Call trace: [17514.378381][ T275] run_state_machine+0x1bc8/0x1c08 [17514.378388][ T275] tcpm_state_machine_work+0x94/0xe4 [17514.378396][ T275] kthread_worker_fn+0x118/0x328 [17514.378406][ T275] kthread+0x1d0/0x23c [17514.378412][ T275] ret_from_fork+0x10/0x20
Nit: thre is little use in the timestamps and the T275 tag. In future patches you can safely ommit such data.
Quoted 24 lines
To prevent the crash, check for
port->partnervalidity before derefencing it in all the call sites.Fixes c97cd0b4b54e ("usb: typec: tcpm: set initial svdm version based on pd revision")Signed-off-by Badhri Jagan Sridharan <[email protected]>--- drivers/usb/typec/tcpm/tcpm.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-)drivers/usb/typec/tcpm/tcpm.c +0
-1@@ -4,7 +4,6 @@* * USB Power Delivery protocol stack. */ -#include <linux/completion.h> #include <linux/debugfs.h> #include <linux/device.h>
drivers/usb/typec/tcpm/tcpm.c static void svdm_consume_identity(struct tcpm_port *port, const u32 *p, int cnt) · line 1582
Quoted 5 lines
drivers/usb/typec/tcpm/tcpm.c +1
-1@@ -1580,7 +1579,8 @@ static void svdm_consume_identity(struct tcpm_port *port, const u32 *p, int cnt) port->partner_ident.cert_stat = p[VDO_INDEX_CSTAT ]; port->partner_ident.product = product; -typec_partner_set_identity (port->partner); +if (!IS_ERR_OR_NULL (port->partner))
I think that it might be better to check typec_register_partner() result in tcpm_typec_connect() and skip setting port->partner in case it returned an error (an error message would be also beneficial). See for example, how this is handled in ucsi_register_partner().
Then these checks can be turned into simple if (port->partner).
Quoted 52 lines
drivers/usb/typec/tcpm/tcpm.c +13
-3+typec_partner_set_identity (port->partner); tcpm_log(port, "Identity: %04x:%04x.%04x", PD_IDH_VID(vdo),@@ -1742,6 +1742,9 @@ static void tcpm_register_partner_altmodes(struct tcpm_port *port)struct typec_altmode *altmode;int i; +if (IS_ERR_OR_NULL (port->partner)) +return ; +for (i =0 ; i < modep->altmodes; i++) { altmode =typec_partner_register_altmode (port->partner, &modep->altmode_desc[i]);@@ -4244,7 +4247,8 @@ static void tcpm_typec_connect(struct tcpm_port *port) port->partner =typec_register_partner (port->typec_port, &port->partner_desc); port->connected = true; -typec_partner_set_usb_power_delivery (port->partner, port->partner_pd); +if (!IS_ERR_OR_NULL (port->partner)) +typec_partner_set_usb_power_delivery (port->partner, port->partner_pd); } }@@ -4323,8 +4327,10 @@ static void tcpm_typec_disconnect(struct tcpm_port *port) port->plug_prime =NULL ; port->cable =NULL ;if (port->connected) { -typec_partner_set_usb_power_delivery (port->partner,NULL ); -typec_unregister_partner (port->partner); +if (!IS_ERR_OR_NULL (port->partner)) { +typec_partner_set_usb_power_delivery (port->partner,NULL ); +typec_unregister_partner (port->partner); + } port->partner =NULL ; port->connected = false; }@@ -4549,6 +4555,9 @@ static enum typec_cc_status tcpm_pwr_opmode_to_rp(enum typec_pwr_opmode opmode)static void tcpm_set_initial_svdm_version (struct tcpm_port *port) { +if (IS_ERR_OR_NULL (port->partner)) +return ; +switch (port->negotiated_rev) {case PD_REV30 :break ;base-commit:
3f12222a4bebeb13ce06ddecc1610ad32fa835ddSignature · 1 line
2.44.0.769.g3c40516874-goog
Signature · 2 lines
With best wishes Dmitry
Badhri Jagan Sridharan
maintainerQuoted 56 lines · Dmitry Baryshkov
Quoted 5 lines · Badhri Jagan Sridharan
typec_register_partner()does not guarantee partner registration to always succeed. In the event of failure,port->partneris set to the error value or NULL. Given thatport->partnervalidity is not checked, this results in the following crash:Quoted 10 lines
[17514.377076][ T275] Unable to handle kernel NULL pointer dereference at virtual address 00000000000003c0 [17514.378270][ T275] pc : run_state_machine+0x1bc8/0x1c08 [17514.378286][ T275] lr : run_state_machine+0x1b90/0x1c08 .. [17514.378377][ T275] Call trace: [17514.378381][ T275] run_state_machine+0x1bc8/0x1c08 [17514.378388][ T275] tcpm_state_machine_work+0x94/0xe4 [17514.378396][ T275] kthread_worker_fn+0x118/0x328 [17514.378406][ T275] kthread+0x1d0/0x23c [17514.378412][ T275] ret_from_fork+0x10/0x20Nit: thre is little use in the timestamps and the T275 tag. In future patches you can safely ommit such data.
Quoted 24 lines
To prevent the crash, check for
port->partnervalidity before derefencing it in all the call sites.Fixes c97cd0b4b54e ("usb: typec: tcpm: set initial svdm version based on pd revision")Signed-off-by Badhri Jagan Sridharan <[email protected]>--- drivers/usb/typec/tcpm/tcpm.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-)drivers/usb/typec/tcpm/tcpm.c +0
-1@@ -4,7 +4,6 @@* * USB Power Delivery protocol stack. */ -#include <linux/completion.h> #include <linux/debugfs.h> #include <linux/device.h> drivers/usb/typec/tcpm/tcpm.c static void svdm_consume_identity(struct tcpm_port *port, const u32 *p, int cnt) · line 1582
Quoted 5 lines
drivers/usb/typec/tcpm/tcpm.c +1
-1@@ -1580,7 +1579,8 @@ static void svdm_consume_identity(struct tcpm_port *port, const u32 *p, int cnt) port->partner_ident.cert_stat = p[VDO_INDEX_CSTAT ]; port->partner_ident.product = product; -typec_partner_set_identity (port->partner); +if (!IS_ERR_OR_NULL (port->partner))I think that it might be better to check
typec_register_partner()result intcpm_typec_connect()and skip settingport->partnerin case it returned an error (an error message would be also beneficial). See for example, how this is handled inucsi_register_partner().
Quoted 1 line
Then these checks can be turned into simple
if (port->partner).
Sounds good ! However, I was wondering whether the error message is redundant as typec_register_partner() also prints one. I have anyways added that in V3.
Thanks,
Badhri
Quoted 57 lines
Quoted 49 lines
drivers/usb/typec/tcpm/tcpm.c +13
-3+typec_partner_set_identity (port->partner); tcpm_log(port, "Identity: %04x:%04x.%04x", PD_IDH_VID(vdo),@@ -1742,6 +1742,9 @@ static void tcpm_register_partner_altmodes(struct tcpm_port *port)struct typec_altmode *altmode;int i; +if (IS_ERR_OR_NULL (port->partner)) +return ; +for (i =0 ; i < modep->altmodes; i++) { altmode =typec_partner_register_altmode (port->partner, &modep->altmode_desc[i]);@@ -4244,7 +4247,8 @@ static void tcpm_typec_connect(struct tcpm_port *port) port->partner =typec_register_partner (port->typec_port, &port->partner_desc); port->connected = true; -typec_partner_set_usb_power_delivery (port->partner, port->partner_pd); +if (!IS_ERR_OR_NULL (port->partner)) +typec_partner_set_usb_power_delivery (port->partner, port->partner_pd); } }@@ -4323,8 +4327,10 @@ static void tcpm_typec_disconnect(struct tcpm_port *port) port->plug_prime =NULL ; port->cable =NULL ;if (port->connected) { -typec_partner_set_usb_power_delivery (port->partner,NULL ); -typec_unregister_partner (port->partner); +if (!IS_ERR_OR_NULL (port->partner)) { +typec_partner_set_usb_power_delivery (port->partner,NULL ); +typec_unregister_partner (port->partner); + } port->partner =NULL ; port->connected = false; }@@ -4549,6 +4555,9 @@ static enum typec_cc_status tcpm_pwr_opmode_to_rp(enum typec_pwr_opmode opmode)static void tcpm_set_initial_svdm_version (struct tcpm_port *port) { +if (IS_ERR_OR_NULL (port->partner)) +return ; +switch (port->negotiated_rev) {case PD_REV30 :break ;Quoted 4 lines
base-commit:
3f12222a4bebeb13ce06ddecc1610ad32fa835dd--
2.44.0.769.g3c40516874-goog--
With best wishes
Dmitry