xsk: fix memory corruptions in net/core/xdp.c
Subsystems (4)
PATCH 1/1 xsk: fix memory corruptions in net/core/xdp.c
From: Clément Lecigne <[email protected]>
Commit 560d958c6c68 ("xsk: add generic XSk &xdp_buff -> skb conversion") introduced a vulnerability in the handling of XDP_PASS for AF_XDP zero-copy frames.
Note: Currently, this specific AF_XDP zero-copy conversion path is only reachable from the drivers/net/ethernet/intel/ice driver.
When building an skb, xdp_build_skb_from_zc() uses the chunk size (xdp->frame_sz) for the allocation. However, napi_build_skb() automatically reserves space at the end of the allocation for the skb_shared_info structure.
Most high performance UMEM applications use 4K chunks, where the corruption cannot happen. However, if the UMEM is configured with 2KB chunks (a very common configuration to maximize packet density in memory), a standard 1500 MTU packet will trigger the corruption because the required space exceeds the 2048 byte chunk size:
Headroom (256) + Packet (1514) + skb_shared_info (320) = 2090 bytes
Because 2090 bytes > 2048 bytes and __skb_put() does not perform bounds checking, the memcpy() writes past the available linear data area and corrupts the skb_shared_info structure. This can lead to arbitrary code execution if pointers like destructor_arg are overwritten.
Additionally, in xdp_copy_frags_from_zc(), the allocation size is set strictly to the fragment size (len), but the subsequent memcpy() uses LARGEST_ALIGN(len). This mismatch results in an out-of-bounds write of up to 7 bytes, which triggers KASAN warnings and is unsafe despite typical page pool allocator padding.
Fix the skb allocation in xdp_build_skb_from_zc() by dynamically calculating the exact truesize required: the sum of the headroom, the packet length, and the skb_shared_info overhead, properly aligned via SKB_DATA_ALIGN.
Fix the out-of-bounds write in xdp_copy_frags_from_zc() by rounding up the allocation request using LARGEST_ALIGN(len) to match the copy operation.
---
net/core/xdp.c +5 -3
@@ -699,7 +699,7 @@ static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb,for (u32 i =0 ; i < nr_frags; i++) {const skb_frag_t *frag = &xinfo->frags[i];u32 len =skb_frag_size (frag); -u32 offset, truesize = len; +u32 offset, truesize =LARGEST_ALIGN (len);struct page *page; page =page_pool_dev_alloc (pp, &offset, &truesize);
@@ -740,7 +740,9 @@ struct sk_buff *xdp_build_skb_from_zc(struct xdp_buff *xdp) {const struct xdp_rxq_info *rxq = xdp->rxq;u32 len = xdp->data_end - xdp->data_meta; -u32 truesize = xdp->frame_sz; +u32 headroom = xdp->data_meta - xdp->data_hard_start; +u32 truesize =SKB_DATA_ALIGN (headroom + len) + +SKB_DATA_ALIGN (sizeof (struct skb_shared_info ));struct sk_buff *skb =NULL ;struct page_pool *pp;int metalen;
@@ -762,7 +764,7 @@ struct sk_buff *xdp_build_skb_from_zc(struct xdp_buff *xdp) }skb_mark_for_recycle (skb); -skb_reserve (skb, xdp->data_meta - xdp->data_hard_start); +skb_reserve (skb, headroom);memcpy (__skb_put (skb, len), xdp->data_meta,LARGEST_ALIGN (len));
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Critical] The new dynamic `truesize` calculation tightly bounds the skb linear buffer size but fails to…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] The new dynamic truesize calculation tightly bounds the skb linear buffer size but fails to account for the LARGEST_ALIGN(len) padding used in the subsequent memcpy(). This leads to an out-of-bounds write that corrupts the skb_shared_info structure. --
commit f8cfaa75cd9fcfb65c25981d740b6eedecb19f7b
Author: Clément Lecigne <[email protected]>
xsk: fix memory corruptions in net/core/xdp.c
This commit attempts to fix a memory corruption in AF_XDP zero-copy handling by dynamically calculating the truesize for skb allocation. It also fixes a mismatch in xdp_copy_frags_from_zc() between allocation and copy sizes.
Quoted 4 lines
net/core/xdp.c +0
-0
[ ... ]
net/core/xdp.c struct sk_buff *xdp_build_skb_from_zc(struct xdp_buff *xdp) · line 745
Quoted 8 lines
net/core/xdp.c +3
-1@@ -740,7 +740,9 @@ struct sk_buff *xdp_build_skb_from_zc(struct xdp_buff *xdp) {const struct xdp_rxq_info *rxq = xdp->rxq;u32 len = xdp->data_end - xdp->data_meta; -u32 truesize = xdp->frame_sz; +u32 headroom = xdp->data_meta - xdp->data_hard_start; +u32 truesize =SKB_DATA_ALIGN (headroom + len) + +SKB_DATA_ALIGN (sizeof (struct skb_shared_info ));
[Severity: Critical]
Does this tight allocation size account for the LARGEST_ALIGN(len) padding used during the memcpy() below?
If an XDP program modifies the packet data making headroom unaligned, and headroom + len is close to a multiple of SMP_CACHE_BYTES, napi_build_skb() will place the skb_shared_info structure exactly at SKB_DATA_ALIGN(headroom + len).
Could the LARGEST_ALIGN(len) size in the memcpy() below write past this linear data area and corrupt the adjacent skb_shared_info structure?
Quoted 3 lines
net/core/xdp.c +0
-0struct sk_buff *skb =NULL ;struct page_pool *pp;int metalen;
[ ... ]
Quoted 8 lines
net/core/xdp.c +1
-1@@ -762,7 +764,7 @@ struct sk_buff *xdp_build_skb_from_zc(struct xdp_buff *xdp) }skb_mark_for_recycle (skb); -skb_reserve (skb, xdp->data_meta - xdp->data_hard_start); +skb_reserve (skb, headroom);memcpy (__skb_put (skb, len), xdp->data_meta,LARGEST_ALIGN (len));
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
From: Clement Lecigne <[email protected]>
Date: Wed, 24 Jun 2026 08:41:28 +0000
Quoted 8 lines
From: Clément Lecigne <[email protected]>
Commit
560d958c6c68("xsk: add generic XSk&xdp_buff-> skb conversion") introduced a vulnerability in the handling ofXDP_PASSforAF_XDPzero-copy frames.Note: Currently, this specific
AF_XDPzero-copy conversion path is only reachable from thedrivers/net/ethernet/intel/icedriver.
idpf uses this, too (every driver based on libeth_xdp in general, currently these two).
Quoted 44 lines
When building an skb,
xdp_build_skb_from_zc()uses the chunk size (xdp->frame_sz) for the allocation. However,napi_build_skb()automatically reserves space at the end of the allocation for theskb_shared_infostructure.Most high performance UMEM applications use 4K chunks, where the corruption cannot happen. However, if the UMEM is configured with 2KB chunks (a very common configuration to maximize packet density in memory), a standard 1500 MTU packet will trigger the corruption because the required space exceeds the 2048 byte chunk size:
Headroom (256) + Packet (1514) +
skb_shared_info(320) = 2090 bytesBecause 2090 bytes > 2048 bytes and
__skb_put()does not perform bounds checking, thememcpy()writes past the available linear data area and corrupts theskb_shared_infostructure. This can lead to arbitrary code execution if pointers likedestructor_argare overwritten.Additionally, in
xdp_copy_frags_from_zc(), the allocation size is set strictly to the fragment size (len), but the subsequentmemcpy()usesLARGEST_ALIGN(len). This mismatch results in an out-of-bounds write of up to 7 bytes, which triggers KASAN warnings and is unsafe despite typical page pool allocator padding.Fix the skb allocation in
xdp_build_skb_from_zc()by dynamically calculating the exact truesize required: the sum of the headroom, the packet length, and theskb_shared_infooverhead, properly aligned viaSKB_DATA_ALIGN.Fix the out-of-bounds write in
xdp_copy_frags_from_zc()by rounding up the allocation request usingLARGEST_ALIGN(len)to match the copy operation.Fixes 560d958c6c68 ("xsk: add generic XSk &xdp_buff -> skb conversion")CC Alexander Lobakin <[email protected]>CC Eric Dumazet <[email protected]>Signed-off-by Clément Lecigne <[email protected]>---
net/core/xdp.c +0
-0
net/core/xdp.c static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb, · line 702
Quoted 5 lines
net/core/xdp.c +1
-1@@ -699,7 +699,7 @@ static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb,for (u32 i =0 ; i < nr_frags; i++) {const skb_frag_t *frag = &xinfo->frags[i];u32 len =skb_frag_size (frag); -u32 offset, truesize = len; +u32 offset, truesize =LARGEST_ALIGN (len);
I think you need to re-sort this to keep RCT, now that the truesize initialization is way longer than it was.
const skb_frag_t *frag = &xinfo->frags[i]; u32 offset, len = skb_frag_size(frag); u32 truesize = LARGEST_ALIGN(len); struct page *page;
net/core/xdp.c static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb, · line 705
Quoted 3 lines
net/core/xdp.c +0
-0struct page *page; page =page_pool_dev_alloc (pp, &offset, &truesize);
BTW usually LARGEST_ALIGN() aligns to 16, I've never seen a bigger one. IIRC Page Pool never returns a truesize aligned to a smaller value. But if you're really able to trigger this, it probably does?
net/core/xdp.c struct sk_buff *xdp_build_skb_from_zc(struct xdp_buff *xdp) · line 745
Quoted 8 lines
net/core/xdp.c +3
-1@@ -740,7 +740,9 @@ struct sk_buff *xdp_build_skb_from_zc(struct xdp_buff *xdp) {const struct xdp_rxq_info *rxq = xdp->rxq;u32 len = xdp->data_end - xdp->data_meta; -u32 truesize = xdp->frame_sz; +u32 headroom = xdp->data_meta - xdp->data_hard_start; +u32 truesize =SKB_DATA_ALIGN (headroom + len) + +SKB_DATA_ALIGN (sizeof (struct skb_shared_info ));
Ah now I get it: xdp->frame_sz doesn't account the shinfo for single-buffer frames, only for multi-buffer ones. The fix looks correct, but I'd use SKB_HEAD_ALIGN() since it does exactly what you're open-coding here and sort the declarations:
{
u32 hr = xdp->data_meta - xdp->data_hard_start;
const struct xdp_rxq_info *rxq = xdp->rxq;
u32 len = xdp->data_end - xdp->data_meta;
u32 truesize = SKB_HEAD_ALIGN(hr + len);
struct sk_buff *skb = NULL;
struct page_pool *pp;
int metalen;
void *data;
if (!IS_ENABLED(CONFIG_PAGE_POOL))
return NULL;
...net/core/xdp.c struct sk_buff *xdp_build_skb_from_zc(struct xdp_buff *xdp) · line 769
Quoted 11 lines
net/core/xdp.c +1
-1struct sk_buff *skb =NULL ;struct page_pool *pp;int metalen;@@ -762,7 +764,7 @@ struct sk_buff *xdp_build_skb_from_zc(struct xdp_buff *xdp) }skb_mark_for_recycle (skb); -skb_reserve (skb, xdp->data_meta - xdp->data_hard_start); +skb_reserve (skb, headroom);memcpy (__skb_put (skb, len), xdp->data_meta,LARGEST_ALIGN (len));
Thanks,
Olek
PATCH v2 1/1 xsk: fix memory corruptions in net/core/xdp.c
From: Clément Lecigne <[email protected]>
Commit 560d958c6c68 ("xsk: add generic XSk &xdp_buff -> skb conversion") introduced a vulnerability in the handling of XDP_PASS for AF_XDP zero-copy frames.
Note: Currently, this specific AF_XDP zero-copy conversion path is only reachable from the drivers/net/ethernet/intel/ice and drivers/net/ethernet/intel/idpf drivers.
When building an skb, xdp_build_skb_from_zc() uses the chunk size (xdp->frame_sz) for the allocation. However, napi_build_skb() automatically reserves space at the end of the allocation for the skb_shared_info structure.
Most high performance UMEM applications use 4K chunks, where the corruption cannot happen. However, if the UMEM is configured with 2KB chunks (a very common configuration to maximize packet density in memory), a standard 1500 MTU packet will trigger the corruption because the required space exceeds the 2048 byte chunk size:
Headroom (256) + Packet (1514) + skb_shared_info (320) = 2090 bytes
Because 2090 bytes > 2048 bytes and __skb_put() does not perform bounds checking, the memcpy() writes past the available linear data area and corrupts the skb_shared_info structure. This can lead to arbitrary code execution if pointers like destructor_arg are overwritten.
Additionally, in xdp_copy_frags_from_zc(), the allocation size is set strictly to the fragment size (len), but the subsequent memcpy() uses LARGEST_ALIGN(len). This mismatch results in an out-of-bounds write of up to 7 bytes, which triggers KASAN warnings and is unsafe despite typical page pool allocator padding.
Fix the skb allocation in xdp_build_skb_from_zc() by dynamically calculating the exact truesize required using SKB_HEAD_ALIGN() to properly account for the headroom, the packet length, and the skb_shared_info overhead.
Fix the out-of-bounds write in xdp_copy_frags_from_zc() by rounding up the allocation request using LARGEST_ALIGN(len) to match the copy operation.
---
Changes since v1:
- Used SKB_HEAD_ALIGN to properly calculate the required allocation size including the skb_shared_info overhead.
- Re-ordered variable declarations.
---
net/core/xdp.c +5 -4
@@ -698,8 +698,8 @@ static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb,for (u32 i =0 ; i < nr_frags; i++) {const skb_frag_t *frag = &xinfo->frags[i]; -u32 len =skb_frag_size (frag); -u32 offset, truesize = len; +u32 offset, len =skb_frag_size (frag); +u32 truesize =LARGEST_ALIGN (len);struct page *page; page =page_pool_dev_alloc (pp, &offset, &truesize);
@@ -738,9 +738,10 @@ static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb,*/ struct sk_buff *xdp_build_skb_from_zc (struct xdp_buff *xdp) { +u32 headroom = xdp->data_meta - xdp->data_hard_start;const struct xdp_rxq_info *rxq = xdp->rxq;u32 len = xdp->data_end - xdp->data_meta; -u32 truesize = xdp->frame_sz; +u32 truesize =SKB_HEAD_ALIGN (headroom + len);struct sk_buff *skb =NULL ;struct page_pool *pp;int metalen;
@@ -762,7 +763,7 @@ struct sk_buff *xdp_build_skb_from_zc(struct xdp_buff *xdp) }skb_mark_for_recycle (skb); -skb_reserve (skb, xdp->data_meta - xdp->data_hard_start); +skb_reserve (skb, headroom);memcpy (__skb_put (skb, len), xdp->data_meta,LARGEST_ALIGN (len));
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Critical] The exact dynamic allocation sizing in xdp_build_skb_from_zc() combined with a rounded-up…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] The exact dynamic allocation sizing in xdp_build_skb_from_zc() combined with a rounded-up memcpy() introduces a reliable out-of-bounds write into skb_shared_info. --
commit 332575c8834e99019b6ead4fd6570c4662e36762
Author: Clément Lecigne <[email protected]>
xsk: fix memory corruptions in net/core/xdp.c
This commit attempts to fix out of bounds writes during AF_XDP zero-copy frame conversions. It introduces dynamic truesize calculations using SKB_HEAD_ALIGN for the skb allocation and LARGEST_ALIGN for fragment allocations to properly account for required headroom and packet length.
Quoted 4 lines
net/core/xdp.c +0
-0
[ ... ]
net/core/xdp.c static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb, · line 744
Quoted 9 lines
net/core/xdp.c +2
-1@@ -738,9 +738,10 @@ static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb,*/ struct sk_buff *xdp_build_skb_from_zc (struct xdp_buff *xdp) { +u32 headroom = xdp->data_meta - xdp->data_hard_start;const struct xdp_rxq_info *rxq = xdp->rxq;u32 len = xdp->data_end - xdp->data_meta; -u32 truesize = xdp->frame_sz; +u32 truesize =SKB_HEAD_ALIGN (headroom + len);
[Severity: Critical]
Does this regression allocate enough linear space to handle a rounded-up memcpy when the headroom is unaligned?
If an XDP program adjusts data_meta such that headroom is not a multiple of 64 (for example, 252 bytes), and a packet of 68 bytes is received, the allocated linear size from SKB_HEAD_ALIGN(320) would be 320.
However, the later memcpy uses LARGEST_ALIGN(len) for the 68 bytes, which could round up to 96 bytes. This means 252 + 96 = 348 bytes could be written into a 320 byte space.
[ ... ]
net/core/xdp.c struct sk_buff *xdp_build_skb_from_zc(struct xdp_buff *xdp) · line 768
Quoted 8 lines
net/core/xdp.c +1
-1@@ -762,7 +763,7 @@ struct sk_buff *xdp_build_skb_from_zc(struct xdp_buff *xdp) }skb_mark_for_recycle (skb); -skb_reserve (skb, xdp->data_meta - xdp->data_hard_start); +skb_reserve (skb, headroom);memcpy (__skb_put (skb, len), xdp->data_meta,LARGEST_ALIGN (len));
[Severity: Critical]
Can this regression corrupt skb_shared_info?
Since the linear space allocation might only be 320 bytes as shown above, but this memcpy could write up to 348 bytes, it appears this might write past the end of the linear data region.
Could this lead to overwriting destructor_arg and cause issues upon SKB free?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Thanks, Olek!
I submitted a v2 of the patch directly as a reply to this thread by mistake, do you still want me to post this new version separately?
Quoted 10 lines · Alexander Lobakin
From: Clement Lecigne <[email protected]>
Date: Wed, 24 Jun 2026 08:41:28 +0000Quoted 6 lines
From: Clément Lecigne <[email protected]>
Commit
560d958c6c68("xsk: add generic XSk&xdp_buff-> skb conversion") introduced a vulnerability in the handling ofXDP_PASSforAF_XDPzero-copy frames.
Quoted 5 lines
Quoted 2 lines
Note: Currently, this specific
AF_XDPzero-copy conversion path is only reachable from thedrivers/net/ethernet/intel/icedriver.idpf uses this, too (every driver based on
libeth_xdpin general, currently these two).
Done.
Quoted 45 lines
Quoted 45 lines
When building an skb,
xdp_build_skb_from_zc()uses the chunk size (xdp->frame_sz) for the allocation. However,napi_build_skb()automatically reserves space at the end of the allocation for theskb_shared_infostructure.Most high performance UMEM applications use 4K chunks, where the corruption cannot happen. However, if the UMEM is configured with 2KB chunks (a very common configuration to maximize packet density in memory), a standard 1500 MTU packet will trigger the corruption because the required space exceeds the 2048 byte chunk size:
Headroom (256) + Packet (1514) +
skb_shared_info(320) = 2090 bytesBecause 2090 bytes > 2048 bytes and
__skb_put()does not perform bounds checking, thememcpy()writes past the available linear data area and corrupts theskb_shared_infostructure. This can lead to arbitrary code execution if pointers likedestructor_argare overwritten.Additionally, in
xdp_copy_frags_from_zc(), the allocation size is set strictly to the fragment size (len), but the subsequentmemcpy()usesLARGEST_ALIGN(len). This mismatch results in an out-of-bounds write of up to 7 bytes, which triggers KASAN warnings and is unsafe despite typical page pool allocator padding.Fix the skb allocation in
xdp_build_skb_from_zc()by dynamically calculating the exact truesize required: the sum of the headroom, the packet length, and theskb_shared_infooverhead, properly aligned viaSKB_DATA_ALIGN.Fix the out-of-bounds write in
xdp_copy_frags_from_zc()by rounding up the allocation request usingLARGEST_ALIGN(len)to match the copy operation.Fixes 560d958c6c68 ("xsk: add generic XSk &xdp_buff -> skb conversion")CC Alexander Lobakin <[email protected]>CC Eric Dumazet <[email protected]>Signed-off-by Clément Lecigne <[email protected]>---
net/core/xdp.c +0
-0@@ -699,7 +699,7 @@ static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb,
Quoted 8 lines
net/core/xdp.c static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb, · line 702
Quoted 5 lines
net/core/xdp.c +1
-1for (u32 i =0 ; i < nr_frags; i++) {const skb_frag_t *frag = &xinfo->frags[i];u32 len =skb_frag_size (frag); -u32 offset, truesize = len; +u32 offset, truesize =LARGEST_ALIGN (len);I think you need to re-sort this to keep RCT, now that the truesize initialization is way longer than it was.
Done.
Quoted 14 lines
const skb_frag_t *frag = &xinfo->frags[i]; u32 offset, len = skb_frag_size(frag); u32 truesize = LARGEST_ALIGN(len); struct page *page;net/core/xdp.c static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb, · line 705
Quoted 3 lines
net/core/xdp.c +0
-0struct page *page; page =page_pool_dev_alloc (pp, &offset, &truesize);BTW usually
LARGEST_ALIGN()aligns to 16, I've never seen a bigger one. IIRC Page Pool never returns a truesize aligned to a smaller value. But if you're really able to trigger this, it probably does?
Quoted 13 lines
net/core/xdp.c struct sk_buff *xdp_build_skb_from_zc(struct xdp_buff *xdp) · line 745
Quoted 8 lines
net/core/xdp.c +3
-1@@ -740,7 +740,9 @@ struct sk_buff *xdp_build_skb_from_zc(struct xdp_buff *xdp) {const struct xdp_rxq_info *rxq = xdp->rxq;u32 len = xdp->data_end - xdp->data_meta; -u32 truesize = xdp->frame_sz; +u32 headroom = xdp->data_meta - xdp->data_hard_start; +u32 truesize =SKB_DATA_ALIGN (headroom + len) + +SKB_DATA_ALIGN (sizeof (struct skb_shared_info ));Ah now I get it:
xdp->frame_szdoesn't account the shinfo for single-buffer frames, only for multi-buffer ones. The fix looks correct, but I'd useSKB_HEAD_ALIGN()since it does exactly what you're open-coding here and sort the declarations:
Good idea, done.
Quoted 22 lines
{ u32 hr = xdp->data_meta - xdp->data_hard_start; const struct xdp_rxq_info *rxq = xdp->rxq; u32 len = xdp->data_end - xdp->data_meta; u32 truesize = SKB_HEAD_ALIGN(hr + len); struct sk_buff *skb = NULL; struct page_pool *pp; int metalen; void *data; if (!IS_ENABLED(CONFIG_PAGE_POOL)) return NULL; ...Quoted 6 lines
net/core/xdp.c +0
-0struct sk_buff *skb =NULL ;struct page_pool *pp;int metalen;@@ -762,7 +764,7 @@ struct sk_buff *xdp_build_skb_from_zc(struct xdp_buff *xdp) }
Quoted 8 lines
net/core/xdp.c struct sk_buff *xdp_build_skb_from_zc(struct xdp_buff *xdp) · line 769
Quoted 5 lines
net/core/xdp.c +1
-1skb_mark_for_recycle (skb); -skb_reserve (skb, xdp->data_meta - xdp->data_hard_start); +skb_reserve (skb, headroom);memcpy (__skb_put (skb, len), xdp->data_meta,LARGEST_ALIGN (len));Thanks,
Olek
Thanks,
-clem