Skip to content

Commit d900f21

Browse files
Yang YingliangSasha Levin
authored andcommitted
scsi: fcoe: Fix possible name leak when device_register() fails
[ Upstream commit 47b6a12 ] If device_register() returns an error, the name allocated by dev_set_name() needs to be freed. As the comment of device_register() says, one should use put_device() to give up the reference in the error path. Fix this by calling put_device(), then the name can be freed in kobject_cleanup(). The 'fcf' is freed in fcoe_fcf_device_release(), so the kfree() in the error path can be removed. The 'ctlr' is freed in fcoe_ctlr_device_release(), so don't use the error label, just return NULL after calling put_device(). Fixes: 9a74e88 ("[SCSI] libfcoe: Add fcoe_sysfs") Signed-off-by: Yang Yingliang <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Martin K. Petersen <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent d770827 commit d900f21

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

drivers/scsi/fcoe/fcoe_sysfs.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -831,14 +831,15 @@ struct fcoe_ctlr_device *fcoe_ctlr_device_add(struct device *parent,
831831

832832
dev_set_name(&ctlr->dev, "ctlr_%d", ctlr->id);
833833
error = device_register(&ctlr->dev);
834-
if (error)
835-
goto out_del_q2;
834+
if (error) {
835+
destroy_workqueue(ctlr->devloss_work_q);
836+
destroy_workqueue(ctlr->work_q);
837+
put_device(&ctlr->dev);
838+
return NULL;
839+
}
836840

837841
return ctlr;
838842

839-
out_del_q2:
840-
destroy_workqueue(ctlr->devloss_work_q);
841-
ctlr->devloss_work_q = NULL;
842843
out_del_q:
843844
destroy_workqueue(ctlr->work_q);
844845
ctlr->work_q = NULL;
@@ -1037,16 +1038,16 @@ struct fcoe_fcf_device *fcoe_fcf_device_add(struct fcoe_ctlr_device *ctlr,
10371038
fcf->selected = new_fcf->selected;
10381039

10391040
error = device_register(&fcf->dev);
1040-
if (error)
1041-
goto out_del;
1041+
if (error) {
1042+
put_device(&fcf->dev);
1043+
goto out;
1044+
}
10421045

10431046
fcf->state = FCOE_FCF_STATE_CONNECTED;
10441047
list_add_tail(&fcf->peers, &ctlr->fcfs);
10451048

10461049
return fcf;
10471050

1048-
out_del:
1049-
kfree(fcf);
10501051
out:
10511052
return NULL;
10521053
}

0 commit comments

Comments
 (0)