Re: [PATCH net-next] net: sparx5: check DT port reg against array size

From
Daniel Machon <daniel.machon@microchip.com>
Date
2026-11-15 22:13:50
Also in
linux-arm-kernel, lkml
Reply to
Haoran Wang, 2026-11-13
Hi,
From: Haoran Wang <redacted>
Date: Fri, 13 Nov 2026 19:42:25 +0800
Subject: [PATCH net-next] net: sparx5: check DT port reg against array size

mchp_sparx5_probe() reads the 'reg' property of each ethernet-port DT
node into portno and passes it to sparx5_create_port(), which uses it
to index the fixed-size sparx5->ports[SPX5_PORTS] array:

    sparx5->ports[config->portno] = spx5_port;

The value is never validated, so a device tree with an out-of-range
'reg' value (runtime overlay, corrupted or attacker-controlled DTB,
kexec-supplied or bootloader-fixed-up DT) turns probe into an
out-of-bounds write, potentially far outside the allocation.
I seem to remember that these type of checks have been rejected with "device
tree is considered trusted". Though, we do the same check in lan966x, so I am
not entirely sure.
quoted hunk ↗ jump to hunk
Add a range check while parsing and skip nodes with an invalid port
number.

Fixes: f3cad2611a77 ("net: sparx5: add hostmode with phylink support")
Signed-off-by: Haoran Wang <redacted>
---
 drivers/net/ethernet/microchip/sparx5/sparx5_main.c | 5 +++++
 1 file changed, 5 insertions(+)
diff --git a/drivers/net/ethernet/microchip/sparx5/sparx5_main.c b/drivers/net/ethernet/microchip/sparx5/sparx5_main.c
index 08a398adf121..6616e76bd0e3 100644
--- a/drivers/net/ethernet/microchip/sparx5/sparx5_main.c
+++ b/drivers/net/ethernet/microchip/sparx5/sparx5_main.c
@@ -845,6 +845,11 @@ static int mchp_sparx5_probe(struct platform_device *pdev)
 			dev_err(sparx5->dev, "port reg property error\n");
 			continue;
 		}
+		if (portno >= SPX5_PORTS) {
This wont do.

SPX5_PORTS is the size of ports[], which matches the sparx5 port count.
lan969x goes through the same probe but has only 30 ports.

Instead do:

        if (portno >= sparx5->data->consts->n_ports)

+			dev_err(sparx5->dev, "port %u: reg out of range\n",
+				portno);
+			continue;
+		}
 		config = &configs[idx];
 		conf = &config->conf;
 		conf->speed = SPEED_UNKNOWN;
--
2.43.0
Since a v2 is needed anyway, please also add what the bot asked for to the
commit message, and properly populate To and CC with get_maintainers.pl

Thanks,
Daniel

pw-bot: cr
Thread (4 messages) 4 messages, 3 authors, last reply 3h ago
Keyboard shortcuts
hback out one level
jnext message in thread / next series-diff position
kprevious message in thread / previous series-diff position
ldrill in
Escclose help / fold thread tree
?toggle this help