[PATCH] niu: validate num_ports against NIU_MAX_PORTS
From: Yang Zi
Date: Tue Aug 25 2026 - 05:44:12 EST
niu_get_and_validate_port() obtains parent->num_ports from hardware
(nr64(ESPC_NUM_PORTS_MACS) & ESPC_NUM_PORTS_MACS_VAL), an 8-bit value that
can range from 0 to 255, with no upper-bound check. The driver's per-port
arrays (rxchan_per_port[], txchan_per_port[], rdc_group_cfg[]) are only
NIU_MAX_PORTS (4) entries, so niu_divide_channels() and
niu_divide_rdc_groups() iterate num_ports times and write out of bounds
whenever num_ports is greater than 4.
Reject num_ports > NIU_MAX_PORTS with -EINVAL so the probe aborts before any
of these arrays are written.
Covers fuzzing bug IDs 321, 322, 323 and 324 (same root cause).
Signed-off-by: Yang Zi <2959243019@xxxxxx>
---
diff --git a/drivers/net/ethernet/sun/niu.c b/drivers/net/ethernet/sun/niu.c
index 54dd7281191d..d33c87489bd9 100644
--- a/drivers/net/ethernet/sun/niu.c
+++ b/drivers/net/ethernet/sun/niu.c
@@ -8586,6 +8586,14 @@ static int niu_get_and_validate_port(struct niu *np)
}
}
+ /* num_ports may come from an 8-bit hardware field (ESPC_NUM_PORTS_MACS),
+ * so validate it against the fixed per-port array sizes (NIU_MAX_PORTS)
+ * to avoid out-of-bounds accesses in niu_divide_channels() and
+ * niu_divide_rdc_groups().
+ */
+ if (parent->num_ports > NIU_MAX_PORTS)
+ return -EINVAL;
+
if (np->port >= parent->num_ports)
return -ENODEV;