patches for DPDK stable branches
 help / color / mirror / Atom feed
* [dpdk-stable] [PATCH 17.11 0/3] Vhost zero-copy backports for 17.11
@ 2019-08-09 11:11 Tiwei Bie
  2019-08-09 11:11 ` [dpdk-stable] [PATCH 17.11 1/3] vhost: restore mbuf first when freeing zmbuf Tiwei Bie
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Tiwei Bie @ 2019-08-09 11:11 UTC (permalink / raw)
  To: yskoh, stable; +Cc: maxime.coquelin, zhihong.wang

Tiwei Bie (3):
  vhost: restore mbuf first when freeing zmbuf
  vhost: fix potential use-after-free for zero copy mbuf
  vhost: fix potential use-after-free for memory region

 lib/librte_vhost/vhost.h      | 34 ++++++++++++++++++++++++++++
 lib/librte_vhost/vhost_user.c | 42 +++++++++++++++++++++++++++--------
 lib/librte_vhost/virtio_net.c | 34 ----------------------------
 3 files changed, 67 insertions(+), 43 deletions(-)

-- 
2.17.1


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [dpdk-stable] [PATCH 17.11 1/3] vhost: restore mbuf first when freeing zmbuf
  2019-08-09 11:11 [dpdk-stable] [PATCH 17.11 0/3] Vhost zero-copy backports for 17.11 Tiwei Bie
@ 2019-08-09 11:11 ` Tiwei Bie
  2019-08-09 11:11 ` [dpdk-stable] [PATCH 17.11 2/3] vhost: fix potential use-after-free for zero copy mbuf Tiwei Bie
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Tiwei Bie @ 2019-08-09 11:11 UTC (permalink / raw)
  To: yskoh, stable; +Cc: maxime.coquelin, zhihong.wang

[ backported from upstream commit 041d37b2ef25faeb1c00ed70a5fc2ea6e93c4828 ]

The mbufs should also be restored in free_zmbufs().

Fixes: b0a985d1f340 ("vhost: add dequeue zero copy")
Fixes: 3ebd930588b7 ("vhost: fix mbuf free")

Signed-off-by: Tiwei Bie <tiwei.bie@intel.com>
Reviewed-by: Maxime Coquelin <maxime.coquelin@redhat.com>
---
 lib/librte_vhost/vhost.h      | 16 ++++++++++++++++
 lib/librte_vhost/vhost_user.c |  1 +
 lib/librte_vhost/virtio_net.c | 16 ----------------
 3 files changed, 17 insertions(+), 16 deletions(-)

diff --git a/lib/librte_vhost/vhost.h b/lib/librte_vhost/vhost.h
index 11ce27118..c9e86a2c9 100644
--- a/lib/librte_vhost/vhost.h
+++ b/lib/librte_vhost/vhost.h
@@ -513,4 +513,20 @@ vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
 	return __vhost_iova_to_vva(dev, vq, iova, len, perm);
 }
 
+static __rte_always_inline void
+restore_mbuf(struct rte_mbuf *m)
+{
+	uint32_t mbuf_size, priv_size;
+
+	while (m) {
+		priv_size = rte_pktmbuf_priv_size(m->pool);
+		mbuf_size = sizeof(struct rte_mbuf) + priv_size;
+		/* start of buffer is after mbuf structure and priv data */
+
+		m->buf_addr = (char *)m + mbuf_size;
+		m->buf_iova = rte_mempool_virt2iova(m) + mbuf_size;
+		m = m->next;
+	}
+}
+
 #endif /* _VHOST_NET_CDEV_H_ */
diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c
index 84c18f965..7c0d8c6e4 100644
--- a/lib/librte_vhost/vhost_user.c
+++ b/lib/librte_vhost/vhost_user.c
@@ -888,6 +888,7 @@ free_zmbufs(struct vhost_virtqueue *vq)
 	     zmbuf != NULL; zmbuf = next) {
 		next = TAILQ_NEXT(zmbuf, next);
 
+		restore_mbuf(zmbuf->mbuf);
 		rte_pktmbuf_free(zmbuf->mbuf);
 		TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
 	}
diff --git a/lib/librte_vhost/virtio_net.c b/lib/librte_vhost/virtio_net.c
index 07d4609ee..e3d50c96b 100644
--- a/lib/librte_vhost/virtio_net.c
+++ b/lib/librte_vhost/virtio_net.c
@@ -1466,22 +1466,6 @@ mbuf_is_consumed(struct rte_mbuf *m)
 	return true;
 }
 
-static __rte_always_inline void
-restore_mbuf(struct rte_mbuf *m)
-{
-	uint32_t mbuf_size, priv_size;
-
-	while (m) {
-		priv_size = rte_pktmbuf_priv_size(m->pool);
-		mbuf_size = sizeof(struct rte_mbuf) + priv_size;
-		/* start of buffer is after mbuf structure and priv data */
-
-		m->buf_addr = (char *)m + mbuf_size;
-		m->buf_iova = rte_mempool_virt2iova(m) + mbuf_size;
-		m = m->next;
-	}
-}
-
 uint16_t
 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
 	struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
-- 
2.17.1


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [dpdk-stable] [PATCH 17.11 2/3] vhost: fix potential use-after-free for zero copy mbuf
  2019-08-09 11:11 [dpdk-stable] [PATCH 17.11 0/3] Vhost zero-copy backports for 17.11 Tiwei Bie
  2019-08-09 11:11 ` [dpdk-stable] [PATCH 17.11 1/3] vhost: restore mbuf first when freeing zmbuf Tiwei Bie
@ 2019-08-09 11:11 ` Tiwei Bie
  2019-08-09 11:11 ` [dpdk-stable] [PATCH 17.11 3/3] vhost: fix potential use-after-free for memory region Tiwei Bie
  2019-08-12 18:52 ` [dpdk-stable] [PATCH 17.11 0/3] Vhost zero-copy backports for 17.11 Yongseok Koh
  3 siblings, 0 replies; 5+ messages in thread
From: Tiwei Bie @ 2019-08-09 11:11 UTC (permalink / raw)
  To: yskoh, stable; +Cc: maxime.coquelin, zhihong.wang

[ backported from upstream commit d767436ee5d26d1d417ae17d1a2a47879bf632a6 ]

Don't free the zero copy mbufs before they have been consumed,
otherwise there could be use-after-free.

Fixes: b0a985d1f340 ("vhost: add dequeue zero copy")

Signed-off-by: Tiwei Bie <tiwei.bie@intel.com>
Reviewed-by: Maxime Coquelin <maxime.coquelin@redhat.com>
---
 lib/librte_vhost/vhost.h      | 12 ++++++++++++
 lib/librte_vhost/vhost_user.c |  3 +++
 lib/librte_vhost/virtio_net.c | 12 ------------
 3 files changed, 15 insertions(+), 12 deletions(-)

diff --git a/lib/librte_vhost/vhost.h b/lib/librte_vhost/vhost.h
index c9e86a2c9..ccd6c015c 100644
--- a/lib/librte_vhost/vhost.h
+++ b/lib/librte_vhost/vhost.h
@@ -529,4 +529,16 @@ restore_mbuf(struct rte_mbuf *m)
 	}
 }
 
+static __rte_always_inline bool
+mbuf_is_consumed(struct rte_mbuf *m)
+{
+	while (m) {
+		if (rte_mbuf_refcnt_read(m) > 1)
+			return false;
+		m = m->next;
+	}
+
+	return true;
+}
+
 #endif /* _VHOST_NET_CDEV_H_ */
diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c
index 7c0d8c6e4..63b200127 100644
--- a/lib/librte_vhost/vhost_user.c
+++ b/lib/librte_vhost/vhost_user.c
@@ -888,6 +888,9 @@ free_zmbufs(struct vhost_virtqueue *vq)
 	     zmbuf != NULL; zmbuf = next) {
 		next = TAILQ_NEXT(zmbuf, next);
 
+		while (!mbuf_is_consumed(zmbuf->mbuf))
+			usleep(1000);
+
 		restore_mbuf(zmbuf->mbuf);
 		rte_pktmbuf_free(zmbuf->mbuf);
 		TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
diff --git a/lib/librte_vhost/virtio_net.c b/lib/librte_vhost/virtio_net.c
index e3d50c96b..71d3d9c5b 100644
--- a/lib/librte_vhost/virtio_net.c
+++ b/lib/librte_vhost/virtio_net.c
@@ -1454,18 +1454,6 @@ get_zmbuf(struct vhost_virtqueue *vq)
 	return NULL;
 }
 
-static __rte_always_inline bool
-mbuf_is_consumed(struct rte_mbuf *m)
-{
-	while (m) {
-		if (rte_mbuf_refcnt_read(m) > 1)
-			return false;
-		m = m->next;
-	}
-
-	return true;
-}
-
 uint16_t
 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
 	struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
-- 
2.17.1


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [dpdk-stable] [PATCH 17.11 3/3] vhost: fix potential use-after-free for memory region
  2019-08-09 11:11 [dpdk-stable] [PATCH 17.11 0/3] Vhost zero-copy backports for 17.11 Tiwei Bie
  2019-08-09 11:11 ` [dpdk-stable] [PATCH 17.11 1/3] vhost: restore mbuf first when freeing zmbuf Tiwei Bie
  2019-08-09 11:11 ` [dpdk-stable] [PATCH 17.11 2/3] vhost: fix potential use-after-free for zero copy mbuf Tiwei Bie
@ 2019-08-09 11:11 ` Tiwei Bie
  2019-08-12 18:52 ` [dpdk-stable] [PATCH 17.11 0/3] Vhost zero-copy backports for 17.11 Yongseok Koh
  3 siblings, 0 replies; 5+ messages in thread
From: Tiwei Bie @ 2019-08-09 11:11 UTC (permalink / raw)
  To: yskoh, stable; +Cc: maxime.coquelin, zhihong.wang

[ backported from upstream commit 2a2904fa9cc44493bcea495bab944b032b24f7cb ]

Reclaim outstanding zmbufs first before freeing memory regions,
otherwise there could be use-after-free.

Fixes: b0a985d1f340 ("vhost: add dequeue zero copy")

Signed-off-by: Tiwei Bie <tiwei.bie@intel.com>
Reviewed-by: Maxime Coquelin <maxime.coquelin@redhat.com>
---
 lib/librte_vhost/vhost.h      |  6 +++++
 lib/librte_vhost/vhost_user.c | 46 +++++++++++++++++++++++++----------
 lib/librte_vhost/virtio_net.c |  6 -----
 3 files changed, 39 insertions(+), 19 deletions(-)

diff --git a/lib/librte_vhost/vhost.h b/lib/librte_vhost/vhost.h
index ccd6c015c..55bfb0b25 100644
--- a/lib/librte_vhost/vhost.h
+++ b/lib/librte_vhost/vhost.h
@@ -541,4 +541,10 @@ mbuf_is_consumed(struct rte_mbuf *m)
 	return true;
 }
 
+static __rte_always_inline void
+put_zmbuf(struct zcopy_mbuf *zmbuf)
+{
+	zmbuf->in_use = 0;
+}
+
 #endif /* _VHOST_NET_CDEV_H_ */
diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c
index 63b200127..bb39999aa 100644
--- a/lib/librte_vhost/vhost_user.c
+++ b/lib/librte_vhost/vhost_user.c
@@ -91,15 +91,47 @@ get_blk_size(int fd)
 	return ret == -1 ? (uint64_t)-1 : (uint64_t)stat.st_blksize;
 }
 
+/*
+ * Reclaim all the outstanding zmbufs for a virtqueue.
+ */
+static void
+drain_zmbuf_list(struct vhost_virtqueue *vq)
+{
+	struct zcopy_mbuf *zmbuf, *next;
+
+	for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
+	     zmbuf != NULL; zmbuf = next) {
+		next = TAILQ_NEXT(zmbuf, next);
+
+		while (!mbuf_is_consumed(zmbuf->mbuf))
+			usleep(1000);
+
+		TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
+		restore_mbuf(zmbuf->mbuf);
+		rte_pktmbuf_free(zmbuf->mbuf);
+		put_zmbuf(zmbuf);
+		vq->nr_zmbuf -= 1;
+	}
+}
+
 static void
 free_mem_region(struct virtio_net *dev)
 {
 	uint32_t i;
 	struct rte_vhost_mem_region *reg;
+	struct vhost_virtqueue *vq;
 
 	if (!dev || !dev->mem)
 		return;
 
+	if (dev->dequeue_zero_copy) {
+		for (i = 0; i < dev->nr_vring; i++) {
+			vq = dev->virtqueue[i];
+			if (vq)
+				drain_zmbuf_list(vq);
+		}
+	}
+
 	for (i = 0; i < dev->mem->nregions; i++) {
 		reg = &dev->mem->regions[i];
 		if (reg->host_user_addr) {
@@ -882,19 +914,7 @@ vhost_user_set_vring_kick(struct virtio_net **pdev, struct VhostUserMsg *pmsg)
 static void
 free_zmbufs(struct vhost_virtqueue *vq)
 {
-	struct zcopy_mbuf *zmbuf, *next;
-
-	for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
-	     zmbuf != NULL; zmbuf = next) {
-		next = TAILQ_NEXT(zmbuf, next);
-
-		while (!mbuf_is_consumed(zmbuf->mbuf))
-			usleep(1000);
-
-		restore_mbuf(zmbuf->mbuf);
-		rte_pktmbuf_free(zmbuf->mbuf);
-		TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
-	}
+	drain_zmbuf_list(vq);
 
 	rte_free(vq->zmbufs);
 }
diff --git a/lib/librte_vhost/virtio_net.c b/lib/librte_vhost/virtio_net.c
index 71d3d9c5b..de8e4c57e 100644
--- a/lib/librte_vhost/virtio_net.c
+++ b/lib/librte_vhost/virtio_net.c
@@ -1112,12 +1112,6 @@ make_rarp_packet(struct rte_mbuf *rarp_mbuf, const struct ether_addr *mac)
 	return 0;
 }
 
-static __rte_always_inline void
-put_zmbuf(struct zcopy_mbuf *zmbuf)
-{
-	zmbuf->in_use = 0;
-}
-
 static __rte_always_inline int
 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
 		  struct vring_desc *descs, uint16_t max_desc,
-- 
2.17.1


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [dpdk-stable] [PATCH 17.11 0/3] Vhost zero-copy backports for 17.11
  2019-08-09 11:11 [dpdk-stable] [PATCH 17.11 0/3] Vhost zero-copy backports for 17.11 Tiwei Bie
                   ` (2 preceding siblings ...)
  2019-08-09 11:11 ` [dpdk-stable] [PATCH 17.11 3/3] vhost: fix potential use-after-free for memory region Tiwei Bie
@ 2019-08-12 18:52 ` Yongseok Koh
  3 siblings, 0 replies; 5+ messages in thread
From: Yongseok Koh @ 2019-08-12 18:52 UTC (permalink / raw)
  To: Tiwei Bie; +Cc: dpdk stable, Maxime Coquelin, zhihong.wang

> On Aug 9, 2019, at 4:11 AM, Tiwei Bie <tiwei.bie@intel.com> wrote:
> 
> Tiwei Bie (3):
>  vhost: restore mbuf first when freeing zmbuf
>  vhost: fix potential use-after-free for zero copy mbuf
>  vhost: fix potential use-after-free for memory region
> 
> lib/librte_vhost/vhost.h      | 34 ++++++++++++++++++++++++++++
> lib/librte_vhost/vhost_user.c | 42 +++++++++++++++++++++++++++--------
> lib/librte_vhost/virtio_net.c | 34 ----------------------------
> 3 files changed, 67 insertions(+), 43 deletions(-)

series applied to stable/17.11

thanks,
Yongseok

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2019-08-12 18:52 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-09 11:11 [dpdk-stable] [PATCH 17.11 0/3] Vhost zero-copy backports for 17.11 Tiwei Bie
2019-08-09 11:11 ` [dpdk-stable] [PATCH 17.11 1/3] vhost: restore mbuf first when freeing zmbuf Tiwei Bie
2019-08-09 11:11 ` [dpdk-stable] [PATCH 17.11 2/3] vhost: fix potential use-after-free for zero copy mbuf Tiwei Bie
2019-08-09 11:11 ` [dpdk-stable] [PATCH 17.11 3/3] vhost: fix potential use-after-free for memory region Tiwei Bie
2019-08-12 18:52 ` [dpdk-stable] [PATCH 17.11 0/3] Vhost zero-copy backports for 17.11 Yongseok Koh

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).