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 <[email protected]>

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/[email protected]?part=2

Reply via email to