From: sashiko-bot@kernel.org
To: "Chandrakanth Patil" <chandrakanth.patil@broadcom.com>
Cc: linux-scsi@vger.kernel.org
Subject: Re: [PATCH v2 01/17] mpi3mr: Fix buffer overflow in BSG passthrough request copy
Date: Tue, 25 Aug 2026 16:09:15 +0000 [thread overview]
Message-ID: <20260825160915.C81FA1F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260825210411.301535-2-chandrakanth.patil@broadcom.com>
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
Pre-existing issues:
- [High] Missing validation of `job->request_len` allows out-of-bounds slab memory reads when parsing the BSG request and buffer entries.
- [High] On 32-bit architectures, pointer wrap-around allows bypassing bounds checks for specific buffer types, leading to out-of-bounds heap access.
- [High] DMA coherent memory allocated for NVMe PRP lists is leaked on command posting failures or command timeouts.
- [High] Potential Use-After-Free of `stgt_priv` if the SCSI target is removed concurrently during a long-running BSG command.
- [High] Data race and use-after-timeout on `mrioc->bsg_cmds` state between timeout handler and interrupt handler.
--
commit 6909218adbd3af92548ad9194e535cec60aaccf1
Author: Chandrakanth Patil <chandrakanth.patil@broadcom.com>
mpi3mr: Fix buffer overflow in BSG passthrough request copy
The size of an incoming BSG request is checked using a variable that is
narrower than the field it is read from, so large values wrap and pass
the check. The copy that follows then uses the full value and writes
past the request buffer.
Widen the variable and copy only the amount that was checked.
Fixes: 506bc1a0d6ba ("scsi: mpi3mr: Add support for MPT commands")
Signed-off-by: Chandrakanth Patil <chandrakanth.patil@broadcom.com>
> diff --git a/drivers/scsi/mpi3mr/mpi3mr_app.c b/drivers/scsi/mpi3mr/mpi3mr_app.c
> index 1353a8ff9c85d..8e5d24793efd0 100644
> --- a/drivers/scsi/mpi3mr/mpi3mr_app.c
> +++ b/drivers/scsi/mpi3mr/mpi3mr_app.c
> @@ -2384,7 +2384,7 @@ static long mpi3mr_bsg_process_mpt_cmds(struct bsg_job *job)
> long rval = -EINVAL;
> struct mpi3mr_ioc *mrioc = NULL;
> u8 *mpi_req = NULL, *sense_buff_k = NULL;
> - u8 mpi_msg_size = 0;
> + u32 mpi_msg_size = 0;
> struct mpi3mr_bsg_packet *bsg_req = NULL;
> struct mpi3mr_bsg_mptcmd *karg;
> struct mpi3mr_buf_entry *buf_entries = NULL;
[Severity: High]
This is a pre-existing issue, but does this lack validation of
job->request_len when parsing the BSG request and buffer entries?
drivers/scsi/mpi3mr/mpi3mr_app.c:mpi3mr_bsg_process_mpt_cmds() {
...
bsg_req = job->request;
karg = (struct mpi3mr_bsg_mptcmd *)&bsg_req->cmd.mptcmd;
...
bufcnt = karg->buf_entry_list.num_of_entries;
...
buf_entries = karg->buf_entry_list.buf_entry;
...
}
And similarly in mpi3mr_bsg_process_drv_cmds():
drivers/scsi/mpi3mr/mpi3mr_app.c:mpi3mr_bsg_process_drv_cmds() {
...
bsg_req = job->request;
drvrcmd = &bsg_req->cmd.drvrcmd;
mrioc = mpi3mr_bsg_verify_adapter(drvrcmd->mrioc_id);
...
}
If a user intentionally specifies a request_len smaller than the size of
struct mpi3mr_bsg_packet, could this result in an out-of-bounds slab memory
read since the driver casts and accesses the buffer without checking the
length?
> @@ -2538,7 +2538,7 @@ static long mpi3mr_bsg_process_mpt_cmds(struct bsg_job *job)
> rval = -EINVAL;
> goto out;
> }
> - memcpy(mpi_req, sgl_iter, buf_entries->buf_len);
> + memcpy(mpi_req, sgl_iter, mpi_msg_size);
> break;
> default:
> invalid_be = 1;
[Severity: High]
This is a pre-existing issue, but does this pointer arithmetic bypass
bounds checks on 32-bit architectures?
drivers/scsi/mpi3mr/mpi3mr_app.c:mpi3mr_bsg_process_mpt_cmds() {
...
case MPI3MR_BSG_BUFTYPE_MPI_REPLY:
sgl_iter = sgl_din_iter;
sgl_din_iter += buf_entries->buf_len;
...
}
If a user submits a massive buf_len (e.g., 0xFFFFFFFF), could the addition
to sgl_din_iter (which is a u8 *) cause an integer wrap-around on 32-bit
systems?
Since a wrapped pointer is mathematically smaller, would it bypass the
safety check:
if (sgl_din_iter > (din_buf + job->reply_payload.payload_len))
And allow an out-of-bounds heap write when the driver copies the reply:
memcpy(drv_buf_iter->bsg_buf, bsg_reply_buf, tmplen);
[Severity: High]
This is a pre-existing issue, but does this code leak DMA coherent memory
on command posting failures or timeouts?
drivers/scsi/mpi3mr/mpi3mr_app.c:mpi3mr_build_nvme_prp() {
...
mrioc->prp_list_virt = dma_alloc_coherent(&mrioc->pdev->dev,
dev_pgsz, &mrioc->prp_list_dma, GFP_KERNEL);
...
}
drivers/scsi/mpi3mr/mpi3mr_app.c:mpi3mr_bsg_process_mpt_cmds() {
...
if (rval) {
mrioc->bsg_cmds.is_waiting = 0;
dprint_bsg_err(mrioc,
"%s: posting bsg request is failed\n", __func__);
rval = -EAGAIN;
if (block_io && stgt_priv)
atomic_dec(&stgt_priv->block_io);
goto out_unlock;
}
...
}
If mpi3mr_admin_request_post() fails or if the command times out, the code
jumps to out_unlock. Does out_unlock fail to free mrioc->prp_list_virt,
leading to a permanent leak of DMA memory?
[Severity: High]
This is a pre-existing issue, but is there a potential Use-After-Free of
stgt_priv if the SCSI target is removed concurrently?
drivers/scsi/mpi3mr/mpi3mr_app.c:mpi3mr_bsg_process_mpt_cmds() {
...
if (tgtdev) {
if (tgtdev->starget && tgtdev->starget->hostdata) {
stgt_priv = (struct mpi3mr_stgt_priv_data *)
tgtdev->starget->hostdata;
atomic_inc(&stgt_priv->block_io);
}
mpi3mr_tgtdev_put(tgtdev);
}
...
if (block_io && stgt_priv)
atomic_dec(&stgt_priv->block_io);
...
}
The reference to the target device is dropped via mpi3mr_tgtdev_put(tgtdev)
before the long wait. If the physical SCSI device is hot-unplugged during
the wait for command completion, could the SCSI midlayer free the starget
and stgt_priv before atomic_dec() is called?
[Severity: High]
This is a pre-existing issue, but is there a data race on mrioc->bsg_cmds
state between the timeout handler and the interrupt handler?
drivers/scsi/mpi3mr/mpi3mr_app.c:mpi3mr_bsg_process_mpt_cmds() {
...
if (!(mrioc->bsg_cmds.state & MPI3MR_CMD_COMPLETE)) {
mrioc->bsg_cmds.is_waiting = 0;
rval = -EAGAIN;
...
}
And in the interrupt handler:
drivers/scsi/mpi3mr/mpi3mr_fw.c:mpi3mr_process_admin_reply_desc() {
...
cmdptr->state |= MPI3MR_CMD_COMPLETE;
...
cmdptr->state &= ~MPI3MR_CMD_PENDING;
...
if (cmdptr->is_waiting) {
cmdptr->is_waiting = 0;
complete(&cmdptr->done);
}
...
}
If a BSG command times out, the timeout handler locklessly sets
is_waiting = 0 and aborts, allowing another thread to acquire the lock
and submit a new command using the same structure.
If a delayed hardware interrupt for the original command fires while the
new command is pending, could the non-atomic modifications to cmdptr->state
in mpi3mr_process_admin_reply_desc corrupt the state of the new command
and prematurely complete it?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260825210411.301535-1-chandrakanth.patil@broadcom.com?part=1
next prev parent reply other threads:[~2026-08-25 16:09 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-25 21:03 [PATCH v2 00/17] scsi: mpi3mr: Fix out-of-bounds accesses and reference leaks Chandrakanth Patil
2026-08-25 21:03 ` [PATCH v2 01/17] mpi3mr: Fix buffer overflow in BSG passthrough request copy Chandrakanth Patil
2026-08-25 16:09 ` sashiko-bot [this message]
2026-08-25 21:03 ` [PATCH v2 02/17] mpi3mr: Fix out-of-bounds read when copying BSG MPI requests Chandrakanth Patil
2026-08-25 16:06 ` sashiko-bot
2026-08-25 21:03 ` [PATCH v2 03/17] mpi3mr: Fix I/O block counter leak on admin request post failure Chandrakanth Patil
2026-08-25 16:06 ` sashiko-bot
2026-08-25 21:03 ` [PATCH v2 04/17] mpi3mr: Fix target device reference leak in BSG task management Chandrakanth Patil
2026-08-25 16:05 ` sashiko-bot
2026-08-25 21:03 ` [PATCH v2 05/17] mpi3mr: Fix buffer overflow when caching log data Chandrakanth Patil
2026-08-25 16:12 ` sashiko-bot
2026-08-25 21:04 ` [PATCH v2 06/17] mpi3mr: Fix out-of-bounds reply frame access Chandrakanth Patil
2026-08-25 21:04 ` [PATCH v2 07/17] mpi3mr: Fix out-of-bounds sense buffer access Chandrakanth Patil
2026-08-25 16:08 ` sashiko-bot
2026-08-25 21:04 ` [PATCH v2 08/17] mpi3mr: Fix out-of-bounds bitmap access during device removal Chandrakanth Patil
2026-08-25 16:11 ` sashiko-bot
2026-08-25 21:04 ` [PATCH v2 09/17] mpi3mr: Fix target device reference leak in device removal handshake Chandrakanth Patil
2026-08-25 16:20 ` sashiko-bot
2026-08-25 21:04 ` [PATCH v2 10/17] mpi3mr: Fix out-of-bounds read in SAS topology change events Chandrakanth Patil
2026-08-25 17:15 ` sashiko-bot
2026-08-25 21:04 ` [PATCH v2 11/17] mpi3mr: Fix out-of-bounds read of event data Chandrakanth Patil
2026-08-25 16:06 ` sashiko-bot
2026-08-25 21:04 ` [PATCH v2 12/17] mpi3mr: Fix out-of-bounds phy array access on link change Chandrakanth Patil
2026-08-25 16:18 ` sashiko-bot
2026-08-25 21:04 ` [PATCH v2 13/17] mpi3mr: Fix buffer overflow in the BSG target device map Chandrakanth Patil
2026-08-25 16:07 ` sashiko-bot
2026-08-25 21:04 ` [PATCH v2 14/17] mpi3mr: Fix out-of-bounds read in PCIe topology change events Chandrakanth Patil
2026-08-25 16:10 ` sashiko-bot
2026-08-25 21:04 ` [PATCH v2 15/17] mpi3mr: zero out diagnostic buffer status memory Chandrakanth Patil
2026-08-25 21:04 ` [PATCH v2 16/17] mpi3mr: Fix use-after-free of the firmware event workqueue Chandrakanth Patil
2026-08-25 16:19 ` sashiko-bot
2026-08-25 21:04 ` [PATCH v2 17/17] mpi3mr: Fix NULL pointer dereference on PCI error recovery Chandrakanth Patil
2026-08-25 16:20 ` sashiko-bot
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=20260825160915.C81FA1F000E9@smtp.kernel.org \
--to=sashiko-bot@kernel.org \
--cc=chandrakanth.patil@broadcom.com \
--cc=linux-scsi@vger.kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
/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