[PATCH] scsi: libsas: Fix potential array out of bounds in sas_check_*_expander_topo().
From: Chaohai Chen
Date: Mon Dec 08 2025 - 02:36:04 EST
No check was made to ensure that parent_phy->appended_phy_id is within the
valid range. If the expander reports an invalid phy_id, it may cause the
array to be out of bounds.
Signed-off-by: Chaohai Chen <wdhh6@xxxxxxxxxx>
---
drivers/scsi/libsas/sas_expander.c | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/drivers/scsi/libsas/sas_expander.c b/drivers/scsi/libsas/sas_expander.c
index d953225f6cc2..07633d795182 100644
--- a/drivers/scsi/libsas/sas_expander.c
+++ b/drivers/scsi/libsas/sas_expander.c
@@ -1266,6 +1266,11 @@ static int sas_check_edge_expander_topo(struct domain_device *child,
struct expander_device *parent_ex = &child->parent->ex_dev;
struct ex_phy *child_phy;
+ if (parent_phy->attached_phy_id >= child_ex->num_phys) {
+ pr_err("Invalid attached_phy_id:%u, num_phys:%u\n",
+ parent_phy->attached_phy_id, child_ex->num_phys);
+ return -EINVAL;
+ }
child_phy = &child_ex->ex_phy[parent_phy->attached_phy_id];
if (child->dev_type == SAS_FANOUT_EXPANDER_DEVICE) {
@@ -1296,6 +1301,11 @@ static int sas_check_fanout_expander_topo(struct domain_device *child,
struct expander_device *child_ex = &child->ex_dev;
struct ex_phy *child_phy;
+ if (parent_phy->attached_phy_id >= child_ex->num_phys) {
+ pr_err("Invalid attached_phy_id:%u, num_phys:%u\n",
+ parent_phy->attached_phy_id, child_ex->num_phys);
+ return -EINVAL;
+ }
child_phy = &child_ex->ex_phy[parent_phy->attached_phy_id];
if (parent_phy->routing_attr == TABLE_ROUTING &&
--
2.43.7