wifi: rtw89: check lengths and indices reported by the chip
v3 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 v3:
- 1/4: bound the pkt_size fallback instead of the end of the copy.
- 3/4: check len in the LPS report handler, as Ping-Ke Shih suggested.
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.
v2: https://lore.kernel.org/linux-wireless/[email protected]/
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 | 20 ++++++++++++++++++-- drivers/net/wireless/realtek/rtw89/phy.c | 6 ++++++ 3 files changed, 27 insertions(+), 2 deletions(-)
Signature · 1 line
2.34.1
PATCH v3 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(). A write size below the data offset makes copy_len wrap, and a single segment then copies desc_info->pkt_size bytes. Use that fallback only when it fits in the buffer.
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.
v3: Replace the end-of-copy check with a bound on the pkt_size fallback in rtw89_skb_put_rx_data() (Ping-Ke Shih).
v2: Move the rx_info->len check to rtw89_pci_sync_skb_for_device_and_validate_rx_info() (Ping-Ke Shih).
drivers/net/wireless/realtek/rtw89/pci.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)
drivers/net/wireless/realtek/rtw89/pci.c +12 -2
@@ -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)
@@ -290,7 +299,8 @@ rtw89_skb_put_rx_data(struct rtw89_dev *rtwdev, bool fs, bool ls,rtw89_hex_dump (rtwdev,RTW89_DBG_TXRX ,"rx_data: " , skb->data, rx_info->len);/* length of a single segment skb is desc_info->pkt_size */ -if (fs && ls) { +if (fs && ls && + offset + desc_info->pkt_size <=RTW89_PCI_RX_BUF_SIZE ) { copy_len = desc_info->pkt_size; }else {rtw89_info (rtwdev,"drop rx data due to invalid length\n" );
Signature · 1 line
2.34.1
RE: [PATCH rtw-next v3 1/4] wifi: rtw89: pci: reject oversized RX packets
Quoted 11 lines · Maoyi Xie
rtw89_skb_put_rx_data()andrtw89_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 againstRTW89_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(). A write size below the data offset makescopy_lenwrap, and a single segment then copiesdesc_info->pkt_sizebytes. Use that fallback only when it fits in the buffer.
Quoted 6 lines
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")Signed-off-by Maoyi Xie <[email protected]>
Ping-Ke Shih
maintainerRe: [PATCH rtw-next v3 1/4] wifi: rtw89: pci: reject oversized RX packets
In reply to Maoyi XieQuoted 11 lines · Maoyi Xie
rtw89_skb_put_rx_data()andrtw89_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 againstRTW89_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(). A write size below the data offset makescopy_lenwrap, and a single segment then copiesdesc_info->pkt_sizebytes. Use that fallback only when it fits in the buffer.
Quoted 7 lines
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")Signed-off-by Maoyi Xie <[email protected]>Acked-by Ping-Ke Shih <[email protected]>
4 patch(es) applied to rtw-next branch of rtw.git, thanks.
889630e2196b wifi: rtw89: pci: reject oversized RX packets 697f31d8ed9e wifi: rtw89: pci: validate hardware index of BD ring 1797ba652033 wifi: rtw89: fw: check C2H event length 9d02643d769a wifi: rtw89: phy: check cur_idx of TAS power report
PATCH v3 2/4 wifi: rtw89: pci: validate hardware index of BD ring
In reply to Maoyi XieThe 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.
---
I used an LLM to help write the harness and this patch.
v3: No change.
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
@@ -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
PATCH v3 3/4 wifi: rtw89: fw: check C2H event length
In reply to Maoyi XieNothing 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 the event length before reading the counts.
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.
v3: Check len instead of c2h->len in rtw89_phy_c2h_lps_rpt(), with unlikely() (Ping-Ke Shih).
v2: Check the length in rtw89_fw_c2h_cmd_handle() with unlikely() (Ping-Ke Shih).
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
@@ -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
@@ -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 (unlikely (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
RE: [PATCH rtw-next v3 3/4] wifi: rtw89: fw: check C2H event length
Quoted 5 lines · 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.
Quoted 7 lines
Drop such events in
rtw89_fw_c2h_cmd_handle(). In the LPS report handler, check the event length before reading the counts.Found with a CodeQL-based static analysis tool. Reproduced in a userspace harness under AddressSanitizer. Not tested on hardware.
Signed-off-by Maoyi Xie <[email protected]>
PATCH v3 4/4 wifi: rtw89: phy: check cur_idx of TAS power report
In reply to Maoyi Xiertw89_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.
v3: No change.
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
@@ -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