Skip to contentSkip to conversation

examples/vhost: fix use-after-free on drain vhost

PATCH Superseded Wenwu Ma 3 participants 5 messages Sep 23, 2021 – Oct 21, 2021

lore
Share & tools
lore

Wenwu Ma

author
#0 lore raw

PATCH 1/1 examples/vhost: fix use-after-free on drain vhost

When a vdev is removed in destroy_device function, the corresponding vhost_txbuff[vdev->vid] will also be free, but the vhost_txbuff[vdev->vid] may still be used in the drain_vhost function, which will cause an error of heap-use-after-free. Therefore, before accessing vhost_txbuff[vdev->vid], we need to check whether the vdev has been removed, if so, let's skip this vdev.

---
 examples/vhost/main.c | 3 +++
 1 file changed, 3 insertions(+)
examples/vhost/main.c +3 -0
index d0bf1f31e3..1f6f7be8e3 100644
@@ -916,6 +916,9 @@ drain_vhost_table(void)
 	uint64_t cur_tsc;
 
 	TAILQ_FOREACH(vdev, &vhost_dev_list, global_vdev_entry) {
+		if (unlikely(vdev->remove == 1))
+			continue;
+
 		vhost_txq = vhost_txbuff[lcore_id * MAX_VHOST_DEVICE
 						+ vdev->vid];
 
Signature · 1 line
2.25.1
#1 lore raw

Hi Wenwu,

Quoted 9 lines

-----Original Message-----
From: Ma, WenwuX <[email protected]>
Sent: Friday, September 24, 2021 4:30 AM
To: [email protected]
Cc: [email protected]; Xia, Chenbo <[email protected]>; Hu, Jiayu <[email protected]>; Yang, YvonneX <[email protected]>; Ma, WenwuX <[email protected]>
Subject: [PATCH] examples/vhost: fix use-after-free on drain vhost

Quoted 3 lines

When a vdev is removed in destroy_device function, the corresponding vhost_txbuff[vdev->vid] will also be free, but the vhost_txbuff[vdev->vid] may still

Free -> freed

vhost_txbuff[vdev->vid] -> vhost TX buffer

Quoted 5 lines

be used in the drain_vhost function, which will cause an error of heap-use-after-free. Therefore, before accessing vhost_txbuff[vdev->vid], we need to check whether the vdev has been removed, if so, let's skip this vdev.

This is a bug fix. Please add fix and cc-stable tag.

Thanks,
Chenbo

Quoted 22 lines
---
 examples/vhost/main.c | 3 +++
 1 file changed, 3 insertions(+)
examples/vhost/main.c +3 -1
index d0bf1f31e3..1f6f7be8e3 100644
@@ -916,6 +916,9 @@ drain_vhost_table(void)
 	uint64_t cur_tsc;
 
 	TAILQ_FOREACH(vdev, &vhost_dev_list, global_vdev_entry) {
+		if (unlikely(vdev->remove == 1))
+			continue;
+
 		vhost_txq = vhost_txbuff[lcore_id * MAX_VHOST_DEVICE
 						+ vdev->vid];
 
--

2.25.1

author
#2 lore raw

PATCH v2 1/1 examples/vhost: fix use-after-free on drain vhost

In reply to Wenwu Ma

When a vdev is removed in destroy_device function, the corresponding vhost TX buffer will also be freed, but the vhost TX buffer may still be used in the drain_vhost function, which will cause an error of heap-use-after-free. Therefore, before accessing vhost TX buffer, we need to check whether the vdev has been removed, if so, let's skip this vdev.

Fixes a68ba8e0a6b6 ("examples/vhost: refactor vhost data path")
---
 examples/vhost/main.c | 3 +++
 1 file changed, 3 insertions(+)
examples/vhost/main.c +3 -0
index d0bf1f31e3..1f6f7be8e3 100644
@@ -916,6 +916,9 @@ drain_vhost_table(void)
 	uint64_t cur_tsc;
 
 	TAILQ_FOREACH(vdev, &vhost_dev_list, global_vdev_entry) {
+		if (unlikely(vdev->remove == 1))
+			continue;
+
 		vhost_txq = vhost_txbuff[lcore_id * MAX_VHOST_DEVICE
 						+ vdev->vid];
 
Signature · 1 line
2.25.1
Reviewed-by on v2 1/1
#3 lore raw
Quoted 17 lines

-----Original Message-----
From: Ma, WenwuX <[email protected]>
Sent: Saturday, September 25, 2021 1:23 AM
To: [email protected]
Cc: [email protected]; Xia, Chenbo <[email protected]>; Jiang, Cheng1 <[email protected]>; Hu, Jiayu <[email protected]>; Yang, YvonneX <[email protected]>; Ma, WenwuX <[email protected]>; [email protected]
Subject: [PATCH v2] examples/vhost: fix use-after-free on drain vhost

When a vdev is removed in destroy_device function, the corresponding vhost TX buffer will also be freed, but the vhost TX buffer may still be used in the drain_vhost function, which will cause an error of heap-use-after-free. Therefore, before accessing vhost TX buffer, we need to check whether the vdev has been removed, if so, let's skip this vdev.

Quoted 5 lines
Fixes a68ba8e0a6b6 ("examples/vhost: refactor vhost data path")

---

#4 lore raw
In reply to Wenwu Ma
Quoted 22 lines · Wenwu Ma

When a vdev is removed in destroy_device function, the corresponding vhost TX buffer will also be freed, but the vhost TX buffer may still be used in the drain_vhost function, which will cause an error of heap-use-after-free. Therefore, before accessing vhost TX buffer, we need to check whether the vdev has been removed, if so, let's skip this vdev.

Fixes a68ba8e0a6b6 ("examples/vhost: refactor vhost data path")
---
  examples/vhost/main.c | 3 +++
  1 file changed, 3 insertions(+)
examples/vhost/main.c +0 -0
index d0bf1f31e3..1f6f7be8e3 100644

examples/vhost/main.c drain_vhost_table(void) · line 924

Quoted 9 lines
examples/vhost/main.c +3 -0
@@ -916,6 +916,9 @@ drain_vhost_table(void)
  	uint64_t cur_tsc;
 
  	TAILQ_FOREACH(vdev, &vhost_dev_list, global_vdev_entry) {
+		if (unlikely(vdev->remove == 1))
+			continue;
+
  		vhost_txq = vhost_txbuff[lcore_id * MAX_VHOST_DEVICE
  						+ vdev->vid];
 

Applied to dpdk-next-virtio/main.

Thanks,
Maxime