mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH -next] scsi: snic: fix double free in snic_tgt_create()
@ 2023-08-19  8:39 Zhu Wang
  2023-08-25  2:03 ` Martin K. Petersen
  2023-08-25 21:50 ` Martin K. Petersen
  0 siblings, 2 replies; 4+ messages in thread
From: Zhu Wang @ 2023-08-19  8:39 UTC (permalink / raw)
  To: kartilak, sebaddel, jejb, martin.petersen, nmusini, bvanassche,
	dan.carpenter, linux-scsi, linux-kernel
  Cc: wangzhu9

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(-)

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;
-- 
2.34.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH -next] scsi: snic: fix double free in snic_tgt_create()
  2023-08-19  8:39 [PATCH -next] scsi: snic: fix double free in snic_tgt_create() Zhu Wang
@ 2023-08-25  2:03 ` Martin K. Petersen
       [not found]   ` <54e80365-21c4-47ed-8b38-8cead6271163@huawei.com>
  2023-08-25 21:50 ` Martin K. Petersen
  1 sibling, 1 reply; 4+ messages in thread
From: Martin K. Petersen @ 2023-08-25  2:03 UTC (permalink / raw)
  To: Zhu Wang
  Cc: kartilak, sebaddel, jejb, martin.petersen, nmusini, bvanassche,
	dan.carpenter, linux-scsi, linux-kernel


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	Oracle Linux Engineering

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH -next] scsi: snic: fix double free in snic_tgt_create()
       [not found]   ` <54e80365-21c4-47ed-8b38-8cead6271163@huawei.com>
@ 2023-08-25  2:32     ` Martin K. Petersen
  0 siblings, 0 replies; 4+ messages in thread
From: Martin K. Petersen @ 2023-08-25  2:32 UTC (permalink / raw)
  To: wangzhu
  Cc: Martin K. Petersen, kartilak, sebaddel, jejb, nmusini,
	bvanassche, dan.carpenter, linux-scsi, linux-kernel


> 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	Oracle Linux Engineering

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH -next] scsi: snic: fix double free in snic_tgt_create()
  2023-08-19  8:39 [PATCH -next] scsi: snic: fix double free in snic_tgt_create() Zhu Wang
  2023-08-25  2:03 ` Martin K. Petersen
@ 2023-08-25 21:50 ` Martin K. Petersen
  1 sibling, 0 replies; 4+ messages in thread
From: Martin K. Petersen @ 2023-08-25 21:50 UTC (permalink / raw)
  To: kartilak, sebaddel, jejb, nmusini, bvanassche, dan.carpenter,
	linux-scsi, linux-kernel, Zhu Wang
  Cc: Martin K . Petersen

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

-- 
Martin K. Petersen	Oracle Linux Engineering

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-08-25 21:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-19  8:39 [PATCH -next] scsi: snic: fix double free in snic_tgt_create() Zhu Wang
2023-08-25  2:03 ` Martin K. Petersen
     [not found]   ` <54e80365-21c4-47ed-8b38-8cead6271163@huawei.com>
2023-08-25  2:32     ` Martin K. Petersen
2023-08-25 21:50 ` Martin K. Petersen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®