Skip to content

Commit d3c5772

Browse files
wenchao-haomartinkpetersen
authored andcommitted
scsi: mpt3sas: Fix NULL pointer access in mpt3sas_transport_port_add()
Port is allocated by sas_port_alloc_num() and rphy is allocated by either sas_end_device_alloc() or sas_expander_alloc(), all of which may return NULL. So we need to check the rphy to avoid possible NULL pointer access. If sas_rphy_add() returned with failure, rphy is set to NULL. We would access the rphy in the following lines which would also result NULL pointer access. Fixes: 78316e9 ("scsi: mpt3sas: Fix possible resource leaks in mpt3sas_transport_port_add()") Signed-off-by: Wenchao Hao <[email protected]> Link: https://lore.kernel.org/r/[email protected] Acked-by: Sathya Prakash Veerichetty <[email protected]> Signed-off-by: Martin K. Petersen <[email protected]>
1 parent 288b327 commit d3c5772

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

drivers/scsi/mpt3sas/mpt3sas_transport.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ mpt3sas_transport_port_add(struct MPT3SAS_ADAPTER *ioc, u16 handle,
785785
goto out_fail;
786786
}
787787
port = sas_port_alloc_num(sas_node->parent_dev);
788-
if ((sas_port_add(port))) {
788+
if (!port || (sas_port_add(port))) {
789789
ioc_err(ioc, "failure at %s:%d/%s()!\n",
790790
__FILE__, __LINE__, __func__);
791791
goto out_fail;
@@ -824,13 +824,20 @@ mpt3sas_transport_port_add(struct MPT3SAS_ADAPTER *ioc, u16 handle,
824824
mpt3sas_port->remote_identify.sas_address;
825825
}
826826

827+
if (!rphy) {
828+
ioc_err(ioc, "failure at %s:%d/%s()!\n",
829+
__FILE__, __LINE__, __func__);
830+
goto out_delete_port;
831+
}
832+
827833
rphy->identify = mpt3sas_port->remote_identify;
828834

829835
if ((sas_rphy_add(rphy))) {
830836
ioc_err(ioc, "failure at %s:%d/%s()!\n",
831837
__FILE__, __LINE__, __func__);
832838
sas_rphy_free(rphy);
833839
rphy = NULL;
840+
goto out_delete_port;
834841
}
835842

836843
if (mpt3sas_port->remote_identify.device_type == SAS_END_DEVICE) {
@@ -857,7 +864,10 @@ mpt3sas_transport_port_add(struct MPT3SAS_ADAPTER *ioc, u16 handle,
857864
rphy_to_expander_device(rphy), hba_port->port_id);
858865
return mpt3sas_port;
859866

860-
out_fail:
867+
out_delete_port:
868+
sas_port_delete(port);
869+
870+
out_fail:
861871
list_for_each_entry_safe(mpt3sas_phy, next, &mpt3sas_port->phy_list,
862872
port_siblings)
863873
list_del(&mpt3sas_phy->port_siblings);

0 commit comments

Comments
 (0)