Skip to contentSkip to conversation

net: usb: smsc95xx: Limit packet length to skb->len

PATCH Superseded Szymon Heidrich 3 participants 6 messages Mar 13, 2023 – Mar 18, 2023

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

PATCH 1/1 net: usb: smsc95xx: Limit packet length to skb->len

Packet length retrieved from skb data may be larger than the actual socket buffer length (up to 1526 bytes). In such case the cloned skb passed up the network stack will leak kernel memory contents.

Fixes 2f7ca802bdae ("net: Add SMSC LAN9500 USB2.0 10/100 ethernet adapter driver")
---
 drivers/net/usb/smsc95xx.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/usb/smsc95xx.c +2 -1
index 32d2c60d3..ba766bdb2 100644
@@ -1851,7 +1851,8 @@ static int smsc95xx_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
 			}
 		} else {
 			/* ETH_FRAME_LEN + 4(CRC) + 2(COE) + 4(Vlan) */
-			if (unlikely(size > (ETH_FRAME_LEN + 12))) {
+			if (unlikely(size > (ETH_FRAME_LEN + 12) ||
+				     size > skb->len)) {
 				netif_dbg(dev, rx_err, dev->net,
 					  "size err header=0x%08x\n", header);
 				return 0;
Signature · 1 line
2.39.2
maintainer
#1 lore raw
Quoted 1 line · On Mon, 13 Mar 2023 23:01:24 +0100 Szymon Heidrich wrote:

Packet length retrieved from skb data may be larger than

nit: s/skb data/descriptor/ may be better in terms of terminology

Quoted 1 line

the actual socket buffer length (up to 1526 bytes). In such

nit: the "up to 1526 bytes" is a bit confusing, I'd remove it.

Quoted 16 lines

case the cloned skb passed up the network stack will leak kernel memory contents.

Fixes 2f7ca802bdae ("net: Add SMSC LAN9500 USB2.0 10/100 ethernet adapter driver")
---
 drivers/net/usb/smsc95xx.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/usb/smsc95xx.c +0 -0
index 32d2c60d3..ba766bdb2 100644

drivers/net/usb/smsc95xx.c static int smsc95xx_rx_fixup(struct usbnet *dev, struct sk_buff *skb) · line 1855

Quoted 6 lines
drivers/net/usb/smsc95xx.c +2 -1
@@ -1851,7 +1851,8 @@ static int smsc95xx_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
 			}
 		} else {
 			/* ETH_FRAME_LEN + 4(CRC) + 2(COE) + 4(Vlan) */
-			if (unlikely(size > (ETH_FRAME_LEN + 12))) {
+			if (unlikely(size > (ETH_FRAME_LEN + 12) ||
+				     size > skb->len)) {

We need this check on both sides of the big if {} statement.

In case the error bit is set and we drop the packet we still end up in skb_pull() which if size > skb->len will panic the kernel.

So let's do this check right after size and align are calculated? The patch for smsc75xx has sadly already been applied so you'll need to prepare a fix to the fix :(

Quoted 3 lines
drivers/net/usb/smsc95xx.c +0 -0
 				netif_dbg(dev, rx_err, dev->net,
 					  "size err header=0x%08x\n", header);
 				return 0;
#2 lore raw

PATCH v2 1/1 net: usb: smsc95xx: Limit packet length to skb->len

Packet length retrieved from descriptor may be larger than the actual socket buffer length. In such case the cloned skb passed up the network stack will leak kernel memory contents.

Fixes 2f7ca802bdae ("net: Add SMSC LAN9500 USB2.0 10/100 ethernet adapter driver")

---
V1 -> V2: Move packet length check to prevent kernel panic in skb_pull

 drivers/net/usb/smsc95xx.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/net/usb/smsc95xx.c +6 -0
index 32d2c60d3..563ecd27b 100644
@@ -1833,6 +1833,12 @@ static int smsc95xx_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
 		size = (u16)((header & RX_STS_FL_) >> 16);
 		align_count = (4 - ((size + NET_IP_ALIGN) % 4)) % 4;
 
+		if (unlikely(size > skb->len)) {
+			netif_dbg(dev, rx_err, dev->net,
+				  "size err header=0x%08x\n", header);
+			return 0;
+		}
+
 		if (unlikely(header & RX_STS_ES_)) {
 			netif_dbg(dev, rx_err, dev->net,
 				  "Error header=0x%08x\n", header);
Signature · 1 line
2.40.0
maintainer Reviewed-by on v2 1/1
#3 lore raw
Quoted 6 lines · On Thu, 16 Mar 2023 11:19:54 +0100 Szymon Heidrich wrote:

Packet length retrieved from descriptor may be larger than the actual socket buffer length. In such case the cloned skb passed up the network stack will leak kernel memory contents.

Fixes 2f7ca802bdae ("net: Add SMSC LAN9500 USB2.0 10/100 ethernet adapter driver")
#4 lore raw
In reply to Szymon Heidrich
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:

Hello:

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

Quoted 8 lines · On Thu, 16 Mar 2023 11:19:54 +0100 you wrote:

Packet length retrieved from descriptor may be larger than the actual socket buffer length. In such case the cloned skb passed up the network stack will leak kernel memory contents.

Fixes 2f7ca802bdae ("net: Add SMSC LAN9500 USB2.0 10/100 ethernet adapter driver")

[...]

Here is the summary with links:
- [v2] net: usb: smsc95xx: Limit packet length to skb->len

    https://git.kernel.org/netdev/net/c/ff821092cf02

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
#5 lore raw
In reply to Jakub Kicinski
Quoted 34 lines · Jakub Kicinski
Quoted 1 line · On Mon, 13 Mar 2023 23:01:24 +0100 Szymon Heidrich wrote:

Packet length retrieved from skb data may be larger than

nit: s/skb data/descriptor/ may be better in terms of terminology

Quoted 1 line

the actual socket buffer length (up to 1526 bytes). In such

nit: the "up to 1526 bytes" is a bit confusing, I'd remove it.

Quoted 16 lines

case the cloned skb passed up the network stack will leak kernel memory contents.

Fixes 2f7ca802bdae ("net: Add SMSC LAN9500 USB2.0 10/100 ethernet adapter driver")
---
 drivers/net/usb/smsc95xx.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/usb/smsc95xx.c +0 -0
index 32d2c60d3..ba766bdb2 100644

drivers/net/usb/smsc95xx.c static int smsc95xx_rx_fixup(struct usbnet *dev, struct sk_buff *skb) · line 1855

Quoted 6 lines
drivers/net/usb/smsc95xx.c +2 -1
@@ -1851,7 +1851,8 @@ static int smsc95xx_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
 			}
 		} else {
 			/* ETH_FRAME_LEN + 4(CRC) + 2(COE) + 4(Vlan) */
-			if (unlikely(size > (ETH_FRAME_LEN + 12))) {
+			if (unlikely(size > (ETH_FRAME_LEN + 12) ||
+				     size > skb->len)) {

We need this check on both sides of the big if {} statement.

Quoted 12 lines

In case the error bit is set and we drop the packet we still end up in skb_pull() which if size > skb->len will panic the kernel.

So let's do this check right after size and align are calculated? The patch for smsc75xx has sadly already been applied so you'll need to prepare a fix to the fix :(

Quoted 3 lines
drivers/net/usb/smsc95xx.c +0 -0
 				netif_dbg(dev, rx_err, dev->net,
 					  "size err header=0x%08x\n", header);
 				return 0;

Thank you very much for you suggestions Kuba, I provided an updated patch for smsc95xx and a new patch addressing smsc75xx. Please let me know in case additional amendments are required.