veth: convert frag_list skbs before running XDP
Subsystems (4)
PATCH 1/1 veth: convert frag_list skbs before running XDP
From: Matt Fleming <[email protected]>
A frag_list skb can reach veth with data_len set but nr_frags zero. veth_convert_skb_to_xdp_buff() only converts skbs that are shared, locked, have frags[], or do not have enough headroom. It later uses skb_is_nonlinear() to decide whether to set XDP_FLAGS_HAS_FRAGS and xdp_frags_size.
That exposes frag_list data to XDP as if it were stored in frags[], but frags[] is empty. AF_XDP copy mode can then trust the bogus XDP fragment metadata, walk an empty fragment entry, and crash in memcpy() from __xsk_rcv().
Route frag_list skbs through skb_pp_cow_data() before exposing them to XDP, and only advertise XDP frags when the resulting skb has frags[]. skb_copy_bits() already handles frag_list input, and skb_pp_cow_data() builds frags[] output with skb_add_rx_frag(), which is the representation XDP multi-buffer expects.
--- drivers/net/veth.c | 4 ++-- net/core/skbuff.c | 9 ++++----- 2 files changed, 6 insertions(+), 7 deletions(-)
drivers/net/veth.c +2 -2
@@ -756,7 +756,7 @@ static int veth_convert_skb_to_xdp_buff(struct veth_rq *rq,u32 frame_sz;if (skb_shared (skb) ||skb_head_is_locked (skb) || -skb_shinfo (skb)->nr_frags || +skb_shinfo (skb)->nr_frags ||skb_has_frag_list (skb) ||skb_headroom (skb) <XDP_PACKET_HEADROOM ) {if (skb_pp_cow_data (rq->page_pool, pskb,XDP_PACKET_HEADROOM )) goto drop;
@@ -771,7 +771,7 @@ static int veth_convert_skb_to_xdp_buff(struct veth_rq *rq,xdp_prepare_buff (xdp, skb->head,skb_headroom (skb),skb_headlen (skb), true); -if (skb_is_nonlinear (skb)) { +if (skb_shinfo (skb)->nr_frags) {skb_shinfo (skb)->xdp_frags_size = skb->data_len;xdp_buff_set_frags_flag (xdp); }else {
net/core/skbuff.c +4 -5
@@ -936,12 +936,11 @@ int skb_pp_cow_data(struct page_pool *pool, struct sk_buff **pskb,int err, i, head_off;void *data; -/* XDP does not support fraglist so we need to linearize -* the skb. +/* +* skb_copy_bits() handles both frags[] and frag_list input. If the +* copied skb remains non-linear, it uses frags[], which is the +* representation used by XDP multi-buffer. */ -if (skb_has_frag_list (skb)) -return -EOPNOTSUPP ; - max_head_size =SKB_WITH_OVERHEAD (PAGE_SIZE - headroom);if (skb->len > max_head_size +MAX_SKB_FRAGS *PAGE_SIZE )return -ENOMEM ;
Signature · 1 line
2.43.0
Quoted 19 lines · Matt Fleming
From: Matt Fleming <[email protected]>
A
frag_listskb can reach veth withdata_lenset butnr_fragszero.veth_convert_skb_to_xdp_buff()only converts skbs that are shared, locked, have frags[], or do not have enough headroom. It later usesskb_is_nonlinear()to decide whether to setXDP_FLAGS_HAS_FRAGSandxdp_frags_size.That exposes
frag_listdata to XDP as if it were stored in frags[], but frags[] is empty.AF_XDPcopy mode can then trust the bogus XDP fragment metadata, walk an empty fragment entry, and crash inmemcpy()from__xsk_rcv().Route
frag_listskbs throughskb_pp_cow_data()before exposing them to XDP, and only advertise XDP frags when the resulting skb has frags[].skb_copy_bits()already handlesfrag_listinput, andskb_pp_cow_data()builds frags[] output withskb_add_rx_frag(), which is the representation XDP multi-buffer expects.
Quoted 3 lines
Fixes 718a18a0c8a6 ("veth: Rework veth_xdp_rcv_skb in order to accept non-linear skb")Signed-off-by Matt Fleming <[email protected]>
Seems reasonable, but a few nits below:
Quoted 10 lines
--- drivers/net/veth.c | 4 ++-- net/core/skbuff.c | 9 ++++----- 2 files changed, 6 insertions(+), 7 deletions(-)drivers/net/veth.c +0
-0
drivers/net/veth.c static int veth_convert_skb_to_xdp_buff(struct veth_rq *rq, · line 759
Quoted 5 lines
drivers/net/veth.c +1
-1@@ -756,7 +756,7 @@ static int veth_convert_skb_to_xdp_buff(struct veth_rq *rq,u32 frame_sz; if (skb_shared(skb) || skb_head_is_locked(skb) || - skb_shinfo(skb)->nr_frags || + skb_shinfo(skb)->nr_frags || skb_has_frag_list(skb) ||
Isn't 'skb_shinfo(skb)->nr_frags || skb_has_frag_list(skb)' basically the same as 'skb_is_nonlinear(skb)'? Which, incidentally, is what generic XDP uses in the check that guards calling into the skb_pp_cow_data() path.
Looking at those two places, generic XDP checks for 'skb_cloned(skb)', while veth checks 'skb_shared(skb) || skb_head_is_locked(skb)'. AFAICT, the latter is stricter; should we update the generic XDP check?
Quoted 17 lines
drivers/net/veth.c +1
-1skb_headroom(skb) < XDP_PACKET_HEADROOM) {if (skb_pp_cow_data (rq->page_pool, pskb,XDP_PACKET_HEADROOM )) goto drop;@@ -771,7 +771,7 @@ static int veth_convert_skb_to_xdp_buff(struct veth_rq *rq,xdp_prepare_buff (xdp, skb->head,skb_headroom (skb),skb_headlen (skb), true); -if (skb_is_nonlinear (skb)) { +if (skb_shinfo (skb)->nr_frags) {skb_shinfo (skb)->xdp_frags_size = skb->data_len;xdp_buff_set_frags_flag (xdp); }else {net/core/skbuff.c +0
-0
net/core/skbuff.c int skb_pp_cow_data(struct page_pool *pool, struct sk_buff **pskb, · line 943
Quoted 10 lines
net/core/skbuff.c +4
-2@@ -936,12 +936,11 @@ int skb_pp_cow_data(struct page_pool *pool, struct sk_buff **pskb,int err, i, head_off;void *data; -/* XDP does not support fraglist so we need to linearize -* the skb. +/* +* skb_copy_bits() handles both frags[] and frag_list input. If the +* copied skb remains non-linear, it uses frags[], which is the +* representation used by XDP multi-buffer. */
This comment sorta reads like a function documentation comment, but it ends up sitting weirdly in the middle of the function body. The comment you're replacing was tied to the statement below, but this one isn't, really. Should we turn it into an actual function doc comment instead?
-Toke
Quoted 9 lines · Toke Høiland-Jørgensen
Quoted 7 lines · Matt Fleming
drivers/net/veth.c +0
-0@@ -756,7 +756,7 @@ static int veth_convert_skb_to_xdp_buff(struct veth_rq *rq,u32 frame_sz;
Quoted 8 lines
drivers/net/veth.c static int veth_convert_skb_to_xdp_buff(struct veth_rq *rq, · line 759
Quoted 3 lines
drivers/net/veth.c +1
-1if (skb_shared(skb) || skb_head_is_locked(skb) || - skb_shinfo(skb)->nr_frags || + skb_shinfo(skb)->nr_frags || skb_has_frag_list(skb) ||Isn't 'skb_shinfo(skb)->nr_frags ||
skb_has_frag_list(skb)' basically the same as 'skb_is_nonlinear(skb)'? Which, incidentally, is what generic XDP uses in the check that guards calling into theskb_pp_cow_data()path.
Yeah, you're right. I tested that expression and it still fixes the bug. I'll update v2 to use skb_is_nonlinear().
Quoted 3 lines
Looking at those two places, generic XDP checks for '
skb_cloned(skb)', while veth checks 'skb_shared(skb)||skb_head_is_locked(skb)'. AFAICT, the latter is stricter; should we update the generic XDP check?
Possibly, but the surrounding code isn't set up to deal with skb_shared() SKBs so that'd be a larger change. I can take a look at that too but I'm going to need more time to get my head around making that change correctly given that there's different fallback rules than veth.
Quoted 20 lines
Quoted 20 lines
drivers/net/veth.c +1
-1skb_headroom(skb) < XDP_PACKET_HEADROOM) {if (skb_pp_cow_data (rq->page_pool, pskb,XDP_PACKET_HEADROOM )) goto drop;@@ -771,7 +771,7 @@ static int veth_convert_skb_to_xdp_buff(struct veth_rq *rq,xdp_prepare_buff (xdp, skb->head,skb_headroom (skb),skb_headlen (skb), true); -if (skb_is_nonlinear (skb)) { +if (skb_shinfo (skb)->nr_frags) {skb_shinfo (skb)->xdp_frags_size = skb->data_len;xdp_buff_set_frags_flag (xdp); }else {net/core/skbuff.c +0
-0@@ -936,12 +936,11 @@ int skb_pp_cow_data(struct page_pool *pool, struct sk_buff **pskb,int err, i, head_off;void *data;
Quoted 12 lines
net/core/skbuff.c int skb_pp_cow_data(struct page_pool *pool, struct sk_buff **pskb, · line 943
Quoted 7 lines
net/core/skbuff.c +4
-2-/* XDP does not support fraglist so we need to linearize -* the skb. +/* +* skb_copy_bits() handles both frags[] and frag_list input. If the +* copied skb remains non-linear, it uses frags[], which is the +* representation used by XDP multi-buffer. */ This comment sorta reads like a function documentation comment, but it ends up sitting weirdly in the middle of the function body. The comment you're replacing was tied to the statement below, but this one isn't, really. Should we turn it into an actual function doc comment instead?
Good point. I'll make this a function doc comment.
Thanks,
Matt
Quoted 26 lines · Matt Fleming
Quoted 9 lines · Toke Høiland-Jørgensen
Quoted 7 lines · Matt Fleming
drivers/net/veth.c +0
-0@@ -756,7 +756,7 @@ static int veth_convert_skb_to_xdp_buff(struct veth_rq *rq,u32 frame_sz;Quoted 8 lines
drivers/net/veth.c static int veth_convert_skb_to_xdp_buff(struct veth_rq *rq, · line 759
Quoted 3 lines
drivers/net/veth.c +1
-1if (skb_shared(skb) || skb_head_is_locked(skb) || - skb_shinfo(skb)->nr_frags || + skb_shinfo(skb)->nr_frags || skb_has_frag_list(skb) ||Isn't 'skb_shinfo(skb)->nr_frags ||
skb_has_frag_list(skb)' basically the same as 'skb_is_nonlinear(skb)'? Which, incidentally, is what generic XDP uses in the check that guards calling into theskb_pp_cow_data()path.Yeah, you're right. I tested that expression and it still fixes the bug. I'll update v2 to use
skb_is_nonlinear().Quoted 3 lines
Looking at those two places, generic XDP checks for '
skb_cloned(skb)', while veth checks 'skb_shared(skb)||skb_head_is_locked(skb)'. AFAICT, the latter is stricter; should we update the generic XDP check?
Quoted 5 lines
Possibly, but the surrounding code isn't set up to deal with
skb_shared()SKBs so that'd be a larger change. I can take a look at that too but I'm going to need more time to get my head around making that change correctly given that there's different fallback rules than veth.
Hi all,
I had an RFC that made veth to reuse generic XDP code path [0]. Jakub commented we could have a common pp cow check on both sides. My plan is to revive this work as this still causes page_pool issues when AF_XDP is used on veth.
[0]: https://lore.kernel.org/bpf/[email protected]/
Thanks,
Maciej
Quoted 39 lines
Quoted 21 lines
Quoted 21 lines
drivers/net/veth.c +1
-1skb_headroom(skb) < XDP_PACKET_HEADROOM) {if (skb_pp_cow_data (rq->page_pool, pskb,XDP_PACKET_HEADROOM )) goto drop;@@ -771,7 +771,7 @@ static int veth_convert_skb_to_xdp_buff(struct veth_rq *rq,xdp_prepare_buff (xdp, skb->head,skb_headroom (skb),skb_headlen (skb), true); -if (skb_is_nonlinear (skb)) { +if (skb_shinfo (skb)->nr_frags) {skb_shinfo (skb)->xdp_frags_size = skb->data_len;xdp_buff_set_frags_flag (xdp); }else {net/core/skbuff.c +0
-0@@ -936,12 +936,11 @@ int skb_pp_cow_data(struct page_pool *pool, struct sk_buff **pskb,int err, i, head_off;void *data;Quoted 12 lines
net/core/skbuff.c int skb_pp_cow_data(struct page_pool *pool, struct sk_buff **pskb, · line 943
Quoted 7 lines
net/core/skbuff.c +4
-2-/* XDP does not support fraglist so we need to linearize -* the skb. +/* +* skb_copy_bits() handles both frags[] and frag_list input. If the +* copied skb remains non-linear, it uses frags[], which is the +* representation used by XDP multi-buffer. */ This comment sorta reads like a function documentation comment, but it ends up sitting weirdly in the middle of the function body. The comment you're replacing was tied to the statement below, but this one isn't, really. Should we turn it into an actual function doc comment instead?
Good point. I'll make this a function doc comment.
Thanks,
Matt
Quoted 9 lines · Maciej Fijalkowski
Hi all,
I had an RFC that made veth to reuse generic XDP code path [0]. Jakub commented we could have a common pp cow check on both sides. My plan is to revive this work as this still causes
page_poolissues whenAF_XDPis used on veth.
That sounds great. I'd be happy to test out the next version.
Thanks,
Matt