The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Lorenzo Bianconi <lorenzo@kernel.org>
To: sun jian <sun.jian.kdev@gmail.com>
Cc: "Mohsin Bashir" <mohsin.bashr@gmail.com>,
	netdev@vger.kernel.org, "Andrew Lunn" <andrew+netdev@lunn.ch>,
	"David S. Miller" <davem@davemloft.net>,
	"Eric Dumazet" <edumazet@google.com>,
	"Jakub Kicinski" <kuba@kernel.org>,
	"Paolo Abeni" <pabeni@redhat.com>,
	"Simon Horman" <horms@kernel.org>,
	"Alexei Starovoitov" <ast@kernel.org>,
	"Daniel Borkmann" <daniel@iogearbox.net>,
	"Jesper Dangaard Brouer" <hawk@kernel.org>,
	"John Fastabend" <john.fastabend@gmail.com>,
	"Stanislav Fomichev" <sdf@fomichev.me>,
	"Kuniyuki Iwashima" <kuniyu@google.com>,
	"Hangbin Liu" <liuhangbin@gmail.com>,
	"Krishna Kumar" <krikku@gmail.com>,
	"Samiullah Khawaja" <skhawaja@google.com>,
	"Martin Karsten" <mkarsten@uwaterloo.ca>,
	"Toke Høiland-Jørgensen" <toke@redhat.com>,
	linux-kernel@vger.kernel.org, bpf@vger.kernel.org,
	maciej.fijalkowski@intel.com, stable@vger.kernel.org
Subject: Re: [PATCH net v2 2/2] veth: fix skb length accounting after XDP frag adjustment
Date: Mon, 3 Aug 2026 10:42:00 +0200	[thread overview]
Message-ID: <anBUWIxmURvbr04i@lore-desk> (raw)
In-Reply-To: <CABFUUZGTJptpOKKAeU3ThibmPpXize4Eud2=EBX-vqaX3YH4pg@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 4412 bytes --]

> On Sat, Aug 1, 2026 at 12:14 AM Mohsin Bashir <mohsin.bashr@gmail.com> wrote:
> >
> >
> >
> > On 7/30/26 8:23 PM, Sun Jian wrote:
> > > veth exposes non-linear skb fragments through an xdp_buff. If an XDP
> > > program adjusts the fragment area, veth_xdp_rcv_skb() copies
> > > xdp_frags_size back to skb->data_len but leaves skb->len containing the
> > > old fragment contribution.
> > >
> > > After a fragment shrink, this makes skb_headlen() larger than the actual
> > > linear area. In the reproduced UDP receive path, __skb_datagram_iter()
> > > copied 1024 bytes past the actual linear tail to userspace, starting at
> > > struct skb_shared_info. The copied bytes included the affected skb's
> > > nr_frags, xdp_frags_size and a kernel pointer from
> > > skb_shinfo(skb)->frags[0]. Real packet data was displaced by the same
> > > amount and truncated at the end.
> > >
> > > Subtract the old data_len before replacing it and add the new data_len
> > > afterwards, keeping skb->len and skb->data_len synchronized.
> > >
> > > The fragment accounting must run before the linear tail adjustment:
> > > when bpf_xdp_adjust_tail() shrinks the packet into the linear area it
> > > releases all fragments, and __skb_put() requires skb->data_len == 0
> > > by that point.
> > >
> > > A 60000-byte UDP datagram on a veth pair with MTU 64000 was shortened by
> > > 1024 bytes from its fragment area. Before the fix, all 10 runs produced
> > > corrupted payloads. After the fix, all 10 runs matched the expected
> > > payload exactly.
> > >
> > > Fixes: 718a18a0c8a6 ("veth: Rework veth_xdp_rcv_skb in order to accept non-linear skb")
> > > Cc: stable@vger.kernel.org
> > > Link: https://lore.kernel.org/bpf/al9T9Eto%2FhRIzP5W@boxer/
> > > Signed-off-by: Sun Jian <sun.jian.kdev@gmail.com>
> > > ---
> > >   drivers/net/veth.c | 23 +++++++++++++++--------
> > >   1 file changed, 15 insertions(+), 8 deletions(-)
> > >
> > > diff --git a/drivers/net/veth.c b/drivers/net/veth.c
> > > index 00e34afd858e..348391e87e14 100644
> > > --- a/drivers/net/veth.c
> > > +++ b/drivers/net/veth.c
> > > @@ -865,18 +865,25 @@ static struct sk_buff *veth_xdp_rcv_skb(struct veth_rq *rq,
> > >
> > >       skb_reset_mac_header(skb);
> > >
> > > -     /* check if bpf_xdp_adjust_tail was used */
> > > -     off = xdp->data_end - orig_data_end;
> > > -     if (off != 0)
> > > -             __skb_put(skb, off); /* positive on grow, negative on shrink */
> > > -
> > >       /* XDP frag metadata (e.g. nr_frags) are updated in eBPF helpers
> > > -      * (e.g. bpf_xdp_adjust_tail), we need to update data_len here.
> > > +      * (e.g. bpf_xdp_adjust_tail). Remove the old fragment contribution
> > > +      * from skb->len before updating data_len, then add the new one back.
> > > +      * This must precede the linear tail adjustment below: a changed
> > > +      * data_end implies that no fragments remain, and __skb_put() requires
> > > +      * a linear skb.
> > >        */
> > > -     if (xdp_buff_has_frags(xdp))
> > > +     skb->len -= skb->data_len;
> > > +     if (xdp_buff_has_frags(xdp)) {
> > >               skb->data_len = skb_shinfo(skb)->xdp_frags_size;
> > > -     else
> > > +             skb->len += skb->data_len;
> > > +     } else {
> > >               skb->data_len = 0;
> > > +     }
> > > +
> > > +     /* check if bpf_xdp_adjust_tail was used */
> > > +     off = xdp->data_end - orig_data_end;
> > > +     if (off != 0)
> > > +             __skb_put(skb, off); /* positive on grow, negative on shrink */
> > >
> > >       skb->protocol = eth_type_trans(skb, rq->dev);
> > >
> >
> > I am most likely missing something here but what happens if we have
> > frags and we attempt to advance data_end while leaving some frags
> > present (e.g., bpf_xdp_pull_data())? Looks like, in that case we would
> > issue __skb_put(skb, off) with off > 0 and we would hit
> > SKB_LINEAR_ASSERT() because skb is still non-linear?
> 
> Hi Mohsin,
> 
> Thanks for catching this. You're right that bpf_xdp_pull_data() can advance
> data_end while leaving frags present. I'll investigate this path and follow up.
> 
> Thanks,
> Sun Jian

I guess we should implement the same approach used in
bpf_prog_run_generic_xdp(), something like:

	if (off) {
		skb_set_tail_pointer();
		skb->len += off;
	}

Regards,
Lorenzo

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]

  reply	other threads:[~2026-08-03  8:42 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-31  3:23 [PATCH net v2 0/2] Fix skb length accounting after XDP frag adjustment Sun Jian
2026-07-31  3:23 ` [PATCH net v2 1/2] net: fix skb length accounting after generic " Sun Jian
2026-07-31 15:45   ` Mohsin Bashir
2026-07-31 15:51   ` Lorenzo Bianconi
2026-07-31  3:23 ` [PATCH net v2 2/2] veth: fix skb length accounting after " Sun Jian
2026-07-31 16:04   ` Lorenzo Bianconi
2026-07-31 16:14   ` Mohsin Bashir
2026-08-03  1:32     ` sun jian
2026-08-03  8:42       ` Lorenzo Bianconi [this message]
2026-08-03 10:14         ` sun jian

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=anBUWIxmURvbr04i@lore-desk \
    --to=lorenzo@kernel.org \
    --cc=andrew+netdev@lunn.ch \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=hawk@kernel.org \
    --cc=horms@kernel.org \
    --cc=john.fastabend@gmail.com \
    --cc=krikku@gmail.com \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=liuhangbin@gmail.com \
    --cc=maciej.fijalkowski@intel.com \
    --cc=mkarsten@uwaterloo.ca \
    --cc=mohsin.bashr@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=sdf@fomichev.me \
    --cc=skhawaja@google.com \
    --cc=stable@vger.kernel.org \
    --cc=sun.jian.kdev@gmail.com \
    --cc=toke@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox