linux-block.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] scsi: sd: call device_del() if device_add_disk() fails
@ 2022-03-29 15:49 Fabio M. De Francesco
  2022-03-30  4:30 ` Dan Carpenter
  2022-03-31 15:26 ` Wenchao Hao
  0 siblings, 2 replies; 15+ messages in thread
From: Fabio M. De Francesco @ 2022-03-29 15:49 UTC (permalink / raw)
  To: axboe, linux-block, linux-kernel, syzkaller-bugs,
	James E.J. Bottomley, Martin K. Petersen, linux-scsi,
	Dan Carpenter
  Cc: Fabio M. De Francesco, syzbot+f08c77040fa163a75a46

In sd_probe(), if device_add_disk() fails it simply calls put_device()
and jumps to the "out" label but the device is never deleted from system.
This leads to a memory leak as reported by Syzbot.[1]

Fix this bug by calling device_del() soon before put_device() when 
device_add_disk() fails.

[1] [syzbot] memory leak in blk_mq_init_tags
https://lore.kernel.org/lkml/000000000000c341cc05db38c1b0@google.com/

Reported-by: syzbot+f08c77040fa163a75a46@syzkaller.appspotmail.com
Suggested-by: Dan Carpenter <dan.carpenter@oracle.com>
Fixes: 2a7a891f4c40 ("scsi: sd: Add error handling support for add_disk()")
Signed-off-by: Fabio M. De Francesco <fmdefrancesco@gmail.com>
---

This patch replace the previous attempt to fix the bug reported by
Syzbot. Therefore, the previous wrong patch at 
https://lore.kernel.org/lkml/20220328084452.11479-1-fmdefrancesco@gmail.com/
must be discarded.

Many thanks to Dan Carpenter.

 drivers/scsi/sd.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index a390679cf458..13d96d0f9dde 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -3474,6 +3474,7 @@ static int sd_probe(struct device *dev)
 
 	error = device_add_disk(dev, gd, NULL);
 	if (error) {
+		device_del(&sdkp->disk_dev);
 		put_device(&sdkp->disk_dev);
 		goto out;
 	}
-- 
2.34.1


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

end of thread, other threads:[~2022-03-31 17:21 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-03-29 15:49 [PATCH] scsi: sd: call device_del() if device_add_disk() fails Fabio M. De Francesco
2022-03-30  4:30 ` Dan Carpenter
2022-03-31 15:26 ` Wenchao Hao
2022-03-31  5:41   ` Dan Carpenter
2022-03-31  5:45     ` Christoph Hellwig
2022-03-31  9:07       ` Fabio M. De Francesco
2022-03-31  9:13         ` Christoph Hellwig
2022-03-31 10:13           ` Fabio M. De Francesco
2022-03-31 12:14     ` Wenchao Hao
2022-03-31 13:42       ` Dan Carpenter
2022-03-31 14:19         ` James Bottomley
2022-03-31 15:11           ` Dan Carpenter
2022-03-31 16:14         ` Fabio M. De Francesco
2022-03-31 16:24           ` Dan Carpenter
2022-03-31 17:21             ` Fabio M. De Francesco

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).