diff mbox series

[-next] scsi: snic: fix double free in snic_tgt_create()

Message ID 20230819083941.164365-1-wangzhu9@huawei.com
State New
Headers show
Series [-next] scsi: snic: fix double free in snic_tgt_create() | expand

Commit Message

Zhu Wang Aug. 19, 2023, 8:39 a.m. UTC
The commit 41320b18a0e0 ("scsi: snic: Fix possible memory leak if
device_add() fails") fix the memory leak caused by dev_set_name() when
device_add() failed. While it did not consider that 'tgt' has already been
released when put_device(&tgt->dev) is called. We removed kfree(tgt) in
the error path to avoid double free 'tgt'. And we moved
put_device(&tgt->dev) after the removed kfree(tgt) to avoid UAF
(Use-After-Free).

Fixes: 41320b18a0e0 ("scsi: snic: Fix possible memory leak if device_add() fails")
Signed-off-by: Zhu Wang <wangzhu9@huawei.com>
---
 drivers/scsi/snic/snic_disc.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

Comments

Martin K. Petersen Aug. 25, 2023, 2:03 a.m. UTC | #1
Zhu,

> The commit 41320b18a0e0 ("scsi: snic: Fix possible memory leak if
> device_add() fails") fix the memory leak caused by dev_set_name() when
> device_add() failed. While it did not consider that 'tgt' has already
> been released when put_device(&tgt->dev) is called. We removed
> kfree(tgt) in the error path to avoid double free 'tgt'.

Where is tgt freed prior to the kfree() call?
Martin K. Petersen Aug. 25, 2023, 2:32 a.m. UTC | #2
> put_device(&tgt->dev) will call snic_tgt_dev_release() in which
> kfree(tgt) is called, so tgt is freed twice

OK, I get it. Thanks!
Martin K. Petersen Aug. 25, 2023, 9:50 p.m. UTC | #3
On Sat, 19 Aug 2023 08:39:41 +0000, Zhu Wang wrote:

> The commit 41320b18a0e0 ("scsi: snic: Fix possible memory leak if
> device_add() fails") fix the memory leak caused by dev_set_name() when
> device_add() failed. While it did not consider that 'tgt' has already been
> released when put_device(&tgt->dev) is called. We removed kfree(tgt) in
> the error path to avoid double free 'tgt'. And we moved
> put_device(&tgt->dev) after the removed kfree(tgt) to avoid UAF
> (Use-After-Free).
> 
> [...]

Applied to 6.5/scsi-fixes, thanks!

[1/1] scsi: snic: fix double free in snic_tgt_create()
      https://git.kernel.org/mkp/scsi/c/1bd3a76880b2
diff mbox series

Patch

diff --git a/drivers/scsi/snic/snic_disc.c b/drivers/scsi/snic/snic_disc.c
index e429ad23c396..4db3ba62fcd3 100644
--- a/drivers/scsi/snic/snic_disc.c
+++ b/drivers/scsi/snic/snic_disc.c
@@ -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;