Skip to contentSkip to conversation

vhost: fix potential buffer overflow

PATCH In review Marvin Liu 2 participants 6 messages Feb 26, 2021 – Mar 25, 2021

lore
Share & tools
lore

Marvin Liu

author
#0 lore raw

PATCH 1/1 vhost: fix potential buffer overflow

In vhost datapath, descriptor's length are mostly used in two coherent operations. First step is used for address translation, second step is used for memory transaction from guest to host. But the iterval between two steps will give a window for malicious guest, in which can change descriptor length after vhost calcuated buffer size. Thus may lead to buffer overflow in vhost side. This potential risk can be eliminated by accessing the descriptor length once.

Fixes 1be4ebb1c464 ("vhost: support indirect descriptor in mergeable Rx")
Fixes 2f3225a7d69b ("vhost: add vector filling support for packed ring")
Fixes 75ed51697820 ("vhost: add packed ring batch dequeue")
lib/librte_vhost/virtio_net.c +11 -7
index 583bf379c6..0a7d008a91 100644
@@ -548,10 +548,11 @@ fill_vec_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
 			return -1;
 		}
 
-		len += descs[idx].len;
+		dlen = descs[idx].len;
+		len += dlen;
 
 		if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
-						descs[idx].addr, descs[idx].len,
+						descs[idx].addr, dlen,
 						perm))) {
 			free_ind_table(idesc);
 			return -1;
@@ -668,9 +669,10 @@ fill_vec_buf_packed_indirect(struct virtio_net *dev,
 			return -1;
 		}
 
-		*len += descs[i].len;
+		dlen = descs[i].len;
+		*len += dlen;
 		if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
-						descs[i].addr, descs[i].len,
+						descs[i].addr, dlen,
 						perm)))
 			return -1;
 	}
@@ -691,6 +693,7 @@ fill_vec_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
 	bool wrap_counter = vq->avail_wrap_counter;
 	struct vring_packed_desc *descs = vq->desc_packed;
 	uint16_t vec_id = *vec_idx;
+	uint64_t dlen;
 
 	if (avail_idx < vq->last_avail_idx)
 		wrap_counter ^= 1;
@@ -723,11 +726,12 @@ fill_vec_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
 							len, perm) < 0))
 				return -1;
 		} else {
-			*len += descs[avail_idx].len;
+			dlen = descs[avail_idx].len;
+			*len += dlen;
 
 			if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
 							descs[avail_idx].addr,
-							descs[avail_idx].len,
+							dlen,
 							perm)))
 				return -1;
 		}
@@ -2314,7 +2318,7 @@ vhost_reserve_avail_batch_packed(struct virtio_net *dev,
 	}
 
 	vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
-		pkts[i]->pkt_len = descs[avail_idx + i].len - buf_offset;
+		pkts[i]->pkt_len = lens[i] - buf_offset;
 		pkts[i]->data_len = pkts[i]->pkt_len;
 		ids[i] = descs[avail_idx + i].id;
 	}
Signature · 1 line
2.17.1
Reviewed-by on v1 1/1
#1 lore raw

Hi Marvin,

Quoted 11 lines · Marvin Liu

In vhost datapath, descriptor's length are mostly used in two coherent operations. First step is used for address translation, second step is used for memory transaction from guest to host. But the iterval between two steps will give a window for malicious guest, in which can change descriptor length after vhost calcuated buffer size. Thus may lead to buffer overflow in vhost side. This potential risk can be eliminated by accessing the descriptor length once.

Fixes 1be4ebb1c464 ("vhost: support indirect descriptor in mergeable Rx")
Fixes 2f3225a7d69b ("vhost: add vector filling support for packed ring")
Fixes 75ed51697820 ("vhost: add packed ring batch dequeue")

As the offending commits have been introduced in different LTS, I would prefer the patch to be split. It will make is easier for backporting later.

Quoted 59 lines
lib/librte_vhost/virtio_net.c +10 -6
index 583bf379c6..0a7d008a91 100644
@@ -548,10 +548,11 @@ fill_vec_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
 			return -1;
 		}
 
-		len += descs[idx].len;
+		dlen = descs[idx].len;
+		len += dlen;
 
 		if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
-						descs[idx].addr, descs[idx].len,
+						descs[idx].addr, dlen,
 						perm))) {
 			free_ind_table(idesc);
 			return -1;
@@ -668,9 +669,10 @@ fill_vec_buf_packed_indirect(struct virtio_net *dev,
 			return -1;
 		}
 
-		*len += descs[i].len;
+		dlen = descs[i].len;
+		*len += dlen;
 		if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
-						descs[i].addr, descs[i].len,
+						descs[i].addr, dlen,
 						perm)))
 			return -1;
 	}
@@ -691,6 +693,7 @@ fill_vec_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
 	bool wrap_counter = vq->avail_wrap_counter;
 	struct vring_packed_desc *descs = vq->desc_packed;
 	uint16_t vec_id = *vec_idx;
+	uint64_t dlen;
 
 	if (avail_idx < vq->last_avail_idx)
 		wrap_counter ^= 1;
@@ -723,11 +726,12 @@ fill_vec_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
 							len, perm) < 0))
 				return -1;
 		} else {
-			*len += descs[avail_idx].len;
+			dlen = descs[avail_idx].len;
+			*len += dlen;
 
 			if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
 							descs[avail_idx].addr,
-							descs[avail_idx].len,
+							dlen,
 							perm)))
 				return -1;
 		}

lib/librte_vhost/virtio_net.c vhost_reserve_avail_batch_packed(struct virtio_net *dev, · line 2324

Quoted 8 lines
lib/librte_vhost/virtio_net.c +1 -1
@@ -2314,7 +2318,7 @@ vhost_reserve_avail_batch_packed(struct virtio_net *dev,
 	}
 
 	vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
-		pkts[i]->pkt_len = descs[avail_idx + i].len - buf_offset;
+		pkts[i]->pkt_len = lens[i] - buf_offset;
 		pkts[i]->data_len = pkts[i]->pkt_len;
 		ids[i] = descs[avail_idx + i].id;
 	}

Other than that, the patch looks valid to me.
With the split done:

Thanks,
Maxime

author
#2 lore raw
Quoted 20 lines

-----Original Message-----
From: Maxime Coquelin <[email protected]>
Sent: Wednesday, March 24, 2021 4:56 PM
To: Liu, Yong <[email protected]>; Xia, Chenbo <[email protected]>
Cc: [email protected]; [email protected]
Subject: Re: [PATCH] vhost: fix potential buffer overflow

Hi Marvin,

Quoted 8 lines · Marvin Liu

In vhost datapath, descriptor's length are mostly used in two coherent operations. First step is used for address translation, second step is used for memory transaction from guest to host. But the iterval between two steps will give a window for malicious guest, in which can change descriptor length after vhost calcuated buffer size. Thus may lead to buffer overflow in vhost side. This potential risk can be eliminated by accessing the descriptor length once.

Quoted 7 lines
Quoted 3 lines
Fixes 1be4ebb1c464 ("vhost: support indirect descriptor in mergeable Rx")
Fixes 2f3225a7d69b ("vhost: add vector filling support for packed ring")
Fixes 75ed51697820 ("vhost: add packed ring batch dequeue")

As the offending commits have been introduced in different LTS, I would prefer the patch to be split. It will make is easier for backporting later.

Maxime,
Thanks for your suggestion, I will split this patch into three parts as they were spread over three different LTS.

Regards,
Marvin

Quoted 81 lines

lib/librte_vhost/virtio_net.c fill_vec_buf_split(struct virtio_net *dev, struct

Quoted 8 lines
lib/librte_vhost/virtio_net.c +0 -0
index 583bf379c6..0a7d008a91 100644
@@ -548,10 +548,11 @@ fill_vec_buf_split(struct virtio_net *dev, struct

vhost_virtqueue *vq,

lib/librte_vhost/virtio_net.c fill_vec_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,

Quoted 9 lines
lib/librte_vhost/virtio_net.c +2 -2
 			return -1;
 		}
 
-		len += descs[idx].len;
+		dlen = descs[idx].len;
+		len += dlen;
 
 		if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
-						descs[idx].addr,

descs[idx].len,

lib/librte_vhost/virtio_net.c fill_vec_buf_packed_indirect(struct virtio_net

Quoted 5 lines
lib/librte_vhost/virtio_net.c +1 -0
+						descs[idx].addr, dlen,
 						perm))) {
 			free_ind_table(idesc);
 			return -1;
@@ -668,9 +669,10 @@ fill_vec_buf_packed_indirect(struct virtio_net

*dev,

lib/librte_vhost/virtio_net.c fill_vec_buf_packed(struct virtio_net *dev, struct

Quoted 13 lines
lib/librte_vhost/virtio_net.c +3 -2
 			return -1;
 		}
 
-		*len += descs[i].len;
+		dlen = descs[i].len;
+		*len += dlen;
 		if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
-						descs[i].addr, descs[i].len,
+						descs[i].addr, dlen,
 						perm)))
 			return -1;
 	}
@@ -691,6 +693,7 @@ fill_vec_buf_packed(struct virtio_net *dev, struct

vhost_virtqueue *vq,

lib/librte_vhost/virtio_net.c fill_vec_buf_packed(struct virtio_net *dev, struct

Quoted 8 lines
lib/librte_vhost/virtio_net.c +1 -0
 	bool wrap_counter = vq->avail_wrap_counter;
 	struct vring_packed_desc *descs = vq->desc_packed;
 	uint16_t vec_id = *vec_idx;
+	uint64_t dlen;
 
 	if (avail_idx < vq->last_avail_idx)
 		wrap_counter ^= 1;
@@ -723,11 +726,12 @@ fill_vec_buf_packed(struct virtio_net *dev, struct

vhost_virtqueue *vq,

lib/librte_vhost/virtio_net.c vhost_reserve_avail_batch_packed(struct

Quoted 15 lines
lib/librte_vhost/virtio_net.c +3 -2
 							len, perm) < 0))
 				return -1;
 		} else {
-			*len += descs[avail_idx].len;
+			dlen = descs[avail_idx].len;
+			*len += dlen;
 
 			if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
 							descs[avail_idx].addr,
-							descs[avail_idx].len,
+							dlen,
 							perm)))
 				return -1;
 		}
@@ -2314,7 +2318,7 @@ vhost_reserve_avail_batch_packed(struct

virtio_net *dev,

lib/librte_vhost/virtio_net.c vhost_reserve_avail_batch_packed(struct virtio_net *dev, · line 2324

Quoted 9 lines
lib/librte_vhost/virtio_net.c +1 -1
 	}
 
 	vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
-		pkts[i]->pkt_len = descs[avail_idx + i].len - buf_offset;
+		pkts[i]->pkt_len = lens[i] - buf_offset;
 		pkts[i]->data_len = pkts[i]->pkt_len;
 		ids[i] = descs[avail_idx + i].id;
 	}

Other than that, the patch looks valid to me.
With the split done:

Thanks,
Maxime

author
#3 lore raw

PATCH 1/3 vhost: fix split ring potential buffer overflow

In reply to Marvin Liu

In vhost datapath, descriptor's length are mostly used in two coherent operations. First step is used for address translation, second step is used for memory transaction from guest to host. But the iterval between two steps will give a window for malicious guest, in which can change descriptor length after vhost calcuated buffer size. Thus may lead to buffer overflow in vhost side. This potential risk can be eliminated by accessing the descriptor length once.

Fixes 1be4ebb1c464 ("vhost: support indirect descriptor in mergeable Rx")
lib/librte_vhost/virtio_net.c +3 -2
index 583bf379c6..576a0a20c0 100644
@@ -548,10 +548,11 @@ fill_vec_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
 			return -1;
 		}
 
-		len += descs[idx].len;
+		dlen = descs[idx].len;
+		len += dlen;
 
 		if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
-						descs[idx].addr, descs[idx].len,
+						descs[idx].addr, dlen,
 						perm))) {
 			free_ind_table(idesc);
 			return -1;
Signature · 1 line
2.17.1
author
#4 lore raw

PATCH 2/3 vhost: fix packed ring potential buffer overflow

Similar as split ring, the multiple accesses of descriptor length will lead to potential risk. One-time access of descriptor length can eliminate this risk.

Fixes 2f3225a7d69b ("vhost: add vector filling support for packed ring")
lib/librte_vhost/virtio_net.c +7 -4
index 576a0a20c0..de43686522 100644
@@ -669,9 +669,10 @@ fill_vec_buf_packed_indirect(struct virtio_net *dev,
 			return -1;
 		}
 
-		*len += descs[i].len;
+		dlen = descs[i].len;
+		*len += dlen;
 		if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
-						descs[i].addr, descs[i].len,
+						descs[i].addr, dlen,
 						perm)))
 			return -1;
 	}
@@ -692,6 +693,7 @@ fill_vec_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
 	bool wrap_counter = vq->avail_wrap_counter;
 	struct vring_packed_desc *descs = vq->desc_packed;
 	uint16_t vec_id = *vec_idx;
+	uint64_t dlen;
 
 	if (avail_idx < vq->last_avail_idx)
 		wrap_counter ^= 1;
@@ -724,11 +726,12 @@ fill_vec_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
 							len, perm) < 0))
 				return -1;
 		} else {
-			*len += descs[avail_idx].len;
+			dlen = descs[avail_idx].len;
+			*len += dlen;
 
 			if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
 							descs[avail_idx].addr,
-							descs[avail_idx].len,
+							dlen,
 							perm)))
 				return -1;
 		}
Signature · 1 line
2.17.1
author
#5 lore raw

PATCH 3/3 vhost: fix potential buffer overflow when batch dequeue

In reply to Marvin Liu

Similar as single dequeue, the multiple accesses of descriptor length will lead to potential risk. One-time access of descriptor length can eliminate this risk.

Fixes 75ed51697820 ("vhost: add packed ring batch dequeue")
lib/librte_vhost/virtio_net.c +1 -1
index de43686522..0a7d008a91 100644
@@ -2318,7 +2318,7 @@ vhost_reserve_avail_batch_packed(struct virtio_net *dev,
 	}
 
 	vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
-		pkts[i]->pkt_len = descs[avail_idx + i].len - buf_offset;
+		pkts[i]->pkt_len = lens[i] - buf_offset;
 		pkts[i]->data_len = pkts[i]->pkt_len;
 		ids[i] = descs[avail_idx + i].id;
 	}
Signature · 1 line
2.17.1