mm81x_mac_ops_prepare_multicast() stores netdev_hw_addr_list_count() and the computed allocation length in u16 variables before checking addr_count > MCAST_FILTER_COUNT_MAX: 1. With 16383 entries, len wraps to 0; kzalloc(0) returns ZERO_SIZE_PTR and writing filter->count = 0 faults. 2. With 65536 entries, u16 addr_count truncates to 0, passing the bounds check after allocating only sizeof(*filter) while the subsequent netdev_hw_addr_list_for_each() writes all list entries out of bounds. 3. MCAST_FILTER_COUNT_MAX evaluates to 256 while struct mm81x_mcast_filter.count is a u8, allowing count to wrap to 0 at 256. Keep addr_count as int, cap MCAST_FILTER_COUNT_MAX at U8_MAX, validate addr_count before allocating, and size the valid buffer with struct_size(). Found by LLM-assisted static analysis. Compile-tested on x86_64 with CONFIG_MM81X=m; no hardware test. Fixes: b1906cea00b0 ("wifi: mm81x: add mm81x Wi-Fi HaLow driver") Signed-off-by: Ziheng Wei --- drivers/net/wireless/morsemicro/mm81x/mac.c | 37 ++++++++++++--------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/drivers/net/wireless/morsemicro/mm81x/mac.c b/drivers/net/wireless/morsemicro/mm81x/mac.c index cd0bb4d21..3310fd626 100644 --- a/drivers/net/wireless/morsemicro/mm81x/mac.c +++ b/drivers/net/wireless/morsemicro/mm81x/mac.c @@ -10,6 +10,8 @@ #include #include #include +#include +#include #include "hif.h" #include "mac.h" #include "bus.h" @@ -21,7 +23,7 @@ * the command does not exceed page/MTU size. This will be far greater than * the number of filters supported by the firmware. */ -#define MCAST_FILTER_COUNT_MAX (1024 / sizeof(filter->addr_list[0])) +#define MCAST_FILTER_COUNT_MAX min_t(int, 1024 / sizeof(__le32), U8_MAX) /* Calculate average RSSI for Rx status */ #define CALC_AVG_RSSI(_avg, _sample) ((((_avg) * 9 + (_sample)) / 10)) @@ -1615,26 +1617,29 @@ static u64 mm81x_mac_ops_prepare_multicast(struct ieee80211_hw *hw, struct mm81x *mors = hw->priv; struct mm81x_mcast_filter *filter; struct netdev_hw_addr *addr; - u16 addr_count = netdev_hw_addr_list_count(mc_list); - u16 len = sizeof(*filter) + addr_count * sizeof(filter->addr_list[0]); + int addr_count = netdev_hw_addr_list_count(mc_list); + size_t len; + + if (addr_count < 0 || addr_count > MCAST_FILTER_COUNT_MAX) { + dev_warn(mors->dev, + "Multicast filtering disabled - too many groups (%d) > %d", + addr_count, MCAST_FILTER_COUNT_MAX); + filter = kzalloc_obj(*filter, GFP_ATOMIC); + if (!filter) + return 0; + filter->count = 0; + return (u64)(unsigned long)filter; + } + len = struct_size(filter, addr_list, addr_count); filter = kzalloc(len, GFP_ATOMIC); if (!filter) return 0; - if (addr_count > MCAST_FILTER_COUNT_MAX) { - dev_warn( - mors->dev, - "Multicast filtering disabled - too many groups (%d) > %u", - addr_count, (u16)MCAST_FILTER_COUNT_MAX); - filter->count = 0; - } else { - netdev_hw_addr_list_for_each(addr, mc_list) { - dev_dbg(mors->dev, "mcast whitelist (%d): %pM", - filter->count, addr->addr); - filter->addr_list[filter->count++] = - mac2le32(addr->addr); - } + netdev_hw_addr_list_for_each(addr, mc_list) { + dev_dbg(mors->dev, "mcast whitelist (%d): %pM", + filter->count, addr->addr); + filter->addr_list[filter->count++] = mac2le32(addr->addr); } return (u64)(unsigned long)filter; base-commit: 97d73c8251226cf27cc6656b4c542869bb8c69fa -- 2.43.0