vhost: fix potential use-after-free for zero copy mbuf
authorTiwei Bie <tiwei.bie@intel.com>
Fri, 22 Feb 2019 02:42:07 +0000 (10:42 +0800)
committerFerruh Yigit <ferruh.yigit@intel.com>
Fri, 1 Mar 2019 17:17:36 +0000 (18:17 +0100)
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")
Cc: stable@dpdk.org
Signed-off-by: Tiwei Bie <tiwei.bie@intel.com>
Reviewed-by: Maxime Coquelin <maxime.coquelin@redhat.com>
lib/librte_vhost/vhost.h
lib/librte_vhost/vhost_user.c
lib/librte_vhost/virtio_net.c

index bcfce27..044651b 100644 (file)
@@ -757,4 +757,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_ */
index e3ddf25..6d82535 100644 (file)
@@ -1218,6 +1218,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);
index 862ca5e..40a2923 100644 (file)
@@ -1306,18 +1306,6 @@ again:
        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;
-}
-
 static __rte_always_inline uint16_t
 virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
        struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)