vhost: add header check in dequeue offload
PATCH 1/1 vhost: add header check in dequeue offload
When parsing the virtio net header and packet header for dequeue offload, we need to perform sanity check on the packet header to ensure:
- No out-of-boundary memory access.
- The packet header and virtio_net header are valid and aligned.
--- lib/librte_vhost/virtio_net.c | 49 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 6 deletions(-)
lib/librte_vhost/virtio_net.c +43 -6
@@ -1821,44 +1821,64 @@ virtio_net_with_host_offload(struct virtio_net *dev)return false; } -static void -parse_ethernet (struct rte_mbuf *m,uint16_t *l4_proto,void **l4_hdr) +static int +parse_ethernet (struct rte_mbuf *m,uint16_t *l4_proto,void **l4_hdr, +uint16_t *len) {struct rte_ipv4_hdr *ipv4_hdr;struct rte_ipv6_hdr *ipv6_hdr;void *l3_hdr =NULL ;struct rte_ether_hdr *eth_hdr;uint16_t ethertype; +uint16_t data_len = m->data_len; eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *); +if (data_len <=sizeof (struct rte_ether_hdr )) +return -EINVAL ; + m->l2_len =sizeof (struct rte_ether_hdr ); ethertype =rte_be_to_cpu_16 (eth_hdr->ether_type); + data_len -=sizeof (struct rte_ether_hdr );if (ethertype ==RTE_ETHER_TYPE_VLAN ) { +if (data_len <=sizeof (struct rte_vlan_hdr )) +return -EINVAL ; +struct rte_vlan_hdr *vlan_hdr = (struct rte_vlan_hdr *)(eth_hdr +1 ); m->l2_len +=sizeof (struct rte_vlan_hdr ); ethertype =rte_be_to_cpu_16 (vlan_hdr->eth_proto); + data_len -=sizeof (struct rte_vlan_hdr ); } l3_hdr = (char *)eth_hdr + m->l2_len;switch (ethertype) {case RTE_ETHER_TYPE_IPV4 : +if (data_len <=sizeof (struct rte_ipv4_hdr )) +return -EINVAL ; ipv4_hdr = l3_hdr; *l4_proto = ipv4_hdr->next_proto_id; m->l3_len =rte_ipv4_hdr_len (ipv4_hdr); +if (data_len <= m->l3_len) { + m->l3_len =0 ; +return -EINVAL ; + } *l4_hdr = (char *)l3_hdr + m->l3_len; m->ol_flags |=PKT_TX_IPV4 ; + data_len -= m->l3_len;break ;case RTE_ETHER_TYPE_IPV6 : +if (data_len <=sizeof (struct rte_ipv6_hdr )) +return -EINVAL ; ipv6_hdr = l3_hdr; *l4_proto = ipv6_hdr->proto; m->l3_len =sizeof (struct rte_ipv6_hdr ); *l4_hdr = (char *)l3_hdr + m->l3_len; m->ol_flags |=PKT_TX_IPV6 ; + data_len -= m->l3_len;break ;default : m->l3_len =0 ;
@@ -1866,6 +1886,9 @@ parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr) *l4_hdr =NULL ;break ; } + + *len = data_len; +return 0 ; }static __rte_always_inline void
@@ -1874,24 +1897,30 @@ vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)uint16_t l4_proto =0 ;void *l4_hdr =NULL ;struct rte_tcp_hdr *tcp_hdr =NULL ; +uint16_t len =0 ;if (hdr->flags ==0 && hdr->gso_type ==VIRTIO_NET_HDR_GSO_NONE )return ; -parse_ethernet (m, &l4_proto, &l4_hdr); +if (parse_ethernet (m, &l4_proto, &l4_hdr, &len) <0 ) +return ; +if (hdr->flags ==VIRTIO_NET_HDR_F_NEEDS_CSUM ) {if (hdr->csum_start == (m->l2_len + m->l3_len)) {switch (hdr->csum_offset) {case (offsetof(struct rte_tcp_hdr , cksum)): -if (l4_proto ==IPPROTO_TCP ) +if (l4_proto ==IPPROTO_TCP && + len >sizeof (struct rte_tcp_hdr )) m->ol_flags |=PKT_TX_TCP_CKSUM ;break ;case (offsetof(struct rte_udp_hdr , dgram_cksum)): -if (l4_proto ==IPPROTO_UDP ) +if (l4_proto ==IPPROTO_UDP && + len >sizeof (struct rte_udp_hdr )) m->ol_flags |=PKT_TX_UDP_CKSUM ;break ;case (offsetof(struct rte_sctp_hdr , cksum)): -if (l4_proto ==IPPROTO_SCTP ) +if (l4_proto ==IPPROTO_SCTP && + len >sizeof (struct rte_sctp_hdr )) m->ol_flags |=PKT_TX_SCTP_CKSUM ;break ;default :
@@ -1904,12 +1933,20 @@ vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN ) {case VIRTIO_NET_HDR_GSO_TCPV4 :case VIRTIO_NET_HDR_GSO_TCPV6 : +if (l4_proto !=IPPROTO_TCP || + len <=sizeof (struct rte_tcp_hdr )) +break ; tcp_hdr = l4_hdr; +if (len <= (tcp_hdr->data_off &0xf0 ) >>2 ) +break ; m->ol_flags |=PKT_TX_TCP_SEG ; m->tso_segsz = hdr->gso_size; m->l4_len = (tcp_hdr->data_off &0xf0 ) >>2 ;break ;case VIRTIO_NET_HDR_GSO_UDP : +if (l4_proto !=IPPROTO_UDP || + len <=sizeof (struct rte_udp_hdr )) +break ; m->ol_flags |=PKT_TX_UDP_SEG ; m->tso_segsz = hdr->gso_size; m->l4_len =sizeof (struct rte_udp_hdr );
Signature · 1 line
2.15.1
Quoted 107 lines
When parsing the virtio net header and packet header for dequeue offload, we need to perform sanity check on the packet header to ensure:
- No out-of-boundary memory access.
- The packet header andvirtio_netheader are valid and aligned.Fixes d0cf91303d73 ("vhost: add Tx offload capabilities")Signed-off-by Xiao Wang <[email protected]>--- lib/librte_vhost/virtio_net.c | 49 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 6 deletions(-)lib/librte_vhost/virtio_net.c +26
-3@@ -1821,44 +1821,64 @@ virtio_net_with_host_offload(struct virtio_net *dev)return false; } -static void -parse_ethernet (struct rte_mbuf *m,uint16_t *l4_proto,void **l4_hdr) +static int +parse_ethernet (struct rte_mbuf *m,uint16_t *l4_proto,void **l4_hdr, +uint16_t *len) {struct rte_ipv4_hdr *ipv4_hdr;struct rte_ipv6_hdr *ipv6_hdr;void *l3_hdr =NULL ;struct rte_ether_hdr *eth_hdr;uint16_t ethertype; +uint16_t data_len = m->data_len; eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *); +if (data_len <=sizeof (struct rte_ether_hdr )) +return -EINVAL ; + m->l2_len =sizeof (struct rte_ether_hdr ); ethertype =rte_be_to_cpu_16 (eth_hdr->ether_type); + data_len -=sizeof (struct rte_ether_hdr );if (ethertype ==RTE_ETHER_TYPE_VLAN ) { +if (data_len <=sizeof (struct rte_vlan_hdr )) +return -EINVAL ; +struct rte_vlan_hdr *vlan_hdr = (struct rte_vlan_hdr *)(eth_hdr +1 ); m->l2_len +=sizeof (struct rte_vlan_hdr ); ethertype =rte_be_to_cpu_16 (vlan_hdr->eth_proto); + data_len -=sizeof (struct rte_vlan_hdr ); } l3_hdr = (char *)eth_hdr + m->l2_len;switch (ethertype) {case RTE_ETHER_TYPE_IPV4 : +if (data_len <=sizeof (struct rte_ipv4_hdr )) +return -EINVAL ; ipv4_hdr = l3_hdr; *l4_proto = ipv4_hdr->next_proto_id; m->l3_len =rte_ipv4_hdr_len (ipv4_hdr); +if (data_len <= m->l3_len) { + m->l3_len =0 ; +return -EINVAL ; + } *l4_hdr = (char *)l3_hdr + m->l3_len; m->ol_flags |=PKT_TX_IPV4 ; + data_len -= m->l3_len;break ;case RTE_ETHER_TYPE_IPV6 : +if (data_len <=sizeof (struct rte_ipv6_hdr )) +return -EINVAL ; ipv6_hdr = l3_hdr; *l4_proto = ipv6_hdr->proto; m->l3_len =sizeof (struct rte_ipv6_hdr ); *l4_hdr = (char *)l3_hdr + m->l3_len; m->ol_flags |=PKT_TX_IPV6 ; + data_len -= m->l3_len;break ;default : m->l3_len =0 ;@@ -1866,6 +1886,9 @@ parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr) *l4_hdr =NULL ;break ; } + + *len = data_len; +return 0 ; }static __rte_always_inline void@@ -1874,24 +1897,30 @@ vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)uint16_t l4_proto =0 ;void *l4_hdr =NULL ;struct rte_tcp_hdr *tcp_hdr =NULL ; +uint16_t len =0 ;if (hdr->flags ==0 && hdr->gso_type ==VIRTIO_NET_HDR_GSO_NONE )return ; -parse_ethernet (m, &l4_proto, &l4_hdr);
lib/librte_vhost/virtio_net.c vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m) · line 1913
Quoted 10 lines
lib/librte_vhost/virtio_net.c +5
-1+if (parse_ethernet (m, &l4_proto, &l4_hdr, &len) <0 ) +return ; +if (hdr->flags ==VIRTIO_NET_HDR_F_NEEDS_CSUM ) {if (hdr->csum_start == (m->l2_len + m->l3_len)) {switch (hdr->csum_offset) {case (offsetof(struct rte_tcp_hdr , cksum)): -if (l4_proto ==IPPROTO_TCP ) +if (l4_proto ==IPPROTO_TCP && + len >sizeof (struct rte_tcp_hdr ))
Shouldn't it be '>=' here?
Quoted 38 lines
lib/librte_vhost/virtio_net.c +12
-3m->ol_flags |=PKT_TX_TCP_CKSUM ;break ;case (offsetof(struct rte_udp_hdr , dgram_cksum)): -if (l4_proto ==IPPROTO_UDP ) +if (l4_proto ==IPPROTO_UDP && + len >sizeof (struct rte_udp_hdr )) m->ol_flags |=PKT_TX_UDP_CKSUM ;break ;case (offsetof(struct rte_sctp_hdr , cksum)): -if (l4_proto ==IPPROTO_SCTP ) +if (l4_proto ==IPPROTO_SCTP && + len >sizeof (struct rte_sctp_hdr )) m->ol_flags |=PKT_TX_SCTP_CKSUM ;break ;default :@@ -1904,12 +1933,20 @@ vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN ) {case VIRTIO_NET_HDR_GSO_TCPV4 :case VIRTIO_NET_HDR_GSO_TCPV6 : +if (l4_proto !=IPPROTO_TCP || + len <=sizeof (struct rte_tcp_hdr )) +break ; tcp_hdr = l4_hdr; +if (len <= (tcp_hdr->data_off &0xf0 ) >>2 ) +break ; m->ol_flags |=PKT_TX_TCP_SEG ; m->tso_segsz = hdr->gso_size; m->l4_len = (tcp_hdr->data_off &0xf0 ) >>2 ;break ;case VIRTIO_NET_HDR_GSO_UDP : +if (l4_proto !=IPPROTO_UDP || + len <=sizeof (struct rte_udp_hdr )) +break ; m->ol_flags |=PKT_TX_UDP_SEG ; m->tso_segsz = hdr->gso_size; m->l4_len =sizeof (struct rte_udp_hdr ); --2.15.1
Hi Konstantin,
Comments inline.
BRs,
Xiao
Quoted 122 lines
-----Original Message-----
From: Ananyev, Konstantin <[email protected]>
Sent: Thursday, March 11, 2021 6:38 PM
To: Wang, Xiao W <[email protected]>; Xia, Chenbo <[email protected]>; [email protected]
Cc: Liu, Yong <[email protected]>; [email protected]; Wang, Xiao W <[email protected]>; [email protected]
Subject: RE: [dpdk-dev] [PATCH] vhost: add header check in dequeue offloadQuoted 2 lines
When parsing the virtio net header and packet header for dequeue
offload,
Quoted 10 lines
we need to perform sanity check on the packet header to ensure:
- No out-of-boundary memory access.
- The packet header andvirtio_netheader are valid and aligned.Fixes d0cf91303d73 ("vhost: add Tx offload capabilities")Signed-off-by Xiao Wang <[email protected]>--- lib/librte_vhost/virtio_net.c | 49+++++++++++++++++++++++++++++++++++++------
lib/librte_vhost/virtio_net.c virtio_net_with_host_offload(struct
Quoted 7 lines
1 file changed, 43 insertions(+), 6 deletions(-)lib/librte_vhost/virtio_net.c +0
-0@@ -1821,44 +1821,64 @@ virtio_net_with_host_offload(struct
virtio_net*dev)lib/librte_vhost/virtio_net.c parse_ethernet(struct rte_mbuf *m, uint16_t
Quoted 67 lines
lib/librte_vhost/virtio_net.c +22
-2return false; } -static void -parse_ethernet (struct rte_mbuf *m,uint16_t *l4_proto,void **l4_hdr) +static int +parse_ethernet (struct rte_mbuf *m,uint16_t *l4_proto,void **l4_hdr, +uint16_t *len) {struct rte_ipv4_hdr *ipv4_hdr;struct rte_ipv6_hdr *ipv6_hdr;void *l3_hdr =NULL ;struct rte_ether_hdr *eth_hdr;uint16_t ethertype; +uint16_t data_len = m->data_len; eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *); +if (data_len <=sizeof (struct rte_ether_hdr )) +return -EINVAL ; + m->l2_len =sizeof (struct rte_ether_hdr ); ethertype =rte_be_to_cpu_16 (eth_hdr->ether_type); + data_len -=sizeof (struct rte_ether_hdr );if (ethertype ==RTE_ETHER_TYPE_VLAN ) { +if (data_len <=sizeof (struct rte_vlan_hdr )) +return -EINVAL ; +struct rte_vlan_hdr *vlan_hdr = (struct rte_vlan_hdr *)(eth_hdr +1 ); m->l2_len +=sizeof (struct rte_vlan_hdr ); ethertype =rte_be_to_cpu_16 (vlan_hdr->eth_proto); + data_len -=sizeof (struct rte_vlan_hdr ); } l3_hdr = (char *)eth_hdr + m->l2_len;switch (ethertype) {case RTE_ETHER_TYPE_IPV4 : +if (data_len <=sizeof (struct rte_ipv4_hdr )) +return -EINVAL ; ipv4_hdr = l3_hdr; *l4_proto = ipv4_hdr->next_proto_id; m->l3_len =rte_ipv4_hdr_len (ipv4_hdr); +if (data_len <= m->l3_len) { + m->l3_len =0 ; +return -EINVAL ; + } *l4_hdr = (char *)l3_hdr + m->l3_len; m->ol_flags |=PKT_TX_IPV4 ; + data_len -= m->l3_len;break ;case RTE_ETHER_TYPE_IPV6 : +if (data_len <=sizeof (struct rte_ipv6_hdr )) +return -EINVAL ; ipv6_hdr = l3_hdr; *l4_proto = ipv6_hdr->proto; m->l3_len =sizeof (struct rte_ipv6_hdr ); *l4_hdr = (char *)l3_hdr + m->l3_len; m->ol_flags |=PKT_TX_IPV6 ; + data_len -= m->l3_len;break ;default : m->l3_len =0 ;@@ -1866,6 +1886,9 @@ parse_ethernet(struct rte_mbuf *m, uint16_t
*l4_proto, void **l4_hdr)lib/librte_vhost/virtio_net.c vhost_dequeue_offload(struct virtio_net_hdr
Quoted 10 lines
lib/librte_vhost/virtio_net.c +3
-0*l4_hdr =NULL ;break ; } + + *len = data_len; +return 0 ; }static __rte_always_inline void@@ -1874,24 +1897,30 @@ vhost_dequeue_offload(struct virtio_net_hdr*hdr, struct
rte_mbuf*m)lib/librte_vhost/virtio_net.c vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
Quoted 6 lines
lib/librte_vhost/virtio_net.c +1
-0uint16_t l4_proto =0 ;void *l4_hdr =NULL ;struct rte_tcp_hdr *tcp_hdr =NULL ; +uint16_t len =0 ; if (hdr->flags == 0 && hdr->gso_type ==
VIRTIO_NET_HDR_GSO_NONE)Quoted 2 lines
lib/librte_vhost/virtio_net.c +0
-0return ;
Quoted 13 lines
lib/librte_vhost/virtio_net.c vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m) · line 1913
Quoted 11 lines
lib/librte_vhost/virtio_net.c +5
-2-parse_ethernet (m, &l4_proto, &l4_hdr); +if (parse_ethernet (m, &l4_proto, &l4_hdr, &len) <0 ) +return ; +if (hdr->flags ==VIRTIO_NET_HDR_F_NEEDS_CSUM ) {if (hdr->csum_start == (m->l2_len + m->l3_len)) {switch (hdr->csum_offset) {case (offsetof(struct rte_tcp_hdr , cksum)): -if (l4_proto ==IPPROTO_TCP ) +if (l4_proto ==IPPROTO_TCP && + len >sizeof (struct rte_tcp_hdr ))Shouldn't it be '>=' here?
You are right, there're packets with no payload. I just did an experiment where I send a tcp packet with only headers from guest using scapy and vhost side get 54 Bytes (14+20+20), there's no padding. I would fix this issue also for udp and sctp.
Thanks for the comment.
-Xiao
PATCH v2 1/1 vhost: add header check in dequeue offload
In reply to Xiao WangWhen parsing the virtio net header and packet header for dequeue offload, we need to perform sanity check on the packet header to ensure:
- No out-of-boundary memory access.
- The packet header and virtio_net header are valid and aligned.
---
v2:
Allow empty L4 payload for cksum offload.
--- lib/librte_vhost/virtio_net.c | 49 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 6 deletions(-)
lib/librte_vhost/virtio_net.c +43 -6
@@ -1821,44 +1821,64 @@ virtio_net_with_host_offload(struct virtio_net *dev)return false; } -static void -parse_ethernet (struct rte_mbuf *m,uint16_t *l4_proto,void **l4_hdr) +static int +parse_ethernet (struct rte_mbuf *m,uint16_t *l4_proto,void **l4_hdr, +uint16_t *len) {struct rte_ipv4_hdr *ipv4_hdr;struct rte_ipv6_hdr *ipv6_hdr;void *l3_hdr =NULL ;struct rte_ether_hdr *eth_hdr;uint16_t ethertype; +uint16_t data_len = m->data_len; eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *); +if (data_len <=sizeof (struct rte_ether_hdr )) +return -EINVAL ; + m->l2_len =sizeof (struct rte_ether_hdr ); ethertype =rte_be_to_cpu_16 (eth_hdr->ether_type); + data_len -=sizeof (struct rte_ether_hdr );if (ethertype ==RTE_ETHER_TYPE_VLAN ) { +if (data_len <=sizeof (struct rte_vlan_hdr )) +return -EINVAL ; +struct rte_vlan_hdr *vlan_hdr = (struct rte_vlan_hdr *)(eth_hdr +1 ); m->l2_len +=sizeof (struct rte_vlan_hdr ); ethertype =rte_be_to_cpu_16 (vlan_hdr->eth_proto); + data_len -=sizeof (struct rte_vlan_hdr ); } l3_hdr = (char *)eth_hdr + m->l2_len;switch (ethertype) {case RTE_ETHER_TYPE_IPV4 : +if (data_len <=sizeof (struct rte_ipv4_hdr )) +return -EINVAL ; ipv4_hdr = l3_hdr; *l4_proto = ipv4_hdr->next_proto_id; m->l3_len =rte_ipv4_hdr_len (ipv4_hdr); +if (data_len <= m->l3_len) { + m->l3_len =0 ; +return -EINVAL ; + } *l4_hdr = (char *)l3_hdr + m->l3_len; m->ol_flags |=PKT_TX_IPV4 ; + data_len -= m->l3_len;break ;case RTE_ETHER_TYPE_IPV6 : +if (data_len <=sizeof (struct rte_ipv6_hdr )) +return -EINVAL ; ipv6_hdr = l3_hdr; *l4_proto = ipv6_hdr->proto; m->l3_len =sizeof (struct rte_ipv6_hdr ); *l4_hdr = (char *)l3_hdr + m->l3_len; m->ol_flags |=PKT_TX_IPV6 ; + data_len -= m->l3_len;break ;default : m->l3_len =0 ;
@@ -1866,6 +1886,9 @@ parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr) *l4_hdr =NULL ;break ; } + + *len = data_len; +return 0 ; }static __rte_always_inline void
@@ -1874,24 +1897,30 @@ vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)uint16_t l4_proto =0 ;void *l4_hdr =NULL ;struct rte_tcp_hdr *tcp_hdr =NULL ; +uint16_t len =0 ;if (hdr->flags ==0 && hdr->gso_type ==VIRTIO_NET_HDR_GSO_NONE )return ; -parse_ethernet (m, &l4_proto, &l4_hdr); +if (parse_ethernet (m, &l4_proto, &l4_hdr, &len) <0 ) +return ; +if (hdr->flags ==VIRTIO_NET_HDR_F_NEEDS_CSUM ) {if (hdr->csum_start == (m->l2_len + m->l3_len)) {switch (hdr->csum_offset) {case (offsetof(struct rte_tcp_hdr , cksum)): -if (l4_proto ==IPPROTO_TCP ) +if (l4_proto ==IPPROTO_TCP && + len >=sizeof (struct rte_tcp_hdr )) m->ol_flags |=PKT_TX_TCP_CKSUM ;break ;case (offsetof(struct rte_udp_hdr , dgram_cksum)): -if (l4_proto ==IPPROTO_UDP ) +if (l4_proto ==IPPROTO_UDP && + len >=sizeof (struct rte_udp_hdr )) m->ol_flags |=PKT_TX_UDP_CKSUM ;break ;case (offsetof(struct rte_sctp_hdr , cksum)): -if (l4_proto ==IPPROTO_SCTP ) +if (l4_proto ==IPPROTO_SCTP && + len >=sizeof (struct rte_sctp_hdr )) m->ol_flags |=PKT_TX_SCTP_CKSUM ;break ;default :
@@ -1904,12 +1933,20 @@ vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN ) {case VIRTIO_NET_HDR_GSO_TCPV4 :case VIRTIO_NET_HDR_GSO_TCPV6 : +if (l4_proto !=IPPROTO_TCP || + len <=sizeof (struct rte_tcp_hdr )) +break ; tcp_hdr = l4_hdr; +if (len <= (tcp_hdr->data_off &0xf0 ) >>2 ) +break ; m->ol_flags |=PKT_TX_TCP_SEG ; m->tso_segsz = hdr->gso_size; m->l4_len = (tcp_hdr->data_off &0xf0 ) >>2 ;break ;case VIRTIO_NET_HDR_GSO_UDP : +if (l4_proto !=IPPROTO_UDP || + len <=sizeof (struct rte_udp_hdr )) +break ; m->ol_flags |=PKT_TX_UDP_SEG ; m->tso_segsz = hdr->gso_size; m->l4_len =sizeof (struct rte_udp_hdr );
Signature · 1 line
2.15.1
Quoted 33 lines · Xiao Wang
When parsing the virtio net header and packet header for dequeue offload, we need to perform sanity check on the packet header to ensure:
- No out-of-boundary memory access.
- The packet header andvirtio_netheader are valid and aligned.Fixes d0cf91303d73 ("vhost: add Tx offload capabilities")Signed-off-by Xiao Wang <[email protected]>---
v2:
Allow empty L4 payload for cksum offload.--- lib/librte_vhost/virtio_net.c | 49 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 6 deletions(-)lib/librte_vhost/virtio_net.c +3
-2@@ -1821,44 +1821,64 @@ virtio_net_with_host_offload(struct virtio_net *dev)return false; } -static void -parse_ethernet (struct rte_mbuf *m,uint16_t *l4_proto,void **l4_hdr) +static int +parse_ethernet (struct rte_mbuf *m,uint16_t *l4_proto,void **l4_hdr, +uint16_t *len) {struct rte_ipv4_hdr *ipv4_hdr;struct rte_ipv6_hdr *ipv6_hdr;
lib/librte_vhost/virtio_net.c virtio_net_with_host_offload(struct virtio_net *dev) · line 1839
Quoted 10 lines
lib/librte_vhost/virtio_net.c +3
-0void *l3_hdr =NULL ;struct rte_ether_hdr *eth_hdr;uint16_t ethertype; +uint16_t data_len = m->data_len; eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *); +if (data_len <=sizeof (struct rte_ether_hdr )) +return -EINVAL ;
On principle, the check should happen before calling rte_pktmbuf_mtod, like what rte_pktmbuf_read does.
Looking at the rest of the patch, does this helper function only handle mono segment mbufs?
My reading of copy_desc_to_mbuf() was that it could generate multi segments mbufs...
[snip]
lib/librte_vhost/virtio_net.c virtio_net_with_host_offload(struct virtio_net *dev) · line 1868
Quoted 10 lines
lib/librte_vhost/virtio_net.c +6
-0case RTE_ETHER_TYPE_IPV4 : +if (data_len <=sizeof (struct rte_ipv4_hdr )) +return -EINVAL ; ipv4_hdr = l3_hdr; *l4_proto = ipv4_hdr->next_proto_id; m->l3_len =rte_ipv4_hdr_len (ipv4_hdr); +if (data_len <= m->l3_len) { + m->l3_len =0 ; +return -EINVAL ; + }
... so here, comparing l3 length to only the first segment length (data_len) would be invalid.
If this helper must deal with multi segments, why not use rte_pktmbuf_read? This function returns access to mbuf data after checking offset and length are contiguous, else copy the needed data in a passed buffer.
Quoted 4 lines
lib/librte_vhost/virtio_net.c +1
-0*l4_hdr = (char *)l3_hdr + m->l3_len; m->ol_flags |=PKT_TX_IPV4 ; + data_len -= m->l3_len;break ;
Signature · 1 line
David Marchand
Quoted 77 lines
-----Original Message-----
From: David Marchand <[email protected]>
Sent: Monday, March 15, 2021 4:17 PM
To: Wang, Xiao W <[email protected]>
Cc: Xia, Chenbo <[email protected]>; Maxime Coquelin <[email protected]>; Liu, Yong <[email protected]>; dev <[email protected]>; Ananyev, Konstantin <[email protected]>; dpdk stable <[email protected]>
Subject: Re: [dpdk-stable] [PATCH v2] vhost: add header check in dequeue offloadQuoted 33 lines · Xiao Wang
When parsing the virtio net header and packet header for dequeue offload, we need to perform sanity check on the packet header to ensure:
- No out-of-boundary memory access.
- The packet header andvirtio_netheader are valid and aligned.Fixes d0cf91303d73 ("vhost: add Tx offload capabilities")Signed-off-by Xiao Wang <[email protected]>---
v2:
Allow empty L4 payload for cksum offload.--- lib/librte_vhost/virtio_net.c | 49 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 6 deletions(-)lib/librte_vhost/virtio_net.c +3
-2@@ -1821,44 +1821,64 @@ virtio_net_with_host_offload(struct virtio_net *dev)return false; } -static void -parse_ethernet (struct rte_mbuf *m,uint16_t *l4_proto,void **l4_hdr) +static int +parse_ethernet (struct rte_mbuf *m,uint16_t *l4_proto,void **l4_hdr, +uint16_t *len) {struct rte_ipv4_hdr *ipv4_hdr;struct rte_ipv6_hdr *ipv6_hdr;lib/librte_vhost/virtio_net.c virtio_net_with_host_offload(struct virtio_net *dev) · line 1839
Quoted 10 lines
lib/librte_vhost/virtio_net.c +3
-0void *l3_hdr =NULL ;struct rte_ether_hdr *eth_hdr;uint16_t ethertype; +uint16_t data_len = m->data_len; eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *); +if (data_len <=sizeof (struct rte_ether_hdr )) +return -EINVAL ;On principle, the check should happen before calling
rte_pktmbuf_mtod, like whatrte_pktmbuf_readdoes.Looking at the rest of the patch, does this helper function only handle mono segment mbufs?
My reading ofcopy_desc_to_mbuf()was that it could generate multi segments mbufs...[snip]
lib/librte_vhost/virtio_net.c virtio_net_with_host_offload(struct virtio_net *dev) · line 1868
Quoted 10 lines
lib/librte_vhost/virtio_net.c +6
-0case RTE_ETHER_TYPE_IPV4 : +if (data_len <=sizeof (struct rte_ipv4_hdr )) +return -EINVAL ; ipv4_hdr = l3_hdr; *l4_proto = ipv4_hdr->next_proto_id; m->l3_len =rte_ipv4_hdr_len (ipv4_hdr); +if (data_len <= m->l3_len) { + m->l3_len =0 ; +return -EINVAL ; + }
Quoted 6 lines
... so here, comparing l3 length to only the first segment length (
data_len) would be invalid.If this helper must deal with multi segments, why not use
rte_pktmbuf_read? This function returns access to mbuf data after checking offset and length are contiguous, else copy the needed data in a passed buffer.
From my understanding, yes multi-seg is allowed, but an expectation Is that at least packet header (l2/l3/l4?) will always reside in first segment.
Quoted 10 lines
lib/librte_vhost/virtio_net.c virtio_net_with_host_offload(struct virtio_net *dev) · line 1872
Quoted 6 lines
lib/librte_vhost/virtio_net.c +1
-0*l4_hdr = (char *)l3_hdr + m->l3_len; m->ol_flags |=PKT_TX_IPV4 ; + data_len -= m->l3_len;break ;--
David Marchand
Hi,
Comments inline.
BRs,
Xiao
Quoted 54 lines
-----Original Message-----
From: Ananyev, Konstantin <[email protected]>
Sent: Tuesday, March 16, 2021 2:53 AM
To: David Marchand <[email protected]>; Wang, Xiao W <[email protected]>
Cc: Xia, Chenbo <[email protected]>; Maxime Coquelin <[email protected]>; Liu, Yong <[email protected]>; dev <[email protected]>; dpdk stable <[email protected]>
Subject: RE: [dpdk-stable] [PATCH v2] vhost: add header check in dequeue offloadQuoted 5 lines
-----Original Message-----
From: David Marchand <[email protected]>
Sent: Monday, March 15, 2021 4:17 PM
To: Wang, Xiao W <[email protected]>
Cc: Xia, Chenbo <[email protected]>; Maxime Coquelin<[email protected]>; Liu, Yong <[email protected]>; dev
Quoted 1 line
<[email protected]>; Ananyev, Konstantin <[email protected]>;
dpdk stable <[email protected]>
Quoted 1 line
Subject: Re: [dpdk-stable] [PATCH v2] vhost: add header check in dequeue
offload
Quoted 2 lines
On Mon, Mar 15, 2021 at 4:52 PM Xiao Wang <[email protected]>
Quoted 2 lines · wrote:
Quoted 2 lines
When parsing the virtio net header and packet header for dequeue
offload,
Quoted 7 lines
Quoted 7 lines
we need to perform sanity check on the packet header to ensure:
- No out-of-boundary memory access.
- The packet header andvirtio_netheader are valid and aligned.Fixes d0cf91303d73 ("vhost: add Tx offload capabilities")Quoted 6 lines
Quoted 6 lines
Signed-off-by Xiao Wang <[email protected]>---
v2:
Allow empty L4 payload for cksum offload.--- lib/librte_vhost/virtio_net.c | 49+++++++++++++++++++++++++++++++++++++------
Quoted 7 lines
lib/librte_vhost/virtio_net.c virtio_net_with_host_offload(struct
Quoted 7 lines
1 file changed, 43 insertions(+), 6 deletions(-)lib/librte_vhost/virtio_net.c +0
-0@@ -1821,44 +1821,64 @@ virtio_net_with_host_offload(struct
virtio_net*dev)Quoted 3 lines
Quoted 3 lines
lib/librte_vhost/virtio_net.c +0
-0return false; }
Quoted 23 lines
Quoted 2 lines
lib/librte_vhost/virtio_net.c virtio_net_with_host_offload(struct · line 1825
Quoted 2 lines
lib/librte_vhost/virtio_net.c +0
-2-static void -parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void**l4_hdr)
Quoted 2 lines
lib/librte_vhost/virtio_net.c virtio_net_with_host_offload(struct · line 1825
Quoted 2 lines
lib/librte_vhost/virtio_net.c +2
-0+static int +parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void**l4_hdr,
Quoted 17 lines
Quoted 4 lines
lib/librte_vhost/virtio_net.c +1
-0+ uint16_t *len) {struct rte_ipv4_hdr *ipv4_hdr;struct rte_ipv6_hdr *ipv6_hdr;lib/librte_vhost/virtio_net.c virtio_net_with_host_offload(struct virtio_net *dev) · line 1846
Quoted 10 lines
lib/librte_vhost/virtio_net.c +3
-0void *l3_hdr =NULL ;struct rte_ether_hdr *eth_hdr;uint16_t ethertype; +uint16_t data_len = m->data_len; eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *); +if (data_len <=sizeof (struct rte_ether_hdr )) +return -EINVAL ;On principle, the check should happen before calling
rte_pktmbuf_mtod, like whatrte_pktmbuf_readdoes.
Yes, I agree. Will fix it in v3.
Quoted 5 lines
Quoted 5 lines
Looking at the rest of the patch, does this helper function only handle mono segment mbufs?
My reading ofcopy_desc_to_mbuf()was that it could generate multi segments mbufs...
copy_desc_to_mbuf() could generate multi seg mbufs, and the whole packet would be copied into these multi-mbufs when packet size is larger than mbuf's capacity. Anyway, one mbuf's capacity is big enough for holding the L2/L3/L4 header.
Quoted 18 lines
Quoted 18 lines
[snip]
lib/librte_vhost/virtio_net.c virtio_net_with_host_offload(struct virtio_net *dev) · line 1868
Quoted 10 lines
case
RTE_ETHER_TYPE_IPV4:lib/librte_vhost/virtio_net.c +6
-0+if (data_len <=sizeof (struct rte_ipv4_hdr )) +return -EINVAL ; ipv4_hdr = l3_hdr; *l4_proto = ipv4_hdr->next_proto_id; m->l3_len =rte_ipv4_hdr_len (ipv4_hdr); +if (data_len <= m->l3_len) { + m->l3_len =0 ; +return -EINVAL ; + }... so here, comparing l3 length to only the first segment length (
data_len) would be invalid.
Quoted 7 lines
Quoted 1 line
If this helper must deal with multi segments, why not use
rte_pktmbuf_read?Quoted 2 lines
This function returns access to mbuf data after checking offset and length are contiguous, else copy the needed data in a passed buffer.
From my understanding, yes multi-seg is allowed, but an expectation Is that at least packet header (l2/l3/l4?) will always reside in first segment.
Yeah, I think so.
Thanks for all the comments,
-Xiao
Quoted 11 lines
Quoted 11 lines
Quoted 7 lines
*l4_hdr = (char *)l3_hdr + m->l3_len; m->ol_flags |= PKT_TX_IPV4; + data_len -= m->l3_len; break;--
David Marchand
PATCH v3 1/1 vhost: add header check in dequeue offload
In reply to Xiao WangWhen parsing the virtio net header and packet header for dequeue offload, we need to perform sanity check on the packet header to ensure:
- No out-of-boundary memory access.
- The packet header and virtio_net header are valid and aligned.
---
v3:
- Check data_len before calling rte_pktmbuf_mtod. (David)
v2:
- Allow empty L4 payload for cksum offload. (Konstantin)
--- lib/librte_vhost/virtio_net.c | 49 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 6 deletions(-)
lib/librte_vhost/virtio_net.c +43 -6
@@ -1821,44 +1821,64 @@ virtio_net_with_host_offload(struct virtio_net *dev)return false; } -static void -parse_ethernet (struct rte_mbuf *m,uint16_t *l4_proto,void **l4_hdr) +static int +parse_ethernet (struct rte_mbuf *m,uint16_t *l4_proto,void **l4_hdr, +uint16_t *len) {struct rte_ipv4_hdr *ipv4_hdr;struct rte_ipv6_hdr *ipv6_hdr;void *l3_hdr =NULL ;struct rte_ether_hdr *eth_hdr;uint16_t ethertype; +uint16_t data_len = m->data_len; + +if (data_len <=sizeof (struct rte_ether_hdr )) +return -EINVAL ; eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *); m->l2_len =sizeof (struct rte_ether_hdr ); ethertype =rte_be_to_cpu_16 (eth_hdr->ether_type); + data_len -=sizeof (struct rte_ether_hdr );if (ethertype ==RTE_ETHER_TYPE_VLAN ) { +if (data_len <=sizeof (struct rte_vlan_hdr )) +return -EINVAL ; +struct rte_vlan_hdr *vlan_hdr = (struct rte_vlan_hdr *)(eth_hdr +1 ); m->l2_len +=sizeof (struct rte_vlan_hdr ); ethertype =rte_be_to_cpu_16 (vlan_hdr->eth_proto); + data_len -=sizeof (struct rte_vlan_hdr ); } l3_hdr = (char *)eth_hdr + m->l2_len;switch (ethertype) {case RTE_ETHER_TYPE_IPV4 : +if (data_len <=sizeof (struct rte_ipv4_hdr )) +return -EINVAL ; ipv4_hdr = l3_hdr; *l4_proto = ipv4_hdr->next_proto_id; m->l3_len =rte_ipv4_hdr_len (ipv4_hdr); +if (data_len <= m->l3_len) { + m->l3_len =0 ; +return -EINVAL ; + } *l4_hdr = (char *)l3_hdr + m->l3_len; m->ol_flags |=PKT_TX_IPV4 ; + data_len -= m->l3_len;break ;case RTE_ETHER_TYPE_IPV6 : +if (data_len <=sizeof (struct rte_ipv6_hdr )) +return -EINVAL ; ipv6_hdr = l3_hdr; *l4_proto = ipv6_hdr->proto; m->l3_len =sizeof (struct rte_ipv6_hdr ); *l4_hdr = (char *)l3_hdr + m->l3_len; m->ol_flags |=PKT_TX_IPV6 ; + data_len -= m->l3_len;break ;default : m->l3_len =0 ;
@@ -1866,6 +1886,9 @@ parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr) *l4_hdr =NULL ;break ; } + + *len = data_len; +return 0 ; }static __rte_always_inline void
@@ -1874,24 +1897,30 @@ vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)uint16_t l4_proto =0 ;void *l4_hdr =NULL ;struct rte_tcp_hdr *tcp_hdr =NULL ; +uint16_t len =0 ;if (hdr->flags ==0 && hdr->gso_type ==VIRTIO_NET_HDR_GSO_NONE )return ; -parse_ethernet (m, &l4_proto, &l4_hdr); +if (parse_ethernet (m, &l4_proto, &l4_hdr, &len) <0 ) +return ; +if (hdr->flags ==VIRTIO_NET_HDR_F_NEEDS_CSUM ) {if (hdr->csum_start == (m->l2_len + m->l3_len)) {switch (hdr->csum_offset) {case (offsetof(struct rte_tcp_hdr , cksum)): -if (l4_proto ==IPPROTO_TCP ) +if (l4_proto ==IPPROTO_TCP && + len >=sizeof (struct rte_tcp_hdr )) m->ol_flags |=PKT_TX_TCP_CKSUM ;break ;case (offsetof(struct rte_udp_hdr , dgram_cksum)): -if (l4_proto ==IPPROTO_UDP ) +if (l4_proto ==IPPROTO_UDP && + len >=sizeof (struct rte_udp_hdr )) m->ol_flags |=PKT_TX_UDP_CKSUM ;break ;case (offsetof(struct rte_sctp_hdr , cksum)): -if (l4_proto ==IPPROTO_SCTP ) +if (l4_proto ==IPPROTO_SCTP && + len >=sizeof (struct rte_sctp_hdr )) m->ol_flags |=PKT_TX_SCTP_CKSUM ;break ;default :
@@ -1904,12 +1933,20 @@ vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN ) {case VIRTIO_NET_HDR_GSO_TCPV4 :case VIRTIO_NET_HDR_GSO_TCPV6 : +if (l4_proto !=IPPROTO_TCP || + len <=sizeof (struct rte_tcp_hdr )) +break ; tcp_hdr = l4_hdr; +if (len <= (tcp_hdr->data_off &0xf0 ) >>2 ) +break ; m->ol_flags |=PKT_TX_TCP_SEG ; m->tso_segsz = hdr->gso_size; m->l4_len = (tcp_hdr->data_off &0xf0 ) >>2 ;break ;case VIRTIO_NET_HDR_GSO_UDP : +if (l4_proto !=IPPROTO_UDP || + len <=sizeof (struct rte_udp_hdr )) +break ; m->ol_flags |=PKT_TX_UDP_SEG ; m->tso_segsz = hdr->gso_size; m->l4_len =sizeof (struct rte_udp_hdr );
Signature · 1 line
2.15.1
Quoted 10 lines · Xiao Wang
When parsing the virtio net header and packet header for dequeue offload, we need to perform sanity check on the packet header to ensure:
- No out-of-boundary memory access.
- The packet header andvirtio_netheader are valid and aligned.Fixes d0cf91303d73 ("vhost: add Tx offload capabilities")Signed-off-by Xiao Wang <[email protected]>
I spent some time digging on this topic.
Afaiu the offload API, vhost is not supposed to populate tx offloads. I would drop this whole parse_ethernet function and replace vhost_dequeue_offload with what virtio does on the rx side.
Please have a look at this series (especially the last patch):
http://patchwork.dpdk.org/project/dpdk/list/?series=16052
Thanks.
Signature · 1 line
David Marchand
Quoted 30 lines
-----Original Message-----
From: David Marchand <[email protected]>
Sent: Thursday, April 1, 2021 8:04 PM
To: Wang, Xiao W <[email protected]>
Cc: Xia, Chenbo <[email protected]>; Maxime Coquelin <[email protected]>; Liu, Yong <[email protected]>; dev <[email protected]>; Ananyev, Konstantin <[email protected]>; dpdk stable <[email protected]>
Subject: Re: [PATCH v3] vhost: add header check in dequeue offloadQuoted 10 lines · Xiao Wang
When parsing the virtio net header and packet header for dequeue offload, we need to perform sanity check on the packet header to ensure:
- No out-of-boundary memory access.
- The packet header andvirtio_netheader are valid and aligned.Fixes d0cf91303d73 ("vhost: add Tx offload capabilities")Signed-off-by Xiao Wang <[email protected]>I spent some time digging on this topic.
Afaiu the offload API, vhost is not supposed to populate tx offloads. I would drop this whole
parse_ethernetfunction and replacevhost_dequeue_offloadwith what virtio does on the rx side.
Quoted 8 lines
Please have a look at this series (especially the last patch):
http://patchwork.dpdk.org/project/dpdk/list/?series=16052Thanks.
--
David Marchand
+Yang ,Yi into this loop who may have comments especially from OVS perspective on CKSUM/TSO/TSO in tunnel/etc..
I think the original vhost implementation here is to help pass virtio's offload request onto the next output port, either physical device or a virtio device.
If we go with series http://patchwork.dpdk.org/project/dpdk/list/?series=16052, then virtual switch need to do an extra translation on the flags:
e.g. PKT_RX_LRO --> PKT_TX_TCP_SEG. The question is that a packet marked with PKT_RX_LRO may come from different types of ports (non-vhost), how vSwitch can tell if TSO request should be set for this packet at transmission?
If I think from an endpoint app's perspective, I'm inclined to agree with your series. If I think from a switch/router's perspective, I'm inclined to keep the current implementation. Maybe we can add PKT_RX_L4_CKSUM_NONE/PKT_RX_LRO flags into the current implementation, seems this method can cover both scenarios.
BRs,
Xiao
Hi,
Quoted 63 lines
-----Original Message-----
From: Wang, Xiao W
Sent: Friday, April 2, 2021 4:39 PM
To: David Marchand <[email protected]>
Cc: Xia, Chenbo <[email protected]>; Maxime Coquelin <[email protected]>; Liu, Yong <[email protected]>; dev <[email protected]>; Ananyev, Konstantin <[email protected]>; dpdk stable <[email protected]>; [email protected]
Subject: RE: [PATCH v3] vhost: add header check in dequeue offloadQuoted 10 lines
-----Original Message-----
From: David Marchand <[email protected]>
Sent: Thursday, April 1, 2021 8:04 PM
To: Wang, Xiao W <[email protected]>
Cc: Xia, Chenbo <[email protected]>; Maxime Coquelin <[email protected]>; Liu, Yong <[email protected]>; dev <[email protected]>; Ananyev, Konstantin <[email protected]>; dpdk stable <[email protected]>
Subject: Re: [PATCH v3] vhost: add header check in dequeue offloadQuoted 4 lines
Quoted 2 lines · Xiao Wang
When parsing the virtio net header and packet header for dequeue
offload,
Quoted 15 lines
Quoted 8 lines
we need to perform sanity check on the packet header to ensure:
- No out-of-boundary memory access.
- The packet header andvirtio_netheader are valid and aligned.Fixes d0cf91303d73 ("vhost: add Tx offload capabilities")Signed-off-by Xiao Wang <[email protected]>I spent some time digging on this topic.
Afaiu the offload API, vhost is not supposed to populate tx offloads. I would drop this whole
parse_ethernetfunction and replacevhost_dequeue_offloadwith what virtio does on the rx side.Quoted 8 lines
Please have a look at this series (especially the last patch):
http://patchwork.dpdk.org/project/dpdk/list/?series=16052Thanks.
--
David Marchand+Yang ,Yi into this loop who may have comments especially from OVS perspective on CKSUM/TSO/TSO in tunnel/etc..
I think the original vhost implementation here is to help pass virtio's offload request onto the next output port, either physical device or a virtio device. If we go with series
http://patchwork.dpdk.org/project/dpdk/list/?series=16052, then virtual switch need to do an extra translation on the flags:
e.g.PKT_RX_LRO-->PKT_TX_TCP_SEG. The question is that a packet marked withPKT_RX_LROmay come from different types of ports (non- vhost), how vSwitch can tell if TSO request should be set for this packet at transmission?
Quoted 10 lines
If I think from an endpoint app's perspective, I'm inclined to agree with your series. If I think from a switch/router's perspective, I'm inclined to keep the current implementation. Maybe we can add PKT_RX_L4_CKSUM_NONE/PKT_RX_LRO flags into the current implementation, seems this method can cover both scenarios.
BRs,
Xiao
Considering the major consumer of vhost API is virtual switch/router, I tend to keep the current implementation and apply this fix patch. Any comments?
BRs,
Xiao
Quoted 2 lines · Xiao W
Considering the major consumer of vhost API is virtual switch/router, I tend to keep the current implementation and apply this fix patch. Any comments?
This is just a hack that bypasses the vswitch control.
It happens to work when the vswitch does nothing.
If anything is done, like popping a vlan header, the vswitch needs to update l3 offset.
Signature · 1 line
David Marchand
Quoted 11 lines · David Marchand
Quoted 2 lines · Xiao W
Considering the major consumer of vhost API is virtual switch/router, I tend to keep the current implementation and apply this fix patch. Any comments?
This is just a hack that bypasses the vswitch control.
It happens to work when the vswitch does nothing.
If anything is done, like popping a vlan header, the vswitch needs to update l3 offset.
I agree with David, current behavior is wrong.
Furthermore, when the lib is used via the Vhost PMD, the application should not have to handle it differently on whether it is Vhost PMD or any physical NIC PMD.
Hi Maxime and David,
I see patch " vhost: fix offload flags in Rx path " http://patches.dpdk.org/project/dpdk/patch/[email protected]/ has been merged, and the legacy implementation is kept. Do you think we still need to fix the header check for the legacy implementation?
BRs,
Xiao
Quoted 33 lines
-----Original Message-----
From: Maxime Coquelin <[email protected]>
Sent: Tuesday, April 13, 2021 10:31 PM
To: David Marchand <[email protected]>; Wang, Xiao W <[email protected]>
Cc: Xia, Chenbo <[email protected]>; Liu, Yong <[email protected]>; dev <[email protected]>; Ananyev, Konstantin <[email protected]>; dpdk stable <[email protected]>; [email protected]
Subject: Re: [PATCH v3] vhost: add header check in dequeue offloadQuoted 1 line · David Marchand
On Mon, Apr 12, 2021 at 11:09 AM Wang, Xiao W
Quoted 1 line · <[email protected]> wrote:
Quoted 1 line
Considering the major consumer of vhost API is virtual switch/router, I
tend to keep the current implementation and apply this fix patch.
Quoted 9 lines
Quoted 1 line
Any comments?
This is just a hack that bypasses the vswitch control.
It happens to work when the vswitch does nothing.
If anything is done, like popping a vlan header, the vswitch needs to update l3 offset.I agree with David, current behavior is wrong.
Furthermore, when the lib is used via the Vhost PMD, the application should not have to handle it differently on whether it is Vhost PMD or any physical NIC PMD.
PATCH v4 1/1 vhost: check header for legacy dequeue offload
In reply to Xiao WangWhen parsing the virtio net header and packet header for dequeue offload, we need to perform sanity check on the packet header to ensure:
- No out-of-boundary memory access.
- The packet header and virtio_net header are valid and aligned.
---
v4:
- Rebase on head of main branch.
- Allow empty L4 payload in GSO.
v3:
- Check data_len before calling rte_pktmbuf_mtod. (David)
v2:
- Allow empty L4 payload for cksum offload. (Konstantin)
--- lib/vhost/virtio_net.c | 52 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 7 deletions(-)
lib/vhost/virtio_net.c +45 -7
@@ -2259,44 +2259,64 @@ virtio_net_with_host_offload(struct virtio_net *dev)return false; } -static void -parse_ethernet (struct rte_mbuf *m,uint16_t *l4_proto,void **l4_hdr) +static int +parse_ethernet (struct rte_mbuf *m,uint16_t *l4_proto,void **l4_hdr, +uint16_t *len) {struct rte_ipv4_hdr *ipv4_hdr;struct rte_ipv6_hdr *ipv6_hdr;void *l3_hdr =NULL ;struct rte_ether_hdr *eth_hdr;uint16_t ethertype; +uint16_t data_len = m->data_len; + +if (data_len <=sizeof (struct rte_ether_hdr )) +return -EINVAL ; eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *); m->l2_len =sizeof (struct rte_ether_hdr ); ethertype =rte_be_to_cpu_16 (eth_hdr->ether_type); + data_len -=sizeof (struct rte_ether_hdr );if (ethertype ==RTE_ETHER_TYPE_VLAN ) { +if (data_len <=sizeof (struct rte_vlan_hdr )) +return -EINVAL ; +struct rte_vlan_hdr *vlan_hdr = (struct rte_vlan_hdr *)(eth_hdr +1 ); m->l2_len +=sizeof (struct rte_vlan_hdr ); ethertype =rte_be_to_cpu_16 (vlan_hdr->eth_proto); + data_len -=sizeof (struct rte_vlan_hdr ); } l3_hdr = (char *)eth_hdr + m->l2_len;switch (ethertype) {case RTE_ETHER_TYPE_IPV4 : +if (data_len <=sizeof (struct rte_ipv4_hdr )) +return -EINVAL ; ipv4_hdr = l3_hdr; *l4_proto = ipv4_hdr->next_proto_id; m->l3_len =rte_ipv4_hdr_len (ipv4_hdr); +if (data_len <= m->l3_len) { + m->l3_len =0 ; +return -EINVAL ; + } *l4_hdr = (char *)l3_hdr + m->l3_len; m->ol_flags |=PKT_TX_IPV4 ; + data_len -= m->l3_len;break ;case RTE_ETHER_TYPE_IPV6 : +if (data_len <=sizeof (struct rte_ipv6_hdr )) +return -EINVAL ; ipv6_hdr = l3_hdr; *l4_proto = ipv6_hdr->proto; m->l3_len =sizeof (struct rte_ipv6_hdr ); *l4_hdr = (char *)l3_hdr + m->l3_len; m->ol_flags |=PKT_TX_IPV6 ; + data_len -= m->l3_len;break ;default : m->l3_len =0 ;
@@ -2304,6 +2324,9 @@ parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr) *l4_hdr =NULL ;break ; } + + *len = data_len; +return 0 ; }static __rte_always_inline void
@@ -2312,21 +2335,27 @@ vhost_dequeue_offload_legacy(struct virtio_net_hdr *hdr, struct rte_mbuf *m)uint16_t l4_proto =0 ;void *l4_hdr =NULL ;struct rte_tcp_hdr *tcp_hdr =NULL ; +uint16_t len =0 , tcp_len; + +if (parse_ethernet (m, &l4_proto, &l4_hdr, &len) <0 ) +return ; -parse_ethernet (m, &l4_proto, &l4_hdr);if (hdr->flags ==VIRTIO_NET_HDR_F_NEEDS_CSUM ) {if (hdr->csum_start == (m->l2_len + m->l3_len)) {switch (hdr->csum_offset) {case (offsetof(struct rte_tcp_hdr , cksum)): -if (l4_proto ==IPPROTO_TCP ) +if (l4_proto ==IPPROTO_TCP && + len >=sizeof (struct rte_tcp_hdr )) m->ol_flags |=PKT_TX_TCP_CKSUM ;break ;case (offsetof(struct rte_udp_hdr , dgram_cksum)): -if (l4_proto ==IPPROTO_UDP ) +if (l4_proto ==IPPROTO_UDP && + len >=sizeof (struct rte_udp_hdr )) m->ol_flags |=PKT_TX_UDP_CKSUM ;break ;case (offsetof(struct rte_sctp_hdr , cksum)): -if (l4_proto ==IPPROTO_SCTP ) +if (l4_proto ==IPPROTO_SCTP && + len >=sizeof (struct rte_sctp_hdr )) m->ol_flags |=PKT_TX_SCTP_CKSUM ;break ;default :
@@ -2339,12 +2368,21 @@ vhost_dequeue_offload_legacy(struct virtio_net_hdr *hdr, struct rte_mbuf *m)switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN ) {case VIRTIO_NET_HDR_GSO_TCPV4 :case VIRTIO_NET_HDR_GSO_TCPV6 : +if (l4_proto !=IPPROTO_TCP || + len <sizeof (struct rte_tcp_hdr )) +break ; tcp_hdr = l4_hdr; + tcp_len = (tcp_hdr->data_off &0xf0 ) >>2 ; +if (len < tcp_len) +break ; m->ol_flags |=PKT_TX_TCP_SEG ; m->tso_segsz = hdr->gso_size; - m->l4_len = (tcp_hdr->data_off &0xf0 ) >>2 ; + m->l4_len = tcp_len;break ;case VIRTIO_NET_HDR_GSO_UDP : +if (l4_proto !=IPPROTO_UDP || + len <sizeof (struct rte_udp_hdr )) +break ; m->ol_flags |=PKT_TX_UDP_SEG ; m->tso_segsz = hdr->gso_size; m->l4_len =sizeof (struct rte_udp_hdr );
Signature · 1 line
2.15.1
Re: [dpdk-dev] [PATCH v4] vhost: check header for legacy dequeue offload
Quoted 5 lines · Xiao Wang
lib/vhost/virtio_net.c +0
-0
lib/vhost/virtio_net.c virtio_net_with_host_offload(struct virtio_net *dev) · line 2265
Quoted 9 lines
lib/vhost/virtio_net.c +3
-2@@ -2259,44 +2259,64 @@ virtio_net_with_host_offload(struct virtio_net *dev)return false; } -static void -parse_ethernet (struct rte_mbuf *m,uint16_t *l4_proto,void **l4_hdr) +static int +parse_ethernet (struct rte_mbuf *m,uint16_t *l4_proto,void **l4_hdr, +uint16_t *len) {
This function name is misleading, name could be parse_headers(). Its semantic gets more and more confusing with those l4_hdr and len pointers.
This function fills ->lX_len in the mbuf, everything is available for caller.
Caller can check that rte_pktmbuf_data_len() is >= m->l2_len + m->l3_len + somesize.
=> no need for len.
l4_hdr can simply be deduced with rte_pktmbuf_mtod_offset(m, struct somestruct *, m->l2_len + m->l3_len).
=> no need for l4_hdr.
lib/vhost/virtio_net.c virtio_net_with_host_offload(struct virtio_net *dev) · line 2268
Quoted 3 lines
lib/vhost/virtio_net.c +0
-0struct rte_ipv4_hdr *ipv4_hdr;struct rte_ipv6_hdr *ipv6_hdr;void *l3_hdr =NULL ;
No need for l3_hdr.
lib/vhost/virtio_net.c virtio_net_with_host_offload(struct virtio_net *dev) · line 2271
Quoted 3 lines
lib/vhost/virtio_net.c +1
-0struct rte_ether_hdr *eth_hdr;uint16_t ethertype; +uint16_t data_len = m->data_len;
Avoid direct access to mbuf internals, we have inline helpers:rte_pktmbuf_data_len(m).
lib/vhost/virtio_net.c virtio_net_with_host_offload(struct virtio_net *dev) · line 2273
Quoted 2 lines
lib/vhost/virtio_net.c +2
-0+ +if (data_len <=sizeof (struct rte_ether_hdr ))
Strictly speaking, < is enough.
lib/vhost/virtio_net.c virtio_net_with_host_offload(struct virtio_net *dev) · line 2280
Quoted 7 lines
lib/vhost/virtio_net.c +2
-0+return -EINVAL ; eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *); m->l2_len =sizeof (struct rte_ether_hdr ); ethertype =rte_be_to_cpu_16 (eth_hdr->ether_type); + data_len -=sizeof (struct rte_ether_hdr );
No need to decrement data_len if checks below are all done for absolute value. See suggestions below.
lib/vhost/virtio_net.c virtio_net_with_host_offload(struct virtio_net *dev) · line 2284
Quoted 4 lines
lib/vhost/virtio_net.c +2
-0if (ethertype ==RTE_ETHER_TYPE_VLAN ) { +if (data_len <=sizeof (struct rte_vlan_hdr )) +return -EINVAL ;
if (data_len < sizeof(rte_ether_hdr) + sizeof(struct rte_vlan_hdr))
lib/vhost/virtio_net.c virtio_net_with_host_offload(struct virtio_net *dev) · line 2291
Quoted 7 lines
lib/vhost/virtio_net.c +2
-0+struct rte_vlan_hdr *vlan_hdr = (struct rte_vlan_hdr *)(eth_hdr +1 ); m->l2_len +=sizeof (struct rte_vlan_hdr ); ethertype =rte_be_to_cpu_16 (vlan_hdr->eth_proto); + data_len -=sizeof (struct rte_vlan_hdr );
Idem.
lib/vhost/virtio_net.c virtio_net_with_host_offload(struct virtio_net *dev) · line 2299
Quoted 8 lines
lib/vhost/virtio_net.c +2
-0} l3_hdr = (char *)eth_hdr + m->l2_len;switch (ethertype) {case RTE_ETHER_TYPE_IPV4 : +if (data_len <=sizeof (struct rte_ipv4_hdr )) +return -EINVAL ;
if (data_len < m->l2_len + sizeof(struct rte_ipv4_hdr))
lib/vhost/virtio_net.c virtio_net_with_host_offload(struct virtio_net *dev) · line 2300
Quoted 1 line
lib/vhost/virtio_net.c +0
-0ipv4_hdr = l3_hdr;
ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv4_hdr *, m->l2_len);
lib/vhost/virtio_net.c virtio_net_with_host_offload(struct virtio_net *dev) · line 2303
Quoted 3 lines
lib/vhost/virtio_net.c +1
-0*l4_proto = ipv4_hdr->next_proto_id; m->l3_len =rte_ipv4_hdr_len (ipv4_hdr); +if (data_len <= m->l3_len) {
if (data_len < m->l2_len + m->l3_len)
lib/vhost/virtio_net.c virtio_net_with_host_offload(struct virtio_net *dev) · line 2304
Quoted 2 lines
lib/vhost/virtio_net.c +2
-0+ m->l3_len =0 ; +return -EINVAL ;
Returning here leaves m->l2_len set.
lib/vhost/virtio_net.c virtio_net_with_host_offload(struct virtio_net *dev) · line 2313
Quoted 8 lines
lib/vhost/virtio_net.c +4
-0+ } *l4_hdr = (char *)l3_hdr + m->l3_len; m->ol_flags |=PKT_TX_IPV4 ; + data_len -= m->l3_len;break ;case RTE_ETHER_TYPE_IPV6 : +if (data_len <=sizeof (struct rte_ipv6_hdr )) +return -EINVAL ;
if (data_len < m->l2_len + sizeof(struct rte_ipv6_hdr)) Returning here leaves m->l2_len set.
lib/vhost/virtio_net.c virtio_net_with_host_offload(struct virtio_net *dev) · line 2314
Quoted 1 line
lib/vhost/virtio_net.c +0
-0ipv6_hdr = l3_hdr;
ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv6_hdr *, m->l2_len);
Quoted 25 lines
lib/vhost/virtio_net.c +7
-0*l4_proto = ipv6_hdr->proto; m->l3_len =sizeof (struct rte_ipv6_hdr ); *l4_hdr = (char *)l3_hdr + m->l3_len; m->ol_flags |=PKT_TX_IPV6 ; + data_len -= m->l3_len;break ;default : m->l3_len =0 ;@@ -2304,6 +2324,9 @@ parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr) *l4_hdr =NULL ;break ; } + + *len = data_len; +return 0 ; }static __rte_always_inline void@@ -2312,21 +2335,27 @@ vhost_dequeue_offload_legacy(struct virtio_net_hdr *hdr, struct rte_mbuf *m)uint16_t l4_proto =0 ;void *l4_hdr =NULL ;struct rte_tcp_hdr *tcp_hdr =NULL ; +uint16_t len =0 , tcp_len; + +if (parse_ethernet (m, &l4_proto, &l4_hdr, &len) <0 )
lib/vhost/virtio_net.c vhost_dequeue_offload_legacy(struct virtio_net_hdr *hdr, struct rte_mbuf *m) · line 2348
Quoted 10 lines
lib/vhost/virtio_net.c +3
-2+return ; -parse_ethernet (m, &l4_proto, &l4_hdr);if (hdr->flags ==VIRTIO_NET_HDR_F_NEEDS_CSUM ) {if (hdr->csum_start == (m->l2_len + m->l3_len)) {switch (hdr->csum_offset) {case (offsetof(struct rte_tcp_hdr , cksum)): -if (l4_proto ==IPPROTO_TCP ) +if (l4_proto ==IPPROTO_TCP && + len >=sizeof (struct rte_tcp_hdr ))
if (rte_pktmbuf_data_len(m) >= m->l2_len + m->l3_len + sizeof(struct rte_tcp_hdr))
Then, if this check is wrong, we leave l2_len, l3_len + PKT_TX_IPVx flag set in mbuf.
These two comments apply to other updates below.
Quoted 16 lines
lib/vhost/virtio_net.c +4
-2m->ol_flags |=PKT_TX_TCP_CKSUM ;break ;case (offsetof(struct rte_udp_hdr , dgram_cksum)): -if (l4_proto ==IPPROTO_UDP ) +if (l4_proto ==IPPROTO_UDP && + len >=sizeof (struct rte_udp_hdr )) m->ol_flags |=PKT_TX_UDP_CKSUM ;break ;case (offsetof(struct rte_sctp_hdr , cksum)): -if (l4_proto ==IPPROTO_SCTP ) +if (l4_proto ==IPPROTO_SCTP && + len >=sizeof (struct rte_sctp_hdr )) m->ol_flags |=PKT_TX_SCTP_CKSUM ;break ;default :
lib/vhost/virtio_net.c vhost_dequeue_offload_legacy(struct virtio_net_hdr *hdr, struct rte_mbuf *m) · line 2374
Quoted 7 lines
lib/vhost/virtio_net.c +3
-0@@ -2339,12 +2368,21 @@ vhost_dequeue_offload_legacy(struct virtio_net_hdr *hdr, struct rte_mbuf *m)switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN ) {case VIRTIO_NET_HDR_GSO_TCPV4 :case VIRTIO_NET_HDR_GSO_TCPV6 : +if (l4_proto !=IPPROTO_TCP || + len <sizeof (struct rte_tcp_hdr )) +break ; tcp_hdr = l4_hdr;
tcp_hdr = rte_pktmbuf_mtod_offset(m, struct rte_tcp_hdr *, m->l2_len + m->l3_len);
Quoted 18 lines
lib/vhost/virtio_net.c +7
-2+ tcp_len = (tcp_hdr->data_off &0xf0 ) >>2 ; +if (len < tcp_len) +break ; m->ol_flags |=PKT_TX_TCP_SEG ; m->tso_segsz = hdr->gso_size; - m->l4_len = (tcp_hdr->data_off &0xf0 ) >>2 ; + m->l4_len = tcp_len;break ;case VIRTIO_NET_HDR_GSO_UDP : +if (l4_proto !=IPPROTO_UDP || + len <sizeof (struct rte_udp_hdr )) +break ; m->ol_flags |=PKT_TX_UDP_SEG ; m->tso_segsz = hdr->gso_size; m->l4_len =sizeof (struct rte_udp_hdr ); --2.15.1
Signature · 1 line
David Marchand
Re: [dpdk-dev] [PATCH v4] vhost: check header for legacy dequeue offload
Hi David,
Thanks for your comments.
I agree with your suggestions. BTW, I notice some other invalid corner cases which need rolling back mbuf->l2_len, l3_len and ol_flag. E.g. the default case in the "switch {}" context is not valid.
BTW, l4_proto variable is better to be a uint8_t, rather than uint16_t.
I will prepare a new version.
BRs,
Xiao
Quoted 242 lines
-----Original Message-----
From: David Marchand <[email protected]>
Sent: Tuesday, June 15, 2021 3:57 PM
To: Wang, Xiao W <[email protected]>
Cc: Maxime Coquelin <[email protected]>; Xia, Chenbo <[email protected]>; Jiang, Cheng1 <[email protected]>; dev <[email protected]>; dpdk stable <[email protected]>
Subject: Re: [dpdk-dev] [PATCH v4] vhost: check header for legacy dequeue offloadlib/vhost/virtio_net.c virtio_net_with_host_offload(struct
Quoted 5 lines · Xiao Wang
lib/vhost/virtio_net.c +0
-0@@ -2259,44 +2259,64 @@ virtio_net_with_host_offload(struct
virtio_net*dev)lib/vhost/virtio_net.c virtio_net_with_host_offload(struct virtio_net *dev) · line 2265
Quoted 9 lines
lib/vhost/virtio_net.c +3
-2return false; } -static void -parse_ethernet (struct rte_mbuf *m,uint16_t *l4_proto,void **l4_hdr) +static int +parse_ethernet (struct rte_mbuf *m,uint16_t *l4_proto,void **l4_hdr, +uint16_t *len) {This function name is misleading, name could be
parse_headers(). Its semantic gets more and more confusing with thosel4_hdrand len pointers.This function fills ->lX_len in the mbuf, everything is available for caller.
Caller can check that
rte_pktmbuf_data_len()is >=m->l2_len+m->l3_len+ somesize.
=> no need for len.
l4_hdrcan simply be deduced withrte_pktmbuf_mtod_offset(m, struct somestruct *, m->l2_len + m->l3_len).
=> no need forl4_hdr.lib/vhost/virtio_net.c virtio_net_with_host_offload(struct virtio_net *dev) · line 2268
Quoted 3 lines
lib/vhost/virtio_net.c +0
-0struct rte_ipv4_hdr *ipv4_hdr;struct rte_ipv6_hdr *ipv6_hdr;void *l3_hdr =NULL ;No need for
l3_hdr.lib/vhost/virtio_net.c virtio_net_with_host_offload(struct virtio_net *dev) · line 2271
Quoted 3 lines
lib/vhost/virtio_net.c +1
-0struct rte_ether_hdr *eth_hdr;uint16_t ethertype; +uint16_t data_len = m->data_len;Avoid direct access to mbuf internals, we have inline helpers:
rte_pktmbuf_data_len(m).lib/vhost/virtio_net.c virtio_net_with_host_offload(struct virtio_net *dev) · line 2273
Quoted 2 lines
lib/vhost/virtio_net.c +2
-0+ +if (data_len <=sizeof (struct rte_ether_hdr ))Strictly speaking, < is enough.
lib/vhost/virtio_net.c virtio_net_with_host_offload(struct virtio_net *dev) · line 2280
Quoted 7 lines
lib/vhost/virtio_net.c +2
-0+return -EINVAL ; eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *); m->l2_len =sizeof (struct rte_ether_hdr ); ethertype =rte_be_to_cpu_16 (eth_hdr->ether_type); + data_len -=sizeof (struct rte_ether_hdr );No need to decrement
data_lenif checks below are all done for absolute value.
See suggestions below.lib/vhost/virtio_net.c virtio_net_with_host_offload(struct virtio_net *dev) · line 2284
Quoted 4 lines
lib/vhost/virtio_net.c +2
-0if (ethertype ==RTE_ETHER_TYPE_VLAN ) { +if (data_len <=sizeof (struct rte_vlan_hdr )) +return -EINVAL ;if (
data_len<sizeof(rte_ether_hdr)+sizeof(struct rte_vlan_hdr))lib/vhost/virtio_net.c virtio_net_with_host_offload(struct virtio_net *dev) · line 2291
Quoted 7 lines
lib/vhost/virtio_net.c +2
-0+struct rte_vlan_hdr *vlan_hdr = (struct rte_vlan_hdr *)(eth_hdr +1 ); m->l2_len +=sizeof (struct rte_vlan_hdr ); ethertype =rte_be_to_cpu_16 (vlan_hdr->eth_proto); + data_len -=sizeof (struct rte_vlan_hdr );Idem.
lib/vhost/virtio_net.c virtio_net_with_host_offload(struct virtio_net *dev) · line 2299
Quoted 8 lines
lib/vhost/virtio_net.c +2
-0} l3_hdr = (char *)eth_hdr + m->l2_len;switch (ethertype) {case RTE_ETHER_TYPE_IPV4 : +if (data_len <=sizeof (struct rte_ipv4_hdr )) +return -EINVAL ;if (
data_len<m->l2_len+sizeof(struct rte_ipv4_hdr))lib/vhost/virtio_net.c virtio_net_with_host_offload(struct virtio_net *dev) · line 2300
Quoted 1 line
lib/vhost/virtio_net.c +0
-0ipv4_hdr = l3_hdr;
ipv4_hdr=rte_pktmbuf_mtod_offset(m, struct rte_ipv4_hdr *, m->l2_len);lib/vhost/virtio_net.c virtio_net_with_host_offload(struct virtio_net *dev) · line 2303
Quoted 3 lines
lib/vhost/virtio_net.c +1
-0*l4_proto = ipv4_hdr->next_proto_id; m->l3_len =rte_ipv4_hdr_len (ipv4_hdr); +if (data_len <= m->l3_len) {if (
data_len<m->l2_len+m->l3_len)lib/vhost/virtio_net.c virtio_net_with_host_offload(struct · line 2304
Quoted 2 lines
lib/vhost/virtio_net.c +2
-0+ m->l3_len =0 ; +return -EINVAL ;Returning here leaves
m->l2_lenset.lib/vhost/virtio_net.c virtio_net_with_host_offload(struct virtio_net *dev) · line 2313
Quoted 8 lines
lib/vhost/virtio_net.c +4
-0+ } *l4_hdr = (char *)l3_hdr + m->l3_len; m->ol_flags |=PKT_TX_IPV4 ; + data_len -= m->l3_len;break ;case RTE_ETHER_TYPE_IPV6 : +if (data_len <=sizeof (struct rte_ipv6_hdr )) +return -EINVAL ;if (
data_len<m->l2_len+sizeof(struct rte_ipv6_hdr)) Returning here leavesm->l2_lenset.lib/vhost/virtio_net.c virtio_net_with_host_offload(struct virtio_net *dev) · line 2314
Quoted 1 line
lib/vhost/virtio_net.c +0
-0ipv6_hdr = l3_hdr;
ipv6_hdr=rte_pktmbuf_mtod_offset(m, struct rte_ipv6_hdr *, m->l2_len);lib/vhost/virtio_net.c parse_ethernet(struct rte_mbuf *m, uint16_t
Quoted 9 lines
lib/vhost/virtio_net.c +1
-0*l4_proto = ipv6_hdr->proto; m->l3_len =sizeof (struct rte_ipv6_hdr ); *l4_hdr = (char *)l3_hdr + m->l3_len; m->ol_flags |=PKT_TX_IPV6 ; + data_len -= m->l3_len;break ;default : m->l3_len =0 ;@@ -2304,6 +2324,9 @@ parse_ethernet(struct rte_mbuf *m, uint16_t
*l4_proto, void **l4_hdr)lib/vhost/virtio_net.c vhost_dequeue_offload_legacy(struct
Quoted 10 lines
lib/vhost/virtio_net.c +3
-0*l4_hdr =NULL ;break ; } + + *len = data_len; +return 0 ; }static __rte_always_inline void@@ -2312,21 +2335,27 @@ vhost_dequeue_offload_legacy(struct
virtio_net_hdr*hdr, structrte_mbuf*m)Quoted 6 lines
lib/vhost/virtio_net.c +3
-0uint16_t l4_proto =0 ;void *l4_hdr =NULL ;struct rte_tcp_hdr *tcp_hdr =NULL ; +uint16_t len =0 , tcp_len; + +if (parse_ethernet (m, &l4_proto, &l4_hdr, &len) <0 )lib/vhost/virtio_net.c vhost_dequeue_offload_legacy(struct virtio_net_hdr *hdr, struct rte_mbuf *m) · line 2348
Quoted 10 lines
lib/vhost/virtio_net.c +3
-2+return ; -parse_ethernet (m, &l4_proto, &l4_hdr);if (hdr->flags ==VIRTIO_NET_HDR_F_NEEDS_CSUM ) {if (hdr->csum_start == (m->l2_len + m->l3_len)) {switch (hdr->csum_offset) {case (offsetof(struct rte_tcp_hdr , cksum)): -if (l4_proto ==IPPROTO_TCP ) +if (l4_proto ==IPPROTO_TCP && + len >=sizeof (struct rte_tcp_hdr ))if (
rte_pktmbuf_data_len(m)>=m->l2_len+m->l3_len+sizeof(struct rte_tcp_hdr))
Then, if this check is wrong, we leavel2_len,l3_len+PKT_TX_IPVxflag set in mbuf.These two comments apply to other updates below.
lib/vhost/virtio_net.c vhost_dequeue_offload_legacy(struct
Quoted 16 lines
lib/vhost/virtio_net.c +4
-2m->ol_flags |=PKT_TX_TCP_CKSUM ;break ;case (offsetof(struct rte_udp_hdr , dgram_cksum)): -if (l4_proto ==IPPROTO_UDP ) +if (l4_proto ==IPPROTO_UDP && + len >=sizeof (struct rte_udp_hdr )) m->ol_flags |=PKT_TX_UDP_CKSUM ;break ;case (offsetof(struct rte_sctp_hdr , cksum)): -if (l4_proto ==IPPROTO_SCTP ) +if (l4_proto ==IPPROTO_SCTP && + len >=sizeof (struct rte_sctp_hdr )) m->ol_flags |=PKT_TX_SCTP_CKSUM ;break ;default :@@ -2339,12 +2368,21 @@ vhost_dequeue_offload_legacy(struct
virtio_net_hdr*hdr, structrte_mbuf*m)lib/vhost/virtio_net.c vhost_dequeue_offload_legacy(struct virtio_net_hdr *hdr, struct rte_mbuf *m) · line 2374
Quoted 7 lines
lib/vhost/virtio_net.c +3
-0switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN ) {case VIRTIO_NET_HDR_GSO_TCPV4 :case VIRTIO_NET_HDR_GSO_TCPV6 : +if (l4_proto !=IPPROTO_TCP || + len <sizeof (struct rte_tcp_hdr )) +break ; tcp_hdr = l4_hdr;
tcp_hdr=rte_pktmbuf_mtod_offset(m, struct rte_tcp_hdr *, m->l2_len + m->l3_len);Quoted 16 lines
lib/vhost/virtio_net.c +7
-2+ tcp_len = (tcp_hdr->data_off &0xf0 ) >>2 ; +if (len < tcp_len) +break ; m->ol_flags |=PKT_TX_TCP_SEG ; m->tso_segsz = hdr->gso_size; - m->l4_len = (tcp_hdr->data_off &0xf0 ) >>2 ; + m->l4_len = tcp_len;break ;case VIRTIO_NET_HDR_GSO_UDP : +if (l4_proto !=IPPROTO_UDP || + len <sizeof (struct rte_udp_hdr )) +break ; m->ol_flags |=PKT_TX_UDP_SEG ; m->tso_segsz = hdr->gso_size; m->l4_len =sizeof (struct rte_udp_hdr ); --Quoted 2 lines
2.15.1
--
David Marchand
PATCH v5 1/1 vhost: check header for legacy dequeue offload
In reply to Xiao WangWhen parsing the virtio net header and packet header for dequeue offload, we need to perform sanity check on the packet header to ensure:
- No out-of-boundary memory access.
- The packet header and virtio_net header are valid and aligned.
---
v5:
- Redefine the function parse_ethernet() to parse_headers(). (David)
- Use mbuf helpers e.g. rte_pktmbuf_data_len() and rte_pktmbuf_mtod_offset(). (David)
- Reset mbuf l2_len, l3_len and ol_flags when detecting anything invalid. (David)
- Improve some check conditions. (David)
- Move the data_len check for L4 header into parse_headers(), in order to avoid duplicated checks in CSUM and GSO.
- Use uint8_t instead of uint16_t for l4_proto variable.
- Detect more invalid corner cases.
v4:
- Rebase on head of main branch.
- Allow empty L4 payload in GSO.
v3:
- Check data_len before calling rte_pktmbuf_mtod. (David)
v2:
- Allow empty L4 payload for cksum offload. (Konstantin)
--- lib/vhost/virtio_net.c | 117 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 89 insertions(+), 28 deletions(-)
lib/vhost/virtio_net.c +89 -28
@@ -2259,14 +2259,17 @@ virtio_net_with_host_offload(struct virtio_net *dev)return false; } -static void -parse_ethernet (struct rte_mbuf *m,uint16_t *l4_proto,void **l4_hdr) +static int +parse_headers (struct rte_mbuf *m,uint8_t *l4_proto) {struct rte_ipv4_hdr *ipv4_hdr;struct rte_ipv6_hdr *ipv6_hdr; -void *l3_hdr =NULL ;struct rte_ether_hdr *eth_hdr;uint16_t ethertype; +uint16_t data_len =rte_pktmbuf_data_len (m); + +if (data_len <sizeof (struct rte_ether_hdr )) +return -EINVAL ; eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
@@ -2274,6 +2277,10 @@ parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr) ethertype =rte_be_to_cpu_16 (eth_hdr->ether_type);if (ethertype ==RTE_ETHER_TYPE_VLAN ) { +if (data_len <sizeof (struct rte_ether_hdr ) + +sizeof (struct rte_vlan_hdr )) + goto error; +struct rte_vlan_hdr *vlan_hdr = (struct rte_vlan_hdr *)(eth_hdr +1 );
@@ -2281,70 +2288,118 @@ parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr) ethertype =rte_be_to_cpu_16 (vlan_hdr->eth_proto); } - l3_hdr = (char *)eth_hdr + m->l2_len; -switch (ethertype) {case RTE_ETHER_TYPE_IPV4 : - ipv4_hdr = l3_hdr; - *l4_proto = ipv4_hdr->next_proto_id; +if (data_len < m->l2_len +sizeof (struct rte_ipv4_hdr )) + goto error; + ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv4_hdr *, + m->l2_len); m->l3_len =rte_ipv4_hdr_len (ipv4_hdr); - *l4_hdr = (char *)l3_hdr + m->l3_len; +if (data_len < m->l2_len + m->l3_len) + goto error; m->ol_flags |=PKT_TX_IPV4 ; + *l4_proto = ipv4_hdr->next_proto_id;break ;case RTE_ETHER_TYPE_IPV6 : - ipv6_hdr = l3_hdr; - *l4_proto = ipv6_hdr->proto; +if (data_len < m->l2_len +sizeof (struct rte_ipv6_hdr )) + goto error; + ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv6_hdr *, + m->l2_len); m->l3_len =sizeof (struct rte_ipv6_hdr ); - *l4_hdr = (char *)l3_hdr + m->l3_len; m->ol_flags |=PKT_TX_IPV6 ; + *l4_proto = ipv6_hdr->proto;break ;default : - m->l3_len =0 ; - *l4_proto =0 ; - *l4_hdr =NULL ; +/* a valid L3 header is needed for further L4 parsing */ + goto error; + } + +/* both CSUM and GSO need a valid L4 header */ +switch (*l4_proto) { +case IPPROTO_TCP : +if (data_len < m->l2_len + m->l3_len + +sizeof (struct rte_tcp_hdr )) + goto error; +break ; +case IPPROTO_UDP : +if (data_len < m->l2_len + m->l3_len + +sizeof (struct rte_udp_hdr )) + goto error;break ; +case IPPROTO_SCTP : +if (data_len < m->l2_len + m->l3_len + +sizeof (struct rte_sctp_hdr )) + goto error; +break ; +default : + goto error; } + +return 0 ; + +error: + m->l2_len =0 ; + m->l3_len =0 ; + m->ol_flags =0 ; +return -EINVAL ; } static __rte_always_inline voidvhost_dequeue_offload_legacy (struct virtio_net_hdr *hdr,struct rte_mbuf *m) { -uint16_t l4_proto =0 ; -void *l4_hdr =NULL ; +uint8_t l4_proto =0 ;struct rte_tcp_hdr *tcp_hdr =NULL ; +uint16_t tcp_len; +uint16_t data_len =rte_pktmbuf_data_len (m); + +if (parse_headers (m, &l4_proto) <0 ) +return ; -parse_ethernet (m, &l4_proto, &l4_hdr);if (hdr->flags ==VIRTIO_NET_HDR_F_NEEDS_CSUM ) {if (hdr->csum_start == (m->l2_len + m->l3_len)) {switch (hdr->csum_offset) {case (offsetof(struct rte_tcp_hdr , cksum)): -if (l4_proto ==IPPROTO_TCP ) - m->ol_flags |=PKT_TX_TCP_CKSUM ; +if (l4_proto !=IPPROTO_TCP ) + goto error; + m->ol_flags |=PKT_TX_TCP_CKSUM ;break ;case (offsetof(struct rte_udp_hdr , dgram_cksum)): -if (l4_proto ==IPPROTO_UDP ) - m->ol_flags |=PKT_TX_UDP_CKSUM ; +if (l4_proto !=IPPROTO_UDP ) + goto error; + m->ol_flags |=PKT_TX_UDP_CKSUM ;break ;case (offsetof(struct rte_sctp_hdr , cksum)): -if (l4_proto ==IPPROTO_SCTP ) - m->ol_flags |=PKT_TX_SCTP_CKSUM ; +if (l4_proto !=IPPROTO_SCTP ) + goto error; + m->ol_flags |=PKT_TX_SCTP_CKSUM ;break ;default : -break ; + goto error; } + }else { + goto error; } } -if (l4_hdr && hdr->gso_type !=VIRTIO_NET_HDR_GSO_NONE ) { +if (hdr->gso_type !=VIRTIO_NET_HDR_GSO_NONE ) {switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN ) {case VIRTIO_NET_HDR_GSO_TCPV4 :case VIRTIO_NET_HDR_GSO_TCPV6 : - tcp_hdr = l4_hdr; +if (l4_proto !=IPPROTO_TCP ) + goto error; + tcp_hdr =rte_pktmbuf_mtod_offset (m, + struct rte_tcp_hdr *, + m->l2_len + m->l3_len); + tcp_len = (tcp_hdr->data_off &0xf0 ) >>2 ; +if (data_len < m->l2_len + m->l3_len + tcp_len) + goto error; m->ol_flags |=PKT_TX_TCP_SEG ; m->tso_segsz = hdr->gso_size; - m->l4_len = (tcp_hdr->data_off &0xf0 ) >>2 ; + m->l4_len = tcp_len;break ;case VIRTIO_NET_HDR_GSO_UDP : +if (l4_proto !=IPPROTO_UDP ) + goto error; m->ol_flags |=PKT_TX_UDP_SEG ; m->tso_segsz = hdr->gso_size; m->l4_len =sizeof (struct rte_udp_hdr );
@@ -2352,9 +2407,15 @@ vhost_dequeue_offload_legacy(struct virtio_net_hdr *hdr, struct rte_mbuf *m)default :VHOST_LOG_DATA (WARNING ,"unsupported gso type %u.\n" , hdr->gso_type); -break ; + goto error; } } +return ; + +error: + m->l2_len =0 ; + m->l3_len =0 ; + m->ol_flags =0 ; }static __rte_always_inline void
Signature · 1 line
2.15.1
Re: [dpdk-dev] [PATCH v5] vhost: check header for legacy dequeue offload
Hi Xiao,
Quoted 24 lines · Xiao Wang
When parsing the virtio net header and packet header for dequeue offload, we need to perform sanity check on the packet header to ensure:
- No out-of-boundary memory access.
- The packet header andvirtio_netheader are valid and aligned.Fixes d0cf91303d73 ("vhost: add Tx offload capabilities")Signed-off-by Xiao Wang <[email protected]>---
v5:
- Redefine the functionparse_ethernet()toparse_headers(). (David)
- Use mbuf helpers e.g.rte_pktmbuf_data_len()andrte_pktmbuf_mtod_offset(). (David)
- Reset mbufl2_len,l3_lenandol_flagswhen detecting anything invalid. (David)
- Improve some check conditions. (David)
- Move thedata_lencheck for L4 header intoparse_headers(), in order to avoid duplicated checks in CSUM and GSO.
- Useuint8_tinstead ofuint16_tforl4_protovariable.
- Detect more invalid corner cases.v4:
- Rebase on head of main branch.
- Allow empty L4 payload in GSO.
Quoted 9 lines
v3:
- Checkdata_lenbefore callingrte_pktmbuf_mtod. (David)v2:
- Allow empty L4 payload for cksum offload. (Konstantin)--- lib/vhost/virtio_net.c | 117 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 89 insertions(+), 28 deletions(-)
Thanks for the fix, it looks good to me:
Thanks,
Maxime
Xia, Chenbo
AppliedRe: [dpdk-dev] [PATCH v5] vhost: check header for legacy dequeue offload
In reply to Xiao WangQuoted 14 lines
-----Original Message-----
From: Wang, Xiao W <[email protected]>
Sent: Monday, June 21, 2021 4:21 PM
To: [email protected]; Xia, Chenbo <[email protected]>; [email protected]
Cc: Jiang, Cheng1 <[email protected]>; [email protected]; Wang, Xiao W <[email protected]>; [email protected]
Subject: [PATCH v5] vhost: check header for legacy dequeue offloadWhen parsing the virtio net header and packet header for dequeue offload, we need to perform sanity check on the packet header to ensure:
- No out-of-boundary memory access.
- The packet header andvirtio_netheader are valid and aligned.
Quoted 5 lines
Fixes d0cf91303d73 ("vhost: add Tx offload capabilities")Signed-off-by Xiao Wang <[email protected]>2.15.1
Applied to next-virtio/main, thanks.