Skip to contentSkip to conversation

vhost/vsock: prevent stale IOTLB after ACCESS_PLATFORM changes

PATCH In review Jia Jia 4 participants 19 messages Jul 30 – Aug 4

Subsystems (4)
lore
Share & tools
lore

Jia Jia

author
#0 lore raw

PATCH 1/1 vhost/vsock: prevent stale IOTLB after ACCESS_PLATFORM changes

vhost_vsock_set_features() initializes dev->iotlb when VIRTIO_F_ACCESS_PLATFORM is enabled. It does not remove that IOTLB when the feature is later cleared. The virtqueue still points at the old IOTLB, and vhost_vsock_handle_tx_kick() passes descriptors to vhost_get_vq_desc(), which translates them through that mapping.

A userspace backend can enable ACCESS_PLATFORM, install an IOTLB entry for a payload GPA, start the device, clear ACCESS_PLATFORM, replace the memory table, and reuse the old HVA before submitting the same GPA again. The feature state then says direct memory access is in use while the TX path still uses the old IOTLB HVA.

Reject clearing ACCESS_PLATFORM while the device IOTLB exists. Also keep the existing IOTLB when a feature update leaves ACCESS_PLATFORM enabled; VHOST_SET_FEATURES is used for runtime log updates and must not discard the current translations by allocating an empty IOTLB.

Fixes e13a6915a03f ("vhost/vsock: add IOTLB API support")
---
 drivers/vhost/vsock.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/vhost/vsock.c +10 -2
index ae01457ea2cd..57e8fd1eb670 100644
@@ -798,6 +798,7 @@ static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid)
 static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features)
 {
 	struct vhost_virtqueue *vq;
+	int ret = -EFAULT;
 	int i;
 
 	if (features & ~VHOST_VSOCK_FEATURES)
@@ -809,7 +810,14 @@ static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features)
 		goto err;
 	}
 
-	if ((features & (1ULL << VIRTIO_F_ACCESS_PLATFORM))) {
+	if (!(features & (1ULL << VIRTIO_F_ACCESS_PLATFORM)) &&
+	    vsock->dev.iotlb) {
+		ret = -EBUSY;
+		goto err;
+	}
+
+	if ((features & (1ULL << VIRTIO_F_ACCESS_PLATFORM)) &&
+	    !vsock->dev.iotlb) {
 		if (vhost_init_device_iotlb(&vsock->dev))
 			goto err;
 	}
@@ -827,7 +835,7 @@ static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features)
 
 err:
 	mutex_unlock(&vsock->dev.mutex);
-	return -EFAULT;
+	return ret;
 }
 
 static long vhost_vsock_dev_ioctl(struct file *f, unsigned int ioctl,
Signature · 1 line
2.34.1
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] The identical IOTLB-discarding bug and stale IOTLB UAF vulnerability fixed in vhost_vsock remain unpatched in the sibling vhost_net subsystem. --

--- Patch [1]: [PATCH] vhost/vsock: prevent stale IOTLB after ACCESS_PLATFORM changes --- commit 6249693cea8dab5e61026ab282a77f329039d592
Author: Jia Jia <[email protected]>

vhost/vsock: prevent stale IOTLB after ACCESS_PLATFORM changes

This commit fixes a vulnerability in vhost_vsock where clearing the VIRTIO_F_ACCESS_PLATFORM feature leaves a stale IOTLB. It also prevents discarding the current translations during runtime log updates by avoiding empty IOTLB allocations.

Quoted 9 lines
drivers/vhost/vsock.c +1 -1
@@ -865,7 +866,14 @@ static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features)
 		goto err;
 	}
 
-	if ((features & (1ULL << VIRTIO_F_ACCESS_PLATFORM))) {
+	if (!(features & (1ULL << VIRTIO_F_ACCESS_PLATFORM)) &&

drivers/vhost/vsock.c static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features) · line 879

Quoted 10 lines
drivers/vhost/vsock.c +7 -0
+	    vsock->dev.iotlb) {
+		ret = -EBUSY;
+		goto err;
+	}
+
+	if ((features & (1ULL << VIRTIO_F_ACCESS_PLATFORM)) &&
+	    !vsock->dev.iotlb) {
 		if (vhost_init_device_iotlb(&vsock->dev))
 			goto err;
 	}

[Severity: High]
This is a pre-existing issue, but does the identical bug still exist in the sibling vhost_net subsystem?

Looking at drivers/vhost/net.c:vhost_net_set_features(), it appears to still use the unconditional allocation logic:

drivers/vhost/net.c:vhost_net_set_features() {
    ...
    if (virtio_features_test_bit(features, VIRTIO_F_ACCESS_PLATFORM)) {
        if (vhost_init_device_iotlb(&n->dev))
            goto out_unlock;
    }
    ...
}

When a virtual machine uses vIOMMU, could a feature update (such as userspace enabling log access during live migration) cause vhost_net_set_features() to unconditionally call vhost_init_device_iotlb(), destroying all current network DMA translations and breaking connectivity?

Additionally, does this function also fail to reject clearing the feature while an IOTLB exists, leaving the same stale IOTLB vulnerability open for vhost_net?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
In reply to Jia Jia
Quoted 36 lines · Jia Jia

vhost_vsock_set_features() initializes dev->iotlb when VIRTIO_F_ACCESS_PLATFORM is enabled. It does not remove that IOTLB when the feature is later cleared. The virtqueue still points at the old IOTLB, and vhost_vsock_handle_tx_kick() passes descriptors to vhost_get_vq_desc(), which translates them through that mapping.

A userspace backend can enable ACCESS_PLATFORM, install an IOTLB entry for a payload GPA, start the device, clear ACCESS_PLATFORM, replace the memory table, and reuse the old HVA before submitting the same GPA again. The feature state then says direct memory access is in use while the TX path still uses the old IOTLB HVA.

Reject clearing ACCESS_PLATFORM while the device IOTLB exists. Also keep the existing IOTLB when a feature update leaves ACCESS_PLATFORM enabled; VHOST_SET_FEATURES is used for runtime log updates and must not discard the current translations by allocating an empty IOTLB.

Fixes e13a6915a03f ("vhost/vsock: add IOTLB API support")
---
 drivers/vhost/vsock.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/vhost/vsock.c +1 -0
index ae01457ea2cd..57e8fd1eb670 100644
@@ -798,6 +798,7 @@ static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid)
 static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features)
 {
 	struct vhost_virtqueue *vq;
+	int ret = -EFAULT;
 	int i;
 
 	if (features & ~VHOST_VSOCK_FEATURES)

drivers/vhost/vsock.c static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features) · line 817

Quoted 9 lines
drivers/vhost/vsock.c +5 -1
@@ -809,7 +810,14 @@ static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features)
 		goto err;
 	}
 
-	if ((features & (1ULL << VIRTIO_F_ACCESS_PLATFORM))) {
+	if (!(features & (1ULL << VIRTIO_F_ACCESS_PLATFORM)) &&
+	    vsock->dev.iotlb) {
+		ret = -EBUSY;
+		goto err;
+	}

This prevents one problem but there are still other issues with how feature bit negotiation and the IOTLB are implemented:

1. VIRTIO_F_ACCESS_PLATFORM is defined by the VIRTIO spec and must not be change after feature bit negotiation. Please reject all feature bit updates except VHOST_F_LOG_ALL to comply with the VIRTIO spec and eliminate potential bugs in drivers/vhost/vsock.c.

2. When the device is reset, the iotlb cannot be left initialized because there is no guarantee that VIRTIO_F_ACCESS_PLATFORM will be negotiated again.

Quoted 17 lines
drivers/vhost/vsock.c +3 -1
+	if ((features & (1ULL << VIRTIO_F_ACCESS_PLATFORM)) &&
+	    !vsock->dev.iotlb) {
 		if (vhost_init_device_iotlb(&vsock->dev))
 			goto err;
 	}
@@ -827,7 +835,7 @@ static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features)
 
 err:
 	mutex_unlock(&vsock->dev.mutex);
-	return -EFAULT;
+	return ret;
 }
 
 static long vhost_vsock_dev_ioctl(struct file *f, unsigned int ioctl,
Signature · 1 line
2.34.1
#3 lore raw
Quoted 50 lines · Stefan Hajnoczi
Quoted 36 lines · Jia Jia

vhost_vsock_set_features() initializes dev->iotlb when VIRTIO_F_ACCESS_PLATFORM is enabled. It does not remove that IOTLB when the feature is later cleared. The virtqueue still points at the old IOTLB, and vhost_vsock_handle_tx_kick() passes descriptors to vhost_get_vq_desc(), which translates them through that mapping.

A userspace backend can enable ACCESS_PLATFORM, install an IOTLB entry for a payload GPA, start the device, clear ACCESS_PLATFORM, replace the memory table, and reuse the old HVA before submitting the same GPA again. The feature state then says direct memory access is in use while the TX path still uses the old IOTLB HVA.

Reject clearing ACCESS_PLATFORM while the device IOTLB exists. Also keep the existing IOTLB when a feature update leaves ACCESS_PLATFORM enabled; VHOST_SET_FEATURES is used for runtime log updates and must not discard the current translations by allocating an empty IOTLB.

Fixes e13a6915a03f ("vhost/vsock: add IOTLB API support")
---
 drivers/vhost/vsock.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/vhost/vsock.c +1 -0
index ae01457ea2cd..57e8fd1eb670 100644
@@ -798,6 +798,7 @@ static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid)
 static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features)
 {
 	struct vhost_virtqueue *vq;
+	int ret = -EFAULT;
 	int i;
 
 	if (features & ~VHOST_VSOCK_FEATURES)

drivers/vhost/vsock.c static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features) · line 817

Quoted 9 lines
drivers/vhost/vsock.c +5 -1
@@ -809,7 +810,14 @@ static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features)
 		goto err;
 	}
 
-	if ((features & (1ULL << VIRTIO_F_ACCESS_PLATFORM))) {
+	if (!(features & (1ULL << VIRTIO_F_ACCESS_PLATFORM)) &&
+	    vsock->dev.iotlb) {
+		ret = -EBUSY;
+		goto err;
+	}

This prevents one problem but there are still other issues with how feature bit negotiation and the IOTLB are implemented:

Quoted 4 lines

1. VIRTIO_F_ACCESS_PLATFORM is defined by the VIRTIO spec and must not be change after feature bit negotiation. Please reject all feature bit updates except VHOST_F_LOG_ALL to comply with the VIRTIO spec and eliminate potential bugs in drivers/vhost/vsock.c.

Unfortunately, vhost does not expose the feature negotiation to the kernel.

Quoted 21 lines

2. When the device is reset, the iotlb cannot be left initialized because there is no guarantee that VIRTIO_F_ACCESS_PLATFORM will be negotiated again.

Quoted 17 lines
drivers/vhost/vsock.c +3 -1
+	if ((features & (1ULL << VIRTIO_F_ACCESS_PLATFORM)) &&
+	    !vsock->dev.iotlb) {
 		if (vhost_init_device_iotlb(&vsock->dev))
 			goto err;
 	}
@@ -827,7 +835,7 @@ static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features)
 
 err:
 	mutex_unlock(&vsock->dev.mutex);
-	return -EFAULT;
+	return ret;
 }
 
 static long vhost_vsock_dev_ioctl(struct file *f, unsigned int ioctl,
Signature · 1 line
2.34.1
#4 lore raw
In reply to Jia Jia
Quoted 12 lines · Jia Jia

vhost_vsock_set_features() initializes dev->iotlb when VIRTIO_F_ACCESS_PLATFORM is enabled. It does not remove that IOTLB when the feature is later cleared. The virtqueue still points at the old IOTLB, and vhost_vsock_handle_tx_kick() passes descriptors to vhost_get_vq_desc(), which translates them through that mapping.

A userspace backend can enable ACCESS_PLATFORM, install an IOTLB entry for a payload GPA, start the device, clear ACCESS_PLATFORM, replace the memory table, and reuse the old HVA before submitting the same GPA again. The feature state then says direct memory access is in use while the TX path still uses the old IOTLB HVA.

Quoted 7 lines

Reject clearing ACCESS_PLATFORM while the device IOTLB exists. Also keep the existing IOTLB when a feature update leaves ACCESS_PLATFORM enabled; VHOST_SET_FEATURES is used for runtime log updates and must not discard the current translations by allocating an empty IOTLB.

Fixes e13a6915a03f ("vhost/vsock: add IOTLB API support")

Thanks for the patch! yet something to improve:

Quoted 17 lines
---
 drivers/vhost/vsock.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/vhost/vsock.c +1 -0
index ae01457ea2cd..57e8fd1eb670 100644
@@ -798,6 +798,7 @@ static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid)
 static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features)
 {
 	struct vhost_virtqueue *vq;
+	int ret = -EFAULT;
 	int i;
 
 	if (features & ~VHOST_VSOCK_FEATURES)

drivers/vhost/vsock.c static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features) · line 817

Quoted 9 lines
drivers/vhost/vsock.c +5 -1
@@ -809,7 +810,14 @@ static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features)
 		goto err;
 	}
 
-	if ((features & (1ULL << VIRTIO_F_ACCESS_PLATFORM))) {
+	if (!(features & (1ULL << VIRTIO_F_ACCESS_PLATFORM)) &&
+	    vsock->dev.iotlb) {
+		ret = -EBUSY;
+		goto err;
+	}

I don't know if this will break anything. Why not just blow out the iotlb? kernel will rebuild it if it needs it afterwards for some reason.

drivers/vhost/vsock.c static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features) · line 823

Quoted 6 lines
drivers/vhost/vsock.c +3 -0
+
+	if ((features & (1ULL << VIRTIO_F_ACCESS_PLATFORM)) &&
+	    !vsock->dev.iotlb) {
 		if (vhost_init_device_iotlb(&vsock->dev))
 			goto err;
 	}

This part you are fixing is harmless. Let's make it a separate patch and document the motivation.

Quoted 11 lines
drivers/vhost/vsock.c +1 -1
@@ -827,7 +835,7 @@ static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features)
 
 err:
 	mutex_unlock(&vsock->dev.mutex);
-	return -EFAULT;
+	return ret;
 }
 
 static long vhost_vsock_dev_ioctl(struct file *f, unsigned int ioctl,
Signature · 1 line
2.34.1
author
#5 lore raw
Quoted 6 lines · Michael S. Tsirkin

I don't know if this will break anything. Why not just blow out the iotlb? kernel will rebuild it if it needs it afterwards for some reason.

This part you are fixing is harmless. Let's make it a separate patch and document the motivation.

Thanks.

Yes, I considered discarding the IOTLB before choosing -EBUSY. My hesitation was not about whether it could eventually be rebuilt, but that doing so would no longer be only a control-plane validation change. It would modify translation state directly used by running virtqueues.

The minimum synchronization I had in mind was roughly:

vhost_vsock_set_features(features):
mutex_lock(dev.mutex)

if (!(features & ACCESS_PLATFORM) && dev.iotlb):
lock all vq.mutex in index order

old = dev.iotlb
dev.iotlb = NULL

for each vq:
vq.iotlb = NULL
reset vq.meta_iotlb
vq.acked_features = features

unlock all vq.mutex
vhost_iotlb_free(old)

...

mutex_unlock(dev.mutex)

The switch has to be serialized against all active VQs. My hesitation was that this turns VHOST_SET_FEATURES into a datapath synchronization
point: the ioctl may wait for in-flight kick handlers, and rebuilding an empty IOTLB may stall queues on misses until the translations are restored. If stopping or flushing the device is also required, the disruption is larger than a normal feature update.

I was not sure whether VHOST_SET_FEATURES is expected to impose that cost on a running device, or whether userspace should quiesce it first. That is why I chose -EBUSY for v1. If synchronization with active VQs is the intended behavior here, I can rework the patch accordingly.

I will also split the existing-IOTLB preservation change into a separate patch and document its motivation.

#6 lore raw
Quoted 46 lines · Jia Jia
Quoted 6 lines · Michael S. Tsirkin

I don't know if this will break anything. Why not just blow out the iotlb? kernel will rebuild it if it needs it afterwards for some reason.

This part you are fixing is harmless. Let's make it a separate patch and document the motivation.

Thanks.

Yes, I considered discarding the IOTLB before choosing -EBUSY. My hesitation was not about whether it could eventually be rebuilt, but that doing so would no longer be only a control-plane validation change. It would modify translation state directly used by running virtqueues.

The minimum synchronization I had in mind was roughly:

vhost_vsock_set_features(features):
mutex_lock(dev.mutex)

if (!(features & ACCESS_PLATFORM) && dev.iotlb):
lock all vq.mutex in index order

old = dev.iotlb
dev.iotlb = NULL

for each vq:
vq.iotlb = NULL
reset vq.meta_iotlb
vq.acked_features = features

unlock all vq.mutex
vhost_iotlb_free(old)

...

mutex_unlock(dev.mutex)

The switch has to be serialized against all active VQs. My hesitation was that this turns VHOST_SET_FEATURES into a datapath synchronization
point: the ioctl may wait for in-flight kick handlers, and rebuilding an empty IOTLB may stall queues on misses until the translations are restored. If stopping or flushing the device is also required, the disruption is larger than a normal feature update.

Quoted 7 lines

I was not sure whether VHOST_SET_FEATURES is expected to impose that cost on a running device, or whether userspace should quiesce it first. That is why I chose -EBUSY for v1. If synchronization with active VQs is the intended behavior here, I can rework the patch accordingly.

I will also split the existing-IOTLB preservation change into a separate patch and document its motivation.

thanks

author
#7 lore raw

v2 cover vhost/vsock: fix device IOTLB feature lifecycle

In reply to Michael S. Tsirkin

As suggested, split the changes into two patches.

Patch 1 detaches and frees the device IOTLB when ACCESS_PLATFORM is cleared. The device and virtqueue IOTLB pointers, metadata caches, and acked features are updated while all virtqueue mutexes are held. It also drops old IOTLB miss messages and wakes readers.

Patch 2 keeps the current IOTLB when ACCESS_PLATFORM remains enabled, so a runtime logging update does not discard valid translations.

Changes since v1:
- discard the existing IOTLB instead of returning -EBUSY
- serialize the switch against all virtqueues
- clear old IOTLB messages and wake readers
- move the existing-IOTLB preservation change to a separate patch

Jia Jia (2):
vhost/vsock: discard IOTLB when ACCESS_PLATFORM is cleared vhost/vsock: keep IOTLB across feature updates

 drivers/vhost/vsock.c | 46 +++++++++++++++++++++++++++++++++++++------
 1 file changed, 40 insertions(+), 6 deletions(-)
Signature · 1 line
2.34.1
author
#8 lore raw

PATCH v2 1/2 vhost/vsock: discard IOTLB when ACCESS_PLATFORM is cleared

vhost_vsock_set_features() leaves the device IOTLB attached when userspace clears VIRTIO_F_ACCESS_PLATFORM. Descriptors can therefore continue to use translations installed before the feature change, including HVAs made stale by a later memory table update.

Detach the IOTLB before acknowledging a feature mask without ACCESS_PLATFORM. Hold all virtqueue mutexes in index order while clearing the device and virtqueue IOTLB pointers, resetting metadata caches, and updating the acknowledged features. This prevents a kick handler from observing a mixed translation state.

Free the old IOTLB after releasing the virtqueue mutexes. Also drop the old IOTLB miss messages and wake readers now that the device no longer accepts IOTLB updates.

Fixes e13a6915a03f ("vhost/vsock: add IOTLB API support")
---
 drivers/vhost/vsock.c | 43 ++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 38 insertions(+), 5 deletions(-)
drivers/vhost/vsock.c +38 -5
index 9aaab6bb8061..562b9e139a76 100644
@@ -851,6 +851,34 @@ static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid)
 	return 0;
 }
 
+/* Caller must hold the device mutex. */
+static void vhost_vsock_clear_iotlb(struct vhost_vsock *vsock, u64 features)
+{
+	struct vhost_iotlb *iotlb;
+	struct vhost_virtqueue *vq;
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++)
+		mutex_lock_nested(&vsock->vqs[i].mutex, i);
+
+	iotlb = vsock->dev.iotlb;
+	vsock->dev.iotlb = NULL;
+
+	for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
+		vq = &vsock->vqs[i];
+		vq->iotlb = NULL;
+		memset(vq->meta_iotlb, 0, sizeof(vq->meta_iotlb));
+		vq->acked_features = features;
+	}
+
+	for (i = ARRAY_SIZE(vsock->vqs); i-- > 0;)
+		mutex_unlock(&vsock->vqs[i].mutex);
+
+	vhost_clear_msg(&vsock->dev);
+	vhost_iotlb_free(iotlb);
+	wake_up_interruptible_poll(&vsock->dev.wait, EPOLLIN | EPOLLRDNORM);
+}
+
 static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features)
 {
 	struct vhost_virtqueue *vq;
@@ -872,11 +900,16 @@ static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features)
 
 	vsock->seqpacket_allow = features & (1ULL << VIRTIO_VSOCK_F_SEQPACKET);
 
-	for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
-		vq = &vsock->vqs[i];
-		mutex_lock(&vq->mutex);
-		vq->acked_features = features;
-		mutex_unlock(&vq->mutex);
+	if (!(features & (1ULL << VIRTIO_F_ACCESS_PLATFORM)) &&
+	    vsock->dev.iotlb) {
+		vhost_vsock_clear_iotlb(vsock, features);
+	} else {
+		for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
+			vq = &vsock->vqs[i];
+			mutex_lock(&vq->mutex);
+			vq->acked_features = features;
+			mutex_unlock(&vq->mutex);
+		}
 	}
 	mutex_unlock(&vsock->dev.mutex);
 	return 0;
Signature · 1 line
2.34.1
#9 lore raw

Re: [PATCH v2 1/2] vhost/vsock: discard IOTLB when ACCESS_PLATFORM is cleared

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [Critical] A NULL pointer dereference occurs in translate_desc() if both dev->iotlb and dev->umem are NULL when a descriptor is processed. --

--- Patch [1]: [PATCH v2 1/2] vhost/vsock: discard IOTLB when ACCESS_PLATFORM is cleared --- commit 711b531d8532907f465c4af9ee082250c352dc13
Author: Jia Jia <[email protected]>

vhost/vsock: discard IOTLB when ACCESS_PLATFORM is cleared

This patch detaches the IOTLB before acknowledging a feature mask without ACCESS_PLATFORM to prevent stale translations. It clears the device and virtqueue IOTLB pointers while holding virtqueue mutexes to ensure kick handlers do not observe mixed translation states.

Quoted 14 lines
drivers/vhost/vsock.c +6 -0
index 9aaab6bb8061c..562b9e139a76f 100644
@@ -851,6 +851,34 @@ static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid)
 	return 0;
 }
 
+/* Caller must hold the device mutex. */
+static void vhost_vsock_clear_iotlb(struct vhost_vsock *vsock, u64 features)
+{
+	struct vhost_iotlb *iotlb;
+	struct vhost_virtqueue *vq;
+	int i;

drivers/vhost/vsock.c static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid) · line 869

Quoted 10 lines
drivers/vhost/vsock.c +10 -0
+
+	for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++)
+		mutex_lock_nested(&vsock->vqs[i].mutex, i);
+
+	iotlb = vsock->dev.iotlb;
+	vsock->dev.iotlb = NULL;
+
+	for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
+		vq = &vsock->vqs[i];
+		vq->iotlb = NULL;

[Severity: Critical]
This is a pre-existing issue, but by clearing dev->iotlb and vq->iotlb to NULL here, does this expose a NULL pointer dereference in translate_desc() if dev->umem was never initialized?

If userspace configures the vrings and kicks the virtqueue without ever calling VHOST_SET_MEM_TABLE, dev->umem remains NULL. When vhost_vsock_clear_iotlb() clears dev->iotlb, the next descriptor translation could trigger this path:

drivers/vhost/vhost.c:translate_desc() {
	...
	struct vhost_iotlb *umem = dev->iotlb ? dev->iotlb : dev->umem;
	...
	while ((u64)len > s) {
		...
		map = vhost_iotlb_itree_first(umem, addr, last);
drivers/vhost/iotlb.c:vhost_iotlb_itree_first() {
	return vhost_iotlb_itree_iter_first(&iotlb->root, start, last);
}

Because umem is NULL, taking the address of iotlb->root passes NULL to the interval tree iterator, which would cause a page fault when dereferenced.

Can a malicious guest or unprivileged userspace process trigger this by negotiating VIRTIO_F_ACCESS_PLATFORM, avoiding VHOST_SET_MEM_TABLE, and then clearing the feature bit dynamically?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#10 lore raw

Re: [PATCH v2 1/2] vhost/vsock: discard IOTLB when ACCESS_PLATFORM is cleared

In reply to Jia Jia
Quoted 41 lines · Jia Jia

vhost_vsock_set_features() leaves the device IOTLB attached when userspace clears VIRTIO_F_ACCESS_PLATFORM. Descriptors can therefore continue to use translations installed before the feature change, including HVAs made stale by a later memory table update.

Detach the IOTLB before acknowledging a feature mask without ACCESS_PLATFORM. Hold all virtqueue mutexes in index order while clearing the device and virtqueue IOTLB pointers, resetting metadata caches, and updating the acknowledged features. This prevents a kick handler from observing a mixed translation state.

Free the old IOTLB after releasing the virtqueue mutexes. Also drop the old IOTLB miss messages and wake readers now that the device no longer accepts IOTLB updates.

Fixes e13a6915a03f ("vhost/vsock: add IOTLB API support")
---
 drivers/vhost/vsock.c | 43 ++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 38 insertions(+), 5 deletions(-)
drivers/vhost/vsock.c +12 -0
index 9aaab6bb8061..562b9e139a76 100644
@@ -851,6 +851,34 @@ static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid)
 	return 0;
 }
 
+/* Caller must hold the device mutex. */
+static void vhost_vsock_clear_iotlb(struct vhost_vsock *vsock, u64 features)
+{
+	struct vhost_iotlb *iotlb;
+	struct vhost_virtqueue *vq;
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++)
+		mutex_lock_nested(&vsock->vqs[i].mutex, i);
+
+	iotlb = vsock->dev.iotlb;
+	vsock->dev.iotlb = NULL;

drivers/vhost/vsock.c static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid) · line 875

Quoted 10 lines
drivers/vhost/vsock.c +10 -0
+
+	for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
+		vq = &vsock->vqs[i];
+		vq->iotlb = NULL;
+		memset(vq->meta_iotlb, 0, sizeof(vq->meta_iotlb));
+		vq->acked_features = features;
+	}
+
+	for (i = ARRAY_SIZE(vsock->vqs); i-- > 0;)
+		mutex_unlock(&vsock->vqs[i].mutex);

Why lock down all vqs like this? Would this work just as well instead?

	iotlb = vsock->dev.iotlb;
	vsock->dev.iotlb = NULL;

	for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
		mutex_lock(&vsock->vqs[i].mutex);
		vq = &vsock->vqs[i];
		vq->iotlb = NULL;
		memset(vq->meta_iotlb, 0, sizeof(vq->meta_iotlb));
		vq->acked_features = features;
		mutex_unlock(&vsock->vqs[i].mutex);
	}

and if no why not?

Quoted 32 lines
drivers/vhost/vsock.c +15 -5
+	vhost_clear_msg(&vsock->dev);
+	vhost_iotlb_free(iotlb);
+	wake_up_interruptible_poll(&vsock->dev.wait, EPOLLIN | EPOLLRDNORM);
+}
+
 static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features)
 {
 	struct vhost_virtqueue *vq;
@@ -872,11 +900,16 @@ static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features)
 
 	vsock->seqpacket_allow = features & (1ULL << VIRTIO_VSOCK_F_SEQPACKET);
 
-	for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
-		vq = &vsock->vqs[i];
-		mutex_lock(&vq->mutex);
-		vq->acked_features = features;
-		mutex_unlock(&vq->mutex);
+	if (!(features & (1ULL << VIRTIO_F_ACCESS_PLATFORM)) &&
+	    vsock->dev.iotlb) {
+		vhost_vsock_clear_iotlb(vsock, features);
+	} else {
+		for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
+			vq = &vsock->vqs[i];
+			mutex_lock(&vq->mutex);
+			vq->acked_features = features;
+			mutex_unlock(&vq->mutex);
+		}
 	}
 	mutex_unlock(&vsock->dev.mutex);
 	return 0;
Signature · 1 line
2.34.1
author
#11 lore raw

Re: [PATCH v2 1/2] vhost/vsock: discard IOTLB when ACCESS_PLATFORM is cleared

Quoted 14 lines · Michael S. Tsirkin

Why lock down all vqs like this? Would this work just as well instead?

	iotlb = vsock->dev.iotlb;
	vsock->dev.iotlb = NULL;

	for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
		mutex_lock(&vsock->vqs[i].mutex);
		vq = &vsock->vqs[i];
		vq->iotlb = NULL;
		memset(vq->meta_iotlb, 0, sizeof(vq->meta_iotlb));
		vq->acked_features = features;
		mutex_unlock(&vsock->vqs[i].mutex);
	}
Quoted 1 line

and if no why not?

Thanks for the review.

My understanding is as follows. The proposed sequence protects the lifetime of the old IOTLB, but it does not keep the translation state consistent during the transition.

dev->iotlb is shared by all VQs, while vq->iotlb, meta_iotlb, and acked_features are per-VQ state. A kick handler only holds its own VQ mutex. If dev->iotlb is cleared first, a handler that already holds a VQ mutex can continue using the old vq->iotlb and metadata cache, while translate_desc() sees dev->iotlb == NULL and falls back to dev->umem. The same handler could therefore observe both the IOVA/IOTLB and GPA/umem views.

Locking each VQ in turn before freeing the old IOTLB prevents a lifetime issue, but it does not remove this mixed-state window. Taking all VQ mutexes before changing dev->iotlb lets active handlers finish and prevents new handlers from running until the shared and per-VQ state has been updated consistently.

If VHOST_SET_FEATURES is guaranteed to run only while all VQs are stopped or otherwise quiesced, then the shorter sequence should be sufficient. Since the ioctl itself does not enforce that, I thought this transition also needed to be safe while a VQ may still be active.

Please correct me if I have misunderstood anything. Thank you very much.

author
#12 lore raw

Re: [PATCH v2 1/2] vhost/vsock: discard IOTLB when ACCESS_PLATFORM is cleared

In reply to Michael S. Tsirkin
Quoted 14 lines · Michael S. Tsirkin

Why lock down all vqs like this? Would this work just as well instead?

      iotlb = vsock->dev.iotlb;
      vsock->dev.iotlb = NULL;

      for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
              mutex_lock(&vsock->vqs[i].mutex);
              vq = &vsock->vqs[i];
              vq->iotlb = NULL;
              memset(vq->meta_iotlb, 0, sizeof(vq->meta_iotlb));
              vq->acked_features = features;
              mutex_unlock(&vsock->vqs[i].mutex);
      }
Quoted 1 line

and if no why not?

Let me add a little more detail to my earlier reasoning. Locking the VQs one at a time does reduce lock hold time and avoids blocking one queue while waiting for another. However, the additional blocking from taking all VQ mutexes is confined to the VHOST_SET_FEATURES transition and does not add any steady-state data-path overhead. vsock has only two VQs, and once the locks have been acquired, the critical section only updates a few pointers, metadata caches, and feature fields.

dev->iotlb is shared by all VQs, while vq->iotlb, meta_iotlb, and acked_features are per-VQ state protected by that VQ's mutex. A kick handler only holds its own VQ mutex. The following interleaving therefore seems possible:

```text
worker: holds vq->mutex with the old vq->iotlb
ioctl:  sets dev->iotlb = NULL
ioctl:  waits for vq->mutex
worker: continues processing with the old per-VQ state
```

During this window, the state can be:

```text
vq->iotlb          = old_iotlb
vq->meta_iotlb     = old mappings
vq->acked_features = ACCESS_PLATFORM enabled
dev->iotlb         = NULL
```

vq_meta_prefetch() may still use the old vq->iotlb and metadata cache, while translate_desc() sees dev->iotlb == NULL and falls back to dev->umem. The same handler could therefore access the vring through the old IOTLB and then interpret a descriptor address as a GPA when translating the payload.

If that IOVA has no corresponding GPA mapping, translate_desc() returns -EFAULT and aborts the current queue-processing pass. If it happens to fall within a valid GPA mapping, the translation may produce an iovec for a different HVA.

Clearing dev->iotlb is also different from replacing one mapping table with another under the same address model, since it changes the address interpretation from IOVA to GPA.

As I mentioned in my earlier reply, I do not see any check in the vhost-vsock VHOST_SET_FEATURES ioctl path that guarantees all VQs are stopped or otherwise quiesced, so I thought the transition also needed to be safe while a VQ may still be active.

Please let me know if I am missing such a guarantee elsewhere. Thanks.

author
#13 lore raw

PATCH v2 2/2 vhost/vsock: keep IOTLB across feature updates

In reply to Jia Jia

VHOST_SET_FEATURES is also used to update logging while a device is running. When ACCESS_PLATFORM stays enabled, allocating a new empty IOTLB on every call drops valid translations and forces avoidable misses.

Initialize the device IOTLB only when one does not already exist.

---
 drivers/vhost/vsock.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/vhost/vsock.c +2 -1
index 562b9e139a76..3703fd219039 100644
@@ -893,7 +893,8 @@ static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features)
 		goto err;
 	}
 
-	if ((features & (1ULL << VIRTIO_F_ACCESS_PLATFORM))) {
+	if ((features & (1ULL << VIRTIO_F_ACCESS_PLATFORM)) &&
+	    !vsock->dev.iotlb) {
 		if (vhost_init_device_iotlb(&vsock->dev))
 			goto err;
 	}
Signature · 1 line
2.34.1
#14 lore raw

Re: [PATCH v2 2/2] vhost/vsock: keep IOTLB across feature updates

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [Critical] Missing bounds checking in vhost_vq_meta_fetch() combined with a failure to clear the metadata cache during VHOST_SET_VRING_ADDR allows malicious userspace to achieve arbitrary kernel read/write via vhost worker threads. --

--- Patch [2]: [PATCH v2 2/2] vhost/vsock: keep IOTLB across feature updates --- commit f04e21a46c433140a2f8e01687d07519c6aa6866
Author: Jia Jia <[email protected]>

vhost/vsock: keep IOTLB across feature updates

This commit modifies vhost_vsock_set_features() to only initialize the device IOTLB when one does not already exist. This prevents dropping valid translations and forcing avoidable misses when VHOST_SET_FEATURES is used to update logging while a device is running.

Quoted 5 lines
drivers/vhost/vsock.c +0 -0
index 562b9e139a76f..3703fd219039b 100644

drivers/vhost/vsock.c static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features) · line 900

Quoted 9 lines
drivers/vhost/vsock.c +2 -1
@@ -893,7 +893,8 @@ static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features)
 		goto err;
 	}
 
-	if ((features & (1ULL << VIRTIO_F_ACCESS_PLATFORM))) {
+	if ((features & (1ULL << VIRTIO_F_ACCESS_PLATFORM)) &&
+	    !vsock->dev.iotlb) {
 		if (vhost_init_device_iotlb(&vsock->dev))
 			goto err;
 	}

[Severity: Critical]
This is a pre-existing issue, but I noticed a potential bounds checking problem in vhost_vq_meta_fetch() and vhost_vring_set_addr() in drivers/vhost/vhost.c.

Does vhost_vq_meta_fetch() lack bounds checking on the requested address?

drivers/vhost/vhost.c:vhost_vq_meta_fetch() {
	const struct vhost_iotlb_map *map = vq->meta_iotlb[type];

	if (!map)
		return NULL;

	return (void __user *)(uintptr_t)(map->addr + addr - map->start);
}

When VHOST_SET_VRING_ADDR is called, vhost_vring_set_addr() updates the vring addresses but does not appear to invalidate the meta_iotlb cache (for example, by calling __vhost_vq_meta_reset()):

drivers/vhost/vhost.c:vhost_vring_set_addr() {
	...
	vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG));
	vq->desc = (void __user *)(unsigned long)a.desc_user_addr;
	vq->avail = (void __user *)(unsigned long)a.avail_user_addr;
	vq->log_addr = a.log_guest_addr;
	vq->used = (void __user *)(unsigned long)a.used_user_addr;

	return 0;
}

Could this allow a crafted, out-of-bounds IOVA for the vring to bypass access_ok() checks and calculate an arbitrary kernel address if a valid IOTLB mapping was previously established and cached?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#15 lore raw

Re: [PATCH v2 2/2] vhost/vsock: keep IOTLB across feature updates

In reply to Jia Jia
Quoted 8 lines · Jia Jia

VHOST_SET_FEATURES is also used to update logging while a device is running. When ACCESS_PLATFORM stays enabled, allocating a new empty IOTLB on every call drops valid translations and forces avoidable misses.

Initialize the device IOTLB only when one does not already exist.

You did not CCc all maintainers and lists with this patchset. ./scripts/get_maintainer.pl -f drivers/vhost/vsock.c will help find out whom to copy.

thanks!

Quoted 20 lines
---
 drivers/vhost/vsock.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/vhost/vsock.c +2 -1
index 562b9e139a76..3703fd219039 100644
@@ -893,7 +893,8 @@ static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features)
 		goto err;
 	}
 
-	if ((features & (1ULL << VIRTIO_F_ACCESS_PLATFORM))) {
+	if ((features & (1ULL << VIRTIO_F_ACCESS_PLATFORM)) &&
+	    !vsock->dev.iotlb) {
 		if (vhost_init_device_iotlb(&vsock->dev))
 			goto err;
 	}
Signature · 1 line
2.34.1
author
#16 lore raw

Re: [PATCH v2 2/2] vhost/vsock: keep IOTLB across feature updates

Quoted 3 lines · Michael S. Tsirkin

You did not CCc all maintainers and lists with this patchset. ./scripts/get_maintainer.pl -f drivers/vhost/vsock.c will help find out whom to copy.

Sorry, I overlooked that. I will add the missing maintainers and lists in the next revision. Thanks for the reminder.

#17 lore raw

Re: [PATCH v2 0/2] vhost/vsock: fix device IOTLB feature lifecycle

In reply to Jia Jia
Quoted 9 lines · Jia Jia

As suggested, split the changes into two patches.

Patch 1 detaches and frees the device IOTLB when ACCESS_PLATFORM is cleared. The device and virtqueue IOTLB pointers, metadata caches, and acked features are updated while all virtqueue mutexes are held. It also drops old IOTLB miss messages and wakes readers.

Patch 2 keeps the current IOTLB when ACCESS_PLATFORM remains enabled, so a runtime logging update does not discard valid translations.

pls do not post v2 as reply to v1. confuses tools. thanks!

Quoted 15 lines

Changes since v1:
- discard the existing IOTLB instead of returning -EBUSY
- serialize the switch against all virtqueues
- clear old IOTLB messages and wake readers
- move the existing-IOTLB preservation change to a separate patch

Jia Jia (2):
vhost/vsock: discard IOTLB when ACCESS_PLATFORM is cleared vhost/vsock: keep IOTLB across feature updates

 drivers/vhost/vsock.c | 46 +++++++++++++++++++++++++++++++++++++------
 1 file changed, 40 insertions(+), 6 deletions(-)
Signature · 1 line
2.34.1
author
#18 lore raw

Re: [PATCH v2 0/2] vhost/vsock: fix device IOTLB feature lifecycle

Quoted 1 line · Michael S. Tsirkin

pls do not post v2 as reply to v1. confuses tools. thanks!

Understood. Thanks for the reminder. I will resend the v2 patch set as a new thread.