Skip to contentSkip to conversation

scsi: snic: fix double free in snic_tgt_create()

PATCH Applied Zhu Wang 2 participants 4 messages Aug 19, 2023 – Aug 25, 2023 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

Zhu Wang

author
#0 lore raw

PATCH 1/1 scsi: snic: fix double free in snic_tgt_create()

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")
---
 drivers/scsi/snic/snic_disc.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/scsi/snic/snic_disc.c +1 -2
index e429ad23c396..4db3ba62fcd3 100644
@@ -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;
Signature · 1 line
2.34.1
#1 lore raw

Zhu,

Quoted 5 lines

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?

Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering

A message that is not in the archive (sent off-list or not yet received): [email protected]

#3 lore raw
Quoted 3 lines

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!

Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering
#4 lore raw
In reply to Zhu Wang
Quoted 9 lines · Zhu Wang

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
Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering