Skip to contentSkip to conversation

net: dl2k: fix potential null deref in receive_packet()

PATCH In review Qasim Ijaz 2 participants 3 messages Mar 21, 2025

Subsystems (2)
lore
Share & tools
lore

Qasim Ijaz

author
#0 lore raw

PATCH 1/1 net: dl2k: fix potential null deref in receive_packet()

If the pkt_len is less than the copy_thresh the netdev_alloc_skb_ip_align() is called to allocate an skbuff, on failure it can return NULL. Since there is no NULL check a NULL deref can occur when setting skb->protocol.

Fix this by introducing a NULL check to handle allocation failure.

Fixes 89d71a66c40d ("net: Use netdev_alloc_skb_ip_align()")
---
 drivers/net/ethernet/dlink/dl2k.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/net/ethernet/dlink/dl2k.c +5 -0
index d0ea92607870..22e9432adea0 100644
@@ -968,6 +968,11 @@ receive_packet (struct net_device *dev)
 							   np->rx_buf_sz,
 							   DMA_FROM_DEVICE);
 			}
+
+			if (unlikely(!skb)) {
+				np->rx_ring[entry].fraginfo = 0;
+				break;
+			}
 			skb->protocol = eth_type_trans (skb, dev);
 #if 0
 			/* Checksum done by hw, but csum value unavailable. */
Signature · 1 line
2.39.5
#1 lore raw
Quoted 9 lines · Qasim Ijaz

If the pkt_len is less than the copy_thresh the netdev_alloc_skb_ip_align() is called to allocate an skbuff, on failure it can return NULL. Since there is no NULL check a NULL deref can occur when setting skb->protocol.

Fix this by introducing a NULL check to handle allocation failure.

Fixes 89d71a66c40d ("net: Use netdev_alloc_skb_ip_align()")

This commit has not changed the behavior in case of memory alloc error.

Quoted 10 lines
---
 drivers/net/ethernet/dlink/dl2k.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/net/ethernet/dlink/dl2k.c +0 -0
index d0ea92607870..22e9432adea0 100644

drivers/net/ethernet/dlink/dl2k.c receive_packet (struct net_device *dev) · line 973

Quoted 6 lines
drivers/net/ethernet/dlink/dl2k.c +3 -0
@@ -968,6 +968,11 @@ receive_packet (struct net_device *dev)
                                                           np->rx_buf_sz,
                                                           DMA_FROM_DEVICE);
                        }
+
+                       if (unlikely(!skb)) {
+                               np->rx_ring[entry].fraginfo = 0;

Not sure how this was tested...

I think this would leak memory.

Quoted 7 lines
drivers/net/ethernet/dlink/dl2k.c +2 -1
+                               break;
+                       }
                        skb->protocol = eth_type_trans (skb, dev);
 #if 0
                        /* Checksum done by hw, but csum value unavailable. */
--

2.39.5

author
#2 lore raw
Quoted 7 lines · Eric Dumazet
Quoted 6 lines · Qasim Ijaz

If the pkt_len is less than the copy_thresh the netdev_alloc_skb_ip_align() is called to allocate an skbuff, on failure it can return NULL. Since there is no NULL check a NULL deref can occur when setting skb->protocol.

Quoted 6 lines
Quoted 3 lines

Fix this by introducing a NULL check to handle allocation failure.

Fixes 89d71a66c40d ("net: Use netdev_alloc_skb_ip_align()")

This commit has not changed the behavior in case of memory alloc error.

Hi Eric,

Thanks for pointing this out, I referenced this commit because it added the netdev_alloc_skb_ip_align() call without ensuring the result of it succeeds or not. Before this change the code used netdev_alloc_skb(), so I now see that there is no functional difference since an allocation occurs in both versions.

Quoted 19 lines
Quoted 10 lines
---
 drivers/net/ethernet/dlink/dl2k.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/net/ethernet/dlink/dl2k.c +0 -0
index d0ea92607870..22e9432adea0 100644

drivers/net/ethernet/dlink/dl2k.c receive_packet (struct net_device *dev) · line 973

Quoted 6 lines
drivers/net/ethernet/dlink/dl2k.c +3 -0
@@ -968,6 +968,11 @@ receive_packet (struct net_device *dev)
                                                           np->rx_buf_sz,
                                                           DMA_FROM_DEVICE);
                        }
+
+                       if (unlikely(!skb)) {
+                               np->rx_ring[entry].fraginfo = 0;

Not sure how this was tested...

Quoted 1 line

I think this would leak memory.

Could you please elaborate on where you think this may arise so I can investigate further and try to amend it?

Thanks
Qasim

Quoted 8 lines
Quoted 8 lines
drivers/net/ethernet/dlink/dl2k.c +2 -1
+                               break;
+                       }
                        skb->protocol = eth_type_trans (skb, dev);
 #if 0
                        /* Checksum done by hw, but csum value unavailable. */
--

2.39.5