From: Zixuan Fu <r33s3n6@gmail.com>
To: kashyap.desai@broadcom.com, sumit.saxena@broadcom.com,
shivasharan.srikanteshwara@broadcom.com, jejb@linux.ibm.com,
martin.petersen@oracle.com
Cc: megaraidlinux.pdl@broadcom.com, linux-scsi@vger.kernel.org,
linux-kernel@vger.kernel.org, baijiaju1990@gmail.com,
Zixuan Fu <r33s3n6@gmail.com>, TOTE Robot <oslab@tsinghua.edu.cn>
Subject: [PATCH] scsi: megaraid: fix a possible double-free bug caused by incorrect error handling in megasas_alloc_cmdlist_fusion()
Date: Thu, 1 Sep 2022 20:54:18 +0800 [thread overview]
Message-ID: <20220901125418.2323348-1-r33s3n6@gmail.com> (raw)
In megasas_alloc_cmdlist_fusion(), when kzalloc() fails, it frees
fusion->cmd_list but does not clear it. Then, the error is propagated to
its caller megasas_alloc_cmds_fusion(), which calls
megasas_free_cmds_fusion(). In megasas_free_cmds_fusion(), it frees
fusion->cmd_list again, which causes a double-free bug.
To fix this possible bug, in megasas_alloc_cmdlist_fusion(),
fusion->cmd_list should be cleared after it is freed.
The error log in our fault-injection testing is shown as follows:
[ 4152.707452][ T21] BUG: KASAN: use-after-free in megasas_free_cmds_fusion+0x2d0/0x1294 [megaraid_sas]
...
[ 4152.756117][ T21] Call trace:
...
[ 4152.795950][ T21] megasas_free_cmds_fusion+0x2d0/0x1294 [megaraid_sas]
[ 4152.803329][ T21] megasas_alloc_cmds_fusion+0x4de0/0x53bc [megaraid_sas]
[ 4152.810887][ T21] megasas_init_adapter_fusion+0xab4/0x30dc [megaraid_sas]
[ 4152.818539][ T21] megasas_init_fw+0x4874/0x925c [megaraid_sas]
[ 4152.825245][ T21] megasas_probe_one+0x978/0x2dd4 [megaraid_sas]
...
[ 4152.864486][ T21] Allocated by task 21:
...
[ 4152.883339][ T21] megasas_alloc_cmds_fusion+0x2a40/0x53bc [megaraid_sas]
[ 4152.890796][ T21] megasas_init_adapter_fusion+0xab4/0x30dc [megaraid_sas]
[ 4152.898331][ T21] megasas_init_fw+0x4874/0x925c [megaraid_sas]
[ 4152.904901][ T21] megasas_probe_one+0x978/0x2dd4 [megaraid_sas]
...
[ 4152.943038][ T21] Freed by task 21:
...
[ 4152.972830][ T21] kfree+0x100/0x374
[ 4152.976998][ T21] megasas_alloc_cmds_fusion+0x45c4/0x53bc [megaraid_sas]
[ 4152.984379][ T21] megasas_init_adapter_fusion+0xab4/0x30dc [megaraid_sas]
[ 4152.991856][ T21] megasas_init_fw+0x4874/0x925c [megaraid_sas]
[ 4152.998384][ T21] megasas_probe_one+0x978/0x2dd4 [megaraid_sas]
Fixes: 70c54e210ee9a ("scsi: megaraid_sas: fix memleak in megasas_alloc_cmdlist_fusion")
Reported-by: TOTE Robot <oslab@tsinghua.edu.cn>
Signed-off-by: Zixuan Fu <r33s3n6@gmail.com>
---
drivers/scsi/megaraid/megaraid_sas_fusion.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/drivers/scsi/megaraid/megaraid_sas_fusion.c b/drivers/scsi/megaraid/megaraid_sas_fusion.c
index 09c5fe37754c..238103e7a028 100644
--- a/drivers/scsi/megaraid/megaraid_sas_fusion.c
+++ b/drivers/scsi/megaraid/megaraid_sas_fusion.c
@@ -613,6 +613,7 @@ megasas_alloc_cmdlist_fusion(struct megasas_instance *instance)
for (j = 0; j < i; j++)
kfree(fusion->cmd_list[j]);
kfree(fusion->cmd_list);
+ fusion->cmd_list = NULL;
dev_err(&instance->pdev->dev,
"Failed from %s %d\n", __func__, __LINE__);
return -ENOMEM;
--
2.25.1
reply other threads:[~2022-09-01 12:54 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220901125418.2323348-1-r33s3n6@gmail.com \
--to=r33s3n6@gmail.com \
--cc=baijiaju1990@gmail.com \
--cc=jejb@linux.ibm.com \
--cc=kashyap.desai@broadcom.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-scsi@vger.kernel.org \
--cc=martin.petersen@oracle.com \
--cc=megaraidlinux.pdl@broadcom.com \
--cc=oslab@tsinghua.edu.cn \
--cc=shivasharan.srikanteshwara@broadcom.com \
--cc=sumit.saxena@broadcom.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®