All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] scsi: leapraid: bound the phy index used to update card links
@ 2026-12-04  6:29 Hongyu Guo
  2026-12-04  7:00 ` sashiko-bot
  2026-12-04  7:29 ` Hongyu Guo
  0 siblings, 2 replies; 4+ messages in thread
From: Hongyu Guo @ 2026-12-04  6:29 UTC (permalink / raw)
  To: linux-scsi
  Cc: Dongdong Hao, James E.J. Bottomley, Martin K. Petersen, stable,
	13057616121

SAS device page 0 provides phy_num, which leapraid passes straight
into leapraid_transport_update_links() as the index into
topo_node->card_phy[]. That array holds phys_num entries from the
IO-unit or expander page, and nothing compares the two. A controller
that reports a larger phy_num in the device page than the parent
topology node has makes the driver write past the card_phy[] array:
attached_hdl and the fields filled by
leapraid_transport_set_identify() land in unrelated slab memory.

Reject the update when the index does not fit the target topology
node.

Fixes: 5597088c9e79 ("scsi: leapraid: Add new SCSI driver")
Cc: stable@vger.kernel.org
Cc: Dongdong Hao <doubled@leap-io-kernel.com>
Signed-off-by: Hongyu Guo <guohongyu@cmss.chinamobile.com>
---
 drivers/scsi/leapraid/leapraid_transport.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/drivers/scsi/leapraid/leapraid_transport.c b/drivers/scsi/leapraid/leapraid_transport.c
index 77ffbd6ddce4..7468276b4a10 100644
--- a/drivers/scsi/leapraid/leapraid_transport.c
+++ b/drivers/scsi/leapraid/leapraid_transport.c
@@ -1146,6 +1146,15 @@ void leapraid_transport_update_links(
 		return;
 	}
 
+	if (phy_index >= topo_node->phys_num) {
+		spin_unlock_irqrestore(&adapter->dev_topo.topo_node_lock,
+				       flags);
+		dev_warn(&adapter->pdev->dev,
+			 "%s: phy index %u out of range, max %u\n",
+			 __func__, phy_index, topo_node->phys_num);
+		return;
+	}
+
 	card_phy = &topo_node->card_phy[phy_index];
 	card_phy->attached_hdl = hdl;
 	spin_unlock_irqrestore(&adapter->dev_topo.topo_node_lock, flags);
-- 
2.43.0




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

* Re: [PATCH] scsi: leapraid: bound the phy index used to update card links
  2026-12-04  6:29 [PATCH] scsi: leapraid: bound the phy index used to update card links Hongyu Guo
@ 2026-12-04  7:00 ` sashiko-bot
  2026-12-04  7:29 ` Hongyu Guo
  1 sibling, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-12-04  7:00 UTC (permalink / raw)
  To: Hongyu Guo, linux-scsi

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] scsi: leapraid: leapraid_transport_update_links() reads
  topo_node after unlock
--

commit 29d15e789a79f9b363f1bebb028e93bbca890708
Author: Hongyu Guo <guohongyu@cmss.chinamobile.com>

scsi: leapraid: bound the phy index used to update card links

This commit adds a bounds check on phy_index against topo_node->phys_num
in leapraid_transport_update_links() before indexing into
topo_node->card_phy[], logging a warning and returning early if it is
out of range.

> diff --git a/drivers/scsi/leapraid/leapraid_transport.c b/drivers/scsi/leapraid/leapraid_transport.c
> index 77ffbd6ddce4c..7468276b4a102 100644
> --- a/drivers/scsi/leapraid/leapraid_transport.c
> +++ b/drivers/scsi/leapraid/leapraid_transport.c
> @@ -1146,6 +1146,15 @@ void leapraid_transport_update_links(
>  		return;
>  	}
>  
> +	if (phy_index >= topo_node->phys_num) {
> +		spin_unlock_irqrestore(&adapter->dev_topo.topo_node_lock,
> +				       flags);
> +		dev_warn(&adapter->pdev->dev,
> +			 "%s: phy index %u out of range, max %u\n",
> +			 __func__, phy_index, topo_node->phys_num);
> +		return;
> +	}

[Severity: Medium]
Can reading topo_node->phys_num in dev_warn() trigger a use-after-free?

struct leapraid_topo_node has no reference count or RCU protection, and
leapraid_exp_node_rm() frees expander nodes right after unlinking them
under topo_node_lock.

Once LEAPRAID_EVT_SCAN_DEV_DONE clears scan_start, fw_evt_thread can run
leapraid_transport_update_links() while the SCSI scan thread removes an
expander via leapraid_probe_sas() -> ... -> leapraid_exp_node_rm():

CPU0 (fw_evt_thread)               CPU1 (SCSI scan)
leapraid_transport_update_links()
  spin_lock_irqsave(...)
  topo_node = ...
  if (phy_index >= topo_node->phys_num)
    spin_unlock_irqrestore(...)
                                   leapraid_probe_sas()
                                     ...
                                     leapraid_exp_node_rm()
                                       spin_lock_irqsave(...)
                                       list_del(&topo_node_exp->list)
                                       spin_unlock_irqrestore(...)
                                       kfree(topo_node_exp)
    dev_warn(..., topo_node->phys_num)  <-- use-after-free

Saving topo_node->phys_num into a local variable before releasing
topo_node_lock avoids dereferencing topo_node after the unlock.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204062938.344219-1-guohongyu@cmss.chinamobile.com?part=1

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

* [PATCH] scsi: leapraid: bound the phy index used to update card links
@ 2026-12-04  7:29 ` Hongyu Guo
  2026-12-04  7:31   ` guohongyu
  0 siblings, 1 reply; 4+ messages in thread
From: Hongyu Guo @ 2026-12-04  7:29 UTC (permalink / raw)
  To: linux-scsi
  Cc: Dongdong Hao, James E.J. Bottomley, Martin K. Petersen, stable,
	13057616121

SAS device page 0 provides phy_num, which leapraid passes straight
into leapraid_transport_update_links() as the index into
topo_node->card_phy[]. That array holds phys_num entries from the
IO-unit or expander page, and nothing compares the two. A controller
that reports a larger phy_num in the device page than the parent
topology node has makes the driver write past the card_phy[] array:
attached_hdl and the fields filled by
leapraid_transport_set_identify() land in unrelated slab memory.

Reject the update when the index does not fit the target topology
node.

Fixes: 5597088c9e79 ("scsi: leapraid: Add new SCSI driver")
Cc: stable@vger.kernel.org
Cc: Dongdong Hao <doubled@leap-io-kernel.com>
Signed-off-by: Hongyu Guo <guohongyu@cmss.chinamobile.com>
---
 drivers/scsi/leapraid/leapraid_transport.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/drivers/scsi/leapraid/leapraid_transport.c b/drivers/scsi/leapraid/leapraid_transport.c
index 77ffbd6ddce4..d480b73c6446 100644
--- a/drivers/scsi/leapraid/leapraid_transport.c
+++ b/drivers/scsi/leapraid/leapraid_transport.c
@@ -1146,6 +1146,15 @@ void leapraid_transport_update_links(
 		return;
 	}
 
+	if (phy_index >= topo_node->phys_num) {
+		dev_warn(&adapter->pdev->dev,
+			 "%s: phy index %u out of range, max %u\n",
+			 __func__, phy_index, topo_node->phys_num);
+		spin_unlock_irqrestore(&adapter->dev_topo.topo_node_lock,
+				       flags);
+		return;
+	}
+
 	card_phy = &topo_node->card_phy[phy_index];
 	card_phy->attached_hdl = hdl;
 	spin_unlock_irqrestore(&adapter->dev_topo.topo_node_lock, flags);
-- 
2.43.0




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

* Re: [PATCH] scsi: leapraid: bound the phy index used to update card links
  2026-12-04  7:29 ` Hongyu Guo
@ 2026-12-04  7:31   ` guohongyu
  0 siblings, 0 replies; 4+ messages in thread
From: guohongyu @ 2026-12-04  7:31 UTC (permalink / raw)
  To: linux-scsi
  Cc: Dongdong Hao, James E.J. Bottomley, Martin K. Petersen,
	13057616121

This is v2 of the patch I sent earlier today
(<20261204062938.344219-1-guohongyu@cmss.chinamobile.com>). The only
change: the out-of-range dev_warn() now reads topo_node->phys_num
while topo_node_lock is still held. Sashiko's review of v1 pointed
out that leapraid_exp_node_rm() frees an expander node without
refcounting after unlinking it, so reading topo_node after unlock
would be a use-after-free.

Please use this version; the earlier submission can be dropped.

Hongyu Guo



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

end of thread, other threads:[~2026-12-04  7:38 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04  6:29 [PATCH] scsi: leapraid: bound the phy index used to update card links Hongyu Guo
2026-12-04  7:00 ` sashiko-bot
2026-12-04  7:29 ` Hongyu Guo
2026-12-04  7:31   ` guohongyu

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.