Skip to contentSkip to conversation

net: ti: icssg-prueth: fix missing data copy and wrong recycle in ZC RX dispatch

PATCH In review David Carlier 2 participants 5 messages Mar 24 – Mar 25

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

PATCH 1/1 net: ti: icssg-prueth: fix missing data copy and wrong recycle in ZC RX dispatch

emac_dispatch_skb_zc() allocates a new skb via napi_alloc_skb() but never copies the packet data from the XDP buffer into it. The skb is passed up the stack containing uninitialized heap memory instead of the actual received packet, leaking kernel heap contents to userspace.

Add the missing memcpy from xdp->data into the skb data area.

Additionally, remove the skb_mark_for_recycle() call since the skb is backed by the NAPI page frag allocator, not page_pool. Marking a non-page_pool skb for recycle causes the free path to return pages to a page_pool that does not own them, corrupting page_pool state.

The non-ZC path (emac_rx_packet) does not have these issues because it uses napi_build_skb() to wrap the existing page_pool page directly, requiring no copy, and correctly marks for recycle since the page comes from page_pool_dev_alloc_pages().

Fixes 7a64bb388df3 ("net: ti: icssg-prueth: Add AF_XDP zero copy for RX")
---
 drivers/net/ethernet/ti/icssg/icssg_common.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/ti/icssg/icssg_common.c +1 -1
index fd4e7622f123..9638a03bebb4 100644
@@ -902,6 +902,7 @@ static void emac_dispatch_skb_zc(struct prueth_emac *emac, struct xdp_buff *xdp,
 
 	skb_reserve(skb, headroom);
 	skb_put(skb, pkt_len);
+	memcpy(skb->data, xdp->data, pkt_len);
 	skb->dev = ndev;
 
 	/* RX HW timestamp */
@@ -912,7 +913,6 @@ static void emac_dispatch_skb_zc(struct prueth_emac *emac, struct xdp_buff *xdp,
 		skb->offload_fwd_mark = emac->offload_fwd_mark;
 	skb->protocol = eth_type_trans(skb, ndev);
 
-	skb_mark_for_recycle(skb);
 	napi_gro_receive(&emac->napi_rx, skb);
 	ndev->stats.rx_bytes += pkt_len;
 	ndev->stats.rx_packets++;
Signature · 1 line
2.53.0
#1 lore raw

Hi David,

Quoted 12 lines · David Carlier

emac_dispatch_skb_zc() allocates a new skb via napi_alloc_skb() but never copies the packet data from the XDP buffer into it. The skb is passed up the stack containing uninitialized heap memory instead of the actual received packet, leaking kernel heap contents to userspace.

Add the missing memcpy from xdp->data into the skb data area.

Additionally, remove the skb_mark_for_recycle() call since the skb is backed by the NAPI page frag allocator, not page_pool. Marking a non-page_pool skb for recycle causes the free path to return pages to a page_pool that does not own them, corrupting page_pool state.

Quoted 5 lines

The non-ZC path (emac_rx_packet) does not have these issues because it uses napi_build_skb() to wrap the existing page_pool page directly, requiring no copy, and correctly marks for recycle since the page comes from page_pool_dev_alloc_pages().

Thanks for the patch. I wonder how were you able to catch this bug?

Quoted 11 lines
Fixes 7a64bb388df3 ("net: ti: icssg-prueth: Add AF_XDP zero copy for RX")
---
  drivers/net/ethernet/ti/icssg/icssg_common.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/ti/icssg/icssg_common.c +0 -0
index fd4e7622f123..9638a03bebb4 100644

drivers/net/ethernet/ti/icssg/icssg_common.c static void emac_dispatch_skb_zc(struct prueth_emac *emac, struct xdp_buff *xdp, · line 905

Quoted 4 lines
drivers/net/ethernet/ti/icssg/icssg_common.c +1 -0
@@ -902,6 +902,7 @@ static void emac_dispatch_skb_zc(struct prueth_emac *emac, struct xdp_buff *xdp,
 
  	skb_reserve(skb, headroom);
  	skb_put(skb, pkt_len);
+	memcpy(skb->data, xdp->data, pkt_len);

Why not use skb_copy_to_linear_data() ?

Quoted 11 lines
drivers/net/ethernet/ti/icssg/icssg_common.c +0 -1
  	skb->dev = ndev;
 
  	/* RX HW timestamp */
@@ -912,7 +913,6 @@ static void emac_dispatch_skb_zc(struct prueth_emac *emac, struct xdp_buff *xdp,
  		skb->offload_fwd_mark = emac->offload_fwd_mark;
  	skb->protocol = eth_type_trans(skb, ndev);
 
-	skb_mark_for_recycle(skb);
  	napi_gro_receive(&emac->napi_rx, skb);
  	ndev->stats.rx_bytes += pkt_len;
  	ndev->stats.rx_packets++;
#2 lore raw

Hi,

Quoted 16 lines · Meghana Malladi

Hi David,

Quoted 12 lines · David Carlier

emac_dispatch_skb_zc() allocates a new skb via napi_alloc_skb() but never copies the packet data from the XDP buffer into it. The skb is passed up the stack containing uninitialized heap memory instead of the actual received packet, leaking kernel heap contents to userspace.

Add the missing memcpy from xdp->data into the skb data area.

Additionally, remove the skb_mark_for_recycle() call since the skb is backed by the NAPI page frag allocator, not page_pool. Marking a non-page_pool skb for recycle causes the free path to return pages to a page_pool that does not own them, corrupting page_pool state.

Quoted 7 lines
Quoted 5 lines

The non-ZC path (emac_rx_packet) does not have these issues because it uses napi_build_skb() to wrap the existing page_pool page directly, requiring no copy, and correctly marks for recycle since the page comes from page_pool_dev_alloc_pages().

Thanks for the patch. I wonder how were you able to catch this bug?

By code review, comparing emac_dispatch_skb_zc() with the non-ZC path emac_rx_packet() and noticing the ZC path allocates a separate skb but never copies data into it.

Quoted 13 lines
Quoted 13 lines
Fixes 7a64bb388df3 ("net: ti: icssg-prueth: Add AF_XDP zero copy for RX")
---
  drivers/net/ethernet/ti/icssg/icssg_common.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/ti/icssg/icssg_common.c +0 -0
index fd4e7622f123..9638a03bebb4 100644
@@ -902,6 +902,7 @@ static void emac_dispatch_skb_zc(struct prueth_emac *emac, struct xdp_buff *xdp,
 
Quoted 5 lines

drivers/net/ethernet/ti/icssg/icssg_common.c static void emac_dispatch_skb_zc(struct prueth_emac *emac, struct xdp_buff *xdp, · line 905

Quoted 3 lines
drivers/net/ethernet/ti/icssg/icssg_common.c +1 -0
      skb_reserve(skb, headroom);
      skb_put(skb, pkt_len);
+     memcpy(skb->data, xdp->data, pkt_len);

Why not use skb_copy_to_linear_data() ?

Ah, that is a good suggestion, did not know it.

Quoted 13 lines
Quoted 12 lines
drivers/net/ethernet/ti/icssg/icssg_common.c +0 -1
      skb->dev = ndev;
 
      /* RX HW timestamp */
@@ -912,7 +913,6 @@ static void emac_dispatch_skb_zc(struct prueth_emac *emac, struct xdp_buff *xdp,
              skb->offload_fwd_mark = emac->offload_fwd_mark;
      skb->protocol = eth_type_trans(skb, ndev);
 
-     skb_mark_for_recycle(skb);
      napi_gro_receive(&emac->napi_rx, skb);
      ndev->stats.rx_bytes += pkt_len;
      ndev->stats.rx_packets++;
#3 lore raw

PATCH 1/1 net: ti: icssg-prueth: fix missing data copy and wrong recycle in ZC RX dispatch

In reply to David Carlier

emac_dispatch_skb_zc() allocates a new skb via napi_alloc_skb() but never copies the packet data from the XDP buffer into it. The skb is passed up the stack containing uninitialized heap memory instead of the actual received packet, leaking kernel heap contents to userspace.

Add the missing memcpy from xdp->data into the skb data area.

Additionally, remove the skb_mark_for_recycle() call since the skb is backed by the NAPI page frag allocator, not page_pool. Marking a non-page_pool skb for recycle causes the free path to return pages to a page_pool that does not own them, corrupting page_pool state.

The non-ZC path (emac_rx_packet) does not have these issues because it uses napi_build_skb() to wrap the existing page_pool page directly, requiring no copy, and correctly marks for recycle since the page comes from page_pool_dev_alloc_pages().

Fixes 7a64bb388df3 ("net: ti: icssg-prueth: Add AF_XDP zero copy for RX")
---
 drivers/net/ethernet/ti/icssg/icssg_common.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/ti/icssg/icssg_common.c +1 -1
index fd4e7622f123..a28a608f9bf4 100644
@@ -902,6 +902,7 @@ static void emac_dispatch_skb_zc(struct prueth_emac *emac, struct xdp_buff *xdp,
 
 	skb_reserve(skb, headroom);
 	skb_put(skb, pkt_len);
+	skb_copy_to_linear_data(skb, xdp->data, pkt_len);
 	skb->dev = ndev;
 
 	/* RX HW timestamp */
@@ -912,7 +913,6 @@ static void emac_dispatch_skb_zc(struct prueth_emac *emac, struct xdp_buff *xdp,
 		skb->offload_fwd_mark = emac->offload_fwd_mark;
 	skb->protocol = eth_type_trans(skb, ndev);
 
-	skb_mark_for_recycle(skb);
 	napi_gro_receive(&emac->napi_rx, skb);
 	ndev->stats.rx_bytes += pkt_len;
 	ndev->stats.rx_packets++;
Signature · 1 line
2.53.0
#4 lore raw

Hi David,

I think you might be missing to put some maintainers in cc. Can you please check: https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/

Also check "resending after review" section here: https://docs.kernel.org/process/maintainer-netdev.html
Grace period of min 24 hours is needed for re-posting and "The new version of patches should be posted as a separate thread, not as a reply to the previous posting."

Quoted 7 lines · David Carlier

emac_dispatch_skb_zc() allocates a new skb via napi_alloc_skb() but never copies the packet data from the XDP buffer into it. The skb is passed up the stack containing uninitialized heap memory instead of the actual received packet, leaking kernel heap contents to userspace.

Add the missing memcpy from xdp->data into the skb data area.

You did make changes in the code as requested but the commit message is stale. Can you post a v2 with updated patch and commit message.

Quoted 36 lines

Additionally, remove the skb_mark_for_recycle() call since the skb is backed by the NAPI page frag allocator, not page_pool. Marking a non-page_pool skb for recycle causes the free path to return pages to a page_pool that does not own them, corrupting page_pool state.

The non-ZC path (emac_rx_packet) does not have these issues because it uses napi_build_skb() to wrap the existing page_pool page directly, requiring no copy, and correctly marks for recycle since the page comes from page_pool_dev_alloc_pages().

Fixes 7a64bb388df3 ("net: ti: icssg-prueth: Add AF_XDP zero copy for RX")
---
  drivers/net/ethernet/ti/icssg/icssg_common.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/ti/icssg/icssg_common.c +1 -1
index fd4e7622f123..a28a608f9bf4 100644
@@ -902,6 +902,7 @@ static void emac_dispatch_skb_zc(struct prueth_emac *emac, struct xdp_buff *xdp,
 
  	skb_reserve(skb, headroom);
  	skb_put(skb, pkt_len);
+	skb_copy_to_linear_data(skb, xdp->data, pkt_len);
  	skb->dev = ndev;
 
  	/* RX HW timestamp */
@@ -912,7 +913,6 @@ static void emac_dispatch_skb_zc(struct prueth_emac *emac, struct xdp_buff *xdp,
  		skb->offload_fwd_mark = emac->offload_fwd_mark;
  	skb->protocol = eth_type_trans(skb, ndev);
 
-	skb_mark_for_recycle(skb);
  	napi_gro_receive(&emac->napi_rx, skb);
  	ndev->stats.rx_bytes += pkt_len;
  	ndev->stats.rx_packets++;