Skip to contentSkip to conversation

wifi: rtw89: check lengths and indices reported by the chip

PATCH New Maoyi Xie 1 participant 5 messages Dec 2 Realtek wireless driver (rtw89)

lore
Share & tools
lore

Maoyi Xie

author
#0 lore raw

v2 cover wifi: rtw89: check lengths and indices reported by the chip

These patches add checks for two lengths and two indices that the chip reports. The patches are independent.

Found with a CodeQL-based static analysis tool that we are developing. Each case was reproduced in a userspace harness under AddressSanitizer. Not tested on hardware. Compile-tested with W=1 on rtw-next.

I used an LLM to help write the harnesses and these patches.

Changes in v2:
- 1/4, 3/4: check the lengths earlier, as Ping-Ke Shih suggested. 3/4 has a new subject.
- 2/4, 4/4: add Acked-by.

v1: https://lore.kernel.org/linux-wireless/[email protected]/

Maoyi Xie (4):
wifi: rtw89: pci: reject oversized RX packets
wifi: rtw89: pci: validate hardware index of BD ring
wifi: rtw89: fw: check C2H event length
wifi: rtw89: phy: check cur_idx of TAS power report

 drivers/net/wireless/realtek/rtw89/fw.c  |  3 +++
 drivers/net/wireless/realtek/rtw89/pci.c | 23 ++++++++++++++++++++++-
 drivers/net/wireless/realtek/rtw89/phy.c |  6 ++++++
 3 files changed, 31 insertions(+), 1 deletion(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v2 1/4 wifi: rtw89: pci: reject oversized RX packets

rtw89_skb_put_rx_data() and rtw89_pci_release_tx_skbs() take the data length of an RX buffer from the 14-bit write size that the chip reports. Nothing checks it against RTW89_PCI_RX_BUF_SIZE. A larger value makes them read up to about 5 KB past the buffer.

Check the write size in rtw89_pci_sync_skb_for_device_and_validate_rx_info(). Also check the end of the copy in rtw89_skb_put_rx_data().

Found with a CodeQL-based static analysis tool. Reproduced in a userspace harness under AddressSanitizer. Not tested on hardware.

Fixes e3ec7017f6a2 ("rtw89: add Realtek 802.11ax driver")

---
I used an LLM to help write the harness and this patch.

v2: Move the rx_info->len check to rtw89_pci_sync_skb_for_device_and_validate_rx_info() (Ping-Ke Shih). Keep the check in rtw89_skb_put_rx_data(). With a write size below the data offset, copy_len wraps and the pkt_size fallback can still read past the buffer.

 drivers/net/wireless/realtek/rtw89/pci.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
drivers/net/wireless/realtek/rtw89/pci.c +16 -1
index 2b6a5e22221d..c47d1a0ce18c 100644
@@ -243,7 +243,16 @@ int rtw89_pci_sync_skb_for_device_and_validate_rx_info(struct rtw89_dev *rtwdev,
 	/* update target rx_tag for next RX */
 	rx_ring->target_rx_tag = rx_info->tag + 1;
 
-	return ret;
+	if (ret)
+		return ret;
+
+	if (unlikely(rx_info->len > RTW89_PCI_RX_BUF_SIZE)) {
+		rtw89_debug(rtwdev, RTW89_DBG_UNEXP, "oversized RXBD len: %u\n",
+			    rx_info->len);
+		return -EINVAL;
+	}
+
+	return 0;
 }
 
 static void rtw89_pci_ctrl_txdma_ch_ax(struct rtw89_dev *rtwdev, bool enable)
@@ -298,6 +307,12 @@ rtw89_skb_put_rx_data(struct rtw89_dev *rtwdev, bool fs, bool ls,
 		}
 	}
 
+	if (unlikely(offset + copy_len > RTW89_PCI_RX_BUF_SIZE)) {
+		rtw89_debug(rtwdev, RTW89_DBG_TXRX,
+			    "oversized RX packet: %u\n", offset + copy_len);
+		return false;
+	}
+
 	skb_put_data(new, skb->data + offset, copy_len);
 
 	return true;
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH v2 2/4 wifi: rtw89: pci: validate hardware index of BD ring

In reply to Maoyi Xie

The hardware index of a BD ring has 12 bits, but a ring has 256 entries. With an index of 512 or more (4095 for a read of 0xffffffff), a failed release can leave the RX write pointer outside the ring. On chips without rx_ring_eq_is_full, the next poll reads rx_ring->buf[] out of bounds.

Ignore an index outside the ring.

Found with a CodeQL-based static analysis tool. Reproduced in a userspace harness under AddressSanitizer. Not tested on hardware.

Fixes e3ec7017f6a2 ("rtw89: add Realtek 802.11ax driver")

---
I used an LLM to help write the harness and this patch.

v2: Add Acked-by.

 drivers/net/wireless/realtek/rtw89/pci.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/net/wireless/realtek/rtw89/pci.c +6 -0
index c47d1a0ce18c..54cbaf817288 100644
@@ -70,6 +70,12 @@ static u32 rtw89_pci_dma_recalc(struct rtw89_dev *rtwdev,
 	len = bd_ring->len;
 
 	cur_rp = FIELD_GET(TXBD_HW_IDX_MASK, cur_idx);
+	if (unlikely(cur_rp >= len)) {
+		rtw89_debug(rtwdev, RTW89_DBG_UNEXP,
+			    "invalid BD ring idx=0x%08x, len=%u\n", cur_idx, len);
+		return 0;
+	}
+
 	if (tx) {
 		cnt = cur_rp >= rp ? cur_rp - rp : len - (rp - cur_rp);
 	} else {
Signature · 1 line
2.34.1
author
#3 lore raw

PATCH v2 3/4 wifi: rtw89: fw: check C2H event length

In reply to Maoyi Xie

Nothing checks the length field of the C2H header against the skb. If the header claims more data than arrived, the debug prints of the LPS report handler read up to 16 KB past the skb data, even with the debug mask off.

Drop such events in rtw89_fw_c2h_cmd_handle(). In the LPS report handler, check that the skb holds the counts before reading them.

Found with a CodeQL-based static analysis tool. Reproduced in a userspace harness under AddressSanitizer. Not tested on hardware.

---
I used an LLM to help write the harness and this patch.

v2: Check the length in rtw89_fw_c2h_cmd_handle() with unlikely() (Ping-Ke Shih). Keep the size check in rtw89_phy_c2h_lps_rpt().

 drivers/net/wireless/realtek/rtw89/fw.c  | 3 +++
 drivers/net/wireless/realtek/rtw89/phy.c | 3 +++
 2 files changed, 6 insertions(+)
drivers/net/wireless/realtek/rtw89/fw.c +3 -0
index 89559454ebe2..2eb77d4c29de 100644
@@ -9701,6 +9701,9 @@ static void rtw89_fw_c2h_cmd_handle(struct rtw89_dev *rtwdev,
 	if (!test_bit(RTW89_FLAG_RUNNING, rtwdev->flags))
 		return;
 
+	if (unlikely(len > skb->len))
+		return;
+
 	switch (category) {
 	case RTW89_C2H_CAT_TEST:
 		break;
drivers/net/wireless/realtek/rtw89/phy.c +3 -0
index 133b591ffa7f..139f5a9abc32 100644
@@ -3449,6 +3449,9 @@ rtw89_phy_c2h_lps_rpt(struct rtw89_dev *rtwdev, struct sk_buff *c2h, u32 len)
 	const __le16 *addr;
 	const u8 *info;
 
+	if (c2h->len < sizeof(*c2h_rpt))
+		return;
+
 	/* elements size of BBCR/BBMCUCR/RFCR are 6/6/10 bytes respectively */
 	cr_len = c2h_rpt->cnt_bbcr * 6 +
 		 c2h_rpt->cnt_bbmcucr * 6 +
Signature · 1 line
2.34.1
author
#4 lore raw

PATCH v2 4/4 wifi: rtw89: phy: check cur_idx of TAS power report

In reply to Maoyi Xie

rtw89_phy_c2h_rfk_tas_pwr() averages txpwr_history[i] for i below cur_idx. cur_idx comes from the firmware report, and nothing checks it against the 20 entries of txpwr_history[]. A larger value makes the loop read past the report.

Ignore the report when cur_idx exceeds the size of txpwr_history[].

Found with a CodeQL-based static analysis tool. Reproduced in a userspace harness under AddressSanitizer. Not tested on hardware.

---
I used an LLM to help write the harness and this patch.

v2: Add Acked-by.

 drivers/net/wireless/realtek/rtw89/phy.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/net/wireless/realtek/rtw89/phy.c +3 -0
index 139f5a9abc32..4cacc5584296 100644
@@ -3541,6 +3541,9 @@ void rtw89_phy_c2h_rfk_tas_pwr(struct rtw89_dev *rtwdev,
 		return;
 
 	cur_idx = le32_to_cpu(content->cur_idx);
+	if (cur_idx > ARRAY_SIZE(content->txpwr_history))
+		return;
+
 	for (i = 0; i < cur_idx; i++) {
 		txpwr = le16_to_cpu(content->txpwr_history[i]);
 		linear += rtw89_db_quarter_to_linear(txpwr);
Signature · 1 line
2.34.1