Skip to contentSkip to conversation

virtio_net: Introduce skb_vnet_common_hdr to avoid typecasting

PATCH Superseded Feng Liu 2 participants 5 messages Aug 17, 2023

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Feng Liu

author
#0 lore raw

PATCH v2 1/1 virtio_net: Introduce skb_vnet_common_hdr to avoid typecasting

The virtio_net driver currently deals with different versions and types of virtio net headers, such as virtio_net_hdr_mrg_rxbuf, virtio_net_hdr_v1_hash, etc. Due to these variations, the code relies on multiple type casts to convert memory between different structures, potentially leading to bugs when there are changes in these structures.

Introduces the "struct skb_vnet_common_hdr" as a unifying header structure using a union. With this approach, various virtio net header structures can be converted by accessing different members of this structure, thus eliminating the need for type casting and reducing the risk of potential bugs.

For example following code:
static struct sk_buff *page_to_skb(struct virtnet_info *vi,
struct receive_queue *rq,
struct page *page, unsigned int offset, unsigned int len, unsigned int truesize, unsigned int headroom)
{
[...]

	struct virtio_net_hdr_mrg_rxbuf *hdr;
[...]
	hdr_len = vi->hdr_len;
[...]
ok:
	hdr = skb_vnet_hdr(skb);
	memcpy(hdr, hdr_p, hdr_len);

[...]
}

When VIRTIO_NET_F_HASH_REPORT feature is enabled, hdr_len = 20 But the sizeof(*hdr) is 12,
memcpy(hdr, hdr_p, hdr_len); will copy 20 bytes to the hdr, which make a potential risk of bug. And this risk can be avoided by introducing struct virtio_net_hdr_mrg_rxbuf.

Change log
v1->v2
feedback from Willem de Bruijn <[email protected]> feedback from Simon Horman <[email protected]>
1. change to use net-next tree.
2. move skb_vnet_common_hdr inside kernel file instead of the UAPI header.

---
 drivers/net/virtio_net.c | 36 +++++++++++++++++++++++-------------
 1 file changed, 23 insertions(+), 13 deletions(-)
drivers/net/virtio_net.c +23 -13
index 1270c8d23463..03cf744de512 100644
@@ -303,6 +303,13 @@ struct padded_vnet_hdr {
 	char padding[12];
 };
 
+struct virtio_net_common_hdr {
+	union {
+		struct virtio_net_hdr_mrg_rxbuf	mrg_hdr;
+		struct virtio_net_hdr_v1_hash hash_v1_hdr;
+	};
+};
+
 static void virtnet_rq_free_unused_buf(struct virtqueue *vq, void *buf);
 static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf);
 
@@ -344,9 +351,10 @@ static int rxq2vq(int rxq)
 	return rxq * 2;
 }
 
-static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb)
+static inline struct virtio_net_common_hdr *
+skb_vnet_common_hdr(struct sk_buff *skb)
 {
-	return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb;
+	return (struct virtio_net_common_hdr *)skb->cb;
 }
 
 /*
@@ -469,7 +477,7 @@ static struct sk_buff *page_to_skb(struct virtnet_info *vi,
 				   unsigned int headroom)
 {
 	struct sk_buff *skb;
-	struct virtio_net_hdr_mrg_rxbuf *hdr;
+	struct virtio_net_common_hdr *hdr;
 	unsigned int copy, hdr_len, hdr_padded_len;
 	struct page *page_to_free = NULL;
 	int tailroom, shinfo_size;
@@ -554,7 +562,7 @@ static struct sk_buff *page_to_skb(struct virtnet_info *vi,
 		give_pages(rq, page);
 
 ok:
-	hdr = skb_vnet_hdr(skb);
+	hdr = skb_vnet_common_hdr(skb);
 	memcpy(hdr, hdr_p, hdr_len);
 	if (page_to_free)
 		put_page(page_to_free);
@@ -966,7 +974,7 @@ static struct sk_buff *receive_small_build_skb(struct virtnet_info *vi,
 		return NULL;
 
 	buf += header_offset;
-	memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len);
+	memcpy(skb_vnet_common_hdr(skb), buf, vi->hdr_len);
 
 	return skb;
 }
@@ -1577,7 +1585,8 @@ static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
 {
 	struct net_device *dev = vi->dev;
 	struct sk_buff *skb;
-	struct virtio_net_hdr_mrg_rxbuf *hdr;
+	struct virtio_net_common_hdr *common_hdr;
+	struct virtio_net_hdr_mrg_rxbuf	*mrg_hdr;
 
 	if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
 		pr_debug("%s: short packet %i\n", dev->name, len);
@@ -1597,18 +1606,19 @@ static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
 	if (unlikely(!skb))
 		return;
 
-	hdr = skb_vnet_hdr(skb);
+	common_hdr = skb_vnet_common_hdr(skb);
 	if (dev->features & NETIF_F_RXHASH && vi->has_rss_hash_report)
-		virtio_skb_set_hash((const struct virtio_net_hdr_v1_hash *)hdr, skb);
+		virtio_skb_set_hash(&common_hdr->hash_v1_hdr, skb);
 
-	if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
+	mrg_hdr = &common_hdr->mrg_hdr;
+	if (mrg_hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
 		skb->ip_summed = CHECKSUM_UNNECESSARY;
 
-	if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
+	if (virtio_net_hdr_to_skb(skb, &mrg_hdr->hdr,
 				  virtio_is_little_endian(vi->vdev))) {
 		net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
-				     dev->name, hdr->hdr.gso_type,
-				     hdr->hdr.gso_size);
+				     dev->name, mrg_hdr->hdr.gso_type,
+				     mrg_hdr->hdr.gso_size);
 		goto frame_err;
 	}
 
@@ -2105,7 +2115,7 @@ static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
 	if (can_push)
 		hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
 	else
-		hdr = skb_vnet_hdr(skb);
+		hdr = &skb_vnet_common_hdr(skb)->mrg_hdr;
 
 	if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
 				    virtio_is_little_endian(vi->vdev), false,
Signature · 1 line
2.37.1 (Apple Git-137.1)
#1 lore raw
Quoted 28 lines · Feng Liu

The virtio_net driver currently deals with different versions and types of virtio net headers, such as virtio_net_hdr_mrg_rxbuf, virtio_net_hdr_v1_hash, etc. Due to these variations, the code relies on multiple type casts to convert memory between different structures, potentially leading to bugs when there are changes in these structures.

Introduces the "struct skb_vnet_common_hdr" as a unifying header structure using a union. With this approach, various virtio net header structures can be converted by accessing different members of this structure, thus eliminating the need for type casting and reducing the risk of potential bugs.

For example following code:
static struct sk_buff *page_to_skb(struct virtnet_info *vi,
struct receive_queue *rq,
struct page *page, unsigned int offset, unsigned int len, unsigned int truesize, unsigned int headroom)
{
[...]

        struct virtio_net_hdr_mrg_rxbuf *hdr;
[...]
        hdr_len = vi->hdr_len;
[...]
ok:
        hdr = skb_vnet_hdr(skb);
        memcpy(hdr, hdr_p, hdr_len);
Quoted 8 lines

[...]
}

When VIRTIO_NET_F_HASH_REPORT feature is enabled, hdr_len = 20 But the sizeof(*hdr) is 12,
memcpy(hdr, hdr_p, hdr_len); will copy 20 bytes to the hdr, which make a potential risk of bug. And this risk can be avoided by introducing struct virtio_net_hdr_mrg_rxbuf.

You mean virtio_net_common_hdr?

I'm not sure I follow the reasoning. Because then hdr_len might be sizeof(virtio_net_hdr_mrg_rxbuf), but sizeof(virtio_net_common_hdr) is larger. So the same issue remains?

Indeed, everywhere this patches replaces the one with the other, you have to verify that nothing was using sizeof(*hdr). Which would not be visible from the truncated patch contents itself.

Quoted 19 lines

Change log
v1->v2
feedback from Willem de Bruijn <[email protected]> feedback from Simon Horman <[email protected]>
1. change to use net-next tree.
2. move skb_vnet_common_hdr inside kernel file instead of the UAPI header.

---
 drivers/net/virtio_net.c | 36 +++++++++++++++++++++++-------------
 1 file changed, 23 insertions(+), 13 deletions(-)
drivers/net/virtio_net.c +0 -0
index 1270c8d23463..03cf744de512 100644

drivers/net/virtio_net.c struct padded_vnet_hdr { · line 311

Quoted 9 lines
drivers/net/virtio_net.c +6 -0
@@ -303,6 +303,13 @@ struct padded_vnet_hdr {
        char padding[12];
 };
 
+struct virtio_net_common_hdr {
+       union {
+               struct virtio_net_hdr_mrg_rxbuf mrg_hdr;
+               struct virtio_net_hdr_v1_hash hash_v1_hdr;
+       };
+};

Perhaps even add in struct virtio_net_hdr. As that is the original of the three structs, and all the initial fields overlap.

Quoted 45 lines
drivers/net/virtio_net.c +7 -5
+
 static void virtnet_rq_free_unused_buf(struct virtqueue *vq, void *buf);
 static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf);
 
@@ -344,9 +351,10 @@ static int rxq2vq(int rxq)
        return rxq * 2;
 }
 
-static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb)
+static inline struct virtio_net_common_hdr *
+skb_vnet_common_hdr(struct sk_buff *skb)
 {
-       return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb;
+       return (struct virtio_net_common_hdr *)skb->cb;
 }
 
 /*
@@ -469,7 +477,7 @@ static struct sk_buff *page_to_skb(struct virtnet_info *vi,
                                   unsigned int headroom)
 {
        struct sk_buff *skb;
-       struct virtio_net_hdr_mrg_rxbuf *hdr;
+       struct virtio_net_common_hdr *hdr;
        unsigned int copy, hdr_len, hdr_padded_len;
        struct page *page_to_free = NULL;
        int tailroom, shinfo_size;
@@ -554,7 +562,7 @@ static struct sk_buff *page_to_skb(struct virtnet_info *vi,
                give_pages(rq, page);
 
 ok:
-       hdr = skb_vnet_hdr(skb);
+       hdr = skb_vnet_common_hdr(skb);
        memcpy(hdr, hdr_p, hdr_len);
        if (page_to_free)
                put_page(page_to_free);
@@ -966,7 +974,7 @@ static struct sk_buff *receive_small_build_skb(struct virtnet_info *vi,
                return NULL;
 
        buf += header_offset;
-       memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len);
+       memcpy(skb_vnet_common_hdr(skb), buf, vi->hdr_len);
 
        return skb;
 }

drivers/net/virtio_net.c static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq, · line 1589

Quoted 6 lines
drivers/net/virtio_net.c +2 -1
@@ -1577,7 +1585,8 @@ static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
 {
        struct net_device *dev = vi->dev;
        struct sk_buff *skb;
-       struct virtio_net_hdr_mrg_rxbuf *hdr;
+       struct virtio_net_common_hdr *common_hdr;
+       struct virtio_net_hdr_mrg_rxbuf *mrg_hdr;

No more need for this second struct now that we have the union. That's its whole purpose?

Quoted 39 lines
drivers/net/virtio_net.c +8 -8
        if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
                pr_debug("%s: short packet %i\n", dev->name, len);
@@ -1597,18 +1606,19 @@ static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
        if (unlikely(!skb))
                return;
 
-       hdr = skb_vnet_hdr(skb);
+       common_hdr = skb_vnet_common_hdr(skb);
        if (dev->features & NETIF_F_RXHASH && vi->has_rss_hash_report)
-               virtio_skb_set_hash((const struct virtio_net_hdr_v1_hash *)hdr, skb);
+               virtio_skb_set_hash(&common_hdr->hash_v1_hdr, skb);
 
-       if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
+       mrg_hdr = &common_hdr->mrg_hdr;
+       if (mrg_hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
                skb->ip_summed = CHECKSUM_UNNECESSARY;
 
-       if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
+       if (virtio_net_hdr_to_skb(skb, &mrg_hdr->hdr,
                                  virtio_is_little_endian(vi->vdev))) {
                net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
-                                    dev->name, hdr->hdr.gso_type,
-                                    hdr->hdr.gso_size);
+                                    dev->name, mrg_hdr->hdr.gso_type,
+                                    mrg_hdr->hdr.gso_size);
                goto frame_err;
        }
 
@@ -2105,7 +2115,7 @@ static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
        if (can_push)
                hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
        else
-               hdr = skb_vnet_hdr(skb);
+               hdr = &skb_vnet_common_hdr(skb)->mrg_hdr;
 
        if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
                                    virtio_is_little_endian(vi->vdev), false,
--
Quoted 2 lines

2.37.1 (Apple Git-137.1)

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

author
#2 lore raw
Quoted 35 lines · Willem de Bruijn

External email: Use caution opening links or attachments

Quoted 31 lines · Feng Liu

The virtio_net driver currently deals with different versions and types of virtio net headers, such as virtio_net_hdr_mrg_rxbuf, virtio_net_hdr_v1_hash, etc. Due to these variations, the code relies on multiple type casts to convert memory between different structures, potentially leading to bugs when there are changes in these structures.

Introduces the "struct skb_vnet_common_hdr" as a unifying header structure using a union. With this approach, various virtio net header structures can be converted by accessing different members of this structure, thus eliminating the need for type casting and reducing the risk of potential bugs.

For example following code:
static struct sk_buff *page_to_skb(struct virtnet_info *vi,
struct receive_queue *rq,
struct page *page, unsigned int offset, unsigned int len, unsigned int truesize, unsigned int headroom)
{
[...]

         struct virtio_net_hdr_mrg_rxbuf *hdr;
[...]
         hdr_len = vi->hdr_len;
[...]
ok:
         hdr = skb_vnet_hdr(skb);
         memcpy(hdr, hdr_p, hdr_len);

[...]
}

Quoted 8 lines
Quoted 5 lines

When VIRTIO_NET_F_HASH_REPORT feature is enabled, hdr_len = 20 But the sizeof(*hdr) is 12,
memcpy(hdr, hdr_p, hdr_len); will copy 20 bytes to the hdr, which make a potential risk of bug. And this risk can be avoided by introducing struct virtio_net_hdr_mrg_rxbuf.

You mean virtio_net_common_hdr?

It is a typo, will correct it.

Quoted 4 lines

I'm not sure I follow the reasoning. Because then hdr_len might be sizeof(virtio_net_hdr_mrg_rxbuf), but sizeof(virtio_net_common_hdr) is larger. So the same issue remains?

static int virtnet_probe(struct virtio_device *vdev) {
[...]

	if (vi->has_rss_hash_report) {
		vi->hdr_len = sizeof(struct virtio_net_hdr_v1_hash); /* hdr_len will 
be 20 bytes */
	}
	else if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
		 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
		vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
	else
		vi->hdr_len = sizeof(struct virtio_net_hdr);

[...]
}

When VIRTIO_NET_F_HASH_REPORT is enabled, hdr_len = 20 (as above); and the size of virtio_net_hdr_mrg_rxbuf is 12, so virtio_net_hdr_mrg_rxbuf is wrong, should use struct virtio_net_common_hdr here.

Quoted 4 lines

Indeed, everywhere this patches replaces the one with the other, you have to verify that nothing was using sizeof(*hdr). Which would not be visible from the truncated patch contents itself.

Have checked. Nothing is using sizeof(*hdr).

Quoted 22 lines
Quoted 22 lines

Change log
v1->v2
feedback from Willem de Bruijn <[email protected]> feedback from Simon Horman <[email protected]>
1. change to use net-next tree.
2. move skb_vnet_common_hdr inside kernel file instead of the UAPI header.

---
  drivers/net/virtio_net.c | 36 +++++++++++++++++++++++-------------
  1 file changed, 23 insertions(+), 13 deletions(-)
drivers/net/virtio_net.c +0 -0
index 1270c8d23463..03cf744de512 100644
@@ -303,6 +303,13 @@ struct padded_vnet_hdr {
         char padding[12];
  };
 
Quoted 10 lines

drivers/net/virtio_net.c struct padded_vnet_hdr { · line 311

Quoted 6 lines
drivers/net/virtio_net.c +6 -0
+struct virtio_net_common_hdr {
+       union {
+               struct virtio_net_hdr_mrg_rxbuf mrg_hdr;
+               struct virtio_net_hdr_v1_hash hash_v1_hdr;
+       };
+};

Perhaps even add in struct virtio_net_hdr. As that is the original of the three structs, and all the initial fields overlap.

But I didn't use virtio_net_hdr in this patch, is it redundant to put it here? what do you think?

Quoted 42 lines
Quoted 42 lines
drivers/net/virtio_net.c +7 -5
+
  static void virtnet_rq_free_unused_buf(struct virtqueue *vq, void *buf);
  static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf);
 
@@ -344,9 +351,10 @@ static int rxq2vq(int rxq)
         return rxq * 2;
  }
 
-static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb)
+static inline struct virtio_net_common_hdr *
+skb_vnet_common_hdr(struct sk_buff *skb)
  {
-       return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb;
+       return (struct virtio_net_common_hdr *)skb->cb;
  }
 
  /*
@@ -469,7 +477,7 @@ static struct sk_buff *page_to_skb(struct virtnet_info *vi,
                                    unsigned int headroom)
  {
         struct sk_buff *skb;
-       struct virtio_net_hdr_mrg_rxbuf *hdr;
+       struct virtio_net_common_hdr *hdr;
         unsigned int copy, hdr_len, hdr_padded_len;
         struct page *page_to_free = NULL;
         int tailroom, shinfo_size;
@@ -554,7 +562,7 @@ static struct sk_buff *page_to_skb(struct virtnet_info *vi,
                 give_pages(rq, page);
 
  ok:
-       hdr = skb_vnet_hdr(skb);
+       hdr = skb_vnet_common_hdr(skb);
         memcpy(hdr, hdr_p, hdr_len);
         if (page_to_free)
                 put_page(page_to_free);
@@ -966,7 +974,7 @@ static struct sk_buff *receive_small_build_skb(struct virtnet_info *vi,
                 return NULL;
 
         buf += header_offset;
-       memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len);
+       memcpy(skb_vnet_common_hdr(skb), buf, vi->hdr_len);
 
Quoted 12 lines

drivers/net/virtio_net.c static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq, · line 1589

Quoted 9 lines
drivers/net/virtio_net.c +2 -1
         return skb;
  }
@@ -1577,7 +1585,8 @@ static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
  {
         struct net_device *dev = vi->dev;
         struct sk_buff *skb;
-       struct virtio_net_hdr_mrg_rxbuf *hdr;
+       struct virtio_net_common_hdr *common_hdr;
+       struct virtio_net_hdr_mrg_rxbuf *mrg_hdr;

No more need for this second struct now that we have the union. That's its whole purpose?

Yes, struct virtio_net_hdr_mrg_rxbuf *mrg_hdr is not needed. Writing mrg_hdr here is just to make the code look more concise, such as mrg_hdr->hdr.flags, if mrg_hdr is not used, it should be written as common_hdr->mrg_hdr.hdr.flags, I think it looks too long. what you think?

Quoted 41 lines
Quoted 41 lines
drivers/net/virtio_net.c +8 -8
         if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
                 pr_debug("%s: short packet %i\n", dev->name, len);
@@ -1597,18 +1606,19 @@ static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
         if (unlikely(!skb))
                 return;
 
-       hdr = skb_vnet_hdr(skb);
+       common_hdr = skb_vnet_common_hdr(skb);
         if (dev->features & NETIF_F_RXHASH && vi->has_rss_hash_report)
-               virtio_skb_set_hash((const struct virtio_net_hdr_v1_hash *)hdr, skb);
+               virtio_skb_set_hash(&common_hdr->hash_v1_hdr, skb);
 
-       if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
+       mrg_hdr = &common_hdr->mrg_hdr;
+       if (mrg_hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
                 skb->ip_summed = CHECKSUM_UNNECESSARY;
 
-       if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
+       if (virtio_net_hdr_to_skb(skb, &mrg_hdr->hdr,
                                   virtio_is_little_endian(vi->vdev))) {
                 net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
-                                    dev->name, hdr->hdr.gso_type,
-                                    hdr->hdr.gso_size);
+                                    dev->name, mrg_hdr->hdr.gso_type,
+                                    mrg_hdr->hdr.gso_size);
                 goto frame_err;
         }
 
@@ -2105,7 +2115,7 @@ static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
         if (can_push)
                 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
         else
-               hdr = skb_vnet_hdr(skb);
+               hdr = &skb_vnet_common_hdr(skb)->mrg_hdr;
 
         if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
                                     virtio_is_little_endian(vi->vdev), false,
--

2.37.1 (Apple Git-137.1)

#3 lore raw
Quoted 65 lines · Feng Liu
Quoted 35 lines · Willem de Bruijn

External email: Use caution opening links or attachments

Quoted 31 lines · Feng Liu

The virtio_net driver currently deals with different versions and types of virtio net headers, such as virtio_net_hdr_mrg_rxbuf, virtio_net_hdr_v1_hash, etc. Due to these variations, the code relies on multiple type casts to convert memory between different structures, potentially leading to bugs when there are changes in these structures.

Introduces the "struct skb_vnet_common_hdr" as a unifying header structure using a union. With this approach, various virtio net header structures can be converted by accessing different members of this structure, thus eliminating the need for type casting and reducing the risk of potential bugs.

For example following code:
static struct sk_buff *page_to_skb(struct virtnet_info *vi,
struct receive_queue *rq,
struct page *page, unsigned int offset, unsigned int len, unsigned int truesize, unsigned int headroom)
{
[...]

         struct virtio_net_hdr_mrg_rxbuf *hdr;
[...]
         hdr_len = vi->hdr_len;
[...]
ok:
         hdr = skb_vnet_hdr(skb);
         memcpy(hdr, hdr_p, hdr_len);

[...]
}

Quoted 8 lines
Quoted 5 lines

When VIRTIO_NET_F_HASH_REPORT feature is enabled, hdr_len = 20 But the sizeof(*hdr) is 12,
memcpy(hdr, hdr_p, hdr_len); will copy 20 bytes to the hdr, which make a potential risk of bug. And this risk can be avoided by introducing struct virtio_net_hdr_mrg_rxbuf.

You mean virtio_net_common_hdr?

It is a typo, will correct it.

Quoted 4 lines

I'm not sure I follow the reasoning. Because then hdr_len might be sizeof(virtio_net_hdr_mrg_rxbuf), but sizeof(virtio_net_common_hdr) is larger. So the same issue remains?

static int virtnet_probe(struct virtio_device *vdev) {
[...]

        if (vi->has_rss_hash_report) {
                vi->hdr_len = sizeof(struct virtio_net_hdr_v1_hash); /* hdr_len will
be 20 bytes */
        }
        else if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
                 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
                vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
        else
                vi->hdr_len = sizeof(struct virtio_net_hdr);
Quoted 6 lines

[...]
}

When VIRTIO_NET_F_HASH_REPORT is enabled, hdr_len = 20 (as above); and the size of virtio_net_hdr_mrg_rxbuf is 12, so virtio_net_hdr_mrg_rxbuf is wrong, should use struct virtio_net_common_hdr here.

I understand in this specific instance. I'm just saying that using sizeof can be wrong both in the new and old case.

This does not fix a real bug, as memcpy just uses hdr_len, which is correct.

Quoted 5 lines
Quoted 4 lines

Indeed, everywhere this patches replaces the one with the other, you have to verify that nothing was using sizeof(*hdr). Which would not be visible from the truncated patch contents itself.

Have checked. Nothing is using sizeof(*hdr).

Thanks.

Quoted 34 lines
Quoted 23 lines
Quoted 23 lines

Change log
v1->v2
feedback from Willem de Bruijn <[email protected]> feedback from Simon Horman <[email protected]>
1. change to use net-next tree.
2. move skb_vnet_common_hdr inside kernel file instead of the UAPI header.

---
  drivers/net/virtio_net.c | 36 +++++++++++++++++++++++-------------
  1 file changed, 23 insertions(+), 13 deletions(-)
drivers/net/virtio_net.c +0 -0
index 1270c8d23463..03cf744de512 100644
@@ -303,6 +303,13 @@ struct padded_vnet_hdr {
         char padding[12];
  };
 
Quoted 10 lines

drivers/net/virtio_net.c struct padded_vnet_hdr { · line 311

Quoted 6 lines
drivers/net/virtio_net.c +6 -0
+struct virtio_net_common_hdr {
+       union {
+               struct virtio_net_hdr_mrg_rxbuf mrg_hdr;
+               struct virtio_net_hdr_v1_hash hash_v1_hdr;
+       };
+};

Perhaps even add in struct virtio_net_hdr. As that is the original of the three structs, and all the initial fields overlap.

Quoted 2 lines

But I didn't use virtio_net_hdr in this patch, is it redundant to put it here? what do you think?

That's true. But if we're going to add a helper to bind together alll the virtio variants, then I think it should be there?

No strong opinion. Leave out if you prefer and no one else speaks up.

Quoted 11 lines
Quoted 10 lines

drivers/net/virtio_net.c static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq, · line 1589

Quoted 7 lines
drivers/net/virtio_net.c +2 -1
@@ -1577,7 +1585,8 @@ static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
  {
         struct net_device *dev = vi->dev;
         struct sk_buff *skb;
-       struct virtio_net_hdr_mrg_rxbuf *hdr;
+       struct virtio_net_common_hdr *common_hdr;
+       struct virtio_net_hdr_mrg_rxbuf *mrg_hdr;

No more need for this second struct now that we have the union. That's its whole purpose?

Quoted 4 lines

Yes, struct virtio_net_hdr_mrg_rxbuf *mrg_hdr is not needed. Writing mrg_hdr here is just to make the code look more concise, such as mrg_hdr->hdr.flags, if mrg_hdr is not used, it should be written as common_hdr->mrg_hdr.hdr.flags, I think it looks too long. what you think?

If we're going to continue to assign to different structs, then I'm honestly not sure how much this patch buys us.

Adding virtio_net_hdr to the union also shortens the code btw. Then it can be common_hdr->hdr.flags

Also, just a shorter variable name than common_hdr. Fine to call it hdr.

Quoted 37 lines
Quoted 37 lines
Quoted 37 lines
drivers/net/virtio_net.c +8 -7
         if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
                 pr_debug("%s: short packet %i\n", dev->name, len);
@@ -1597,18 +1606,19 @@ static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
         if (unlikely(!skb))
                 return;
 
-       hdr = skb_vnet_hdr(skb);
+       common_hdr = skb_vnet_common_hdr(skb);
         if (dev->features & NETIF_F_RXHASH && vi->has_rss_hash_report)
-               virtio_skb_set_hash((const struct virtio_net_hdr_v1_hash *)hdr, skb);
+               virtio_skb_set_hash(&common_hdr->hash_v1_hdr, skb);
 
-       if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
+       mrg_hdr = &common_hdr->mrg_hdr;
+       if (mrg_hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
                 skb->ip_summed = CHECKSUM_UNNECESSARY;
 
-       if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
+       if (virtio_net_hdr_to_skb(skb, &mrg_hdr->hdr,
                                   virtio_is_little_endian(vi->vdev))) {
                 net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
-                                    dev->name, hdr->hdr.gso_type,
-                                    hdr->hdr.gso_size);
+                                    dev->name, mrg_hdr->hdr.gso_type,
+                                    mrg_hdr->hdr.gso_size);
                 goto frame_err;
         }
 
@@ -2105,7 +2115,7 @@ static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
         if (can_push)
                 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
         else
-               hdr = skb_vnet_hdr(skb);
+               hdr = &skb_vnet_common_hdr(skb)->mrg_hdr;
 
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
drivers/net/virtio_net.c +0 -1
         if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
                                     virtio_is_little_endian(vi->vdev), false,
--

2.37.1 (Apple Git-137.1)

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

author
#4 lore raw
Quoted 4 lines · Willem de Bruijn
Quoted 4 lines
Quoted 2 lines

You mean virtio_net_common_hdr?

It is a typo, will correct it.

will do

Quoted 19 lines
Quoted 19 lines
Quoted 4 lines

I'm not sure I follow the reasoning. Because then hdr_len might be sizeof(virtio_net_hdr_mrg_rxbuf), but sizeof(virtio_net_common_hdr) is larger. So the same issue remains?

static int virtnet_probe(struct virtio_device *vdev) {
[...]

         if (vi->has_rss_hash_report) {
                 vi->hdr_len = sizeof(struct virtio_net_hdr_v1_hash); /* hdr_len will
be 20 bytes */
         }
         else if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
                  virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
                 vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
         else
                 vi->hdr_len = sizeof(struct virtio_net_hdr);

[...]
}

Quoted 9 lines
Quoted 3 lines

When VIRTIO_NET_F_HASH_REPORT is enabled, hdr_len = 20 (as above); and the size of virtio_net_hdr_mrg_rxbuf is 12, so virtio_net_hdr_mrg_rxbuf is wrong, should use struct virtio_net_common_hdr here.

I understand in this specific instance. I'm just saying that using sizeof can be wrong both in the new and old case.

This does not fix a real bug, as memcpy just uses hdr_len, which is correct.

Already have used hdr_len in the patch instead of sizeof(*hdr)

Quoted 7 lines
Quoted 5 lines
Quoted 4 lines

Indeed, everywhere this patches replaces the one with the other, you have to verify that nothing was using sizeof(*hdr). Which would not be visible from the truncated patch contents itself.

Have checked. Nothing is using sizeof(*hdr).

Thanks.

:-D

Quoted 34 lines
Quoted 34 lines
Quoted 33 lines
Quoted 20 lines

Change log
v1->v2
feedback from Willem de Bruijn <[email protected]> feedback from Simon Horman <[email protected]>
1. change to use net-next tree.
2. move skb_vnet_common_hdr inside kernel file instead of the UAPI header.

---
   drivers/net/virtio_net.c | 36 +++++++++++++++++++++++-------------
   1 file changed, 23 insertions(+), 13 deletions(-)
drivers/net/virtio_net.c +0 -0
index 1270c8d23463..03cf744de512 100644

drivers/net/virtio_net.c struct padded_vnet_hdr { · line 311

Quoted 9 lines
drivers/net/virtio_net.c +6 -0
@@ -303,6 +303,13 @@ struct padded_vnet_hdr {
          char padding[12];
   };
 
+struct virtio_net_common_hdr {
+       union {
+               struct virtio_net_hdr_mrg_rxbuf mrg_hdr;
+               struct virtio_net_hdr_v1_hash hash_v1_hdr;
+       };
+};

Perhaps even add in struct virtio_net_hdr. As that is the original of the three structs, and all the initial fields overlap.

Quoted 8 lines
Quoted 2 lines

But I didn't use virtio_net_hdr in this patch, is it redundant to put it here? what do you think?

That's true. But if we're going to add a helper to bind together alll the virtio variants, then I think it should be there?

No strong opinion. Leave out if you prefer and no one else speaks up.

will do

Quoted 16 lines
Quoted 11 lines
Quoted 10 lines

drivers/net/virtio_net.c static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq, · line 1589

Quoted 7 lines
drivers/net/virtio_net.c +2 -1
@@ -1577,7 +1585,8 @@ static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
   {
          struct net_device *dev = vi->dev;
          struct sk_buff *skb;
-       struct virtio_net_hdr_mrg_rxbuf *hdr;
+       struct virtio_net_common_hdr *common_hdr;
+       struct virtio_net_hdr_mrg_rxbuf *mrg_hdr;

No more need for this second struct now that we have the union. That's its whole purpose?

Quoted 4 lines

Yes, struct virtio_net_hdr_mrg_rxbuf *mrg_hdr is not needed. Writing mrg_hdr here is just to make the code look more concise, such as mrg_hdr->hdr.flags, if mrg_hdr is not used, it should be written as common_hdr->mrg_hdr.hdr.flags, I think it looks too long. what you think?

Quoted 8 lines

If we're going to continue to assign to different structs, then I'm honestly not sure how much this patch buys us.

Adding virtio_net_hdr to the union also shortens the code btw. Then it can be common_hdr->hdr.flags

Also, just a shorter variable name than common_hdr. Fine to call it hdr.

will do

Quoted 42 lines
Quoted 42 lines
Quoted 42 lines
Quoted 42 lines
drivers/net/virtio_net.c +8 -8
          if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
                  pr_debug("%s: short packet %i\n", dev->name, len);
@@ -1597,18 +1606,19 @@ static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
          if (unlikely(!skb))
                  return;
 
-       hdr = skb_vnet_hdr(skb);
+       common_hdr = skb_vnet_common_hdr(skb);
          if (dev->features & NETIF_F_RXHASH && vi->has_rss_hash_report)
-               virtio_skb_set_hash((const struct virtio_net_hdr_v1_hash *)hdr, skb);
+               virtio_skb_set_hash(&common_hdr->hash_v1_hdr, skb);
 
-       if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
+       mrg_hdr = &common_hdr->mrg_hdr;
+       if (mrg_hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
                  skb->ip_summed = CHECKSUM_UNNECESSARY;
 
-       if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
+       if (virtio_net_hdr_to_skb(skb, &mrg_hdr->hdr,
                                    virtio_is_little_endian(vi->vdev))) {
                  net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
-                                    dev->name, hdr->hdr.gso_type,
-                                    hdr->hdr.gso_size);
+                                    dev->name, mrg_hdr->hdr.gso_type,
+                                    mrg_hdr->hdr.gso_size);
                  goto frame_err;
          }
 
@@ -2105,7 +2115,7 @@ static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
          if (can_push)
                  hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
          else
-               hdr = skb_vnet_hdr(skb);
+               hdr = &skb_vnet_common_hdr(skb)->mrg_hdr;
 
          if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
                                      virtio_is_little_endian(vi->vdev), false,
--

2.37.1 (Apple Git-137.1)