Skip to content

Commit

Permalink
scsi: fcoe: Fix possible name leak when device_register() fails
Browse files Browse the repository at this point in the history
[ 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]>
  • Loading branch information
Yang Yingliang authored and Sasha Levin committed Jan 18, 2023
1 parent 782eed2 commit 715a4be
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions drivers/scsi/fcoe/fcoe_sysfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -830,14 +830,15 @@ struct fcoe_ctlr_device *fcoe_ctlr_device_add(struct device *parent,

dev_set_name(&ctlr->dev, "ctlr_%d", ctlr->id);
error = device_register(&ctlr->dev);
if (error)
goto out_del_q2;
if (error) {
destroy_workqueue(ctlr->devloss_work_q);
destroy_workqueue(ctlr->work_q);
put_device(&ctlr->dev);
return NULL;
}

return ctlr;

out_del_q2:
destroy_workqueue(ctlr->devloss_work_q);
ctlr->devloss_work_q = NULL;
out_del_q:
destroy_workqueue(ctlr->work_q);
ctlr->work_q = NULL;
Expand Down Expand Up @@ -1036,16 +1037,16 @@ struct fcoe_fcf_device *fcoe_fcf_device_add(struct fcoe_ctlr_device *ctlr,
fcf->selected = new_fcf->selected;

error = device_register(&fcf->dev);
if (error)
goto out_del;
if (error) {
put_device(&fcf->dev);
goto out;
}

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

return fcf;

out_del:
kfree(fcf);
out:
return NULL;
}
Expand Down

0 comments on commit 715a4be

Please sign in to comment.