Re: [PATCH net-next v2] net: sparx5: check DT port reg against chip port count
From: Daniel Machon
Date: Tue Nov 17 2026 - 07:59:39 EST
> mchp_sparx5_probe() reads the DT ethernet-port 'reg' property into
> portno, which sparx5_create_port() later uses to index the fixed-size
> sparx5->ports[] array:
>
> sparx5->ports[config->portno] = spx5_port;
>
> The value is never validated, so an out-of-range 'reg' (runtime overlay,
> corrupted or attacker-controlled DTB, kexec-supplied or bootloader
> fixed-up DT) turns probe into an out-of-bounds write.
>
> Skip port nodes with an out-of-range 'reg', as lan966x does in
> lan966x_probe_port(). The bound is the per-chip port count from the
Actually, lan966x returns -EINVAL and fails. This patch just skips the port,
which is fine.
> match data (sparx5->data->consts->n_ports), so lan969x, which shares
> this probe path and has only 30 ports, is bounded correctly as well.
>
> Found by a scripted scan of of_property_read_u32() call sites used to
> index fixed-size arrays, followed by manual review. Verified at runtime
> on a pristine v7.4-rc2 arm64 QEMU virt machine (inline KASAN,
> CONFIG_SPARX5_SWITCH=m): an out-of-tree module supplies only a
> malicious DT overlay, the unmodified in-tree driver performs the store.
> reg = 15839 yields a KASAN use-after-free, reg = 0x20000000 a wild
> write 4 GiB out (paging oops), and reg = 65 silent in-struct corruption
> with a later NULL oops. With this patch all three are skipped. The PoC
> and full logs are posted in the thread. Not tested on real hardware,
> QEMU virt (cortex-a57) and compile testing only.
>
> Fixes: f3cad2611a77 ("net: sparx5: add hostmode with phylink support")
> Signed-off-by: Haoran Wang <wanghr2026@xxxxxxxxxxxxxxxxxx>
The in-body From: line from v1 is gone, so the author is now
haoranwangsec@xxxxxxxxx, which does not match the Signed-off-by.
checkpatch warns about this.
> ---
> v1 -> v2:
>
> - Check against sparx5->data->consts->n_ports instead of SPX5_PORTS,
> so that lan969x (30 ports), which goes through the same probe, is
> bounded correctly as well (Daniel Machon).
> - Document how the issue was found, the runtime verification and the
> test setup, as requested by the patch bot.
>
> drivers/net/ethernet/microchip/sparx5/sparx5_main.c | 5 +++++
> 1 file changed, 5 insertions(+)
>
> diff --git a/a/drivers/net/ethernet/microchip/sparx5/sparx5_main.c b/b/drivers/net/ethernet/microchip/sparx5/sparx5_main.c
> index 08a398a..1647067 100644
> --- a/a/drivers/net/ethernet/microchip/sparx5/sparx5_main.c
> +++ b/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 >= 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;
The fix looks correct, however the patch is broken and does not apply.
pw-bot: cr