New upstream version 18.05
[deb_dpdk.git] / lib / librte_vhost / vhost_user.c
index 2d008fb..947290f 100644 (file)
@@ -1,5 +1,22 @@
 /* SPDX-License-Identifier: BSD-3-Clause
- * Copyright(c) 2010-2016 Intel Corporation
+ * Copyright(c) 2010-2018 Intel Corporation
+ */
+
+/* Security model
+ * --------------
+ * The vhost-user protocol connection is an external interface, so it must be
+ * robust against invalid inputs.
+ *
+ * This is important because the vhost-user master is only one step removed
+ * from the guest.  Malicious guests that have escaped will then launch further
+ * attacks from the vhost-user master.
+ *
+ * Even in deployments where guests are trusted, a bug in the vhost-user master
+ * can still cause invalid messages to be sent.  Such messages must not
+ * compromise the stability of the DPDK application by causing crashes, memory
+ * corruption, or other problematic behavior.
+ *
+ * Do not assume received VhostUserMsg fields contain sensible values!
  */
 
 #include <stdint.h>
@@ -50,6 +67,8 @@ static const char *vhost_message_str[VHOST_USER_MAX] = {
        [VHOST_USER_NET_SET_MTU]  = "VHOST_USER_NET_SET_MTU",
        [VHOST_USER_SET_SLAVE_REQ_FD]  = "VHOST_USER_SET_SLAVE_REQ_FD",
        [VHOST_USER_IOTLB_MSG]  = "VHOST_USER_IOTLB_MSG",
+       [VHOST_USER_CRYPTO_CREATE_SESS] = "VHOST_USER_CRYPTO_CREATE_SESS",
+       [VHOST_USER_CRYPTO_CLOSE_SESS] = "VHOST_USER_CRYPTO_CLOSE_SESS",
 };
 
 static uint64_t
@@ -116,7 +135,14 @@ vhost_user_set_owner(void)
 static int
 vhost_user_reset_owner(struct virtio_net *dev)
 {
+       struct rte_vdpa_device *vdpa_dev;
+       int did = -1;
+
        if (dev->flags & VIRTIO_DEV_RUNNING) {
+               did = dev->vdpa_dev_id;
+               vdpa_dev = rte_vdpa_get_device(did);
+               if (vdpa_dev && vdpa_dev->ops->dev_close)
+                       vdpa_dev->ops->dev_close(dev->vid);
                dev->flags &= ~VIRTIO_DEV_RUNNING;
                dev->notify_ops->destroy_device(dev->vid);
        }
@@ -138,6 +164,18 @@ vhost_user_get_features(struct virtio_net *dev)
        return features;
 }
 
+/*
+ * The queue number that we support are requested.
+ */
+static uint32_t
+vhost_user_get_queue_num(struct virtio_net *dev)
+{
+       uint32_t queue_num = 0;
+
+       rte_vhost_driver_get_queue_num(dev->ifname, &queue_num);
+       return queue_num;
+}
+
 /*
  * We receive the negotiated features supported by us and the virtio device.
  */
@@ -145,6 +183,8 @@ static int
 vhost_user_set_features(struct virtio_net *dev, uint64_t features)
 {
        uint64_t vhost_features = 0;
+       struct rte_vdpa_device *vdpa_dev;
+       int did = -1;
 
        rte_vhost_driver_get_features(dev->ifname, &vhost_features);
        if (features & ~vhost_features) {
@@ -181,7 +221,7 @@ vhost_user_set_features(struct virtio_net *dev, uint64_t features)
        } else {
                dev->vhost_hlen = sizeof(struct virtio_net_hdr);
        }
-       LOG_DEBUG(VHOST_CONFIG,
+       VHOST_LOG_DEBUG(VHOST_CONFIG,
                "(%d) mergeable RX buffers %s, virtio 1 %s\n",
                dev->vid,
                (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ? "on" : "off",
@@ -207,6 +247,11 @@ vhost_user_set_features(struct virtio_net *dev, uint64_t features)
                }
        }
 
+       did = dev->vdpa_dev_id;
+       vdpa_dev = rte_vdpa_get_device(did);
+       if (vdpa_dev && vdpa_dev->ops->set_features)
+               vdpa_dev->ops->set_features(dev->vid);
+
        return 0;
 }
 
@@ -221,6 +266,17 @@ vhost_user_set_vring_num(struct virtio_net *dev,
 
        vq->size = msg->payload.state.num;
 
+       /* VIRTIO 1.0, 2.4 Virtqueues says:
+        *
+        *   Queue Size value is always a power of 2. The maximum Queue Size
+        *   value is 32768.
+        */
+       if ((vq->size & (vq->size - 1)) || vq->size > 32768) {
+               RTE_LOG(ERR, VHOST_CONFIG,
+                       "invalid virtqueue size %u\n", vq->size);
+               return -1;
+       }
+
        if (dev->dequeue_zero_copy) {
                vq->nr_zmbuf = 0;
                vq->last_zmbuf_idx = 0;
@@ -474,13 +530,13 @@ translate_ring_addresses(struct virtio_net *dev, int vq_index)
 
        vq->log_guest_addr = addr->log_guest_addr;
 
-       LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address desc: %p\n",
+       VHOST_LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address desc: %p\n",
                        dev->vid, vq->desc);
-       LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address avail: %p\n",
+       VHOST_LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address avail: %p\n",
                        dev->vid, vq->avail);
-       LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address used: %p\n",
+       VHOST_LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address used: %p\n",
                        dev->vid, vq->used);
-       LOG_DEBUG(VHOST_CONFIG, "(%d) log_guest_addr: %" PRIx64 "\n",
+       VHOST_LOG_DEBUG(VHOST_CONFIG, "(%d) log_guest_addr: %" PRIx64 "\n",
                        dev->vid, vq->log_guest_addr);
 
        return dev;
@@ -513,7 +569,7 @@ vhost_user_set_vring_addr(struct virtio_net **pdev, VhostUserMsg *msg)
 
        if (vq->enabled && (dev->features &
                                (1ULL << VHOST_USER_F_PROTOCOL_FEATURES))) {
-               dev = translate_ring_addresses(dev, msg->payload.state.index);
+               dev = translate_ring_addresses(dev, msg->payload.addr.index);
                if (!dev)
                        return -1;
 
@@ -538,7 +594,7 @@ vhost_user_set_vring_base(struct virtio_net *dev,
        return 0;
 }
 
-static void
+static int
 add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
                   uint64_t host_phys_addr, uint64_t size)
 {
@@ -548,6 +604,10 @@ add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
                dev->max_guest_pages *= 2;
                dev->guest_pages = realloc(dev->guest_pages,
                                        dev->max_guest_pages * sizeof(*page));
+               if (!dev->guest_pages) {
+                       RTE_LOG(ERR, VHOST_CONFIG, "cannot realloc guest_pages\n");
+                       return -1;
+               }
        }
 
        if (dev->nr_guest_pages > 0) {
@@ -556,7 +616,7 @@ add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
                if (host_phys_addr == last_page->host_phys_addr +
                                      last_page->size) {
                        last_page->size += size;
-                       return;
+                       return 0;
                }
        }
 
@@ -564,9 +624,11 @@ add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
        page->guest_phys_addr = guest_phys_addr;
        page->host_phys_addr  = host_phys_addr;
        page->size = size;
+
+       return 0;
 }
 
-static void
+static int
 add_guest_pages(struct virtio_net *dev, struct rte_vhost_mem_region *reg,
                uint64_t page_size)
 {
@@ -580,7 +642,9 @@ add_guest_pages(struct virtio_net *dev, struct rte_vhost_mem_region *reg,
        size = page_size - (guest_phys_addr & (page_size - 1));
        size = RTE_MIN(size, reg_size);
 
-       add_one_guest_page(dev, guest_phys_addr, host_phys_addr, size);
+       if (add_one_guest_page(dev, guest_phys_addr, host_phys_addr, size) < 0)
+               return -1;
+
        host_user_addr  += size;
        guest_phys_addr += size;
        reg_size -= size;
@@ -589,12 +653,16 @@ add_guest_pages(struct virtio_net *dev, struct rte_vhost_mem_region *reg,
                size = RTE_MIN(reg_size, page_size);
                host_phys_addr = rte_mem_virt2iova((void *)(uintptr_t)
                                                  host_user_addr);
-               add_one_guest_page(dev, guest_phys_addr, host_phys_addr, size);
+               if (add_one_guest_page(dev, guest_phys_addr, host_phys_addr,
+                               size) < 0)
+                       return -1;
 
                host_user_addr  += size;
                guest_phys_addr += size;
                reg_size -= size;
        }
+
+       return 0;
 }
 
 #ifdef RTE_LIBRTE_VHOST_DEBUG
@@ -648,8 +716,9 @@ vhost_memory_changed(struct VhostUserMemory *new,
 }
 
 static int
-vhost_user_set_mem_table(struct virtio_net *dev, struct VhostUserMsg *pmsg)
+vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *pmsg)
 {
+       struct virtio_net *dev = *pdev;
        struct VhostUserMemory memory = pmsg->payload.memory;
        struct rte_vhost_mem_region *reg;
        void *mmap_addr;
@@ -657,8 +726,15 @@ vhost_user_set_mem_table(struct virtio_net *dev, struct VhostUserMsg *pmsg)
        uint64_t mmap_offset;
        uint64_t alignment;
        uint32_t i;
+       int populate;
        int fd;
 
+       if (memory.nregions > VHOST_MEMORY_MAX_NREGIONS) {
+               RTE_LOG(ERR, VHOST_CONFIG,
+                       "too many memory regions (%u)\n", memory.nregions);
+               return -1;
+       }
+
        if (dev->mem && !vhost_memory_changed(&memory, dev->mem)) {
                RTE_LOG(INFO, VHOST_CONFIG,
                        "(%d) memory regions not changed\n", dev->vid);
@@ -709,7 +785,17 @@ vhost_user_set_mem_table(struct virtio_net *dev, struct VhostUserMsg *pmsg)
                reg->fd              = fd;
 
                mmap_offset = memory.regions[i].mmap_offset;
-               mmap_size   = reg->size + mmap_offset;
+
+               /* Check for memory_size + mmap_offset overflow */
+               if (mmap_offset >= -reg->size) {
+                       RTE_LOG(ERR, VHOST_CONFIG,
+                               "mmap_offset (%#"PRIx64") and memory_size "
+                               "(%#"PRIx64") overflow\n",
+                               mmap_offset, reg->size);
+                       goto err_mmap;
+               }
+
+               mmap_size = reg->size + mmap_offset;
 
                /* mmap() without flag of MAP_ANONYMOUS, should be called
                 * with length argument aligned with hugepagesz at older
@@ -727,8 +813,9 @@ vhost_user_set_mem_table(struct virtio_net *dev, struct VhostUserMsg *pmsg)
                }
                mmap_size = RTE_ALIGN_CEIL(mmap_size, alignment);
 
+               populate = (dev->dequeue_zero_copy) ? MAP_POPULATE : 0;
                mmap_addr = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE,
-                                MAP_SHARED | MAP_POPULATE, fd, 0);
+                                MAP_SHARED | populate, fd, 0);
 
                if (mmap_addr == MAP_FAILED) {
                        RTE_LOG(ERR, VHOST_CONFIG,
@@ -742,7 +829,12 @@ vhost_user_set_mem_table(struct virtio_net *dev, struct VhostUserMsg *pmsg)
                                      mmap_offset;
 
                if (dev->dequeue_zero_copy)
-                       add_guest_pages(dev, reg, alignment);
+                       if (add_guest_pages(dev, reg, alignment) < 0) {
+                               RTE_LOG(ERR, VHOST_CONFIG,
+                                       "adding guest pages to region %u failed.\n",
+                                       i);
+                               goto err_mmap;
+                       }
 
                RTE_LOG(INFO, VHOST_CONFIG,
                        "guest memory region %u, size: 0x%" PRIx64 "\n"
@@ -763,6 +855,25 @@ vhost_user_set_mem_table(struct virtio_net *dev, struct VhostUserMsg *pmsg)
                        mmap_offset);
        }
 
+       for (i = 0; i < dev->nr_vring; i++) {
+               struct vhost_virtqueue *vq = dev->virtqueue[i];
+
+               if (vq->desc || vq->avail || vq->used) {
+                       /*
+                        * If the memory table got updated, the ring addresses
+                        * need to be translated again as virtual addresses have
+                        * changed.
+                        */
+                       vring_invalidate(dev, vq);
+
+                       dev = translate_ring_addresses(dev, i);
+                       if (!dev)
+                               return -1;
+
+                       *pdev = dev;
+               }
+       }
+
        dump_guest_pages(dev);
 
        return 0;
@@ -885,17 +996,24 @@ vhost_user_get_vring_base(struct virtio_net *dev,
                          VhostUserMsg *msg)
 {
        struct vhost_virtqueue *vq = dev->virtqueue[msg->payload.state.index];
+       struct rte_vdpa_device *vdpa_dev;
+       int did = -1;
 
        /* We have to stop the queue (virtio) if it is running. */
        if (dev->flags & VIRTIO_DEV_RUNNING) {
+               did = dev->vdpa_dev_id;
+               vdpa_dev = rte_vdpa_get_device(did);
+               if (vdpa_dev && vdpa_dev->ops->dev_close)
+                       vdpa_dev->ops->dev_close(dev->vid);
                dev->flags &= ~VIRTIO_DEV_RUNNING;
                dev->notify_ops->destroy_device(dev->vid);
        }
 
        dev->flags &= ~VIRTIO_DEV_READY;
+       dev->flags &= ~VIRTIO_DEV_VDPA_CONFIGURED;
 
-       /* Here we are safe to get the last used index */
-       msg->payload.state.num = vq->last_used_idx;
+       /* Here we are safe to get the last avail index */
+       msg->payload.state.num = vq->last_avail_idx;
 
        RTE_LOG(INFO, VHOST_CONFIG,
                "vring base idx:%d file:%d\n", msg->payload.state.index,
@@ -910,6 +1028,11 @@ vhost_user_get_vring_base(struct virtio_net *dev,
 
        vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
 
+       if (vq->callfd >= 0)
+               close(vq->callfd);
+
+       vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
+
        if (dev->dequeue_zero_copy)
                free_zmbufs(vq);
        rte_free(vq->shadow_used_ring);
@@ -930,16 +1053,24 @@ vhost_user_set_vring_enable(struct virtio_net *dev,
                            VhostUserMsg *msg)
 {
        int enable = (int)msg->payload.state.num;
+       int index = (int)msg->payload.state.index;
+       struct rte_vdpa_device *vdpa_dev;
+       int did = -1;
 
        RTE_LOG(INFO, VHOST_CONFIG,
                "set queue enable: %d to qp idx: %d\n",
-               enable, msg->payload.state.index);
+               enable, index);
+
+       did = dev->vdpa_dev_id;
+       vdpa_dev = rte_vdpa_get_device(did);
+       if (vdpa_dev && vdpa_dev->ops->set_vring_state)
+               vdpa_dev->ops->set_vring_state(dev->vid, index, enable);
 
        if (dev->notify_ops->vring_state_changed)
                dev->notify_ops->vring_state_changed(dev->vid,
-                               msg->payload.state.index, enable);
+                               index, enable);
 
-       dev->virtqueue[msg->payload.state.index]->enabled = enable;
+       dev->virtqueue[index]->enabled = enable;
 
        return 0;
 }
@@ -948,9 +1079,10 @@ static void
 vhost_user_get_protocol_features(struct virtio_net *dev,
                                 struct VhostUserMsg *msg)
 {
-       uint64_t features, protocol_features = VHOST_USER_PROTOCOL_FEATURES;
+       uint64_t features, protocol_features;
 
        rte_vhost_driver_get_features(dev->ifname, &features);
+       rte_vhost_driver_get_protocol_features(dev->ifname, &protocol_features);
 
        /*
         * REPLY_ACK protocol feature is only mandatory for now
@@ -996,6 +1128,15 @@ vhost_user_set_log_base(struct virtio_net *dev, struct VhostUserMsg *msg)
 
        size = msg->payload.log.mmap_size;
        off  = msg->payload.log.mmap_offset;
+
+       /* Don't allow mmap_offset to point outside the mmap region */
+       if (off > size) {
+               RTE_LOG(ERR, VHOST_CONFIG,
+                       "log offset %#"PRIx64" exceeds log size %#"PRIx64"\n",
+                       off, size);
+               return -1;
+       }
+
        RTE_LOG(INFO, VHOST_CONFIG,
                "log mmap size: %"PRId64", offset: %"PRId64"\n",
                size, off);
@@ -1004,7 +1145,7 @@ vhost_user_set_log_base(struct virtio_net *dev, struct VhostUserMsg *msg)
         * mmap from 0 to workaround a hugepage mmap bug: mmap will
         * fail when offset is not page size aligned.
         */
-       addr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+       addr = mmap(0, size + off, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
        close(fd);
        if (addr == MAP_FAILED) {
                RTE_LOG(ERR, VHOST_CONFIG, "mmap log base failed!\n");
@@ -1037,6 +1178,8 @@ static int
 vhost_user_send_rarp(struct virtio_net *dev, struct VhostUserMsg *msg)
 {
        uint8_t *mac = (uint8_t *)&msg->payload.u64;
+       struct rte_vdpa_device *vdpa_dev;
+       int did = -1;
 
        RTE_LOG(DEBUG, VHOST_CONFIG,
                ":: mac: %02x:%02x:%02x:%02x:%02x:%02x\n",
@@ -1052,6 +1195,10 @@ vhost_user_send_rarp(struct virtio_net *dev, struct VhostUserMsg *msg)
         */
        rte_smp_wmb();
        rte_atomic16_set(&dev->broadcast_rarp, 1);
+       did = dev->vdpa_dev_id;
+       vdpa_dev = rte_vdpa_get_device(did);
+       if (vdpa_dev && vdpa_dev->ops->migration_done)
+               vdpa_dev->ops->migration_done(dev->vid);
 
        return 0;
 }
@@ -1214,13 +1361,13 @@ read_vhost_message(int sockfd, struct VhostUserMsg *msg)
 }
 
 static int
-send_vhost_message(int sockfd, struct VhostUserMsg *msg)
+send_vhost_message(int sockfd, struct VhostUserMsg *msg, int *fds, int fd_num)
 {
        if (!msg)
                return 0;
 
        return send_fd_message(sockfd, (char *)msg,
-               VHOST_USER_HDR_SIZE + msg->size, NULL, 0);
+               VHOST_USER_HDR_SIZE + msg->size, fds, fd_num);
 }
 
 static int
@@ -1234,7 +1381,7 @@ send_vhost_reply(int sockfd, struct VhostUserMsg *msg)
        msg->flags |= VHOST_USER_VERSION;
        msg->flags |= VHOST_USER_REPLY_MASK;
 
-       return send_vhost_message(sockfd, msg);
+       return send_vhost_message(sockfd, msg, NULL, 0);
 }
 
 /*
@@ -1314,8 +1461,11 @@ vhost_user_msg_handler(int vid, int fd)
 {
        struct virtio_net *dev;
        struct VhostUserMsg msg;
+       struct rte_vdpa_device *vdpa_dev;
+       int did = -1;
        int ret;
        int unlock_required = 0;
+       uint32_t skip_master = 0;
 
        dev = get_device(vid);
        if (dev == NULL)
@@ -1393,6 +1543,21 @@ vhost_user_msg_handler(int vid, int fd)
 
        }
 
+       if (dev->extern_ops.pre_msg_handle) {
+               uint32_t need_reply;
+
+               ret = (*dev->extern_ops.pre_msg_handle)(dev->vid,
+                               (void *)&msg, &need_reply, &skip_master);
+               if (ret < 0)
+                       goto skip_to_reply;
+
+               if (need_reply)
+                       send_vhost_reply(fd, &msg);
+
+               if (skip_master)
+                       goto skip_to_post_handle;
+       }
+
        switch (msg.request.master) {
        case VHOST_USER_GET_FEATURES:
                msg.payload.u64 = vhost_user_get_features(dev);
@@ -1421,7 +1586,7 @@ vhost_user_msg_handler(int vid, int fd)
                break;
 
        case VHOST_USER_SET_MEM_TABLE:
-               ret = vhost_user_set_mem_table(dev, &msg);
+               ret = vhost_user_set_mem_table(&dev, &msg);
                break;
 
        case VHOST_USER_SET_LOG_BASE:
@@ -1466,7 +1631,7 @@ vhost_user_msg_handler(int vid, int fd)
                break;
 
        case VHOST_USER_GET_QUEUE_NUM:
-               msg.payload.u64 = VHOST_MAX_QUEUE_PAIRS;
+               msg.payload.u64 = (uint64_t)vhost_user_get_queue_num(dev);
                msg.size = sizeof(msg.payload.u64);
                send_vhost_reply(fd, &msg);
                break;
@@ -1493,9 +1658,22 @@ vhost_user_msg_handler(int vid, int fd)
        default:
                ret = -1;
                break;
+       }
+
+skip_to_post_handle:
+       if (dev->extern_ops.post_msg_handle) {
+               uint32_t need_reply;
 
+               ret = (*dev->extern_ops.post_msg_handle)(
+                               dev->vid, (void *)&msg, &need_reply);
+               if (ret < 0)
+                       goto skip_to_reply;
+
+               if (need_reply)
+                       send_vhost_reply(fd, &msg);
        }
 
+skip_to_reply:
        if (unlock_required)
                vhost_user_unlock_all_queue_pairs(dev);
 
@@ -1519,6 +1697,16 @@ vhost_user_msg_handler(int vid, int fd)
                }
        }
 
+       did = dev->vdpa_dev_id;
+       vdpa_dev = rte_vdpa_get_device(did);
+       if (vdpa_dev && virtio_is_ready(dev) &&
+                       !(dev->flags & VIRTIO_DEV_VDPA_CONFIGURED) &&
+                       msg.request.master == VHOST_USER_SET_VRING_ENABLE) {
+               if (vdpa_dev->ops->dev_conf)
+                       vdpa_dev->ops->dev_conf(dev->vid);
+               dev->flags |= VIRTIO_DEV_VDPA_CONFIGURED;
+       }
+
        return 0;
 }
 
@@ -1537,7 +1725,7 @@ vhost_user_iotlb_miss(struct virtio_net *dev, uint64_t iova, uint8_t perm)
                },
        };
 
-       ret = send_vhost_message(dev->slave_req_fd, &msg);
+       ret = send_vhost_message(dev->slave_req_fd, &msg, NULL, 0);
        if (ret < 0) {
                RTE_LOG(ERR, VHOST_CONFIG,
                                "Failed to send IOTLB miss message (%d)\n",