Skip to contentSkip to conversation

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

PATCH Superseded Maoyi Xie 2 participants 9 messages Dec 1 – Dec 2 Realtek wireless driver (rtw89)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Maoyi Xie

author
#0 lore raw

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.

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

 drivers/net/wireless/realtek/rtw89/pci.c | 18 ++++++++++++++++++
 drivers/net/wireless/realtek/rtw89/phy.c |  6 ++++++
 2 files changed, 24 insertions(+)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 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 14-bit fields that the chip writes. Nothing checks it against RTW89_PCI_RX_BUF_SIZE. A larger length makes them read up to about 5 KB past the buffer.

Drop such a packet.

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.

 drivers/net/wireless/realtek/rtw89/pci.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
drivers/net/wireless/realtek/rtw89/pci.c +12 -0
index 76523d057825..daa3a978bd31 100644
@@ -298,6 +298,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;
@@ -674,6 +680,12 @@ static u32 rtw89_pci_release_tx_skbs(struct rtw89_dev *rtwdev,
 		return cnt;
 	}
 
+	if (unlikely(rx_info->len > RTW89_PCI_RX_BUF_SIZE)) {
+		rtw89_debug(rtwdev, RTW89_DBG_TXRX,
+			    "oversized RP frame: %u\n", rx_info->len);
+		goto err_sync_device;
+	}
+
 	rtw89_chip_query_rxdesc(rtwdev, &desc_info, skb->data, rxinfo_size);
 
 	/* first segment has RX desc */
Signature · 1 line
2.34.1
maintainer
#2 lore raw

RE: [PATCH rtw-next 1/4] wifi: rtw89: pci: reject oversized RX packets

Quoted 4 lines · Maoyi Xie

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

Can we validate rx_info->len > RTW89_PCI_RX_BUF_SIZE earlier at rtw89_pci_sync_skb_for_device_and_validate_rx_info()?

author
#3 lore raw

PATCH 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.

 drivers/net/wireless/realtek/rtw89/pci.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/net/wireless/realtek/rtw89/pci.c +6 -0
index daa3a978bd31..d0cd73ea04b7 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
maintainer Acked-by on v1 2/4
#4 lore raw

RE: [PATCH rtw-next 2/4] wifi: rtw89: pci: validate hardware index of BD ring

Quoted 6 lines · 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.

Quoted 8 lines

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")
author
#5 lore raw

PATCH 3/4 wifi: rtw89: phy: check LPS report C2H event length

In reply to Maoyi Xie

The LPS report handler checks the length from the C2H header and the counts in the report against each other, but not against the skb. If the header claims more data than arrived, the debug prints read up to 16 KB past the skb data, even with the debug mask off.

Check the skb length before processing the report.

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.

 drivers/net/wireless/realtek/rtw89/phy.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/net/wireless/realtek/rtw89/phy.c +3 -0
index 133b591ffa7f..b4f1802025c9 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) || len > c2h->len)
+		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
maintainer
#6 lore raw

RE: [PATCH rtw-next 3/4] wifi: rtw89: phy: check LPS report C2H event length

Quoted 22 lines · Maoyi Xie

The LPS report handler checks the length from the C2H header and the counts in the report against each other, but not against the skb. If the header claims more data than arrived, the debug prints read up to 16 KB past the skb data, even with the debug mask off.

Check the skb length before processing the report.

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.

 drivers/net/wireless/realtek/rtw89/phy.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/net/wireless/realtek/rtw89/phy.c +0 -0
index 133b591ffa7f..b4f1802025c9 100644

drivers/net/wireless/realtek/rtw89/phy.c rtw89_phy_c2h_lps_rpt(struct rtw89_dev *rtwdev, struct sk_buff *c2h, u32 len) · line 3454

Quoted 6 lines
drivers/net/wireless/realtek/rtw89/phy.c +3 -0
@@ -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) || len > c2h->len)
+               return;
+

Validate 'len > c2h->len' earlier (with unlikely) at rtw89_fw_c2h_cmd_handle().

Quoted 5 lines
drivers/net/wireless/realtek/rtw89/phy.c +0 -1
        /* elements size of BBCR/BBMCUCR/RFCR are 6/6/10 bytes respectively */
        cr_len = c2h_rpt->cnt_bbcr * 6 +
                 c2h_rpt->cnt_bbmcucr * 6 +
--

2.34.1

author
#7 lore raw

PATCH 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.

 drivers/net/wireless/realtek/rtw89/phy.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/net/wireless/realtek/rtw89/phy.c +3 -0
index b4f1802025c9..26675a31ced9 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
maintainer Acked-by on v1 4/4
#8 lore raw

RE: [PATCH rtw-next 4/4] wifi: rtw89: phy: check cur_idx of TAS power report

Quoted 11 lines · 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.