From: Willem de Bruijn <willemdebruijn.kernel@gmail.com>
To: Willem de Bruijn <willemdebruijn.kernel@gmail.com>,
Willem de Bruijn <willemdebruijn.kernel@gmail.com>,
Willem de Bruijn <willemdebruijn.kernel@gmail.com>,
Willem de Bruijn <willemdebruijn.kernel@gmail.com>,
lazyming <minhnguyen.080505@gmail.com>,
netdev@vger.kernel.org
Cc: davem@davemloft.net, edumazet@google.com, kuba@kernel.org,
pabeni@redhat.com, horms@kernel.org, w@1wt.eu,
security@kernel.org, linux-kernel@vger.kernel.org,
lazyming <minhnguyen.080505@gmail.com>,
stable@vger.kernel.org, asml.silence@gmail.com,
achender@kernel.org, mst@redhat.com, jasowang@redhat.com
Subject: Re: [PATCH net] net: skbuff: fix missing zerocopy reference in pskb_carve helpers
Date: Mon, 25 May 2026 13:31:53 -0400 [thread overview]
Message-ID: <willemdebruijn.kernel.2a4ec672bf2d7@gmail.com> (raw)
In-Reply-To: <willemdebruijn.kernel.9bf2a08cffd8@gmail.com>
Willem de Bruijn wrote:
> Willem de Bruijn wrote:
> > Willem de Bruijn wrote:
> > > Willem de Bruijn wrote:
> > > > lazyming wrote:
> > > > > pskb_carve_inside_header() and pskb_carve_inside_nonlinear() both copy
> > > > > the old skb_shared_info header into a new buffer via memcpy(), which
> > > > > includes the destructor_arg pointer (uarg) for MSG_ZEROCOPY skbs.
> > > >
> > > > These functions are not supposed to maintain zerocopy frags.
> > > >
> > > > Both call skb_orphan_frags.
> > > >
> > > > I think what may need to happen is to invert the order of that call
> > > > and the memcpy. Current code:
> > > >
> > > > memcpy((struct skb_shared_info *)(data + size),
> > > > skb_shinfo(skb), offsetof(struct skb_shared_info, frags[0]));
> > > > if (skb_orphan_frags(skb, gfp_mask)) {
> > > > skb_kfree_head(data);
> > > > return -ENOMEM;
> > > > }
> > >
> > > Never mind. This actually corresponds to the first Sashiko report you
> > > mentioned: if zerocopy skbs are converted, then the memcpy prior to
> > > that call will have stale state.
> > >
> > > For skbs where skb_orphan_frags does not do a deep copy, we do need to
> > > take this extra reference.
> > >
> > > Reviewed-by: Willem de Bruijn <willemb@google.com>
> >
> > Not sure the potential preexisting issue is reachable.
> >
> > Vhost-net and other zerocopy that predates MSG_ZEROCOPY does not
> > refcount ubuf_info. Instead it calls skb_copy_ubufs on skb_clone.
> >
> > So if such an skb reaches pskb_expand_head, it should be guaranteed to
> > not be a clone. Same for the carve methods added later.
> >
> > But, the commit that added zerocopy, commit a6686f2f382b
> > ("skbuff: skb supports zero-copy buffers"), included this
> > pksb_expand_head call to skb_copy_ubufs from the start. That implies
> > that was expected to be reachable. I just don't see how yet.
> >
> > If it is reachable, then all that is needed is to clear shinfo->flags.
> > Or more neatly,
> >
> > skb_shinfo(skb)->flags &= ~SKBFL_ALL_ZEROCOPY;
>
> Also, I'm not the expert on more recent managed frags
> (SKBFL_MANAGED_FRAG_REFS).
>
> That calls skb_zcopy_downgrade_managed in pskb_expand_head, but not in
> the two other functions with memcpy before skb_copy_ubufs:
> pskb_carve_inside_header and pskb_carve_inside_nonlinear.
>
> I assume because those shorten the skb, so no risk of getting mixed
> mode refcounted and non-refcounted frags?
>
> In general zerocopy can be split in refcounted and non-refcounted.
>
> Refcounted zerocopy will not downgrade in these cases, so will not
> modify shinfo->flags after memcpy.
>
> Non-refcounted should always get converted to copy in skb_clone,
> so will not enter the skb_cloned() branch here.
>
> If in doubt maybe warrants a rare WARN_ON_ONCE patch.
I was unable to find a path also with the help of Gemini.
It did spot an interesting case where a cloned unrefcounted zerocopy
skb can be created. But it is reduced to uncloned immediately.
skb_morph is like skb_clone (calls __skb_clone), but skips the
skb_orphan_frags check. Its only caller ensures that this is safe.
But I'm inclined to send a patch to net-next that makes skb_morph
itself safe, by orphaning as well (and updating the caller to
gracefully handle skb_morph failure).
next prev parent reply other threads:[~2026-05-25 17:31 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-21 12:16 [PATCH net] net: skbuff: fix missing zerocopy reference in pskb_carve helpers lazyming
2026-05-23 8:58 ` lazyming
2026-05-24 13:37 ` Willem de Bruijn
2026-05-24 14:06 ` Willem de Bruijn
2026-05-25 15:18 ` Willem de Bruijn
2026-05-25 15:31 ` Willem de Bruijn
2026-05-25 17:31 ` Willem de Bruijn [this message]
2026-05-26 14:50 ` Pavel Begunkov
2026-05-25 19:54 ` Jakub Kicinski
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=willemdebruijn.kernel.2a4ec672bf2d7@gmail.com \
--to=willemdebruijn.kernel@gmail.com \
--cc=achender@kernel.org \
--cc=asml.silence@gmail.com \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=horms@kernel.org \
--cc=jasowang@redhat.com \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=minhnguyen.080505@gmail.com \
--cc=mst@redhat.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=security@kernel.org \
--cc=stable@vger.kernel.org \
--cc=w@1wt.eu \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.