Skip to content

Commit

Permalink
scsi: snic: Fix double free in snic_tgt_create()
Browse files Browse the repository at this point in the history
Commit 41320b1 ("scsi: snic: Fix possible memory leak if device_add()
fails") fixed the memory leak caused by dev_set_name() when device_add()
failed. However, it did not consider that 'tgt' has already been released
when put_device(&tgt->dev) is called. Remove kfree(tgt) in the error path
to avoid double free of 'tgt' and move put_device(&tgt->dev) after the
removed kfree(tgt) to avoid a use-after-free.

Fixes: 41320b1 ("scsi: snic: Fix possible memory leak if device_add() fails")
Signed-off-by: Zhu Wang <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Martin K. Petersen <[email protected]>
  • Loading branch information
PeterZhu789 authored and martinkpetersen committed Aug 25, 2023
1 parent 60c5fd2 commit 1bd3a76
Showing 1 changed file with 1 addition and 2 deletions.
3 changes: 1 addition & 2 deletions drivers/scsi/snic/snic_disc.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,11 @@ snic_tgt_create(struct snic *snic, struct snic_tgt_id *tgtid)
"Snic Tgt: device_add, with err = %d\n",
ret);

put_device(&tgt->dev);
put_device(&snic->shost->shost_gendev);
spin_lock_irqsave(snic->shost->host_lock, flags);
list_del(&tgt->list);
spin_unlock_irqrestore(snic->shost->host_lock, flags);
kfree(tgt);
put_device(&tgt->dev);
tgt = NULL;

return tgt;
Expand Down

0 comments on commit 1bd3a76

Please sign in to comment.