1: 7cdfa75c2637 ! 1: c96f55cfcbc6 wifi: nxpwifi: fix tid and reorder-window bounds checks in RX reorder @@ Metadata Author: guohongyu   ## Commit message ## - wifi: nxpwifi: fix tid and reorder-window bounds checks in RX reorder - - The nxpwifi RX reorder code indexes several MAX_NUM_TID-sized arrays - (struct nxpwifi_private::rx_reorder_tbl_ptr[8] and - rx_reorder_tbl_lock[8], main.h:548/550) with a tid that is neither - validated at the callers nor at the function entry, and accesses a - win_size-sized flex array (tbl->rx_reorder_ptr[]) with a pkt_index that - can exceed win_size. Both are remote-reachable through a received - 802.11 frame. - - Bug 1 - tid used as array index without bounds check - - nxpwifi_11n_rx_reorder_pkt() and nxpwifi_11n_create_rx_reorder_tbl() - take a tid parameter that flows directly into: - - list_for_each_entry_rcu(tbl, &priv->rx_reorder_tbl_ptr[tid], list) (11n_rxreorder.c:187) - spin_lock_bh(&priv->rx_reorder_tbl_lock[tid]); (11n_rxreorder.c:324) - list_add_tail_rcu(&new_node->list, &priv->rx_reorder_tbl_ptr[tid]); (11n_rxreorder.c:325) - - These arrays have MAX_NUM_TID (8) entries. The tid comes from - local_rx_pd->priority in the receive path (sta_rx.c:232, uap_txrx.c:405) - - a u8 filled by firmware from the received frame's rxpd, range 0-255 - - or from the 4-bit TID field of an ADDBA request (11n_rxreorder.c:379, - range 0-15). Neither path checks tid < MAX_NUM_TID before calling into - the reorder code; sta_rx.c:224-226 does gate the rx_seq[] write on - priority < MAX_NUM_TID, but the subsequent nxpwifi_11n_rx_reorder_pkt() - call at sta_rx.c:232 drops the same check. A tid >= 8 reads - rx_reorder_tbl_ptr[] out of bounds, takes a spinlock on a garbage - address, and writes an out-of-bounds list_head. - - Fix: validate tid < MAX_NUM_TID at the entry of both - nxpwifi_11n_rx_reorder_pkt() and nxpwifi_11n_create_rx_reorder_tbl(), - covering all callers. - - Bug 2 - pkt_index not bounded by win_size - - tbl->rx_reorder_ptr is allocated with win_size entries - (11n_rxreorder.c:280, kzalloc_flex). In nxpwifi_11n_rx_reorder_pkt() - the out-of-window drop check (11n_rxreorder.c:459-470) uses TWOPOW11 - (2048) rather than win_size, so a seq_num within [start_win, - start_win + 2048) passes the drop check regardless of win_size. - pkt_index is then computed as seq_num - start_win (11n_rxreorder.c:491) - and can reach 0..2047, but the write - - tbl->rx_reorder_ptr[pkt_index] = payload; (11n_rxreorder.c:500) - - only has win_size slots. With win_size = 1 (allowed: create_rx_reorder_tbl - only rejects win_size <= 0 at 11n_rxreorder.c:276) any pkt_index > 0 is a - heap out-of-bounds write of up to ~2048 pointer slots. - - Fix: bound pkt_index against tbl->win_size before the rx_reorder_ptr[] - access. - - Together these close an out-of-bounds read + spinlock-on-garbage and an - out-of-bounds heap write, both reachable from a received frame. + wifi: nxpwifi: validate tid in the RX reorder-table lookup + + The RX reorder code uses the tid passed by its callers to index the + MAX_NUM_TID-sized rx_reorder_tbl_ptr[] and rx_reorder_tbl_lock[] + arrays. Several callers take the tid from a received frame or from a + firmware event and never validate it, so a tid >= MAX_NUM_TID accesses + the arrays out of bounds. + + Validate tid in nxpwifi_11n_get_rx_reorder_tbl() itself, so that every + caller is covered. Also validate it at the entry of + nxpwifi_11n_create_rx_reorder_tbl(): when the lookup returns NULL, the + create path adds a new entry to the tid-indexed rx_reorder_tbl_ptr[] + list, which the lookup check cannot cover. + + In nxpwifi_11n_rx_reorder_pkt(), the out-of-window drop check bounds + seq_num by TWOPOW11 rather than by win_size. The window advance that + follows keeps the computed pkt_index within win_size, but it does not + trigger when win_size == 1, so pkt_index can still run past the + win_size-sized tbl->rx_reorder_ptr[] flex array. Bound pkt_index by + win_size before the array access.  Fixes: 73b01e57ed3e ("wifi: nxp: add nxpwifi driver for IW61x") Cc: stable@vger.kernel.org Signed-off-by: guohongyu   ## drivers/net/wireless/nxp/nxpwifi/11n_rxreorder.c ## +@@ drivers/net/wireless/nxp/nxpwifi/11n_rxreorder.c: nxpwifi_11n_get_rx_reorder_tbl(struct nxpwifi_private *priv, int tid, u8 *ta) + { + struct nxpwifi_rx_reorder_tbl *tbl, *found = NULL; +  ++ if (tid >= MAX_NUM_TID) ++ return NULL; ++ + guard(rcu)(); +  + list_for_each_entry_rcu(tbl, &priv->rx_reorder_tbl_ptr[tid], list) {  @@ drivers/net/wireless/nxp/nxpwifi/11n_rxreorder.c: nxpwifi_11n_create_rx_reorder_tbl(struct nxpwifi_private *priv, u8 *ta, u16 last_seq = 0; struct nxpwifi_sta_node *node; @@ drivers/net/wireless/nxp/nxpwifi/11n_rxreorder.c: nxpwifi_11n_create_rx_reorder_ /* Existing TID/TA: flush and move window to SSN. */ tbl = nxpwifi_11n_get_rx_reorder_tbl(priv, tid, ta); if (tbl) { -@@ drivers/net/wireless/nxp/nxpwifi/11n_rxreorder.c: int nxpwifi_11n_rx_reorder_pkt(struct nxpwifi_private *priv, - bool init_window_shift = false; - int ret = 0; -  -+ if (tid >= MAX_NUM_TID) -+ return -EINVAL; -+ - tbl = nxpwifi_11n_get_rx_reorder_tbl(priv, tid, ta); - if (!tbl) { - if (pkt_type != PKT_TYPE_BAR)  @@ drivers/net/wireless/nxp/nxpwifi/11n_rxreorder.c: int nxpwifi_11n_rx_reorder_pkt(struct nxpwifi_private *priv, else pkt_index = (seq_num + MAX_TID_VALUE) - start_win;