Skip to contentSkip to conversation

ether: check the first segment length on SW VLAN insertion

PATCH Applied Andrew Rybchenko 3 participants 4 messages May 27, 2020 – Sep 14, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ether: check the first segment length on SW VLAN insertion

SW VLAN insertion relies on Ethernet addresses location in contigous memory (do not split across mbuf segments). There is no any formal requirements on data location and mbuf structure which guarantee it. So, check it explicitly to avoid corrupted packets if the condition is violated. Typically software VLAN insertion is done on Tx prepare stage and application will get indication that the packet is invalid and cannot be transmitted.

---
 lib/librte_net/rte_ether.h | 4 ++++
 1 file changed, 4 insertions(+)
lib/librte_net/rte_ether.h +4 -0
index 0ae4e75b6c..d7c076bba8 100644
@@ -357,6 +357,10 @@ static inline int rte_vlan_insert(struct rte_mbuf **m)
 	if (!RTE_MBUF_DIRECT(*m) || rte_mbuf_refcnt_read(*m) > 1)
 		return -EINVAL;
 
+	/* Can't insert header if the first segment is too short */
+	if (rte_pktmbuf_data_len(*m) < 2 * RTE_ETHER_ADDR_LEN)
+		return -EINVAL;
+
 	oh = rte_pktmbuf_mtod(*m, struct rte_ether_hdr *);
 	nh = (struct rte_ether_hdr *)
 		rte_pktmbuf_prepend(*m, sizeof(struct rte_vlan_hdr));
Signature · 1 line
2.17.1
#1 lore raw
Quoted 18 lines · On Wed, 27 May 2020 15:31:41 +0100 Andrew Rybchenko…

SW VLAN insertion relies on Ethernet addresses location in contigous memory (do not split across mbuf segments). There is no any formal requirements on data location and mbuf structure which guarantee it. So, check it explicitly to avoid corrupted packets if the condition is violated. Typically software VLAN insertion is done on Tx prepare stage and application will get indication that the packet is invalid and cannot be transmitted.

---
 lib/librte_net/rte_ether.h | 4 ++++
 1 file changed, 4 insertions(+)
lib/librte_net/rte_ether.h +0 -0
index 0ae4e75b6c..d7c076bba8 100644

lib/librte_net/rte_ether.h static inline int rte_vlan_insert(struct rte_mbuf **m) · line 362

Quoted 6 lines
lib/librte_net/rte_ether.h +3 -0
@@ -357,6 +357,10 @@ static inline int rte_vlan_insert(struct rte_mbuf **m)
 	if (!RTE_MBUF_DIRECT(*m) || rte_mbuf_refcnt_read(*m) > 1)
 		return -EINVAL;
 
+	/* Can't insert header if the first segment is too short */
+	if (rte_pktmbuf_data_len(*m) < 2 * RTE_ETHER_ADDR_LEN)
+		return -EINVAL;

Looks good, but you could also make it handle the fragment case with:

lib/librte_net/rte_ether.h +6 -2
index 0ae4e75b6c58..4d0e310a4fac 100644
@@ -350,14 +350,18 @@ static inline int rte_vlan_strip(struct rte_mbuf *m)
  */
 static inline int rte_vlan_insert(struct rte_mbuf **m)
 {
-	struct rte_ether_hdr *oh, *nh;
+	struct rte_ether_hdr *nh, tmp;
+	const struct rte_ether_hdr *oh;
 	struct rte_vlan_hdr *vh;
 
 	/* Can't insert header if mbuf is shared */
 	if (!RTE_MBUF_DIRECT(*m) || rte_mbuf_refcnt_read(*m) > 1)
 		return -EINVAL;
 
-	oh = rte_pktmbuf_mtod(*m, struct rte_ether_hdr *);
+	oh = rte_pktmbuf_read(*m, 0, sizeof(*oh), &tmp);
+	if (unlikely(oh == NULL))
+		return -EINVAL;
+
 	nh = (struct rte_ether_hdr *)
 		rte_pktmbuf_prepend(*m, sizeof(struct rte_vlan_hdr));
 	if (nh == NULL)
#2 lore raw
Quoted 46 lines · Stephen Hemminger
Quoted 18 lines · On Wed, 27 May 2020 15:31:41 +0100 Andrew Rybchenko…

SW VLAN insertion relies on Ethernet addresses location in contigous memory (do not split across mbuf segments). There is no any formal requirements on data location and mbuf structure which guarantee it. So, check it explicitly to avoid corrupted packets if the condition is violated. Typically software VLAN insertion is done on Tx prepare stage and application will get indication that the packet is invalid and cannot be transmitted.

---
 lib/librte_net/rte_ether.h | 4 ++++
 1 file changed, 4 insertions(+)
lib/librte_net/rte_ether.h +0 -0
index 0ae4e75b6c..d7c076bba8 100644

lib/librte_net/rte_ether.h static inline int rte_vlan_insert(struct rte_mbuf **m) · line 362

Quoted 6 lines
lib/librte_net/rte_ether.h +3 -0
@@ -357,6 +357,10 @@ static inline int rte_vlan_insert(struct rte_mbuf **m)
 	if (!RTE_MBUF_DIRECT(*m) || rte_mbuf_refcnt_read(*m) > 1)
 		return -EINVAL;
 
+	/* Can't insert header if the first segment is too short */
+	if (rte_pktmbuf_data_len(*m) < 2 * RTE_ETHER_ADDR_LEN)
+		return -EINVAL;

Looks good, but you could also make it handle the fragment case with:

lib/librte_net/rte_ether.h +2 -1
index 0ae4e75b6c58..4d0e310a4fac 100644
@@ -350,14 +350,18 @@ static inline int rte_vlan_strip(struct rte_mbuf *m)
  */
 static inline int rte_vlan_insert(struct rte_mbuf **m)
 {
-	struct rte_ether_hdr *oh, *nh;
+	struct rte_ether_hdr *nh, tmp;
+	const struct rte_ether_hdr *oh;
 	struct rte_vlan_hdr *vh;
 
 	/* Can't insert header if mbuf is shared */
 	if (!RTE_MBUF_DIRECT(*m) || rte_mbuf_refcnt_read(*m) > 1)

lib/librte_net/rte_ether.h static inline int rte_vlan_strip(struct rte_mbuf *m) · line 367

Quoted 10 lines
lib/librte_net/rte_ether.h +4 -1
 		return -EINVAL;
 
-	oh = rte_pktmbuf_mtod(*m, struct rte_ether_hdr *);
+	oh = rte_pktmbuf_read(*m, 0, sizeof(*oh), &tmp);
+	if (unlikely(oh == NULL))
+		return -EINVAL;
+
 	nh = (struct rte_ether_hdr *)
 		rte_pktmbuf_prepend(*m, sizeof(struct rte_vlan_hdr));
 	if (nh == NULL)

It is more complicated since memmove() below should be rewritten in a similar way as rte_pktmbuf_read(), but write. I'm not sure it worse the effort.

Reviewed-by Applied
#3 lore raw
Quoted 48 lines · Andrew Rybchenko
Quoted 47 lines · Stephen Hemminger
Quoted 18 lines · On Wed, 27 May 2020 15:31:41 +0100 Andrew Rybchenko…

SW VLAN insertion relies on Ethernet addresses location in contigous memory (do not split across mbuf segments). There is no any formal requirements on data location and mbuf structure which guarantee it. So, check it explicitly to avoid corrupted packets if the condition is violated. Typically software VLAN insertion is done on Tx prepare stage and application will get indication that the packet is invalid and cannot be transmitted.

---
 lib/librte_net/rte_ether.h | 4 ++++
 1 file changed, 4 insertions(+)
lib/librte_net/rte_ether.h +0 -0
index 0ae4e75b6c..d7c076bba8 100644

lib/librte_net/rte_ether.h static inline int rte_vlan_insert(struct rte_mbuf **m) · line 362

Quoted 6 lines
lib/librte_net/rte_ether.h +3 -0
@@ -357,6 +357,10 @@ static inline int rte_vlan_insert(struct rte_mbuf **m)
 	if (!RTE_MBUF_DIRECT(*m) || rte_mbuf_refcnt_read(*m) > 1)
 		return -EINVAL;
 
+	/* Can't insert header if the first segment is too short */
+	if (rte_pktmbuf_data_len(*m) < 2 * RTE_ETHER_ADDR_LEN)
+		return -EINVAL;

Looks good, but you could also make it handle the fragment case with:

lib/librte_net/rte_ether.h +2 -1
index 0ae4e75b6c58..4d0e310a4fac 100644
@@ -350,14 +350,18 @@ static inline int rte_vlan_strip(struct rte_mbuf *m)
  */
 static inline int rte_vlan_insert(struct rte_mbuf **m)
 {
-	struct rte_ether_hdr *oh, *nh;
+	struct rte_ether_hdr *nh, tmp;
+	const struct rte_ether_hdr *oh;
 	struct rte_vlan_hdr *vh;
 
 	/* Can't insert header if mbuf is shared */
 	if (!RTE_MBUF_DIRECT(*m) || rte_mbuf_refcnt_read(*m) > 1)
 		return -EINVAL;
 
Quoted 14 lines

lib/librte_net/rte_ether.h static inline int rte_vlan_strip(struct rte_mbuf *m) · line 367

Quoted 9 lines
lib/librte_net/rte_ether.h +4 -1
-	oh = rte_pktmbuf_mtod(*m, struct rte_ether_hdr *);
+	oh = rte_pktmbuf_read(*m, 0, sizeof(*oh), &tmp);
+	if (unlikely(oh == NULL))
+		return -EINVAL;
+
 	nh = (struct rte_ether_hdr *)
 		rte_pktmbuf_prepend(*m, sizeof(struct rte_vlan_hdr));
 	if (nh == NULL)

It is more complicated since memmove() below should be rewritten in a similar way as rte_pktmbuf_read(), but write. I'm not sure it worse the effort.

Applied to dpdk-next-net/main, thanks.