Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 24766175 authored by Zhu Wang's avatar Zhu Wang Committed by Greg Kroah-Hartman
Browse files

scsi: snic: Fix double free in snic_tgt_create()



commit 1bd3a76880b2bce017987cf53780b372cf59528e upstream.

Commit 41320b18a0e0 ("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: 41320b18a0e0 ("scsi: snic: Fix possible memory leak if device_add() fails")
Signed-off-by: default avatarZhu Wang <wangzhu9@huawei.com>
Link: https://lore.kernel.org/r/20230819083941.164365-1-wangzhu9@huawei.com


Signed-off-by: default avatarMartin K. Petersen <martin.petersen@oracle.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 884945d0
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
@@ -316,12 +316,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;