fman_set_port_params() uses the port id, which comes from the device tree "cell-index" property, to index fixed-size tables without validating it: set_port_liodn(fman, port_id, fman->liodn_base[port_id], fman->liodn_offset[port_id]); ... mac_id = hw_port_id_to_sw_port_id(fman->state->rev_info.major, port_id); if (port_params->max_frame_length >= fman->state->mac_mfl[mac_id]) { fman->state->port_mfl[mac_id] = port_params->max_frame_length; liodn_base[] and liodn_offset[] have FMAN_LIODN_TBL (64) entries, and set_port_liodn() also accesses fmbm_spliodn[port_id - 1]. mac_mfl[] and port_mfl[] have MAX_NUM_OF_MACS (10) entries, but hw_port_id_to_sw_port_id() maps, for example, RX port id 0x12 to 10 and TX port id 0x3f to 23. fman_port_probe() truncates the property to a u8 and does not otherwise check it, so a device tree with a bad cell-index can cause out-of-bounds accesses. Reject a port id of 0, a port id outside the LIODN table, and a port id that does not map to a MAC, before any state or register is changed. A cell-index above 0xff that aliases a valid id after the u8 truncation is not caught here. Found by static analysis tool CodeQL. Fixes: 414fd46e7762 ("fsl/fman: Add FMan support") Assisted-by: LLM codeql Signed-off-by: Weigang He --- Notes: Compile-tested only (ARCH=arm64 and ARCH=x86_64 allmodconfig, W=1). Not tested on hardware, and there is no reproducer. The CodeQL query behind this report was synthesized with LLM assistance, and the fix and changelog were drafted with LLM assistance; I have reviewed them. drivers/net/ethernet/freescale/fman/fman.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/drivers/net/ethernet/freescale/fman/fman.c b/drivers/net/ethernet/freescale/fman/fman.c index 46cc28895e565..e25050189b1f3 100644 --- a/drivers/net/ethernet/freescale/fman/fman.c +++ b/drivers/net/ethernet/freescale/fman/fman.c @@ -2103,6 +2103,18 @@ int fman_set_port_params(struct fman *fman, unsigned long flags; u8 port_id = port_params->port_id, mac_id; + /* + * port_id comes from the device tree: reject ids outside the LIODN + * table and ids that do not map to a MAC. + */ + mac_id = hw_port_id_to_sw_port_id(fman->state->rev_info.major, port_id); + if (port_id == 0 || port_id >= FMAN_LIODN_TBL || + mac_id >= MAX_NUM_OF_MACS) { + dev_err(fman->dev, "%s: invalid port id %u\n", __func__, + port_id); + return -EINVAL; + } + spin_lock_irqsave(&fman->spinlock, flags); err = set_num_of_tasks(fman, port_params->port_id, @@ -2170,8 +2182,6 @@ int fman_set_port_params(struct fman *fman, if (fman->state->rev_info.major < 6) set_port_order_restoration(fman->fpm_regs, port_id); - mac_id = hw_port_id_to_sw_port_id(fman->state->rev_info.major, port_id); - if (port_params->max_frame_length >= fman->state->mac_mfl[mac_id]) { fman->state->port_mfl[mac_id] = port_params->max_frame_length; } else { base-commit: 165768bb70265b5c38cf0b73fafd75be235f8b14 -- 2.43.0