All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Björn Töpel" <bjorn@kernel.org>, bpf@vger.kernel.org
Cc: intel-wired-lan@lists.osuosl.org
Subject: Re: [PATCH net 2/3] xsk: Keep the shared info of zero-copy buffers in kernel memory
Date: Sat, 28 Nov 2026 14:22:21 +0000	[thread overview]
Message-ID: <sashiko-outbox-238767@kernel.org> (raw)
In-Reply-To: <20261127142131.3527737-3-bjorn@kernel.org>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Critical] xsk: skb_shinfo() buffer overflow in
  xdp_build_skb_from_zc() on XDP_PASS
- [Medium] xsk: lost XDP_FLAGS_SINFO_EXT flag on buffer reuse in
  buf_to_xdp()
--

commit 8a374e24c8b71b5be821eeab702cf5c5a6b0a6c4
Author: Björn Töpel <bjorn@kernel.org>

xsk: Keep the shared info of zero-copy buffers in kernel memory

This commit moves the skb_shared_info of AF_XDP zero-copy RX buffers from
the UMEM chunk tailroom into struct xsk_buff_pool and drops the tailroom
reservation from the chunk and headroom limits.

> diff --git a/include/net/xdp.h b/include/net/xdp.h
> index 07231adfb5f82..68dc9caacfa93 100644
> --- a/include/net/xdp.h
> +++ b/include/net/xdp.h
[ ... ]
> @@ -178,17 +189,26 @@ xdp_prepare_buff(struct xdp_buff *xdp, unsigned char *hard_start,
>  
>  /* Reserve memory area at end-of data area.
>   *
> - * This macro reserves tailroom in the XDP buffer by limiting the
> - * XDP/BPF data access to data_hard_end.  Notice same area (and size)
> - * is used for XDP_PASS, when constructing the SKB via build_skb().
> + * This reserves tailroom in the XDP buffer by limiting the XDP/BPF data
> + * access to data_hard_end.  Notice same area (and size) is used for
> + * XDP_PASS, when constructing the SKB via build_skb(). Buffers with
> + * %XDP_FLAGS_SINFO_EXT keep the shared info elsewhere and reserve none.
>   */
> -#define xdp_data_hard_end(xdp)				\
> -	((xdp)->data_hard_start + (xdp)->frame_sz -	\
> -	 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
> +static __always_inline void *xdp_data_hard_end(const struct xdp_buff *xdp)
> +{
> +	u32 tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
> +
> +	if (xdp->flags & XDP_FLAGS_SINFO_EXT)
> +		tailroom = 0;
> +	return xdp->data_hard_start + xdp->frame_sz - tailroom;
> +}

[Severity: Critical]
Can xdp_build_skb_from_zc() overflow the allocated skb buffer and overwrite
skb_shinfo(skb) on XDP_PASS?

With XDP_FLAGS_SINFO_EXT removing the skb_shared_info tailroom reservation
from xdp_data_hard_end() and xsk_pool_get_rx_frame_size(), packet data in a
zero-copy buffer can now extend all the way to xdp->data_hard_start +
xdp->frame_sz.  However, xdp_build_skb_from_zc() still allocates the skb
buffer using truesize = xdp->frame_sz:

net/core/xdp.c:xdp_build_skb_from_zc() {
	...
	u32 len = xdp->data_end - xdp->data_meta;
	u32 truesize = xdp->frame_sz;
	...
	data = page_pool_dev_alloc_va(pp, &truesize);
	if (unlikely(!data))
		goto out;

	skb = napi_build_skb(data, truesize);
	if (unlikely(!skb)) {
		page_pool_free_va(pp, data, true);
		goto out;
	}

	skb_reserve(skb, xdp->data_meta - xdp->data_hard_start);

	memcpy(__skb_put(skb, len), xdp->data_meta, LARGEST_ALIGN(len));
	...
}

Because napi_build_skb() carves SKB_DATA_ALIGN(sizeof(struct skb_shared_info))
out of the end of truesize, any zero-copy frame whose headroom plus
LARGEST_ALIGN(len) reaches into the last 320 bytes of xdp->frame_sz (such
as any multi-buffer head buffer on ice or idpf, or a buffer using the larger
UMEM headroom now permitted by xdp_umem_reg()) will copy past skb->end into
skb_shinfo(skb) and potentially past data + truesize into adjacent memory.

> diff --git a/include/net/xdp_sock_drv.h b/include/net/xdp_sock_drv.h
> index a40ad3f90f13c..fc22314e72c4e 100644
> --- a/include/net/xdp_sock_drv.h
> +++ b/include/net/xdp_sock_drv.h
[ ... ]
> @@ -226,8 +213,10 @@ static inline void xsk_buff_set_size(struct xdp_buff *xdp, u32 size)
>  	xdp->data = xdp->data_hard_start + XDP_PACKET_HEADROOM;
>  	xdp->data_meta = xdp->data;
>  	xdp->data_end = xdp->data + size;
> -	/* Frags flag is cleared when the frags of a head are released */
> -	xdp->flags &= XDP_FLAGS_HAS_FRAGS;
> +	/* Frags flag is cleared when the frags of a head are released. The
> +	 * shared info flag stays, see xp_set_rxq_info().
> +	 */
> +	xdp->flags &= XDP_FLAGS_HAS_FRAGS | XDP_FLAGS_SINFO_EXT;
>  }

[Severity: Medium]
Can an xdp_buff_xsk lose XDP_FLAGS_SINFO_EXT permanently when recycled after
virtio_net processes a mergeable multi-buffer RX packet?

xp_set_rxq_info() sets XDP_FLAGS_SINFO_EXT once at queue setup, and both
xp_alloc() and xsk_buff_set_size() only mask xdp->flags with &= rather than
setting the bit.  In virtio_net, buf_to_xdp() clears xdp->flags on follow-on
buffers before xsk_append_merge_buffer() frees them back to the pool:

drivers/net/virtio_net.c:buf_to_xdp() {
	...
	if (first_buf) {
		xsk_buff_set_size(xdp, len);
	} else {
		xdp_prepare_buff(xdp, xdp->data_hard_start,
				 XDP_PACKET_HEADROOM - vi->hdr_len, len, 1);
		xdp->flags = 0;
	}
	...
}

When that buffer is later reallocated as a head buffer, XDP_FLAGS_SINFO_EXT
remains cleared, causing xdp_get_shared_info_from_buff() to point back into
the UMEM chunk tailroom and xdp_data_hard_end() to subtract sizeof(struct
skb_shared_info) again.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261127142131.3527737-1-bjorn@kernel.org?part=2

  parent reply	other threads:[~2026-11-28 14:22 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27 14:21 [PATCH net 0/3] xsk: Keep the shared info of zero-copy buffers in kernel memory Björn Töpel
2026-11-27 14:21 ` [PATCH net 1/3] libeth: xsk: Count each XDP_TX buffer on its own Björn Töpel
2026-11-28 15:14   ` netdev-bot+sashiko
2026-11-30 11:58     ` Björn Töpel
2026-11-27 14:21 ` [PATCH net 2/3] xsk: Keep the shared info of zero-copy buffers in kernel memory Björn Töpel
2026-11-27 19:26   ` Maciej Fijalkowski
2026-11-30 12:05     ` Björn Töpel
2026-11-28 14:22   ` sashiko-bot [this message]
2026-11-28 15:14   ` netdev-bot+sashiko
2026-11-30 12:04     ` Björn Töpel
2026-11-27 14:21 ` [PATCH net 3/3] selftests/bpf: Drop the zero-copy tailroom from the xsk tests Björn Töpel
2026-11-27 15:49   ` Maciej Fijalkowski
2026-11-27 16:56     ` Björn Töpel
2026-11-27 14:24 ` [PATCH net 0/3] xsk: Keep the shared info of zero-copy buffers in kernel memory netdev-bot+sinfo
2026-11-30 11:53   ` Björn Töpel

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=sashiko-outbox-238767@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bjorn@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=intel-wired-lan@lists.osuosl.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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.