Skip to contentSkip to conversation

vhost: reset the vring metadata cache on vring reconfiguration

PATCH Superseded Jun Yang 3 participants 5 messages Aug 3 – Aug 5

Subsystems (5)
lore
Share & tools
lore

Jun Yang

author
#0 lore raw

PATCH 1/1 vhost: reset the vring metadata cache on vring reconfiguration

From: Jun Yang <[email protected]>

vq->meta_iotlb[] caches the vhost_iotlb_map that backs each vring metadata region, and iotlb_access_ok() returns early on a cache hit, taking the hit as proof that the region has already been validated:

	if (vhost_vq_meta_fetch(vq, addr, len, type))
		return true;

The cache is reset on VHOST_IOTLB_UPDATE and VHOST_IOTLB_INVALIDATE, on device IOTLB (re)initialisation and on vq reset, but not when VHOST_SET_VRING_ADDR replaces vq->desc, vq->avail and vq->used, nor when VHOST_SET_VRING_NUM changes the region sizes.

With a device IOTLB attached both ioctls are accepted while the vq is live, and neither validates the addresses at ioctl time: vq_access_ok() and vq_log_used_access_ok() return true early because the addresses are GIOVAs, deferring validation to prefetch time. Once the cache has been populated that deferred validation no longer runs -- vq_meta_prefetch() hits the stale entry and returns true -- and vhost_vq_meta_fetch() keeps translating through the old mapping as

	map->addr + addr - map->start

for an address the mapping no longer covers. vhost_copy_to_user() and vhost_copy_from_user() consume the result with __copy_to_user() and __copy_from_user(), which do not check it either, so a subsequent used ring update or descriptor fetch accesses memory outside the region the IOTLB actually maps.

Reset the metadata cache whenever the vring is reconfigured, so the new addresses are pushed back through iotlb_access_ok()'s slow path.

Fixes f88949138058 ("vhost: introduce O(1) vq metadata cache")
---
 drivers/vhost/vhost.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/vhost/vhost.c +8 -0
index 4c525b3e16ea..77c96993100b 100644
@@ -2126,6 +2126,14 @@ static long vhost_vring_set_num_addr(struct vhost_dev *d,
 		BUG();
 	}
 
+	/*
+	 * The metadata cache holds the IOTLB mapping that backed the previous
+	 * desc/avail/used addresses and vring size, both of which are being
+	 * replaced here.  iotlb_access_ok() takes a cache hit as proof that the
+	 * region was validated, so the stale entries have to go.
+	 */
+	__vhost_vq_meta_reset(vq);
+
 	mutex_unlock(&vq->mutex);
 
 	return r;
Signature · 1 line
2.55.0
#1 lore raw
Quoted 30 lines · Jun Yang

From: Jun Yang <[email protected]>

vq->meta_iotlb[] caches the vhost_iotlb_map that backs each vring metadata region, and iotlb_access_ok() returns early on a cache hit, taking the hit as proof that the region has already been validated:

	if (vhost_vq_meta_fetch(vq, addr, len, type))
		return true;

The cache is reset on VHOST_IOTLB_UPDATE and VHOST_IOTLB_INVALIDATE, on device IOTLB (re)initialisation and on vq reset, but not when VHOST_SET_VRING_ADDR replaces vq->desc, vq->avail and vq->used, nor when VHOST_SET_VRING_NUM changes the region sizes.

With a device IOTLB attached both ioctls are accepted while the vq is live, and neither validates the addresses at ioctl time: vq_access_ok() and vq_log_used_access_ok() return true early because the addresses are GIOVAs, deferring validation to prefetch time. Once the cache has been populated that deferred validation no longer runs -- vq_meta_prefetch() hits the stale entry and returns true -- and vhost_vq_meta_fetch() keeps translating through the old mapping as

	map->addr + addr - map->start

for an address the mapping no longer covers. vhost_copy_to_user() and vhost_copy_from_user() consume the result with __copy_to_user() and __copy_from_user(), which do not check it either, so a subsequent used ring update or descriptor fetch accesses memory outside the region the IOTLB actually maps.

Quoted 7 lines

Reset the metadata cache whenever the vring is reconfigured, so the new addresses are pushed back through iotlb_access_ok()'s slow path.

Fixes f88949138058 ("vhost: introduce O(1) vq metadata cache")

And it seems AI assisted writing at least the commit log am I right?

You are supposed to use:

Assisted-by AGENT_NAME:MODEL_VERSION
Quoted 25 lines
---
 drivers/vhost/vhost.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/vhost/vhost.c +8 -0
index 4c525b3e16ea..77c96993100b 100644
@@ -2126,6 +2126,14 @@ static long vhost_vring_set_num_addr(struct vhost_dev *d,
 		BUG();
 	}
 
+	/*
+	 * The metadata cache holds the IOTLB mapping that backed the previous
+	 * desc/avail/used addresses and vring size, both of which are being
+	 * replaced here.  iotlb_access_ok() takes a cache hit as proof that the
+	 * region was validated, so the stale entries have to go.
+	 */
+	__vhost_vq_meta_reset(vq);
+
 	mutex_unlock(&vq->mutex);
 
 	return r;
Signature · 1 line
2.55.0
maintainer Acked-by on v1 1/1
#2 lore raw
In reply to Jun Yang
Quoted 30 lines · Jun Yang

From: Jun Yang <[email protected]>

vq->meta_iotlb[] caches the vhost_iotlb_map that backs each vring metadata region, and iotlb_access_ok() returns early on a cache hit, taking the hit as proof that the region has already been validated:

	if (vhost_vq_meta_fetch(vq, addr, len, type))
		return true;

The cache is reset on VHOST_IOTLB_UPDATE and VHOST_IOTLB_INVALIDATE, on device IOTLB (re)initialisation and on vq reset, but not when VHOST_SET_VRING_ADDR replaces vq->desc, vq->avail and vq->used, nor when VHOST_SET_VRING_NUM changes the region sizes.

With a device IOTLB attached both ioctls are accepted while the vq is live, and neither validates the addresses at ioctl time: vq_access_ok() and vq_log_used_access_ok() return true early because the addresses are GIOVAs, deferring validation to prefetch time. Once the cache has been populated that deferred validation no longer runs -- vq_meta_prefetch() hits the stale entry and returns true -- and vhost_vq_meta_fetch() keeps translating through the old mapping as

	map->addr + addr - map->start

for an address the mapping no longer covers. vhost_copy_to_user() and vhost_copy_from_user() consume the result with __copy_to_user() and __copy_from_user(), which do not check it either, so a subsequent used ring update or descriptor fetch accesses memory outside the region the IOTLB actually maps.

Quoted 7 lines

Reset the metadata cache whenever the vring is reconfigured, so the new addresses are pushed back through iotlb_access_ok()'s slow path.

Fixes f88949138058 ("vhost: introduce O(1) vq metadata cache")

patch itself ok:

Quoted 25 lines
---
 drivers/vhost/vhost.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/vhost/vhost.c +8 -0
index 4c525b3e16ea..77c96993100b 100644
@@ -2126,6 +2126,14 @@ static long vhost_vring_set_num_addr(struct vhost_dev *d,
 		BUG();
 	}
 
+	/*
+	 * The metadata cache holds the IOTLB mapping that backed the previous
+	 * desc/avail/used addresses and vring size, both of which are being
+	 * replaced here.  iotlb_access_ok() takes a cache hit as proof that the
+	 * region was validated, so the stale entries have to go.
+	 */
+	__vhost_vq_meta_reset(vq);
+
 	mutex_unlock(&vq->mutex);
 
 	return r;
Signature · 1 line
2.55.0
author
#3 lore raw

PATCH v2 1/1 vhost: reset the vring metadata cache on vring reconfiguration

In reply to Jun Yang

From: Jun Yang <[email protected]>

vq->meta_iotlb[] caches the vhost_iotlb_map that backs each vring metadata region, and iotlb_access_ok() returns early on a cache hit, taking the hit as proof that the region has already been validated:

	if (vhost_vq_meta_fetch(vq, addr, len, type))
		return true;

The cache is reset on VHOST_IOTLB_UPDATE and VHOST_IOTLB_INVALIDATE, on device IOTLB (re)initialisation and on vq reset, but not when VHOST_SET_VRING_ADDR replaces vq->desc, vq->avail and vq->used, nor when VHOST_SET_VRING_NUM changes the region sizes.

With a device IOTLB attached both ioctls are accepted while the vq is live, and neither validates the addresses at ioctl time: vq_access_ok() and vq_log_used_access_ok() return true early because the addresses are GIOVAs, deferring validation to prefetch time. Once the cache has been populated that deferred validation no longer runs -- vq_meta_prefetch() hits the stale entry and returns true -- and vhost_vq_meta_fetch() keeps translating through the old mapping as

	map->addr + addr - map->start

for an address the mapping no longer covers. vhost_copy_to_user() and vhost_copy_from_user() consume the result with __copy_to_user() and __copy_from_user(), which do not check it either, so a subsequent used ring update or descriptor fetch accesses memory outside the region the IOTLB actually maps.

Reset the metadata cache whenever the vring is reconfigured, so the new addresses are pushed back through iotlb_access_ok()'s slow path.

Fixes f88949138058 ("vhost: introduce O(1) vq metadata cache")
Assisted-by tencentos-corvus-ai:kimi-k3

---
v2: add Assisted-by and Acked-by tags

 drivers/vhost/vhost.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/vhost/vhost.c +8 -0
index 4c525b3e16ea..77c96993100b 100644
@@ -2126,6 +2126,14 @@ static long vhost_vring_set_num_addr(struct vhost_dev *d,
 		BUG();
 	}
 
+	/*
+	 * The metadata cache holds the IOTLB mapping that backed the previous
+	 * desc/avail/used addresses and vring size, both of which are being
+	 * replaced here.  iotlb_access_ok() takes a cache hit as proof that the
+	 * region was validated, so the stale entries have to go.
+	 */
+	__vhost_vq_meta_reset(vq);
+
 	mutex_unlock(&vq->mutex);
 
 	return r;
Signature · 1 line
2.50.1 (Apple Git-155)
#4 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] vhost_put_user falls back to __vhost_get_user_slow which hardcodes VHOST_ACCESS_RO, bypassing IOTLB write protections. --

--- Patch [1]: [PATCH v2] vhost: reset the vring metadata cache on vring reconfiguration --- commit 3ebd42f4cc1c99600e85fca94195264924a8cc97
Author: Jun Yang <[email protected]>
vhost: reset the vring metadata cache on vring reconfiguration

This commit ensures the vring metadata cache is reset whenever the vring is reconfigured. This forces new addresses to be validated through the slower access path instead of hitting stale cache entries.

Quoted 4 lines
drivers/vhost/vhost.c +0 -0
index 4c525b3e16ead..77c96993100b5 100644

[ ... ]

drivers/vhost/vhost.c static long vhost_vring_set_num_addr(struct vhost_dev *d, · line 2136

Quoted 12 lines
drivers/vhost/vhost.c +8 -0
@@ -2126,6 +2126,14 @@ static long vhost_vring_set_num_addr(struct vhost_dev *d,
 		BUG();
 	}
 
+	/*
+	 * The metadata cache holds the IOTLB mapping that backed the previous
+	 * desc/avail/used addresses and vring size, both of which are being
+	 * replaced here.  iotlb_access_ok() takes a cache hit as proof that the
+	 * region was validated, so the stale entries have to go.
+	 */
+	__vhost_vq_meta_reset(vq);
+

[Severity: High]
This is a pre-existing issue, but does resetting the metadata cache here expose a vulnerability where IOTLB write protections can be bypassed?

When the cache is reset, a subsequent backend configuration can trigger vhost_update_used_flags(), which calls vhost_put_user() to write to the used ring. Because the cache is empty, vhost_put_user() falls back to __vhost_get_user_slow().

Looking at the fallback in __vhost_get_user_slow():

drivers/vhost/vhost.c:__vhost_get_user_slow() {
    ...
	ret = translate_desc(vq, (u64)(uintptr_t)addr, size, vq->iotlb_iov,
			     ARRAY_SIZE(vq->iotlb_iov),
			     VHOST_ACCESS_RO);
    ...
}

Since translate_desc() unconditionally requests VHOST_ACCESS_RO even for write operations via vhost_put_user(), could a guest bypass vIOMMU restrictions by mapping target memory as read-only and letting vhost_put_user() overwrite it?

Quoted 3 lines
drivers/vhost/vhost.c +0 -0
 	mutex_unlock(&vq->mutex);
 
 	return r;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1