Skip to contentSkip to conversation

net: wwan: mhi_wwan_mbim: guard against a cyclic NDP chain

PATCH Superseded Guanglei Zhu 3 participants 9 messages Sep 10 – Sep 17

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/3 net: wwan: mhi_wwan_mbim: guard against a cyclic NDP chain

The NDP traversal in mhi_mbim_rx() only stops when wNextNdpIndex is zero. Nothing requires the offsets to advance, so a modem that points an NDP at itself, or at an earlier NDP, keeps the loop spinning forever on one CPU.

Break out when the next NDP offset is not larger than the current one.

Fixes aa730a9905b7 ("net: wwan: Add MHI MBIM network driver")

---

Verified in a QEMU guest with a fault injector feeding the driver's receive callback an NTB whose single NDP points at itself: the unpatched driver spins in mhi_mbim_rx() with one CPU pinned at 100% and the thread never returns. With this check the loop terminates within one iteration.

 drivers/net/wwan/mhi_wwan_mbim.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/net/wwan/mhi_wwan_mbim.c +8 -0
index a94998712..ef158edeb 100644
@@ -254,6 +254,7 @@ static int mbim_rx_verify_ndp16(struct sk_buff *skb, struct usb_cdc_ncm_ndp16 *n
 static void mhi_mbim_rx(struct mhi_mbim_context *mbim, struct sk_buff *skb)
 {
 	int ndpoffset;
+	int last_ndpoffset = 0;
 
 	/* Check NTB header and retrieve first NDP offset */
 	ndpoffset = mbim_rx_verify_nth16(mbim, skb);
@@ -265,6 +266,13 @@ static void mhi_mbim_rx(struct mhi_mbim_context *mbim, struct sk_buff *skb)
 	/* Process each NDP */
 	while (1) {
 		struct usb_cdc_ncm_ndp16 ndp16;
+
+		if (ndpoffset <= last_ndpoffset) {
+			net_err_ratelimited("mbim: non-increasing NDP offset (%u)\n",
+					    ndpoffset);
+			break;
+		}
+		last_ndpoffset = ndpoffset;
 		struct usb_cdc_ncm_dpe16 dpe16;
 		struct mhi_mbim_link *link;
 		int nframes, n, dpeoffset;
Signature · 1 line
2.43.0
#1 lore raw

PATCH 2/3 net: wwan: mhi_wwan_mbim: check skb_copy_bits() return value

mhi_mbim_rx() ignores the return value of skb_copy_bits() when it copies each datagram out of the NTB. The datagram offset and length come from the DPE, which is only checked to lie within the NTB itself, so a modem can point a datagram outside the received skb. The copy then fails and the freshly allocated skbn is passed to netif_rx() with its uninitialized contents still in place, leaking kernel heap memory into the network stack.

Free the skb and account an error when the copy fails.

Fixes aa730a9905b7 ("net: wwan: Add MHI MBIM network driver")

---

Verified in a QEMU guest with a fault injector pointing a DPE outside the received NTB: the copy fails, and the unpatched driver hands the uninitialized skbn to the network stack (observed as "unknown protocol" on bytes that were never written). With this check the failed datagram is dropped and counted as an rx error.

 drivers/net/wwan/mhi_wwan_mbim.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/net/wwan/mhi_wwan_mbim.c +7 -1
index ef158edeb..71e700008 100644
@@ -328,7 +328,13 @@ static void mhi_mbim_rx(struct mhi_mbim_context *mbim, struct sk_buff *skb)
 				continue;
 
 			skb_put(skbn, dgram_len);
-			skb_copy_bits(skb, dgram_offset, skbn->data, dgram_len);
+			if (skb_copy_bits(skb, dgram_offset, skbn->data, dgram_len)) {
+				dev_kfree_skb_any(skbn);
+				u64_stats_update_begin(&link->rx_syncp);
+				u64_stats_inc(&link->rx_errors);
+				u64_stats_update_end(&link->rx_syncp);
+				continue;
+			}
 
 			switch (skbn->data[0] & 0xf0) {
 			case 0x40:
Signature · 1 line
2.43.0
maintainer
#2 lore raw

Re: [PATCH 2/3] net: wwan: mhi_wwan_mbim: check skb_copy_bits() return value

Quoted 29 lines · Guanglei Zhu

mhi_mbim_rx() ignores the return value of skb_copy_bits() when it copies each datagram out of the NTB. The datagram offset and length come from the DPE, which is only checked to lie within the NTB itself, so a modem can point a datagram outside the received skb. The copy then fails and the freshly allocated skbn is passed to netif_rx() with its uninitialized contents still in place, leaking kernel heap memory into the network stack.

Free the skb and account an error when the copy fails.

Fixes aa730a9905b7 ("net: wwan: Add MHI MBIM network driver")

---

Verified in a QEMU guest with a fault injector pointing a DPE outside the received NTB: the copy fails, and the unpatched driver hands the uninitialized skbn to the network stack (observed as "unknown protocol" on bytes that were never written). With this check the failed datagram is dropped and counted as an rx error.

 drivers/net/wwan/mhi_wwan_mbim.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/net/wwan/mhi_wwan_mbim.c +0 -0
index ef158edeb..71e700008 100644

drivers/net/wwan/mhi_wwan_mbim.c static void mhi_mbim_rx(struct mhi_mbim_context *mbim, struct sk_buff *skb) · line 336

Quoted 10 lines
drivers/net/wwan/mhi_wwan_mbim.c +6 -1
@@ -328,7 +328,13 @@ static void mhi_mbim_rx(struct mhi_mbim_context *mbim, struct sk_buff *skb)
                                continue;
 
                        skb_put(skbn, dgram_len);
-                       skb_copy_bits(skb, dgram_offset, skbn->data, dgram_len);
+                       if (skb_copy_bits(skb, dgram_offset, skbn->data, dgram_len)) {
+                               dev_kfree_skb_any(skbn);
+                               u64_stats_update_begin(&link->rx_syncp);
+                               u64_stats_inc(&link->rx_errors);
+                               u64_stats_update_end(&link->rx_syncp);
+                               continue;

We perform the same error handling in the switch cases below, so it would be better to factor it out into a common helper or a shared error path.

Quoted 7 lines
drivers/net/wwan/mhi_wwan_mbim.c +1 -1
+                       }
 
                        switch (skbn->data[0] & 0xf0) {
                        case 0x40:
--

2.43.0

#3 lore raw

PATCH 3/3 net: wwan: t7xx: validate the netif index in t7xx_ccmni_recv_skb()

In reply to Guanglei Zhu

The netif index carried in the DPMAIF PIT header is five bits wide, but ccmni_inst[] only has room for NIC_DEV_MAX (21) entries. t7xx_ccmni_recv_skb() indexes the array without a bounds check, so indexes 21 to 31 read past it. The out-of-bounds value lands in the callback table that follows the array, which is never NULL, so the existing !ccmni check does not catch it and the driver dereferences whatever sits there as a struct t7xx_ccmni.

Drop the skb when the index is out of range.

Fixes 05d19bf500f8 ("net: wwan: t7xx: Add WWAN network interface")

---

Verified in a QEMU guest with a fault injector setting the netif index to 25: the unpatched driver reads a value past ccmni_inst[], which lands in the callback table, and dereferences it far enough to queue the skb. With this check the packet is dropped. Well-formed traffic on index 0 is unaffected.

 drivers/net/wwan/t7xx/t7xx_netdev.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/net/wwan/t7xx/t7xx_netdev.c +4 -0
index fc0a7cb18..8f32c2d26 100644
@@ -420,6 +420,10 @@ static void t7xx_ccmni_recv_skb(struct t7xx_ccmni_ctrl *ccmni_ctlb, struct sk_bu
 
 	skb_cb = T7XX_SKB_CB(skb);
 	netif_id = skb_cb->netif_idx;
+	if (netif_id >= NIC_DEV_MAX) {
+		dev_kfree_skb(skb);
+		return;
+	}
 	ccmni = READ_ONCE(ccmni_ctlb->ccmni_inst[netif_id]);
 	if (!ccmni) {
 		dev_kfree_skb(skb);
Signature · 1 line
2.43.0
maintainer
#4 lore raw
In reply to Guanglei Zhu
Quoted 35 lines · Guanglei Zhu

The NDP traversal in mhi_mbim_rx() only stops when wNextNdpIndex is zero. Nothing requires the offsets to advance, so a modem that points an NDP at itself, or at an earlier NDP, keeps the loop spinning forever on one CPU.

Break out when the next NDP offset is not larger than the current one.

Fixes aa730a9905b7 ("net: wwan: Add MHI MBIM network driver")

---

Verified in a QEMU guest with a fault injector feeding the driver's receive callback an NTB whose single NDP points at itself: the unpatched driver spins in mhi_mbim_rx() with one CPU pinned at 100% and the thread never returns. With this check the loop terminates within one iteration.

 drivers/net/wwan/mhi_wwan_mbim.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/net/wwan/mhi_wwan_mbim.c +1 -0
index a94998712..ef158edeb 100644
@@ -254,6 +254,7 @@ static int mbim_rx_verify_ndp16(struct sk_buff *skb, struct usb_cdc_ncm_ndp16 *n
 static void mhi_mbim_rx(struct mhi_mbim_context *mbim, struct sk_buff *skb)
 {
        int ndpoffset;
+       int last_ndpoffset = 0;
 
        /* Check NTB header and retrieve first NDP offset */
        ndpoffset = mbim_rx_verify_nth16(mbim, skb);

drivers/net/wwan/mhi_wwan_mbim.c static void mhi_mbim_rx(struct mhi_mbim_context *mbim, struct sk_buff *skb) · line 275

Quoted 10 lines
drivers/net/wwan/mhi_wwan_mbim.c +7 -0
@@ -265,6 +266,13 @@ static void mhi_mbim_rx(struct mhi_mbim_context *mbim, struct sk_buff *skb)
        /* Process each NDP */
        while (1) {
                struct usb_cdc_ncm_ndp16 ndp16;
+
+               if (ndpoffset <= last_ndpoffset) {
+                       net_err_ratelimited("mbim: non-increasing NDP offset (%u)\n",
+                                           ndpoffset);
+                       break;
+               }
+               last_ndpoffset = ndpoffset;

It would be better to address this under the next_ndp retrieval, something like:

```
n = (int)le16_to_cpu(ndp16.wNextNdpIndex);
if (n > ndpoffset)
   ndpoffset = n;
else
  break;
```
Quoted 6 lines
drivers/net/wwan/mhi_wwan_mbim.c +0 -1
                struct usb_cdc_ncm_dpe16 dpe16;
                struct mhi_mbim_link *link;
                int nframes, n, dpeoffset;
--

2.43.0

#5 lore raw

PATCH v2 1/3 net: wwan: mhi_wwan_mbim: guard against a cyclic NDP chain

In reply to Guanglei Zhu

The NDP traversal in mhi_mbim_rx() only stops when wNextNdpIndex is zero. Nothing requires the offsets to advance, so a modem that points an NDP at itself, or at an earlier NDP, keeps the loop spinning forever on one CPU.

Break out when the next NDP offset is not larger than the current one.

Fixes aa730a9905b7 ("net: wwan: Add MHI MBIM network driver")

Verified in a QEMU guest with a fault injector feeding the driver's receive callback an NTB whose single NDP points at itself: the unpatched driver spins in mhi_mbim_rx() with one CPU pinned at 100% and the thread never returns. With this check the loop terminates within one iteration.

Changes in v2: move the non-increasing check to the wNextNdpIndex retrieval site, as suggested by Loic Poulain, instead of tracking the previous offset in a separate variable.

---
 drivers/net/wwan/mhi_wwan_mbim.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/net/wwan/mhi_wwan_mbim.c +7 -3
index a949987125..5679948546 100644
@@ -349,9 +349,13 @@ static void mhi_mbim_rx(struct mhi_mbim_context *mbim, struct sk_buff *skb)
 unlock:
 		rcu_read_unlock();
 next_ndp:
-		/* Other NDP to process? */
-		ndpoffset = (int)le16_to_cpu(ndp16.wNextNdpIndex);
-		if (!ndpoffset)
+		/* Other NDP to process?  The offsets must advance, or a
+		 * self-referencing NDP keeps the loop spinning forever.
+		 */
+		n = (int)le16_to_cpu(ndp16.wNextNdpIndex);
+		if (n > ndpoffset)
+			ndpoffset = n;
+		else
 			break;
 	}
 
Signature · 1 line
2.43.0
#6 lore raw

PATCH v2 2/3 net: wwan: mhi_wwan_mbim: check skb_copy_bits() return value

mhi_mbim_rx() ignores the return value of skb_copy_bits() when it copies each datagram out of the NTB. The datagram offset and length come from the DPE, which is only checked to lie within the NTB itself, so a modem can point a datagram outside the received skb. The copy then fails and the freshly allocated skbn is passed to netif_rx() with its uninitialized contents still in place, leaking kernel heap memory into the network stack.

Free the skb and account an error when the copy fails.

Fixes aa730a9905b7 ("net: wwan: Add MHI MBIM network driver")

Verified in a QEMU guest with a fault injector pointing a DPE outside the received NTB: the copy fails, and the unpatched driver hands the uninitialized skbn to the network stack (observed as "unknown protocol" on bytes that were never written). With this check the failed datagram is dropped and counted as an rx error.

Changes in v2: factor the free-and-count sequence out into mhi_mbim_rx_drop(), shared with the unknown-protocol path, as suggested by Loic Poulain.

---
 drivers/net/wwan/mhi_wwan_mbim.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)
drivers/net/wwan/mhi_wwan_mbim.c +13 -5
index 5679948546..336f89756f 100644
@@ -251,6 +251,14 @@ static int mbim_rx_verify_ndp16(struct sk_buff *skb, struct usb_cdc_ncm_ndp16 *n
 	return ret;
 }
 
+static void mhi_mbim_rx_drop(struct mhi_mbim_link *link, struct sk_buff *skb)
+{
+	dev_kfree_skb_any(skb);
+	u64_stats_update_begin(&link->rx_syncp);
+	u64_stats_inc(&link->rx_errors);
+	u64_stats_update_end(&link->rx_syncp);
+}
+
 static void mhi_mbim_rx(struct mhi_mbim_context *mbim, struct sk_buff *skb)
 {
 	int ndpoffset;
@@ -320,7 +328,10 @@ static void mhi_mbim_rx(struct mhi_mbim_context *mbim, struct sk_buff *skb)
 				continue;
 
 			skb_put(skbn, dgram_len);
-			skb_copy_bits(skb, dgram_offset, skbn->data, dgram_len);
+			if (skb_copy_bits(skb, dgram_offset, skbn->data, dgram_len)) {
+				mhi_mbim_rx_drop(link, skbn);
+				continue;
+			}
 
 			switch (skbn->data[0] & 0xf0) {
 			case 0x40:
@@ -332,10 +343,7 @@ static void mhi_mbim_rx(struct mhi_mbim_context *mbim, struct sk_buff *skb)
 			default:
 				net_err_ratelimited("%s: unknown protocol\n",
 						    link->ndev->name);
-				dev_kfree_skb_any(skbn);
-				u64_stats_update_begin(&link->rx_syncp);
-				u64_stats_inc(&link->rx_errors);
-				u64_stats_update_end(&link->rx_syncp);
+				mhi_mbim_rx_drop(link, skbn);
 				continue;
 			}
 
Signature · 1 line
2.43.0
#7 lore raw

PATCH v2 3/3 net: wwan: t7xx: validate the netif index in t7xx_ccmni_recv_skb()

In reply to Guanglei Zhu

The netif index carried in the DPMAIF PIT header is five bits wide, but ccmni_inst[] only has room for NIC_DEV_MAX (21) entries. t7xx_ccmni_recv_skb() indexes the array without a bounds check, so indexes 21 to 31 read past it. The out-of-bounds value lands in the callback table that follows the array, which is never NULL, so the existing !ccmni check does not catch it and the driver dereferences whatever sits there as a struct t7xx_ccmni.

Drop the skb when the index is out of range.

Fixes 05d19bf500f8 ("net: wwan: t7xx: Add WWAN network interface")

Verified in a QEMU guest with a fault injector setting the netif index to 25: the unpatched driver reads a value past ccmni_inst[], which lands in the callback table, and dereferences it far enough to queue the skb. With this check the packet is dropped. Well-formed traffic on index 0 is unaffected.

Changes in v2: none.

---
 drivers/net/wwan/t7xx/t7xx_netdev.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/net/wwan/t7xx/t7xx_netdev.c +4 -0
index fc0a7cb181..8f32c2d269 100644
@@ -420,6 +420,10 @@ static void t7xx_ccmni_recv_skb(struct t7xx_ccmni_ctrl *ccmni_ctlb, struct sk_bu
 
 	skb_cb = T7XX_SKB_CB(skb);
 	netif_id = skb_cb->netif_idx;
+	if (netif_id >= NIC_DEV_MAX) {
+		dev_kfree_skb(skb);
+		return;
+	}
 	ccmni = READ_ONCE(ccmni_ctlb->ccmni_inst[netif_id]);
 	if (!ccmni) {
 		dev_kfree_skb(skb);
Signature · 1 line
2.43.0
#8 lore raw
In reply to Guanglei Zhu
This series was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:

Quoted 9 lines · On Fri, 11 Sep 2026 10:17:32 +0800 you wrote:

The NDP traversal in mhi_mbim_rx() only stops when wNextNdpIndex is zero. Nothing requires the offsets to advance, so a modem that points an NDP at itself, or at an earlier NDP, keeps the loop spinning forever on one CPU.

Break out when the next NDP offset is not larger than the current one.

[...]

Here is the summary with links:
- [v2,1/3] net: wwan: mhi_wwan_mbim: guard against a cyclic NDP chain

    https://git.kernel.org/netdev/net/c/5d063822ac51
  - [v2,2/3] net: wwan: mhi_wwan_mbim: check skb_copy_bits() return value
    https://git.kernel.org/netdev/net/c/31550d585589
  - [v2,3/3] net: wwan: t7xx: validate the netif index in t7xx_ccmni_recv_skb()
    https://git.kernel.org/netdev/net/c/c7ead9704249

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html