Skip to content

Commit 981f14d

Browse files
Heng Qikuba-moo
authored andcommitted
virtio-net: fix possible unsigned integer overflow
When the single-buffer xdp is loaded and after xdp_linearize_page() is called, *num_buf becomes 0 and (*num_buf - 1) may overflow into a large integer in virtnet_build_xdp_buff_mrg(), resulting in unexpected packet dropping. Fixes: ef75cb5 ("virtio-net: build xdp_buff with multi buffers") Signed-off-by: Heng Qi <hengqi@linux.alibaba.com> Reviewed-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> Link: https://lore.kernel.org/r/20230131085004.98687-1-hengqi@linux.alibaba.com Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent 028fb19 commit 981f14d

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

drivers/net/virtio_net.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@ static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
723723
* have enough headroom.
724724
*/
725725
static struct page *xdp_linearize_page(struct receive_queue *rq,
726-
u16 *num_buf,
726+
int *num_buf,
727727
struct page *p,
728728
int offset,
729729
int page_off,
@@ -823,7 +823,7 @@ static struct sk_buff *receive_small(struct net_device *dev,
823823
if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) {
824824
int offset = buf - page_address(page) + header_offset;
825825
unsigned int tlen = len + vi->hdr_len;
826-
u16 num_buf = 1;
826+
int num_buf = 1;
827827

828828
xdp_headroom = virtnet_get_headroom(vi);
829829
header_offset = VIRTNET_RX_PAD + xdp_headroom;
@@ -996,7 +996,7 @@ static int virtnet_build_xdp_buff_mrg(struct net_device *dev,
996996
void *buf,
997997
unsigned int len,
998998
unsigned int frame_sz,
999-
u16 *num_buf,
999+
int *num_buf,
10001000
unsigned int *xdp_frags_truesize,
10011001
struct virtnet_rq_stats *stats)
10021002
{
@@ -1014,6 +1014,9 @@ static int virtnet_build_xdp_buff_mrg(struct net_device *dev,
10141014
xdp_prepare_buff(xdp, buf - VIRTIO_XDP_HEADROOM,
10151015
VIRTIO_XDP_HEADROOM + vi->hdr_len, len - vi->hdr_len, true);
10161016

1017+
if (!*num_buf)
1018+
return 0;
1019+
10171020
if (*num_buf > 1) {
10181021
/* If we want to build multi-buffer xdp, we need
10191022
* to specify that the flags of xdp_buff have the
@@ -1027,10 +1030,10 @@ static int virtnet_build_xdp_buff_mrg(struct net_device *dev,
10271030
shinfo->xdp_frags_size = 0;
10281031
}
10291032

1030-
if ((*num_buf - 1) > MAX_SKB_FRAGS)
1033+
if (*num_buf > MAX_SKB_FRAGS + 1)
10311034
return -EINVAL;
10321035

1033-
while ((--*num_buf) >= 1) {
1036+
while (--*num_buf > 0) {
10341037
buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx);
10351038
if (unlikely(!buf)) {
10361039
pr_debug("%s: rx error: %d buffers out of %d missing\n",
@@ -1083,7 +1086,7 @@ static struct sk_buff *receive_mergeable(struct net_device *dev,
10831086
struct virtnet_rq_stats *stats)
10841087
{
10851088
struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
1086-
u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
1089+
int num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
10871090
struct page *page = virt_to_head_page(buf);
10881091
int offset = buf - page_address(page);
10891092
struct sk_buff *head_skb, *curr_skb;

0 commit comments

Comments
 (0)