Linux SCSI subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Shivasharan S" <shivasharan.srikanteshwara@broadcom.com>,
	linux-scsi@vger.kernel.org
Subject: Re: [PATCH 26/37] scsi: mpt3sas: Enforce minimum reply frame size and validate chain segment size
Date: Fri, 06 Nov 2026 01:24:39 +0000	[thread overview]
Message-ID: <sashiko-outbox-200512@kernel.org> (raw)
In-Reply-To: <1793925772-1303-27-git-send-email-shivasharan.srikanteshwara@broadcom.com>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] scsi: mpt3sas: minimum reply_sz check in _base_allocate_memory_pools() too small
- [Medium] scsi: mpt3sas: chain_sz bound in _base_allocate_memory_pools() truncates u8
--

commit a651558740938275a089ff4ceac26e9d6bedfb39
Author: Shivasharan S <shivasharan.srikanteshwara@broadcom.com>

scsi: mpt3sas: Enforce minimum reply frame size and validate chain segment size

This commit adds validation in _base_allocate_memory_pools() to enforce a
minimum reply frame size based on MPI2DefaultReply_t and to bounds-check
IOCMaxChainSegmentSize when calculating chain_segment_sz.

> diff --git a/drivers/scsi/mpt3sas/mpt3sas_base.c b/drivers/scsi/mpt3sas/mpt3sas_base.c
> index 3622d8a42725b..1af9dc241666c 100644
> --- a/drivers/scsi/mpt3sas/mpt3sas_base.c
> +++ b/drivers/scsi/mpt3sas/mpt3sas_base.c
> @@ -6395,21 +6395,46 @@ _base_allocate_memory_pools(struct MPT3SAS_ADAPTER *ioc)
>  	/* reply frame size */
>  	ioc->reply_sz = facts->ReplyFrameSize * 4;
>  
> +	/* Enforce minimum reply frame size */
> +	if (ioc->reply_sz < sizeof(MPI2DefaultReply_t)) {
> +		ioc_err(ioc,
> +			"Firmware reported an invalid ReplyFrameSize (%d bytes). Cannot safely attach.\n",
> +			ioc->reply_sz);
> +		return -EINVAL;
> +	}

[Severity: High]
Is sizeof(MPI2DefaultReply_t) (20 bytes) large enough to prevent out-of-bounds
reads and writes on the reply buffers when ReplyFrameSize is between 5 and 12
dwords (20 to 48 bytes)?

In mpt3sas_base_attach(), the internal command reply buffers and DMA reply
frames are allocated using ioc->reply_sz:

drivers/scsi/mpt3sas/mpt3sas_base.c:mpt3sas_base_attach() {
	...
	ioc->base_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
	ioc->port_enable_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
	ioc->transport_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
	ioc->scsih_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
	ioc->tm_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
	ioc->config_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
	ioc->ctl_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
	...
}

However, command submission and completion paths unconditionally zero, copy,
or dereference larger reply structures from these buffers without checking
ioc->reply_sz. For example, during mpt3sas_base_attach() ->
_base_make_ioc_operational(), _base_display_fwpkg_version() copies 24 bytes
(sizeof(Mpi2FWUploadReply_t)) from ioc->base_cmds.reply:

drivers/scsi/mpt3sas/mpt3sas_base.c:_base_display_fwpkg_version() {
	...
			memcpy(&mpi_reply, ioc->base_cmds.reply,
					sizeof(Mpi2FWUploadReply_t));
	...
}

And _base_static_config_pages() -> _config_request() writes 24 bytes
(sizeof(Mpi2ConfigReply_t)) into ioc->config_cmds.reply:

drivers/scsi/mpt3sas/mpt3sas_config.c:_config_request() {
	...
	memset(ioc->config_cmds.reply, 0, sizeof(Mpi2ConfigReply_t));
	...
}

Other paths access even larger reply structures, such as
mpt3sas_scsih_issue_tm() writing sizeof(Mpi2SCSITaskManagementReply_t) (28
bytes) to ioc->tm_cmds.reply, _transport_smp_handler() copying
sizeof(Mpi2SmpPassthroughReply_t) (28 bytes) from ioc->transport_cmds.reply,
mpt3sas_base_scsi_enclosure_processor() copying sizeof(Mpi2SepReply_t) (32
bytes) from ioc->base_cmds.reply, and _scsi_send_scsi_io(), _scsih_io_done(),
and mpt3sas_ctl_done() casting reply buffers to Mpi2SCSIIOReply_t (52 bytes):

drivers/scsi/mpt3sas/mpt3sas_scsih.c:_scsi_send_scsi_io() {
	...
		mpi_reply = ioc->scsih_cmds.reply;
		transfer_packet->sense_length =
		   min_t(u32, le32_to_cpu(mpi_reply->SenseCount),
			 sizeof(transfer_packet->sense));
	...
}

Should the minimum ioc->reply_sz check require at least enough space for the
largest reply structure accessed by the driver, such as Mpi2SCSIIOReply_t?

> +
> +	/* calculate the max scatter element size */
> +	sge_size = max_t(u16, ioc->sge_size, ioc->sge_size_ieee);
> +
>  	/* chain segment size */
>  	if (ioc->hba_mpi_version_belonged != MPI2_VERSION) {
> -		if (facts->IOCMaxChainSegmentSize)
> -			ioc->chain_segment_sz =
> -					facts->IOCMaxChainSegmentSize *
> +		if (facts->IOCMaxChainSegmentSize) {
> +			/* compute in 32 bits so the product cannot wrap the u16 field */
> +			u32 chain_sz = (u32)facts->IOCMaxChainSegmentSize *
>  					MAX_CHAIN_ELEMT_SZ;
> -		else
> -		/* set to 128 bytes size if IOCMaxChainSegmentSize is zero */
> +
> +			/* reject values that wrap or leave < 2 SGEs per chain */
> +			if (chain_sz < 2 * sge_size || chain_sz > 0xFFFF) {

[Severity: Medium]
Can allowing chain_sz up to 0xFFFF truncate chain_offset when building
multi-chain IEEE scatter-gather lists in _base_build_sg_scmd_ieee()?

If facts->IOCMaxChainSegmentSize is between 257 and 4095, chain_sz is between
4112 and 65520 bytes, which passes this check and sets
ioc->max_sges_in_chain_message to facts->IOCMaxChainSegmentSize - 1 (256 to
4094).

When _base_build_sg_scmd_ieee() constructs an SGL that spans multiple chain
segments (sges_left > ioc->max_sges_in_chain_message), it sets chain_offset to
ioc->max_sges_in_chain_message and passes it to _base_add_sg_single_ieee():

drivers/scsi/mpt3sas/mpt3sas_base.c:_base_build_sg_scmd_ieee() {
	...
		sges_in_segment = (sges_left <=
		    ioc->max_sges_in_chain_message) ? sges_left :
		    ioc->max_sges_in_chain_message;
		chain_offset = (sges_left == sges_in_segment) ?
		    0 : sges_in_segment;
		chain_length = sges_in_segment * ioc->sge_size_ieee;
		if (chain_offset)
			chain_length += ioc->sge_size_ieee;
		_base_add_sg_single_ieee(sg_local, chain_sgl_flags,
		    chain_offset, chain_length, chain_dma);
	...
}

In _base_add_sg_single_ieee(), chain_offset is a u8 parameter written into
Mpi25IeeeSgeChain64_t::NextChainOffset (an 8-bit field in 16-byte SGE units,
with a maximum value of 255):

drivers/scsi/mpt3sas/mpt3sas_base.c:_base_add_sg_single_ieee() {
	Mpi25IeeeSgeChain64_t *sgel = paddr;

	sgel->Flags = flags;
	sgel->NextChainOffset = chain_offset;
	...
}

Should IOCMaxChainSegmentSize (or ioc->max_sges_in_chain_message) be bounded
so that chain_offset fits in the u8 NextChainOffset field?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/1793925772-1303-1-git-send-email-shivasharan.srikanteshwara@broadcom.com?part=26

  reply	other threads:[~2026-11-06  1:24 UTC|newest]

Thread overview: 55+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-06  0:42 [PATCH 00/37] scsi: mpt3sas: Validate firmware/user input and fix error-path bugs Shivasharan S
2026-11-06  0:42 ` [PATCH 01/37] scsi: mpt3sas: Validate firmware-reported IOCRequestFrameSize Shivasharan S
2026-11-06  1:17   ` sashiko-bot
2026-11-06  0:42 ` [PATCH 02/37] scsi: mpt3sas: Validate RAID configuration page length Shivasharan S
2026-11-06  1:15   ` sashiko-bot
2026-11-06  0:42 ` [PATCH 03/37] scsi: mpt3sas: Pin Scsi_Host while an ioctl is using its ioc Shivasharan S
2026-11-06  1:19   ` sashiko-bot
2026-11-06  0:42 ` [PATCH 04/37] scsi: mpt3sas: Fix locking and bounds checks on host_trace_buffer sysfs files Shivasharan S
2026-11-06  1:25   ` sashiko-bot
2026-11-06  0:42 ` [PATCH 05/37] scsi: mpt3sas: Fix unlocked use of raid_device in enable_sdev_max_qd_store Shivasharan S
2026-11-06  0:42 ` [PATCH 06/37] scsi: mpt3sas: Fix invalid kfree() of fw_event_work event_data Shivasharan S
2026-11-06  0:42 ` [PATCH 07/37] scsi: mpt3sas: Validate SMP request/response length in bsg handler Shivasharan S
2026-11-06  1:17   ` sashiko-bot
2026-11-06  0:42 ` [PATCH 08/37] scsi: mpt3sas: Validate firmware/user supplied device handles against MaxDevHandle Shivasharan S
2026-11-06  1:15   ` sashiko-bot
2026-11-06  0:42 ` [PATCH 09/37] scsi: mpt3sas: Clamp topology/IR event entry counts to EventDataLength Shivasharan S
2026-11-06  1:13   ` sashiko-bot
2026-11-06  0:42 ` [PATCH 10/37] scsi: mpt3sas: NULL ioc->reply_post after freeing it Shivasharan S
2026-11-06  0:42 ` [PATCH 11/37] scsi: mpt3sas: Fix port_table memory leak in _scsih_sas_port_refresh Shivasharan S
2026-11-06  0:42 ` [PATCH 12/37] scsi: mpt3sas: Fix leak and dangling pointer for io_uring_poll_queues Shivasharan S
2026-11-06  0:42 ` [PATCH 13/37] scsi: mpt3sas: Validate max_data_size in MPT3EVENTREPORT ioctl Shivasharan S
2026-11-06  0:42 ` [PATCH 14/37] scsi: mpt3sas: Fix buffer overflow in _scsih_scsi_ioc_info Shivasharan S
2026-11-06 11:16   ` kernel test robot
2026-11-06 11:30   ` kernel test robot
2026-11-06  0:42 ` [PATCH 15/37] scsi: mpt3sas: Fix Scsi_Host leak on invalid/tampered device probe Shivasharan S
2026-11-06  0:42 ` [PATCH 16/37] scsi: mpt3sas: Fix page fault from stale ioc->chip after PCI error recovery Shivasharan S
2026-11-06  1:22   ` sashiko-bot
2026-11-06  0:42 ` [PATCH 17/37] scsi: mpt3sas: Clamp ValidEntries in driver trigger page update helpers Shivasharan S
2026-11-06  1:09   ` sashiko-bot
2026-11-06  0:42 ` [PATCH 18/37] scsi: mpt3sas: Clamp firmware reply copies to the allocated reply buffer Shivasharan S
2026-11-06  0:42 ` [PATCH 19/37] scsi: mpt3sas: Reject implausibly small MaxReplyDescriptorPostQueueDepth Shivasharan S
2026-11-06  1:17   ` sashiko-bot
2026-11-06  0:42 ` [PATCH 20/37] scsi: mpt3sas: Validate CurrentHostPageSize before using it as a shift amount Shivasharan S
2026-11-06  1:12   ` sashiko-bot
2026-11-06  0:42 ` [PATCH 21/37] scsi: mpt3sas: Clamp sense_length before copying into transfer_packet->sense Shivasharan S
2026-11-06  0:42 ` [PATCH 22/37] scsi: mpt3sas: Clamp num_luns in _scsih_wait_for_target_to_become_ready Shivasharan S
2026-11-06  0:42 ` [PATCH 23/37] scsi: mpt3sas: Validate phy_number in mpt3sas_transport_update_links Shivasharan S
2026-11-06  1:11   ` sashiko-bot
2026-11-06  0:42 ` [PATCH 24/37] scsi: mpt3sas: Require CAP_SYS_RAWIO for mpt2ctl/mpt3ctl ioctls Shivasharan S
2026-11-06  0:42 ` [PATCH 25/37] scsi: mpt3sas: Fix SGE offset boundary check in _ctl_do_mpt_command Shivasharan S
2026-11-06  0:42 ` [PATCH 26/37] scsi: mpt3sas: Enforce minimum reply frame size and validate chain segment size Shivasharan S
2026-11-06  1:24   ` sashiko-bot [this message]
2026-11-06  0:42 ` [PATCH 27/37] scsi: mpt3sas: Replace panic() with error return in hard_reset_handler Shivasharan S
2026-11-06  0:42 ` [PATCH 28/37] scsi: mpt3sas: Replace panic() calls with error returns in _config_request Shivasharan S
2026-11-06  1:18   ` sashiko-bot
2026-11-06  0:42 ` [PATCH 29/37] scsi: mpt3sas: Bound event-log copy size against reply frame size Shivasharan S
2026-11-06  0:42 ` [PATCH 30/37] scsi: mpt3sas: Add _ctl_diag_buffer_reset() helper to clear diag buffer state Shivasharan S
2026-11-06  0:42 ` [PATCH 31/37] scsi: mpt3sas: Restrict ioc_dump debugfs and replace raw ioc dump with text Shivasharan S
2026-11-06  0:42 ` [PATCH 32/37] scsi: mpt3sas: Fix out-of-bounds bit operation in _scsih_check_topo_delete_events Shivasharan S
2026-11-06  0:42 ` [PATCH 33/37] scsi: mpt3sas: Bound SMP passthrough buffer reads to actual buffer size Shivasharan S
2026-11-06  0:42 ` [PATCH 34/37] scsi: mpt3sas: Release sas_device reference on error in mpt3sas_transport_port_add Shivasharan S
2026-11-06  0:42 ` [PATCH 35/37] scsi: mpt3sas: NULL the event_log pointer after freeing it Shivasharan S
2026-11-06  0:42 ` [PATCH 36/37] scsi: mpt3sas: Bound NVMe passthrough transfer size against PRP buffer capacity Shivasharan S
2026-11-06  0:42 ` [PATCH 37/37] scsi: mpt3sas: Update driver version to 59.100.00.00 Shivasharan S
2026-11-06  1:37   ` 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=sashiko-outbox-200512@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=shivasharan.srikanteshwara@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