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 v2 03/37] scsi: mpt3sas: Pin Scsi_Host while an ioctl is using its ioc
Date: Wed, 18 Nov 2026 10:41:26 +0000	[thread overview]
Message-ID: <sashiko-outbox-220732@kernel.org> (raw)
In-Reply-To: <1794995806-5835-4-git-send-email-shivasharan.srikanteshwara@broadcom.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] scsi: mpt3sas: premature publication of ioc on
  mpt3sas_ioc_list in _scsih_probe()
- [Medium] scsi: mpt3sas: scsi_host_get() in _ctl_verify_adapter() does
  not block teardown
- [Low] scsi: mpt3sas: missing Fixes trailer on bug-fix commit
--

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

> scsi: mpt3sas: Pin Scsi_Host while an ioctl is using its ioc
>
> Take a Scsi_Host reference in _ctl_verify_adapter() and release it
> once the ioctl is done, so the adapter can't be torn down while an
> ioctl is in flight.
>
> Signed-off-by: Shivasharan S <shivasharan.srikanteshwara@broadcom.com>
> Signed-off-by: Ranjan Kumar <ranjan.kumar@broadcom.com>

[Severity: Low]
This is a bug fix for the unreferenced ioc lookup in the ioctl path.  Should
it include a Fixes tag?

  Fixes: f92363d12359 ("[SCSI] mpt3sas: add new driver supporting 12GB SAS")

> diff --git a/drivers/scsi/mpt3sas/mpt3sas_ctl.c b/drivers/scsi/mpt3sas/mpt3sas_ctl.c
> index 8bb947004885b..f4eeef572f6ec 100644
> --- a/drivers/scsi/mpt3sas/mpt3sas_ctl.c
> +++ b/drivers/scsi/mpt3sas/mpt3sas_ctl.c
> @@ -447,6 +447,11 @@ _ctl_verify_adapter(int ioc_number, struct MPT3SAS_ADAPTER **iocpp,
>  				continue;
>  		}
>  out:
> +		if (!ioc->shost || !scsi_host_get(ioc->shost)) {
> +			spin_unlock(&gioc_lock);
> +			*iocpp = NULL;
> +			return -1;
> +		}
>  		spin_unlock(&gioc_lock);
>  		*iocpp = ioc;
>  		return ioc_number;

[Severity: Medium]
Does scsi_host_get(ioc->shost) prevent scsih_remove() from tearing down
shost->tag_set while an ioctl is already in flight?

scsi_host_get() pins the Scsi_Host memory allocation via shost->shost_gendev
and blocks new lookups once scsi_remove_host() sets SHOST_DEL, but it does
not take shost->tagset_refcnt or block scsih_remove().  If an ioctl acquires
ioc->pci_access_mutex and passes the ioc->remove_host check before
scsih_remove() sets ioc->remove_host = 1, scsih_remove() proceeds without
acquiring ioc->pci_access_mutex until mpt3sas_base_detach() ->
mpt3sas_base_free_resources():

CPU0                                  CPU1
_ctl_ioctl_main()
  _ctl_verify_adapter()
    scsi_host_get(ioc->shost)
  mutex_lock(&ioc->pci_access_mutex)
  if (ioc->remove_host) // false
                                      scsih_remove()
                                        ioc->remove_host = 1;
                                        ...
                                        sas_remove_host(shost)
                                          scsi_remove_host(shost)
                                            ...
                                            blk_mq_free_tag_set(&shost->tag_set)
  _ctl_do_reset()
    mpt3sas_base_hard_reset_handler()
      mpt3sas_wait_for_commands_to_complete()
        scsi_host_busy(ioc->shost)
          blk_mq_tagset_busy_iter()   <-- use-after-free / NULL dereference
                                        mpt3sas_base_detach(ioc)
                                          mpt3sas_base_free_resources(ioc)
                                            mutex_lock(&ioc->pci_access_mutex)

Acquiring ioc->pci_access_mutex around setting ioc->remove_host = 1 in
scsih_remove(), or synchronizing with in-flight ioctls before
sas_remove_host(), would prevent teardown from racing with an active ioctl.

[ ... ]

> diff --git a/drivers/scsi/mpt3sas/mpt3sas_scsih.c b/drivers/scsi/mpt3sas/mpt3sas_scsih.c
> index a77bbc3d2b666..b30a215141c35 100644
> --- a/drivers/scsi/mpt3sas/mpt3sas_scsih.c
> +++ b/drivers/scsi/mpt3sas/mpt3sas_scsih.c
> @@ -13496,11 +13496,12 @@ _scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
>  		return -ENODEV;
>  	}
>  
> +	/* shost must be valid before the ioc becomes visible to ioctls */
> +	ioc->shost = shost;
>  	INIT_LIST_HEAD(&ioc->list);
>  	spin_lock(&gioc_lock);
>  	list_add_tail(&ioc->list, &mpt3sas_ioc_list);
>  	spin_unlock(&gioc_lock);
> -	ioc->shost = shost;
>  	ioc->pdev = pdev;
>  	ioc->scsi_io_cb_idx = scsi_io_cb_idx;
>  	ioc->tm_cb_idx = tm_cb_idx;

[Severity: Medium]
Can an ioctl on /dev/mpt3ctl or /dev/mpt2ctl access ioc before _scsih_probe()
has initialized its mutexes and fields?

In _mpt3sas_init(), mpt3sas_ctl_init() registers the misc devices before
pci_register_driver() runs, and the device nodes remain active across hotplug
events.  Because scsi_host_alloc() sets shost->shost_state to SHOST_CREATED,
scsi_host_get(ioc->shost) in _ctl_verify_adapter() succeeds as soon as ioc is
added to mpt3sas_ioc_list:

CPU0                                  CPU1
_scsih_probe()
  ioc->shost = shost;
  list_add_tail(&ioc->list,
                &mpt3sas_ioc_list);
                                      _ctl_ioctl_main()
                                        _ctl_verify_adapter()
                                          scsi_host_get(ioc->shost)
                                        mutex_lock(&ioc->pci_access_mutex)
                                          <-- uninitialized mutex
                                        if (ioc->is_driver_loading || ...)
                                          // still 0
                                        mutex_lock(&ioc->ctl_cmds.mutex)
                                          <-- uninitialized mutex
  ioc->pdev = pdev;
  ...
  mutex_init(&ioc->pci_access_mutex);
  ...
  ioc->is_driver_loading = 1;
  mpt3sas_base_attach(ioc);
    mutex_init(&ioc->ctl_cmds.mutex);

At the point list_add_tail() publishes ioc, ioc->pdev is still NULL,
ioc->pci_access_mutex has not been initialized, ioc->is_driver_loading is
still 0 (until after alloc_ordered_workqueue()), and ioc->ctl_cmds.mutex is
not initialized until mpt3sas_base_attach().  Adding ioc to mpt3sas_ioc_list
only after ioc->pci_access_mutex is initialized and ioc->is_driver_loading is
set to 1 (or after mpt3sas_base_attach() succeeds) would close this window.

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

  reply	other threads:[~2026-11-18 10:41 UTC|newest]

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

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-220732@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