wifi: rtw88: fix OOB read from firmware RX descriptor exceeding DMA buffer
PATCH v2 1/1 wifi: rtw88: fix OOB read from firmware RX descriptor exceeding DMA buffer
From: Tristan Madani <[email protected]>
In rtw_pci_rx_napi(), new_len is computed as the sum of pkt_len (14-bit descriptor field, max 16383) and pkt_offset (drv_info_sz + shift, both firmware-controlled). The result can exceed RTK_PCI_RX_BUF_SIZE (11478), causing an out-of-bounds read from the pre-allocated DMA buffer when skb_put_data copies new_len bytes. The USB transport already validates this (rtw_usb_rx_data_put checks against RTW_USB_MAX_RECVBUF_SZ); the PCIe path does not.
Add a check that new_len does not exceed the DMA buffer size.
---
Note: v2 resubmission -- original sent via Gmail had HTML rendering issues. This version uses git send-email for plain-text formatting.
Changes in v2:
- v2: clarify field widths and maximum new_len derivation in commit message, per Ping-Ke Shih's feedback.
drivers/net/wireless/realtek/rtw88/pci.c | 5 +++++
1 file changed, 5 insertions(+)
drivers/net/wireless/realtek/rtw88/pci.c +5 -0
@@ -1078,6 +1078,11 @@ static int rtw_pci_rx_napi(struct rtw_dev *rtwdev, struct rtw_pci *rtwpci, new_len = pkt_stat.pkt_len + pkt_offset; +if (new_len >RTK_PCI_RX_BUF_SIZE ) { +rtw_dbg (rtwdev,RTW_DBG_RX , +"oversized RX packet: %u\n" , new_len); + goto next_rp; + } new =dev_alloc_skb (new_len);if (WARN_ONCE (!new,"rx routine starvation\n" )) goto next_rp;
Quoted 31 lines · Tristan Madani
From: Tristan Madani <[email protected]>
In
rtw_pci_rx_napi(),new_lenis computed as the sum ofpkt_len(14-bit descriptor field, max 16383) andpkt_offset(drv_info_sz+ shift, both firmware-controlled). The result can exceedRTK_PCI_RX_BUF_SIZE(11478), causing an out-of-bounds read from the pre-allocated DMA buffer whenskb_put_datacopiesnew_lenbytes. The USB transport already validates this (rtw_usb_rx_data_putchecks againstRTW_USB_MAX_RECVBUF_SZ); the PCIe path does not.Add a check that
new_lendoes not exceed the DMA buffer size.Fixes e3037485c68e ("rtw88: new Realtek 802.11ac driver")Signed-off-by Tristan Madani <[email protected]>---
Note: v2 resubmission -- original sent via Gmail had HTML rendering issues. This version uses git send-email for plain-text formatting.Changes in v2:
- v2: clarify field widths and maximumnew_lenderivation in commit message, per Ping-Ke Shih's feedback.
drivers/net/wireless/realtek/rtw88/pci.c| 5 +++++1 file changed, 5 insertions(+)drivers/net/wireless/realtek/rtw88/pci.c +0
-0@@ -1078,6 +1078,11 @@ static int rtw_pci_rx_napi(struct rtw_dev *rtwdev, struct rtw_pci *rtwpci, new_len = pkt_stat.pkt_len + pkt_offset;
drivers/net/wireless/realtek/rtw88/pci.c static int rtw_pci_rx_napi(struct rtw_dev *rtwdev, struct rtw_pci *rtwpci, · line 1088
Quoted 10 lines
drivers/net/wireless/realtek/rtw88/pci.c +5
-0+if (new_len >RTK_PCI_RX_BUF_SIZE ) { +rtw_dbg (rtwdev,RTW_DBG_RX , +"oversized RX packet: %u\n" , new_len); + goto next_rp; + } new =dev_alloc_skb (new_len);if (WARN_ONCE (!new,"rx routine starvation\n" )) goto next_rp;
I'm working on a patch which will implement the same validation in rtw_rx_query_rx_desc(), along with two other checks. I got a report about too short packets from RTL8814AU, so USB devices can also benefit from checking pkt_len. It will make this patch redundant.
Well, kind of. Maybe RTK_PCI_RX_BUF_SIZE is too small? 11454 + 24 doesn't take into account the PHY info size.
Ping-Ke Shih
maintainerQuoted 27 lines · Bitterblue Smith
Quoted 25 lines · Tristan Madani
From: Tristan Madani <[email protected]>
In
rtw_pci_rx_napi(),new_lenis computed as the sum ofpkt_len(14-bit descriptor field, max 16383) andpkt_offset(drv_info_sz+ shift, both firmware-controlled). The result can exceedRTK_PCI_RX_BUF_SIZE(11478), causing an out-of-bounds read from the pre-allocated DMA buffer whenskb_put_datacopiesnew_lenbytes. The USB transport already validates this (rtw_usb_rx_data_putchecks againstRTW_USB_MAX_RECVBUF_SZ); the PCIe path does not.Add a check that
new_lendoes not exceed the DMA buffer size.Fixes e3037485c68e ("rtw88: new Realtek 802.11ac driver")Signed-off-by Tristan Madani <[email protected]>---
Note: v2 resubmission -- original sent via Gmail had HTML rendering issues. This version uses git send-email for plain-text formatting.Changes in v2:
- v2: clarify field widths and maximumnew_lenderivation in commit message, per Ping-Ke Shih's feedback.
drivers/net/wireless/realtek/rtw88/pci.c| 5 +++++1 file changed, 5 insertions(+)
Quoted 7 lines
drivers/net/wireless/realtek/rtw88/pci.c static int rtw_pci_rx_napi(struct rtw_dev *rtwdev, struct rtw_pci *rtwpci, · line 1079
Quoted 7 lines
drivers/net/wireless/realtek/rtw88/pci.c +1
-0@@ -1078,6 +1078,11 @@ static int rtw_pci_rx_napi(struct rtw_dev *rtwdev, struct rtw_pci *rtwpci, new_len = pkt_stat.pkt_len + pkt_offset; +if (new_len >RTK_PCI_RX_BUF_SIZE ) {
Since this is data (hot) path, I'd prefer unlikely(new_len > RTK_PCI_RX_BUF_SIZE).
Quoted 10 lines
drivers/net/wireless/realtek/rtw88/pci.c static int rtw_pci_rx_napi(struct rtw_dev *rtwdev, struct rtw_pci *rtwpci,
Quoted 9 lines
drivers/net/wireless/realtek/rtw88/pci.c +4
-0+rtw_dbg (rtwdev,RTW_DBG_RX , +"oversized RX packet: %u\n" , new_len); + goto next_rp; + } new =dev_alloc_skb (new_len);if (WARN_ONCE (!new,"rx routine starvation\n" )) goto next_rp;
Quoted 5 lines
I'm working on a patch which will implement the same validation in
rtw_rx_query_rx_desc(), along with two other checks. I got a report about too short packets from RTL8814AU, so USB devices can also benefit from checkingpkt_len. It will make this patch redundant.
Bitterblue, if you can take the change of this patch into your patch, I'd skip this patch. Please let me know your thought.
Quoted 3 lines
Well, kind of. Maybe
RTK_PCI_RX_BUF_SIZEis too small? 11454 + 24 doesn't take into account the PHY info size.
In rtw_pci_sync_rx_desc_device(), driver does
buf_desc->buf_size = cpu_to_le16(RTK_PCI_RX_BUF_SIZE);
This is to tell hardware the size of RX DMA buffer. I think hardware can't DMA data over this size.
Ping-Ke
Quoted 48 lines · Ping-Ke Shih
Quoted 27 lines · Bitterblue Smith
Quoted 25 lines · Tristan Madani
From: Tristan Madani <[email protected]>
In
rtw_pci_rx_napi(),new_lenis computed as the sum ofpkt_len(14-bit descriptor field, max 16383) andpkt_offset(drv_info_sz+ shift, both firmware-controlled). The result can exceedRTK_PCI_RX_BUF_SIZE(11478), causing an out-of-bounds read from the pre-allocated DMA buffer whenskb_put_datacopiesnew_lenbytes. The USB transport already validates this (rtw_usb_rx_data_putchecks againstRTW_USB_MAX_RECVBUF_SZ); the PCIe path does not.Add a check that
new_lendoes not exceed the DMA buffer size.Fixes e3037485c68e ("rtw88: new Realtek 802.11ac driver")Signed-off-by Tristan Madani <[email protected]>---
Note: v2 resubmission -- original sent via Gmail had HTML rendering issues. This version uses git send-email for plain-text formatting.Changes in v2:
- v2: clarify field widths and maximumnew_lenderivation in commit message, per Ping-Ke Shih's feedback.
drivers/net/wireless/realtek/rtw88/pci.c| 5 +++++1 file changed, 5 insertions(+)Quoted 7 lines
drivers/net/wireless/realtek/rtw88/pci.c static int rtw_pci_rx_napi(struct rtw_dev *rtwdev, struct rtw_pci *rtwpci, · line 1079
Quoted 7 lines
drivers/net/wireless/realtek/rtw88/pci.c +1
-0@@ -1078,6 +1078,11 @@ static int rtw_pci_rx_napi(struct rtw_dev *rtwdev, struct rtw_pci *rtwpci, new_len = pkt_stat.pkt_len + pkt_offset; +if (new_len >RTK_PCI_RX_BUF_SIZE ) {Since this is data (hot) path, I'd prefer
unlikely(new_len > RTK_PCI_RX_BUF_SIZE).Quoted 10 lines
Quoted 9 lines
drivers/net/wireless/realtek/rtw88/pci.c +4
-0+rtw_dbg (rtwdev,RTW_DBG_RX , +"oversized RX packet: %u\n" , new_len); + goto next_rp; + } new =dev_alloc_skb (new_len);if (WARN_ONCE (!new,"rx routine starvation\n" )) goto next_rp;
Quoted 9 lines
Quoted 5 lines
I'm working on a patch which will implement the same validation in
rtw_rx_query_rx_desc(), along with two other checks. I got a report about too short packets from RTL8814AU, so USB devices can also benefit from checkingpkt_len. It will make this patch redundant.Bitterblue, if you can take the change of this patch into your patch, I'd skip this patch. Please let me know your thought.
No, I was mistaken. My patch doesn't do exactly the same thing as this patch. It just checks if pkt_len is more than 11454. This patch is also needed. Sorry for the noise.
Quoted 10 lines
Quoted 3 lines
Well, kind of. Maybe
RTK_PCI_RX_BUF_SIZEis too small? 11454 + 24 doesn't take into account the PHY info size.In
rtw_pci_sync_rx_desc_device(), driver doesbuf_desc->buf_size = cpu_to_le16(RTK_PCI_RX_BUF_SIZE);This is to tell hardware the size of RX DMA buffer. I think hardware can't DMA data over this size.
Indeed, I don't think the hardware will write more than RTK_PCI_RX_BUF_SIZE bytes. But I wonder if some bytes won't be lost (or the entire packet?) if it ever receives a frame of 11454 bytes and wants to attach a PHY status? Then it would need a buffer of 11454 + 24 + 32 bytes. I don't know if this ever happens.
Quoted 2 lines
Ping-Ke
Ping-Ke Shih
maintainerQuoted 13 lines · Bitterblue Smith
Quoted 11 lines · Ping-Ke Shih
Quoted 3 lines · Bitterblue Smith
Well, kind of. Maybe
RTK_PCI_RX_BUF_SIZEis too small? 11454 + 24 doesn't take into account the PHY info size.In
rtw_pci_sync_rx_desc_device(), driver doesbuf_desc->buf_size = cpu_to_le16(RTK_PCI_RX_BUF_SIZE);This is to tell hardware the size of RX DMA buffer. I think hardware can't DMA data over this size.
Quoted 5 lines
Indeed, I don't think the hardware will write more than
RTK_PCI_RX_BUF_SIZEbytes. But I wonder if some bytes won't be lost (or the entire packet?) if it ever receives a frame of 11454 bytes and wants to attach a PHY status? Then it would need a buffer of 11454 + 24 + 32 bytes. I don't know if this ever happens.
I think the total size including PHY status must be smaller than RTK_PCI_RX_BUF_SIZE, otherwise we should enlarge RTK_PCI_RX_BUF_SIZE.
The rtw89 can split a packet into multiple segments if RX buffer is smaller than a receiving packet, but it has exact overload, so we enlarge the buffer as large as necessary.
For rtw88, I can't find similar implementation. Maybe we can try to enlarge the size to see if any improvement.
Ping-Ke