Fix vhost security issues
cover Fix vhost security issues
From: Maxime Coquelin <[email protected]>
Marvin Liu (1):
vhost: fix translated address not checked
Maxime Coquelin (3):
vhost: check log mmap offset and size overflow
vhost: fix vring index check
vhost/crypto: validate keys lengths
Xiaolong Ye (1):
vhost: fix potential memory space leak
Xuan Ding (1):
vhost: fix potential fd leak
lib/librte_vhost/vhost_crypto.c | 17 +++++++++++++++++ lib/librte_vhost/vhost_user.c | 30 +++++++++++++++++++++++------- lib/librte_vhost/virtio_net.c | 4 ++++ 3 files changed, 44 insertions(+), 7 deletions(-)
Signature · 1 line
2.25.2
PATCH 1/6 vhost: check log mmap offset and size overflow
From: Maxime Coquelin <[email protected]>
vhost_user_set_log_base() is a message handler that is called to handle the VHOST_USER_SET_LOG_BASE message. Its payload contains a 64 bit size and offset. Both are added up and used as a size when calling mmap().
There is no integer overflow check. If an integer overflow occurs a smaller memory map would be created than requested. Since the returned mapping is mapped as writable and used for logging, a memory corruption could occur.
CVE-2020-10722
--- lib/librte_vhost/vhost_user.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
lib/librte_vhost/vhost_user.c +3 -3
@@ -2059,10 +2059,10 @@ vhost_user_set_log_base(struct virtio_net **pdev, 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) { +/* Check for mmap size and offset overflow. */ +if (off >= -size) {VHOST_LOG_CONFIG (ERR , -"log offset %#" PRIx64" exceeds log size %#" PRIx64"\n" , +"log offset %#" PRIx64" and log size %#" PRIx64" overflow\n" , off, size);return RTE_VHOST_MSG_RESULT_ERR ; }
Signature · 1 line
2.25.2
PATCH 2/6 vhost: fix vring index check
In reply to Ferruh YigitFrom: Maxime Coquelin <[email protected]>
vhost_user_check_and_alloc_queue_pair() is used to extract a vring index from a payload. This function validates the index and is called early on in when performing message handling. Most message handlers depend on it correctly validating the vring index.
Depending on the message type the vring index is in different parts of the payload. The function contains a switch/case for each type and copies the index. This is stored in a uint16. This index is then validated. Depending on the message, the source index is an unsigned int. If integer truncation occurs (uint->uint16) the top 16 bits of the index are never validated.
When they are used later on (e.g. in vhost_user_set_vring_num() or vhost_user_set_vring_addr()) it can lead to out of bound indexing. The out of bound indexed data gets written to, and hence this can cause memory corruption.
This patch fixes this vulnerability by declaring vring index as an unsigned int in vhost_user_check_and_alloc_queue_pair().
CVE-2020-10723
--- lib/librte_vhost/vhost_user.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/librte_vhost/vhost_user.c +1 -1
@@ -2526,7 +2526,7 @@ static int vhost_user_check_and_alloc_queue_pair(struct virtio_net *dev, struct VhostUserMsg *msg) { - uint16_t vring_idx; + uint32_t vring_idx;switch (msg->request.master) {case VHOST_USER_SET_VRING_KICK :
Signature · 1 line
2.25.2
PATCH 3/6 vhost/crypto: validate keys lengths
In reply to Ferruh YigitFrom: Maxime Coquelin <[email protected]>
transform_cipher_param() and transform_chain_param() handle the payload data for the VHOST_USER_CRYPTO_CREATE_SESS message. These payloads have to be validated, since it could come from untrusted sources.
Two buffers and their lenghts are defined in this payload, one the the auth key and one for the cipher key. But above functions do not validate the key length inputs, which could lead to read out of bounds, as buffers have static sizes of 64 bytes for the cipher key and 512 bytes for the auth key.
This patch adds necessary checks on the key length field before being used.
CVE-2020-10724
--- lib/librte_vhost/vhost_crypto.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
lib/librte_vhost/vhost_crypto.c +17 -0
@@ -238,6 +238,11 @@ transform_cipher_param(struct rte_crypto_sym_xform *xform,if (unlikely (ret <0 ))return ret; +if (param->cipher_key_len >VHOST_USER_CRYPTO_MAX_CIPHER_KEY_LENGTH ) { +VC_LOG_DBG ("Invalid cipher key length\n" ); +return -VIRTIO_CRYPTO_BADMSG ; + } + xform->type =RTE_CRYPTO_SYM_XFORM_CIPHER ; xform->cipher.key.length = param->cipher_key_len;if (xform->cipher.key.length >0 )
@@ -288,6 +293,12 @@ transform_chain_param(struct rte_crypto_sym_xform *xforms, &xform_cipher->cipher.algo);if (unlikely (ret <0 ))return ret; + +if (param->cipher_key_len >VHOST_USER_CRYPTO_MAX_CIPHER_KEY_LENGTH ) { +VC_LOG_DBG ("Invalid cipher key length\n" ); +return -VIRTIO_CRYPTO_BADMSG ; + } + xform_cipher->type =RTE_CRYPTO_SYM_XFORM_CIPHER ; xform_cipher->cipher.key.length = param->cipher_key_len; xform_cipher->cipher.key.data = param->cipher_key_buf;
@@ -302,6 +313,12 @@ transform_chain_param(struct rte_crypto_sym_xform *xforms, ret =auth_algo_transform (param->hash_algo, &xform_auth->auth.algo);if (unlikely (ret <0 ))return ret; + +if (param->auth_key_len >VHOST_USER_CRYPTO_MAX_HMAC_KEY_LENGTH ) { +VC_LOG_DBG ("Invalid auth key length\n" ); +return -VIRTIO_CRYPTO_BADMSG ; + } + xform_auth->auth.digest_length = param->digest_len; xform_auth->auth.key.length = param->auth_key_len; xform_auth->auth.key.data = param->auth_key_buf;
Signature · 1 line
2.25.2
PATCH 4/6 vhost: fix translated address not checked
In reply to Ferruh YigitFrom: Marvin Liu <[email protected]>
Malicious guest can construct desc with invalid address and zero buffer length. That will request vhost to check both translated address and translated data length. This patch will add missed address check.
CVE-2020-10725
--- lib/librte_vhost/virtio_net.c | 4 ++++ 1 file changed, 4 insertions(+)
lib/librte_vhost/virtio_net.c +4 -0
@@ -1072,6 +1072,8 @@ virtio_dev_rx_batch_packed(struct virtio_net *dev, VHOST_ACCESS_RW);vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +if (unlikely (!desc_addrs[i])) +return -1 ;if (unlikely (lens[i] != descs[avail_idx + i].len))return -1 ; }
@@ -1827,6 +1829,8 @@ vhost_reserve_avail_batch_packed(struct virtio_net *dev, }vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +if (unlikely (!desc_addrs[i])) +return -1 ;if (unlikely ((lens[i] != descs[avail_idx + i].len)))return -1 ; }
Signature · 1 line
2.25.2
PATCH 5/6 vhost: fix potential memory space leak
In reply to Ferruh YigitFrom: Xiaolong Ye <[email protected]>
A malicious container which has direct access to the vhost-user socket can keep sending VHOST_USER_GET_INFLIGHT_FD messages which may cause leaking resources until resulting a DOS. Fix it by unmapping the dev->inflight_info->addr before assigning new mapped addr to it.
CVE-2020-10726
--- lib/librte_vhost/vhost_user.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
lib/librte_vhost/vhost_user.c +8 -1
@@ -1433,6 +1433,11 @@ vhost_user_get_inflight_fd(struct virtio_net **pdev, }memset (addr,0 , mmap_size); +if (dev->inflight_info->addr) { +munmap (dev->inflight_info->addr, dev->inflight_info->size); + dev->inflight_info->addr =NULL ; + } + dev->inflight_info->addr = addr; dev->inflight_info->size = msg->payload.inflight.mmap_size = mmap_size; dev->inflight_info->fd = msg->fds[0 ] = fd;
@@ -1517,8 +1522,10 @@ vhost_user_set_inflight_fd(struct virtio_net **pdev, VhostUserMsg *msg, } } -if (dev->inflight_info->addr) +if (dev->inflight_info->addr) {munmap (dev->inflight_info->addr, dev->inflight_info->size); + dev->inflight_info->addr =NULL ; + } addr =mmap (0 , mmap_size,PROT_READ |PROT_WRITE ,MAP_SHARED , fd, mmap_offset);
Signature · 1 line
2.25.2
PATCH 6/6 vhost: fix potential fd leak
In reply to Ferruh YigitFrom: Xuan Ding <[email protected]>
Vhost will create temporary file when receiving VHOST_USER_GET_INFLIGHT_FD message. Malicious guest can send endless this message to drain out the resource of host.
When receiving VHOST_USER_GET_INFLIGHT_FD message repeatedly, closing the file created during the last handling of this message.
CVE-2020-10726
--- lib/librte_vhost/vhost_user.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
lib/librte_vhost/vhost_user.c +11 -2
@@ -206,7 +206,7 @@ vhost_backend_cleanup(struct virtio_net *dev) dev->inflight_info->addr =NULL ; } -if (dev->inflight_info->fd >0 ) { +if (dev->inflight_info->fd >=0 ) {close (dev->inflight_info->fd); dev->inflight_info->fd =-1 ; }
@@ -1408,6 +1408,7 @@ vhost_user_get_inflight_fd(struct virtio_net **pdev, "failed to alloc dev inflight area\n");return RTE_VHOST_MSG_RESULT_ERR ; } + dev->inflight_info->fd =-1 ; } num_queues = msg->payload.inflight.num_queues;
@@ -1438,6 +1439,11 @@ vhost_user_get_inflight_fd(struct virtio_net **pdev, dev->inflight_info->addr =NULL ; } +if (dev->inflight_info->fd >=0 ) { +close (dev->inflight_info->fd); + dev->inflight_info->fd =-1 ; + } + dev->inflight_info->addr = addr; dev->inflight_info->size = msg->payload.inflight.mmap_size = mmap_size; dev->inflight_info->fd = msg->fds[0 ] = fd;
@@ -1520,6 +1526,7 @@ vhost_user_set_inflight_fd(struct virtio_net **pdev, VhostUserMsg *msg, "failed to alloc dev inflight area\n");return RTE_VHOST_MSG_RESULT_ERR ; } + dev->inflight_info->fd =-1 ; }if (dev->inflight_info->addr) {
@@ -1534,8 +1541,10 @@ vhost_user_set_inflight_fd(struct virtio_net **pdev, VhostUserMsg *msg,return RTE_VHOST_MSG_RESULT_ERR ; } -if (dev->inflight_info->fd) +if (dev->inflight_info->fd >=0 ) {close (dev->inflight_info->fd); + dev->inflight_info->fd =-1 ; + } dev->inflight_info->fd = fd; dev->inflight_info->addr = addr;
Signature · 1 line
2.25.2
Quoted 14 lines · Ferruh Yigit
From: Maxime Coquelin <[email protected]>
Marvin Liu (1):
vhost: fix translated address not checkedMaxime Coquelin (3):
vhost: check log mmap offset and size overflow
vhost: fix vring index check
vhost/crypto: validate keys lengthsXiaolong Ye (1):
vhost: fix potential memory space leak
Quoted 8 lines
Xuan Ding (1):
vhost: fix potential fd leaklib/librte_vhost/vhost_crypto.c | 17 +++++++++++++++++ lib/librte_vhost/vhost_user.c | 30 +++++++++++++++++++++++------- lib/librte_vhost/virtio_net.c | 4 ++++ 3 files changed, 44 insertions(+), 7 deletions(-)
Series applied, thanks.
Signature · 1 line
David Marchand