Skip to contentSkip to conversation

wifi: mt76: sdio: check the RX packet length in mt76s_rx_run_queue()

PATCH In review Maoyi Xie 2 participants 4 messages Nov 19 – Nov 20

Subsystems (3)
lore
Share & tools
lore

Maoyi Xie

author
#0 lore raw

PATCH 1/1 wifi: mt76: sdio: check the RX packet length in mt76s_rx_run_queue()

mt76s_rx_run_queue() reads the packets that the chip announced in the interrupt status into one buffer. It then takes the length of each packet from the first word of the packet and does not compare it with the bytes read. A larger length can make the driver read TX status records past the end of the buffer and build an skb that extends past it.

Stop at a packet that does not fit in the bytes read.

Found with a CodeQL-based static analysis tool that we are developing. Reproduced in a userspace harness under ASan, which reports no such read with this patch. Not tested on hardware, only compile-tested with W=1 on next-20261116.

Fixes dacf0acfe2ce ("mt76: sdio: extend sdio module to support CONNAC2")

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

 drivers/net/wireless/mediatek/mt76/sdio_txrx.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/net/wireless/mediatek/mt76/sdio_txrx.c +2 -0
index 3f314e8e1e69f92..86dd1165d2c7f1f 100644
@@ -127,6 +127,8 @@ mt76s_rx_run_queue(struct mt76_dev *dev, enum mt76_rxq_id qid,
 
 		/* parse rxd to get the actual packet length */
 		len = le32_get_bits(rxd[0], GENMASK(15, 0));
+		if (len > end - buf)
+			break;
 
 		/* Optimized path for TXS */
 		if (!dev->drv->rx_check || dev->drv->rx_check(dev, buf, len)) {

base-commit: 7e1392a42ea288d70a266f6553fe06f57f8647ea

Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 37 lines

mt76s_rx_run_queue() reads the packets that the chip announced in the interrupt status into one buffer. It then takes the length of each packet from the first word of the packet and does not compare it with the bytes read. A larger length can make the driver read TX status records past the end of the buffer and build an skb that extends past it.

Stop at a packet that does not fit in the bytes read.

Found with a CodeQL-based static analysis tool that we are developing. Reproduced in a userspace harness under ASan, which reports no such read with this patch. Not tested on hardware, only compile-tested with W=1 on next-20261116.

Fixes dacf0acfe2ce ("mt76: sdio: extend sdio module to support CONNAC2")

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

 drivers/net/wireless/mediatek/mt76/sdio_txrx.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/net/wireless/mediatek/mt76/sdio_txrx.c +2 -0
index 3f314e8e1e69f92..86dd1165d2c7f1f 100644
@@ -127,6 +127,8 @@ mt76s_rx_run_queue(struct mt76_dev *dev, enum mt76_rxq_id qid,
 
 		/* parse rxd to get the actual packet length */
 		len = le32_get_bits(rxd[0], GENMASK(15, 0));
+		if (len > end - buf)
+			break;
 
 		/* Optimized path for TXS */
 		if (!dev->drv->rx_check || dev->drv->rx_check(dev, buf, len)) {
Quoted 4 lines

base-commit: 7e1392a42ea288d70a266f6553fe06f57f8647ea

Signature · 1 line
2.34.1

I do not think this patch is correct since we actually consume just len - MT_SKB_HEAD_LEN bytes at most, right? I guess we should add something similar to the patch below, what do you think?

Regards,
Lorenzo

drivers/net/wireless/mediatek/mt76/sdio_txrx.c +3 -0
index 3f314e8e1e69..4f01d239e0ac 100644
@@ -62,6 +62,9 @@ mt76s_build_rx_skb(void *data, int data_len, int buf_len)
 	int len = min_t(int, data_len, MT_SKB_HEAD_LEN);
 	struct sk_buff *skb;
 
+	if (data_len > len && data_len - len > buf_len)
+		return NULL;
+
 	skb = alloc_skb(len, GFP_KERNEL);
 	if (!skb)
 		return NULL;
author
#2 lore raw

Hi Lorenzo,

Quoted 3 lines · Lorenzo Bianconi

I do not think this patch is correct since we actually consume just len - MT_SKB_HEAD_LEN bytes at most, right? I guess we should add something similar to the patch below, what do you think?

Thanks for looking at this.

The fragment is len - MT_SKB_HEAD_LEN bytes, yes. But mt76s_build_rx_skb() first copies up to MT_SKB_HEAD_LEN bytes from buf into the skb head, so a frame still needs all len bytes between buf and end.

drivers/net/wireless/mediatek/mt76/sdio_txrx.c mt76s_build_rx_skb(void *data, int data_len, int buf_len) · line 66

Quoted 2 lines
drivers/net/wireless/mediatek/mt76/sdio_txrx.c +2 -0
+     if (data_len > len && data_len - len > buf_len)
+             return NULL;

The caller passes buf_len = round_up(data_len + 4, 4), so this check never triggers. With it applied, my harness still reports the same reads past the buffer.

Also, rx_check() runs before mt76s_build_rx_skb(). For a TXS frame, mt7921_rx_check() and mt7615_rx_check() walk the records up to rxd + len / 4, which a check in mt76s_build_rx_skb() cannot cover.

So I think we should keep the check before rx_check(), as in this patch. Frames that fit are not affected, and the loop already ends at a frame that does not fit.

Thanks,
Maoyi

maintainer
#3 lore raw
Quoted 7 lines · Maoyi Xie

Hi Lorenzo,

Quoted 3 lines · Lorenzo Bianconi

I do not think this patch is correct since we actually consume just len - MT_SKB_HEAD_LEN bytes at most, right? I guess we should add something similar to the patch below, what do you think?

Quoted 6 lines

Thanks for looking at this.

The fragment is len - MT_SKB_HEAD_LEN bytes, yes. But mt76s_build_rx_skb() first copies up to MT_SKB_HEAD_LEN bytes from buf into the skb head, so a frame still needs all len bytes between buf and end.

ack, you right.

Quoted 8 lines

drivers/net/wireless/mediatek/mt76/sdio_txrx.c mt76s_build_rx_skb(void *data, int data_len, int buf_len) · line 66

Quoted 3 lines
drivers/net/wireless/mediatek/mt76/sdio_txrx.c +2 -0
+     if (data_len > len && data_len - len > buf_len)
+             return NULL;

The caller passes buf_len = round_up(data_len + 4, 4), so this check never triggers. With it applied, my harness still reports the same reads past the buffer.

Quoted 7 lines

Also, rx_check() runs before mt76s_build_rx_skb(). For a TXS frame, mt7921_rx_check() and mt7615_rx_check() walk the records up to rxd + len / 4, which a check in mt76s_build_rx_skb() cannot cover.

So I think we should keep the check before rx_check(), as in this patch. Frames that fit are not affected, and the loop already ends at a frame that does not fit.

ack, I agree. Thx for fixing it.

Regards,
Lorenzo

Quoted 3 lines

Thanks,
Maoyi